Skip to content

Commit fa1ea69

Browse files
committedJul 23, 2018
[ASTMatchers] add matcher for decltypeType and its underlyingType
Summary: This patch introduces a new matcher for `DecltypeType` and its underlying type in order to fix a bug in clang-tidy, see https://reviews.llvm.org/D48717 for more. Reviewers: aaron.ballman, alexfh, NoQ, dcoughlin Reviewed By: aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D48759 llvm-svn: 337703
1 parent 4f4dfba commit fa1ea69

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed
 

Diff for: ‎clang/include/clang/ASTMatchers/ASTMatchers.h

+26
Original file line numberDiff line numberDiff line change
@@ -5111,6 +5111,18 @@ AST_TYPELOC_TRAVERSE_MATCHER_DECL(hasValueType, getValue,
51115111
/// matches "auto n" and "auto i"
51125112
extern const AstTypeMatcher<AutoType> autoType;
51135113

5114+
/// Matches types nodes representing C++11 decltype(<expr>) types.
5115+
///
5116+
/// Given:
5117+
/// \code
5118+
/// short i = 1;
5119+
/// int j = 42;
5120+
/// decltype(i + j) result = i + j;
5121+
/// \endcode
5122+
/// decltypeType()
5123+
/// matches "decltype(i + j)"
5124+
extern const AstTypeMatcher<DecltypeType> decltypeType;
5125+
51145126
/// Matches \c AutoType nodes where the deduced type is a specific type.
51155127
///
51165128
/// Note: There is no \c TypeLoc for the deduced type and thus no
@@ -5128,6 +5140,20 @@ extern const AstTypeMatcher<AutoType> autoType;
51285140
AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType,
51295141
AST_POLYMORPHIC_SUPPORTED_TYPES(AutoType));
51305142

5143+
/// Matches \c DecltypeType nodes to find out the underlying type.
5144+
///
5145+
/// Given
5146+
/// \code
5147+
/// decltype(1) a = 1;
5148+
/// decltype(2.0) b = 2.0;
5149+
/// \endcode
5150+
/// decltypeType(hasUnderlyingType(isInteger()))
5151+
/// matches "auto a"
5152+
///
5153+
/// Usable as: Matcher<DecltypeType>
5154+
AST_TYPE_TRAVERSE_MATCHER(hasUnderlyingType, getUnderlyingType,
5155+
AST_POLYMORPHIC_SUPPORTED_TYPES(DecltypeType));
5156+
51315157
/// Matches \c FunctionType nodes.
51325158
///
51335159
/// Given

Diff for: ‎clang/lib/ASTMatchers/ASTMatchersInternal.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ const AstTypeMatcher<IncompleteArrayType> incompleteArrayType;
800800
const AstTypeMatcher<VariableArrayType> variableArrayType;
801801
const AstTypeMatcher<AtomicType> atomicType;
802802
const AstTypeMatcher<AutoType> autoType;
803+
const AstTypeMatcher<DecltypeType> decltypeType;
803804
const AstTypeMatcher<FunctionType> functionType;
804805
const AstTypeMatcher<FunctionProtoType> functionProtoType;
805806
const AstTypeMatcher<ParenType> parenType;

Diff for: ‎clang/lib/ASTMatchers/Dynamic/Registry.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ RegistryMaps::RegistryMaps() {
188188
REGISTER_MATCHER(decayedType);
189189
REGISTER_MATCHER(decl);
190190
REGISTER_MATCHER(declaratorDecl);
191+
REGISTER_MATCHER(decltypeType);
191192
REGISTER_MATCHER(declCountIs);
192193
REGISTER_MATCHER(declRefExpr);
193194
REGISTER_MATCHER(declStmt);

Diff for: ‎clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,12 @@ TEST(TypeMatching, MatchesAutoTypes) {
12061206
// autoType(hasDeducedType(isInteger()))));
12071207
}
12081208

1209+
TEST(TypeMatching, MatchesDeclTypes) {
1210+
EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;", decltypeType()));
1211+
EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;",
1212+
decltypeType(hasUnderlyingType(isInteger()))));
1213+
}
1214+
12091215
TEST(TypeMatching, MatchesFunctionTypes) {
12101216
EXPECT_TRUE(matches("int (*f)(int);", functionType()));
12111217
EXPECT_TRUE(matches("void f(int i) {}", functionType()));

0 commit comments

Comments
 (0)
Please sign in to comment.