Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -4781,6 +4781,16 @@ +
Matches casts whose subexpr matches a given matcher. + +Example matches the first cast + (matcher = implicitCastExpr(hasSubExpr(floatLiteral()))))) + int i = 0.0; + int j = '0'; +
Matches classTemplateSpecializations, templateSpecializationType and functionDecl that have at least one TemplateArgument matching the given Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -4109,6 +4109,21 @@ return InnerMatcher.matches(Node.getType(), Finder, Builder); } +/// \brief Matches casts whose subexpr matches a given matcher. +/// +/// Example matches the first cast +/// (matcher = implicitCastExpr(hasSubExpr(floatLiteral()))))) +/// int i = 0.0; +/// int j = '0'; +AST_POLYMORPHIC_MATCHER_P( + hasSubExpr, + AST_POLYMORPHIC_SUPPORTED_TYPES(CastExpr), + internal::Matcher, InnerMatcher) { + const Expr *const SubExpr = Node.getSubExpr(); + return (SubExpr != nullptr && + InnerMatcher.matches(*SubExpr, Finder, Builder)); +} + /// \brief Matches RecordDecl object that are spelled with "struct." /// /// Example matches S, but not C or U. Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -1232,6 +1232,13 @@ cxxMethodDecl(ofClass(hasName("X"))), true, "-std=gnu++98")); } +TEST(HasSubExpr, MatchesSimpleCase) { + EXPECT_TRUE(matches("int i = 0.0;", + implicitCastExpr(hasSubExpr(floatLiteral())))); + EXPECT_TRUE(notMatches("int i = '0';", + implicitCastExpr(hasSubExpr(floatLiteral())))); +} + TEST(HasDestinationType, MatchesSimpleCase) { EXPECT_TRUE(matches("char* p = static_cast (0);", cxxStaticCastExpr(hasDestinationType(