[ASTMatcher] Add isStaticStorageClass matcher for varDecl and functionDecl.
authorHaojian Wu <hokein@google.com>
Mon, 26 Sep 2016 16:01:52 +0000 (16:01 +0000)
committerHaojian Wu <hokein@google.com>
Mon, 26 Sep 2016 16:01:52 +0000 (16:01 +0000)
Reviewers: klimek

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D24821

llvm-svn: 282415

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

index 662bfd94e18d97f907a3c4f685446b1b08b0944e..2c01bfa7a4494508cf9b86d7b857442c9293d2c0 100644 (file)
@@ -2610,6 +2610,20 @@ functionDecl(isNoThrow()) and functionProtoType(isNoThrow())
 </pre></td></tr>
 
 
+<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>&gt;</td><td class="name" onclick="toggle('isStaticStorageClass0')"><a name="isStaticStorageClass0Anchor">isStaticStorageClass</a></td><td></td></tr>
+<tr><td colspan="4" class="doc" id="isStaticStorageClass0"><pre>Matches variablefunction declarations that have static storage class
+(with "static" key word) written in the source.
+
+Given:
+  static void f() {}
+  static int i = 0;
+functionDecl(isStaticStorageClass())
+  matches the function declaration f.
+varDecl(isStaticStorageClass())
+  matches the variable declaration i.
+</pre></td></tr>
+
+
 <tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>&gt;</td><td class="name" onclick="toggle('isTemplateInstantiation0')"><a name="isTemplateInstantiation0Anchor">isTemplateInstantiation</a></td><td></td></tr>
 <tr><td colspan="4" class="doc" id="isTemplateInstantiation0"><pre>Matches template instantiations of function, class, or static
 member variable template instantiations.
@@ -3473,6 +3487,20 @@ functionDecl(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('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
+(with "static" key word) written in the source.
+
+Given:
+  static void f() {}
+  static int i = 0;
+functionDecl(isStaticStorageClass())
+  matches the function declaration f.
+varDecl(isStaticStorageClass())
+  matches the variable declaration i.
+</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('isTemplateInstantiation1')"><a name="isTemplateInstantiation1Anchor">isTemplateInstantiation</a></td><td></td></tr>
 <tr><td colspan="4" class="doc" id="isTemplateInstantiation1"><pre>Matches template instantiations of function, class, or static
 member variable template instantiations.
@@ -4092,7 +4120,7 @@ matcher, or is a pointer to a type that matches the InnerMatcher.
 
 
 <tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXMethodDecl.html">CXXMethodDecl</a>&gt;</td><td class="name" onclick="toggle('forEachOverridden0')"><a name="forEachOverridden0Anchor">forEachOverridden</a></td><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXMethodDecl.html">CXXMethodDecl</a>&gt; InnerMatcher</td></tr>
-<tr><td colspan="4" class="doc" id="forEachOverridden0"><pre>Matches each method overridden by the given method. This matcher may
+<tr><td colspan="4" class="doc" id="forEachOverridden0"><pre>Matches each method overriden by the given method. This matcher may
 produce multiple matches.
 
 Given
index fdef3bd70bbdde9c779adbb437f923a01b978805..a45b0bebcae09209319c36b5d88e9117ea0c1f9f 100644 (file)
@@ -3387,6 +3387,24 @@ AST_POLYMORPHIC_MATCHER(isExternC, AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
   return Node.isExternC();
 }
 
+/// \brief Matches variable/function declarations that have static storage class
+/// (with "static" key word) written in the source.
+///
+/// Given:
+/// \code
+///   static void f() {}
+///   static int i = 0;
+/// \endcode
+/// functionDecl(isStaticStorageClass())
+///   matches the function declaration f.
+/// varDecl(isStaticStorageClass())
+///   matches the variable declaration i.
+AST_POLYMORPHIC_MATCHER(isStaticStorageClass,
+                        AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
+                                                        VarDecl)) {
+  return Node.getStorageClass() == SC_Static;
+}
+
 /// \brief Matches deleted function declarations.
 ///
 /// Given:
index 1527eacb6ab761f7125c1ae071e6c39fd4940f86..5834211bdb0e2e7fa3cc220306b9499b7aaf3ae3 100644 (file)
@@ -848,6 +848,14 @@ TEST(IsExternC, MatchesExternCVariableDeclarations) {
   EXPECT_TRUE(notMatches("int i;", varDecl(isExternC())));
 }
 
+TEST(IsStaticStorageClass, MatchesStaticDeclarations) {
+  EXPECT_TRUE(
+      matches("static void f() {}", functionDecl(isStaticStorageClass())));
+  EXPECT_TRUE(matches("static int i = 1;", varDecl(isStaticStorageClass())));
+  EXPECT_TRUE(notMatches("int i = 1;", varDecl(isStaticStorageClass())));
+  EXPECT_TRUE(notMatches("void f() {}", functionDecl(isStaticStorageClass())));
+}
+
 TEST(IsDefaulted, MatchesDefaultedFunctionDeclarations) {
   EXPECT_TRUE(notMatches("class A { ~A(); };",
                          functionDecl(hasName("~A"), isDefaulted())));