leco leco - 5 months ago 20
C Question

Is there a reliable tool for removing comments in ASM/C/C++ code?

I know this question has been asked before (e.g.: see Remove comments from C/C++ code), but I haven't found any satisfiable result.

I am parsing a set of complex C/C++ code that first must be normalized, which includes eliminating comments from the input source code.

All decommenting tools I have tried failed to a certain degree, and that includes:

  • decomment

  • stripcmt

  • cloc

Note: I have also tried "gcc -fpreprocessed -E", but it does not lead to a perfect result; the output has some weird macro annotations for keeping tack of certain lines of code.

To illustrate the problem with a particular tool (cloc), removing comments from this header file leads to removing non-comments as well, such as all the includes in the begining of that file.

That said, is there any reliable tool for comment removal that can be used in stripping out comments in exceptionally complex code?

Much appreciated.


if [[ "$#" != 1 ]] ; then
  echo "Usage: stripcomments input-file" > /dev/stderr

gcc -fpreprocessed -dD -E -P "$1" 2> /dev/null