Skip to content

Commit

Permalink
[clang][dataflow] Add a test for context-sensitive analysis on a self…
Browse files Browse the repository at this point in the history
…-referential class. (#66359)

The test demonstrates that the `this` pointer seen in the constructor
has the
same value as the address of the variable the object is constructed
into.
  • Loading branch information
martinboehme committed Sep 15, 2023
1 parent c0a64ec commit 0069004
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5481,6 +5481,41 @@ TEST(TransferTest, ContextSensitiveConstructorDefault) {
{BuiltinOptions{ContextSensitiveOptions{}}});
}

TEST(TransferTest, ContextSensitiveSelfReferentialClass) {
// Test that the `this` pointer seen in the constructor has the same value
// as the address of the variable the object is constructed into.
std::string Code = R"(
class MyClass {
public:
MyClass() : Self(this) {}
MyClass *Self;
};
void target() {
MyClass MyObj;
MyClass *SelfPtr = MyObj.Self;
// [[p]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p"));

const ValueDecl *MyObjDecl = findValueDecl(ASTCtx, "MyObj");
ASSERT_THAT(MyObjDecl, NotNull());

const ValueDecl *SelfDecl = findValueDecl(ASTCtx, "SelfPtr");
ASSERT_THAT(SelfDecl, NotNull());

const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
auto &SelfVal = *cast<PointerValue>(Env.getValue(*SelfDecl));
EXPECT_EQ(Env.getStorageLocation(*MyObjDecl), &SelfVal.getPointeeLoc());
},
{BuiltinOptions{ContextSensitiveOptions{}}});
}

TEST(TransferTest, UnnamedBitfieldInitializer) {
std::string Code = R"(
struct B {};
Expand Down

0 comments on commit 0069004

Please sign in to comment.