Skip to content

Commit 8979361

Browse files
committed
CPP: Exclude functions containing preprocessor logic.
1 parent 5cb30b0 commit 8979361

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

cpp/ql/src/Critical/DeadCodeGoto.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
import cpp
13+
import semmle.code.cpp.commons.Exclusions
1314

1415
Stmt getNextRealStmt(Block b, int i) {
1516
result = b.getStmt(i + 1) and
@@ -30,4 +31,6 @@ where b.getStmt(i) = js
3031
// the next statement isn't a loop that can be jumped into
3132
and not exists (LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls)
3233
and not exists (SwitchCase sc | s.(Loop).getStmt().getAChild*() = sc)
34+
// no preprocessor logic applies
35+
and not functionContainsPreprocCode(js.getEnclosingFunction())
3336
select js, "This statement makes $@ unreachable.", s, s.toString()

cpp/ql/src/semmle/code/cpp/commons/Exclusions.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,16 @@ predicate functionContainsDisabledCode(Function f) {
5858
)
5959
)
6060
}
61+
62+
/**
63+
* Holds if the function `f` contains code that could be excluded by the preprocessor.
64+
*/
65+
predicate functionContainsPreprocCode(Function f) {
66+
// `f` contains a preprocessor branch
67+
exists(PreprocessorBranchDirective pbd, string file, int pbdStartLine, int fBlockStartLine, int fBlockEndLine |
68+
functionLocation(f, file, fBlockStartLine, fBlockEndLine) and
69+
pbdLocation(pbd, file, pbdStartLine) and
70+
pbdStartLine <= fBlockEndLine and
71+
pbdStartLine >= fBlockStartLine
72+
)
73+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
| test.cpp:2:2:2:12 | goto ... | This statement makes $@ unreachable. | test.cpp:3:2:3:5 | ExprStmt | ExprStmt |
22
| test.cpp:9:3:9:8 | break; | This statement makes $@ unreachable. | test.cpp:10:3:10:6 | ExprStmt | ExprStmt |
33
| test.cpp:37:3:37:8 | break; | This statement makes $@ unreachable. | test.cpp:38:3:38:11 | return ... | return ... |
4-
| test.cpp:91:2:91:11 | goto ... | This statement makes $@ unreachable. | test.cpp:93:2:93:5 | ExprStmt | ExprStmt |

cpp/ql/test/query-tests/Critical/DeadCodeGoto/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void test8() {
8888
int x = 0;
8989

9090
#ifdef CONFIG_DEFINE
91-
goto skip; // GOOD (the `x++` is still reachable in some configurations) [FALSE POSITIVE]
91+
goto skip; // GOOD (the `x++` is still reachable in some configurations)
9292
#endif
9393
x++;
9494

0 commit comments

Comments
 (0)