[ASTMatchers] New matcher hasReturnValue added

Summary: A checker (will be uploaded after this patch) needs to check implicit casts. Existing generic matcher "has" ignores implicit casts and parenthesized expressions and no specific matcher for matching return value expression preexisted. The patch adds such a matcher (hasReturnValue).

Reviewers: klimek, sbenza

Subscribers: xazax.hun, klimek, cfe-commits

Patch by Ádám Balogh!

Differential Revision: http://reviews.llvm.org/D17986

llvm-svn: 264037
This commit is contained in:
Alexander Kornienko 2016-03-22 11:03:03 +00:00
parent e5c095923d
commit 976921d4b4
4 changed files with 35 additions and 0 deletions

View File

@ -4854,6 +4854,18 @@ Usable as: Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Block
</pre></td></tr>
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1ReturnStmt.html">ReturnStmt</a>&gt;</td><td class="name" onclick="toggle('hasReturnValue0')"><a name="hasReturnValue0Anchor">hasReturnValue</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt; 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&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1StmtExpr.html">StmtExpr</a>&gt;</td><td class="name" onclick="toggle('hasAnySubstatement1')"><a name="hasAnySubstatement1Anchor">hasAnySubstatement</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>&gt; 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.

View File

@ -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,

View File

@ -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);

View File

@ -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