[cfe-dev] Fix for preprocessor CommentHandler bug

Richard Smith richard at metafoo.co.uk
Mon Jun 18 11:49:14 PDT 2012


On Mon, Jun 18, 2012 at 9:50 AM, Andy Gibbs <andyg1001 at hotmail.co.uk> wrote:
> On Monday, June 18, 2012 2:20 AM, Richard Smith wrote:
>> It also looks like you've changed the behavior of this
>> code:
>>
>> #if 0
>> #if 1
>> #endif junk
>> #endif
>
> Well caught!  But it is also the case that other #directives inside an #if 0
> will also not provide these warnings.  Take the following snippet:
>
> #if 0 junk
> #if 1 junk
> #else junk
> #endif junk
> #endif junk
>
> Even without my patch, only the first #if and both #endifs produce
> diagnostics.  The way the preprocessor seems to be designed, in most cases
> it simply discards the whole line (comments included) following the
> preprocessor directive when inside a skipped block.  With the change I made,
> one might argue, the diagnostics from the preprocessor are moving towards
> more consistenty: i.e. inside a skipped #if block, there are (or should be)
> no diagnostics generated.  This matches nicely with the fact that we ignore
> comment handling inside skipped #if blocks (thinking ahead to the -verify
> enhancement...)
>
> But on the other hand, of course, I can easily revert the regression.

This is a bugfix, not a regression -- the new behavior is the one
mandated by all relevant standards:

"Each directive’s condition is checked in order. If it evaluates to
false (zero), the group that it controls is
skipped: directives are processed only through the name that
determines the directive in order to keep track
of the level of nested conditionals; the rest of the directives’
preprocessing tokens are ignored, as are the other
preprocessing tokens in the group."

> What do you think?

I'd like this split out into a separate patch with a testcase.




More information about the cfe-dev mailing list