From: Cong Liu Date: Fri, 24 Jun 2016 09:38:03 +0000 (+0000) Subject: IgnoringImplicit matcher. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=85b4750b8d7cfecda65218124138d8254ffa3c46;p=clang IgnoringImplicit matcher. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@273659 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 3606367d2f..30af7e326a 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2347,7 +2347,7 @@ Given private: int c; }; fieldDecl(isPrivate()) - matches 'int c;' + matches 'int c;' @@ -2361,7 +2361,7 @@ Given private: int c; }; fieldDecl(isProtected()) - matches 'int b;' + matches 'int b;' @@ -2375,7 +2375,7 @@ Given private: int c; }; fieldDecl(isPublic()) - matches 'int a;' + matches 'int a;' @@ -4455,6 +4455,25 @@ only match the declarations for b, c, and d. +Matcher<Expr>ignoringImplicitast_matchers::Matcher<Expr> InnerMatcher +
Matches expressions that match InnerMatcher after any implicit AST
+nodes are stripped off.
+
+Parentheses and explicit casts are not discarded.
+Given
+  class C {};
+  C a = C();
+  C b;
+  C c = b;
+The matchers
+   varDecl(hasInitializer(ignoringImplicit(cxxConstructExpr())))
+would match the declarations for a, b, and c.
+While
+   varDecl(hasInitializer(cxxConstructExpr()))
+only match the declarations for b and c.
+
+ + Matcher<Expr>ignoringParenCastsMatcher<Expr> InnerMatcher
Matches expressions that match InnerMatcher after parentheses and
 casts are stripped off.
diff --git a/docs/tools/dump_ast_matchers.py b/docs/tools/dump_ast_matchers.py
old mode 100644
new mode 100755
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 712eef0d3c..41080e98e8 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -548,6 +548,32 @@ AST_POLYMORPHIC_MATCHER_P(
                              Builder);
 }
 
+/// \brief Matches expressions that match InnerMatcher after any implicit AST
+/// nodes are stripped off.
+///
+/// Parentheses and explicit casts are not discarded.
+/// Given
+/// \code
+///   class C {};
+///   C a = C();
+///   C b;
+///   C c = b;
+/// \endcode
+/// The matchers
+/// \code
+///    varDecl(hasInitializer(ignoringImplicit(cxxConstructExpr())))
+/// \endcode
+/// would match the declarations for a, b, and c.
+/// While
+/// \code
+///    varDecl(hasInitializer(cxxConstructExpr()))
+/// \endcode
+/// only match the declarations for b and c.
+AST_MATCHER_P(Expr, ignoringImplicit, ast_matchers::internal::Matcher,
+              InnerMatcher) {
+  return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder);
+}
+
 /// \brief Matches expressions that match InnerMatcher after any implicit casts
 /// are stripped off.
 ///
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index 4961da8925..1fe08581db 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -265,6 +265,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(hasUnarySelector);
   REGISTER_MATCHER(hasValueType);
   REGISTER_MATCHER(ifStmt);
+  REGISTER_MATCHER(ignoringImplicit);
   REGISTER_MATCHER(ignoringImpCasts);
   REGISTER_MATCHER(ignoringParenCasts);
   REGISTER_MATCHER(ignoringParenImpCasts);
diff --git a/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index cc5cf715a7..a3f616967d 100644
--- a/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1088,6 +1088,16 @@ TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) {
                            unless(anything())))));
 }
 
+TEST(IgnoringImplicit, MatchesImplicit) {
+  EXPECT_TRUE(matches("class C {}; C a = C();",
+                      varDecl(has(ignoringImplicit(cxxConstructExpr())))));
+}
+
+TEST(IgnoringImplicit, DoesNotMatchIncorrectly) {
+  EXPECT_TRUE(
+      notMatches("class C {}; C a = C();", varDecl(has(cxxConstructExpr()))));
+}
+
 TEST(IgnoringImpCasts, MatchesImpCasts) {
   // This test checks that ignoringImpCasts matches when implicit casts are
   // present and its inner matcher alone does not match.