@@ -56,6 +56,10 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
5656{
5757 const irep_idt &type_id=type.id ();
5858
59+ PRECONDITION (
60+ type_id != ID_code,
61+ source_location.as_string () + " : cannot initialize code type" );
62+
5963 if (type_id==ID_unsignedbv ||
6064 type_id==ID_signedbv ||
6165 type_id==ID_pointer ||
@@ -119,12 +123,6 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
119123 result.add_source_location ()=source_location;
120124 return result;
121125 }
122- else if (type_id==ID_code)
123- {
124- error ().source_location =source_location;
125- error () << " cannot initialize code-type" << eom;
126- throw 0 ;
127- }
128126 else if (type_id==ID_array)
129127 {
130128 const array_typet &array_type=to_array_type (type);
@@ -160,10 +158,15 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
160158 if (nondet)
161159 return side_effect_expr_nondett (type, source_location);
162160
163- error ().source_location =source_location;
164- error () << " failed to zero-initialize array of non-fixed size `"
165- << format (array_type.size ()) << " '" << eom;
166- throw 0 ;
161+ std::ostringstream oss;
162+ oss << format (array_type.size ());
163+
164+ INVARIANT (
165+ false ,
166+ " non-infinite array size expression must be convertible to an "
167+ " integer" ,
168+ source_location.as_string () +
169+ " : failed to zero-initialize array of size `" + oss.str () + " '" );
167170 }
168171
169172 DATA_INVARIANT (
@@ -188,10 +191,14 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
188191 if (nondet)
189192 return side_effect_expr_nondett (type, source_location);
190193
191- error ().source_location =source_location;
192- error () << " failed to zero-initialize vector of non-fixed size `"
193- << format (vector_type.size ()) << " '" << eom;
194- throw 0 ;
194+ std::ostringstream oss;
195+ oss << format (vector_type.size ());
196+
197+ INVARIANT (
198+ false ,
199+ " vector size must be convertible to an integer" ,
200+ source_location.as_string () +
201+ " : failed to zero-initialize vector of size `" + oss.str () + " '" );
195202 }
196203
197204 DATA_INVARIANT (
@@ -323,9 +330,12 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
323330 }
324331 else
325332 {
326- error ().source_location =source_location;
327- error () << " failed to initialize `" << format (type) << " '" << eom;
328- throw 0 ;
333+ std::ostringstream oss;
334+ oss << format (type);
335+
336+ PRECONDITION (
337+ false ,
338+ source_location.as_string () + " : cannot initialize " + oss.str () + " '" );
329339 }
330340}
331341
@@ -354,35 +364,17 @@ exprt zero_initializer(
354364 const source_locationt &source_location,
355365 const namespacet &ns)
356366{
357- std::ostringstream oss;
358- stream_message_handlert mh (oss);
359-
360- try
361- {
362- expr_initializert<false > z_i (ns, mh);
363- return z_i (type, source_location);
364- }
365- catch (int )
366- {
367- throw oss.str ();
368- }
367+ null_message_handlert null_message_handler;
368+ expr_initializert<false > z_i (ns, null_message_handler);
369+ return z_i (type, source_location);
369370}
370371
371372exprt nondet_initializer (
372373 const typet &type,
373374 const source_locationt &source_location,
374375 const namespacet &ns)
375376{
376- std::ostringstream oss;
377- stream_message_handlert mh (oss);
378-
379- try
380- {
381- expr_initializert<true > z_i (ns, mh);
382- return z_i (type, source_location);
383- }
384- catch (int )
385- {
386- throw oss.str ();
387- }
377+ null_message_handlert null_message_handler;
378+ expr_initializert<true > z_i (ns, null_message_handler);
379+ return z_i (type, source_location);
388380}
0 commit comments