Bug 7543 - no warning for always-false "if (!a & 0x4)" bitwise and on boolean value
Summary: no warning for always-false "if (!a & 0x4)" bitwise and on boolean value
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 2.96 (redhat)
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: diagnostic, patch
: 9944 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-08-08 09:06 UTC by trt
Modified: 2018-04-05 02:01 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-12-10 05:44:12


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description trt 2002-08-08 09:06:00 UTC
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.
Comment 1 trt 2002-08-08 09:06:00 UTC
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
Comment 2 Wolfgang Bangerth 2003-03-07 02:48:27 UTC
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
Comment 3 Wolfgang Bangerth 2003-03-10 14:46:47 UTC
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
Comment 4 Wolfgang Bangerth 2003-03-10 20:50:11 UTC
State-Changed-From-To: feedback->analyzed
State-Changed-Why: I checked it, the patch is not applied.
Comment 5 Andrew Pinski 2003-10-30 06:58:33 UTC
Note the patch still needs a changelog and testcases and maybe a copyright assignment.
Comment 6 Andrew Pinski 2004-07-20 15:20:01 UTC
*** Bug 9944 has been marked as a duplicate of this bug. ***
Comment 7 Giovanni Bajo 2004-07-20 15:31:42 UTC
PR 9944 has another tentative patch for this bug.
Comment 8 Tom Truscott 2004-10-01 14:14:10 UTC
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.)
Comment 9 Wolfgang Bangerth 2004-10-01 14:25:22 UTC
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. 
Comment 10 Joseph S. Myers 2004-10-01 15:28:19 UTC
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.

Comment 11 Manuel López-Ibáñez 2007-01-20 18:06:42 UTC
Any news on this? Is this an important warning?
Comment 12 John Levon 2007-01-21 03:15:19 UTC
It's been a cause of bugs time and time again, so yes, it's useful and important.
Comment 13 Manuel López-Ibáñez 2008-08-23 13:49:49 UTC
(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.
Comment 14 Manuel López-Ibáñez 2008-10-24 10:10:32 UTC
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

Comment 15 Manuel López-Ibáñez 2008-10-24 10:12:49 UTC
This is FIXED in GCC 4.4