@@ -17,6 +17,8 @@ Author: Diffblue Ltd.
17
17
#include < util/std_code.h>
18
18
#include < util/std_expr.h>
19
19
20
+ #include < functional>
21
+
20
22
recursive_initializationt::recursive_initializationt (
21
23
recursive_initialization_configt initialization_config,
22
24
goto_modelt &goto_model)
@@ -38,20 +40,27 @@ void recursive_initializationt::initialize(
38
40
}
39
41
else if (type.id () == ID_pointer)
40
42
{
41
- if (
42
- lhs.id () == ID_symbol &&
43
- should_be_treated_as_array (to_symbol_expr (lhs).get_identifier ()))
43
+ if (lhs.id () == ID_symbol)
44
44
{
45
- initialize_dynamic_array (lhs, depth, known_tags, body);
46
- }
47
- else
48
- {
49
- initialize_pointer (lhs, depth, known_tags, body);
45
+ auto const &lhs_symbol = to_symbol_expr (lhs);
46
+ if (should_be_treated_as_cstring (lhs_symbol.get_identifier ()))
47
+ {
48
+ initialize_cstring (lhs, depth, known_tags, body);
49
+ return ;
50
+ }
51
+ else if (should_be_treated_as_array (lhs_symbol.get_identifier ()))
52
+ {
53
+ initialize_dynamic_array (
54
+ lhs, depth, known_tags, body, default_array_member_initialization ());
55
+ return ;
56
+ }
50
57
}
58
+ initialize_pointer (lhs, depth, known_tags, body);
51
59
}
52
60
else if (type.id () == ID_array)
53
61
{
54
- initialize_array (lhs, depth, known_tags, body);
62
+ initialize_array (
63
+ lhs, depth, known_tags, body, default_array_member_initialization ());
55
64
}
56
65
else
57
66
{
@@ -163,19 +172,17 @@ void recursive_initializationt::initialize_array(
163
172
const exprt &array,
164
173
const std::size_t depth,
165
174
const recursion_sett &known_tags,
166
- code_blockt &body)
175
+ code_blockt &body,
176
+ array_convertert array_member_initialization)
167
177
{
168
178
PRECONDITION (array.type ().id () == ID_array);
169
179
const auto &array_type = to_array_type (array.type ());
170
180
const auto array_size =
171
181
numeric_cast_v<std::size_t >(to_constant_expr (array_type.size ()));
172
182
for (std::size_t index = 0 ; index < array_size; index++)
173
183
{
174
- initialize (
175
- index_exprt (array, from_integer (index, size_type ())),
176
- depth,
177
- known_tags,
178
- body);
184
+ array_member_initialization (
185
+ array, array_size, index, depth, known_tags, body);
179
186
}
180
187
}
181
188
@@ -202,11 +209,19 @@ optionalt<irep_idt> recursive_initializationt::get_associated_size_variable(
202
209
array_name);
203
210
}
204
211
212
+ bool recursive_initializationt::should_be_treated_as_cstring (
213
+ const irep_idt &pointer_name) const
214
+ {
215
+ return initialization_config.pointers_to_treat_as_cstrings .count (
216
+ pointer_name) != 0 ;
217
+ }
218
+
205
219
void recursive_initializationt::initialize_dynamic_array (
206
220
const exprt &pointer,
207
221
const std::size_t depth,
208
222
const recursion_sett &known_tags,
209
- code_blockt &body)
223
+ code_blockt &body,
224
+ array_convertert array_initialization)
210
225
{
211
226
PRECONDITION (pointer.type ().id () == ID_pointer);
212
227
@@ -269,7 +284,8 @@ void recursive_initializationt::initialize_dynamic_array(
269
284
std::size_t array_counter = 0 ;
270
285
for (const auto &array_variable : array_variables)
271
286
{
272
- initialize (array_variable, depth + 1 , known_tags, body);
287
+ initialize_array (
288
+ array_variable, depth + 1 , known_tags, body, array_initialization);
273
289
body.add (code_assignt{
274
290
index_exprt{arrays, from_integer (array_counter++, size_type ())},
275
291
address_of_exprt{
@@ -303,6 +319,16 @@ void recursive_initializationt::initialize_dynamic_array(
303
319
body.add (code_assignt{pointer, index_exprt{arrays, nondet_index}});
304
320
}
305
321
322
+ void recursive_initializationt::initialize_cstring (
323
+ const exprt &pointer,
324
+ std::size_t depth,
325
+ const recursion_sett &known_tags,
326
+ code_blockt &body)
327
+ {
328
+ initialize_dynamic_array (
329
+ pointer, depth, known_tags, body, cstring_member_initialization ());
330
+ }
331
+
306
332
std::string recursive_initialization_configt::to_string () const
307
333
{
308
334
std::ostringstream out{};
@@ -332,6 +358,57 @@ std::string recursive_initialization_configt::to_string() const
332
358
<< associated_array_size.second ;
333
359
}
334
360
out << " \n ]" ;
361
+ out << " \n pointers_to_treat_as_cstrings = [" ;
362
+ for (const auto &pointer_to_treat_as_string_name :
363
+ pointers_to_treat_as_cstrings)
364
+ {
365
+ out << " \n " << pointer_to_treat_as_string_name << std::endl;
366
+ }
367
+ out << " \n ]" ;
335
368
out << " \n }" ;
336
369
return out.str ();
337
370
}
371
+
372
+ recursive_initializationt::array_convertert
373
+ recursive_initializationt::default_array_member_initialization ()
374
+ {
375
+ return [this ](
376
+ const exprt &array,
377
+ const std::size_t length,
378
+ const std::size_t current_index,
379
+ const std::size_t depth,
380
+ const recursion_sett &known_tags,
381
+ code_blockt &body) {
382
+ PRECONDITION (array.type ().id () == ID_array);
383
+ initialize (
384
+ index_exprt{array, from_integer (current_index, size_type ())},
385
+ depth,
386
+ known_tags,
387
+ body);
388
+ };
389
+ }
390
+
391
+ recursive_initializationt::array_convertert
392
+ recursive_initializationt::cstring_member_initialization ()
393
+ {
394
+ return [this ](
395
+ const exprt &array,
396
+ const std::size_t length,
397
+ const std::size_t current_index,
398
+ const std::size_t depth,
399
+ const recursion_sett &known_tags,
400
+ code_blockt &body) {
401
+ PRECONDITION (array.type ().id () == ID_array);
402
+ PRECONDITION (array.type ().subtype () == char_type ());
403
+ auto const member =
404
+ index_exprt{array, from_integer (current_index, size_type ())};
405
+ if (current_index + 1 == length)
406
+ {
407
+ body.add (code_assignt{member, from_integer (0 , array.type ().subtype ())});
408
+ }
409
+ else
410
+ {
411
+ initialize (member, depth, known_tags, body);
412
+ }
413
+ };
414
+ }
0 commit comments