r319638 - [analyzer] Don't treat lambda-captures float constexprs as undefined

Devin Coughlin via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 3 20:46:48 PST 2017


Author: dcoughlin
Date: Sun Dec  3 20:46:47 2017
New Revision: 319638

URL: http://llvm.org/viewvc/llvm-project?rev=319638&view=rev
Log:
[analyzer] Don't treat lambda-captures float constexprs as undefined

RegionStore has special logic to evaluate captured constexpr variables.
However, if the constexpr initializer cannot be evaluated as an integer, the
value is treated as undefined. This leads to false positives when, for example,
a constexpr float is captured by a lambda.

To fix this, treat a constexpr capture that cannot be evaluated as unknown
rather than undefined.

rdar://problem/35784662

Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp
    cfe/trunk/test/Analysis/lambdas.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp?rev=319638&r1=319637&r2=319638&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp Sun Dec  3 20:46:47 2017
@@ -1864,11 +1864,18 @@ SVal RegionStoreManager::getBindingForVa
     return svalBuilder.getRegionValueSymbolVal(R);
 
   // Is 'VD' declared constant?  If so, retrieve the constant value.
-  if (VD->getType().isConstQualified())
-    if (const Expr *Init = VD->getInit())
+  if (VD->getType().isConstQualified()) {
+    if (const Expr *Init = VD->getInit()) {
       if (Optional<SVal> V = svalBuilder.getConstantVal(Init))
         return *V;
 
+      // If the variable is const qualified and has an initializer but
+      // we couldn't evaluate initializer to a value, treat the value as
+      // unknown.
+      return UnknownVal();
+    }
+  }
+
   // This must come after the check for constants because closure-captured
   // constant variables may appear in UnknownSpaceRegion.
   if (isa<UnknownSpaceRegion>(MS))

Modified: cfe/trunk/test/Analysis/lambdas.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/lambdas.cpp?rev=319638&r1=319637&r2=319638&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/lambdas.cpp (original)
+++ cfe/trunk/test/Analysis/lambdas.cpp Sun Dec  3 20:46:47 2017
@@ -337,6 +337,16 @@ void captureByReference() {
   lambda2();
 }
 
+void testCapturedConstExprFloat() {
+  constexpr float localConstant = 4.0;
+  auto lambda = []{
+    // Don't treat localConstant as containing a garbage value
+    float copy = localConstant; // no-warning
+    (void)copy;
+  };
+
+  lambda();
+}
 
 // CHECK: [B2 (ENTRY)]
 // CHECK:   Succs (1): B1




More information about the cfe-commits mailing list