[PATCH] D16526: Add hasRetValue narrowing matcher for returnStmt
Richard via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 24 13:25:32 PST 2016
LegalizeAdulthood created this revision.
LegalizeAdulthood added a reviewer: klimek.
LegalizeAdulthood added a subscriber: cfe-commits.
Herald added a subscriber: klimek.
Add `hasRetValue` narrowing matcher for `returnStmt` so that you can match on the expression returned by a return stmt (or lack thereof).
Currently you can only match the type of a return value on a `functionDecl` with `returns`.
An example:
clanger> cat /tmp/a.cpp
void g() {
return;
}
void f() {
return (void) g();
}
int h() {
return 1;
}
int j() {
return h();
}
bool k() {
return true;
}
~/dev/build
clanger> bin/clang-query /tmp/a.cpp -- -std=c++11
clang-query> match functionDecl(returns(voidType()))
Match #1:
/tmp/a.cpp:1:1: note: "root" binds here
void g() {
^~~~~~~~~~
Match #2:
/tmp/a.cpp:5:1: note: "root" binds here
void f() {
^~~~~~~~~~
2 matches.
clang-query> match returnStmt(unless(hasRetValue(expr())))
Match #1:
/tmp/a.cpp:2:3: note: "root" binds here
return;
^~~~~~
1 match.
clang-query> ^D
http://reviews.llvm.org/D16526
Files:
docs/LibASTMatchersReference.html
include/clang/ASTMatchers/ASTMatchers.h
lib/ASTMatchers/Dynamic/Registry.cpp
unittests/ASTMatchers/ASTMatchersTest.cpp
Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -1807,6 +1807,11 @@
recordDecl(hasName("Y")))))));
}
+TEST(HasRetValue, MatchesReturnExpressions) {
+
+ EXPECT_TRUE(matches("bool f() { return true; }", returnStmt(hasRetValue(expr()))));
+}
+
TEST(IsExternC, MatchesExternCFunctionDeclarations) {
EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC())));
EXPECT_TRUE(matches("extern \"C\" { void f() {} }",
Index: lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Registry.cpp
+++ lib/ASTMatchers/Dynamic/Registry.cpp
@@ -235,6 +235,7 @@
REGISTER_MATCHER(hasQualifier);
REGISTER_MATCHER(hasRangeInit);
REGISTER_MATCHER(hasReceiverType);
+ REGISTER_MATCHER(hasRetValue);
REGISTER_MATCHER(hasRHS);
REGISTER_MATCHER(hasSelector);
REGISTER_MATCHER(hasSingleDecl);
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -2977,6 +2977,14 @@
return InnerMatcher.matches(Node.getReturnType(), Finder, Builder);
}
+/// \brief Matches the return expression of a return statement.
+///
+AST_MATCHER_P(ReturnStmt, hasRetValue,
+ internal::Matcher<Expr>, InnerMatcher) {
+ const Expr *const RetValue = Node.getRetValue();
+ return RetValue != nullptr && InnerMatcher.matches(*RetValue, Finder, Builder);
+}
+
/// \brief Matches extern "C" function declarations.
///
/// Given:
Index: docs/LibASTMatchersReference.html
===================================================================
--- docs/LibASTMatchersReference.html
+++ docs/LibASTMatchersReference.html
@@ -4607,6 +4607,12 @@
</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('hasRetValue0')"><a name="hasRetValue0Anchor">hasRetValue</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="hasRetValue0"><pre>Matches the return expression of a return statement.
+
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('alignOfExpr0')"><a name="alignOfExpr0Anchor">alignOfExpr</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html">UnaryExprOrTypeTraitExpr</a>> InnerMatcher</td></tr>
<tr><td colspan="4" class="doc" id="alignOfExpr0"><pre>Same as unaryExprOrTypeTraitExpr, but only matching
alignof.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16526.45835.patch
Type: text/x-patch
Size: 2926 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160124/4ec435f0/attachment.bin>
More information about the cfe-commits
mailing list