Thread: c preprocessor output issue with comments after #include

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    3

    c preprocessor output issue with comments after #include

    Why can't I add add a comment after a c directive ?
    For eg.
    I added the following line in a .c file

    #include <a.h> /* test comment*/

    I tried the c preprocessor (gcc -E) to output the preprocessed output.

    I am using 3.4.6 version of gcc
    The preprocessor output with the following options -C -dI -dD -undef didn't give me the comment after #include
    It only outputs if I add the comment in the next line as follows.
    #include <a.h>
    /* test comment*/

    It works as expected when I tried it out with an older version of gcc - 2.95.4
    Does this mean something is broken in the newer version of gcc or a new option is required to output the comment after the directive.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    well since #include's arent really C, the #include line may be terminated by a newline, that is, everything after the #include is considered the statement. therefore no comments

    i dont know though and i dont think it really matters, just put the comment above the line where it should be

    if this is the most difficult part of your program then id be very happy

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    This really matters to me since I have a code generation tool where it uses the preprocessed o/p. Currently this is one way to pass on some info to the tool to do some validation/control the generated code. For eg. I could say #include <a.h> /* ignore */
    The 'ignore' key word is checked in the code generation tool to control the generated code.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The following are the first four (of eight) phases of translation specified in the C Standard:

    Trigraph Replacement - The preprocessor replaces trigraph sequences with the characters they represent.
    Line Splicing - Physical source lines that are continued with escaped newline sequences are spliced to form logical lines.
    Tokenization - The preprocessor breaks the result into preprocessing tokens and whitespace. It replaces comments with whitespace.
    Macro Expansion and Directive Handling - Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands macros and, in the 1999 version of the C standard, handles _Pragma operators.
    C preprocessor - Wikipedia, the free encyclopedia

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    cant you just change how this code generation tool works so to get around this problem? ie, have it look elsewhere for the 'ignore' keyword?

    id also like to know what this means

    It works as expected when I tried it out with an older version of gcc - 2.95.4
    what does "work as expected" mean? what do you intend to happen when you place a comment after a #include? because the #include is just a way to say "copy stuff in here later", so the only thing that makes sense (from the preprocessor's side) would be to expand out the #include and put the comment after it. but you seem to want something else...

    edit: you could just compile with older versions of gcc

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    When I used gcc - 2.95.4 with '-E -C' option, even if I have comment in the same line of the c directive it gives me the comment in the output, #include <a.h> /* ignore */ gives me /* ignore */ in the o/p.
    When I upgrade the gcc to 3.4.6, the comment disappears.
    I need to go to the latest gcc version for some other reason. I have no option.
    I was wondering why would the latest gcc behaves differently here with the same options.
    Anyway, if I don't have any other solution, I will have to change the code generator.
    The requirement for me is to control the output as follows.
    If I say #include <a.h> /* ignore */ , that would mean, preprocess a.h, but I don't want "#include <a.h>" in the output.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Does this mean something is broken in the newer version of gcc or a new option is required to output the comment after the directive.
    Or something was broken in the older version, and it has now been fixed.

    The C standard simply states that ALL comments are deleted at the same time that pre-processor directives are processed. Which means that ANY compiler option which aims to preserve comments after this stage is by their nature "implementation specific" (in other words, you're on your own if the goal posts move).

    Here's what Using and Porting the GNU Compiler Collection (GCC): Invoking GCC has to say about -C
    -C
    Tell the preprocessor not to discard comments. Used with the `-E' option.
    And the latter Preprocessor Options - Using the GNU Compiler Collection (GCC)
    -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 `#'.
    -CC
    Do not discard comments, including during macro expansion. This is like -C, except that comments contained within macros are also passed through to the output file where the macro is expanded.

    In addition to the side-effects of the -C option, the -CC option causes all C++-style comments inside a macro to be converted to C-style comments. This is to prevent later use of that macro from inadvertently commenting out the remainder of the source line.

    The -CC option is generally used to support lint comments.
    See - there is a lot more detail describing what goes on (it's called product improvement).
    Though I was somewhat surprised that the -CC option doesn't preserve such a comment either.

    You could always submit a bug report to the GCC team.

    Or failing that, grab the source code for the compiler and hack it to restore what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use this part of dirent.h
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 01-31-2009, 08:51 AM
  2. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  3. STL link issue.
    By Hulag in forum C++ Programming
    Replies: 1
    Last Post: 02-18-2005, 04:02 PM
  4. Dialog Will not appear?
    By curlious in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2003, 10:32 AM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM

Tags for this Thread