From 4c92d444ffb0c65cfc85e60edca08d32fb10fdae Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Fri, 23 May 2014 17:49:03 +0000 Subject: [PATCH] Add the hasRangeInit() matcher for range-based for loop. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209533 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/ASTMatchers/ASTMatchers.h | 14 ++++++++++++++ unittests/ASTMatchers/ASTMatchersTest.cpp | 3 +++ 2 files changed, 17 insertions(+) diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index e7d5e5f1a1..2bee8e61b8 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -937,6 +937,20 @@ AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher, 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, + InnerMatcher) { + const Expr *const Init = Node.getRangeInit(); + return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder)); +} + /// \brief Matches while statements. /// /// Given diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index e7d99242fc..4b36467915 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -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) { -- 2.40.0