Skip to content

Commit 41d344a

Browse files
committed
C++: Support if constexpr in QL CFG
This fixes the test `cpp/ql/test/library-tests/constexpr_if/cfg.ql`, which broke when the QL CFG was enabled. The new cases are just copy-pastes of the `IfStmt` cases (they don't share a useful common superclass) with added checks for whether their constant value equals 0.
1 parent 2eed38e commit 41d344a

File tree

1 file changed

+35
-3
lines changed
  • cpp/ql/src/semmle/code/cpp/controlflow/internal

1 file changed

+35
-3
lines changed

cpp/ql/src/semmle/code/cpp/controlflow/internal/CFG.qll

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,21 @@ private predicate subEdge(Pos p1, Node n1, Node n2, Pos p2) {
888888
p2.nodeAfter(n2, s)
889889
)
890890
or
891+
// ConstexprIfStmt -> condition ; { then, else } -> // same as IfStmt
892+
exists(ConstexprIfStmt s |
893+
p1.nodeAt(n1, s) and
894+
p2.nodeBefore(n2, s.getCondition())
895+
or
896+
p1.nodeAfter(n1, s.getThen()) and
897+
p2.nodeBeforeDestructors(n2, s)
898+
or
899+
p1.nodeAfter(n1, s.getElse()) and
900+
p2.nodeBeforeDestructors(n2, s)
901+
or
902+
p1.nodeAfterDestructors(n1, s) and
903+
p2.nodeAfter(n2, s)
904+
)
905+
or
891906
// WhileStmt -> condition ; body -> condition ; after dtors -> after
892907
exists(WhileStmt s |
893908
p1.nodeAt(n1, s) and
@@ -1175,9 +1190,8 @@ private class ExceptionSource extends Node {
11751190
}
11761191

11771192
/**
1178-
* Holds if `test` is the test of a control-flow construct that will always
1179-
* have true/false sub-edges out of it, where the `truth`-sub-edge goes to
1180-
* `(n2, p2)`.
1193+
* Holds if `test` is the test of a control-flow construct where the `truth`
1194+
* sub-edge goes to `(n2, p2)`.
11811195
*/
11821196
private predicate conditionJumpsTop(Expr test, boolean truth, Node n2, Pos p2) {
11831197
exists(IfStmt s | test = s.getCondition() |
@@ -1192,6 +1206,24 @@ private predicate conditionJumpsTop(Expr test, boolean truth, Node n2, Pos p2) {
11921206
p2.nodeBeforeDestructors(n2, s)
11931207
)
11941208
or
1209+
exists(ConstexprIfStmt s, string cond |
1210+
test = s.getCondition() and
1211+
cond = test.getFullyConverted().getValue()
1212+
|
1213+
truth = true and
1214+
cond != "0" and
1215+
p2.nodeBefore(n2, s.getThen())
1216+
or
1217+
truth = false and
1218+
cond = "0" and
1219+
p2.nodeBefore(n2, s.getElse())
1220+
or
1221+
not exists(s.getElse()) and
1222+
truth = false and
1223+
cond = "0" and
1224+
p2.nodeBeforeDestructors(n2, s)
1225+
)
1226+
or
11951227
exists(Loop l |
11961228
(
11971229
l instanceof WhileStmt

0 commit comments

Comments
 (0)