Skip to content

Commit

Permalink
[Clang][ASTMatcher] Add a matcher for the name of a DependentScopeDec…
Browse files Browse the repository at this point in the history
…lRefExpr (#121656)

Add the `hasDependentName` matcher to match the name of
`DependentScopeDeclRefExpr`

Fixes #121610
  • Loading branch information
AmrDeveloper authored Jan 6, 2025
1 parent 510263a commit f3590c1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions clang/docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -3449,6 +3449,19 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentScopeDeclRefExpr.html">DependentScopeDeclRefExpr</a>&gt;</td><td class="name" onclick="toggle('hasDependentName0')"><a name="hasDependentName0Anchor">hasDependentName</a></td><td>std::string N</td></tr>
<tr><td colspan="4" class="doc" id="hasDependentName0"><pre>Matches the dependent name of a DependentScopeDeclRefExpr.

Matches the dependent name of a DependentScopeDeclRefExpr

Given:

template &lt;class T&lt; class X : T { void f() { T::v; } };

dependentScopeDeclRefExpr(hasDependentName("v")) matches `T::v`
</pre></td></tr>


<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>
<tr><td colspan="4" class="doc" id="memberHasSameNameAsBoundNode0"><pre>Matches template-dependent, but known, member names against an already-bound
node
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,8 @@ AST Matchers

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

- Add ``hasDependentName`` matcher to match the dependent name of a DependentScopeDeclRefExpr.

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

Expand Down
11 changes: 11 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3257,6 +3257,17 @@ AST_MATCHER_P(CXXDependentScopeMemberExpr, memberHasSameNameAsBoundNode,
});
}

/// Matches the dependent name of a DependentScopeDeclRefExpr
///
/// Given:
/// \code
/// template <class T> class X : T { void f() { T::v; } };
/// \endcode
/// \c dependentScopeDeclRefExpr(hasDependentName("v")) matches `T::v`
AST_MATCHER_P(DependentScopeDeclRefExpr, hasDependentName, std::string, N) {
return Node.getDeclName().getAsString() == N;
}

/// Matches C++ classes that are directly or indirectly derived from a class
/// matching \c Base, or Objective-C classes that directly or indirectly
/// subclass a class matching \c Base.
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 @@ -314,6 +314,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(hasDeducedType);
REGISTER_MATCHER(hasDefaultArgument);
REGISTER_MATCHER(hasDefinition);
REGISTER_MATCHER(hasDependentName);
REGISTER_MATCHER(hasDescendant);
REGISTER_MATCHER(hasDestinationType);
REGISTER_MATCHER(hasDirectBase);
Expand Down
18 changes: 18 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,24 @@ TEST_P(ASTMatchersTest, ArgumentCountIs_CXXConstructExpr) {
Constructor1Arg));
}

TEST_P(ASTMatchersTest, HasDependentName_DependentScopeDeclRefExpr) {
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) {
// FIXME: Fix this test to work with delayed template parsing.
return;
}

EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };",
dependentScopeDeclRefExpr(hasDependentName("v"))));

EXPECT_TRUE(matches("template <typename T> struct S { static T Foo; };"
"template <typename T> void x() { (void)S<T>::Foo; }",
dependentScopeDeclRefExpr(hasDependentName("Foo"))));

EXPECT_TRUE(matches("template <typename T> struct S { static T foo(); };"
"template <typename T> void x() { S<T>::foo(); }",
dependentScopeDeclRefExpr(hasDependentName("foo"))));
}

TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) {

// Member functions:
Expand Down

0 comments on commit f3590c1

Please sign in to comment.