From 353f3ca4e997f658c65570e75749220e1abcad72 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Tue, 7 Jan 2025 19:13:26 +0100 Subject: [PATCH 1/2] [Clang][ASTMatcher] Extend `hasDependentName` matcher to match DependentNameType name --- clang/docs/LibASTMatchersReference.html | 15 ++++++++++++++ clang/docs/ReleaseNotes.rst | 2 +- clang/include/clang/ASTMatchers/ASTMatchers.h | 20 +++++++++++++++---- .../clang/ASTMatchers/ASTMatchersInternal.h | 8 ++++++++ .../ASTMatchers/ASTMatchersNarrowingTest.cpp | 8 ++++++++ 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index 18f9e7d6c0ea0..48dfd9cac0033 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -3462,6 +3462,21 @@

Narrowing Matchers

+Matcher<DependentNameType>hasDependentNamestd::string N +
Matches the dependent name of a DependentNameType.
+
+Matches the dependent name of a DependentNameType
+
+Given:
+
+  template <typename T< struct declToImport {
+    typedef typename T::type dependent_name;
+  };
+
+dependentNameType(hasDependentName("type")) matches `T::type`
+
+ + 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 93915e5db7d13..2258452d07ec5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1130,7 +1130,7 @@ AST Matchers
 
 - Add ``dependentTemplateSpecializationType`` matcher to match a dependent template specialization type.
 
-- Add ``hasDependentName`` matcher to match the dependent name of a DependentScopeDeclRefExpr.
+- Add ``hasDependentName`` matcher to match the dependent name of a DependentScopeDeclRefExpr or DependentNameType.
 
 clang-format
 ------------
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index f10135d7a901f..f32170c93bee2 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3257,15 +3257,27 @@ AST_MATCHER_P(CXXDependentScopeMemberExpr, memberHasSameNameAsBoundNode,
       });
 }
 
-/// Matches the dependent name of a DependentScopeDeclRefExpr
+/// Matches the dependent name of a DependentScopeDeclRefExpr or
+/// DependentNameType
 ///
 /// 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;
+///
+/// Given:
+/// \code
+///  template  struct declToImport {
+///    typedef typename T::type dependent_name;
+///  };
+/// \endcode
+/// \c dependentNameType(hasDependentName("type")) matches `T::type`
+AST_POLYMORPHIC_MATCHER_P(hasDependentName,
+                          AST_POLYMORPHIC_SUPPORTED_TYPES(
+                              DependentScopeDeclRefExpr, DependentNameType),
+                          std::string, N) {
+  return internal::getDependentName(Node) == N;
 }
 
 /// Matches C++ classes that are directly or indirectly derived from a class
@@ -7724,7 +7736,7 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher,
 
 /// Matches a dependent name type
 ///
-/// Example matches  T::type
+/// Example matches T::type
 /// \code
 ///  template  struct declToImport {
 ///    typedef typename T::type dependent_name;
diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index 04804d5def046..1f7b5e7cac846 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -2343,6 +2343,14 @@ MatchTemplateArgLocAt(const TemplateSpecializationTypeLoc &Node,
          InnerMatcher.matches(Node.getArgLoc(Index), Finder, Builder);
 }
 
+inline std::string getDependentName(const DependentScopeDeclRefExpr &node) {
+  return node.getDeclName().getAsString();
+}
+
+inline std::string getDependentName(const DependentNameType &node) {
+  return node.getIdentifier()->getName().str();
+}
+
 } // namespace internal
 
 } // namespace ast_matchers
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index f3d953454173c..b2cd0dbd28ae1 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2251,6 +2251,14 @@ TEST_P(ASTMatchersTest, HasDependentName_DependentScopeDeclRefExpr) {
   EXPECT_TRUE(matches("template  struct S { static T foo(); };"
                       "template  void x() { S::foo(); }",
                       dependentScopeDeclRefExpr(hasDependentName("foo"))));
+
+  EXPECT_TRUE(matches(
+      R"(
+        template  struct declToImport {
+          typedef typename T::type dependent_name;
+        };
+      )",
+      dependentNameType(hasDependentName(("type")))));
 }
 
 TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {

From 70751cc080e7da7e4f3a87eabcae654e7aaf8672 Mon Sep 17 00:00:00 2001
From: AmrDeveloper 
Date: Wed, 8 Jan 2025 17:16:02 +0100
Subject: [PATCH 2/2] Create a test function for has dependent name for
 DependentNameType

---
 clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index b2cd0dbd28ae1..92ec79d126575 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2251,6 +2251,13 @@ TEST_P(ASTMatchersTest, HasDependentName_DependentScopeDeclRefExpr) {
   EXPECT_TRUE(matches("template  struct S { static T foo(); };"
                       "template  void x() { S::foo(); }",
                       dependentScopeDeclRefExpr(hasDependentName("foo"))));
+}
+
+TEST_P(ASTMatchersTest, HasDependentName_DependentNameType) {
+  if (!GetParam().isCXX()) {
+    // FIXME: Fix this test to work with delayed template parsing.
+    return;
+  }
 
   EXPECT_TRUE(matches(
       R"(
@@ -2258,7 +2265,7 @@ TEST_P(ASTMatchersTest, HasDependentName_DependentScopeDeclRefExpr) {
           typedef typename T::type dependent_name;
         };
       )",
-      dependentNameType(hasDependentName(("type")))));
+      dependentNameType(hasDependentName("type"))));
 }
 
 TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {