-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang][ASTMatchers] Add arrayTypeLoc ast matcher for ArrayTypeLoc
#168990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@llvm/pr-subscribers-clang Author: Yu Hao (yuhaouy) ChangesThere's Note that there's already Full diff: https://github.com/llvm/llvm-project/pull/168990.diff 3 Files Affected:
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index bca2d8425b3f5..e3ec26207d2bc 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7003,6 +7003,19 @@ AST_MATCHER_P(ReferenceTypeLoc, hasReferentLoc, internal::Matcher<TypeLoc>,
return ReferentMatcher.matches(Node.getPointeeLoc(), Finder, Builder);
}
+/// Matches `ArrayTypeLoc`s.
+///
+/// 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
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 0874b3d0c45f5..13696a6fc71f2 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -807,6 +807,8 @@ const internal::VariadicDynCastAllOfMatcher<TypeLoc, PointerTypeLoc>
pointerTypeLoc;
const internal::VariadicDynCastAllOfMatcher<TypeLoc, ReferenceTypeLoc>
referenceTypeLoc;
+const internal::VariadicDynCastAllOfMatcher<TypeLoc, ArrayTypeLoc>
+ arrayTypeLoc;
const internal::VariadicDynCastAllOfMatcher<TypeLoc,
TemplateSpecializationTypeLoc>
templateSpecializationTypeLoc;
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index 3fcb5582d3dd7..108b32e5d91b9 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -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()) {
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
🐧 Linux x64 Test Results
|
| return ReferentMatcher.matches(Node.getPointeeLoc(), Finder, Builder); | ||
| } | ||
|
|
||
| /// Matches `ArrayTypeLoc`s. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Done.
arrayTypeLoc ast matcher for ArrayTypeLocarrayTypeLoc ast matcher for ArrayTypeLoc
There's
arrayTypematcher for matchingArrayType, but no matcher forArrayTypeLoc. This change complements it.Note that there's already
hasElementTypeLocmatcher, which was declared together with thehasElementTypematcher.