<< Back to previous view

[PCC-359] cpp does not leave comments in expanded macros, even with -CC Created: 22/Jun/11  Updated: 26/Sep/12

Status: Closed
Project: pcc
Component/s: C preprocessor
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Iain Hibbert Assignee: Iain Hibbert
Resolution: Fixed Votes: 0
Environment: NetBSD/i386

File Attachments: File diff2    

comments are discarded inside macro expansions, even when -CC is
passed to the preprocessor (or front-end), this is important for use with
lint which likes to take note of comments such as in the following macro

#define macro(x) do { foo(x); } while (/*CONSTCOND*/0)

in order to disable warnings about constant conditionals..

"gcc -C" retains comments except in macro expansions
"gcc -CC" retains all comments

I marked this as Minor, since in the context of a C compiler this is not
an essential feature, but lint is a useful tool especially so for pcc which
does not contain as much code analysis as some other compilers

Comment by Iain Hibbert [ 08/Jul/11 10:49 PM ]
I thought about this some more because I surely recalled a previous issue which
referenced comments inside macros.. and I found that if I make a macro that
references that macro:

#define foo(x) do { _foo_ ## x (); } while (/* CONSTCOND */0)
#define bar foo(Y)


then cpp outputs

do { _foo_X (); } while (0);
do { _foo_Y (); } while (/* CONSTCOND */0);

.. which is kind of kooky

I tracked this down to the insblock() function which attempts to process CMNT from sloscan(),
but in fact if readmac == 0 then this is never returned.. applying the attached patch seems to
fix it (and make lint in src/lib/libc proceeds to completion), but I'm not sure if the readmac++/--
ought to be inside the insblock() function so that it is always covered, or if that (or this) would
cause some other side effect..
Comment by Iain Hibbert [ 09/Jul/11 10:51 AM ]
Actually that was not sufficient as I found another failure relating to
loss of /* CONSTCOND */ from a plain macro, shown by

#define BAZ do { _foo_(); } while (/* CONSTCOND */0)

the diff2 attached places the readmac ++/-- into insblock(), so that
comments are kept (as appropriate) during macro expansions.. with
that, my build script completes all of src/lib with MKLINT=yes
Comment by Iain Hibbert [ 27/Jul/11 05:13 PM ]
ragge says my fix is correct, and I have applied it
Comment by Iain Hibbert [ 08/Aug/12 08:29 PM ]
I reopen this as I found another instance where it does not work but no time to look into it at present

#define foo(a) (111 /*LINTED*/ a 111)
#define bar(a) (222 a /*LINTED*/ 222)


with "pcc -E -Wp,-CC" produces

(111 /*LINTED*/ FIRST 111);
(222 SECOND /*LINTED*/ 222);
(111 /*LINTED*/ (222 THIRD 222) 111);
(222 (111 FOURTH 111) /*LINTED*/ 222);

and with "gcc -E -Wp,-CC" produces

(111 /*LINTED*/ FIRST 111);
(222 SECOND /*LINTED*/ 222);
(111 /*LINTED*/ (222 THIRD /*LINTED*/ 222) 111);
(222 (111 /*LINTED*/ FOURTH 111) /*LINTED*/ 222);

so note, that pcc does not provide the comment expanding the nested macro for some reason.. this does cause a further problem with lint when the /*LINTED*/ comment is lost, sigh..
Comment by Iain Hibbert [ 26/Sep/12 08:32 PM ]
have extended my previous fix, to solve the extended issue
Generated at Sat Oct 21 12:18:30 CEST 2017 using JIRA Enterprise Edition, Version: 3.13.1-#333.