https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89127

            Bug ID: 89127
           Summary: missing -Wtype-limits for trivially false expressions
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

GCC appears to fold certain non-constant relational expressions into constants
very early when it determines their value based on the limited range of the
non-constant operand.  For instance, it folds (x * x < 0) to false for any
integer x.  This ability would suggest that GCC should likewise be able to
issue -Wtype-limits warnings for such expressions as the manual indicates it's
designed to do:

  Warn if a comparison is always true or always false due to the limited range
of the data type.

However, GCC does not issue the -Wtype-limits warning in these cases.  Either
the manual needs to be updated to make it clear that the warning doesn't
consider such expressions or (preferably) the warning should be enhanced to
detect these cases since they could be masking bugs.

$ cat u.c && gcc -S -Wall -Wextra -Wtype-limits
-fdump-tree-original=/dev/stdout u.c
void f (int x)
{
  if (x + __INT_MAX__ + 1 < 0)   // folded to false but no -Wtype-limits
    __builtin_abort ();

  if (x - x < 0)                 // ditto
    __builtin_abort ();

  if (x * x < 0)                 // ditto
    __builtin_abort ();

  if (__builtin_abs (x) < 0)     // ditto
    __builtin_abort ();
}


;; Function f (null)
;; enabled by -tree-original


{
  if (0)
    {
      __builtin_abort ();
    }
  if (0)
    {
      __builtin_abort ();
    }
  if (0)
    {
      __builtin_abort ();
    }
  if (0)
    {
      __builtin_abort ();
    }
}

Reply via email to