diff options
author | Clement Courbet <courbet@google.com> | 2018-11-22 14:00:56 +0000 |
---|---|---|
committer | Clement Courbet <courbet@google.com> | 2018-11-22 14:00:56 +0000 |
commit | c022c51f89cd6bc5df9d06ea3e4dacf225429347 (patch) | |
tree | 00fd7d7e25a71c3857b8fc9c8ffb5a5836102360 /clang | |
parent | 509f7d7c30ab76a046382cea731537ab061b889d (diff) |
[ASTMatchers] Add hasSideEffect() matcher.
Summary: Exposes Expr::HasSideEffects.
Reviewers: aaron.ballman
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D54830
llvm-svn: 347462
Diffstat (limited to 'clang')
-rw-r--r-- | clang/docs/LibASTMatchersReference.html | 18 | ||||
-rw-r--r-- | clang/include/clang/ASTMatchers/ASTMatchers.h | 20 | ||||
-rw-r--r-- | clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 | ||||
-rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 16 |
4 files changed, 55 insertions, 0 deletions
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index b547f7ece62d..ce651c282eb7 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -2817,6 +2817,24 @@ enum class Y {}; </pre></td></tr> +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>></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& 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<<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>></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/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index f42ac5b701ba..73ae8fb244a6 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/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/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp index 3ed189a524cc..fb9e110d30a1 100644 --- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/clang/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/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index e37bcbeec1f3..076d21a1f5ea 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/clang/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 |