[clang] [C++20][Modules] Fix merging of anonymous members of class templates. (PR #155948)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 15 19:02:01 PDT 2025


ChuanqiXu9 wrote:

> > How these decls get merged if:
> > ```
> > // a.h
> > template <typename T>
> > struct S { union { T x; }; };
> > 
> > // b.h
> > import "a.h";
> > inline void f(S<int> s = {}) { s.x; }
> > 
> > // main.cpp
> > import "a.h";
> > void g(S<int>) {}
> > 
> > import "b.h";
> > void h() { f(); }
> > ```
> > 
> > 
> >     
> >       
> >     
> > 
> >       
> >     
> > 
> >     
> >   
> > I feel we can get some ideas from it.
> 
> In this case, because of the missing `using SI = S<int>;` in `a.h`, when we import it in `main.cpp`, there's no `ClassTemplateSpecializationDecl` to be imported from `a.h` at all. We create a new instance of `ClassTemplateSpecializationDecl` within `main.cpp` where the `isFromASTFile` is set to `false` (since we created within `main.cpp`). The "[not `isFromASTFile`](https://github.com/llvm/llvm-project/blob/main/clang/lib/Serialization/ASTReaderDecl.cpp#L3430)" condition in this case kicks in, and therefore behaves correctly.

Got it. And I am curious, how does it handle the case that the previous class template specialization comes from AST case?

https://github.com/llvm/llvm-project/pull/155948


More information about the cfe-commits mailing list