Skip to content

Commit 5a56a47

Browse files
committed
C++: Make 'getInstructionConvertedResultExpression' equivalent in C and C++.
1 parent e86d2f8 commit 5a56a47

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 |
@@ -142,8 +151,24 @@ astGuardsCompare
142151
| 109 | y < 0+0 when ... < ... is true |
143152
| 109 | y >= 0+0 when ... < ... is false |
144153
| 109 | y >= 0+0 when ... \|\| ... is false |
154+
| 126 | 1 != 0 when 1 is true |
155+
| 126 | 1 != 0 when ... && ... is true |
156+
| 126 | 1 == 0 when 1 is false |
157+
| 126 | call to test3_condition != 0 when ... && ... is true |
158+
| 126 | call to test3_condition != 0 when call to test3_condition is true |
159+
| 126 | call to test3_condition == 0 when call to test3_condition is false |
160+
| 131 | b != 0 when b is true |
161+
| 131 | b == 0 when b is false |
162+
| 137 | 0 != 0 when 0 is true |
163+
| 137 | 0 == 0 when 0 is false |
145164
| 146 | x != 0 when ! ... is false |
146165
| 146 | x == 0 when ! ... is true |
166+
| 152 | x != 0 when ... && ... is true |
167+
| 152 | x != 0 when x is true |
168+
| 152 | x == 0 when x is false |
169+
| 152 | y != 0 when ... && ... is true |
170+
| 152 | y != 0 when y is true |
171+
| 152 | y == 0 when y is false |
147172
| 156 | ... + ... != x+0 when ... == ... is false |
148173
| 156 | ... + ... == x+0 when ... == ... is true |
149174
| 156 | x != ... + ...+0 when ... == ... is false |
@@ -182,6 +207,8 @@ astGuardsCompare
182207
| 175 | call to foo != 0+0 when ... == ... is false |
183208
| 175 | call to foo == 0 when ... == ... is true |
184209
| 175 | call to foo == 0+0 when ... == ... is true |
210+
| 181 | x != 0 when x is true |
211+
| 181 | x == 0 when x is false |
185212
astGuardsControl
186213
| test.c:7:9:7:13 | ... > ... | false | 10 | 11 |
187214
| test.c:7:9:7:13 | ... > ... | true | 7 | 9 |
@@ -253,13 +280,29 @@ astGuardsControl
253280
| test.c:109:9:109:14 | ... == ... | false | 113 | 113 |
254281
| test.c:109:9:109:23 | ... \|\| ... | false | 113 | 113 |
255282
| test.c:109:19:109:23 | ... < ... | false | 113 | 113 |
283+
| test.c:126:7:126:7 | 1 | true | 126 | 126 |
284+
| test.c:126:7:126:7 | 1 | true | 126 | 128 |
285+
| test.c:126:7:126:7 | 1 | true | 131 | 131 |
286+
| test.c:126:7:126:7 | 1 | true | 131 | 132 |
287+
| test.c:126:7:126:7 | 1 | true | 134 | 123 |
288+
| test.c:126:7:126:28 | ... && ... | true | 126 | 128 |
289+
| test.c:126:12:126:26 | call to test3_condition | true | 126 | 128 |
290+
| test.c:131:7:131:7 | b | true | 131 | 132 |
291+
| test.c:137:7:137:7 | 0 | false | 142 | 136 |
256292
| test.c:146:7:146:8 | ! ... | true | 146 | 147 |
293+
| test.c:152:10:152:10 | x | true | 151 | 152 |
294+
| test.c:152:10:152:10 | x | true | 152 | 152 |
295+
| test.c:152:10:152:15 | ... && ... | true | 151 | 152 |
296+
| test.c:152:15:152:15 | y | true | 151 | 152 |
257297
| test.c:156:9:156:19 | ... == ... | true | 156 | 157 |
258298
| test.c:159:9:159:19 | ... == ... | true | 159 | 160 |
259299
| test.c:162:9:162:18 | ... < ... | true | 162 | 163 |
260300
| test.c:165:9:165:18 | ... < ... | true | 165 | 166 |
261301
| test.c:175:13:175:32 | ... == ... | false | 175 | 175 |
262302
| test.c:175:13:175:32 | ... == ... | true | 175 | 175 |
303+
| test.c:181:9:181:9 | x | false | 183 | 184 |
304+
| test.c:181:9:181:9 | x | true | 181 | 182 |
305+
| test.c:181:9:181:9 | x | true | 186 | 180 |
263306
| test.cpp:18:8:18:10 | call to get | true | 19 | 19 |
264307
| test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 |
265308
| test.cpp:31:7:31:13 | ... == ... | false | 34 | 34 |
@@ -481,9 +524,27 @@ astGuardsEnsure_const
481524
| test.c:109:9:109:14 | ... == ... | test.c:109:9:109:9 | x | != | 0 | 109 | 109 |
482525
| test.c:109:9:109:14 | ... == ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 |
483526
| test.c:109:9:109:23 | ... \|\| ... | test.c:109:9:109:9 | x | != | 0 | 113 | 113 |
527+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 126 |
528+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
529+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 131 |
530+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 131 | 132 |
531+
| test.c:126:7:126:7 | 1 | test.c:126:7:126:7 | 1 | != | 0 | 134 | 123 |
532+
| test.c:126:7:126:28 | ... && ... | test.c:126:7:126:7 | 1 | != | 0 | 126 | 128 |
533+
| test.c:126:7:126:28 | ... && ... | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
534+
| test.c:126:12:126:26 | call to test3_condition | test.c:126:12:126:26 | call to test3_condition | != | 0 | 126 | 128 |
535+
| test.c:131:7:131:7 | b | test.c:131:7:131:7 | b | != | 0 | 131 | 132 |
536+
| test.c:137:7:137:7 | 0 | test.c:137:7:137:7 | 0 | == | 0 | 142 | 136 |
484537
| test.c:146:7:146:8 | ! ... | test.c:146:8:146:8 | x | == | 0 | 146 | 147 |
538+
| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 151 | 152 |
539+
| test.c:152:10:152:10 | x | test.c:152:10:152:10 | x | != | 0 | 152 | 152 |
540+
| test.c:152:10:152:15 | ... && ... | test.c:152:10:152:10 | x | != | 0 | 151 | 152 |
541+
| test.c:152:10:152:15 | ... && ... | test.c:152:15:152:15 | y | != | 0 | 151 | 152 |
542+
| test.c:152:15:152:15 | y | test.c:152:15:152:15 | y | != | 0 | 151 | 152 |
485543
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | != | 0 | 175 | 175 |
486544
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | == | 0 | 175 | 175 |
545+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 181 | 182 |
546+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | != | 0 | 186 | 180 |
547+
| test.c:181:9:181:9 | x | test.c:181:9:181:9 | x | == | 0 | 183 | 184 |
487548
| test.cpp:18:8:18:10 | call to get | test.cpp:18:8:18:10 | call to get | != | 0 | 19 | 19 |
488549
| test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | -1 | 30 | 30 |
489550
| 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
@@ -161,13 +161,21 @@
161161
| 125 | ! ... != 1 when ! ... is false |
162162
| 125 | ! ... == 0 when ! ... is false |
163163
| 125 | ! ... == 1 when ! ... is true |
164+
| 126 | 1 != 0 when 1 is true |
165+
| 126 | 1 != 0 when ... && ... is true |
166+
| 126 | 1 == 0 when 1 is false |
167+
| 126 | call to test3_condition != 0 when ... && ... is true |
168+
| 126 | call to test3_condition != 0 when call to test3_condition is true |
169+
| 126 | call to test3_condition == 0 when call to test3_condition is false |
164170
| 131 | ... + ... != a+0 when call to __builtin_expect is false |
165171
| 131 | ... + ... == a+0 when call to __builtin_expect is true |
166172
| 131 | a != ... + ...+0 when call to __builtin_expect is false |
167173
| 131 | a != b+42 when call to __builtin_expect is false |
168174
| 131 | a == ... + ...+0 when call to __builtin_expect is true |
169175
| 131 | a == b+42 when call to __builtin_expect is true |
176+
| 131 | b != 0 when b is true |
170177
| 131 | b != a+-42 when call to __builtin_expect is false |
178+
| 131 | b == 0 when b is false |
171179
| 131 | b == a+-42 when call to __builtin_expect is true |
172180
| 131 | call to __builtin_expect != 0 when call to __builtin_expect is true |
173181
| 131 | call to __builtin_expect == 0 when call to __builtin_expect is false |
@@ -181,6 +189,8 @@
181189
| 135 | b == a+-42 when call to __builtin_expect is false |
182190
| 135 | call to __builtin_expect != 0 when call to __builtin_expect is true |
183191
| 135 | call to __builtin_expect == 0 when call to __builtin_expect is false |
192+
| 137 | 0 != 0 when 0 is true |
193+
| 137 | 0 == 0 when 0 is false |
184194
| 141 | 42 != a+0 when call to __builtin_expect is false |
185195
| 141 | 42 == a+0 when call to __builtin_expect is true |
186196
| 141 | a != 42 when call to __builtin_expect is false |
@@ -199,7 +209,11 @@
199209
| 145 | call to __builtin_expect == 0 when call to __builtin_expect is false |
200210
| 146 | x != 0 when ! ... is false |
201211
| 146 | x == 0 when ! ... is true |
212+
| 152 | p != 0 when p is true |
213+
| 152 | p == 0 when p is false |
202214
| 158 | p != 0 when ! ... is false |
203215
| 158 | p == 0 when ! ... is true |
216+
| 164 | s != 0 when s is true |
217+
| 164 | s == 0 when s is false |
204218
| 170 | s != 0 when ! ... is false |
205219
| 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)