From 4cd48886384570e0e5bb0b712f4fd45f8decd27e Mon Sep 17 00:00:00 2001 From: AmrDeveloper 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 fc55788801325..6a03aeb5eec2a 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -3449,6 +3449,19 @@

Narrowing Matchers

+Matcher<DependentScopeDeclRefExpr>hasDependentNamestd::string N +
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`
+
+ + Matcher<CXXDependentScopeMemberExpr>memberHasSameNameAsBoundNodestd::string BindingID
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 5e75fc447636e..4ef69ca4c743e 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 dd0fedb2cda2d..6828fc6da1d5c 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 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 97e6bbc093fe4..336d3a14f7955 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 056b7c7b571ef..4278e3d4fe595 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 X : T { void f() { T::v; } };",
+                      dependentScopeDeclRefExpr(hasDependentName("v"))));
+
+  EXPECT_TRUE(
+      matches("template  struct S { static T Foo; };"
+              "template  void declToImport() { (void)S::Foo; }",
+              dependentScopeDeclRefExpr(hasDependentName("Foo"))));
+}
+
 TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {
 
   // Member functions:

From e653a695260143304b71af662f98b1826082d2f6 Mon Sep 17 00:00:00 2001
From: AmrDeveloper 
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 6a03aeb5eec2a..18f9e7d6c0ea0 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -3450,9 +3450,9 @@ 

Narrowing Matchers

Matcher<DependentScopeDeclRefExpr>hasDependentNamestd::string N -
Matches the dependent name of a dependent scope decl ref expr.
+
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 6828fc6da1d5c..f10135d7a901f 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 4278e3d4fe595..a6554a8238cee 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 X : T { void f() { T::v; } };",
                       dependentScopeDeclRefExpr(hasDependentName("v"))));
 
-  EXPECT_TRUE(
-      matches("template  struct S { static T Foo; };"
-              "template  void declToImport() { (void)S::Foo; }",
-              dependentScopeDeclRefExpr(hasDependentName("Foo"))));
+  EXPECT_TRUE(matches("template  struct S { static T Foo; };"
+                      "template  void x() { (void)S::Foo; }",
+                      dependentScopeDeclRefExpr(hasDependentName("Foo"))));
+
+  EXPECT_TRUE(matches("template  struct S { static T foo(); };"
+                      "template  void x() { S::foo; }",
+                      dependentScopeDeclRefExpr(hasDependentName("foo"))));
 }
 
 TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {

From 3ad4bd065d3dfa181f5b679b65c3b87612764f24 Mon Sep 17 00:00:00 2001
From: AmrDeveloper 
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 4ef69ca4c743e..f2b27893b7a9d 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 
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 a6554a8238cee..f3d953454173c 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  struct S { static T foo(); };"
-                      "template  void x() { S::foo; }",
+                      "template  void x() { S::foo(); }",
                       dependentScopeDeclRefExpr(hasDependentName("foo"))));
 }