[Bug c/89127] missing -Wtype-limits for trivially false expressions

2019-01-31 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89127

--- Comment #3 from Martin Sebor  ---
I see what you mean.  It might perhaps be useful to mention the bigint rule of
thumb in the manual.  At the same time, the warning still doesn't work even
under this restricted interpretation.  For example, in:

  void f (unsigned char i)
  {
if (2 * i > 2 * UCHAR_MAX)
  __builtin_abort ();
  }

the comparison is trivially false due to the limited range of i's type.  GCC
sees that and in EVRP eliminates it, but without issuing the warning (or a
warning).

That said, the distinction is rather subtle between (i * i < 0) being false
because of multiplication's mathematical properties and (2 * i > 2 * UCHAR_MAX)
being false because of i's limited range.  As a user, my expectation is to get
a warning for comparisons that cannot be true, especially in controlling
expressions of conditional and iteration statements.  Whether that's because of
a limited range of the type of the expression or for some other reason is
unimportant.  What matters to me is that the code I wrote is not dead.

Incidentally, besides the warning not working as I expect, the test cases in
comment #0 were partly prompted by the implementation of the warning in
vr-values.c where I had initially assumed some of these expressions might be
handled.  Obviously, they're not because, as the original dump shows, they
never make it anywhere near VRP, but I hadn't taken the time to come up with
test cases that do make it there.  I have now spent some more time trying to
come up with one but so far no luck.  It doesn't even look like anything in the
test suite exercises that code, even with -Wtype-limits enabled by default.

Here are a few other trivial examples where a warning would be helpful (and,
IIUC, should be issued even under the strict interpretation):

  extern unsigned char i;

  if (i >> 31 != 0) __builtin_abort ();
  if (i / 2 > 200) __builtin_abort ();
  if (i & 0x100 != 0) __builtin_abort ();

And here's one where GCC not only doesn't warn, the trunk doesn't even fold it
anymore (GCC 8 does fold it; this is due to pr89143):

  void f (signed char i)
  {
if (__builtin_abs (i) > SCHAR_MAX + 1)
  __builtin_abort ();
  }

[Bug c/89127] missing -Wtype-limits for trivially false expressions

2019-01-30 Thread glisse at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89127

--- Comment #2 from Marc Glisse  ---
The warning is not about all comparisons that are always true/false, only about
the case where this is "due to the limited range of the data type" (let me
stress **type**). Only the first of your examples has that property (it can be
rewritten as x

[Bug c/89127] missing -Wtype-limits for trivially false expressions

2019-01-30 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89127

Martin Sebor  changed:

   What|Removed |Added

   Keywords||diagnostic
   See Also||https://gcc.gnu.org/bugzill
   ||a/show_bug.cgi?id=89126
  Known to fail||4.3.5, 4.4.7, 4.8.5, 4.9.4,
   ||5.4.0, 6.5.0, 7.3.0, 8.2.0,
   ||9.0

--- Comment #1 from Martin Sebor  ---
This never worked.  See also bug 89126.