r373743 - [NFCI] Improve the -Wbool-operation's warning message

Arthur O'Dwyer via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 8 10:09:22 PDT 2019


On Mon, Oct 7, 2019 at 6:58 PM Dávid Bolvanský <david.bolvansky at gmail.com>
wrote:
>> FWIW I found the "always evaluates to 'true'" bit important to
>> understand the warning.
>
> Yeah. I moved this check somewhere else, so we can print precise message:
> r373973 should emit "bitwise negation of a boolean expression always
> evaluates to 'true'; did you mean logical negation?" where possible.
> In the suspicious case like int i = ~b there is a general message
> "bitwise negation of a boolean expression; did you mean logical
> negation?".
>
> I like it now. What do you think? fine for you?

I see. Yes, all the cases I tried produce appropriate diagnostics. I like
it!

Hm, there is no "bitwise negation of a boolean expression always
> evaluates to 'true'; did you mean logical negation?" for chromium
> case [ https://bugs.chromium.org/p/chromium/issues/detail?id=1011810 ].
> I will try to fix it.


The important part there seems to be that the result of `~b` (which must be
either -1 or -2) is used as the operand to `!=` or `==`.
My opinion is that it is not worth the extra complication just to improve
the error message for this case.
It would be interesting to do some kind of *general*-purpose dataflow
before emitting diagnostics...
I notice that Clang's optimizer is smart enough to optimize
    bool foo(bool a, bool b) {
        return a == ~b;
    }
    bool bar(int x) {
        return x + 1 < -INT_MAX;
    }
into `return 0`. If it could propagate that information up and produce a
diagnostic, users might appreciate that. But the challenge as always is
that we can never tell if the user might sometimes be doing that sort of
thing on purpose (in inlined code, in macros, in generated code, etc).

my $.02,
–Arthur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191008/0282dd01/attachment.html>


More information about the cfe-commits mailing list