gcc currently does not warn about the logic error in the following program: int subr(int flags) { return (!flags & 0x80) ? 1 : 0; } It should have been (!(flags & 0x80)) Release: gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81) How-To-Repeat: Compile the above program, with -W -Wall, whatever. No warning message.
Fix: A patch to gcc which gives a warning in this situation appears in http://gcc.gnu.org/ml/gcc-patches/2002-06/msg01523.html
State-Changed-From-To: open->feedback State-Changed-Why: Tom, did your patch get applied to CVS? What's the state of this? Thanks Wolfgang
From: Wolfgang Bangerth <bangerth@ticam.utexas.edu> To: gcc-gnats@gcc.gnu.org Cc: Subject: Re: c/7543: Suggest new warning message for gcc Date: Mon, 10 Mar 2003 14:46:47 -0600 (CST) ---------- Forwarded message ---------- Date: Fri, 7 Mar 2003 11:43:55 -0500 (EST) From: Tom Truscott <trt@unx.sas.com> To: bangerth@dealii.org Subject: Re: c/7543: Suggest new warning message for gcc > Tom, did your patch get applied to CVS? What's the state of > this? I don't know if it was applied, but I strongly doubt it. The state of this is totally defunct. I have tried different request strategies for a few years and have concluded that only if I become a gcc insider can I get even the simplest changes made. I don't have the time, energy, or interest in that. I'm content with the dozens of a warnings I've added to my personal copy of gcc, which I run daily to spot problems in my company's product (20M+ LOC). If there is a gcc insider who would like to shepherd specific warning changes I will be happy to supply them. Beyond that I'm not interested, not even in discussing this, so feel to mark this bug NOFIX or whatever. Thanks, Tom Truscott
State-Changed-From-To: feedback->analyzed State-Changed-Why: I checked it, the patch is not applied.
Note the patch still needs a changelog and testcases and maybe a copyright assignment.
*** Bug 9944 has been marked as a duplicate of this bug. ***
PR 9944 has another tentative patch for this bug.
Can we just close this 'wonfix'? There are so many simpler/effective warning message improvements that are available to gcc. (e.g. the special case of boolean & non-boolean-constant can be caught with a three-line addition to fold() in fold-const.c) This elaborate patch would be way way down any list. Thanks. (By the way, if there a gcc person interested in such a list, please contact me.)
As the example you show in your patch demonstrates, there are even cases inside gcc that would be triggered by this patch. I believe it would therefore be useful. Also, jsm's response was not dismissive of the patch at all, though we would indeed have to have a Changelog entry as well as testcases. Joseph, would you mind commenting on the usefulness of an approach as mentioned in the patch linked from comment #1? W.
Subject: Re: no warning for always-false "if (!a & 0x4)" bitwise and on boolean value On Fri, 1 Oct 2004, bangerth at dealii dot org wrote: > Joseph, would you mind commenting on the usefulness of an approach as mentioned > in the patch linked from comment #1? It seems reasonable to me - and as a -Wparentheses warning it avoids the question of whether the fold-const.c warnings really should be mandatory. The patch would need updating for the removal of C_EXP_ORIGINAL_CODE and its replacement by c_expr structures. The patch should cause the original_code be set appropriately (in the parser for now) where this patch requires it. gcc.dg/Wparentheses-[2-8].c give examples of testing -Wparentheses warnings: test all relevant combinations of operators, both parenthesised and non-parenthesised, with constant and non-constant operands, and make sure each case does or does not warn as appropriate.
Any news on this? Is this an important warning?
It's been a cause of bugs time and time again, so yes, it's useful and important.
(In reply to comment #8) > Thanks. (By the way, if there a gcc person interested in such a list, please > contact me.) I am interested in such a list. Thanks in advance.
Subject: Bug 7543 Author: manu Date: Fri Oct 24 10:09:06 2008 New Revision: 141340 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=141340 Log: 2008-10-24 Manuel López-Ibáñez <manu@gcc.gnu.org> PR c/7543 * value-prof.c (gimple_stringop_fixed_value): Use parentheses around bit operation. * profile.c (is_edge_inconsistent): Likewise. * fold-const.c (truth_value_p): Move from here... * tree.h (truth_value_p): ... to here. * c-tree.h (c_expr): Update description of original_code. * c-typeck.c (parser_build_unary_op): Set original_code. (parser_build_binary_op): Update call to warn_about_parentheses. * c-common.c (warn_about_parentheses): Take two additional arguments of the operands. Use a switch. Quote operators appropriately. Define macro APPEARS_TO_BE_BOOLEAN_EXPR_P. Add warning about !x | y and !x & y. * c-common.h (warn_about_parentheses): Update declaration. cp/ * typeck.c (build_x_binary_op): Update call to warn_about_parentheses. * parser.c (cp_parser_binary_expression): Add note about passing the correct code for unary expressions. testsuite/ * gcc.dg/Wparentheses-11.c: New. * g++.dg/warn/Wparentheses-25.C: New. XFAILED. Added: trunk/gcc/testsuite/g++.dg/warn/Wparentheses-25.C trunk/gcc/testsuite/gcc.dg/Wparentheses-11.c Modified: trunk/gcc/ChangeLog trunk/gcc/c-common.c trunk/gcc/c-common.h trunk/gcc/c-tree.h trunk/gcc/c-typeck.c trunk/gcc/cp/ChangeLog trunk/gcc/cp/parser.c trunk/gcc/cp/typeck.c trunk/gcc/fold-const.c trunk/gcc/profile.c trunk/gcc/testsuite/ChangeLog trunk/gcc/tree.h trunk/gcc/value-prof.c
This is FIXED in GCC 4.4