GCC preprocessor removing comments - gcc

Is it possible to instruct the GCC preprocessor not to remove comments when processing files?

GCC has the -C option to preserve comments.
`-C'
Do not discard comments. All comments are passed through to the
output file, except for comments in processed directives, which
are deleted along with the directive.
You should be prepared for side effects when using `-C'; it causes
the preprocessor to treat comments as tokens in their own right.
For example, comments appearing at the start of what would be a
directive line have the effect of turning that line into an
ordinary source line, since the first token on the line is no
longer a `#'.

Yes, you can do it with the -C option. E.g.
gcc -C -E myfile.c

man gcc and use / -C (slash, space, dash, capitcal C) to make less (which is probably your pager program) search for the -C option, use n to search again (the description is the 3rd hit). Related options are both above and below.

Related

Running GCC preprocessor -fdirectives-only *without* implicit -dD

Basically, I want to do what the title says.
I have some mildly complex header files, and I want to generate a single header file I can release as the public interface of a DLL.
I'm generating the header file as follows:
${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} -fdirectives-only -P \
-E ${CMAKE_CURRENT_SOURCE_DIR}/dll_header_base.h -o generated_public_header.h
If I run the compiler without -fdirectives-only, the precompiler is much more aggressive about stripping out contents of the header. The output of -fdirectives-only is almost what I want, but that particular flag forces the -dD flag on as well, which prepends all the compiler-defined macros to the generated header (450 lines of them!).
I don't really understand why -fdirectives-only forces -dD in the first place, and for my case, the later flag basically makes it useless. Looking at the options for -dLETTER, there also seems to be no way to turn it off.
How do I extract the directives-only preprocessed output without all the additional cruft?
Yes, I could pretty easily solve this with command line tools, but I want to keep the solution entirely constrained to the compiler/CMAKE. Eventually, I'd like to also support building on windows.

How do I use -Wall option on gcc/g++ and turn off the multi line comments warnings?

Like the title, how do I use -Wall option on gcc/g++ and turn off the multi line comments warnings?
The comment look like that:
// Comment starts here \
// and end here (the // at the begging is not necessary)
You could use /* .. */ for the multiline comment.
/* foo bar comment
lala blah
*/
Edit:
I found a solution in another post here: How can I hide "defined but not used" warnings in GCC?
If you add the option -Wno-comment the warning is gone.
gcc -Wall -Wno-comment test.c -o test
Its also explained here: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
best wishes,
Matthias
The GCC warnings are documented here: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Specifically, you're looking for:
-Wcomment
Warn whenever a comment-start sequence /*' appears in a/*' comment, or whenever a Backslash-Newline appears in a `//' comment. This warning is enabled by -Wall.
So to turn that warning off, you should be able to use -Wno-comment.
The backslash continuation induces the warning; it is not necessary if you start the next line with a comment start.
Remove the backslash continuation and replace the backslashes at the start of the line with // (as written, it is not a comment at all) and you should quell the warnings.
You can also write:
/\
\
* A regular C89 comment with trigraphs for good measure.
*??/
??/
/
and anyone who presented that code to me for review would be sent back to fix it immediately (but pity the poor compiler writer who has to handle such nonsense correctly!).
As to how to fix it, if (as is mentioned in a comment), it is prevalent then you can see whether your version of GCC will support -Wno-comment to suppress warnings about multiline comments. That is probably simplest. Failing that, you have to decide whether you can risk using a heuristic parser to fix the problem:
sed -e 's%\(//.*\)\\$%\1%' -i.bak *.c
(assuming GNU sed for the -i.bak option). This removes the trailing backslash from any such comment. The main place where I could see this causing trouble is in a macro definition such as:
#define MACRO(c,d) { (c) = (d) + 1; } // Comment here \
more comment here
It's an inane macro, but because there is no comment-start on the second line of the comment, the heuristic substitution in the sed script would give you a syntactically invalid program (unless you just so happened to have macros such that 'more comment here' expands to valid code, which is pretty unlikely).
If the heuristic mechanism won't work, then maybe you need a full C comment stripper. If you can't find one on the Internet somewhere, I have such a program (and its test files include comments such as the gruesome one shown above) — see my profile.

How to make gcc uses march=native as default?

Is there a way to change the specs file so that it will pass -march=native if nothing is specified in command line?
Related things in the default specs file is:
*cc1:
%(cc1_cpu)
*cc1_cpu:
%{march=native:%>march=native %:local_cpu_detect(arch) %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)}} %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}
I am not sure how specs works. Simply specifying -march=native before or after %(cc1_cpu) doesn't work. However, this line does take effect because GCC will report error if I put -something_wierd instead of -march=native.
Another thing I noticed is if I put %{march=i386:-something_wierd} before %(cc1_cpu), gcc reports error so looks like -march=i386 is always passed in if nothing is specified, so is there a way to distinguish between nothing specified and -march=i386 in specs file?
BTW, what does %> do? Seems like it is not specified in the documentation.
I am using MinGW's gcc-4.6.2.
Referring to your last question: The gcc 4.6.1 sources (gcc/gcc.c) contain the following comment on %>:
%>S Similar to "%<S", but keep it in the GCC command line.
For the sake of completeness following the comment for %< form the same file:
%<S remove all occurrences of -S from the command line.
Note - this command is position dependent. % commands in the
spec string before this one will see -S, % commands in the
spec string after this one will not.
To answer the first question in short: yes, but ....
... the only generic solution I found has the significant drawback that the -march option will be ignored, so every build is done as if -march=native had been specified. Anyhow there is a workaround to that.
1 The solution (without workaround)
Create a specs-file called let's say specs.nativealways containing:
*cc1_cpu:
%<march=* -march=native %>march=native %:local_cpu_detect(arch) %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)} %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}
When using the specs-file (for example by invoking gcc with the option -specs=specs.nativealways) the build will be done as if -march=native was specified (with the mentioned drawback that any occurrence of option -march=<arch> would have simply been ignored).
2 The workaround
To still by able to override the newly configured default behavior one can use a modified version of the specs-file described above, introducing a new option called -myarch using the same syntax as -march (except for -myarch=native, which won't work, which does not metter as native now is the default).
The modfied specs-file looks like this:
*cc1_cpu:
%<march=* %{myarch=*:%<myarch* -march=%* ; :-march=native %>march=native %:local_cpu_detect(arch) %{!mtune=*:%>mtune=native %:local_cpu_detect(tune)}} %{mtune=native:%>mtune=native %:local_cpu_detect(tune)}
PS: This has been tested with with gcc 4.6.2 on Linux, but should work on MinGW.
While not a direct answer to your question, you can reach a very similar effect by defining CFLAGS and CXXFLAGS in your shell's initialization file. 99% of the Makefiles are sufficiently standard to pick up the environment values and pass the flags to gcc.
*cc1_cpu:
+ %{!march*:-march=native}

Xcode -- Expand all macros in a file, without doing a full precompile?

I am trying to read some code that has a lot of macros in it. And often the macros are chained. Is there any way to see a version of the file where all the macros have been expanded -- without doing a full run of the preprocessor (which would also do stuff like expand #imports)? This would really help me read the code.
EDIT: Often the macros are defined in other files.
Not sure if there's a way to do this in Xcode, but you can use the compiler, specifically the -E option, which stops processing right after preprocessing.
cc -E foo.c
will print all the preprocessed results on stdout. And
cc -E foo.c -o foo.preproc
will dump the preprocessed output into foo.preproc.
As best I can tell, the answer to my question is that there is no way to do it. The best I can do is do a full precompile, then search for the part of the file that starts after all the #include statements.

Preprocessor output

How do I view the output produced by the C pre-processor, prior to its conversion into an object file?
I want to see what the MACRO definitions do to my code.
gcc -E file.c
or
g++ -E file.cpp
will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output.
Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output.
For Visual C++ the switch is /E which spits the preprocessor output to screen.
You can also call the C Preprocessor directly.
cpp infile outfile
Check out man cpp for more info.
For GCC,
gcc -E -dM file.c
or
g++ -E -dM file.cpp
should do the job. -dM, as GNU Preprocessor manual puts it, should generate a list of ‘#define’ directives for all the macros defined during the execution of the preprocessor, including predefined macros.
It depends on the compiler you use.
With GCC, you can specify the -E flag on the command-line to let the compiler produce the pre-processor output.
If using CLion by Jetbrains, you can use the action "clangd: Preprocess current TU"
So hit shift shift and start typing clangd...
Best assign it to a shortcut for simpler reuse in preferences->keymap:
Shout out to marcosbento
PS: TU means 'translation unit' (see here LLVM translation unit)
You can check out my script described here:
http://mosermichael.github.io/cstuff/all/projects/2011/09/16/preprocessor.html
It formats the preprocessor output into a (hopefully) readable html document: lines that are different due to preprocessor are marked in the file.

Resources