[C/C++ PATCH] Implement -Wtautological-compare (PR c++/66555, c/54979)

Marek Polacek polacek@redhat.com
Wed Jul 22 20:58:00 GMT 2015


On Wed, Jul 22, 2015 at 01:48:03PM -0600, Martin Sebor wrote:
> On 07/22/2015 01:06 PM, Marek Polacek wrote:
> >On Wed, Jul 22, 2015 at 12:43:53PM -0600, Martin Sebor wrote:
> >>On 07/14/2015 09:18 AM, Marek Polacek wrote:
> >>>Code such as "if (i == i)" is hardly ever desirable, so we should be able
> >>>to warn about this to prevent dumb mistakes.
> >>
> >>I haven't tried the patch or even studied it very carefully but
> >>I wonder if this is also the case when i is declared volatile.
> >>I.e., do we want to issue a warning there? (If we do, the text
> >>of the warning would need to be adjusted in those cases since
> >>the expression need not evaluate to true.)
> >
> >We don't warn for volatiles because operand_equal_p doesn't consider
> >decls with side effects as same.  Admittedly the test doesn't test
> >that...
> 
> I see. Thanks for clarifying that. Not warning makes sense. I would
> suggest to add a test case for it then to make sure it's deliberate.

Here:

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

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

	PR c++/66555
	PR c/54979
	* c-common.c (find_array_ref_with_const_idx_r): New function.
	(warn_tautological_cmp): New function.
	* c-common.h (warn_tautological_cmp): Declare.
	* c.opt (Wtautological-compare): New option.

	* c-typeck.c (parser_build_binary_op): Call warn_tautological_cmp.

	* call.c (build_new_op_1): Call warn_tautological_cmp.
	* pt.c (tsubst_copy_and_build): Use sentinel to suppress tautological
	compare warnings.

	* doc/invoke.texi: Document -Wtautological-compare.

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

diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index c94596f..6a79b95 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -1861,6 +1861,70 @@ warn_logical_operator (location_t location, enum tree_code code, tree type,
     }
 }
 
+/* Helper function for warn_tautological_cmp.  Look for ARRAY_REFs
+   with constant indices.  */
+
+static tree
+find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
+{
+  tree expr = *expr_p;
+
+  if ((TREE_CODE (expr) == ARRAY_REF
+       || TREE_CODE (expr) == ARRAY_RANGE_REF)
+      && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
+    {
+      *(bool *) data = true;
+      *walk_subtrees = 0;
+    }
+
+  return NULL_TREE;
+}
+
+/* Warn if a self-comparison always evaluates to true or false.  LOC
+   is the location of the comparison with code CODE, LHS and RHS are
+   operands of the comparison.  */
+
+void
+warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
+{
+  if (TREE_CODE_CLASS (code) != tcc_comparison)
+    return;
+
+  /* We do not warn for constants because they are typical of macro
+     expansions that test for features, sizeof, and similar.  */
+  if (CONSTANT_CLASS_P (lhs) || CONSTANT_CLASS_P (rhs))
+    return;
+
+  /* Don't warn for e.g.
+     HOST_WIDE_INT n;
+     ...
+     if (n == (long) n) ...
+   */
+  if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
+      || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
+    return;
+
+  if (operand_equal_p (lhs, rhs, 0))
+    {
+      /* Don't warn about array references with constant indices;
+	 these are likely to come from a macro.  */
+      bool found = false;
+      walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
+				    &found);
+      if (found)
+	return;
+      const bool always_true = (code == EQ_EXPR || code == LE_EXPR
+				|| code == GE_EXPR || code == UNLE_EXPR
+				|| code == UNGE_EXPR || code == UNEQ_EXPR);
+      if (always_true)
+	warning_at (loc, OPT_Wtautological_compare,
+		    "self-comparison always evaluates to true");
+      else
+	warning_at (loc, OPT_Wtautological_compare,
+		    "self-comparison always evaluates to false");
+    }
+}
+
 /* Warn about logical not used on the left hand side operand of a comparison.
    This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
    Do not warn if RHS is of a boolean type.  */
diff --git gcc/c-family/c-common.h gcc/c-family/c-common.h
index a198e79..f0640c7 100644
--- gcc/c-family/c-common.h
+++ gcc/c-family/c-common.h
@@ -812,6 +812,7 @@ extern bool warn_if_unused_value (const_tree, location_t);
 extern void warn_logical_operator (location_t, enum tree_code, tree,
 				   enum tree_code, tree, enum tree_code, tree);
 extern void warn_logical_not_parentheses (location_t, enum tree_code, tree);
+extern void warn_tautological_cmp (location_t, enum tree_code, tree, tree);
 extern void check_main_parameter_types (tree decl);
 extern bool c_determine_visibility (tree);
 extern bool vector_types_compatible_elements_p (tree, tree);
diff --git gcc/c-family/c.opt gcc/c-family/c.opt
index dc760d7..cb3af48 100644
--- gcc/c-family/c.opt
+++ gcc/c-family/c.opt
@@ -848,6 +848,10 @@ Wsystem-headers
 C ObjC C++ ObjC++ Warning
 ; Documented in common.opt
 
+Wtautological-compare
+C ObjC C++ ObjC++ Var(warn_tautological_compare) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
+Warn if a comparison always evaluates to true or false
+
 Wterminate
 C++ ObjC++ Warning Var(warn_terminate) Init(1)
 Warn if a throw expression will always result in a call to terminate()
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index d3d0abd..e8c8189 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -3430,6 +3430,9 @@ parser_build_binary_op (location_t location, enum tree_code code,
     warn_logical_operator (location, code, TREE_TYPE (result.value),
 			   code1, arg1.value, code2, arg2.value);
 
+  if (warn_tautological_compare)
+    warn_tautological_cmp (location, code, arg1.value, arg2.value);
+
   if (warn_logical_not_paren
       && TREE_CODE_CLASS (code) == tcc_comparison
       && code1 == TRUTH_NOT_EXPR
diff --git gcc/cp/call.c gcc/cp/call.c
index 8dda1de..1be2527 100644
--- gcc/cp/call.c
+++ gcc/cp/call.c
@@ -5651,6 +5651,8 @@ build_new_op_1 (location_t loc, enum tree_code code, int flags, tree arg1,
 	  && ((code_orig_arg1 == BOOLEAN_TYPE)
 	      ^ (code_orig_arg2 == BOOLEAN_TYPE)))
 	maybe_warn_bool_compare (loc, code, arg1, arg2);
+      if (complain & tf_warning && warn_tautological_compare)
+	warn_tautological_cmp (loc, code, arg1, arg2);
       /* Fall through.  */
     case PLUS_EXPR:
     case MINUS_EXPR:
diff --git gcc/cp/pt.c gcc/cp/pt.c
index 95ec376..1538711 100644
--- gcc/cp/pt.c
+++ gcc/cp/pt.c
@@ -14917,6 +14917,7 @@ tsubst_copy_and_build (tree t,
       {
 	warning_sentinel s1(warn_type_limits);
 	warning_sentinel s2(warn_div_by_zero);
+	warning_sentinel s3(warn_tautological_compare);
 	tree op0 = RECUR (TREE_OPERAND (t, 0));
 	tree op1 = RECUR (TREE_OPERAND (t, 1));
 	tree r = build_x_binary_op
diff --git gcc/doc/invoke.texi gcc/doc/invoke.texi
index 413ac16..f21c66b 100644
--- gcc/doc/invoke.texi
+++ gcc/doc/invoke.texi
@@ -283,7 +283,8 @@ Objective-C and Objective-C++ Dialects}.
 -Wsuggest-final-types @gol -Wsuggest-final-methods -Wsuggest-override @gol
 -Wmissing-format-attribute @gol
 -Wswitch  -Wswitch-default  -Wswitch-enum -Wswitch-bool -Wsync-nand @gol
--Wsystem-headers  -Wtrampolines  -Wtrigraphs  -Wtype-limits  -Wundef @gol
+-Wsystem-headers  -Wtautological-compare  -Wtrampolines  -Wtrigraphs @gol
+-Wtype-limits  -Wundef @gol
 -Wuninitialized  -Wunknown-pragmas  -Wno-pragmas @gol
 -Wunsuffixed-float-constants  -Wunused  -Wunused-function @gol
 -Wunused-label  -Wunused-local-typedefs -Wunused-parameter @gol
@@ -3452,6 +3453,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}.
 -Wenum-compare @r{(in C/ObjC; this is on by default in C++)} @gol
 -Wimplicit-int @r{(C and Objective-C only)} @gol
 -Wimplicit-function-declaration @r{(C and Objective-C only)} @gol
+-Wbool-compare  @gol
 -Wcomment  @gol
 -Wformat   @gol
 -Wmain @r{(only for C/ObjC and unless} @option{-ffreestanding}@r{)}  @gol
@@ -3468,6 +3470,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}.
 -Wstrict-aliasing  @gol
 -Wstrict-overflow=1  @gol
 -Wswitch  @gol
+-Wtautological-compare  @gol
 -Wtrigraphs  @gol
 -Wuninitialized  @gol
 -Wunknown-pragmas  @gol
@@ -4513,6 +4516,18 @@ code.  However, note that using @option{-Wall} in conjunction with this
 option does @emph{not} warn about unknown pragmas in system
 headers---for that, @option{-Wunknown-pragmas} must also be used.
 
+@item -Wtautological-compare
+@opindex Wtautological-compare
+@opindex Wno-tautological-compare
+Warn if a self-comparison always evaluates to true or false.  This
+warning detects various mistakes such as:
+@smallexample
+int i = 1;
+@dots{}
+if (i > i) @{ @dots{} @}
+@end smallexample
+This warning is enabled by @option{-Wall}.
+
 @item -Wtrampolines
 @opindex Wtrampolines
 @opindex Wno-trampolines
diff --git gcc/testsuite/c-c++-common/Wtautological-compare-1.c gcc/testsuite/c-c++-common/Wtautological-compare-1.c
index e69de29..71ba4f8 100644
--- gcc/testsuite/c-c++-common/Wtautological-compare-1.c
+++ gcc/testsuite/c-c++-common/Wtautological-compare-1.c
@@ -0,0 +1,70 @@
+/* PR c++/66555 */
+/* { dg-do compile } */
+/* { dg-options "-Wtautological-compare" } */
+
+#define X 5
+#define Y 5
+#define A a
+enum { U };
+
+void
+fn1 (int a, int *p)
+{
+  if (a > a); /* { dg-warning "self-comparison always evaluates to false" } */
+  if (a < a); /* { dg-warning "self-comparison always evaluates to false" } */
+  if (a >= a); /* { dg-warning "self-comparison always evaluates to true" } */
+  if (a <= a); /* { dg-warning "self-comparison always evaluates to true" } */
+  if (a == a); /* { dg-warning "self-comparison always evaluates to true" } */
+  if (a != a); /* { dg-warning "self-comparison always evaluates to false" } */
+  if (A == A); /* { dg-warning "self-comparison always evaluates to true" } */
+  if ((unsigned) a != (unsigned) a);
+  if ((a + 1) <= (a + 1)); /* { dg-warning "self-comparison always evaluates to true" } */
+  if (1 ? a == a : 0); /* { dg-warning "self-comparison always evaluates to true" } */
+  if (fn1 == fn1); /* { dg-warning "self-comparison always evaluates to true" } */
+  if (*p == *p); /* { dg-warning "self-comparison always evaluates to true" } */
+
+  volatile int v = 5;
+  if (v == v);
+  if (v != v);
+}
+
+void
+fn2 (int a)
+{
+  if (sizeof (int) >= 4);
+  if (sizeof (char) != 1);
+  if (sizeof (long) != sizeof (long long));
+  if (0 < sizeof (short));
+  if (5 != 5);
+  if (X > 5);
+  if (X == X);
+  if (3 + 4 == 6 + 1);
+  if ((unsigned) a != (unsigned long) a);
+  if (U == U);
+  if (U > 0);
+}
+
+void
+fn3 (int i, int j)
+{
+  static int a[16];
+  static int b[8][8];
+
+  if (a[5] == a[5]);
+  if (a[X] != a[Y]);
+  if (a[X] != a[X]);
+  if (a[i] == a[i]); /* { dg-warning "self-comparison always evaluates to true" } */
+  if (b[5][5] == b[5][5]);
+  if (b[X][Y] >= b[Y][X]);
+  if (b[X][X] == b[Y][Y]);
+  if (b[i][j] != b[i][j]); /* { dg-warning "self-comparison always evaluates to false" } */
+  if (b[i][Y] < b[i][X]);
+  if (b[X][j] < b[X][j]);
+  if ((a[i] + 4) == (4 + a[i])); /* { dg-warning "self-comparison always evaluates to true" } */
+}
+
+int
+fn4 (int x, int y)
+{
+  return x > x ? 1 : 0; /* { dg-warning "self-comparison always evaluates to false" } */
+}
diff --git gcc/testsuite/g++.dg/cpp0x/decltype-54581.C gcc/testsuite/g++.dg/cpp0x/decltype-54581.C
index 4b81b5a..6322730 100644
--- gcc/testsuite/g++.dg/cpp0x/decltype-54581.C
+++ gcc/testsuite/g++.dg/cpp0x/decltype-54581.C
@@ -1,5 +1,5 @@
 /* { dg-do compile { target c++11 } } */
-/* { dg-options "-Wall" } */
+/* { dg-options "-Wall -Wno-tautological-compare" } */
 
 typedef float v4f __attribute__((vector_size(4*sizeof(float))));
 
diff --git gcc/testsuite/g++.dg/other/vector-compare.C gcc/testsuite/g++.dg/other/vector-compare.C
index 03ff5fd..77b0f51 100644
--- gcc/testsuite/g++.dg/other/vector-compare.C
+++ gcc/testsuite/g++.dg/other/vector-compare.C
@@ -1,5 +1,5 @@
 /* { dg-do compile { target c++11 } } */
-/* { dg-options "-Wall" } */
+/* { dg-options "-Wall -Wno-tautological-compare" } */
 
 // Check that we can compare vector types that really are the same through
 // typedefs.

	Marek



More information about the Gcc-patches mailing list