Add the hasRangeInit() matcher for range-based for loop.

llvm-svn: 209533
This commit is contained in:
Manuel Klimek 2014-05-23 17:49:03 +00:00
parent 4625f85bb7
commit 8651081bbe
2 changed files with 17 additions and 0 deletions

View File

@ -937,6 +937,20 @@ AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>,
return (Var != nullptr && InnerMatcher.matches(*Var, Finder, Builder));
}
/// \brief Matches the range initialization statement of a for loop.
///
/// Example:
/// forStmt(hasRangeInit(anything()))
/// matches 'a' in
/// \code
/// for (int x : a) { }
/// \endcode
AST_MATCHER_P(CXXForRangeStmt, hasRangeInit, internal::Matcher<Expr>,
InnerMatcher) {
const Expr *const Init = Node.getRangeInit();
return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
}
/// \brief Matches while statements.
///
/// Given

View File

@ -2391,6 +2391,9 @@ TEST(For, ForLoopInternals) {
TEST(For, ForRangeLoopInternals) {
EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
forRangeStmt(hasLoopVariable(anything()))));
EXPECT_TRUE(matches(
"void f(){ int a[] {1, 2}; for (int i : a); }",
forRangeStmt(hasRangeInit(declRefExpr(to(varDecl(hasName("a"))))))));
}
TEST(For, NegativeForLoopInternals) {