Skip to content

Commit 2756c0e

Browse files
committed
C++: Don't report results in files with compilation errors.
1 parent c599b02 commit 2756c0e

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,28 @@
1414
import cpp
1515

1616
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
17+
not f.isStatic()
18+
or
19+
exists(BlockExpr be | be.getFunction() = f)
20+
or
21+
f instanceof MemberFunction
22+
or
23+
f instanceof TemplateFunction
24+
or
25+
f.getFile() instanceof HeaderFile
26+
or
27+
f.getAnAttribute().hasName("constructor")
28+
or
29+
f.getAnAttribute().hasName("destructor")
30+
or
31+
f.getAnAttribute().hasName("used")
32+
or
2533
f.getAnAttribute().hasName("unused")
34+
or
35+
// a compiler error in the same file suggests we may be missing data
36+
exists(Diagnostic d | d.getFile() = f.getFile() and d.getSeverity() >= 3)
37+
or
38+
exists(ErrorExpr ee | ee.getFile() = f.getFile())
2639
}
2740

2841
predicate immediatelyReachableVariable(Variable v) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
| extraction_error.c:4:13:4:43 | my_function2_called_after_error | Static function my_function2_called_after_error is unreachable | extraction_error.c:4:13:4:43 | my_function2_called_after_error | my_function2_called_after_error |
2-
| extraction_error.c:5:13:5:35 | my_function3_not_called | Static function my_function3_not_called is unreachable | extraction_error.c:5:13:5:35 | my_function3_not_called | my_function3_not_called |
31
| unused_functions.c:16:13:16:27 | unused_function | Static function unused_function is unreachable | unused_functions.c:16:13:16:27 | unused_function | unused_function |
42
| unused_functions.c:20:13:20:28 | unused_function2 | Static function unused_function2 is unreachable ($@ must be removed at the same time) | unused_functions.c:24:13:24:28 | unused_function3 | unused_function3 |
53
| unused_functions.c:24:13:24:28 | unused_function3 | Static function unused_function3 is unreachable | unused_functions.c:24:13:24:28 | unused_function3 | unused_function3 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// semmle-extractor-options: --expect_errors
22

33
static void my_function1_called() {} // GOOD
4-
static void my_function2_called_after_error() {} // GOOD [FALSE POSITIVE]
5-
static void my_function3_not_called() {} // BAD
4+
static void my_function2_called_after_error() {} // GOOD
5+
static void my_function3_not_called() {} // BAD [NOT DETECTED]
66

77
int main(void) {
88
my_function1_called();

0 commit comments

Comments
 (0)