Skip to content

Commit

Permalink
[Clang][ASTMatcher] Add dependentNameType AST matcher (llvm#121263)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper authored and sunshaoce committed Dec 30, 2024
1 parent 672446f commit 989a0d7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 3 deletions.
9 changes: 9 additions & 0 deletions clang/docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,15 @@ <h2 id="decl-matchers">Node Matchers</h2>
matches "decltype(i + j)"
</pre></td></tr>

<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>&gt;</td><td class="name" onclick="toggle('dependentNameType0')"><a name="dependentNameType0Anchor">dependentNameType</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentNameType.html">DependentNameType</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="dependentNameType0"><pre>Matches a dependent name type.

Example matches T::type

template <typename T> struct declToImport {
typedef typename T::type dependent_name;
};
</pre></td></tr>

<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>&gt;</td><td class="name" onclick="toggle('deducedTemplateSpecializationType0')"><a name="deducedTemplateSpecializationType0Anchor">deducedTemplateSpecializationType</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DeducedTemplateSpecializationType.html">DeducedTemplateSpecializationType</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="deducedTemplateSpecializationType0"><pre>Matches C++17 deduced template specialization types, e.g. deduced class
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,8 @@ AST Matchers

- Add ``dependentScopeDeclRefExpr`` matcher to match expressions that refer to dependent scope declarations.

- Add ``dependentNameType`` matcher to match a dependent name type.

clang-format
------------

Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7711,6 +7711,16 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
return InnerType.matches(Node.getDecayedType(), Finder, Builder);
}

/// Matches a 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.
///
Expand Down
1 change: 1 addition & 0 deletions clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions clang/lib/ASTMatchers/Dynamic/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(decompositionDecl);
REGISTER_MATCHER(declCountIs);
REGISTER_MATCHER(declRefExpr);
REGISTER_MATCHER(dependentNameType);
REGISTER_MATCHER(dependentScopeDeclRefExpr);
REGISTER_MATCHER(declStmt);
REGISTER_MATCHER(declaratorDecl);
Expand Down
3 changes: 0 additions & 3 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {"
Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,20 @@ TEST_P(ASTMatchersTest, DeducedTemplateSpecializationType) {
deducedTemplateSpecializationType()));
}

TEST_P(ASTMatchersTest, DependentNameType) {
if (!GetParam().isCXX()) {
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"))))));
Expand Down

0 comments on commit 989a0d7

Please sign in to comment.