From: Devin Coughlin Date: Mon, 4 Dec 2017 04:46:47 +0000 (+0000) Subject: [analyzer] Don't treat lambda-captures float constexprs as undefined X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6a217e4c25e66055338c0c58d33d8a7e793f8b28;p=clang [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319638 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Core/RegionStore.cpp b/lib/StaticAnalyzer/Core/RegionStore.cpp index 39b92ee3a1..ecb32cc378 100644 --- a/lib/StaticAnalyzer/Core/RegionStore.cpp +++ b/lib/StaticAnalyzer/Core/RegionStore.cpp @@ -1864,11 +1864,18 @@ SVal RegionStoreManager::getBindingForVar(RegionBindingsConstRef B, 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 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(MS)) diff --git a/test/Analysis/lambdas.cpp b/test/Analysis/lambdas.cpp index f3ff9b9539..38a2e3a84f 100644 --- a/test/Analysis/lambdas.cpp +++ b/test/Analysis/lambdas.cpp @@ -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