[clang] [clang][dataflow] Handle when `this` refers to a different location (PR #146900)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 14 06:11:57 PDT 2025
martinboehme wrote:
> > As an initial comment, wanted to confirm that we've clarified what the semantics of the AST are here.
> > In #128068, I asked:
> > > What I'm not clear on is what the rule is that disambiguates this. Is it simply that if a `CXXThisExpr` is contained in an `InitListExpr`, it refers to the result object of that `InitListExpr` (and not the this pointer of the enclosing `CXXMethodDecl`)?
> >
> >
> > Have you been able to get a definitive clarification on this?
>
> @yronglin chimed in at [#128068 (comment)](https://github.com/llvm/llvm-project/issues/128068#issuecomment-3013510399) but haven't heard from others beyond that.
>
> What I see in the code / debugger is like that, involving `CXXDefaultInitExpr`: "if a `CXXThisExpr` is contained in an `CXXDefaultInitExpr` in an `InitListExpr` (or `CXXParenListInitExpr`) (and not the...)".
>
> * while parsing and building an `InitListExpr` (one example is around [here](https://github.com/llvm/llvm-project/blob/ace3d3002ff465d433dce526ad3b641170a3dda7/clang/lib/Parse/ParseExprCXX.cpp#L1799-L1805))
> * it will eventually hit `FillInEmptyInitForField`[here](https://github.com/llvm/llvm-project/blob/e933cfcfb2d207a7ff63cf152a7a78555d431d49/clang/lib/Sema/SemaInit.cpp#L804-L824)
> * and `BuildCXXDefaultInitExpr` will have an [`CXXThisScopeRAII`](https://github.com/llvm/llvm-project/blob/dc8e89b2b3787defa9ef1d72014c8a68c1b09a5f/clang/lib/Sema/SemaExpr.cpp#L5650) which sets [`CXXThisTypeOverride`](https://github.com/llvm/llvm-project/blob/dc8e89b2b3787defa9ef1d72014c8a68c1b09a5f/clang/lib/Sema/SemaExprCXX.cpp#L1251) with the `Field->getParent()`'s type
>
> similarly, there's some CodeGen code that prepares for a different `this` for fields [here](https://github.com/llvm/llvm-project/blob/ace3d3002ff465d433dce526ad3b641170a3dda7/clang/lib/CodeGen/CGExprAgg.cpp#L1845) using [`FieldConstructionScope`](https://github.com/llvm/llvm-project/blob/ace3d3002ff465d433dce526ad3b641170a3dda7/clang/lib/CodeGen/CodeGenFunction.h#L1759) based on the slot for the InitListExpr [from here](https://github.com/llvm/llvm-project/blob/ace3d3002ff465d433dce526ad3b641170a3dda7/clang/lib/CodeGen/CGExprAgg.cpp#L1785)
>
> If the `CXXThisExpr` is not under an `CXXDefaultInitExpr`, then you use the "previous" this. [Example](https://godbolt.org/z/KdMno4qWf) where `this` for `x` is type `Foo*` but `this` for `i` is `Other*`.
Sorry for the late reply -- was busy with other things.
Thanks for gathering these references. I haven't looked at everything in detail, but this looks persuasive. However, I wonder if we could get some confirmation from a person who has worked with this code themselves -- maybe ask in the Clang Frontend section of the Clang forum?
https://github.com/llvm/llvm-project/pull/146900
More information about the cfe-commits
mailing list