summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2014-02-05 16:35:08 +0000
committerAlexander Kornienko <alexfh@google.com>2014-02-05 16:35:08 +0000
commit9b539e1dd9a134ccae450a7fb81fa64e7442e0c7 (patch)
treeef85aa6cdf410187b2e764fddf5b3b804e0e5b21 /clang
parent0b79be8ab24c8302c071157df7dc79b1d5b8a7e9 (diff)
downloadbcm5719-llvm-9b539e1dd9a134ccae450a7fb81fa64e7442e0c7.tar.gz
bcm5719-llvm-9b539e1dd9a134ccae450a7fb81fa64e7442e0c7.zip
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
Diffstat (limited to 'clang')
-rw-r--r--clang/include/clang/ASTMatchers/ASTMatchers.h32
-rw-r--r--clang/unittests/ASTMatchers/ASTMatchersTest.cpp5
2 files changed, 28 insertions, 9 deletions
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index c2f4928e8f5..4be693f2929 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -842,15 +842,6 @@ const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
/// \endcode
const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
-/// \brief Matches range-based for statements.
-///
-/// forRangeStmt() matches 'for (auto a : i)'
-/// \code
-/// int i[] = {1, 2, 3}; for (auto a : i);
-/// for(int j = 0; j < 5; ++j);
-/// \endcode
-const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt;
-
/// \brief Matches the increment statement of a for loop.
///
/// Example:
@@ -880,6 +871,29 @@ AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>,
return (Init != NULL && InnerMatcher.matches(*Init, Finder, Builder));
}
+/// \brief Matches range-based for statements.
+///
+/// forRangeStmt() matches 'for (auto a : i)'
+/// \code
+/// int i[] = {1, 2, 3}; for (auto a : i);
+/// for(int j = 0; j < 5; ++j);
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt;
+
+/// \brief Matches the initialization statement of a for loop.
+///
+/// Example:
+/// forStmt(hasLoopVariable(anything()))
+/// matches 'int x' in
+/// \code
+/// for (int x : a) { }
+/// \endcode
+AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>,
+ InnerMatcher) {
+ const VarDecl *const Var = Node.getLoopVariable();
+ return (Var != NULL && InnerMatcher.matches(*Var, Finder, Builder));
+}
+
/// \brief Matches while statements.
///
/// Given
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
index 771cf673ab2..a76903fc4af 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -2339,6 +2339,11 @@ TEST(For, ForLoopInternals) {
forStmt(hasLoopInit(anything()))));
}
+TEST(For, ForRangeLoopInternals) {
+ EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }",
+ forRangeStmt(hasLoopVariable(anything()))));
+}
+
TEST(For, NegativeForLoopInternals) {
EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }",
forStmt(hasCondition(expr()))));
OpenPOWER on IntegriCloud