From ea98fe0936a1b9046fcd47a6ad37a7e757acea0d Mon Sep 17 00:00:00 2001 From: Clement Courbet Date: Thu, 22 Nov 2018 14:00:56 +0000 Subject: [PATCH] [ASTMatchers] Add hasSideEffect() matcher. Summary: Exposes Expr::HasSideEffects. Reviewers: aaron.ballman Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D54830 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@347462 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibASTMatchersReference.html | 18 +++++++++++++++++ include/clang/ASTMatchers/ASTMatchers.h | 20 +++++++++++++++++++ lib/ASTMatchers/Dynamic/Registry.cpp | 1 + .../ASTMatchers/ASTMatchersNarrowingTest.cpp | 16 +++++++++++++++ 4 files changed, 55 insertions(+) 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 {}; +Matcher<Expr>hasSideEffects +
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'.
+
+ + Matcher<Expr>isInstantiationDependent
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
-- 
2.40.0