[clang] ba27993 - [dataflow] Fix crash when InitListExpr is not a prvalue (#80970)

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 15 01:59:55 PST 2024


Author: Paul Semel
Date: 2024-02-15T10:59:51+01:00
New Revision: ba279934c6ab09d5394a89d8318651aefd8d565b

URL: https://github.com/llvm/llvm-project/commit/ba279934c6ab09d5394a89d8318651aefd8d565b
DIFF: https://github.com/llvm/llvm-project/commit/ba279934c6ab09d5394a89d8318651aefd8d565b.diff

LOG: [dataflow] Fix crash when InitListExpr is not a prvalue (#80970)

Added: 
    

Modified: 
    clang/lib/Analysis/FlowSensitive/Transfer.cpp
    clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index f0b15f43b1f423..fc7395457f551d 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -664,9 +664,10 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
     QualType Type = S->getType();
 
     if (!Type->isStructureOrClassType()) {
-      if (auto *Val = Env.createValue(Type))
-        Env.setValue(*S, *Val);
-
+      // Until array initialization is implemented, we don't need to care about
+      // cases where `getNumInits() > 1`.
+      if (S->getNumInits() == 1)
+        propagateValueOrStorageLocation(*S->getInit(0), *S, Env);
       return;
     }
 

diff  --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 4b3b3511f848e8..87e6e83d2e03a9 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -2349,6 +2349,24 @@ TEST(TransferTest, AssignmentOperatorReturnsByValue) {
          ASTContext &ASTCtx) {});
 }
 
+TEST(TransferTest, InitListExprAsXValue) {
+  // This is a crash repro.
+  std::string Code = R"(
+    void target() {
+      bool&& Foo{false};
+      // [[p]]
+    }
+  )";
+  runDataflow(
+      Code,
+      [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+         ASTContext &ASTCtx) {
+        const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+        const auto &FooVal = getValueForDecl<BoolValue>(ASTCtx, Env, "Foo");
+        ASSERT_TRUE(FooVal.formula().isLiteral(false));
+      });
+}
+
 TEST(TransferTest, CopyConstructor) {
   std::string Code = R"(
     struct A {


        


More information about the cfe-commits mailing list