]> granicus.if.org Git - clang/commitdiff
[ASTMatchers] Add hasSideEffect() matcher.
authorClement Courbet <courbet@google.com>
Thu, 22 Nov 2018 14:00:56 +0000 (14:00 +0000)
committerClement Courbet <courbet@google.com>
Thu, 22 Nov 2018 14:00:56 +0000 (14:00 +0000)
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
include/clang/ASTMatchers/ASTMatchers.h
lib/ASTMatchers/Dynamic/Registry.cpp
unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

index b547f7ece62dc5356a0c23b208b95873c0d68962..ce651c282eb7e210a97be214086bd21cbb4fa0df 100644 (file)
@@ -2817,6 +2817,24 @@ enum class Y {};
 </pre></td></tr>
 
 
+<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt;</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&amp; 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&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1Expr.html">Expr</a>&gt;</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 f42ac5b701bacd55a6e5c6997f7abc2eea5d897c..73ae8fb244a69c6df33c4fa0c4287dbfbaf5e4d7 100644 (file)
@@ -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
index 3ed189a524cc798e92ac91c587db5a4bddf263bb..fb9e110d30a17d605449f12104c9701fd35f0dfa 100644 (file)
@@ -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);
index e37bcbeec1f3030e74c4c03642b8a2531f9af6e2..076d21a1f5ea04e69cf4e857cbef37571015f451 100644 (file)
@@ -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