aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2007-10-29 16:40:01 +0000
committerTed Kremenek <kremenek@apple.com>2007-10-29 16:40:01 +0000
commit6a261551023f8eebc6abd64054e1b9d16615f3d4 (patch)
treefdc9bab3636e121a29e193c287ab18a30edd3de7
parente8c49533521c40643653f943d47229e62d277f88 (diff)
For checking for floating point comparison using == or !=, we now suppress
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
-rw-r--r--Sema/SemaExpr.cpp16
-rw-r--r--test/Sema/floating-point-compare.c4
2 files changed, 17 insertions, 3 deletions
diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp
index 36112e6488..59cff7cc7d 100644
--- a/Sema/SemaExpr.cpp
+++ b/Sema/SemaExpr.cpp
@@ -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;
}
diff --git a/test/Sema/floating-point-compare.c b/test/Sema/floating-point-compare.c
index 6adc228345..62388915a2 100644
--- a/test/Sema/floating-point-compare.c
+++ b/test/Sema/floating-point-compare.c
@@ -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
+}