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

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 13 02:42:20 PST 2024


================
@@ -2313,6 +2313,28 @@ TEST(TransferTest, AssignmentOperatorWithInitAndInheritance) {
          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 ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+        ASSERT_THAT(FooDecl, NotNull());
+        const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+        const auto *FooVal =
+            dyn_cast_or_null<BoolValue>(Env.getValue(*FooDecl));
+        ASSERT_THAT(FooVal, NotNull());
+        ASSERT_EQ(FooVal, &Env.getBoolLiteralValue(false));
----------------
martinboehme wrote:

```suggestion
        const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
        const auto &FooVal = getValueForDecl<BoolValue>(ASTCtx, Env, "Foo");
        ASSERT_TRUE(FooVal.formula().isLiteral(false));
```

(There's unfortunately still a lot of tests that don't use the `getValueForDecl()` helper and are thus needlessly complicated.)

https://github.com/llvm/llvm-project/pull/80970


More information about the cfe-commits mailing list