Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 7670c72

Browse files
committed
Add typedefNameDecl() and typeAliasDecl() to the AST matchers; improves hasType() to match on TypedefNameDecl nodes.
Patch by Clement Courbet. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@266331 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 423f337 commit 7670c72

File tree

5 files changed

+85
-9
lines changed

5 files changed

+85
-9
lines changed

docs/LibASTMatchersReference.html

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,36 @@ <h2 id="decl-matchers">Node Matchers</h2>
405405
</pre></td></tr>
406406

407407

408+
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('typeAliasDecl0')"><a name="typeAliasDecl0Anchor">typeAliasDecl</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1TypeAliasDecl.html">TypeAliasDecl</a>&gt;...</td></tr>
409+
<tr><td colspan="4" class="doc" id="typeAliasDecl0"><pre>Matches type alias declarations.
410+
411+
Given
412+
typedef int X;
413+
using Y = int;
414+
typeAliasDecl()
415+
matches "using Y = int", but not "typedef int X"
416+
</pre></td></tr>
417+
418+
408419
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('typedefDecl0')"><a name="typedefDecl0Anchor">typedefDecl</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1TypedefDecl.html">TypedefDecl</a>&gt;...</td></tr>
409420
<tr><td colspan="4" class="doc" id="typedefDecl0"><pre>Matches typedef declarations.
410421

411422
Given
412423
typedef int X;
424+
using Y = int;
413425
typedefDecl()
414-
matches "typedef int X"
426+
matches "typedef int X", but not "using Y = int"
427+
</pre></td></tr>
428+
429+
430+
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>&gt;</td><td class="name" onclick="toggle('typedefNameDecl0')"><a name="typedefNameDecl0Anchor">typedefNameDecl</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1TypedefNameDecl.html">TypedefNameDecl</a>&gt;...</td></tr>
431+
<tr><td colspan="4" class="doc" id="typedefNameDecl0"><pre>Matches typedef name declarations.
432+
433+
Given
434+
typedef int X;
435+
using Y = int;
436+
typedefNameDecl()
437+
matches "typedef int X" and "using Y = int"
415438
</pre></td></tr>
416439

417440

@@ -5083,7 +5106,7 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
50835106
</pre></td></tr>
50845107

50855108

5086-
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1TypedefDecl.html">TypedefDecl</a>&gt;</td><td class="name" onclick="toggle('hasType1')"><a name="hasType1Anchor">hasType</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>&gt; InnerMatcher</td></tr>
5109+
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1TypedefNameDecl.html">TypedefNameDecl</a>&gt;</td><td class="name" onclick="toggle('hasType1')"><a name="hasType1Anchor">hasType</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>&gt; InnerMatcher</td></tr>
50875110
<tr><td colspan="4" class="doc" id="hasType1"><pre>Matches if the expression's or declaration's type matches a type
50885111
matcher.
50895112

include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,35 @@ const internal::VariadicDynCastAllOfMatcher<Decl, TranslationUnitDecl>
163163
/// Given
164164
/// \code
165165
/// typedef int X;
166+
// using Y = int;
166167
/// \endcode
167168
/// typedefDecl()
168-
/// matches "typedef int X"
169+
/// matches "typedef int X", but not "using Y = int"
169170
const internal::VariadicDynCastAllOfMatcher<Decl, TypedefDecl> typedefDecl;
170171

172+
/// \brief Matches typedef name declarations.
173+
///
174+
/// Given
175+
/// \code
176+
/// typedef int X;
177+
// using Y = int;
178+
/// \endcode
179+
/// typedefNameDecl()
180+
/// matches "typedef int X" and "using Y = int"
181+
const internal::VariadicDynCastAllOfMatcher<Decl, TypedefNameDecl>
182+
typedefNameDecl;
183+
184+
/// \brief Matches type alias declarations.
185+
///
186+
/// Given
187+
/// \code
188+
/// typedef int X;
189+
// using Y = int;
190+
/// \endcode
191+
/// typeAliasDecl()
192+
/// matches "using Y = int", but not "typedef int X"
193+
const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl> typeAliasDecl;
194+
171195
/// \brief Matches AST nodes that were expanded within the main-file.
172196
///
173197
/// Example matches X but not Y
@@ -2451,9 +2475,9 @@ AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher,
24512475
/// typedef int U;
24522476
/// \endcode
24532477
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
2454-
hasType, AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, TypedefDecl, ValueDecl),
2478+
hasType, AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, TypedefNameDecl, ValueDecl),
24552479
internal::Matcher<QualType>, InnerMatcher, 0) {
2456-
return InnerMatcher.matches(internal::getUnderlyingType<NodeType>(Node),
2480+
return InnerMatcher.matches(internal::getUnderlyingType(Node),
24572481
Finder, Builder);
24582482
}
24592483

include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ struct VariadicFunction {
9696

9797
/// \brief Unifies obtaining the underlying type of a regular node through
9898
/// `getType` and a TypedefNameDecl node through `getUnderlyingType`.
99-
template <typename NodeType>
100-
inline QualType getUnderlyingType(const NodeType &Node) {
99+
inline QualType getUnderlyingType(const Expr &Node) { return Node.getType(); }
100+
101+
inline QualType getUnderlyingType(const ValueDecl &Node) {
101102
return Node.getType();
102103
}
103104

104-
template <> inline QualType getUnderlyingType(const TypedefDecl &Node) {
105+
inline QualType getUnderlyingType(const TypedefNameDecl &Node) {
105106
return Node.getUnderlyingType();
106107
}
107108

lib/ASTMatchers/Dynamic/Registry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@ RegistryMaps::RegistryMaps() {
388388
REGISTER_MATCHER(translationUnitDecl);
389389
REGISTER_MATCHER(type);
390390
REGISTER_MATCHER(typedefDecl);
391+
REGISTER_MATCHER(typedefNameDecl);
391392
REGISTER_MATCHER(typedefType);
393+
REGISTER_MATCHER(typeAliasDecl);
392394
REGISTER_MATCHER(typeLoc);
393395
REGISTER_MATCHER(unaryExprOrTypeTraitExpr);
394396
REGISTER_MATCHER(unaryOperator);

unittests/ASTMatchers/ASTMatchersTest.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,16 @@ TEST(HasType, MatchesTypedefDecl) {
11011101
typedefDecl(hasType(asString("foo")), hasName("bar"))));
11021102
}
11031103

1104+
TEST(HasType, MatchesTypedefNameDecl) {
1105+
EXPECT_TRUE(matches("using X = int;", typedefNameDecl(hasType(asString("int")))));
1106+
EXPECT_TRUE(matches("using T = const int;",
1107+
typedefNameDecl(hasType(asString("const int")))));
1108+
EXPECT_TRUE(notMatches("using T = const int;",
1109+
typedefNameDecl(hasType(asString("int")))));
1110+
EXPECT_TRUE(matches("using foo = int; using bar = foo;",
1111+
typedefNameDecl(hasType(asString("foo")), hasName("bar"))));
1112+
}
1113+
11041114
TEST(HasTypeLoc, MatchesDeclaratorDecls) {
11051115
EXPECT_TRUE(matches("int x;",
11061116
varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
@@ -5404,9 +5414,25 @@ TEST(EqualsBoundNodeMatcher, UnlessDescendantsOfAncestorsMatch) {
54045414
.bind("data")));
54055415
}
54065416

5407-
TEST(TypeDefDeclMatcher, Match) {
5417+
TEST(TypedefDeclMatcher, Match) {
54085418
EXPECT_TRUE(matches("typedef int typedefDeclTest;",
54095419
typedefDecl(hasName("typedefDeclTest"))));
5420+
EXPECT_TRUE(notMatches("using typedefDeclTest2 = int;",
5421+
typedefDecl(hasName("typedefDeclTest2"))));
5422+
}
5423+
5424+
TEST(TypeAliasDeclMatcher, Match) {
5425+
EXPECT_TRUE(matches("using typeAliasTest2 = int;",
5426+
typeAliasDecl(hasName("typeAliasTest2"))));
5427+
EXPECT_TRUE(notMatches("typedef int typeAliasTest;",
5428+
typeAliasDecl(hasName("typeAliasTest"))));
5429+
}
5430+
5431+
TEST(TypedefNameDeclMatcher, Match) {
5432+
EXPECT_TRUE(matches("typedef int typedefNameDeclTest1;",
5433+
typedefNameDecl(hasName("typedefNameDeclTest1"))));
5434+
EXPECT_TRUE(matches("using typedefNameDeclTest2 = int;",
5435+
typedefNameDecl(hasName("typedefNameDeclTest2"))));
54105436
}
54115437

54125438
TEST(IsInlineMatcher, IsInline) {

0 commit comments

Comments
 (0)