2

I'd like to strip the comments from a c++ file. This post provides the following solution for this:

gcc -fpreprocessed -dD -E test.c

However, this command appears to collapse long multiline comments. This causes behavior differences if the macro __LINE__ is used.

Can the gcc solution be salvaged to not change __LINE__-dependent behavior? Or, a non-gcc solution would work fine too.

Example test.c:

int main() {
        /*
         *
         *
         *
         *
         * comment 1
         */
        // comment 2
        return 0;
}

Output using gcc 4.9.2:

$ gcc -fpreprocessed -dD -E test.c
# 1 "test.c"
int main() {
# 10 "test.c"
 return 0;
}

If we delete // comment 2, then we instead get the desired output without comment collapsing:

$ gcc -fpreprocessed -dD -E test.c
# 1 "test.c"
int main() {







 return 0;
}
Community
  • 1
  • 1
dshin
  • 2,354
  • 19
  • 29
  • 1
    Good point. Probably not. Ask the GCC mailing list. – Lightness Races in Orbit Jan 18 '16 at 18:00
  • 1
    Well, having such like _"`__LINE__`-dependent behavior?"_ sounds not to be a really clever idea. Can you elaborate that use case please? – πάντα ῥεῖ Jan 18 '16 at 18:04
  • Does it have to be a gcc solution? How about [a flex/c program](http://stackoverflow.com/questions/8886410/stripping-multiline-comments-in-c-with-regex/8886750#8886750)? – Beta Jan 18 '16 at 18:14
  • @πάνταῥεῖ Unfortunately explaining the use case will take quite a bit of effort. I fear that may derail an otherwise well-defined question. – dshin Jan 18 '16 at 18:35
  • @Beta It does not need to gcc. I'm not familiar with flex but I can look into your linked answer if something better doesn't show up. – dshin Jan 18 '16 at 18:36
  • @jxh I updated question details with gcc version and example input/output. – dshin Jan 18 '16 at 18:45
  • [decomment.text](https://github.com/vitaly-t/decomment) can do that, using option `space`. – vitaly-t Jan 21 '16 at 05:53

1 Answers1

1

In GCC, the # directives with the numbers and file name correspond to line numbers, and thus maintain proper __LINE__ values.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • Aha! I actually simplified my problem statement somewhat - it was not gcc's compiled output that was having `__LINE__` problems, but rather a python process that was reading the comment-stripped `gcc -fpreprocessed -dD -E` output and failing to match the original source line-by-line. Now that I know how to interpret the # directives, I can adjust my python script appropriately to match correctly. Thanks! – dshin Jan 18 '16 at 18:53