[clang][dataflow] Bugfix for `refreshStructValue()`.
authorMartin Braenne <mboehme@google.com>
Mon, 17 Jul 2023 18:42:36 +0000 (18:42 +0000)
committerMartin Braenne <mboehme@google.com>
Mon, 17 Jul 2023 18:56:25 +0000 (18:56 +0000)
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

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
clang/unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp

index 5d301b8..54632f5 100644 (file)
@@ -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);
   }
index e9531d1..ba316f7 100644 (file)
@@ -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<FunctionDecl>("target", Results);
+  ASSERT_THAT(Target, NotNull());
+
+  Results = match(declRefExpr(to(varDecl(hasName("s")))).bind("s"), Context);
+  const auto *DRE = selectFirst<DeclRefExpr>("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