[clang] [Clang][ASTMatcher] Add a matcher for the name of a DependentScopeDeclRefExpr (PR #121656)
Nathan Ridge via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 5 17:49:55 PST 2025
https://github.com/HighCommander4 updated https://github.com/llvm/llvm-project/pull/121656
>From 4cd48886384570e0e5bb0b712f4fd45f8decd27e Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sat, 4 Jan 2025 17:36:05 +0100
Subject: [PATCH 1/4] [Clang][ASTMatcher] Add a matcher for the name of a
DependentScopeDeclRefExpr
---
clang/docs/LibASTMatchersReference.html | 13 +++++++++++++
clang/docs/ReleaseNotes.rst | 2 ++
clang/include/clang/ASTMatchers/ASTMatchers.h | 11 +++++++++++
clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 +
.../ASTMatchers/ASTMatchersNarrowingTest.cpp | 15 +++++++++++++++
5 files changed, 42 insertions(+)
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index fc557888013254..6a03aeb5eec2a1 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -3449,6 +3449,19 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentScopeDeclRefExpr.html">DependentScopeDeclRefExpr</a>></td><td class="name" onclick="toggle('hasDependentName0')"><a name="hasDependentName0Anchor">hasDependentName</a></td><td>std::string N</td></tr>
+<tr><td colspan="4" class="doc" id="hasDependentName0"><pre>Matches the dependent name of a dependent scope decl ref expr.
+
+Matches the dependent name of a dependent scope decl ref expr
+
+Given:
+
+ template <class T< class X : T { void f() { T::v; } };
+
+dependentScopeDeclRefExpr(hasDependentName("v")) matches `T::v`
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXDependentScopeMemberExpr.html">CXXDependentScopeMemberExpr</a>></td><td class="name" onclick="toggle('memberHasSameNameAsBoundNode0')"><a name="memberHasSameNameAsBoundNode0Anchor">memberHasSameNameAsBoundNode</a></td><td>std::string BindingID</td></tr>
<tr><td colspan="4" class="doc" id="memberHasSameNameAsBoundNode0"><pre>Matches template-dependent, but known, member names against an already-bound
node
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5e75fc447636e0..4ef69ca4c743ed 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1116,6 +1116,8 @@ AST Matchers
- Add ``dependentTemplateSpecializationType`` matcher to match a dependent template specialization type.
+- Add ``hasDependentName`` matcher to match the dependent name of a dependent scope decl ref expr.
+
clang-format
------------
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index dd0fedb2cda2d4..6828fc6da1d5cf 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3257,6 +3257,17 @@ AST_MATCHER_P(CXXDependentScopeMemberExpr, memberHasSameNameAsBoundNode,
});
}
+/// Matches the dependent name of a dependent scope decl ref expr
+///
+/// Given:
+/// \code
+/// template <class T> class X : T { void f() { T::v; } };
+/// \endcode
+/// \c dependentScopeDeclRefExpr(hasDependentName("v")) matches `T::v`
+AST_MATCHER_P(DependentScopeDeclRefExpr, hasDependentName, std::string, N) {
+ return Node.getDeclName().getAsString() == N;
+}
+
/// Matches C++ classes that are directly or indirectly derived from a class
/// matching \c Base, or Objective-C classes that directly or indirectly
/// subclass a class matching \c Base.
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 97e6bbc093fe46..336d3a14f79559 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -314,6 +314,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(hasDeducedType);
REGISTER_MATCHER(hasDefaultArgument);
REGISTER_MATCHER(hasDefinition);
+ REGISTER_MATCHER(hasDependentName);
REGISTER_MATCHER(hasDescendant);
REGISTER_MATCHER(hasDestinationType);
REGISTER_MATCHER(hasDirectBase);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 056b7c7b571ef4..4278e3d4fe5959 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2235,6 +2235,21 @@ TEST_P(ASTMatchersTest, ArgumentCountIs_CXXConstructExpr) {
Constructor1Arg));
}
+TEST_P(ASTMatchersTest, hasDependentName_DependentScopeDeclRefExpr) {
+ if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) {
+ // FIXME: Fix this test to work with delayed template parsing.
+ return;
+ }
+
+ EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };",
+ dependentScopeDeclRefExpr(hasDependentName("v"))));
+
+ EXPECT_TRUE(
+ matches("template <typename T> struct S { static T Foo; };"
+ "template <typename T> void declToImport() { (void)S<T>::Foo; }",
+ dependentScopeDeclRefExpr(hasDependentName("Foo"))));
+}
+
TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {
// Member functions:
>From e653a695260143304b71af662f98b1826082d2f6 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sun, 5 Jan 2025 13:42:55 +0100
Subject: [PATCH 2/4] Address code review comments
---
clang/docs/LibASTMatchersReference.html | 4 ++--
clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +-
.../ASTMatchers/ASTMatchersNarrowingTest.cpp | 13 ++++++++-----
3 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index 6a03aeb5eec2a1..18f9e7d6c0ea06 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -3450,9 +3450,9 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentScopeDeclRefExpr.html">DependentScopeDeclRefExpr</a>></td><td class="name" onclick="toggle('hasDependentName0')"><a name="hasDependentName0Anchor">hasDependentName</a></td><td>std::string N</td></tr>
-<tr><td colspan="4" class="doc" id="hasDependentName0"><pre>Matches the dependent name of a dependent scope decl ref expr.
+<tr><td colspan="4" class="doc" id="hasDependentName0"><pre>Matches the dependent name of a DependentScopeDeclRefExpr.
-Matches the dependent name of a dependent scope decl ref expr
+Matches the dependent name of a DependentScopeDeclRefExpr
Given:
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 6828fc6da1d5cf..f10135d7a901f1 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3257,7 +3257,7 @@ AST_MATCHER_P(CXXDependentScopeMemberExpr, memberHasSameNameAsBoundNode,
});
}
-/// Matches the dependent name of a dependent scope decl ref expr
+/// Matches the dependent name of a DependentScopeDeclRefExpr
///
/// Given:
/// \code
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 4278e3d4fe5959..a6554a8238cee5 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2235,7 +2235,7 @@ TEST_P(ASTMatchersTest, ArgumentCountIs_CXXConstructExpr) {
Constructor1Arg));
}
-TEST_P(ASTMatchersTest, hasDependentName_DependentScopeDeclRefExpr) {
+TEST_P(ASTMatchersTest, HasDependentName_DependentScopeDeclRefExpr) {
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) {
// FIXME: Fix this test to work with delayed template parsing.
return;
@@ -2244,10 +2244,13 @@ TEST_P(ASTMatchersTest, hasDependentName_DependentScopeDeclRefExpr) {
EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };",
dependentScopeDeclRefExpr(hasDependentName("v"))));
- EXPECT_TRUE(
- matches("template <typename T> struct S { static T Foo; };"
- "template <typename T> void declToImport() { (void)S<T>::Foo; }",
- dependentScopeDeclRefExpr(hasDependentName("Foo"))));
+ EXPECT_TRUE(matches("template <typename T> struct S { static T Foo; };"
+ "template <typename T> void x() { (void)S<T>::Foo; }",
+ dependentScopeDeclRefExpr(hasDependentName("Foo"))));
+
+ EXPECT_TRUE(matches("template <typename T> struct S { static T foo(); };"
+ "template <typename T> void x() { S<T>::foo; }",
+ dependentScopeDeclRefExpr(hasDependentName("foo"))));
}
TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {
>From 3ad4bd065d3dfa181f5b679b65c3b87612764f24 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sun, 5 Jan 2025 13:45:49 +0100
Subject: [PATCH 3/4] Update release notes
---
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 4ef69ca4c743ed..f2b27893b7a9db 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1116,7 +1116,7 @@ AST Matchers
- Add ``dependentTemplateSpecializationType`` matcher to match a dependent template specialization type.
-- Add ``hasDependentName`` matcher to match the dependent name of a dependent scope decl ref expr.
+- Add ``hasDependentName`` matcher to match the dependent name of a DependentScopeDeclRefExpr.
clang-format
------------
>From 27bb7d12637d29fa1aeed8896e814da3e771ff22 Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Sun, 5 Jan 2025 20:49:46 -0500
Subject: [PATCH 4/4] Call static member function in hasDependentName test
---
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index a6554a8238cee5..f3d953454173ca 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2249,7 +2249,7 @@ TEST_P(ASTMatchersTest, HasDependentName_DependentScopeDeclRefExpr) {
dependentScopeDeclRefExpr(hasDependentName("Foo"))));
EXPECT_TRUE(matches("template <typename T> struct S { static T foo(); };"
- "template <typename T> void x() { S<T>::foo; }",
+ "template <typename T> void x() { S<T>::foo(); }",
dependentScopeDeclRefExpr(hasDependentName("foo"))));
}
More information about the cfe-commits
mailing list