From: Martin Braenne Date: Mon, 17 Jul 2023 18:42:36 +0000 (+0000) Subject: [clang][dataflow] Bugfix for `refreshStructValue()`. X-Git-Tag: upstream/17.0.6~1480 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0f6cf555674959d0b21769fc1c46e23584561f2a;p=platform%2Fupstream%2Fllvm.git [clang][dataflow] Bugfix for `refreshStructValue()`. In the case where the expression was not yet associated with a storage location, we created a new storage location but failed to associate it with the expression. The newly added test fails without the fix. Reviewed By: xazax.hun Differential Revision: https://reviews.llvm.org/D155465 --- diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp index 5d301b8..54632f5 100644 --- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp +++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp @@ -1005,6 +1005,7 @@ StructValue &refreshStructValue(const Expr &Expr, Environment &Env) { StorageLocation *Loc = Env.getStorageLocationStrict(Expr); if (Loc == nullptr) { Loc = &Env.createStorageLocation(Expr); + Env.setStorageLocation(Expr, *Loc); } Env.setValue(*Loc, NewVal); } diff --git a/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp b/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp index e9531d1..ba316f7 100644 --- a/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp @@ -25,6 +25,7 @@ namespace { using namespace clang; using namespace dataflow; using ::clang::dataflow::test::getFieldValue; +using ::testing::IsNull; using ::testing::NotNull; class EnvironmentTest : public ::testing::Test { @@ -252,4 +253,35 @@ TEST_F(EnvironmentTest, InitGlobalVarsConstructor) { EXPECT_THAT(Env.getValue(*Var), NotNull()); } +TEST_F(EnvironmentTest, RefreshStructValue) { + using namespace ast_matchers; + + std::string Code = R"cc( + struct S {}; + void target () { + S s; + s; + } + )cc"; + + auto Unit = + tooling::buildASTFromCodeWithArgs(Code, {"-fsyntax-only", "-std=c++11"}); + auto &Context = Unit->getASTContext(); + + ASSERT_EQ(Context.getDiagnostics().getClient()->getNumErrors(), 0U); + + auto Results = match(functionDecl(hasName("target")).bind("target"), Context); + const auto *Target = selectFirst("target", Results); + ASSERT_THAT(Target, NotNull()); + + Results = match(declRefExpr(to(varDecl(hasName("s")))).bind("s"), Context); + const auto *DRE = selectFirst("s", Results); + ASSERT_THAT(DRE, NotNull()); + + Environment Env(DAContext, *Target); + EXPECT_THAT(Env.getStorageLocationStrict(*DRE), IsNull()); + refreshStructValue(*DRE, Env); + EXPECT_THAT(Env.getStorageLocationStrict(*DRE), NotNull()); +} + } // namespace