//
// 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
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.
/// @{
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) {
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);
}
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) {
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) {
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);
}
"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);
}