[clang-tools-extra] [include-cleaner] Add handling for FriendDecls (PR #72125)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 13 07:22:06 PST 2023


https://github.com/kadircet created https://github.com/llvm/llvm-project/pull/72125

Fixes https://github.com/llvm/llvm-project/issues/64382.


>From 8a96c67f70f9ae77d3e1576f49cd1ff7880e6e00 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Mon, 13 Nov 2023 16:20:01 +0100
Subject: [PATCH] [include-cleaner] Add handling for FriendDecls

Fixes https://github.com/llvm/llvm-project/issues/64382.
---
 clang-tools-extra/include-cleaner/lib/WalkAST.cpp        | 9 +++++++++
 .../include-cleaner/unittests/WalkASTTest.cpp            | 5 +++++
 2 files changed, 14 insertions(+)

diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index 307e0652f9ccaf8..6c4d9b7862d915b 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -11,6 +11,7 @@
 #include "clang/AST/ASTFwd.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
+#include "clang/AST/DeclFriend.h"
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/ExprCXX.h"
@@ -243,6 +244,14 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
     return true;
   }
 
+  bool VisitFriendDecl(FriendDecl *D) {
+    // We already visit the TypeLoc properly, but need to special case the decl
+    // case.
+    if (auto *FD = D->getFriendDecl())
+      report(D->getLocation(), FD);
+    return true;
+  }
+
   bool VisitConceptReference(const ConceptReference *CR) {
     report(CR->getConceptNameLoc(), CR->getFoundDecl());
     return true;
diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index 9b99d5a5c32bad3..bdfc24b8edee38f 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -550,5 +550,10 @@ TEST(WalkAST, Concepts) {
   // FIXME: Foo should be explicitly referenced.
   testWalk("template<typename T> concept Foo = true;", "void func() { ^Foo auto x = 1; }");
 }
+
+TEST(WalkAST, FriendDecl) {
+  testWalk("void $explicit^foo();", "struct Bar { friend void ^foo(); };");
+  testWalk("struct $explicit^Foo {};", "struct Bar { friend struct ^Foo; };");
+}
 } // namespace
 } // namespace clang::include_cleaner



More information about the cfe-commits mailing list