Skip to content

Commit

Permalink
[Clang][ASTMatcher] Add dependentScopeDeclRefExpr matcher (#120996)
Browse files Browse the repository at this point in the history
Fixes #120937
  • Loading branch information
AmrDeveloper authored Dec 28, 2024
1 parent b2fd0a7 commit 48bf0a9
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions clang/docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,12 @@ <h2 id="decl-matchers">Node Matchers</h2>
if (x) {}
</pre></td></tr>

<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>&gt;</td><td class="name" onclick="toggle('dependentScopeDeclRefExpr0')"><a name="dependentScopeDeclRefExpr0Anchor">dependentScopeDeclRefExpr</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DependentScopeDeclRefExpr.html">DependentScopeDeclRefExpr</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="dependentScopeDeclRefExpr0"><pre>Matches expressions that refer to dependent scope declarations.

Example matches T::v
template <class T> class X : T { void f() { T::v; } };
</pre></td></tr>

<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>&gt;</td><td class="name" onclick="toggle('declStmt0')"><a name="declStmt0Anchor">declStmt</a></td><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1DeclStmt.html">DeclStmt</a>&gt;...</td></tr>
<tr><td colspan="4" class="doc" id="declStmt0"><pre>Matches declaration statements.
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,8 @@ AST Matchers

- Ensure ``pointee`` matches Objective-C pointer types.

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

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 @@ -2125,6 +2125,16 @@ extern const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr>
declRefExpr;

/// Matches expressions that refer to dependent scope declarations.
///
/// example matches T::v;
/// \code
/// template <class T> class X : T { void f() { T::v; } };
/// \endcode
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
DependentScopeDeclRefExpr>
dependentScopeDeclRefExpr;

/// Matches a reference to an ObjCIvar.
///
/// Example: matches "a" in "init" method:
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,8 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, CXXRewrittenBinaryOperator>
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXFoldExpr> cxxFoldExpr;
const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr;
const internal::VariadicDynCastAllOfMatcher<Stmt, DependentScopeDeclRefExpr>
dependentScopeDeclRefExpr;
const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr> objcIvarRefExpr;
const internal::VariadicDynCastAllOfMatcher<Stmt, BlockExpr> blockExpr;
const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
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(dependentScopeDeclRefExpr);
REGISTER_MATCHER(declStmt);
REGISTER_MATCHER(declaratorDecl);
REGISTER_MATCHER(decltypeType);
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 @@ -3172,9 +3172,6 @@ TEST_P(ImportDecl, ImportFieldOrder) {
recordDecl(hasFieldOrder({"b", "a"})));
}

const internal::VariadicDynCastAllOfMatcher<Expr, DependentScopeDeclRefExpr>
dependentScopeDeclRefExpr;

TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
MatchVerifier<Decl> Verifier;
testImport("template <typename T> struct S { static T foo; };"
Expand Down
21 changes: 17 additions & 4 deletions clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,21 @@ TEST_P(ASTMatchersTest, DeclRefExpr) {
Reference));
}

TEST_P(ASTMatchersTest, 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()));

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

TEST_P(ASTMatchersTest, CXXMemberCallExpr) {
if (!GetParam().isCXX()) {
return;
Expand Down Expand Up @@ -629,10 +644,8 @@ TEST_P(ASTMatchersTest, MemberExpr_MatchesVariable) {
EXPECT_TRUE(matches("template <class T>"
"class X : T { void f() { this->T::v; } };",
cxxDependentScopeMemberExpr()));
// FIXME: Add a matcher for DependentScopeDeclRefExpr.
EXPECT_TRUE(
notMatches("template <class T> class X : T { void f() { T::v; } };",
cxxDependentScopeMemberExpr()));
EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };",
dependentScopeDeclRefExpr()));
EXPECT_TRUE(matches("template <class T> void x() { T t; t.v; }",
cxxDependentScopeMemberExpr()));
}
Expand Down

0 comments on commit 48bf0a9

Please sign in to comment.