From bdbd11db849506c4d99036dd674f03d1eda815cc Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Sat, 28 Dec 2024 13:23:33 +0100 Subject: [PATCH 1/3] [Clang][ASTMatcher] Add `dependentNameType` Matcher --- clang/docs/LibASTMatchersReference.html | 9 +++++++++ clang/docs/ReleaseNotes.rst | 2 ++ clang/include/clang/ASTMatchers/ASTMatchers.h | 10 ++++++++++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp | 1 + clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 + clang/unittests/AST/ASTImporterTest.cpp | 3 --- .../unittests/ASTMatchers/ASTMatchersNodeTest.cpp | 15 +++++++++++++++ 7 files changed, 38 insertions(+), 3 deletions(-) diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index ddc99020604c9..69fd43b211472 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -2536,6 +2536,15 @@

Node Matchers

matches "decltype(i + j)" +Matcher<Stmt>dependentNameTypeMatcher<DependentNameType>... +
Matches dependent name type.
+
+Example matches T::type
+
+  template  struct declToImport {
+    typedef typename T::type dependent_name;
+  };
+
Matcher<Type>deducedTemplateSpecializationTypeMatcher<DeducedTemplateSpecializationType>...
Matches C++17 deduced template specialization types, e.g. deduced class
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 983c1da20ed4c..7446aaf57a02d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1110,6 +1110,8 @@ AST Matchers
 
 - Add ``dependentScopeDeclRefExpr`` matcher to match expressions that refer to dependent scope declarations.
 
+- Add ``dependentNameType`` matcher to match dependent name type.
+
 clang-format
 ------------
 
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 22e2546ab81e0..b27914306b827 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7711,6 +7711,16 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher,
   return InnerType.matches(Node.getDecayedType(), Finder, Builder);
 }
 
+/// Matches dependent name type
+///
+/// Example matches  T::type
+/// \code
+///  template  struct declToImport {
+///    typedef typename T::type dependent_name;
+///  };
+/// \endcode
+extern const AstTypeMatcher dependentNameType;
+
 /// Matches declarations whose declaration context, interpreted as a
 /// Decl, matches \c InnerMatcher.
 ///
diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 8c744eebbdfb5..a47633bf4bae2 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -1108,6 +1108,7 @@ const AstTypeMatcher substTemplateTypeParmType;
 const AstTypeMatcher templateTypeParmType;
 const AstTypeMatcher injectedClassNameType;
 const AstTypeMatcher decayedType;
+const AstTypeMatcher dependentNameType;
 AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasElementType,
                                  AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType,
                                                                  ComplexType));
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 685d626d2978b..674129aee5924 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -218,6 +218,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(cxxTryStmt);
   REGISTER_MATCHER(cxxUnresolvedConstructExpr);
   REGISTER_MATCHER(decayedType);
+  REGISTER_MATCHER(dependentNameType);
   REGISTER_MATCHER(decl);
   REGISTER_MATCHER(decompositionDecl);
   REGISTER_MATCHER(declCountIs);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index ec062a5cc953b..ee1d896f1ca6d 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -3196,9 +3196,6 @@ TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
                  has(callExpr(has(dependentScopeDeclRefExpr())))))))));
 }
 
-const internal::VariadicDynCastAllOfMatcher
-    dependentNameType;
-
 TEST_P(ImportExpr, DependentNameType) {
   MatchVerifier Verifier;
   testImport("template  struct declToImport {"
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index a3baad367a27b..6dfa3d9c0992f 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -1912,6 +1912,21 @@ TEST_P(ASTMatchersTest, DeducedTemplateSpecializationType) {
               deducedTemplateSpecializationType()));
 }
 
+TEST_P(ASTMatchersTest, DependentNameType) {
+  if (!GetParam().isCXX()) {
+    // FIXME: Add a test for `dependentNameType()` that does not depend on C++.
+    return;
+  }
+
+  EXPECT_TRUE(matches(
+      R"(
+        template  struct declToImport {
+          typedef typename T::type dependent_name;
+        };
+      )",
+      dependentNameType()));
+}
+
 TEST_P(ASTMatchersTest, RecordType) {
   EXPECT_TRUE(matches("struct S {}; struct S s;",
                       recordType(hasDeclaration(recordDecl(hasName("S"))))));

From 73ff8bb835abedf3c3c006937a78b88f8853ffa4 Mon Sep 17 00:00:00 2001
From: AmrDeveloper 
Date: Sat, 28 Dec 2024 16:36:45 +0100
Subject: [PATCH 2/3] Address code review comments

---
 clang/docs/LibASTMatchersReference.html       | 2 +-
 clang/docs/ReleaseNotes.rst                   | 2 +-
 clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +-
 clang/lib/ASTMatchers/Dynamic/Registry.cpp    | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index 69fd43b211472..6f167826fcef6 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2537,7 +2537,7 @@ 

Node Matchers

Matcher<Stmt>dependentNameTypeMatcher<DependentNameType>... -
Matches dependent name type.
+
Matches a dependent name type.
 
 Example matches T::type
 
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7446aaf57a02d..210ccc16eeb4f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1110,7 +1110,7 @@ AST Matchers
 
 - Add ``dependentScopeDeclRefExpr`` matcher to match expressions that refer to dependent scope declarations.
 
-- Add ``dependentNameType`` matcher to match dependent name type.
+- Add ``dependentNameType`` matcher to match a dependent name type.
 
 clang-format
 ------------
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index b27914306b827..9a046714068a5 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7711,7 +7711,7 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher,
   return InnerType.matches(Node.getDecayedType(), Finder, Builder);
 }
 
-/// Matches dependent name type
+/// Matches a dependent name type
 ///
 /// Example matches  T::type
 /// \code
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 674129aee5924..bfdee412c5328 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -218,11 +218,11 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(cxxTryStmt);
   REGISTER_MATCHER(cxxUnresolvedConstructExpr);
   REGISTER_MATCHER(decayedType);
-  REGISTER_MATCHER(dependentNameType);
   REGISTER_MATCHER(decl);
   REGISTER_MATCHER(decompositionDecl);
   REGISTER_MATCHER(declCountIs);
   REGISTER_MATCHER(declRefExpr);
+  REGISTER_MATCHER(dependentNameType);
   REGISTER_MATCHER(dependentScopeDeclRefExpr);
   REGISTER_MATCHER(declStmt);
   REGISTER_MATCHER(declaratorDecl);

From 6cf15761925415bcca85ef5571a18c850909606b Mon Sep 17 00:00:00 2001
From: AmrDeveloper 
Date: Sun, 29 Dec 2024 10:18:32 +0100
Subject: [PATCH 3/3] Address code review comments

---
 clang/docs/LibASTMatchersReference.html             | 2 +-
 clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index 6f167826fcef6..8564f2650d205 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2536,7 +2536,7 @@ 

Node Matchers

matches "decltype(i + j)"
-Matcher<Stmt>dependentNameTypeMatcher<DependentNameType>... +Matcher<Type>dependentNameTypeMatcher<DependentNameType>...
Matches a dependent name type.
 
 Example matches T::type
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
index 6dfa3d9c0992f..b8521e2f95768 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -1914,7 +1914,6 @@ TEST_P(ASTMatchersTest, DeducedTemplateSpecializationType) {
 
 TEST_P(ASTMatchersTest, DependentNameType) {
   if (!GetParam().isCXX()) {
-    // FIXME: Add a test for `dependentNameType()` that does not depend on C++.
     return;
   }