[clang] [clang][ASTImporter] Improve import of friend class templates. (PR #74627)

Balázs Kéri via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 9 08:57:14 PST 2024


================
@@ -5919,15 +5919,26 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
   if (ToD)
     return ToD;
 
-  bool IsFriendTemplate = D->getFriendObjectKind() != Decl::FOK_None;
-  bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext()
-                                            : DC->isDependentContext();
-  bool DependentFriend = IsFriendTemplate && IsDependentContext;
+  // Should check if a declaration is friend in a dependent context.
+  // Such templates are not linked together in a declaration chain.
+  // The ASTImporter strategy is to map existing forward declarations to
+  // imported ones only if strictly necessary, otherwise import these as new
+  // forward declarations. In case of the "dependent friend" declarations, new
+  // declarations are created, but not linked in a declaration chain.
+  auto IsDependentFriend = [](ClassTemplateDecl *TD) {
+    bool IsFriendTemplate = TD->getFriendObjectKind() != Decl::FOK_None;
+    DeclContext *DC = TD->getDeclContext();
+    DeclContext *LexicalDC = TD->getLexicalDeclContext();
+    bool IsDependentContext = DC != LexicalDC ? LexicalDC->isDependentContext()
+                                              : DC->isDependentContext();
----------------
balazske wrote:

Interesting observation, I think the simplification can be made in the original code too, I did not find this when I reviewed this code.

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


More information about the cfe-commits mailing list