diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index ba3fa51b87b5ddffdbbd3d8abb450e53d9b3ea14..58ab6d12850fd70f2ddbf5ef7f2b5658a35049b8 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -4854,6 +4854,18 @@ Usable as: Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Block </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ReturnStmt.html">ReturnStmt</a>></td><td class="name" onclick="toggle('hasReturnValue0')"><a name="hasReturnValue0Anchor">hasReturnValue</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>> InnerMatcher</td></tr> +<tr><td colspan="4" class="doc" id="hasReturnValue0"><pre>Matches the return value expression of a return statement + +Given + return a + b; +hasReturnValue(binaryOperator()) + matches 'return a + b' +with binaryOperator() + matching 'a + b' +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1StmtExpr.html">StmtExpr</a>></td><td class="name" onclick="toggle('hasAnySubstatement1')"><a name="hasAnySubstatement1Anchor">hasAnySubstatement</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="hasAnySubstatement1"><pre>Matches compound statements where at least one substatement matches a given matcher. Also matches StmtExprs that have CompoundStmt as children. diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index e428d8c3957f3020fea8fcaad483992ef8ef5fa6..6835ea0871b58e9f44a8f4cda23fab252016ae34 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -5008,6 +5008,22 @@ AST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) { 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<Expr>, + InnerMatcher) { + return InnerMatcher.matches(*Node.getRetValue(), Finder, Builder); +} + + /// \brief Matches CUDA kernel call expression. /// /// Example matches, diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp index 0a8e820d03d92cfb4d327d2047ab1e367900a572..9bf57851adecd9e74caa7d28df8d642ca241770c 100644 --- a/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/lib/ASTMatchers/Dynamic/Registry.cpp @@ -243,6 +243,7 @@ RegistryMaps::RegistryMaps() { REGISTER_MATCHER(hasQualifier); REGISTER_MATCHER(hasRangeInit); REGISTER_MATCHER(hasReceiverType); + REGISTER_MATCHER(hasReturnValue); REGISTER_MATCHER(hasRHS); REGISTER_MATCHER(hasSelector); REGISTER_MATCHER(hasSingleDecl); diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 63dc1a8b6497b070016a007f5b7d9497814f52c7..713ef5a84feea3e6673b870a5513cdf39d34ad25 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -5488,5 +5488,11 @@ TEST(NullPointerConstants, Basic) { EXPECT_TRUE(notMatches("int i = 0;", expr(nullPointerConstant()))); } +TEST(StatementMatcher, HasReturnValue) { + StatementMatcher RetVal = returnStmt(hasReturnValue(binaryOperator())); + EXPECT_TRUE(matches("int F() { int a, b; return a + b; }", RetVal)); + EXPECT_FALSE(matches("int F() { int a; return a; }", RetVal)); +} + } // end namespace ast_matchers } // end namespace clang