[cfe-commits] [patch] Add -Wdangling-else-parentheses

Nico Weber thakis at chromium.org
Thu Dec 22 15:24:40 PST 2011


Hi Richard,

On Tue, Dec 20, 2011 at 6:10 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> However, based on GCC's history of not diagnosing this case, there is a
> significant quantity of real-world code which uses constructs like this in
> order to hide the warning on if-else constructs which come from inside macros:
>
>  #define FOO(X) \
>    switch (0) default: \
>      if (!(X)) \
>        HandleDisabledThing(); \
>      else
>        GetThing()
>
>  // ...
>
>  if (cond)
>    FOO(x) << "hello"; // no warning
>
> After some discussion off-line with Chandler, we have agreed that we think the
> warning should be suppressed if:
>
>  - Both the 'else' and the associated 'if' come from the same macro expansion
>  - The non-associated 'if' does not come from that macro expansion.

That example happens to work already, because the "default:" causes
ParseLabeledStatement() to be called, which doesn't track the
TrailingElseLoc.

I'm landing the CL as is with that code of yours added to the test.
The macro location heuristic you proposed above sgtm, and we can add
it if we come across code that needs it.

Thanks!

Nico




More information about the cfe-commits mailing list