File tree Expand file tree Collapse file tree 4 files changed +56
-8
lines changed
Best Practices/Unused Entities
test/query-tests/Best Practices/Unused Entities/UnusedStaticFunctions Expand file tree Collapse file tree 4 files changed +56
-8
lines changed Original file line number Diff line number Diff line change 13
13
14
14
import cpp
15
15
16
+ pragma [ noinline]
17
+ predicate possiblyIncompleteFile ( File f ) {
18
+ exists ( Diagnostic d | d .getFile ( ) = f and d .getSeverity ( ) >= 3 )
19
+ }
20
+
16
21
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
25
38
f .getAnAttribute ( ) .hasName ( "unused" )
39
+ or
40
+ // a compiler error in the same file suggests we may be missing data
41
+ possiblyIncompleteFile ( f .getFile ( ) )
26
42
}
27
43
28
44
predicate immediatelyReachableVariable ( Variable v ) {
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -33,3 +33,16 @@ static void f6(void);
33
33
static void f5 (void ) { f6 (); }
34
34
static void f6 (void ) { f5 (); }
35
35
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
+
You can’t perform that action at this time.
0 commit comments