Skip to content

Commit a0b7150

Browse files
committed
[Clang][ASTMatcher] Add dependentScopeDeclRefExpr Matcher
1 parent 11676da commit a0b7150

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

clang/docs/LibASTMatchersReference.html

+6
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,12 @@ <h2 id="decl-matchers">Node Matchers</h2>
18421842
if (x) {}
18431843
</pre></td></tr>
18441844

1845+
<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>
1846+
<tr><td colspan="4" class="doc" id="dependentScopeDeclRefExpr0"><pre>Matches expressions that refer to dependent scope declarations.
1847+
1848+
Example matches T::v
1849+
template <class T> class X : T { void f() { T::v; } };
1850+
</pre></td></tr>
18451851

18461852
<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>
18471853
<tr><td colspan="4" class="doc" id="declStmt0"><pre>Matches declaration statements.

clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,8 @@ AST Matchers
11011101

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

1104+
- Add ``dependentScopeDeclRefExpr`` matcher to match expressions that refer to dependent scope declarations.
1105+
11041106
clang-format
11051107
------------
11061108

clang/include/clang/ASTMatchers/ASTMatchers.h

+10
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,16 @@ extern const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
21252125
extern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr>
21262126
declRefExpr;
21272127

2128+
/// Matches expressions that refer to dependent scope declarations.
2129+
///
2130+
/// example matches T::v;
2131+
/// \code
2132+
/// template <class T> class X : T { void f() { T::v; } };
2133+
/// \endcode
2134+
extern const internal::VariadicDynCastAllOfMatcher<Stmt,
2135+
DependentScopeDeclRefExpr>
2136+
dependentScopeDeclRefExpr;
2137+
21282138
/// Matches a reference to an ObjCIvar.
21292139
///
21302140
/// Example: matches "a" in "init" method:

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,8 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, CXXRewrittenBinaryOperator>
924924
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXFoldExpr> cxxFoldExpr;
925925
const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
926926
const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr;
927+
const internal::VariadicDynCastAllOfMatcher<Stmt, DependentScopeDeclRefExpr>
928+
dependentScopeDeclRefExpr;
927929
const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr> objcIvarRefExpr;
928930
const internal::VariadicDynCastAllOfMatcher<Stmt, BlockExpr> blockExpr;
929931
const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;

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(dependentScopeDeclRefExpr);
225226
REGISTER_MATCHER(declStmt);
226227
REGISTER_MATCHER(declaratorDecl);
227228
REGISTER_MATCHER(decltypeType);

clang/unittests/AST/ASTImporterTest.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -3172,9 +3172,6 @@ TEST_P(ImportDecl, ImportFieldOrder) {
31723172
recordDecl(hasFieldOrder({"b", "a"})));
31733173
}
31743174

3175-
const internal::VariadicDynCastAllOfMatcher<Expr, DependentScopeDeclRefExpr>
3176-
dependentScopeDeclRefExpr;
3177-
31783175
TEST_P(ImportExpr, DependentScopeDeclRefExpr) {
31793176
MatchVerifier<Decl> Verifier;
31803177
testImport("template <typename T> struct S { static T foo; };"

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,22 @@ TEST_P(ASTMatchersTest, DeclRefExpr) {
556556
Reference));
557557
}
558558

559+
TEST_P(ASTMatchersTest, DependentScopeDeclRefExpr) {
560+
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) {
561+
// FIXME: Add a test for `dependentScopeDeclRefExpr()` that does not depend
562+
// on C++.
563+
return;
564+
}
565+
566+
EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };",
567+
dependentScopeDeclRefExpr()));
568+
569+
EXPECT_TRUE(
570+
matches("template <typename T> struct S { static T Foo; };"
571+
"template <typename T> void declToImport() { (void)S<T>::Foo; }",
572+
dependentScopeDeclRefExpr()));
573+
}
574+
559575
TEST_P(ASTMatchersTest, CXXMemberCallExpr) {
560576
if (!GetParam().isCXX()) {
561577
return;
@@ -629,10 +645,8 @@ TEST_P(ASTMatchersTest, MemberExpr_MatchesVariable) {
629645
EXPECT_TRUE(matches("template <class T>"
630646
"class X : T { void f() { this->T::v; } };",
631647
cxxDependentScopeMemberExpr()));
632-
// FIXME: Add a matcher for DependentScopeDeclRefExpr.
633-
EXPECT_TRUE(
634-
notMatches("template <class T> class X : T { void f() { T::v; } };",
635-
cxxDependentScopeMemberExpr()));
648+
EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };",
649+
dependentScopeDeclRefExpr()));
636650
EXPECT_TRUE(matches("template <class T> void x() { T t; t.v; }",
637651
cxxDependentScopeMemberExpr()));
638652
}

0 commit comments

Comments
 (0)