]> granicus.if.org Git - clang/commitdiff
For checking for floating point comparison using == or !=, we now suppress
authorTed Kremenek <kremenek@apple.com>
Mon, 29 Oct 2007 16:40:01 +0000 (16:40 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 29 Oct 2007 16:40:01 +0000 (16:40 +0000)
errors for cases such as "x == x".

Added test case to test this feature.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43447 91177308-0d34-0410-b5e6-96231b3b80d8

Sema/SemaExpr.cpp
test/Sema/floating-point-compare.c

index 36112e6488b993021dada136a8a4cf8688157e11..59cff7cc7d4bd100300d91151de45489d28a6f2f 100644 (file)
@@ -1208,9 +1208,19 @@ inline QualType Sema::CheckCompareOperands( // C99 6.5.8
     if (lType->isRealType() && rType->isRealType())
       return Context.IntTy;
   } else {
-    if (lType->isFloatingType() && rType->isFloatingType()) 
-      Diag(loc, diag::warn_floatingpoint_eq);
-
+    if (lType->isFloatingType() && rType->isFloatingType()) {
+      // Special case: check for x == x (which is OK).
+      bool EmitWarning = true;
+      
+      if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex))
+        if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex))
+          if (DRL->getDecl() == DRR->getDecl())
+            EmitWarning = false;
+      
+      if (EmitWarning)
+        Diag(loc, diag::warn_floatingpoint_eq);
+    }
+    
     if (lType->isArithmeticType() && rType->isArithmeticType())
       return Context.IntTy;
   }
index 6adc2283453fc3007bf17bd15cf0e25a8cb9bc5d..62388915a26dd08f2a2ff1dc477c321ba0c0f39e 100644 (file)
@@ -7,3 +7,7 @@ int foo(float x, float y) {
 int bar(float x, float y) {
   return x != y; // expected-warning {{comparing floating point with ==}}
 }
+
+int qux(float x) {
+  return x == x; // no-warning
+}