Skip to content

Commit 032678a

Browse files
committed
C++: Extend tests to also test the new predicates.
1 parent dbd47b3 commit 032678a

File tree

2 files changed

+87
-30
lines changed

2 files changed

+87
-30
lines changed

cpp/ql/test/library-tests/controlflow/guards-ir/tests.ql

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,32 @@ import semmle.code.cpp.controlflow.IRGuards
44
query predicate astGuards(GuardCondition guard) { any() }
55

66
query predicate astGuardsCompare(int startLine, string msg) {
7-
exists(GuardCondition guard, Expr left, Expr right, int k, string which, string op |
7+
exists(GuardCondition guard, Expr left, int k, string which, string op |
88
exists(boolean sense |
99
sense = true and which = "true"
1010
or
1111
sense = false and which = "false"
1212
|
13-
guard.comparesLt(left, right, k, true, sense) and op = " < "
13+
exists(Expr right |
14+
guard.comparesLt(left, right, k, true, sense) and op = " < "
15+
or
16+
guard.comparesLt(left, right, k, false, sense) and op = " >= "
17+
or
18+
guard.comparesEq(left, right, k, true, sense) and op = " == "
19+
or
20+
guard.comparesEq(left, right, k, false, sense) and op = " != "
21+
|
22+
msg = left + op + right + "+" + k + " when " + guard + " is " + which
23+
)
1424
or
15-
guard.comparesLt(left, right, k, false, sense) and op = " >= "
16-
or
17-
guard.comparesEq(left, right, k, true, sense) and op = " == "
18-
or
19-
guard.comparesEq(left, right, k, false, sense) and op = " != "
25+
(
26+
guard.comparesEq(left, k, true, sense) and op = " == "
27+
or
28+
guard.comparesEq(left, k, false, sense) and op = " != "
29+
) and
30+
msg = left + op + k + " when " + guard + " is " + which
2031
) and
21-
startLine = guard.getLocation().getStartLine() and
22-
msg = left + op + right + "+" + k + " when " + guard + " is " + which
32+
startLine = guard.getLocation().getStartLine()
2333
)
2434
}
2535

@@ -46,28 +56,52 @@ query predicate astGuardsEnsure(
4656
)
4757
}
4858

59+
query predicate astGuardsEnsure_const(
60+
GuardCondition guard, Expr left, string op, int k, int start, int end
61+
) {
62+
exists(BasicBlock block |
63+
guard.ensuresEq(left, k, block, true) and op = "=="
64+
or
65+
guard.ensuresEq(left, k, block, false) and op = "!="
66+
|
67+
block.hasLocationInfo(_, start, _, end, _)
68+
)
69+
}
70+
4971
query predicate irGuards(IRGuardCondition guard) { any() }
5072

5173
query predicate irGuardsCompare(int startLine, string msg) {
52-
exists(IRGuardCondition guard, Operand left, Operand right, int k, string which, string op |
74+
exists(IRGuardCondition guard, Operand left, int k, string which, string op |
5375
exists(boolean sense |
5476
sense = true and which = "true"
5577
or
5678
sense = false and which = "false"
5779
|
58-
guard.comparesLt(left, right, k, true, sense) and op = " < "
80+
exists(Operand right |
81+
guard.comparesLt(left, right, k, true, sense) and op = " < "
82+
or
83+
guard.comparesLt(left, right, k, false, sense) and op = " >= "
84+
or
85+
guard.comparesEq(left, right, k, true, sense) and op = " == "
86+
or
87+
guard.comparesEq(left, right, k, false, sense) and op = " != "
88+
|
89+
msg =
90+
left.getAnyDef().getUnconvertedResultExpression() + op +
91+
right.getAnyDef().getUnconvertedResultExpression() + "+" + k + " when " + guard + " is "
92+
+ which
93+
)
5994
or
60-
guard.comparesLt(left, right, k, false, sense) and op = " >= "
61-
or
62-
guard.comparesEq(left, right, k, true, sense) and op = " == "
63-
or
64-
guard.comparesEq(left, right, k, false, sense) and op = " != "
95+
(
96+
guard.comparesEq(left, k, true, sense) and op = " == "
97+
or
98+
guard.comparesEq(left, k, false, sense) and op = " != "
99+
) and
100+
msg =
101+
left.getAnyDef().getUnconvertedResultExpression() + op + k + " when " + guard + " is " +
102+
which
65103
) and
66-
startLine = guard.getLocation().getStartLine() and
67-
msg =
68-
left.getAnyDef().getUnconvertedResultExpression() + op +
69-
right.getAnyDef().getUnconvertedResultExpression() + "+" + k + " when " + guard + " is " +
70-
which
104+
startLine = guard.getLocation().getStartLine()
71105
)
72106
}
73107

@@ -95,3 +129,16 @@ query predicate irGuardsEnsure(
95129
block.getLocation().hasLocationInfo(_, start, _, end, _)
96130
)
97131
}
132+
133+
query predicate irGuardsEnsure_const(
134+
IRGuardCondition guard, Instruction left, string op, int k, int start, int end
135+
) {
136+
exists(IRBlock block, Operand leftOp |
137+
guard.ensuresEq(leftOp, k, block, true) and op = "=="
138+
or
139+
guard.ensuresEq(leftOp, k, block, false) and op = "!="
140+
|
141+
leftOp = left.getAUse() and
142+
block.getLocation().hasLocationInfo(_, start, _, end, _)
143+
)
144+
}

cpp/ql/test/library-tests/controlflow/guards/GuardsCompare.ql

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@
77
import cpp
88
import semmle.code.cpp.controlflow.Guards
99

10-
from GuardCondition guard, Expr left, Expr right, int k, string which, string op, string msg
10+
from GuardCondition guard, Expr left, int k, string which, string op, string msg
1111
where
1212
exists(boolean sense |
1313
sense = true and which = "true"
1414
or
1515
sense = false and which = "false"
1616
|
17-
guard.comparesLt(left, right, k, true, sense) and op = " < "
17+
exists(Expr right |
18+
guard.comparesLt(left, right, k, true, sense) and op = " < "
19+
or
20+
guard.comparesLt(left, right, k, false, sense) and op = " >= "
21+
or
22+
guard.comparesEq(left, right, k, true, sense) and op = " == "
23+
or
24+
guard.comparesEq(left, right, k, false, sense) and op = " != "
25+
|
26+
msg = left + op + right + "+" + k + " when " + guard + " is " + which
27+
)
1828
or
19-
guard.comparesLt(left, right, k, false, sense) and op = " >= "
20-
or
21-
guard.comparesEq(left, right, k, true, sense) and op = " == "
22-
or
23-
guard.comparesEq(left, right, k, false, sense) and op = " != "
24-
) and
25-
msg = left + op + right + "+" + k + " when " + guard + " is " + which
29+
(
30+
guard.comparesEq(left, k, true, sense) and op = " == "
31+
or
32+
guard.comparesEq(left, k, false, sense) and op = " != "
33+
) and
34+
msg = left + op + k + " when " + guard + " is " + which
35+
)
2636
select guard.getLocation().getStartLine(), msg

0 commit comments

Comments
 (0)