Giter VIP home page Giter VIP logo

Comments (8)

hebaishi avatar hebaishi commented on July 17, 2024

Here's a perl script to make that change:

change_header.pl

#!/usr/bin/perl
open INFILE, "<$ARGV[0]" or die "Unable to open <$ARGV[0]: $!\n";
@lines = <INFILE>;
close (INFILE);

$header_name = uc($ARGV[0]);
$header_name =~ s/\./\_/g;

@output = ();

$pragma_flag = 0;

foreach $line(@lines) {
  if ($line =~ m/\#pragma\s+once/gi) {
    push @output, "#ifndef $header_name\n";
    push @output, "#define $header_name\n";
    $pragma_flag = 1;
  }
  else {
    push @output, $line;
  }
}
if ($pragma_flag == 1){
  push @output, "#endif\n";
}

open OUTFILE, ">$ARGV[0]" or die "Unable to open >$ARGV[0]: $!\n";
foreach $line(@output) {
  print OUTFILE $line;
}
close (OUTFILE);

This is how the script works. Note that it overwrites the file.

$ cat header.h
#pragma once
Line 1
Line 2
Line 3
$ ./change_header.pl header.h
$ cat header.h
#ifndef HEADER_H
#define HEADER_H
Line 1
Line 2
Line 3
#endif

And this is how you run it on all your files.. Make a backup first though, and do a test run with a dummy file to make sure everything works as expected!!

find . -name \*.h -exec ./change_header.pl {} \;

from linter-gcc.

hebaishi avatar hebaishi commented on July 17, 2024

Hello. Has your issue been resolved?

from linter-gcc.

riesinger avatar riesinger commented on July 17, 2024

Well technically yes, the script works, but why does gcc warn me about using pragma once?

from linter-gcc.

hebaishi avatar hebaishi commented on July 17, 2024

The reason is that although #pragma once is widely supported, it is still a non-standard feature. You can read more about this here. What I do in these scenarios is switch to a standard feature to remove the warning. I think it's a good idea to favour a clean build (one with no warnings, even with all warning options switched on) as it helps you to identify problems easily. However, the choice is entirely yours.

from linter-gcc.

riesinger avatar riesinger commented on July 17, 2024

Ah thank you very much, I actually didn't know that. I don't know why it would be non-standard, as it is much more practical than the default include guards... Whatever.

from linter-gcc.

kitlith avatar kitlith commented on July 17, 2024

Sorry for the bump, but saying that #pragma once is a non-standard feature is irrevelant to the warning, @hebaishi. The warning is because gcc is seeing one file being passed to it, the header file with the #pragma once in it, and it 'thinks': "Hey, this is a main file, why does it have a pragma that tells me not to include it more than once? I'll let the developer know about this."

This warning is a problem with how the linter works, it passes the file you are working on as the main file to gcc. In this case, I would reccomend passing the flag to disable the #pragma once warning to gcc, -Wno- ... wait a moment, this flag doesn't exist.

I'd recomend re-opening this issue, or perhaps I can open a new issue that references this one.

My thoughts for fixing this -- you could create a file that includes the header file you're trying to lint, or you could match on that warning and remove it. I'd recomend the former, though the latter might be useful as a configuration option so that users can remove warnings that they can't disable.

Example commands for the former:

gcc -c -fsyntax-only myheader.h

as opposed to:

echo '#include "myheader.h"' | gcc -x c++-header -c -fsyntax-only -

Reference: http://stackoverflow.com/questions/16726352/is-is-possible-to-disable-this-warning-in-clang-warning-pragma-once-in-main-f

from linter-gcc.

hebaishi avatar hebaishi commented on July 17, 2024

@KitLing thanks for the useful info. I think the second option would be better: it avoids the creation of a temporary file, and adds flexibility so that the user can potentially remove other warnings (if there are any) that can't be disabled.

from linter-gcc.

kitlith avatar kitlith commented on July 17, 2024

Well, piping the #include to gcc avoids the temporary file, which is why I provided a (shell) example demonstrating that.

I do agree that the second option would give extra flexibility, but it could also encourage people to block specific warnings that do have flags to disable them. And, honestly, I'd prefer the first method because it's better to just plain not generate the spurious warning than to filter it out.

Doesn't mean we can't have both, just would reccomend warnings around the second option.

from linter-gcc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.