[clang] [clang][ASTMatchers] Add `arrayTypeLoc` ast matcher for ArrayTypeLoc (PR #168990)
Yu Hao via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 21 12:18:00 PST 2025
https://github.com/yuhaouy updated https://github.com/llvm/llvm-project/pull/168990
>From e0837e401e2dbe2431efe2b3b8164c748c76e4fe Mon Sep 17 00:00:00 2001
From: Yu Hao <yuhaoyu at google.com>
Date: Thu, 20 Nov 2025 18:24:13 -0800
Subject: [PATCH 1/7] Introduce arrayTypeLoc ast matcher for ArrayTypeLoc
---
clang/include/clang/ASTMatchers/ASTMatchers.h | 13 ++++++++++++
clang/lib/ASTMatchers/ASTMatchersInternal.cpp | 2 ++
.../ASTMatchers/ASTMatchersNodeTest.cpp | 20 +++++++++++++++++++
3 files changed, 35 insertions(+)
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()) {
>From e67c7000ab2a6b9997524972c3404fcc8f3c709c Mon Sep 17 00:00:00 2001
From: Yu Hao <yuhaoyu at google.com>
Date: Thu, 20 Nov 2025 18:42:18 -0800
Subject: [PATCH 2/7] Fix format issue
---
clang/lib/ASTMatchers/ASTMatchersInternal.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 13696a6fc71f2..fbb8b49676045 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -807,8 +807,7 @@ const internal::VariadicDynCastAllOfMatcher<TypeLoc, PointerTypeLoc>
pointerTypeLoc;
const internal::VariadicDynCastAllOfMatcher<TypeLoc, ReferenceTypeLoc>
referenceTypeLoc;
-const internal::VariadicDynCastAllOfMatcher<TypeLoc, ArrayTypeLoc>
- arrayTypeLoc;
+const internal::VariadicDynCastAllOfMatcher<TypeLoc, ArrayTypeLoc> arrayTypeLoc;
const internal::VariadicDynCastAllOfMatcher<TypeLoc,
TemplateSpecializationTypeLoc>
templateSpecializationTypeLoc;
>From 507288987a493be8e33385cdafcdb245d53ab1d9 Mon Sep 17 00:00:00 2001
From: Yu Hao <yuhaoyu at google.com>
Date: Fri, 21 Nov 2025 10:56:22 -0800
Subject: [PATCH 3/7] add arrayTypeLoc to the dynamic matchers
---
clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 66848f7c42127..d1b19659905ca 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -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);
>From c0d1b52e21ef69c180832c4a5c1906185e3a1780 Mon Sep 17 00:00:00 2001
From: Yu Hao <yuhaoyu at google.com>
Date: Fri, 21 Nov 2025 10:57:56 -0800
Subject: [PATCH 4/7] update ast matchers reference doc
---
clang/docs/LibASTMatchersReference.html | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index ac1abb4d9f381..e34ac30b8f5a4 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2449,6 +2449,18 @@ <h2 id="decl-matchers">Node Matchers</h2>
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1TypeLoc.html">TypeLoc</a>></td><td class="name" onclick="toggle('arrayTypeLoc0')"><a name="arrayTypeLoc0Anchor">arrayTypeLoc</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1ArrayTypeLoc.html">ArrayTypeLoc</a>>...</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<<a href="https://clang.llvm.org/doxygen/classclang_1_1TypeLoc.html">TypeLoc</a>></td><td class="name" onclick="toggle('pointerTypeLoc0')"><a name="pointerTypeLoc0Anchor">pointerTypeLoc</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1PointerTypeLoc.html">PointerTypeLoc</a>>...</td></tr>
<tr><td colspan="4" class="doc" id="pointerTypeLoc0"><pre>Matches pointer `TypeLoc`s.
>From 54f46dc29a75f1998598d7ee359b8092f1d0d0e3 Mon Sep 17 00:00:00 2001
From: Yu Hao <yuhaoyu at google.com>
Date: Fri, 21 Nov 2025 11:17:30 -0800
Subject: [PATCH 5/7] Update clang release notes
---
clang/docs/ReleaseNotes.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 62c8c0130c3d0..fd1a096c1242d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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`s.
clang-format
------------
>From 27b863a14d46c50aee279ef7b50daa514595d7c6 Mon Sep 17 00:00:00 2001
From: Yu Hao <yuhaoyu at google.com>
Date: Fri, 21 Nov 2025 11:29:19 -0800
Subject: [PATCH 6/7] fix release notes erors
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fd1a096c1242d..23f83affe3ac3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -687,7 +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`s.
+- Add ``arrayTypeLoc`` matcher for matching ``ArrayTypeLoc``s.
clang-format
------------
>From e984e2ff41ec8917fb8963a43bfcd2ad6a5e0786 Mon Sep 17 00:00:00 2001
From: Yu Hao <yuhaoyu at google.com>
Date: Fri, 21 Nov 2025 12:17:44 -0800
Subject: [PATCH 7/7] fix release note error due to the letter after the inline
markup
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 23f83affe3ac3..d67f5f302a67d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -687,7 +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``s.
+- Add ``arrayTypeLoc`` matcher for matching ``ArrayTypeLoc``.
clang-format
------------
More information about the cfe-commits
mailing list