[cfe-dev] Strange difference between Clang and GCC's -Wparentheses

Justin Bogner mail at justinbogner.com
Tue Jan 7 08:32:52 PST 2014

Chandler Carruth <chandlerc at gmail.com> writes:

> Currently Clang and GCC warn about:
> (a || b && c)
> Due to the very common mistake by programmers which ignores the relative
> operator precedence of || and &&. This forces the user to place explicit
> parentheses to group either (a || b) or (b && c) to be evaluated first.
> However, GCC warns and Clang is silent about this:
> (a || b && 1)
> Worse, the same is true for:
> (a || b && "foo")
> Now, I find Clang's rationale seems somewhat reasonable: the grouping doesn't
> change the truth table for this expression. However, I find this clever
> interpretation problematic for several reasons:
> 1) It's a complex rule to teach programmers with dubious gains. This
> exception doesn't realistically make the warning's false positives
> rare, it just eliminates one common (but not the only common)
> pattern. On the flip side, it is not a trivial rule to teach or
> predict for programmers. *Any* constant that we can evaluate to true
> will satisfy this.

I haven't looked at this code. Does this special case add much/any

> 2) ...

Ignoring... :)

> 3) It makes it annoying to track warnings in both Clang and GCC. If
> you mostly develop with Clang, but someone else checks out your code
> with GCC it'll suddenly start warning (much like it just did in a
> recent commit.)

IMHO, this is the strongest argument. I have no problem with clang
accepting this kind of code, but having to add parentheses here isn't as
bad as having to turn off -Wparentheses. Disabling the warning could
easily read toa real bug when somebody changes something about the
condition in the future. If this also reduces code complexity that's a

> Thoughts? I'm happy to fix this, the change is trivial in SemaExpr.cpp, but
> wanted to get some confirmation from Richard and Argyrios who have worked on
> this warning that such a change would be OK.
> -Chandler
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

More information about the cfe-dev mailing list