[clang] [C++20][Modules] Load function body from the module that gives canonical decl (PR #111992)
Dmitry Polukhin via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 2 07:36:23 PST 2024
dmpolukhin wrote:
> The missing bit was `-O1`, as soon as I've added that, I am now getting the error with this commit and not getting any error without this commit. Here is a proper reproducer: [friends.tgz](https://github.com/user-attachments/files/17950471/friends.tgz)
Thank you for the reproducer. I was able to reproduce it with this PR. It seems that the root cause that deserialization may create additional forward declaration for friend functions (they don't have body because only primary declaration gets deserialized with body) but these extra declarations might be used as a pattern for template instantiation that results in the compilation error. This may happen with and without my change. I'm looking for the best way to fix it. I found that using canonical decl in [TemplateDeclInstantiator::VisitFunctionDecl](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp#L2308) fixes issue for your reproducer but I'm looking for general solution:
```
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 332004055b58..30f2f57269ce 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -2198,9 +2198,11 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(
FunctionTemplate->setLexicalDeclContext(LexicalDC);
- if (isFriend && D->isThisDeclarationADefinition()) {
+ // TODO: Find better place to resolve decl to canonical decl!
+ FunctionDecl* CanonicalDecl = D->getCanonicalDecl();
+ if (isFriend && CanonicalDecl->isThisDeclarationADefinition()) {
FunctionTemplate->setInstantiatedFromMemberTemplate(
- D->getDescribedFunctionTemplate());
+ CanonicalDecl->getDescribedFunctionTemplate());
}
} else if (FunctionTemplate) {
// Record this function template specialization.
```
https://github.com/llvm/llvm-project/pull/111992
More information about the cfe-commits
mailing list