Skip to content

Commit d296905

Browse files
committed
C++: Remove workaround for missing comparisons against 0 in C code.
1 parent c47a92d commit d296905

File tree

3 files changed

+5
-228
lines changed

3 files changed

+5
-228
lines changed

cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -895,55 +895,6 @@ private module Cached {
895895
value.(BooleanValue).getValue() = false
896896
}
897897

898-
/**
899-
* Holds if `op` is an operand that is eventually used in a unary comparison
900-
* with a constant.
901-
*/
902-
private predicate isRelevantUnaryComparisonOperand(Operand op) {
903-
// Base case: `op` is an operand of a `CompareEQInstruction` or `CompareNEInstruction`,
904-
// and the other operand is a constant.
905-
exists(CompareInstruction eq, Instruction instr |
906-
eq.hasOperands(op, instr.getAUse()) and
907-
exists(int_value(instr))
908-
|
909-
eq instanceof CompareEQInstruction
910-
or
911-
eq instanceof CompareNEInstruction
912-
)
913-
or
914-
// C doesn't have int-to-bool conversions, so `if(x)` will just generate:
915-
// r2_1(glval<int>) = VariableAddress[x]
916-
// r2_2(int) = Load[x] : &:r2_1, m1_6
917-
// v2_3(void) = ConditionalBranch : r2_2
918-
exists(ConditionalBranchInstruction branch | branch.getConditionOperand() = op)
919-
or
920-
// If `!x` is a relevant unary comparison then so is `x`.
921-
exists(LogicalNotInstruction logicalNot |
922-
isRelevantUnaryComparisonOperand(unique( | | logicalNot.getAUse())) and
923-
logicalNot.getUnaryOperand() = op
924-
)
925-
or
926-
// If `y` is a relevant unary comparison and `y = x` then so is `x`.
927-
not op.isDefinitionInexact() and
928-
exists(CopyInstruction copy |
929-
isRelevantUnaryComparisonOperand(unique( | | copy.getAUse())) and
930-
op = copy.getSourceValueOperand()
931-
)
932-
or
933-
// If phi(x1, x2) is a relevant unary comparison then so are `x1` and `x2`.
934-
not op.isDefinitionInexact() and
935-
exists(PhiInstruction phi |
936-
isRelevantUnaryComparisonOperand(unique( | | phi.getAUse())) and
937-
op = phi.getAnInputOperand()
938-
)
939-
or
940-
// If `__builtin_expect(x)` is a relevant unary comparison then so is `x`.
941-
exists(BuiltinExpectCallInstruction call |
942-
isRelevantUnaryComparisonOperand(unique( | | call.getAUse())) and
943-
op = call.getConditionOperand()
944-
)
945-
}
946-
947898
/** Rearrange various simple comparisons into `op == k` form. */
948899
private predicate unary_simple_comparison_eq(
949900
ValueNumber test, Operand op, int k, AbstractValue value
@@ -956,14 +907,12 @@ private module Cached {
956907
case.getValue().toInt() = k
957908
)
958909
or
959-
isRelevantUnaryComparisonOperand(op) and
960-
op.getDef() = test.getAnInstruction() and
961-
(
962-
k = 1 and
963-
value.(BooleanValue).getValue() = true
910+
exists(Instruction const | int_value(const) = k |
911+
value.(BooleanValue).getValue() = true and
912+
test.(CompareEQValueNumber).hasOperands(op, const.getAUse())
964913
or
965-
k = 0 and
966-
value.(BooleanValue).getValue() = false
914+
value.(BooleanValue).getValue() = false and
915+
test.(CompareNEValueNumber).hasOperands(op, const.getAUse())
967916
)
968917
}
969918

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
| 7 | 0 < x+0 when ... > ... is true |
22
| 7 | 0 >= x+0 when ... > ... is false |
3-
| 7 | ... > ... != 0 when ... > ... is true |
4-
| 7 | ... > ... == 0 when ... > ... is false |
53
| 7 | x < 0+1 when ... > ... is false |
64
| 7 | x < 1 when ... > ... is false |
75
| 7 | x >= 0+1 when ... > ... is true |
@@ -12,12 +10,6 @@
1210
| 17 | 1 < y+0 when ... && ... is true |
1311
| 17 | 1 < y+0 when ... > ... is true |
1412
| 17 | 1 >= y+0 when ... > ... is false |
15-
| 17 | ... < ... != 0 when ... && ... is true |
16-
| 17 | ... < ... != 0 when ... < ... is true |
17-
| 17 | ... < ... == 0 when ... < ... is false |
18-
| 17 | ... > ... != 0 when ... && ... is true |
19-
| 17 | ... > ... != 0 when ... > ... is true |
20-
| 17 | ... > ... == 0 when ... > ... is false |
2113
| 17 | x < 0 when ... && ... is true |
2214
| 17 | x < 0 when ... < ... is true |
2315
| 17 | x < 0+0 when ... && ... is true |
@@ -34,50 +26,36 @@
3426
| 18 | call to get == 0 when call to get is false |
3527
| 26 | 0 < x+0 when ... > ... is true |
3628
| 26 | 0 >= x+0 when ... > ... is false |
37-
| 26 | ... > ... != 0 when ... > ... is true |
38-
| 26 | ... > ... == 0 when ... > ... is false |
3929
| 26 | x < 0+1 when ... > ... is false |
4030
| 26 | x < 1 when ... > ... is false |
4131
| 26 | x >= 0+1 when ... > ... is true |
4232
| 26 | x >= 1 when ... > ... is true |
4333
| 31 | - ... != x+0 when ... == ... is false |
4434
| 31 | - ... == x+0 when ... == ... is true |
45-
| 31 | ... == ... != 0 when ... == ... is true |
46-
| 31 | ... == ... == 0 when ... == ... is false |
4735
| 31 | x != -1 when ... == ... is false |
4836
| 31 | x != - ...+0 when ... == ... is false |
4937
| 31 | x == -1 when ... == ... is true |
5038
| 31 | x == - ...+0 when ... == ... is true |
5139
| 34 | 10 < j+1 when ... < ... is false |
5240
| 34 | 10 >= j+1 when ... < ... is true |
53-
| 34 | ... < ... != 0 when ... < ... is true |
54-
| 34 | ... < ... == 0 when ... < ... is false |
5541
| 34 | j < 10 when ... < ... is true |
5642
| 34 | j < 10+0 when ... < ... is true |
5743
| 34 | j >= 10 when ... < ... is false |
5844
| 34 | j >= 10+0 when ... < ... is false |
5945
| 42 | 10 < j+1 when ... < ... is false |
6046
| 42 | 10 >= j+1 when ... < ... is true |
61-
| 42 | ... < ... != 0 when ... < ... is true |
62-
| 42 | ... < ... == 0 when ... < ... is false |
63-
| 42 | call to getABool != 0 when call to getABool is true |
64-
| 42 | call to getABool == 0 when call to getABool is false |
6547
| 42 | j < 10 when ... < ... is true |
6648
| 42 | j < 10+0 when ... < ... is true |
6749
| 42 | j >= 10 when ... < ... is false |
6850
| 42 | j >= 10+0 when ... < ... is false |
6951
| 44 | 0 < z+0 when ... > ... is true |
7052
| 44 | 0 >= z+0 when ... > ... is false |
71-
| 44 | ... > ... != 0 when ... > ... is true |
72-
| 44 | ... > ... == 0 when ... > ... is false |
7353
| 44 | z < 0+1 when ... > ... is false |
7454
| 44 | z < 1 when ... > ... is false |
7555
| 44 | z >= 0+1 when ... > ... is true |
7656
| 44 | z >= 1 when ... > ... is true |
7757
| 45 | 0 < y+0 when ... > ... is true |
7858
| 45 | 0 >= y+0 when ... > ... is false |
79-
| 45 | ... > ... != 0 when ... > ... is true |
80-
| 45 | ... > ... == 0 when ... > ... is false |
8159
| 45 | y < 0+1 when ... > ... is false |
8260
| 45 | y < 1 when ... > ... is false |
8361
| 45 | y >= 0+1 when ... > ... is true |
@@ -88,12 +66,6 @@
8866
| 58 | 0 < y+1 when ... \|\| ... is false |
8967
| 58 | 0 == x+0 when ... == ... is true |
9068
| 58 | 0 >= y+1 when ... < ... is true |
91-
| 58 | ... < ... != 0 when ... < ... is true |
92-
| 58 | ... < ... == 0 when ... < ... is false |
93-
| 58 | ... < ... == 0 when ... \|\| ... is false |
94-
| 58 | ... == ... != 0 when ... == ... is true |
95-
| 58 | ... == ... == 0 when ... == ... is false |
96-
| 58 | ... == ... == 0 when ... \|\| ... is false |
9769
| 58 | x != 0 when ... == ... is false |
9870
| 58 | x != 0 when ... \|\| ... is false |
9971
| 58 | x != 0+0 when ... == ... is false |
@@ -115,8 +87,6 @@
11587
| 74 | i >= 11 when i is Case[11..20] |
11688
| 75 | 0 != x+0 when ... == ... is false |
11789
| 75 | 0 == x+0 when ... == ... is true |
118-
| 75 | ... == ... != 0 when ... == ... is true |
119-
| 75 | ... == ... == 0 when ... == ... is false |
12090
| 75 | x != 0 when ... == ... is false |
12191
| 75 | x != 0+0 when ... == ... is false |
12292
| 75 | x == 0 when ... == ... is true |
@@ -127,12 +97,6 @@
12797
| 85 | 0 == x+0 when ... && ... is true |
12898
| 85 | 0 == x+0 when ... == ... is true |
12999
| 85 | 0 == y+0 when ... != ... is false |
130-
| 85 | ... != ... != 0 when ... != ... is true |
131-
| 85 | ... != ... != 0 when ... && ... is true |
132-
| 85 | ... != ... == 0 when ... != ... is false |
133-
| 85 | ... == ... != 0 when ... && ... is true |
134-
| 85 | ... == ... != 0 when ... == ... is true |
135-
| 85 | ... == ... == 0 when ... == ... is false |
136100
| 85 | x != 0 when ... == ... is false |
137101
| 85 | x != 0+0 when ... == ... is false |
138102
| 85 | x == 0 when ... && ... is true |
@@ -149,26 +113,18 @@
149113
| 93 | c == 0 when c is false |
150114
| 94 | 0 != x+0 when ... != ... is true |
151115
| 94 | 0 == x+0 when ... != ... is false |
152-
| 94 | ... != ... != 0 when ... != ... is true |
153-
| 94 | ... != ... == 0 when ... != ... is false |
154116
| 94 | x != 0 when ... != ... is true |
155117
| 94 | x != 0+0 when ... != ... is true |
156118
| 94 | x == 0 when ... != ... is false |
157119
| 94 | x == 0+0 when ... != ... is false |
158-
| 99 | f != 0 when f is true |
159-
| 99 | f == 0 when f is false |
160120
| 102 | 10 < j+1 when ... < ... is false |
161121
| 102 | 10 >= j+1 when ... < ... is true |
162-
| 102 | ... < ... != 0 when ... < ... is true |
163-
| 102 | ... < ... == 0 when ... < ... is false |
164122
| 102 | j < 10 when ... < ... is true |
165123
| 102 | j < 10+0 when ... < ... is true |
166124
| 102 | j >= 10 when ... < ... is false |
167125
| 102 | j >= 10+0 when ... < ... is false |
168126
| 105 | 0.0 != f+0 when ... != ... is true |
169127
| 105 | 0.0 == f+0 when ... != ... is false |
170-
| 105 | ... != ... != 0 when ... != ... is true |
171-
| 105 | ... != ... == 0 when ... != ... is false |
172128
| 105 | f != 0.0+0 when ... != ... is true |
173129
| 105 | f == 0.0+0 when ... != ... is false |
174130
| 109 | 0 != x+0 when ... == ... is false |
@@ -177,12 +133,6 @@
177133
| 109 | 0 < y+1 when ... \|\| ... is false |
178134
| 109 | 0 == x+0 when ... == ... is true |
179135
| 109 | 0 >= y+1 when ... < ... is true |
180-
| 109 | ... < ... != 0 when ... < ... is true |
181-
| 109 | ... < ... == 0 when ... < ... is false |
182-
| 109 | ... < ... == 0 when ... \|\| ... is false |
183-
| 109 | ... == ... != 0 when ... == ... is true |
184-
| 109 | ... == ... == 0 when ... == ... is false |
185-
| 109 | ... == ... == 0 when ... \|\| ... is false |
186136
| 109 | x != 0 when ... == ... is false |
187137
| 109 | x != 0 when ... \|\| ... is false |
188138
| 109 | x != 0+0 when ... == ... is false |
@@ -197,21 +147,10 @@
197147
| 109 | y >= 0+0 when ... \|\| ... is false |
198148
| 111 | 0.0 != i+0 when ... != ... is true |
199149
| 111 | 0.0 == i+0 when ... != ... is false |
200-
| 111 | ... != ... != 0 when ... != ... is true |
201-
| 111 | ... != ... == 0 when ... != ... is false |
202150
| 111 | i != 0.0+0 when ... != ... is true |
203151
| 111 | i == 0.0+0 when ... != ... is false |
204-
| 122 | b != 0 when b is true |
205-
| 122 | b == 0 when b is false |
206-
| 125 | ! ... != 0 when ! ... is true |
207-
| 125 | ! ... == 0 when ! ... is false |
208-
| 125 | call to safe != 0 when ! ... is false |
209-
| 125 | call to safe != 0 when call to safe is true |
210-
| 125 | call to safe == 0 when call to safe is false |
211152
| 131 | ... + ... != a+0 when call to __builtin_expect is false |
212153
| 131 | ... + ... == a+0 when call to __builtin_expect is true |
213-
| 131 | ... == ... != 0 when call to __builtin_expect is true |
214-
| 131 | ... == ... == 0 when call to __builtin_expect is false |
215154
| 131 | a != ... + ...+0 when call to __builtin_expect is false |
216155
| 131 | a != b+42 when call to __builtin_expect is false |
217156
| 131 | a == ... + ...+0 when call to __builtin_expect is true |
@@ -220,8 +159,6 @@
220159
| 131 | b == a+-42 when call to __builtin_expect is true |
221160
| 131 | call to __builtin_expect != 0 when call to __builtin_expect is true |
222161
| 131 | call to __builtin_expect == 0 when call to __builtin_expect is false |
223-
| 135 | ... != ... != 0 when call to __builtin_expect is true |
224-
| 135 | ... != ... == 0 when call to __builtin_expect is false |
225162
| 135 | ... + ... != a+0 when call to __builtin_expect is true |
226163
| 135 | ... + ... == a+0 when call to __builtin_expect is false |
227164
| 135 | a != ... + ...+0 when call to __builtin_expect is true |
@@ -234,8 +171,6 @@
234171
| 135 | call to __builtin_expect == 0 when call to __builtin_expect is false |
235172
| 141 | 42 != a+0 when call to __builtin_expect is false |
236173
| 141 | 42 == a+0 when call to __builtin_expect is true |
237-
| 141 | ... == ... != 0 when call to __builtin_expect is true |
238-
| 141 | ... == ... == 0 when call to __builtin_expect is false |
239174
| 141 | a != 42 when call to __builtin_expect is false |
240175
| 141 | a != 42+0 when call to __builtin_expect is false |
241176
| 141 | a == 42 when call to __builtin_expect is true |
@@ -244,23 +179,15 @@
244179
| 141 | call to __builtin_expect == 0 when call to __builtin_expect is false |
245180
| 145 | 42 != a+0 when call to __builtin_expect is true |
246181
| 145 | 42 == a+0 when call to __builtin_expect is false |
247-
| 145 | ... != ... != 0 when call to __builtin_expect is true |
248-
| 145 | ... != ... == 0 when call to __builtin_expect is false |
249182
| 145 | a != 42 when call to __builtin_expect is true |
250183
| 145 | a != 42+0 when call to __builtin_expect is true |
251184
| 145 | a == 42 when call to __builtin_expect is false |
252185
| 145 | a == 42+0 when call to __builtin_expect is false |
253186
| 145 | call to __builtin_expect != 0 when call to __builtin_expect is true |
254187
| 145 | call to __builtin_expect == 0 when call to __builtin_expect is false |
255-
| 146 | ! ... != 0 when ! ... is true |
256-
| 146 | ! ... == 0 when ! ... is false |
257188
| 146 | x != 0 when ! ... is false |
258189
| 146 | x == 0 when ! ... is true |
259-
| 158 | ! ... != 0 when ! ... is true |
260-
| 158 | ! ... == 0 when ! ... is false |
261190
| 158 | p != 0 when ! ... is false |
262191
| 158 | p == 0 when ! ... is true |
263-
| 170 | ! ... != 0 when ! ... is true |
264-
| 170 | ! ... == 0 when ! ... is false |
265192
| 170 | s != 0 when ! ... is false |
266193
| 170 | s == 0 when ! ... is true |

0 commit comments

Comments
 (0)