@@ -114,13 +114,16 @@ exprt c_typecheck_baset::do_initializer_rec(
114114 {
115115 // fill up
116116 tmp.type ()=type;
117- exprt zero=
118- zero_initializer (
119- full_type.subtype (),
120- value.source_location (),
121- *this ,
122- get_message_handler ());
123- tmp.operands ().resize (integer2size_t (array_size), zero);
117+ const auto zero =
118+ zero_initializer (full_type.subtype (), value.source_location (), *this );
119+ if (!zero.has_value ())
120+ {
121+ err_location (value);
122+ error () << " cannot zero-initialize array with subtype `"
123+ << to_string (full_type.subtype ()) << " '" << eom;
124+ throw 0 ;
125+ }
126+ tmp.operands ().resize (integer2size_t (array_size), *zero);
124127 }
125128 }
126129
@@ -171,13 +174,16 @@ exprt c_typecheck_baset::do_initializer_rec(
171174 {
172175 // fill up
173176 tmp2.type ()=type;
174- exprt zero=
175- zero_initializer (
176- full_type.subtype (),
177- value.source_location (),
178- *this ,
179- get_message_handler ());
180- tmp2.operands ().resize (integer2size_t (array_size), zero);
177+ const auto zero =
178+ zero_initializer (full_type.subtype (), value.source_location (), *this );
179+ if (!zero.has_value ())
180+ {
181+ err_location (value);
182+ error () << " cannot zero-initialize array with subtype `"
183+ << to_string (full_type.subtype ()) << " '" << eom;
184+ throw 0 ;
185+ }
186+ tmp2.operands ().resize (integer2size_t (array_size), *zero);
181187 }
182188 }
183189
@@ -395,13 +401,16 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
395401 to_array_type (full_type).size ().is_nil ()))
396402 {
397403 // we are willing to grow an incomplete or zero-sized array
398- exprt zero=
399- zero_initializer (
400- full_type.subtype (),
401- value.source_location (),
402- *this ,
403- get_message_handler ());
404- dest->operands ().resize (integer2size_t (index)+1 , zero);
404+ const auto zero = zero_initializer (
405+ full_type.subtype (), value.source_location (), *this );
406+ if (!zero.has_value ())
407+ {
408+ err_location (value);
409+ error () << " cannot zero-initialize array with subtype `"
410+ << to_string (full_type.subtype ()) << " '" << eom;
411+ throw 0 ;
412+ }
413+ dest->operands ().resize (integer2size_t (index) + 1 , *zero);
405414
406415 // todo: adjust type!
407416 }
@@ -461,15 +470,17 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
461470 {
462471 // Note that gcc issues a warning if the union component is switched.
463472 // Build a union expression from given component.
464- union_exprt union_expr (type);
465- union_expr.op ()=
466- zero_initializer (
467- component.type (),
468- value.source_location (),
469- *this ,
470- get_message_handler ());
473+ const auto zero =
474+ zero_initializer (component.type (), value.source_location (), *this );
475+ if (!zero.has_value ())
476+ {
477+ err_location (value);
478+ error () << " cannot zero-initialize union component of type `"
479+ << to_string (component.type ()) << " '" << eom;
480+ throw 0 ;
481+ }
482+ union_exprt union_expr (component.get_name (), *zero, type);
471483 union_expr.add_source_location ()=value.source_location ();
472- union_expr.set_component_name (component.get_name ());
473484 *dest=union_expr;
474485 }
475486
@@ -524,15 +535,17 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
524535 const union_typet::componentt &component=
525536 union_type.components ().front ();
526537
527- union_exprt union_expr (type);
528- union_expr.op ()=
529- zero_initializer (
530- component.type (),
531- value.source_location (),
532- *this ,
533- get_message_handler ());
538+ const auto zero =
539+ zero_initializer (component.type (), value.source_location (), *this );
540+ if (!zero.has_value ())
541+ {
542+ err_location (value);
543+ error () << " cannot zero-initialize union component of type `"
544+ << to_string (component.type ()) << " '" << eom;
545+ throw 0 ;
546+ }
547+ union_exprt union_expr (component.get_name (), *zero, type);
534548 union_expr.add_source_location ()=value.source_location ();
535- union_expr.set_component_name (component.get_name ());
536549 *dest=union_expr;
537550 }
538551 }
@@ -830,9 +843,15 @@ exprt c_typecheck_baset::do_initializer_list(
830843 full_type.id ()==ID_vector)
831844 {
832845 // start with zero everywhere
833- result=
834- zero_initializer (
835- type, value.source_location (), *this , get_message_handler ());
846+ const auto zero = zero_initializer (type, value.source_location (), *this );
847+ if (!zero.has_value ())
848+ {
849+ err_location (value.source_location ());
850+ error () << " cannot zero-initialize `" << to_string (full_type) << " '"
851+ << eom;
852+ throw 0 ;
853+ }
854+ result = *zero;
836855 }
837856 else if (full_type.id ()==ID_array)
838857 {
@@ -845,9 +864,15 @@ exprt c_typecheck_baset::do_initializer_list(
845864 else
846865 {
847866 // start with zero everywhere
848- result=
849- zero_initializer (
850- type, value.source_location (), *this , get_message_handler ());
867+ const auto zero = zero_initializer (type, value.source_location (), *this );
868+ if (!zero.has_value ())
869+ {
870+ err_location (value.source_location ());
871+ error () << " cannot zero-initialize `" << to_string (full_type) << " '"
872+ << eom;
873+ throw 0 ;
874+ }
875+ result = *zero;
851876 }
852877
853878 // 6.7.9, 14: An array of character type may be initialized by a character
0 commit comments