Skip to content

Commit 9b539e1

Browse files
committedFeb 5, 2014
Added the hasLoopVariable sub-matcher for forRangeStmt.
Summary: This sub-matcher makes it possible to access directly the range-based for loop variable: forRangeStmt(hasLoopVariable(anything()).bind(...)). I've tried to re-generate the docs, but the diffs seem to include much more than this change could cause, so I'd better leave docs update to someone who knows the intended changes in the contents better. Reviewers: klimek Reviewed By: klimek CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D2702 llvm-svn: 200850
1 parent 0b79be8 commit 9b539e1

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed
 

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

+23-9
Original file line numberDiff line numberDiff line change
@@ -842,15 +842,6 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
842842
/// \endcode
843843
const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
844844

845-
/// \brief Matches range-based for statements.
846-
///
847-
/// forRangeStmt() matches 'for (auto a : i)'
848-
/// \code
849-
/// int i[] = {1, 2, 3}; for (auto a : i);
850-
/// for(int j = 0; j < 5; ++j);
851-
/// \endcode
852-
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt;
853-
854845
/// \brief Matches the increment statement of a for loop.
855846
///
856847
/// Example:
@@ -880,6 +871,29 @@ AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>,
880871
return (Init != NULL && InnerMatcher.matches(*Init, Finder, Builder));
881872
}
882873

874+
/// \brief Matches range-based for statements.
875+
///
876+
/// forRangeStmt() matches 'for (auto a : i)'
877+
/// \code
878+
/// int i[] = {1, 2, 3}; for (auto a : i);
879+
/// for(int j = 0; j < 5; ++j);
880+
/// \endcode
881+
const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt;
882+
883+
/// \brief Matches the initialization statement of a for loop.
884+
///
885+
/// Example:
886+
/// forStmt(hasLoopVariable(anything()))
887+
/// matches 'int x' in
888+
/// \code
889+
/// for (int x : a) { }
890+
/// \endcode
891+
AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>,
892+
InnerMatcher) {
893+
const VarDecl *const Var = Node.getLoopVariable();
894+
return (Var != NULL && InnerMatcher.matches(*Var, Finder, Builder));
895+
}
896+
883897
/// \brief Matches while statements.
884898
///
885899
/// Given

‎clang/unittests/ASTMatchers/ASTMatchersTest.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,11 @@ TEST(For, ForLoopInternals) {
23392339
forStmt(hasLoopInit(anything()))));
23402340
}
23412341

2342+
TEST(For, ForRangeLoopInternals) {
2343+
EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
2344+
forRangeStmt(hasLoopVariable(anything()))));
2345+
}
2346+
23422347
TEST(For, NegativeForLoopInternals) {
23432348
EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
23442349
forStmt(hasCondition(expr()))));

0 commit comments

Comments
 (0)
Please sign in to comment.