timshen updated this revision to Diff 60704.
timshen added a comment.

Done.


http://reviews.llvm.org/D21241

Files:
  docs/LibASTMatchersReference.html
  include/clang/ASTMatchers/ASTMatchers.h
  lib/ASTMatchers/Dynamic/Registry.cpp
  unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1997,5 +1997,34 @@
   EXPECT_TRUE(notMatches(CppString2, returnStmt(forFunction(hasName("F")))));
 }
 
+TEST(IgnoringExprWithCleanups, MatchesExprWithCleanups) {
+  EXPECT_TRUE(
+      matches("struct A { ~A(); }; A Foo(); A x = Foo();",
+              varDecl(hasInitializer(ignoringExprWithCleanups(
+                  cxxConstructExpr(has(materializeTemporaryExpr())))))));
+}
+
+TEST(IgnoringExprWithCleanups, MatchesWithoutExprWithCleanups) {
+  EXPECT_TRUE(matches(
+      "int x = 0; const int y = x;",
+      varDecl(hasInitializer(ignoringExprWithCleanups(
+          ignoringParenImpCasts(declRefExpr(to(varDecl(hasName("x"))))))))));
+}
+
+TEST(IgnoringExprWithCleanups, DoesNotMatchIncorrectly) {
+  EXPECT_TRUE(notMatches("char c = ((3));",
+                         varDecl(hasInitializer(ignoringExprWithCleanups(
+                             ignoringParenImpCasts(unless(anything())))))));
+  EXPECT_TRUE(notMatches("float y = (float(0));",
+                         varDecl(hasInitializer(ignoringExprWithCleanups(
+                             ignoringParenImpCasts(integerLiteral()))))));
+  EXPECT_TRUE(notMatches("float y = (float)0;",
+                         varDecl(hasInitializer(ignoringExprWithCleanups(
+                             ignoringParenImpCasts(integerLiteral()))))));
+  EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);",
+                         varDecl(hasInitializer(ignoringExprWithCleanups(
+                             ignoringParenImpCasts(integerLiteral()))))));
+}
+
 } // namespace ast_matchers
 } // namespace clang
Index: lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Registry.cpp
+++ lib/ASTMatchers/Dynamic/Registry.cpp
@@ -265,6 +265,7 @@
   REGISTER_MATCHER(hasUnarySelector);
   REGISTER_MATCHER(hasValueType);
   REGISTER_MATCHER(ifStmt);
+  REGISTER_MATCHER(ignoringExprWithCleanups);
   REGISTER_MATCHER(ignoringImpCasts);
   REGISTER_MATCHER(ignoringParenCasts);
   REGISTER_MATCHER(ignoringParenImpCasts);
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -625,6 +625,16 @@
   return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder);
 }
 
+/// \brief Matches expressions that match InnerMatcher after ExprWithCleanups
+/// are stripped off.
+AST_MATCHER_P(Expr, ignoringExprWithCleanups, internal::Matcher<Expr>,
+              InnerMatcher) {
+  auto E = &Node;
+  if (auto Cleanups = dyn_cast<ExprWithCleanups>(E))
+    E = Cleanups->getSubExpr();
+  return InnerMatcher.matches(*E, Finder, Builder);
+}
+
 /// \brief Matches types that match InnerMatcher after any parens are stripped.
 ///
 /// Given
Index: docs/LibASTMatchersReference.html
===================================================================
--- docs/LibASTMatchersReference.html
+++ docs/LibASTMatchersReference.html
@@ -2347,7 +2347,7 @@
   private:   int c;
   };
 fieldDecl(isPrivate())
-  matches 'int c;' 
+  matches 'int c;'
 </pre></td></tr>
 
 
@@ -2361,7 +2361,7 @@
   private:   int c;
   };
 fieldDecl(isProtected())
-  matches 'int b;' 
+  matches 'int b;'
 </pre></td></tr>
 
 
@@ -2375,7 +2375,7 @@
   private:   int c;
   };
 fieldDecl(isPublic())
-  matches 'int a;' 
+  matches 'int a;'
 </pre></td></tr>
 
 
@@ -4432,6 +4432,12 @@
 </pre></td></tr>
 
 
+<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html";>Expr</a>&gt;</td><td class="name" onclick="toggle('ignoringExprWithCleanups0')"><a name="ignoringExprWithCleanups0Anchor">ignoringExprWithCleanups</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html";>Expr</a>&gt; InnerMatcher</td></tr>
+<tr><td colspan="4" class="doc" id="ignoringExprWithCleanups0"><pre>Matches expressions that match InnerMatcher after ExprWithCleanups
+are stripped off.
+</pre></td></tr>
+
+
 <tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html";>Expr</a>&gt;</td><td class="name" onclick="toggle('ignoringImpCasts0')"><a name="ignoringImpCasts0Anchor">ignoringImpCasts</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html";>Expr</a>&gt; InnerMatcher</td></tr>
 <tr><td colspan="4" class="doc" id="ignoringImpCasts0"><pre>Matches expressions that match InnerMatcher after any implicit casts
 are stripped off.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to