@@ -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
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,20 @@ 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 .find (
216
+ pointer_name) !=
217
+ initialization_config.pointers_to_treat_as_cstrings .end ();
218
+ }
219
+
205
220
void recursive_initializationt::initialize_dynamic_array (
206
221
const exprt &pointer,
207
222
std::size_t depth,
208
223
const recursion_sett &known_tags,
209
- code_blockt &body)
224
+ code_blockt &body,
225
+ array_convertert array_initialization)
210
226
{
211
227
PRECONDITION (pointer.type ().id () == ID_pointer);
212
228
@@ -264,7 +280,8 @@ void recursive_initializationt::initialize_dynamic_array(
264
280
std::size_t array_counter = 0 ;
265
281
for (const auto &array_variable : array_variables)
266
282
{
267
- initialize (array_variable, depth + 1 , known_tags, body);
283
+ initialize_array (
284
+ array_variable, depth + 1 , known_tags, body, array_initialization);
268
285
body.add (code_assignt{
269
286
index_exprt{arrays, from_integer (array_counter++, size_type ())},
270
287
address_of_exprt{
@@ -296,6 +313,16 @@ void recursive_initializationt::initialize_dynamic_array(
296
313
body.add (code_assignt{pointer, index_exprt{arrays, nondet_index}});
297
314
}
298
315
316
+ void recursive_initializationt::initialize_cstring (
317
+ const exprt &pointer,
318
+ std::size_t depth,
319
+ const recursion_sett &known_tags,
320
+ code_blockt &body)
321
+ {
322
+ initialize_dynamic_array (
323
+ pointer, depth, known_tags, body, cstring_member_initialization ());
324
+ }
325
+
299
326
std::string recursive_initialization_configt::to_string () const
300
327
{
301
328
std::ostringstream out{};
@@ -325,6 +352,57 @@ std::string recursive_initialization_configt::to_string() const
325
352
<< associated_array_size.second ;
326
353
}
327
354
out << " \n ]" ;
355
+ out << " \n pointers_to_treat_as_cstrings = [" ;
356
+ for (const auto &pointer_to_treat_as_string_name :
357
+ pointers_to_treat_as_cstrings)
358
+ {
359
+ out << " \n " << pointer_to_treat_as_string_name << std::endl;
360
+ }
361
+ out << " \n ]" ;
328
362
out << " \n }" ;
329
363
return out.str ();
330
364
}
365
+
366
+ recursive_initializationt::array_convertert
367
+ recursive_initializationt::default_array_member_initialization ()
368
+ {
369
+ return [this ](
370
+ const exprt &array,
371
+ const std::size_t length,
372
+ const std::size_t current_index,
373
+ const std::size_t depth,
374
+ const recursion_sett &known_tags,
375
+ code_blockt &body) {
376
+ PRECONDITION (array.type ().id () == ID_array);
377
+ initialize (
378
+ index_exprt{array, from_integer (current_index, size_type ())},
379
+ depth,
380
+ known_tags,
381
+ body);
382
+ };
383
+ }
384
+
385
+ recursive_initializationt::array_convertert
386
+ recursive_initializationt::cstring_member_initialization ()
387
+ {
388
+ return [this ](
389
+ const exprt &array,
390
+ const std::size_t length,
391
+ const std::size_t current_index,
392
+ const std::size_t depth,
393
+ const recursion_sett &known_tags,
394
+ code_blockt &body) {
395
+ PRECONDITION (array.type ().id () == ID_array);
396
+ PRECONDITION (array.type ().subtype () == char_type ());
397
+ auto const member =
398
+ index_exprt{array, from_integer (current_index, size_type ())};
399
+ if (current_index + 1 == length)
400
+ {
401
+ body.add (code_assignt{member, from_integer (0 , array.type ().subtype ())});
402
+ }
403
+ else
404
+ {
405
+ initialize (member, depth, known_tags, body);
406
+ }
407
+ };
408
+ }
0 commit comments