[clang] [Clang] Implement C++26’s P2893R3 ‘Variadic friends’ (PR #101448)

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 6 09:13:30 PDT 2024


================
@@ -487,7 +487,26 @@ void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
     }
 
     this->Indent();
-    Visit(*D);
+
+    // Group friend declarations if need be.
+    if (isa<FriendDecl>(*D)) {
+      auto *FD = cast<FriendDecl>(*D);
+      VisitFriendDecl(FD);
+      SourceLocation FriendLoc = FD->getFriendLoc();
+
+      // Use a separate iterator; 'D' is always one declaration 'behind' in
+      // this loop; the last friend printed here (or the first printed just
+      // now before this loop if there are no subsequent friends) will be
+      // skipped by the '++D' of the outer loop.
+      for (DeclContext::decl_iterator It; It = std::next(D), It != DEnd; ++D) {
+        auto *NextFriend = dyn_cast<FriendDecl>(*It);
+        if (!NextFriend || NextFriend->getFriendLoc() != FriendLoc)
+          break;
+        VisitFriendDecl(NextFriend, false);
+      }
+    } else {
+      Visit(*D);
+    }
----------------
Sirraide wrote:

Hmm, the problem is that in the AST, we just have multiple `FriendDecl`s in a row with no way of getting from one decl to the next one (that I know of, maybe there’s something I don’t know about, but even then, this function would have to know about it and just skip the remaining friend decls or sth like that...), so we’d have to pass the decl iterator to `VisitFriendDecl`, which feels a bit weird to me. 

Alternatively, we could also move this into a separate helper.

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


More information about the cfe-commits mailing list