diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp index e0e3b71503d21..cdb1bc3cd16ac 100644 --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -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> &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(Env.getValue(*SelfDecl)); + EXPECT_EQ(Env.getStorageLocation(*MyObjDecl), &SelfVal.getPointeeLoc()); + }, + {BuiltinOptions{ContextSensitiveOptions{}}}); +} + TEST(TransferTest, UnnamedBitfieldInitializer) { std::string Code = R"( struct B {};