Skip to content

Commit 9ba6e8d

Browse files
authored
[Clang][ASTMatcher] Extend hasDependentName to match DependentNameType name (#121975)
Extend `hasDependentName` to be a polymorphic matcher that matches the name of either `DependentNameType` or `DependentScopeDeclRefExpr`
1 parent 1a7e7ef commit 9ba6e8d

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

clang/docs/LibASTMatchersReference.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,6 +3462,21 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
34623462
</pre></td></tr>
34633463

34643464

3465+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentNameType.html">DependentNameType</a>&gt;</td><td class="name" onclick="toggle('hasDependentName1')"><a name="hasDependentName1Anchor">hasDependentName</a></td><td>std::string N</td></tr>
3466+
<tr><td colspan="4" class="doc" id="hasDependentName1"><pre>Matches the dependent name of a DependentNameType.
3467+
3468+
Matches the dependent name of a DependentNameType
3469+
3470+
Given:
3471+
3472+
template &lt;typename T&lt; struct declToImport {
3473+
typedef typename T::type dependent_name;
3474+
};
3475+
3476+
dependentNameType(hasDependentName("type")) matches `T::type`
3477+
</pre></td></tr>
3478+
3479+
34653480
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXDependentScopeMemberExpr.html">CXXDependentScopeMemberExpr</a>&gt;</td><td class="name" onclick="toggle('memberHasSameNameAsBoundNode0')"><a name="memberHasSameNameAsBoundNode0Anchor">memberHasSameNameAsBoundNode</a></td><td>std::string BindingID</td></tr>
34663481
<tr><td colspan="4" class="doc" id="memberHasSameNameAsBoundNode0"><pre>Matches template-dependent, but known, member names against an already-bound
34673482
node

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ AST Matchers
11301130

11311131
- Add ``dependentTemplateSpecializationType`` matcher to match a dependent template specialization type.
11321132

1133-
- Add ``hasDependentName`` matcher to match the dependent name of a DependentScopeDeclRefExpr.
1133+
- Add ``hasDependentName`` matcher to match the dependent name of a DependentScopeDeclRefExpr or DependentNameType.
11341134

11351135
clang-format
11361136
------------

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,15 +3257,27 @@ AST_MATCHER_P(CXXDependentScopeMemberExpr, memberHasSameNameAsBoundNode,
32573257
});
32583258
}
32593259

3260-
/// Matches the dependent name of a DependentScopeDeclRefExpr
3260+
/// Matches the dependent name of a DependentScopeDeclRefExpr or
3261+
/// DependentNameType
32613262
///
32623263
/// Given:
32633264
/// \code
32643265
/// template <class T> class X : T { void f() { T::v; } };
32653266
/// \endcode
32663267
/// \c dependentScopeDeclRefExpr(hasDependentName("v")) matches `T::v`
3267-
AST_MATCHER_P(DependentScopeDeclRefExpr, hasDependentName, std::string, N) {
3268-
return Node.getDeclName().getAsString() == N;
3268+
///
3269+
/// Given:
3270+
/// \code
3271+
/// template <typename T> struct declToImport {
3272+
/// typedef typename T::type dependent_name;
3273+
/// };
3274+
/// \endcode
3275+
/// \c dependentNameType(hasDependentName("type")) matches `T::type`
3276+
AST_POLYMORPHIC_MATCHER_P(hasDependentName,
3277+
AST_POLYMORPHIC_SUPPORTED_TYPES(
3278+
DependentScopeDeclRefExpr, DependentNameType),
3279+
std::string, N) {
3280+
return internal::getDependentName(Node) == N;
32693281
}
32703282

32713283
/// Matches C++ classes that are directly or indirectly derived from a class
@@ -7724,7 +7736,7 @@ AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
77247736

77257737
/// Matches a dependent name type
77267738
///
7727-
/// Example matches T::type
7739+
/// Example matches T::type
77287740
/// \code
77297741
/// template <typename T> struct declToImport {
77307742
/// typedef typename T::type dependent_name;

clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,14 @@ MatchTemplateArgLocAt(const TemplateSpecializationTypeLoc &Node,
23432343
InnerMatcher.matches(Node.getArgLoc(Index), Finder, Builder);
23442344
}
23452345

2346+
inline std::string getDependentName(const DependentScopeDeclRefExpr &node) {
2347+
return node.getDeclName().getAsString();
2348+
}
2349+
2350+
inline std::string getDependentName(const DependentNameType &node) {
2351+
return node.getIdentifier()->getName().str();
2352+
}
2353+
23462354
} // namespace internal
23472355

23482356
} // namespace ast_matchers

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,21 @@ TEST_P(ASTMatchersTest, HasDependentName_DependentScopeDeclRefExpr) {
22532253
dependentScopeDeclRefExpr(hasDependentName("foo"))));
22542254
}
22552255

2256+
TEST_P(ASTMatchersTest, HasDependentName_DependentNameType) {
2257+
if (!GetParam().isCXX()) {
2258+
// FIXME: Fix this test to work with delayed template parsing.
2259+
return;
2260+
}
2261+
2262+
EXPECT_TRUE(matches(
2263+
R"(
2264+
template <typename T> struct declToImport {
2265+
typedef typename T::type dependent_name;
2266+
};
2267+
)",
2268+
dependentNameType(hasDependentName("type"))));
2269+
}
2270+
22562271
TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {
22572272

22582273
// Member functions:

0 commit comments

Comments
 (0)