[clang] [C++20][Modules] Load function body from the module that gives canonical decl (PR #111992)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 5 18:13:35 PST 2024


================
@@ -1976,14 +1976,16 @@ TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
   if (!InstParams)
     return nullptr;
 
+  // Use canonical templated decl because only canonical decl has body
+  // if declarations were merged during loading from modules.
+  FunctionDecl *TemplatedDecl = D->getTemplatedDecl()->getCanonicalDecl();
----------------
ChuanqiXu9 wrote:

Such problems are pretty common and I thought if we can make it generally. And also I am wondering the definition for canonical decl. But this is not the problem of this patch. This won't make a blocker issue.

I roughly remember that the definition of `CanonicalDecl` are the first declaration not the declaration has the definition. So may be it is better to write this as:

```
FunctionDecl *TemplatedDecl = D->getTemplatedDecl();
if (TemplatedDecl doesn't have a body)
     iterate redecls of TemplatedDecl and assign the decl that has a body to `TemplatedDecl`.
```

(maybe we should have an interface like something like `getRedeclWithDef()` if we don't have one)

And also I admit there some existing codes use `getCanonicalDecl` to try to do the similar things, but they are their problems.

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


More information about the cfe-commits mailing list