llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang
            
<details>
<summary>Changes</summary>
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.

--
Full diff: https://github.com/llvm/llvm-project/pull/66359.diff

1 Files Affected:

- (modified) clang/unittests/Analysis/FlowSensitive/TransferTest.cpp (+35) 


<pre>
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp 
b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 05ee5df0e95a433..e29dc86d7aaffb3 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -5457,6 +5457,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&amp;quot;(
+    class MyClass {
+    public:
+      MyClass() : Self(this) {}
+      MyClass *Self;
+    };
+
+    void target() {
+      MyClass MyObj;
+      MyClass *SelfPtr = MyObj.Self;
+      // [[p]]
+    }
+  )&amp;quot;;
+  runDataflow(
+      Code,
+      [](const 
llvm::StringMap&amp;lt;DataflowAnalysisState&amp;lt;NoopLattice&amp;gt;&amp;gt; 
&amp;amp;Results,
+         ASTContext &amp;amp;ASTCtx) {
+        ASSERT_THAT(Results.keys(), 
UnorderedElementsAre(&amp;quot;p&amp;quot;));
+
+        const ValueDecl *MyObjDecl = findValueDecl(ASTCtx, 
&amp;quot;MyObj&amp;quot;);
+        ASSERT_THAT(MyObjDecl, NotNull());
+
+        const ValueDecl *SelfDecl = findValueDecl(ASTCtx, 
&amp;quot;SelfPtr&amp;quot;);
+        ASSERT_THAT(SelfDecl, NotNull());
+
+        const Environment &amp;amp;Env = getEnvironmentAtAnnotation(Results, 
&amp;quot;p&amp;quot;);
+        auto &amp;amp;SelfVal = 
*cast&amp;lt;PointerValue&amp;gt;(Env.getValue(*SelfDecl));
+        EXPECT_EQ(Env.getStorageLocation(*MyObjDecl), 
&amp;amp;SelfVal.getPointeeLoc());
+      },
+      {BuiltinOptions{ContextSensitiveOptions{}}});
+}
+
 TEST(TransferTest, UnnamedBitfieldInitializer) {
   std::string Code = R&amp;quot;(
     struct B {};
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66359
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to