Skip to content

Commit dee011b

Browse files
committedAug 20, 2019
Removed the 'id' AST matcher, which is superseded by '.bind()'
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 llvm-svn: 369380
1 parent 5877fb7 commit dee011b

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed
 

‎clang/include/clang/ASTMatchers/ASTMatchers.h

+4-13
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
//
2020
// For more complicated match expressions we're often interested in accessing
2121
// multiple parts of the matched AST nodes once a match is found. In that case,
22-
// use the id(...) matcher around the match expressions that match the nodes
23-
// you want to access.
22+
// call `.bind("name")` on match expressions that match the nodes you want to
23+
// access.
2424
//
2525
// For example, when we're interested in child classes of a certain class, we
2626
// would write:
27-
// cxxRecordDecl(hasName("MyClass"), has(id("child", recordDecl())))
27+
// cxxRecordDecl(hasName("MyClass"), has(recordDecl().bind("child")))
2828
// When the match is found via the MatchFinder, a user provided callback will
2929
// be called with a BoundNodes instance that contains a mapping from the
30-
// strings that we provided for the id(...) calls to the nodes that were
30+
// strings that we provided for the `.bind()` calls to the nodes that were
3131
// matched.
3232
// In the given example, each time our matcher finds a match we get a callback
3333
// where "child" is bound to the RecordDecl node of the matching child
@@ -131,15 +131,6 @@ class BoundNodes {
131131
internal::BoundNodesMap MyBoundNodes;
132132
};
133133

134-
/// If the provided matcher matches a node, binds the node to \c ID.
135-
///
136-
/// FIXME: Do we want to support this now that we have bind()?
137-
template <typename T>
138-
internal::Matcher<T> id(StringRef ID,
139-
const internal::BindableMatcher<T> &InnerMatcher) {
140-
return InnerMatcher.bind(ID);
141-
}
142-
143134
/// Types of matchers for the top-level classes in the AST class
144135
/// hierarchy.
145136
/// @{

‎clang/unittests/Tooling/RefactoringCallbacksTest.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@ TEST(RefactoringCallbacksTest, ReplacesStmtsWithString) {
3838
std::string Code = "void f() { int i = 1; }";
3939
std::string Expected = "void f() { ; }";
4040
ReplaceStmtWithText Callback("id", ";");
41-
expectRewritten(Code, Expected, id("id", declStmt()), Callback);
41+
expectRewritten(Code, Expected, declStmt().bind("id"), Callback);
4242
}
4343

4444
TEST(RefactoringCallbacksTest, ReplacesStmtsInCalledMacros) {
4545
std::string Code = "#define A void f() { int i = 1; }\nA";
4646
std::string Expected = "#define A void f() { ; }\nA";
4747
ReplaceStmtWithText Callback("id", ";");
48-
expectRewritten(Code, Expected, id("id", declStmt()), Callback);
48+
expectRewritten(Code, Expected, declStmt().bind("id"), Callback);
4949
}
5050

5151
TEST(RefactoringCallbacksTest, IgnoresStmtsInUncalledMacros) {
5252
std::string Code = "#define A void f() { int i = 1; }";
5353
std::string Expected = "#define A void f() { int i = 1; }";
5454
ReplaceStmtWithText Callback("id", ";");
55-
expectRewritten(Code, Expected, id("id", declStmt()), Callback);
55+
expectRewritten(Code, Expected, declStmt().bind("id"), Callback);
5656
}
5757

5858
TEST(RefactoringCallbacksTest, ReplacesInteger) {
5959
std::string Code = "void f() { int i = 1; }";
6060
std::string Expected = "void f() { int i = 2; }";
6161
ReplaceStmtWithText Callback("id", "2");
62-
expectRewritten(Code, Expected, id("id", expr(integerLiteral())), Callback);
62+
expectRewritten(Code, Expected, expr(integerLiteral()).bind("id"), Callback);
6363
}
6464

6565
TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
@@ -68,38 +68,38 @@ TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
6868
ReplaceStmtWithStmt Callback("always-false", "should-be");
6969
expectRewritten(
7070
Code, Expected,
71-
id("always-false",
72-
conditionalOperator(hasCondition(cxxBoolLiteral(equals(false))),
73-
hasFalseExpression(id("should-be", expr())))),
71+
conditionalOperator(hasCondition(cxxBoolLiteral(equals(false))),
72+
hasFalseExpression(expr().bind("should-be")))
73+
.bind("always-false"),
7474
Callback);
7575
}
7676

7777
TEST(RefactoringCallbacksTest, ReplacesIfStmt) {
7878
std::string Code = "bool a; void f() { if (a) f(); else a = true; }";
7979
std::string Expected = "bool a; void f() { f(); }";
8080
ReplaceIfStmtWithItsBody Callback("id", true);
81-
expectRewritten(
82-
Code, Expected,
83-
id("id", ifStmt(hasCondition(implicitCastExpr(hasSourceExpression(
84-
declRefExpr(to(varDecl(hasName("a"))))))))),
85-
Callback);
81+
expectRewritten(Code, Expected,
82+
ifStmt(hasCondition(implicitCastExpr(hasSourceExpression(
83+
declRefExpr(to(varDecl(hasName("a"))))))))
84+
.bind("id"),
85+
Callback);
8686
}
8787

8888
TEST(RefactoringCallbacksTest, RemovesEntireIfOnEmptyElse) {
8989
std::string Code = "void f() { if (false) int i = 0; }";
9090
std::string Expected = "void f() { }";
9191
ReplaceIfStmtWithItsBody Callback("id", false);
92-
expectRewritten(Code, Expected,
93-
id("id", ifStmt(hasCondition(cxxBoolLiteral(equals(false))))),
94-
Callback);
92+
expectRewritten(
93+
Code, Expected,
94+
ifStmt(hasCondition(cxxBoolLiteral(equals(false)))).bind("id"), Callback);
9595
}
9696

9797
TEST(RefactoringCallbacksTest, TemplateJustText) {
9898
std::string Code = "void f() { int i = 1; }";
9999
std::string Expected = "void f() { FOO }";
100100
auto Callback = ReplaceNodeWithTemplate::create("id", "FOO");
101101
EXPECT_FALSE(Callback.takeError());
102-
expectRewritten(Code, Expected, id("id", declStmt()), **Callback);
102+
expectRewritten(Code, Expected, declStmt().bind("id"), **Callback);
103103
}
104104

105105
TEST(RefactoringCallbacksTest, TemplateSimpleSubst) {
@@ -108,7 +108,7 @@ TEST(RefactoringCallbacksTest, TemplateSimpleSubst) {
108108
auto Callback = ReplaceNodeWithTemplate::create("decl", "long x = ${init}");
109109
EXPECT_FALSE(Callback.takeError());
110110
expectRewritten(Code, Expected,
111-
id("decl", varDecl(hasInitializer(id("init", expr())))),
111+
varDecl(hasInitializer(expr().bind("init"))).bind("decl"),
112112
**Callback);
113113
}
114114

@@ -119,7 +119,7 @@ TEST(RefactoringCallbacksTest, TemplateLiteral) {
119119
"string x = \"$$-${init}\"");
120120
EXPECT_FALSE(Callback.takeError());
121121
expectRewritten(Code, Expected,
122-
id("decl", varDecl(hasInitializer(id("init", expr())))),
122+
varDecl(hasInitializer(expr().bind("init"))).bind("decl"),
123123
**Callback);
124124
}
125125

0 commit comments

Comments
 (0)
Please sign in to comment.