Skip to content

Commit 07dd31d

Browse files
committed
[ASTMatchers] fix isStaticStorageClass not matching definitions of forward declared functions
```c++ static void foo(); void foo() {} struct A { static void bar(); }; void A::bar() {} ``` Both definitions refer to their previous declaration, but were not considered `static`, because the matcher did not check the canonical declaration.
1 parent 8345a95 commit 07dd31d

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

clang/docs/LibASTMatchersReference.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4683,8 +4683,10 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
46834683
static int i = 0;
46844684
extern int j;
46854685
int k;
4686+
static void l();
4687+
void l() {}
46864688
functionDecl(isStaticStorageClass())
4687-
matches the function declaration f.
4689+
matches the function declaration of f and l, and the definition of l.
46884690
varDecl(isStaticStorageClass())
46894691
matches the variable declaration i.
46904692
</pre></td></tr>

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,9 @@ AST Matchers
10411041

10421042
- Ensure ``pointee`` matches Objective-C pointer types.
10431043

1044+
- Fixed `isStaticStorageClass` not matching the definition if the definition was
1045+
not marked `static` as well.
1046+
10441047
clang-format
10451048
------------
10461049

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5418,15 +5418,17 @@ AST_POLYMORPHIC_MATCHER(isExternC, AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
54185418
/// static int i = 0;
54195419
/// extern int j;
54205420
/// int k;
5421+
/// static void l();
5422+
/// void l() {}
54215423
/// \endcode
54225424
/// functionDecl(isStaticStorageClass())
5423-
/// matches the function declaration f.
5425+
/// matches the function declaration of f and l, and the definition of l.
54245426
/// varDecl(isStaticStorageClass())
54255427
/// matches the variable declaration i.
54265428
AST_POLYMORPHIC_MATCHER(isStaticStorageClass,
54275429
AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
54285430
VarDecl)) {
5429-
return Node.getStorageClass() == SC_Static;
5431+
return Node.getCanonicalDecl()->getStorageClass() == SC_Static;
54305432
}
54315433

54325434
/// Matches deleted function declarations.

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,14 @@ TEST_P(ASTMatchersTest, IsStaticStorageClass) {
18261826
EXPECT_TRUE(notMatches("int i = 1;", varDecl(isStaticStorageClass())));
18271827
EXPECT_TRUE(notMatches("extern int i;", varDecl(isStaticStorageClass())));
18281828
EXPECT_TRUE(notMatches("void f() {}", functionDecl(isStaticStorageClass())));
1829+
1830+
if (!GetParam().isCXX())
1831+
return;
1832+
1833+
EXPECT_TRUE(matches("static void foo(); void foo() {}",
1834+
functionDecl(isDefinition(), isStaticStorageClass())));
1835+
EXPECT_TRUE(matches("struct A { static void bar(); }; void A::bar() {}",
1836+
cxxMethodDecl(isDefinition(), isStaticStorageClass())));
18291837
}
18301838

18311839
TEST_P(ASTMatchersTest, IsDefaulted) {

0 commit comments

Comments
 (0)