Skip to content

Commit 73f977c

Browse files
authored
Merge pull request #10510 from geoffw0/staticfn
C++: Fix FPs for cpp/unused-static-function in files that were not extracted completely
2 parents 1d745a6 + d60a829 commit 73f977c

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

cpp/ql/src/Best Practices/Unused Entities/UnusedStaticFunctions.ql

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,32 @@
1313

1414
import cpp
1515

16+
pragma[noinline]
17+
predicate possiblyIncompleteFile(File f) {
18+
exists(Diagnostic d | d.getFile() = f and d.getSeverity() >= 3)
19+
}
20+
1621
predicate immediatelyReachableFunction(Function f) {
17-
not f.isStatic() or
18-
exists(BlockExpr be | be.getFunction() = f) or
19-
f instanceof MemberFunction or
20-
f instanceof TemplateFunction or
21-
f.getFile() instanceof HeaderFile or
22-
f.getAnAttribute().hasName("constructor") or
23-
f.getAnAttribute().hasName("destructor") or
24-
f.getAnAttribute().hasName("used") or
22+
not f.isStatic()
23+
or
24+
exists(BlockExpr be | be.getFunction() = f)
25+
or
26+
f instanceof MemberFunction
27+
or
28+
f instanceof TemplateFunction
29+
or
30+
f.getFile() instanceof HeaderFile
31+
or
32+
f.getAnAttribute().hasName("constructor")
33+
or
34+
f.getAnAttribute().hasName("destructor")
35+
or
36+
f.getAnAttribute().hasName("used")
37+
or
2538
f.getAnAttribute().hasName("unused")
39+
or
40+
// a compiler error in the same file suggests we may be missing data
41+
possiblyIncompleteFile(f.getFile())
2642
}
2743

2844
predicate immediatelyReachableVariable(Variable v) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed false positives from the "Unused static function" (`cpp/unused-static-function`) query in files that had errors during compilation.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// semmle-extractor-options: --expect_errors
2+
3+
static void my_function1_called() {} // GOOD
4+
static void my_function2_called_after_error() {} // GOOD
5+
static void my_function3_not_called() {} // BAD [NOT DETECTED]
6+
7+
int main(void) {
8+
my_function1_called();
9+
10+
--- compilation stops here because this line is not valid C code ---
11+
12+
my_function2_called_after_error();
13+
14+
return 0;
15+
}

cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticFunctions/unused_static_functions.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ static void f6(void);
3333
static void f5(void) { f6(); }
3434
static void f6(void) { f5(); }
3535

36+
// f7 and f8 are reachable from `function_caller`
37+
static int f7() { return 1; } // GOOD
38+
static void f8() { } // GOOD
39+
40+
void function_caller()
41+
{
42+
auto my_lambda = []() {
43+
return f7();
44+
}();
45+
46+
f8();
47+
}
48+

0 commit comments

Comments
 (0)