]> granicus.if.org Git - clang/commitdiff
Removed the 'id' AST matcher, which is superseded by '.bind()'
authorDmitri Gribenko <gribozavr@gmail.com>
Tue, 20 Aug 2019 13:02:28 +0000 (13:02 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Tue, 20 Aug 2019 13:02:28 +0000 (13:02 +0000)
Summary:
The 'id' matcher is not even included in the AST Matchers Reference
document, so I don't expect there to be a significant number of users.

There's no reason to provide two ways to do the exact same thing that
only have a minor syntactic difference.

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D66462

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@369380 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/ASTMatchers/ASTMatchers.h
unittests/Tooling/RefactoringCallbacksTest.cpp

index e6d25069f1c7a8915dd2ec74dd5b265bfecd6bb7..909e4d29fb2495d1767c853558d9995ff3be34ec 100644 (file)
 //
 //  For more complicated match expressions we're often interested in accessing
 //  multiple parts of the matched AST nodes once a match is found. In that case,
-//  use the id(...) matcher around the match expressions that match the nodes
-//  you want to access.
+//  call `.bind("name")` on match expressions that match the nodes you want to
+//  access.
 //
 //  For example, when we're interested in child classes of a certain class, we
 //  would write:
-//    cxxRecordDecl(hasName("MyClass"), has(id("child", recordDecl())))
+//    cxxRecordDecl(hasName("MyClass"), has(recordDecl().bind("child")))
 //  When the match is found via the MatchFinder, a user provided callback will
 //  be called with a BoundNodes instance that contains a mapping from the
-//  strings that we provided for the id(...) calls to the nodes that were
+//  strings that we provided for the `.bind()` calls to the nodes that were
 //  matched.
 //  In the given example, each time our matcher finds a match we get a callback
 //  where "child" is bound to the RecordDecl node of the matching child
@@ -131,15 +131,6 @@ private:
   internal::BoundNodesMap MyBoundNodes;
 };
 
-/// If the provided matcher matches a node, binds the node to \c ID.
-///
-/// FIXME: Do we want to support this now that we have bind()?
-template <typename T>
-internal::Matcher<T> id(StringRef ID,
-                        const internal::BindableMatcher<T> &InnerMatcher) {
-  return InnerMatcher.bind(ID);
-}
-
 /// Types of matchers for the top-level classes in the AST class
 /// hierarchy.
 /// @{
index 1663581d65e557d568de99c55ae0b10d6cc3c1cc..4a883c38c858f590733a5b7cb2dc94e1b50b8515 100644 (file)
@@ -38,28 +38,28 @@ TEST(RefactoringCallbacksTest, ReplacesStmtsWithString) {
   std::string Code = "void f() { int i = 1; }";
   std::string Expected = "void f() { ; }";
   ReplaceStmtWithText Callback("id", ";");
-  expectRewritten(Code, Expected, id("id", declStmt()), Callback);
+  expectRewritten(Code, Expected, declStmt().bind("id"), Callback);
 }
 
 TEST(RefactoringCallbacksTest, ReplacesStmtsInCalledMacros) {
   std::string Code = "#define A void f() { int i = 1; }\nA";
   std::string Expected = "#define A void f() { ; }\nA";
   ReplaceStmtWithText Callback("id", ";");
-  expectRewritten(Code, Expected, id("id", declStmt()), Callback);
+  expectRewritten(Code, Expected, declStmt().bind("id"), Callback);
 }
 
 TEST(RefactoringCallbacksTest, IgnoresStmtsInUncalledMacros) {
   std::string Code = "#define A void f() { int i = 1; }";
   std::string Expected = "#define A void f() { int i = 1; }";
   ReplaceStmtWithText Callback("id", ";");
-  expectRewritten(Code, Expected, id("id", declStmt()), Callback);
+  expectRewritten(Code, Expected, declStmt().bind("id"), Callback);
 }
 
 TEST(RefactoringCallbacksTest, ReplacesInteger) {
   std::string Code = "void f() { int i = 1; }";
   std::string Expected = "void f() { int i = 2; }";
   ReplaceStmtWithText Callback("id", "2");
-  expectRewritten(Code, Expected, id("id", expr(integerLiteral())), Callback);
+  expectRewritten(Code, Expected, expr(integerLiteral()).bind("id"), Callback);
 }
 
 TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
@@ -68,9 +68,9 @@ TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
   ReplaceStmtWithStmt Callback("always-false", "should-be");
   expectRewritten(
       Code, Expected,
-      id("always-false",
-         conditionalOperator(hasCondition(cxxBoolLiteral(equals(false))),
-                             hasFalseExpression(id("should-be", expr())))),
+      conditionalOperator(hasCondition(cxxBoolLiteral(equals(false))),
+                          hasFalseExpression(expr().bind("should-be")))
+          .bind("always-false"),
       Callback);
 }
 
@@ -78,20 +78,20 @@ TEST(RefactoringCallbacksTest, ReplacesIfStmt) {
   std::string Code = "bool a; void f() { if (a) f(); else a = true; }";
   std::string Expected = "bool a; void f() { f(); }";
   ReplaceIfStmtWithItsBody Callback("id", true);
-  expectRewritten(
-      Code, Expected,
-      id("id", ifStmt(hasCondition(implicitCastExpr(hasSourceExpression(
-                   declRefExpr(to(varDecl(hasName("a"))))))))),
-      Callback);
+  expectRewritten(Code, Expected,
+                  ifStmt(hasCondition(implicitCastExpr(hasSourceExpression(
+                             declRefExpr(to(varDecl(hasName("a"))))))))
+                      .bind("id"),
+                  Callback);
 }
 
 TEST(RefactoringCallbacksTest, RemovesEntireIfOnEmptyElse) {
   std::string Code = "void f() { if (false) int i = 0; }";
   std::string Expected = "void f() {  }";
   ReplaceIfStmtWithItsBody Callback("id", false);
-  expectRewritten(Code, Expected,
-                  id("id", ifStmt(hasCondition(cxxBoolLiteral(equals(false))))),
-                  Callback);
+  expectRewritten(
+      Code, Expected,
+      ifStmt(hasCondition(cxxBoolLiteral(equals(false)))).bind("id"), Callback);
 }
 
 TEST(RefactoringCallbacksTest, TemplateJustText) {
@@ -99,7 +99,7 @@ TEST(RefactoringCallbacksTest, TemplateJustText) {
   std::string Expected = "void f() { FOO }";
   auto Callback = ReplaceNodeWithTemplate::create("id", "FOO");
   EXPECT_FALSE(Callback.takeError());
-  expectRewritten(Code, Expected, id("id", declStmt()), **Callback);
+  expectRewritten(Code, Expected, declStmt().bind("id"), **Callback);
 }
 
 TEST(RefactoringCallbacksTest, TemplateSimpleSubst) {
@@ -108,7 +108,7 @@ TEST(RefactoringCallbacksTest, TemplateSimpleSubst) {
   auto Callback = ReplaceNodeWithTemplate::create("decl", "long x = ${init}");
   EXPECT_FALSE(Callback.takeError());
   expectRewritten(Code, Expected,
-                  id("decl", varDecl(hasInitializer(id("init", expr())))),
+                  varDecl(hasInitializer(expr().bind("init"))).bind("decl"),
                   **Callback);
 }
 
@@ -119,7 +119,7 @@ TEST(RefactoringCallbacksTest, TemplateLiteral) {
                                                   "string x = \"$$-${init}\"");
   EXPECT_FALSE(Callback.takeError());
   expectRewritten(Code, Expected,
-                  id("decl", varDecl(hasInitializer(id("init", expr())))),
+                  varDecl(hasInitializer(expr().bind("init"))).bind("decl"),
                   **Callback);
 }