]> granicus.if.org Git - clang/commitdiff
Add the isStaticLocal() AST matcher for matching on local static variables.
authorAaron Ballman <aaron@aaronballman.com>
Mon, 29 Oct 2018 13:47:56 +0000 (13:47 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Mon, 29 Oct 2018 13:47:56 +0000 (13:47 +0000)
Patch by Joe Ranieri.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@345502 91177308-0d34-0410-b5e6-96231b3b80d8

docs/LibASTMatchersReference.html
include/clang/ASTMatchers/ASTMatchers.h
lib/ASTMatchers/Dynamic/Registry.cpp
unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

index d0a865283ef4acab61311050b4e23a708b489fe3..c1554656011abe823336c764cdaf82d5d9d81bf3 100644 (file)
@@ -4123,6 +4123,18 @@ varDecl(isExternC())
 </pre></td></tr>
 
 
+<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;</td><td class="name" onclick="toggle('isStaticLocal0')"><a name="isStaticLocal0Anchor">isStaticLocal</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isStaticLocal0"><pre>Matches a static variable with local scope.
+
+Example matches y (matcher = varDecl(isStaticLocal()))
+void f() {
+  int x;
+  static int y;
+}
+static int z;
+</pre></td></tr>
+
+
 <tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;</td><td class="name" onclick="toggle('isStaticStorageClass1')"><a name="isStaticStorageClass1Anchor">isStaticStorageClass</a></td><td></td></tr>
 <tr><td colspan="4" class="doc" id="isStaticStorageClass1"><pre>Matches variablefunction declarations that have "static" storage
 class specifier ("static" keyword) written in the source.
index 3f8690bd8ae7854582f81d6451d062a48a59379e..6635f25d5572427bd47668610fc2c68ff20ec1f2 100644 (file)
@@ -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.
 ///
index d52c46aa1eb3a5a3f1ad76faa549aa6ab349a53c..318f39dee94e9450767f87d33afe0c820262a797 100644 (file)
@@ -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);
index 39a5d577291977fbaa214eef7ff823fa7a5a874b..e37bcbeec1f3030e74c4c03642b8a2531f9af6e2 100644 (file)
@@ -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;";