[PATCH] -Wtautological-compare should be quiet on floats

Marek Polacek polacek@redhat.com
Wed Jul 29 14:24:00 GMT 2015


As discussed elsewhere, -Wtautological-compare shouldn't warn about
floating-point types because of the way NaN behave.

I've been meaning to commit this one as obvious, but I'm not sure
whether I should also use HONOR_NANS or whether I can safely ignore
that here.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2015-07-29  Marek Polacek  <polacek@redhat.com>

	* c-common.c (warn_tautological_cmp): Bail for float types.

	* c-c++-common/Wtautological-compare-3.c: New test.

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index caa801e..9456729 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -1910,6 +1910,12 @@ warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
       || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
     return;
 
+  /* Don't warn if either LHS or RHS has an IEEE floating point-type.
+     It could be a NaN, and NaN never compares equal to anything, even
+     itself.  */
+  if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
+    return;
+
   if (operand_equal_p (lhs, rhs, 0))
     {
       /* Don't warn about array references with constant indices;
diff --git gcc/testsuite/c-c++-common/Wtautological-compare-3.c gcc/testsuite/c-c++-common/Wtautological-compare-3.c
index e69de29..64807b0 100644
--- gcc/testsuite/c-c++-common/Wtautological-compare-3.c
+++ gcc/testsuite/c-c++-common/Wtautological-compare-3.c
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-options "-Wtautological-compare" } */
+/* Test we don't warn for floats.  */
+
+struct S { double d; float f; };
+
+void
+fn1 (int i, float f, double d, struct S *s, float *fp)
+{
+  if (f == f);
+  if (f != f);
+  if (d == d);
+  if (d != d);
+  if (fp[i] == fp[i]);
+  if (fp[i] != fp[i]);
+  if (s->f == s->f);
+  if (s->f != s->f);
+  if (s->d == s->d);
+  if (s->d != s->d);
+}

	Marek



More information about the Gcc-patches mailing list