[clang] 0f6cf55 - [clang][dataflow] Bugfix for `refreshStructValue()`.

Martin Braenne via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 17 11:56:34 PDT 2023


Author: Martin Braenne
Date: 2023-07-17T18:56:25Z
New Revision: 0f6cf555674959d0b21769fc1c46e23584561f2a

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

LOG: [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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index 5d301b815d38e0..54632f5662b63d 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 e9531d1e7e8fd4..ba316f7d35e895 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<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


        


More information about the cfe-commits mailing list