[clang] [dataflow] Fix crash when InitListExpr is not a prvalue (PR #80970)
Paul Semel via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 13 03:50:02 PST 2024
https://github.com/paulsemel updated https://github.com/llvm/llvm-project/pull/80970
>From 584e875913dc2f3d667aae95f44225c1c0b3101a Mon Sep 17 00:00:00 2001
From: Paul Semel <semelpaul at gmail.com>
Date: Wed, 7 Feb 2024 10:26:23 +0000
Subject: [PATCH] [dataflow] Fix crash when InitListExpr is not a prvalue
---
clang/lib/Analysis/FlowSensitive/Transfer.cpp | 7 ++++---
.../Analysis/FlowSensitive/TransferTest.cpp | 18 ++++++++++++++++++
2 files changed, 22 insertions(+), 3 deletions(-)
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