Skip to content

Commit 9d6deb2

Browse files
committed
C++: Make 'getInstructionConvertedResultExpression' equivalent in C and C++.
1 parent 6333f8f commit 9d6deb2

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-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

+70
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 |
@@ -184,10 +193,29 @@ astGuardsCompare
184193
| 109 | y < 0+0 when ... < ... is true |
185194
| 109 | y >= 0+0 when ... < ... is false |
186195
| 109 | y >= 0+0 when ... \|\| ... is false |
196+
<<<<<<< HEAD
187197
| 146 | ! ... != 0 when ! ... is true |
188198
| 146 | ! ... == 0 when ! ... is false |
199+
=======
200+
| 126 | 1 != 0 when 1 is true |
201+
| 126 | 1 != 0 when ... && ... is true |
202+
| 126 | 1 == 0 when 1 is false |
203+
| 126 | call to test3_condition != 0 when ... && ... is true |
204+
| 126 | call to test3_condition != 0 when call to test3_condition is true |
205+
| 126 | call to test3_condition == 0 when call to test3_condition is false |
206+
| 131 | b != 0 when b is true |
207+
| 131 | b == 0 when b is false |
208+
| 137 | 0 != 0 when 0 is true |
209+
| 137 | 0 == 0 when 0 is false |
210+
>>>>>>> c9b478b6b7 (C++: Make 'getInstructionConvertedResultExpression' equivalent in C and C++.)
189211
| 146 | x != 0 when ! ... is false |
190212
| 146 | x == 0 when ! ... is true |
213+
| 152 | x != 0 when ... && ... is true |
214+
| 152 | x != 0 when x is true |
215+
| 152 | x == 0 when x is false |
216+
| 152 | y != 0 when ... && ... is true |
217+
| 152 | y != 0 when y is true |
218+
| 152 | y == 0 when y is false |
191219
| 156 | ... + ... != x+0 when ... == ... is false |
192220
| 156 | ... + ... == x+0 when ... == ... is true |
193221
| 156 | ... == ... != 0 when ... == ... is true |
@@ -236,6 +264,8 @@ astGuardsCompare
236264
| 175 | call to foo != 0+0 when ... == ... is false |
237265
| 175 | call to foo == 0 when ... == ... is true |
238266
| 175 | call to foo == 0+0 when ... == ... is true |
267+
| 181 | x != 0 when x is true |
268+
| 181 | x == 0 when x is false |
239269
astGuardsControl
240270
| test.c:7:9:7:13 | ... > ... | false | 10 | 11 |
241271
| test.c:7:9:7:13 | ... > ... | true | 7 | 9 |
@@ -307,13 +337,29 @@ astGuardsControl
307337
| test.c:109:9:109:14 | ... == ... | false | 113 | 113 |
308338
| test.c:109:9:109:23 | ... \|\| ... | false | 113 | 113 |
309339
| test.c:109:19:109:23 | ... < ... | false | 113 | 113 |
340+
| test.c:126:7:126:7 | 1 | true | 126 | 126 |
341+
| test.c:126:7:126:7 | 1 | true | 126 | 128 |
342+
| test.c:126:7:126:7 | 1 | true | 131 | 131 |
343+
| test.c:126:7:126:7 | 1 | true | 131 | 132 |
344+
| test.c:126:7:126:7 | 1 | true | 134 | 123 |
345+
| test.c:126:7:126:28 | ... && ... | true | 126 | 128 |
346+
| test.c:126:12:126:26 | call to test3_condition | true | 126 | 128 |
347+
| test.c:131:7:131:7 | b | true | 131 | 132 |
348+
| test.c:137:7:137:7 | 0 | false | 142 | 136 |
310349
| test.c:146:7:146:8 | ! ... | true | 146 | 147 |
350+
| test.c:152:10:152:10 | x | true | 151 | 152 |
351+
| test.c:152:10:152:10 | x | true | 152 | 152 |
352+
| test.c:152:10:152:15 | ... && ... | true | 151 | 152 |
353+
| test.c:152:15:152:15 | y | true | 151 | 152 |
311354
| test.c:156:9:156:19 | ... == ... | true | 156 | 157 |
312355
| test.c:159:9:159:19 | ... == ... | true | 159 | 160 |
313356
| test.c:162:9:162:18 | ... < ... | true | 162 | 163 |
314357
| test.c:165:9:165:18 | ... < ... | true | 165 | 166 |
315358
| test.c:175:13:175:32 | ... == ... | false | 175 | 175 |
316359
| test.c:175:13:175:32 | ... == ... | true | 175 | 175 |
360+
| test.c:181:9:181:9 | x | false | 183 | 184 |
361+
| test.c:181:9:181:9 | x | true | 181 | 182 |
362+
| test.c:181:9:181:9 | x | true | 186 | 180 |
317363
| test.cpp:18:8:18:10 | call to get | true | 19 | 19 |
318364
| test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 |
319365
| test.cpp:31:7:31:13 | ... == ... | false | 34 | 34 |
@@ -611,6 +657,7 @@ astGuardsEnsure_const
611657
| test.c:109:9:109:14 | ... == ... | test.c:109:9:109:14 | ... == ... | == | 0 | 109 | 109 |
612658
| test.c:109:9:109:14 | ... == ... | test.c:109:9:109:14 | ... == ... | == | 0 | 113 | 113 |
613659
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 |
660+
<<<<<<< HEAD
614661
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:9:109:14 | ... == ... | == | 0 | 113 | 113 |
615662
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:19:109:23 | ... < ... | == | 0 | 113 | 113 |
616663
| test.c:109:19:109:23 | ... < ... | test.c:109:19:109:23 | ... < ... | == | 0 | 113 | 113 |
@@ -624,6 +671,29 @@ astGuardsEnsure_const
624671
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | == | 0 | 175 | 175 |
625672
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:32 | ... == ... | != | 0 | 175 | 175 |
626673
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:32 | ... == ... | == | 0 | 175 | 175 |
674+
=======
675+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 126 |
676+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
677+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 131 |
678+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 132 |
679+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 134 | 123 |
680+
| test.c:126:7:126:28 | ... && ... | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
681+
| test.c:126:7:126:28 | ... && ... | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
682+
| test.c:126:12:126:26 | call to test3_condition | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
683+
| test.c:131:7:131:7 | b | test.c:131:7:131:7 | b | != | 0 | 131 | 132 |
684+
| test.c:137:7:137:7 | 0 | test.c:137:7:137:7 | 0 | == | 0 | 142 | 136 |
685+
| test.c:146:7:146:8 | ! ... | test.c:146:8:146:8 | x | == | 0 | 146 | 147 |
686+
| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 151 | 152 |
687+
| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 152 | 152 |
688+
| test.c:152:10:152:15 | ... && ... | test.c:152:10:152:10 | x | != | 0 | 151 | 152 |
689+
| test.c:152:10:152:15 | ... && ... | test.c:152:15:152:15 | y | != | 0 | 151 | 152 |
690+
| test.c:152:15:152:15 | y | test.c:152:15:152:15 | y | != | 0 | 151 | 152 |
691+
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | != | 0 | 175 | 175 |
692+
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | == | 0 | 175 | 175 |
693+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 181 | 182 |
694+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 186 | 180 |
695+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | == | 0 | 183 | 184 |
696+
>>>>>>> c9b478b6b7 (C++: Make 'getInstructionConvertedResultExpression' equivalent in C and C++.)
627697
| test.cpp:18:8:18:10 | call to get | test.cpp:18:8:18:10 | call to get | != | 0 | 19 | 19 |
628698
| test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 30 | 30 |
629699
| 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
@@ -208,6 +208,12 @@
208208
| 125 | call to safe != 0 when ! ... is false |
209209
| 125 | call to safe != 0 when call to safe is true |
210210
| 125 | call to safe == 0 when call to safe is false |
211+
| 126 | 1 != 0 when 1 is true |
212+
| 126 | 1 != 0 when ... && ... is true |
213+
| 126 | 1 == 0 when 1 is false |
214+
| 126 | call to test3_condition != 0 when ... && ... is true |
215+
| 126 | call to test3_condition != 0 when call to test3_condition is true |
216+
| 126 | call to test3_condition == 0 when call to test3_condition is false |
211217
| 131 | ... + ... != a+0 when call to __builtin_expect is false |
212218
| 131 | ... + ... == a+0 when call to __builtin_expect is true |
213219
| 131 | ... == ... != 0 when call to __builtin_expect is true |
@@ -216,7 +222,9 @@
216222
| 131 | a != b+42 when call to __builtin_expect is false |
217223
| 131 | a == ... + ...+0 when call to __builtin_expect is true |
218224
| 131 | a == b+42 when call to __builtin_expect is true |
225+
| 131 | b != 0 when b is true |
219226
| 131 | b != a+-42 when call to __builtin_expect is false |
227+
| 131 | b == 0 when b is false |
220228
| 131 | b == a+-42 when call to __builtin_expect is true |
221229
| 131 | call to __builtin_expect != 0 when call to __builtin_expect is true |
222230
| 131 | call to __builtin_expect == 0 when call to __builtin_expect is false |
@@ -232,6 +240,8 @@
232240
| 135 | b == a+-42 when call to __builtin_expect is false |
233241
| 135 | call to __builtin_expect != 0 when call to __builtin_expect is true |
234242
| 135 | call to __builtin_expect == 0 when call to __builtin_expect is false |
243+
| 137 | 0 != 0 when 0 is true |
244+
| 137 | 0 == 0 when 0 is false |
235245
| 141 | 42 != a+0 when call to __builtin_expect is false |
236246
| 141 | 42 == a+0 when call to __builtin_expect is true |
237247
| 141 | ... == ... != 0 when call to __builtin_expect is true |
@@ -256,10 +266,14 @@
256266
| 146 | ! ... == 0 when ! ... is false |
257267
| 146 | x != 0 when ! ... is false |
258268
| 146 | x == 0 when ! ... is true |
269+
| 152 | p != 0 when p is true |
270+
| 152 | p == 0 when p is false |
259271
| 158 | ! ... != 0 when ! ... is true |
260272
| 158 | ! ... == 0 when ! ... is false |
261273
| 158 | p != 0 when ! ... is false |
262274
| 158 | p == 0 when ! ... is true |
275+
| 164 | s != 0 when s is true |
276+
| 164 | s == 0 when s is false |
263277
| 170 | ! ... != 0 when ! ... is true |
264278
| 170 | ! ... == 0 when ! ... is false |
265279
| 170 | s != 0 when ! ... is false |

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
@@ -340,10 +340,22 @@ unary
340340
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:19:109:23 | ... < ... | == | 0 | 113 | 113 |
341341
| test.c:109:19:109:23 | ... < ... | test.c:109:19:109:19 | y | >= | 0 | 113 | 113 |
342342
| test.c:109:19:109:23 | ... < ... | test.c:109:19:109:23 | ... < ... | == | 0 | 113 | 113 |
343+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 126 |
344+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
345+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 131 |
346+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 132 |
347+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 134 | 123 |
348+
| test.c:126:7:126:28 | ... && ... | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
349+
| test.c:126:7:126:28 | ... && ... | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
350+
| test.c:126:12:126:26 | call to test3_condition | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
351+
| test.c:131:7:131:7 | b | test.c:131:7:131:7 | b | != | 0 | 131 | 132 |
352+
| test.c:137:7:137:7 | 0 | test.c:137:7:137:7 | 0 | == | 0 | 142 | 136 |
343353
| test.c:146:7:146:8 | ! ... | test.c:146:7:146:8 | ! ... | != | 0 | 146 | 147 |
344354
| test.c:146:7:146:8 | ! ... | test.c:146:8:146:8 | x | == | 0 | 146 | 147 |
355+
| test.c:152:8:152:8 | p | test.c:152:8:152:8 | p | != | 0 | 152 | 154 |
345356
| test.c:158:8:158:9 | ! ... | test.c:158:8:158:9 | ! ... | != | 0 | 158 | 160 |
346357
| test.c:158:8:158:9 | ! ... | test.c:158:9:158:9 | p | == | 0 | 158 | 160 |
358+
| test.c:164:8:164:8 | s | test.c:164:8:164:8 | s | != | 0 | 164 | 166 |
347359
| test.c:170:8:170:9 | ! ... | test.c:170:8:170:9 | ! ... | != | 0 | 170 | 172 |
348360
| test.c:170:8:170:9 | ! ... | test.c:170:9:170:9 | s | == | 0 | 170 | 172 |
349361
| test.cpp:18:8:18:10 | call to get | test.cpp:18:8:18:10 | call to get | != | 0 | 19 | 19 |

0 commit comments

Comments
 (0)