Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions clang/docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,18 @@ <h2 id="decl-matchers">Node Matchers</h2>
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1TypeLoc.html">TypeLoc</a>&gt;</td><td class="name" onclick="toggle('arrayTypeLoc0')"><a name="arrayTypeLoc0Anchor">arrayTypeLoc</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1ArrayTypeLoc.html">ArrayTypeLoc</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="arrayTypeLoc0"><pre>Matches `ArrayTypeLoc`s.

Given
int a[] = {1, 2};
int b[3];
void f() { int c[a[0]]; }
arrayTypeLoc()
matches "int a[]", "int b[3]" and "int c[a[0]]".
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1TypeLoc.html">TypeLoc</a>&gt;</td><td class="name" onclick="toggle('pointerTypeLoc0')"><a name="pointerTypeLoc0Anchor">pointerTypeLoc</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1PointerTypeLoc.html">PointerTypeLoc</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="pointerTypeLoc0"><pre>Matches pointer `TypeLoc`s.

Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ AST Matchers
- Fixed detection of explicit parameter lists in ``LambdaExpr``. (#GH168452)
- Added ``hasExplicitParameters`` for ``LambdaExpr`` as an output attribute to
AST JSON dumps.
- Add ``arrayTypeLoc`` matcher for matching ``ArrayTypeLoc``.

clang-format
------------
Expand Down
13 changes: 13 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7003,6 +7003,19 @@ AST_MATCHER_P(ReferenceTypeLoc, hasReferentLoc, internal::Matcher<TypeLoc>,
return ReferentMatcher.matches(Node.getPointeeLoc(), Finder, Builder);
}

/// Matches `ArrayTypeLoc`s.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be sure to run clang/docs/tools/dump_ast_matchers.py to regenerate the documentation for the new matcher.

You should also update clang/lib/ASTMatchers/Dynamic/Registry.cpp to add the dynamic matcher at the same time.

And finally, this should come with a release note in clang/docs/ReleaseNotes.rst.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done.

///
/// Given
/// \code
/// int a[] = {1, 2};
/// int b[3];
/// void f() { int c[a[0]]; }
/// \endcode
/// arrayTypeLoc()
/// matches "int a[]", "int b[3]" and "int c[a[0]]".
extern const internal::VariadicDynCastAllOfMatcher<TypeLoc, ArrayTypeLoc>
arrayTypeLoc;

/// Matches template specialization `TypeLoc`s.
///
/// Given
Expand Down
1 change: 1 addition & 0 deletions clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ const internal::VariadicDynCastAllOfMatcher<TypeLoc, PointerTypeLoc>
pointerTypeLoc;
const internal::VariadicDynCastAllOfMatcher<TypeLoc, ReferenceTypeLoc>
referenceTypeLoc;
const internal::VariadicDynCastAllOfMatcher<TypeLoc, ArrayTypeLoc> arrayTypeLoc;
const internal::VariadicDynCastAllOfMatcher<TypeLoc,
TemplateSpecializationTypeLoc>
templateSpecializationTypeLoc;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/ASTMatchers/Dynamic/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(argumentCountAtLeast);
REGISTER_MATCHER(arraySubscriptExpr);
REGISTER_MATCHER(arrayType);
REGISTER_MATCHER(arrayTypeLoc);
REGISTER_MATCHER(asString);
REGISTER_MATCHER(asmStmt);
REGISTER_MATCHER(atomicExpr);
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,26 @@ TEST_P(ASTMatchersTest, ReferenceTypeLocTest_BindsToAnyRvalueReferenceTypeLoc) {
EXPECT_TRUE(matches("float&& r = 3.0;", matcher));
}

TEST_P(ASTMatchersTest, ArrayTypeLocTest_BindsToAnyArrayTypeLoc) {
auto matcher = varDecl(hasName("x"), hasTypeLoc(arrayTypeLoc()));
EXPECT_TRUE(matches("int x[3];", matcher));
EXPECT_TRUE(matches("float x[3];", matcher));
EXPECT_TRUE(matches("char x[3];", matcher));
EXPECT_TRUE(matches("void* x[3];", matcher));
EXPECT_TRUE(matches("const int x[3] = {1, 2, 3};", matcher));
EXPECT_TRUE(matches("int x[3][4];", matcher));
EXPECT_TRUE(matches("void foo(int x[]);", matcher));
EXPECT_TRUE(matches("int a[] = {1, 2}; void foo() {int x[a[0]];}", matcher));
}

TEST_P(ASTMatchersTest, ArrayTypeLocTest_DoesNotBindToNonArrayTypeLoc) {
auto matcher = varDecl(hasName("x"), hasTypeLoc(arrayTypeLoc()));
EXPECT_TRUE(notMatches("int x;", matcher));
EXPECT_TRUE(notMatches("float x;", matcher));
EXPECT_TRUE(notMatches("char x;", matcher));
EXPECT_TRUE(notMatches("void* x;", matcher));
}

TEST_P(ASTMatchersTest,
TemplateSpecializationTypeLocTest_BindsToVarDeclTemplateSpecialization) {
if (!GetParam().isCXX()) {
Expand Down
Loading