From: Alexander Kornienko Date: Tue, 22 Mar 2016 11:03:03 +0000 (+0000) Subject: [ASTMatchers] New matcher hasReturnValue added X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=00c662ce2bed0c884fc2960cf15e39d343806687;p=clang [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@264037 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index ba3fa51b87..58ab6d1285 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -4854,6 +4854,18 @@ Usable as: Matcher<ReturnStmt>hasReturnValueMatcher<Expr> InnerMatcher +
Matches the return value expression of a return statement
+
+Given
+  return a + b;
+hasReturnValue(binaryOperator())
+  matches 'return a + b'
+with binaryOperator()
+  matching 'a + b'
+
+ + Matcher<StmtExpr>hasAnySubstatementMatcher<Stmt> InnerMatcher
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 e428d8c395..6835ea0871 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, 
+              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 0a8e820d03..9bf57851ad 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 63dc1a8b64..713ef5a84f 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