I have a need to strip "some" of the comments from a C file. The comments can be C or C++ style and can ofcourse be part, single or multi line comments. As in

/* usual C style comment */
int i;   /* this is going to count things */

// a single line comment
// which can be followed by one or more

/* long comments
    over two or
    even more lines

To simply remove them with a regex is reasonably simple. There is a twist in the tail with my question. I need to preserve the line numbers of the remaining code with the use of the C pre-processor directives

#line constant

These work by having the line number as the constant and hold the line number from the original file.

For single line comments this can be done by replacing them with a blank line. If there is only one single line comment and the line before and after is not already blank. In which case I need to insert the directive. For Multi-line comments the lines must be removed not just space filled as it looks way to ugly! A directive needs to replace the lines removed also taking into account any existing blank line before of after.

I have a rather messy Perl script that reads in the files with a while loop counting the lines as I go. It can remove the multi line comments and do the directive but the blank lines before and after are the real problem. I have tried a few things but it gets very messy too fast with flags and counters all over the place. I have also tried slurping the entire C file into an array so i can traverse backwards and forwards through the file to pick off the blank lines before any comments.

One good thing, there are no embedded comments of any combination.

> For Multi-line comments the lines must be removed not just space filled as it looks way to ugly!
And a file full of #line directives isn't ugly?

Seriously, is anyone going to READ your post-processed code?

Do it in several steps
- replace each comment character with a space
- delete all / +$/ (all trailing white space)
- collapse adjacent blank lines with #line

This article has been dead for over six months. Start a new discussion instead.