Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AaronBallman I've add more changes based on your comments. Can you please take a look and merge the pull request?

///
/// 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
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