Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -4841,6 +4841,26 @@ return false; } +/// \brief Matches the return value expression of a return statement +/// +/// Given +/// \code +/// return a+b; +/// \endcode +/// hasReturnValue(binaryOperator()) +/// matches 'return a+b' +/// with binaryOperator() +/// matching 'a+b' +AST_MATCHER_P(ReturnStmt, hasReturnValue, internal::Matcher, InnerMatcher) { + BoundNodesTreeBuilder Result(*Builder); + if(InnerMatcher.matches(*Node.getRetValue(), Finder, &Result)) { + *Builder = std::move(Result); + return true; + } + return false; +} + + /// \brief Matches CUDA kernel call expression. /// /// Example matches, Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -238,6 +238,7 @@ REGISTER_MATCHER(hasQualifier); REGISTER_MATCHER(hasRangeInit); REGISTER_MATCHER(hasReceiverType); + REGISTER_MATCHER(hasReturnValue); REGISTER_MATCHER(hasRHS); REGISTER_MATCHER(hasSelector); REGISTER_MATCHER(hasSingleDecl); Index: unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -5385,5 +5385,11 @@ EXPECT_TRUE(notMatches("int i = 0;", expr(nullPointerConstant()))); } +TEST(StatementMatcher, HasReturnValue) { + StatementMatcher RetVal = returnStmt(hasReturnValue(binaryOperator())); + EXPECT_TRUE(matches("int F() { return a+b; }", RetVal)); + EXPECT_FALSE(matches("int F() { return a; }", RetVal)); +} + } // end namespace ast_matchers } // end namespace clang