aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClement Courbet <courbet@google.com>2018-11-22 06:12:48 -0800
committerandroid-build-merger <android-build-merger@google.com>2018-11-22 06:12:48 -0800
commita917e6bd4b995777b4f424c6e51d16d0fc42bd37 (patch)
tree4d8d83146f01c7df289038cbe250c77faea872ab
parent8f1ccf84731335f2c8154c2df10478d249039e89 (diff)
parentea98fe0936a1b9046fcd47a6ad37a7e757acea0d (diff)
downloadclang-a917e6bd4b995777b4f424c6e51d16d0fc42bd37.tar.gz
[ASTMatchers] Add hasSideEffect() matcher.
am: ea98fe0936 Change-Id: Ie36e83f31d76dc53c39b1ead144cb0f213ff1545
-rw-r--r--docs/LibASTMatchersReference.html18
-rw-r--r--include/clang/ASTMatchers/ASTMatchers.h20
-rw-r--r--lib/ASTMatchers/Dynamic/Registry.cpp1
-rw-r--r--unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp16
4 files changed, 55 insertions, 0 deletions
diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html
index b547f7ece6..ce651c282e 100644
--- a/docs/LibASTMatchersReference.html
+++ b/docs/LibASTMatchersReference.html
@@ -2817,6 +2817,24 @@ enum class Y {};
</pre></td></tr>
+<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt;</td><td class="name" onclick="toggle('hasSideEffects0')"><a name="hasSideEffects0Anchor">hasSideEffects</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="hasSideEffects0"><pre>Matches expressions with potential side effects other than producing
+a value, such as a calling a function, throwing an exception, or reading a
+volatile variable.
+
+Given
+ void f(int&amp; a, int b, volatile int c) {
+ call();
+ a = 0;
+ a;
+ b;
+ c;
+ }
+expr(hasSideEffects())
+ matches 'call()', 'a = 0', 'c', but not '0', 'a', 'b'.
+</pre></td></tr>
+
+
<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt;</td><td class="name" onclick="toggle('isInstantiationDependent0')"><a name="isInstantiationDependent0Anchor">isInstantiationDependent</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isInstantiationDependent0"><pre>Matches expressions that are instantiation-dependent even if it is
neither type- nor value-dependent.
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index f42ac5b701..73ae8fb244 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -4118,6 +4118,26 @@ AST_MATCHER_P(IfStmt, hasConditionVariableStatement,
InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
}
+/// \brief Matches expressions with potential side effects other than producing
+/// a value, such as a calling a function, throwing an exception, or reading a
+/// volatile variable.
+///
+/// Given
+/// \code
+/// void f(int& a, int b, volatile int c) {
+/// call();
+/// a = 0;
+/// a;
+/// b;
+/// c;
+/// }
+/// \endcode
+/// expr(hasSideEffects())
+/// matches 'call()', 'a = 0', 'c', but not '0', 'a', 'b'.
+AST_MATCHER(Expr, hasSideEffects) {
+ return Node.HasSideEffects(Finder->getASTContext());
+}
+
/// Matches the index expression of an array subscript expression.
///
/// Given
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index 3ed189a524..fb9e110d30 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -294,6 +294,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(hasReturnValue);
REGISTER_MATCHER(hasRHS);
REGISTER_MATCHER(hasSelector);
+ REGISTER_MATCHER(hasSideEffects);
REGISTER_MATCHER(hasSingleDecl);
REGISTER_MATCHER(hasSize);
REGISTER_MATCHER(hasSizeExpr);
diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index e37bcbeec1..076d21a1f5 100644
--- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2259,5 +2259,21 @@ TEST(Matcher, isMain) {
notMatches("int main2() {}", functionDecl(isMain())));
}
+TEST(Matcher, hasSideEffects) {
+ EXPECT_TRUE(matches("void call();"
+ "void f() { call(); }",
+ expr(hasSideEffects())));
+ EXPECT_TRUE(matches("void f(int& a) { a = 0; }", expr(hasSideEffects())));
+ EXPECT_TRUE(
+ matches("void f(volatile int a) { (void)a; }", expr(hasSideEffects())));
+
+ EXPECT_TRUE(notMatches("void call();"
+ "void f() { }",
+ expr(hasSideEffects())));
+ EXPECT_TRUE(
+ notMatches("void f(int& a) { (void)a; }", expr(hasSideEffects())));
+ EXPECT_TRUE(notMatches("void f(int a) { (void)a; }", expr(hasSideEffects())));
+}
+
} // namespace ast_matchers
} // namespace clang