Skip to content

Commit c9b478b

Browse files
committed
C++: Make 'getInstructionConvertedResultExpression' equivalent in C and C++.
1 parent 93e78f5 commit c9b478b

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

Diff for: cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

+14
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ module Raw {
171171
// forwarded the result of another translated expression.
172172
instruction = translatedExpr.getInstruction(_)
173173
)
174+
or
175+
// Consider the snippet `if(x) { ... }` where `x` is an integer.
176+
// In C++ there is a `BoolConversion` conversion on `x` which generates a
177+
// `CompareNEInstruction` whose `getInstructionUnconvertedResultExpression`
178+
// is the `BoolConversion`. Thus, calling `getInstructionConvertedResultExpression` on
179+
// the `CompareNEInstruction` gives `x` in C++ code.
180+
// However, in C there is no such conversion to return. So instead we have
181+
// to map the result of `getInstructionConvertedResultExpression` on
182+
// the `CompareNEInstruction` to `x` manually.
183+
exists(TranslatedValueCondition translatedValueCondition |
184+
translatedValueCondition = getTranslatedCondition(result) and
185+
translatedValueCondition.shouldGenerateCompareNE() and
186+
instruction = translatedValueCondition.getInstruction(ValueConditionCompareTag())
187+
)
174188
}
175189

176190
cached

Diff for: cpp/ql/test/library-tests/controlflow/guards-ir/tests.expected

+61
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,21 @@ astGuards
2020
| test.c:109:9:109:14 | ... == ... |
2121
| test.c:109:9:109:23 | ... \|\| ... |
2222
| test.c:109:19:109:23 | ... < ... |
23+
| test.c:126:7:126:7 | 1 |
24+
| test.c:126:7:126:28 | ... && ... |
25+
| test.c:126:12:126:26 | call to test3_condition |
26+
| test.c:131:7:131:7 | b |
27+
| test.c:137:7:137:7 | 0 |
2328
| test.c:146:7:146:8 | ! ... |
29+
| test.c:152:10:152:10 | x |
30+
| test.c:152:10:152:15 | ... && ... |
31+
| test.c:152:15:152:15 | y |
2432
| test.c:156:9:156:19 | ... == ... |
2533
| test.c:159:9:159:19 | ... == ... |
2634
| test.c:162:9:162:18 | ... < ... |
2735
| test.c:165:9:165:18 | ... < ... |
2836
| test.c:175:13:175:32 | ... == ... |
37+
| test.c:181:9:181:9 | x |
2938
| test.cpp:18:8:18:10 | call to get |
3039
| test.cpp:31:7:31:13 | ... == ... |
3140
| test.cpp:42:13:42:20 | call to getABool |
@@ -138,8 +147,24 @@ astGuardsCompare
138147
| 109 | y < 0+0 when ... < ... is true |
139148
| 109 | y >= 0+0 when ... < ... is false |
140149
| 109 | y >= 0+0 when ... \|\| ... is false |
150+
| 126 | 1 != 0 when 1 is true |
151+
| 126 | 1 != 0 when ... && ... is true |
152+
| 126 | 1 == 0 when 1 is false |
153+
| 126 | call to test3_condition != 0 when ... && ... is true |
154+
| 126 | call to test3_condition != 0 when call to test3_condition is true |
155+
| 126 | call to test3_condition == 0 when call to test3_condition is false |
156+
| 131 | b != 0 when b is true |
157+
| 131 | b == 0 when b is false |
158+
| 137 | 0 != 0 when 0 is true |
159+
| 137 | 0 == 0 when 0 is false |
141160
| 146 | x != 0 when ! ... is false |
142161
| 146 | x == 0 when ! ... is true |
162+
| 152 | x != 0 when ... && ... is true |
163+
| 152 | x != 0 when x is true |
164+
| 152 | x == 0 when x is false |
165+
| 152 | y != 0 when ... && ... is true |
166+
| 152 | y != 0 when y is true |
167+
| 152 | y == 0 when y is false |
143168
| 156 | ... + ... != x+0 when ... == ... is false |
144169
| 156 | ... + ... == x+0 when ... == ... is true |
145170
| 156 | x != ... + ...+0 when ... == ... is false |
@@ -178,6 +203,8 @@ astGuardsCompare
178203
| 175 | call to foo != 0+0 when ... == ... is false |
179204
| 175 | call to foo == 0 when ... == ... is true |
180205
| 175 | call to foo == 0+0 when ... == ... is true |
206+
| 181 | x != 0 when x is true |
207+
| 181 | x == 0 when x is false |
181208
astGuardsControl
182209
| test.c:7:9:7:13 | ... > ... | false | 10 | 11 |
183210
| test.c:7:9:7:13 | ... > ... | true | 7 | 9 |
@@ -249,13 +276,29 @@ astGuardsControl
249276
| test.c:109:9:109:14 | ... == ... | false | 113 | 113 |
250277
| test.c:109:9:109:23 | ... \|\| ... | false | 113 | 113 |
251278
| test.c:109:19:109:23 | ... < ... | false | 113 | 113 |
279+
| test.c:126:7:126:7 | 1 | true | 126 | 126 |
280+
| test.c:126:7:126:7 | 1 | true | 126 | 128 |
281+
| test.c:126:7:126:7 | 1 | true | 131 | 131 |
282+
| test.c:126:7:126:7 | 1 | true | 131 | 132 |
283+
| test.c:126:7:126:7 | 1 | true | 134 | 123 |
284+
| test.c:126:7:126:28 | ... && ... | true | 126 | 128 |
285+
| test.c:126:12:126:26 | call to test3_condition | true | 126 | 128 |
286+
| test.c:131:7:131:7 | b | true | 131 | 132 |
287+
| test.c:137:7:137:7 | 0 | false | 142 | 136 |
252288
| test.c:146:7:146:8 | ! ... | true | 146 | 147 |
289+
| test.c:152:10:152:10 | x | true | 151 | 152 |
290+
| test.c:152:10:152:10 | x | true | 152 | 152 |
291+
| test.c:152:10:152:15 | ... && ... | true | 151 | 152 |
292+
| test.c:152:15:152:15 | y | true | 151 | 152 |
253293
| test.c:156:9:156:19 | ... == ... | true | 156 | 157 |
254294
| test.c:159:9:159:19 | ... == ... | true | 159 | 160 |
255295
| test.c:162:9:162:18 | ... < ... | true | 162 | 163 |
256296
| test.c:165:9:165:18 | ... < ... | true | 165 | 166 |
257297
| test.c:175:13:175:32 | ... == ... | false | 175 | 175 |
258298
| test.c:175:13:175:32 | ... == ... | true | 175 | 175 |
299+
| test.c:181:9:181:9 | x | false | 183 | 184 |
300+
| test.c:181:9:181:9 | x | true | 181 | 182 |
301+
| test.c:181:9:181:9 | x | true | 186 | 180 |
259302
| test.cpp:18:8:18:10 | call to get | true | 19 | 19 |
260303
| test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 |
261304
| test.cpp:31:7:31:13 | ... == ... | false | 34 | 34 |
@@ -477,9 +520,27 @@ astGuardsEnsure_const
477520
| test.c:109:9:109:14 | ... == ... | test.c:109:9:109:9 | x | != | 0 | 109 | 109 |
478521
| test.c:109:9:109:14 | ... == ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 |
479522
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 |
523+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 126 |
524+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
525+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 131 |
526+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 132 |
527+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 134 | 123 |
528+
| test.c:126:7:126:28 | ... && ... | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
529+
| test.c:126:7:126:28 | ... && ... | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
530+
| test.c:126:12:126:26 | call to test3_condition | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
531+
| test.c:131:7:131:7 | b | test.c:131:7:131:7 | b | != | 0 | 131 | 132 |
532+
| test.c:137:7:137:7 | 0 | test.c:137:7:137:7 | 0 | == | 0 | 142 | 136 |
480533
| test.c:146:7:146:8 | ! ... | test.c:146:8:146:8 | x | == | 0 | 146 | 147 |
534+
| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 151 | 152 |
535+
| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 152 | 152 |
536+
| test.c:152:10:152:15 | ... && ... | test.c:152:10:152:10 | x | != | 0 | 151 | 152 |
537+
| test.c:152:10:152:15 | ... && ... | test.c:152:15:152:15 | y | != | 0 | 151 | 152 |
538+
| test.c:152:15:152:15 | y | test.c:152:15:152:15 | y | != | 0 | 151 | 152 |
481539
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | != | 0 | 175 | 175 |
482540
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | == | 0 | 175 | 175 |
541+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 181 | 182 |
542+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 186 | 180 |
543+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | == | 0 | 183 | 184 |
483544
| test.cpp:18:8:18:10 | call to get | test.cpp:18:8:18:10 | call to get | != | 0 | 19 | 19 |
484545
| test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 30 | 30 |
485546
| test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 34 | 34 |

Diff for: cpp/ql/test/library-tests/controlflow/guards/Guards.expected

+7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
| test.c:109:9:109:14 | ... == ... |
2020
| test.c:109:9:109:23 | ... \|\| ... |
2121
| test.c:109:19:109:23 | ... < ... |
22+
| test.c:126:7:126:7 | 1 |
23+
| test.c:126:7:126:28 | ... && ... |
24+
| test.c:126:12:126:26 | call to test3_condition |
25+
| test.c:131:7:131:7 | b |
26+
| test.c:137:7:137:7 | 0 |
2227
| test.c:146:7:146:8 | ! ... |
28+
| test.c:152:8:152:8 | p |
2329
| test.c:158:8:158:9 | ! ... |
30+
| test.c:164:8:164:8 | s |
2431
| test.c:170:8:170:9 | ! ... |
2532
| test.cpp:18:8:18:10 | call to get |
2633
| test.cpp:31:7:31:13 | ... == ... |

Diff for: cpp/ql/test/library-tests/controlflow/guards/GuardsCompare.expected

+14
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,21 @@
149149
| 111 | 0.0 == i+0 when ... != ... is false |
150150
| 111 | i != 0.0+0 when ... != ... is true |
151151
| 111 | i == 0.0+0 when ... != ... is false |
152+
| 126 | 1 != 0 when 1 is true |
153+
| 126 | 1 != 0 when ... && ... is true |
154+
| 126 | 1 == 0 when 1 is false |
155+
| 126 | call to test3_condition != 0 when ... && ... is true |
156+
| 126 | call to test3_condition != 0 when call to test3_condition is true |
157+
| 126 | call to test3_condition == 0 when call to test3_condition is false |
152158
| 131 | ... + ... != a+0 when call to __builtin_expect is false |
153159
| 131 | ... + ... == a+0 when call to __builtin_expect is true |
154160
| 131 | a != ... + ...+0 when call to __builtin_expect is false |
155161
| 131 | a != b+42 when call to __builtin_expect is false |
156162
| 131 | a == ... + ...+0 when call to __builtin_expect is true |
157163
| 131 | a == b+42 when call to __builtin_expect is true |
164+
| 131 | b != 0 when b is true |
158165
| 131 | b != a+-42 when call to __builtin_expect is false |
166+
| 131 | b == 0 when b is false |
159167
| 131 | b == a+-42 when call to __builtin_expect is true |
160168
| 131 | call to __builtin_expect != 0 when call to __builtin_expect is true |
161169
| 131 | call to __builtin_expect == 0 when call to __builtin_expect is false |
@@ -169,6 +177,8 @@
169177
| 135 | b == a+-42 when call to __builtin_expect is false |
170178
| 135 | call to __builtin_expect != 0 when call to __builtin_expect is true |
171179
| 135 | call to __builtin_expect == 0 when call to __builtin_expect is false |
180+
| 137 | 0 != 0 when 0 is true |
181+
| 137 | 0 == 0 when 0 is false |
172182
| 141 | 42 != a+0 when call to __builtin_expect is false |
173183
| 141 | 42 == a+0 when call to __builtin_expect is true |
174184
| 141 | a != 42 when call to __builtin_expect is false |
@@ -187,7 +197,11 @@
187197
| 145 | call to __builtin_expect == 0 when call to __builtin_expect is false |
188198
| 146 | x != 0 when ! ... is false |
189199
| 146 | x == 0 when ! ... is true |
200+
| 152 | p != 0 when p is true |
201+
| 152 | p == 0 when p is false |
190202
| 158 | p != 0 when ! ... is false |
191203
| 158 | p == 0 when ! ... is true |
204+
| 164 | s != 0 when s is true |
205+
| 164 | s == 0 when s is false |
192206
| 170 | s != 0 when ! ... is false |
193207
| 170 | s == 0 when ! ... is true |

Diff for: cpp/ql/test/library-tests/controlflow/guards/GuardsControl.expected

+11
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,19 @@
6868
| test.c:109:9:109:14 | ... == ... | false | 113 | 113 |
6969
| test.c:109:9:109:23 | ... \|\| ... | false | 113 | 113 |
7070
| test.c:109:19:109:23 | ... < ... | false | 113 | 113 |
71+
| test.c:126:7:126:7 | 1 | true | 126 | 126 |
72+
| test.c:126:7:126:7 | 1 | true | 126 | 128 |
73+
| test.c:126:7:126:7 | 1 | true | 131 | 131 |
74+
| test.c:126:7:126:7 | 1 | true | 131 | 132 |
75+
| test.c:126:7:126:7 | 1 | true | 134 | 123 |
76+
| test.c:126:7:126:28 | ... && ... | true | 126 | 128 |
77+
| test.c:126:12:126:26 | call to test3_condition | true | 126 | 128 |
78+
| test.c:131:7:131:7 | b | true | 131 | 132 |
79+
| test.c:137:7:137:7 | 0 | false | 142 | 136 |
7180
| test.c:146:7:146:8 | ! ... | true | 146 | 147 |
81+
| test.c:152:8:152:8 | p | true | 152 | 154 |
7282
| test.c:158:8:158:9 | ! ... | true | 158 | 160 |
83+
| test.c:164:8:164:8 | s | true | 164 | 166 |
7384
| test.c:170:8:170:9 | ! ... | true | 170 | 172 |
7485
| test.cpp:18:8:18:10 | call to get | true | 19 | 19 |
7586
| test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 |

Diff for: cpp/ql/test/library-tests/controlflow/guards/GuardsEnsure.expected

+12
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,20 @@ unary
261261
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 |
262262
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:19:109:19 | y | >= | 0 | 113 | 113 |
263263
| test.c:109:19:109:23 | ... < ... | test.c:109:19:109:19 | y | >= | 0 | 113 | 113 |
264+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 126 |
265+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
266+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 131 |
267+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 132 |
268+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 134 | 123 |
269+
| test.c:126:7:126:28 | ... && ... | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
270+
| test.c:126:7:126:28 | ... && ... | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
271+
| test.c:126:12:126:26 | call to test3_condition | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
272+
| test.c:131:7:131:7 | b | test.c:131:7:131:7 | b | != | 0 | 131 | 132 |
273+
| test.c:137:7:137:7 | 0 | test.c:137:7:137:7 | 0 | == | 0 | 142 | 136 |
264274
| test.c:146:7:146:8 | ! ... | test.c:146:8:146:8 | x | == | 0 | 146 | 147 |
275+
| test.c:152:8:152:8 | p | test.c:152:8:152:8 | p | != | 0 | 152 | 154 |
265276
| test.c:158:8:158:9 | ! ... | test.c:158:9:158:9 | p | == | 0 | 158 | 160 |
277+
| test.c:164:8:164:8 | s | test.c:164:8:164:8 | s | != | 0 | 164 | 166 |
266278
| test.c:170:8:170:9 | ! ... | test.c:170:9:170:9 | s | == | 0 | 170 | 172 |
267279
| test.cpp:18:8:18:10 | call to get | test.cpp:18:8:18:10 | call to get | != | 0 | 19 | 19 |
268280
| test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 30 | 30 |

0 commit comments

Comments
 (0)