Skip to content

Commit 6230f1b

Browse files
authored
[Clang][ASTMatcher] Add dependentNameType AST matcher (#121263)
Fixes: #121240
1 parent 0b96f1c commit 6230f1b

File tree

7 files changed

+37
-3
lines changed

7 files changed

+37
-3
lines changed

clang/docs/LibASTMatchersReference.html

+9
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,15 @@ <h2 id="decl-matchers">Node Matchers</h2>
25362536
matches "decltype(i + j)"
25372537
</pre></td></tr>
25382538

2539+
<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>
2540+
<tr><td colspan="4" class="doc" id="dependentNameType0"><pre>Matches a dependent name type.
2541+
2542+
Example matches T::type
2543+
2544+
template <typename T> struct declToImport {
2545+
typedef typename T::type dependent_name;
2546+
};
2547+
</pre></td></tr>
25392548

25402549
<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>
25412550
<tr><td colspan="4" class="doc" id="deducedTemplateSpecializationType0"><pre>Matches C++17 deduced template specialization types, e.g. deduced class

clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,8 @@ AST Matchers
11101110

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

1113+
- Add ``dependentNameType`` matcher to match a dependent name type.
1114+
11131115
clang-format
11141116
------------
11151117

clang/include/clang/ASTMatchers/ASTMatchers.h

+10
Original file line numberDiff line numberDiff line change
@@ -7711,6 +7711,16 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
77117711
return InnerType.matches(Node.getDecayedType(), Finder, Builder);
77127712
}
77137713

7714+
/// Matches a dependent name type
7715+
///
7716+
/// Example matches T::type
7717+
/// \code
7718+
/// template <typename T> struct declToImport {
7719+
/// typedef typename T::type dependent_name;
7720+
/// };
7721+
/// \endcode
7722+
extern const AstTypeMatcher<DependentNameType> dependentNameType;
7723+
77147724
/// Matches declarations whose declaration context, interpreted as a
77157725
/// Decl, matches \c InnerMatcher.
77167726
///

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ const AstTypeMatcher<SubstTemplateTypeParmType> substTemplateTypeParmType;
11081108
const AstTypeMatcher<TemplateTypeParmType> templateTypeParmType;
11091109
const AstTypeMatcher<InjectedClassNameType> injectedClassNameType;
11101110
const AstTypeMatcher<DecayedType> decayedType;
1111+
const AstTypeMatcher<DependentNameType> dependentNameType;
11111112
AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasElementType,
11121113
AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType,
11131114
ComplexType));

clang/lib/ASTMatchers/Dynamic/Registry.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ RegistryMaps::RegistryMaps() {
222222
REGISTER_MATCHER(decompositionDecl);
223223
REGISTER_MATCHER(declCountIs);
224224
REGISTER_MATCHER(declRefExpr);
225+
REGISTER_MATCHER(dependentNameType);
225226
REGISTER_MATCHER(dependentScopeDeclRefExpr);
226227
REGISTER_MATCHER(declStmt);
227228
REGISTER_MATCHER(declaratorDecl);

clang/unittests/AST/ASTImporterTest.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -3196,9 +3196,6 @@ TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
31963196
has(callExpr(has(dependentScopeDeclRefExpr())))))))));
31973197
}
31983198

3199-
const internal::VariadicDynCastAllOfMatcher<Type, DependentNameType>
3200-
dependentNameType;
3201-
32023199
TEST_P(ImportExpr, DependentNameType) {
32033200
MatchVerifier<Decl> Verifier;
32043201
testImport("template <typename T> struct declToImport {"

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,20 @@ TEST_P(ASTMatchersTest, DeducedTemplateSpecializationType) {
19121912
deducedTemplateSpecializationType()));
19131913
}
19141914

1915+
TEST_P(ASTMatchersTest, DependentNameType) {
1916+
if (!GetParam().isCXX()) {
1917+
return;
1918+
}
1919+
1920+
EXPECT_TRUE(matches(
1921+
R"(
1922+
template <typename T> struct declToImport {
1923+
typedef typename T::type dependent_name;
1924+
};
1925+
)",
1926+
dependentNameType()));
1927+
}
1928+
19151929
TEST_P(ASTMatchersTest, RecordType) {
19161930
EXPECT_TRUE(matches("struct S {}; struct S s;",
19171931
recordType(hasDeclaration(recordDecl(hasName("S"))))));

0 commit comments

Comments
 (0)