Skip to content

Commit

Permalink
[ASTMatchers] Add isInAnonymousNamespace narrowing matcher
Browse files Browse the repository at this point in the history
Used in a couple clang-tidy checks so it could be extracted
out as its own matcher.

Differential Revision: https://reviews.llvm.org/D140328
  • Loading branch information
carlosgalvezp committed Dec 23, 2022
1 parent 68a8880 commit 125ccd3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
19 changes: 19 additions & 0 deletions clang/docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -3962,6 +3962,25 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
cxxRecordDecl(hasName("vector"), isInStdNamespace()) will match only #1.
</pre></td></tr>

<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('isInAnonymousNamespace0')"><a name="isInAnonymousNamespace0Anchor">isInAnonymousNamespace</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isInAnonymousNamespace0"><pre>Matches declarations in an anonymous namespace.

Given
class vector {};
namespace foo {
class vector {};
namespace {
class vector {}; // #1
}
}
namespace {
class vector {}; // #2
namespace foo {
class vector{}; // #3
}
}
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace()) will match #1, #2 and #3.
</pre></td></tr>

<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('isInstantiated0')"><a name="isInstantiated0Anchor">isInstantiated</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isInstantiated0"><pre>Matches declarations that are template instantiations or are inside
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ Build System Changes

AST Matchers
------------
- Add ``isInAnoymousNamespace`` matcher to match declarations in an anonymous namespace.

clang-format
------------
Expand Down
24 changes: 24 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7813,6 +7813,30 @@ AST_MATCHER(NamespaceDecl, isAnonymous) {
/// cxxRecordDecl(hasName("vector"), isInStdNamespace()) will match only #1.
AST_MATCHER(Decl, isInStdNamespace) { return Node.isInStdNamespace(); }

/// Matches declarations in an anonymous namespace.
///
/// Given
/// \code
/// class vector {};
/// namespace foo {
/// class vector {};
/// namespace {
/// class vector {}; // #1
/// }
/// }
/// namespace {
/// class vector {}; // #2
/// namespace foo {
/// class vector{}; // #3
/// }
/// }
/// \endcode
/// cxxRecordDecl(hasName("vector"), isInAnonymousNamespace()) will match
/// #1, #2 and #3.
AST_MATCHER(Decl, isInAnonymousNamespace) {
return Node.isInAnonymousNamespace();
}

/// If the given case statement does not use the GNU case range
/// extension, matches the constant given in the statement.
///
Expand Down
35 changes: 35 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3550,6 +3550,41 @@ TEST_P(ASTMatchersTest, InStdNamespace) {
cxxRecordDecl(hasName("vector"), isInStdNamespace())));
}

TEST_P(ASTMatchersTest, InAnonymousNamespace) {
if (!GetParam().isCXX()) {
return;
}

EXPECT_TRUE(
notMatches("class vector {};"
"namespace foo {"
" class vector {};"
"}",
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace())));

EXPECT_TRUE(
matches("namespace {"
" class vector {};"
"}",
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace())));

EXPECT_TRUE(
matches("namespace foo {"
" namespace {"
" class vector {};"
" }"
"}",
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace())));

EXPECT_TRUE(
matches("namespace {"
" namespace foo {"
" class vector {};"
" }"
"}",
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace())));
}

TEST_P(ASTMatchersTest, InStdNamespace_CXX11) {
if (!GetParam().isCXX11OrLater()) {
return;
Expand Down

0 comments on commit 125ccd3

Please sign in to comment.