https://github.com/AmrDeveloper created https://github.com/llvm/llvm-project/pull/121263
Add AST Matcher for `DependentNameType` Fixes: https://github.com/llvm/llvm-project/issues/121240 >From 21873d01238f8da0eb0f19656801347400afae65 Mon Sep 17 00:00:00 2001 From: AmrDeveloper <am...@programmer.net> Date: Sat, 28 Dec 2024 13:23:33 +0100 Subject: [PATCH] [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 ddc99020604c94..69fd43b2114723 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -2536,6 +2536,15 @@ <h2 id="decl-matchers">Node Matchers</h2> matches "decltype(i + j)" </pre></td></tr> +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('dependentNameType0')"><a name="dependentNameType0Anchor">dependentNameType</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentNameType.html">DependentNameType</a>>...</td></tr> +<tr><td colspan="4" class="doc" id="dependentNameType0"><pre>Matches dependent name type. + +Example matches T::type + + template <typename T> struct declToImport { + typedef typename T::type dependent_name; + }; +</pre></td></tr> <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td><td class="name" onclick="toggle('deducedTemplateSpecializationType0')"><a name="deducedTemplateSpecializationType0Anchor">deducedTemplateSpecializationType</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1DeducedTemplateSpecializationType.html">DeducedTemplateSpecializationType</a>>...</td></tr> <tr><td colspan="4" class="doc" id="deducedTemplateSpecializationType0"><pre>Matches C++17 deduced template specialization types, e.g. deduced class diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 983c1da20ed4c8..7446aaf57a02dc 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 22e2546ab81e0a..b27914306b8270 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<QualType>, return InnerType.matches(Node.getDecayedType(), Finder, Builder); } +/// Matches dependent name type +/// +/// Example matches T::type +/// \code +/// template <typename T> struct declToImport { +/// typedef typename T::type dependent_name; +/// }; +/// \endcode +extern const AstTypeMatcher<DependentNameType> 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 8c744eebbdfb50..a47633bf4bae24 100644 --- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -1108,6 +1108,7 @@ const AstTypeMatcher<SubstTemplateTypeParmType> substTemplateTypeParmType; const AstTypeMatcher<TemplateTypeParmType> templateTypeParmType; const AstTypeMatcher<InjectedClassNameType> injectedClassNameType; const AstTypeMatcher<DecayedType> decayedType; +const AstTypeMatcher<DependentNameType> 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 685d626d2978bf..674129aee59241 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 ec062a5cc953b8..ee1d896f1ca6dc 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<Type, DependentNameType> - dependentNameType; - TEST_P(ImportExpr, DependentNameType) { MatchVerifier<Decl> Verifier; testImport("template <typename T> struct declToImport {" diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp index a3baad367a27b1..0a5ecd29432c5f 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 <typename T> 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")))))); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits