From: Haojian Wu Date: Mon, 26 Sep 2016 16:01:52 +0000 (+0000) Subject: [ASTMatcher] Add isStaticStorageClass matcher for varDecl and functionDecl. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b2c1023a57dfe16e64e14fcd207a82f1571345a1;p=clang [ASTMatcher] Add isStaticStorageClass matcher for varDecl and functionDecl. Reviewers: klimek Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D24821 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@282415 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 662bfd94e1..2c01bfa7a4 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -2610,6 +2610,20 @@ functionDecl(isNoThrow()) and functionProtoType(isNoThrow()) +Matcher<FunctionDecl>isStaticStorageClass +
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.
+
+ + Matcher<FunctionDecl>isTemplateInstantiation
Matches template instantiations of function, class, or static
 member variable template instantiations.
@@ -3473,6 +3487,20 @@ functionDecl(isExternC())
 
+Matcher<VarDecl>isStaticStorageClass +
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.
+
+ + Matcher<VarDecl>isTemplateInstantiation
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.
 
 
 Matcher<CXXMethodDecl>forEachOverriddenMatcher<CXXMethodDecl> InnerMatcher
-
Matches each method overridden by the given method. This matcher may
+
Matches each method overriden by the given method. This matcher may
 produce multiple matches.
 
 Given
diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h
index fdef3bd70b..a45b0bebca 100644
--- a/include/clang/ASTMatchers/ASTMatchers.h
+++ b/include/clang/ASTMatchers/ASTMatchers.h
@@ -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:
diff --git a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 1527eacb6a..5834211bdb 100644
--- a/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -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())));