diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index d0a865283ef4..c1554656011a 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -4123,6 +4123,18 @@ varDecl(isExternC()) +
Matches a static variable with local scope. + +Example matches y (matcher = varDecl(isStaticLocal())) +void f() { + int x; + static int y; +} +static int z; +
Matches variablefunction declarations that have "static" storage class specifier ("static" keyword) written in the source. diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 3f8690bd8ae7..6635f25d5572 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/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/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp index d52c46aa1eb3..318f39dee94e 100644 --- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/clang/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/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index 39a5d5772919..e37bcbeec1f3 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/clang/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;";