[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
This commit is contained in:
Stanislav Gatev 2022-01-18 15:51:57 +00:00
parent 7a403436db
commit 68226e572f
2 changed files with 31 additions and 0 deletions

View File

@ -182,6 +182,7 @@ public:
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)

View File

@ -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