]> granicus.if.org Git - clang/commitdiff
Add the hasRangeInit() matcher for range-based for loop.
authorManuel Klimek <klimek@google.com>
Fri, 23 May 2014 17:49:03 +0000 (17:49 +0000)
committerManuel Klimek <klimek@google.com>
Fri, 23 May 2014 17:49:03 +0000 (17:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209533 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/ASTMatchers/ASTMatchers.h
unittests/ASTMatchers/ASTMatchersTest.cpp

index e7d5e5f1a14fea159f40005d24f4757d795aa801..2bee8e61b857246502fcd8bfbff5461b9ec098b7 100644 (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
index e7d99242fcf0f69bdfe8434b5766cf9cc45aa9d9..4b364679152340af6e27e2f2c343498bb3730a35 100644 (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) {