From: Jonas Toth Date: Mon, 23 Jul 2018 15:59:27 +0000 (+0000) Subject: [ASTMatchers] add matcher for decltypeType and its underlyingType X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fbd6d5e462c9daeba44d2c0787265f47575ad5f3;p=clang [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@337703 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 5c8ec31066..850caf63c7 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -5111,6 +5111,18 @@ AST_TYPELOC_TRAVERSE_MATCHER_DECL(hasValueType, getValue, /// matches "auto n" and "auto i" extern const AstTypeMatcher autoType; +/// Matches types nodes representing C++11 decltype() types. +/// +/// Given: +/// \code +/// short i = 1; +/// int j = 42; +/// decltype(i + j) result = i + j; +/// \endcode +/// decltypeType() +/// matches "decltype(i + j)" +extern const AstTypeMatcher decltypeType; + /// Matches \c AutoType nodes where the deduced type is a specific type. /// /// Note: There is no \c TypeLoc for the deduced type and thus no @@ -5128,6 +5140,20 @@ extern const AstTypeMatcher autoType; AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType, AST_POLYMORPHIC_SUPPORTED_TYPES(AutoType)); +/// Matches \c DecltypeType nodes to find out the underlying type. +/// +/// Given +/// \code +/// decltype(1) a = 1; +/// decltype(2.0) b = 2.0; +/// \endcode +/// decltypeType(hasUnderlyingType(isInteger())) +/// matches "auto a" +/// +/// Usable as: Matcher +AST_TYPE_TRAVERSE_MATCHER(hasUnderlyingType, getUnderlyingType, + AST_POLYMORPHIC_SUPPORTED_TYPES(DecltypeType)); + /// Matches \c FunctionType nodes. /// /// Given diff --git a/lib/ASTMatchers/ASTMatchersInternal.cpp b/lib/ASTMatchers/ASTMatchersInternal.cpp index d8af47d750..0c5902d20c 100644 --- a/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -800,6 +800,7 @@ const AstTypeMatcher incompleteArrayType; const AstTypeMatcher variableArrayType; const AstTypeMatcher atomicType; const AstTypeMatcher autoType; +const AstTypeMatcher decltypeType; const AstTypeMatcher functionType; const AstTypeMatcher functionProtoType; const AstTypeMatcher parenType; diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp index 769d985c05..5a01c5d1f9 100644 --- a/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/lib/ASTMatchers/Dynamic/Registry.cpp @@ -188,6 +188,7 @@ RegistryMaps::RegistryMaps() { REGISTER_MATCHER(decayedType); REGISTER_MATCHER(decl); REGISTER_MATCHER(declaratorDecl); + REGISTER_MATCHER(decltypeType); REGISTER_MATCHER(declCountIs); REGISTER_MATCHER(declRefExpr); REGISTER_MATCHER(declStmt); diff --git a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp index fc1a57c488..e521940983 100644 --- a/unittests/ASTMatchers/ASTMatchersNodeTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersNodeTest.cpp @@ -1206,6 +1206,12 @@ TEST(TypeMatching, MatchesAutoTypes) { // autoType(hasDeducedType(isInteger())))); } +TEST(TypeMatching, MatchesDeclTypes) { + EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;", decltypeType())); + EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;", + decltypeType(hasUnderlyingType(isInteger())))); +} + TEST(TypeMatching, MatchesFunctionTypes) { EXPECT_TRUE(matches("int (*f)(int);", functionType())); EXPECT_TRUE(matches("void f(int i) {}", functionType()));