From: Ted Kremenek Date: Thu, 29 Nov 2007 00:59:04 +0000 (+0000) Subject: Enhanced implementation of -Wfloat-equal to check for comparisons against X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1b500bb0d40aa3ebf1ace47340bb5f401a9ae99c;p=clang Enhanced implementation of -Wfloat-equal to check for comparisons against floating-point literals that are represented exactly by the APFloat in FloatingLiteral. For such literals, we do not emit a warning since such checks are often performed in real code to see if a variable has changed from its original value. This heuristic clearly can lead to false negatives, but the hope is it will significantly reduce false positives to help make the compiler flag more useful. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44424 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/SemaChecking.cpp b/Sema/SemaChecking.cpp index cb22f03a31..20f0b81b43 100644 --- a/Sema/SemaChecking.cpp +++ b/Sema/SemaChecking.cpp @@ -715,6 +715,24 @@ void Sema::CheckFloatComparison(SourceLocation loc, Expr* lex, Expr *rex) { if (DRL->getDecl() == DRR->getDecl()) EmitWarning = false; + + // Special case: check for comparisons against literals that can be exactly + // represented by APFloat. In such cases, do not emit a warning. This + // is a heuristic: often comparison against such literals are used to + // detect if a value in a variable has not changed. This clearly can + // lead to false negatives. + if (EmitWarning) { + if (FloatingLiteral* FLL = dyn_cast(LeftExprSansParen)) { + if (FLL->isExact()) + EmitWarning = false; + } + else + if (FloatingLiteral* FLR = dyn_cast(RightExprSansParen)){ + if (FLR->isExact()) + EmitWarning = false; + } + } + // Check for comparisons with builtin types. if (EmitWarning) if (CallExpr* CL = dyn_cast(LeftExprSansParen))