Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -5486,6 +5486,22 @@ /// } /// int z; /// \endcode +/// +/// From clang/Basic/Linkage.h: +/// > UniqueExternalLinkage: +/// > External linkage within a unique namespace. +/// > From the language perspective, these entities have external +/// > linkage. However, since they reside in an anonymous namespace, +/// > their names are unique to this translation unit, which is +/// > equivalent to having internal linkage from the code-generation +/// > point of view. +/// +/// Example matches f (matcher = functionDecl(hasExternalFormalLinkage())) +/// \code +/// namespace { +/// void f() {} +/// } +/// \endcode AST_MATCHER(NamedDecl, hasExternalFormalLinkage) { return Node.hasExternalFormalLinkage(); } Index: unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -1939,12 +1939,18 @@ TEST(HasExternalFormalLinkage, Basic) { EXPECT_TRUE(matches("int a = 0;", namedDecl(hasExternalFormalLinkage()))); - EXPECT_FALSE( - matches("static int a = 0;", namedDecl(hasExternalFormalLinkage()))); - EXPECT_FALSE(matches("static void f(void) { int a = 0; }", - namedDecl(hasExternalFormalLinkage()))); + EXPECT_TRUE( + notMatches("static int a = 0;", namedDecl(hasExternalFormalLinkage()))); + EXPECT_TRUE(notMatches("static void f(void) { int a = 0; }", + namedDecl(hasExternalFormalLinkage()))); EXPECT_TRUE(matches("void f(void) { int a = 0; }", namedDecl(hasExternalFormalLinkage()))); + + // Despite having internal semantic linkage, the anonymous namespace member + // has external linkage because the member has a unique name in all + // translation units. + EXPECT_TRUE(matches("namespace { int a = 0; }", + namedDecl(hasExternalFormalLinkage()))); } } // namespace ast_matchers