[cfe-commits] r43450 - in /cfe/trunk: Sema/SemaExpr.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/self-comparison.c

Ted Kremenek kremenek at apple.com
Mon Oct 29 09:58:49 PDT 2007


Author: kremenek
Date: Mon Oct 29 11:58:49 2007
New Revision: 43450

URL: http://llvm.org/viewvc/llvm-project?rev=43450&view=rev
Log:
For non-floating point types, added check for expressions of the form
"x == x" and "x != x".  We emit a warning for these since they always evaluate
to a constant value and often indicate a logical error.

Added test case for this check.

Added:
    cfe/trunk/test/Sema/self-comparison.c
Modified:
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=43450&r1=43449&r2=43450&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Mon Oct 29 11:58:49 2007
@@ -1218,6 +1218,13 @@
   QualType lType = lex->getType();
   QualType rType = rex->getType();
   
+  if (!lType->isFloatingType()) {
+    if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
+      if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
+        if (DRL->getDecl() == DRR->getDecl())
+          Diag(loc, diag::warn_selfcomparison);      
+  }
+  
   if (isRelational) {
     if (lType->isRealType() && rType->isRealType())
       return Context.IntTy;

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=43450&r1=43449&r2=43450&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Mon Oct 29 11:58:49 2007
@@ -824,6 +824,11 @@
 DIAG(warn_floatingpoint_eq, WARNING,
   "comparing floating point with == or != is unsafe")
 
+// CHECK: for non-floating point, expressions of the form x == x or x != x
+// should result in a warning, since these always evaluate to a constant.
+DIAG(warn_selfcomparison,WARNING,
+     "self-comparison always results in a constant value.")
+
 // CHECK: stores to variables that are no longer live (dead stores)
 DIAG(warn_dead_store, WARNING, "value stored to variable is never used")
 

Added: cfe/trunk/test/Sema/self-comparison.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/self-comparison.c?rev=43450&view=auto

==============================================================================
--- cfe/trunk/test/Sema/self-comparison.c (added)
+++ cfe/trunk/test/Sema/self-comparison.c Mon Oct 29 11:58:49 2007
@@ -0,0 +1,17 @@
+// RUN: clang -fsyntax-only -verify %s
+
+int foo(int x) {
+  return x == x; // expected-warning {{self-comparison always results}}
+}
+
+int foo2(int x) {
+  return (x) != (((x))); // expected-warning {{self-comparison always results}}
+}
+
+int bar(float x) {
+  return x == x; // no-warning
+}
+
+int bar2(float x) {
+  return x != x; // no-warning
+}
\ No newline at end of file





More information about the cfe-commits mailing list