forked from OSchip/llvm-project
Add the hasRangeInit() matcher for range-based for loop.
llvm-svn: 209533
This commit is contained in:
parent
4625f85bb7
commit
8651081bbe
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue