From 67c63cb9f6cb94fcc7ad47cc8a827ed243adab44 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Mon, 29 Oct 2018 13:47:56 +0000 Subject: [PATCH] Add the isStaticLocal() AST matcher for matching on local static variables. Patch by Joe Ranieri. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@345502 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/LibASTMatchersReference.html | 12 ++++++++++++ include/clang/ASTMatchers/ASTMatchers.h | 14 ++++++++++++++ lib/ASTMatchers/Dynamic/Registry.cpp | 1 + unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp | 8 ++++++++ 4 files changed, 35 insertions(+) diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index d0a865283e..c155465601 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -4123,6 +4123,18 @@ varDecl(isExternC()) +Matcher<VarDecl>isStaticLocal +
Matches a static variable with local scope.
+
+Example matches y (matcher = varDecl(isStaticLocal()))
+void f() {
+  int x;
+  static int y;
+}
+static int z;
+
+ + Matcher<VarDecl>isStaticStorageClass
Matches variablefunction declarations that have "static" storage
 class specifier ("static" keyword) written in the source.
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index 3f8690bd8a..6635f25d55 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -3300,6 +3300,20 @@ AST_MATCHER_P(
           InnerMatcher.matches(*Initializer, Finder, Builder));
 }
 
+/// \brief Matches a static variable with local scope.
+///
+/// Example matches y (matcher = varDecl(isStaticLocal()))
+/// \code
+/// void f() {
+///   int x;
+///   static int y;
+/// }
+/// static int z;
+/// \endcode
+AST_MATCHER(VarDecl, isStaticLocal) {
+  return Node.isStaticLocal();
+}
+
 /// Matches a variable declaration that has function scope and is a
 /// non-static local variable.
 ///
diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp
index d52c46aa1e..318f39dee9 100644
--- a/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -378,6 +378,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(isPure);
   REGISTER_MATCHER(isScoped);
   REGISTER_MATCHER(isSignedInteger);
+  REGISTER_MATCHER(isStaticLocal);
   REGISTER_MATCHER(isStaticStorageClass);
   REGISTER_MATCHER(isStruct);
   REGISTER_MATCHER(isTemplateInstantiation);
diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 39a5d57729..e37bcbeec1 100644
--- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -667,6 +667,14 @@ TEST(Matcher, VarDecl_Storage) {
   EXPECT_TRUE(matches("void f() { static int X; }", M));
 }
 
+TEST(Matcher, VarDecl_IsStaticLocal) {
+  auto M = varDecl(isStaticLocal());
+  EXPECT_TRUE(matches("void f() { static int X; }", M));
+  EXPECT_TRUE(notMatches("static int X;", M));
+  EXPECT_TRUE(notMatches("void f() { int X; }", M));
+  EXPECT_TRUE(notMatches("int X;", M));
+}
+
 TEST(Matcher, VarDecl_StorageDuration) {
   std::string T =
     "void f() { int x; static int y; } int a;static int b;extern int c;";
-- 
2.40.0