Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the comments generated by cpp

I use #include ".../frontend/tokens.mll" in lexer.mll, and then cpp -C -P frontend/lexer.mll -o frontend/lexer_new.mll to generate lexer_new.mll.

That worked until I upgraded my ubuntu from 12.04 to 14.04 yesterday.

The compilation gives an error:

ocamllex frontend/lexer_new.mll
File "frontend/lexer_new.mll", line 1, character 1: illegal character /.
make: *** [frontend/lexer_new.ml] Error 3

That is because in lexer_new.mll several lines of C comments have been inserted in the beginning:

/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   ... */

I don't remember if same comments were generated before upgrading.

Does anyone know how to get ride of these comments?

PS: the gcc version is : gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

like image 220
SoftTimur Avatar asked Aug 14 '14 18:08

SoftTimur


1 Answers

UPDATE: It's possible that pcu's answer is more correct than this one.

You can use cpp with flag -nostdinc and do not use flag -C.

I'll leave this answer here.


Omitting the -C option appears to inhibit the inserted copyright message.

From the documentation:

'-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 '#'.

Comments in the source code are discarded by default. The -C option causes them to be passed through. Apparently in recent versions it also inserts that copyright message.

This might have other effects, good or bad. If -C was working for you before, it may be that some things that looked like C comments in your OCaml code were being passed through from lexer.mll to lexer_new.mll; omitting -C would cause them to be removed. If that's an issue, you might want to keep the -C option and add a filter after the preprocessor that removes the added comment. (Writing such a filter is left as an exercise.)

More information: running

cpp -C /dev/null

indicates that the copyright comment comes from /usr/include/stdc-predef.h:

$ cpp -C /dev/null
# 1 "/dev/null"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
[39 lines deleted]
# 1 "/dev/null"
$

(The -P option was inhibiting the # directives that indicate where the text came from.) Apparently that file is included by default when preprocessing C source. It defines a few C-specific predefined macros such as __STDC_IEC_559__ and __STDC_ISO_10646__.

Why were you using -C for non-C code?

like image 107
Keith Thompson Avatar answered Oct 20 '22 02:10

Keith Thompson