On 09/12/16 21:30, Jeff Law wrote: > On 09/02/2016 12:53 PM, Bernd Edlinger wrote: >> Hi! >> >> As reported in PR77434 and PR77421 there should be a warning for >> suspicious uses of conditional expressions with non-boolean arguments. >> >> This warning triggers on conditional expressions in boolean context, >> when both possible results are non-zero integer constants, so that >> the resulting truth value does in fact not depend on the condition >> itself. Thus something like "if (a == b ? 1 : 2)" is always bogus, >> and was most likely meant to be "if (a == (b ? 1 : 2))". >> >> >> Boot-strap and reg-testing on x86_64-pc-linux-gnu without regressions. >> Is it OK for trunk. >> >> >> Thanks >> Bernd. >> >> >> changelog-pr77434.txt >> >> >> gcc: >> 2016-09-02 Bernd Edlinger <bernd.edlin...@hotmail.de> >> >> PR c++/77434 >> * doc/invoke.texi: Document -Wcond-in-bool-context. >> >> PR middle-end/77421 >> * dwarf2out.c (output_loc_operands): Fix assertion. >> >> c-family: >> 2016-09-02 Bernd Edlinger <bernd.edlin...@hotmail.de> >> >> PR c++/77434 >> * c.opt (Wcond-in-bool-context): New warning. >> * c-common.c (c_common_truthvalue_conversion): Warn on integer >> constants in boolean context. >> >> testsuite: >> 2016-09-02 Bernd Edlinger <bernd.edlin...@hotmail.de> >> >> PR c++/77434 >> * c-c++-common/Wcond-in-bool-context.c: New test. > For some reason the non-symmerty of the changes to > c_common_truthvalue_conversion caused me to have to think far more about > this than I probably should have. > > Couldn't we have a new function > integer_zerop_or_onep > > Then use > > && (!integer_zerop_or_onep (TREE_OPERAND (expr, 1)) > || !integer_zerop_or_onep (TREE_OPERAND (expr, 2))) > > Ie, if they're both constants and either is not [0,1], then we warn. > > With that cleanup, this is OK. > > jeff >
Yes, I will update the patch accordingly. I agree, a statement like "if (x ? 0 : 2)" can be called suspicious as well, even if the result of x is not ignored as in "if (x ? 1 : 2)". After I posted this patch, I extended the patch to cover also suspicious int shift-left operations, at https://gcc.gnu.org/ml/gcc-patches/2016-09/msg00155.html [PATCHv2] Add a warning for suspicious use of conditional expressions in boolean context Therefore, I would like to rename the warning to -Wint-in-bool-context so that the other patch can extend that instead of creating even more warning names with much longer names. Is renaming the warning OK as well, or could you have a look at the follow-up patch please? Thanks Bernd.