summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/docs/LibASTMatchersReference.html27
-rw-r--r--clang/include/clang/ASTMatchers/ASTMatchers.h30
-rw-r--r--clang/include/clang/ASTMatchers/ASTMatchersInternal.h7
-rw-r--r--clang/lib/ASTMatchers/Dynamic/Registry.cpp2
-rw-r--r--clang/unittests/ASTMatchers/ASTMatchersTest.cpp28
5 files changed, 85 insertions, 9 deletions
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index f31d9490cc5..0f474136ee2 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -405,13 +405,36 @@ decl(hasDeclContext(translationUnitDecl()))
</pre></td></tr>
+<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>
+<tr><td colspan="4" class="doc" id="typeAliasDecl0"><pre>Matches type alias declarations.
+
+Given
+ typedef int X;
+ using Y = int;
+typeAliasDecl()
+ matches "using Y = int", but not "typedef int X"
+</pre></td></tr>
+
+
<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>
<tr><td colspan="4" class="doc" id="typedefDecl0"><pre>Matches typedef declarations.
Given
typedef int X;
+ using Y = int;
typedefDecl()
- matches "typedef int X"
+ matches "typedef int X", but not "using Y = int"
+</pre></td></tr>
+
+
+<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>
+<tr><td colspan="4" class="doc" id="typedefNameDecl0"><pre>Matches typedef name declarations.
+
+Given
+ typedef int X;
+ using Y = int;
+typedefNameDecl()
+ matches "typedef int X" and "using Y = int"
</pre></td></tr>
@@ -5083,7 +5106,7 @@ Usable as: Any Matcher
</pre></td></tr>
-<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>
+<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>
<tr><td colspan="4" class="doc" id="hasType1"><pre>Matches if the expression's or declaration's type matches a type
matcher.
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 2a734f14fb1..ee472fc734c 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -163,11 +163,35 @@ const internal::VariadicDynCastAllOfMatcher<Decl, TranslationUnitDecl>
/// Given
/// \code
/// typedef int X;
+// using Y = int;
/// \endcode
/// typedefDecl()
-/// matches "typedef int X"
+/// matches "typedef int X", but not "using Y = int"
const internal::VariadicDynCastAllOfMatcher<Decl, TypedefDecl> typedefDecl;
+/// \brief Matches typedef name declarations.
+///
+/// Given
+/// \code
+/// typedef int X;
+// using Y = int;
+/// \endcode
+/// typedefNameDecl()
+/// matches "typedef int X" and "using Y = int"
+const internal::VariadicDynCastAllOfMatcher<Decl, TypedefNameDecl>
+ typedefNameDecl;
+
+/// \brief Matches type alias declarations.
+///
+/// Given
+/// \code
+/// typedef int X;
+// using Y = int;
+/// \endcode
+/// typeAliasDecl()
+/// matches "using Y = int", but not "typedef int X"
+const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl> typeAliasDecl;
+
/// \brief Matches AST nodes that were expanded within the main-file.
///
/// Example matches X but not Y
@@ -2451,9 +2475,9 @@ AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher,
/// typedef int U;
/// \endcode
AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
- hasType, AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, TypedefDecl, ValueDecl),
+ hasType, AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, TypedefNameDecl, ValueDecl),
internal::Matcher<QualType>, InnerMatcher, 0) {
- return InnerMatcher.matches(internal::getUnderlyingType<NodeType>(Node),
+ return InnerMatcher.matches(internal::getUnderlyingType(Node),
Finder, Builder);
}
diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index efe15156d7e..4b986c53252 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -96,12 +96,13 @@ private:
/// \brief Unifies obtaining the underlying type of a regular node through
/// `getType` and a TypedefNameDecl node through `getUnderlyingType`.
-template <typename NodeType>
-inline QualType getUnderlyingType(const NodeType &Node) {
+inline QualType getUnderlyingType(const Expr &Node) { return Node.getType(); }
+
+inline QualType getUnderlyingType(const ValueDecl &Node) {
return Node.getType();
}
-template <> inline QualType getUnderlyingType(const TypedefDecl &Node) {
+inline QualType getUnderlyingType(const TypedefNameDecl &Node) {
return Node.getUnderlyingType();
}
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
index 9bf57851ade..9fd43332b7f 100644
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -388,7 +388,9 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(translationUnitDecl);
REGISTER_MATCHER(type);
REGISTER_MATCHER(typedefDecl);
+ REGISTER_MATCHER(typedefNameDecl);
REGISTER_MATCHER(typedefType);
+ REGISTER_MATCHER(typeAliasDecl);
REGISTER_MATCHER(typeLoc);
REGISTER_MATCHER(unaryExprOrTypeTraitExpr);
REGISTER_MATCHER(unaryOperator);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
index df818ba9422..7d5cba90862 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -1101,6 +1101,16 @@ TEST(HasType, MatchesTypedefDecl) {
typedefDecl(hasType(asString("foo")), hasName("bar"))));
}
+TEST(HasType, MatchesTypedefNameDecl) {
+ EXPECT_TRUE(matches("using X = int;", typedefNameDecl(hasType(asString("int")))));
+ EXPECT_TRUE(matches("using T = const int;",
+ typedefNameDecl(hasType(asString("const int")))));
+ EXPECT_TRUE(notMatches("using T = const int;",
+ typedefNameDecl(hasType(asString("int")))));
+ EXPECT_TRUE(matches("using foo = int; using bar = foo;",
+ typedefNameDecl(hasType(asString("foo")), hasName("bar"))));
+}
+
TEST(HasTypeLoc, MatchesDeclaratorDecls) {
EXPECT_TRUE(matches("int x;",
varDecl(hasName("x"), hasTypeLoc(loc(asString("int"))))));
@@ -5404,9 +5414,25 @@ TEST(EqualsBoundNodeMatcher, UnlessDescendantsOfAncestorsMatch) {
.bind("data")));
}
-TEST(TypeDefDeclMatcher, Match) {
+TEST(TypedefDeclMatcher, Match) {
EXPECT_TRUE(matches("typedef int typedefDeclTest;",
typedefDecl(hasName("typedefDeclTest"))));
+ EXPECT_TRUE(notMatches("using typedefDeclTest2 = int;",
+ typedefDecl(hasName("typedefDeclTest2"))));
+}
+
+TEST(TypeAliasDeclMatcher, Match) {
+ EXPECT_TRUE(matches("using typeAliasTest2 = int;",
+ typeAliasDecl(hasName("typeAliasTest2"))));
+ EXPECT_TRUE(notMatches("typedef int typeAliasTest;",
+ typeAliasDecl(hasName("typeAliasTest"))));
+}
+
+TEST(TypedefNameDeclMatcher, Match) {
+ EXPECT_TRUE(matches("typedef int typedefNameDeclTest1;",
+ typedefNameDecl(hasName("typedefNameDeclTest1"))));
+ EXPECT_TRUE(matches("using typedefNameDeclTest2 = int;",
+ typedefNameDecl(hasName("typedefNameDeclTest2"))));
}
TEST(IsInlineMatcher, IsInline) {
OpenPOWER on IntegriCloud