summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/include-fixer/find-all-symbols/FindAllSymbols.cpp11
-rw-r--r--clang-tools-extra/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp21
2 files changed, 29 insertions, 3 deletions
diff --git a/clang-tools-extra/include-fixer/find-all-symbols/FindAllSymbols.cpp b/clang-tools-extra/include-fixer/find-all-symbols/FindAllSymbols.cpp
index 15b6f91fc67..60c0615c822 100644
--- a/clang-tools-extra/include-fixer/find-all-symbols/FindAllSymbols.cpp
+++ b/clang-tools-extra/include-fixer/find-all-symbols/FindAllSymbols.cpp
@@ -161,9 +161,14 @@ void FindAllSymbols::registerMatchers(MatchFinder *MatchFinder) {
MatchFinder->addMatcher(CxxRecordDecl.bind("decl"), this);
// Matchers for function declarations.
- MatchFinder->addMatcher(
- functionDecl(CommonFilter, anyOf(ExternCMatcher, CCMatcher)).bind("decl"),
- this);
+ // We want to exclude friend declaration, but the `DeclContext` of a friend
+ // function declaration is not the class in which it is declared, so we need
+ // to explicitly check if the parent is a `friendDecl`.
+ MatchFinder->addMatcher(functionDecl(CommonFilter,
+ unless(hasParent(friendDecl())),
+ anyOf(ExternCMatcher, CCMatcher))
+ .bind("decl"),
+ this);
// Matcher for typedef and type alias declarations.
//
diff --git a/clang-tools-extra/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp b/clang-tools-extra/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
index a034088d2b2..dd20b9fabc7 100644
--- a/clang-tools-extra/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
+++ b/clang-tools-extra/unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp
@@ -433,5 +433,26 @@ TEST_F(FindAllSymbolsTest, MacroTestWithIWYU) {
EXPECT_TRUE(hasSymbol(Symbol));
}
+TEST_F(FindAllSymbolsTest, NoFriendTest) {
+ static const char Code[] = R"(
+ class WorstFriend {
+ friend void Friend();
+ friend class BestFriend;
+ };
+ )";
+ runFindAllSymbols(Code);
+ SymbolInfo Symbol = SymbolInfo("WorstFriend", SymbolInfo::SymbolKind::Class,
+ HeaderName, 2, {});
+ EXPECT_TRUE(hasSymbol(Symbol));
+
+ Symbol = SymbolInfo("Friend", SymbolInfo::SymbolKind::Function, HeaderName,
+ 3, {});
+ EXPECT_FALSE(hasSymbol(Symbol));
+
+ Symbol = SymbolInfo("BestFriend", SymbolInfo::SymbolKind::Class, HeaderName,
+ 4, {});
+ EXPECT_FALSE(hasSymbol(Symbol));
+}
+
} // namespace find_all_symbols
} // namespace clang
OpenPOWER on IntegriCloud