Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -2817,6 +2817,24 @@ +
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'. +
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