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
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319638
91177308-0d34-0410-b5e6-
96231b3b80d8
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))
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