[clang] 68226e5 - [clang][dataflow] Add a test to justify skipping past references in UO_Deref
Stanislav Gatev via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 18 13:28:10 PST 2022
Author: Stanislav Gatev
Date: 2022-01-18T21:27:43Z
New Revision: 68226e572f41105446413b12ee95ab5540b2b6ac
URL: https://github.com/llvm/llvm-project/commit/68226e572f41105446413b12ee95ab5540b2b6ac
DIFF: https://github.com/llvm/llvm-project/commit/68226e572f41105446413b12ee95ab5540b2b6ac.diff
LOG: [clang][dataflow] Add a test to justify skipping past references in UO_Deref
This is part of the implementation of the dataflow analysis framework.
See "[RFC] A dataflow analysis framework for Clang AST" on cfe-dev.
Reviewed-by: xazax.hun
Differential Revision: https://reviews.llvm.org/D117567
Added:
Modified:
clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index 0979f55c5a5c8..865191916e502 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -182,6 +182,7 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
switch (S->getOpcode()) {
case UO_Deref: {
+ // Skip past a reference to handle dereference of a dependent pointer.
const auto *SubExprVal = cast_or_null<PointerValue>(
Env.getValue(*SubExpr, SkipPast::Reference));
if (SubExprVal == nullptr)
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index ea035ba013daf..d1d8705d26c0a 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -1758,4 +1758,34 @@ TEST_F(TransferTest, AddrOfReference) {
});
}
+TEST_F(TransferTest, DerefDependentPtr) {
+ std::string Code = R"(
+ template <typename T>
+ void target(T *Foo) {
+ T &Bar = *Foo;
+ /*[[p]]*/
+ }
+ )";
+ runDataflow(
+ Code, [](llvm::ArrayRef<
+ std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
+ Results,
+ ASTContext &ASTCtx) {
+ ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
+ const Environment &Env = Results[0].second.Env;
+
+ const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
+ ASSERT_THAT(FooDecl, NotNull());
+
+ const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
+ ASSERT_THAT(BarDecl, NotNull());
+
+ const auto *FooVal =
+ cast<PointerValue>(Env.getValue(*FooDecl, SkipPast::None));
+ const auto *BarVal =
+ cast<ReferenceValue>(Env.getValue(*BarDecl, SkipPast::None));
+ EXPECT_EQ(&BarVal->getPointeeLoc(), &FooVal->getPointeeLoc());
+ });
+}
+
} // namespace
More information about the cfe-commits
mailing list