This revision was automatically updated to reflect the committed changes. Closed by commit rC347462: [ASTMatchers] Add hasSideEffect() matcher. (authored by courbet, committed by ).
Changed prior to commit: https://reviews.llvm.org/D54830?vs=175044&id=175045#toc Repository: rC Clang https://reviews.llvm.org/D54830 Files: docs/LibASTMatchersReference.html include/clang/ASTMatchers/ASTMatchers.h lib/ASTMatchers/Dynamic/Registry.cpp unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -294,6 +294,7 @@ REGISTER_MATCHER(hasReturnValue); REGISTER_MATCHER(hasRHS); REGISTER_MATCHER(hasSelector); + REGISTER_MATCHER(hasSideEffects); REGISTER_MATCHER(hasSingleDecl); REGISTER_MATCHER(hasSize); REGISTER_MATCHER(hasSizeExpr); Index: unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2259,5 +2259,21 @@ 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 Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -2817,6 +2817,24 @@ </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. Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -4118,6 +4118,26 @@ 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
Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -294,6 +294,7 @@ REGISTER_MATCHER(hasReturnValue); REGISTER_MATCHER(hasRHS); REGISTER_MATCHER(hasSelector); + REGISTER_MATCHER(hasSideEffects); REGISTER_MATCHER(hasSingleDecl); REGISTER_MATCHER(hasSize); REGISTER_MATCHER(hasSizeExpr); Index: unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2259,5 +2259,21 @@ 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 Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -2817,6 +2817,24 @@ </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. Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -4118,6 +4118,26 @@ 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
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits