[PATCH] D54830: [ASTMatchers] Add hasSideEffect() matcher.

Clement Courbet via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 22 02:41:57 PST 2018


courbet created this revision.
courbet added a reviewer: aaron.ballman.
Herald added a subscriber: cfe-commits.

Exposes Expr::HasSideEffects.


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: 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: 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: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -4118,6 +4118,24 @@
          InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
 }
 
+/// \brief Matches expressions with potential side effects.
+///
+/// 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: docs/LibASTMatchersReference.html
===================================================================
--- docs/LibASTMatchersReference.html
+++ docs/LibASTMatchersReference.html
@@ -2806,6 +2806,22 @@
 </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.
+
+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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54830.175029.patch
Type: text/x-patch
Size: 3488 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181122/d430a23e/attachment-0001.bin>


More information about the cfe-commits mailing list