Bug 45610 - misleading warning when && operator paired with a constant - is it operator precedence?
Summary: misleading warning when && operator paired with a constant - is it operator p...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.3.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-09 10:19 UTC by Alexander Stohr
Modified: 2014-03-24 21:55 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Stohr 2010-09-09 10:19:13 UTC
gcc --version
gcc (GCC) 4.3.1
Copyright (C) 2008 Free Software Foundation, Inc.

found in package cairo-1.9.14 and file test/cairo-test.c with critical line 1597:
  if (ctx->thread == 0 && ! RUNNING_ON_VALGRIND)

build outputs:
"warning: logical '&&' with non-zero constant will always evaluate as true"

from the used setup the VALGRIND macro is assumed to have a true value.
(a code checker would probably suggest using round braces around the "==" part.
the used default project tuning of gcc does not raise a warning on operator precedence.)

whats the problem?
a) if you use && with a constant and something else
then your result will always either be false (const=false) or something else (const=true).
there is no case for the compiler seeing this as fixed to true.
b) maybe its unintended operator precedence that makes the case behave strangely
Comment 1 jsm-csl@polyomino.org.uk 2010-09-09 12:20:50 UTC
Subject: Re:   New: misleading warning when && operator paired
 with a constant - is it operator precedence?

You don't give a self-contained testcase whose behavior with different 
versions can be compared, but the warnings now are

      if (or_op)
        warning_at (location, OPT_Wlogical_op, "logical %<or%>"
                    " applied to non-boolean constant");
      else
        warning_at (location, OPT_Wlogical_op, "logical %<and%>"
                    " applied to non-boolean constant");

and the reason is

  /* Warn if &&/|| are being used in a context where it is
     likely that the bitwise equivalent was intended by the
     programmer. That is, an expression such as op && MASK
     where op should not be any boolean expression, nor a
     constant, and mask seems to be a non-boolean integer constant.  */

Comment 2 Manuel López-Ibáñez 2010-09-09 13:15:53 UTC
We need a testcase

http://gcc.gnu.org/bugs/minimize.html

but I am pretty sure this is not warned anymore in GCC 4.6 (and probably GCC 4.4 and 4.5)
Comment 3 Marek Polacek 2014-03-24 21:55:36 UTC
Thus should be fixed.