]> granicus.if.org Git - clang/commitdiff
[analyzer] Fix lambdas that are capturing constants.
authorGabor Horvath <xazax.hun@gmail.com>
Tue, 27 Oct 2015 13:46:39 +0000 (13:46 +0000)
committerGabor Horvath <xazax.hun@gmail.com>
Tue, 27 Oct 2015 13:46:39 +0000 (13:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251407 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Core/ExprEngine.cpp
test/Analysis/lambdas.cpp

index afef624f5dcf9f87ee883433fa062a904e1b6020..f6129a963f89fea668f42281964f0fa7642b7597 100644 (file)
@@ -1859,13 +1859,20 @@ void ExprEngine::VisitCommonDeclRefExpr(const Expr *Ex, const NamedDecl *D,
       FieldDecl *LambdaThisCaptureField;
       CXXRec->getCaptureFields(LambdaCaptureFields, LambdaThisCaptureField);
       const FieldDecl *FD = LambdaCaptureFields[VD];
-      Loc CXXThis =
-          svalBuilder.getCXXThis(MD, LocCtxt->getCurrentStackFrame());
-      SVal CXXThisVal = state->getSVal(CXXThis);
-      V = state->getLValue(FD, CXXThisVal);
-      if (FD->getType()->isReferenceType() &&
-          !VD->getType()->isReferenceType())
-        CaptureByReference = true;
+      if (!FD) {
+        // When a constant is captured, sometimes no corresponding field is
+        // created in the lambda object.
+        assert(VD->getType().isConstQualified());
+        V = state->getLValue(VD, LocCtxt);
+      } else {
+        Loc CXXThis =
+            svalBuilder.getCXXThis(MD, LocCtxt->getCurrentStackFrame());
+        SVal CXXThisVal = state->getSVal(CXXThis);
+        V = state->getLValue(FD, CXXThisVal);
+        if (FD->getType()->isReferenceType() &&
+            !VD->getType()->isReferenceType())
+          CaptureByReference = true;
+      }
     } else {
       V = state->getLValue(VD, LocCtxt);
     }
index 36af7e1e84e445bdb894dd23b360e6f6cb393870..10f6d5595820a20f253555e9c1bec9a93cbabd60 100644 (file)
@@ -195,6 +195,21 @@ struct DontCrash {
   }
 };
 
+
+// Capture constants
+
+void captureConstants() {
+  const int i = 5;
+  [=]() {
+    if (i != 5)
+      clang_analyzer_warnIfReached();
+  }();
+  [&] {
+    if (i != 5)
+      clang_analyzer_warnIfReached();
+  }();
+}
+
 // CHECK: [B2 (ENTRY)]
 // CHECK:   Succs (1): B1
 // CHECK: [B1]