Skip to content

Commit 1fa7178

Browse files
committed
Error handling cleanup in expr_initializer.cpp (and implied changes)
1 parent 6dcb6ab commit 1fa7178

9 files changed

+58
-138
lines changed

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ bool ansi_c_entry_point(
177177
return false; // give up
178178
}
179179

180-
if(static_lifetime_init(symbol_table, symbol.location, message_handler))
181-
return true;
180+
static_lifetime_init(symbol_table, symbol.location, message_handler);
182181

183182
return generate_ansi_c_start_function(symbol, symbol_table, message_handler);
184183
}

src/ansi-c/c_typecheck_initializer.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,8 @@ 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());
117+
exprt zero =
118+
zero_initializer(full_type.subtype(), value.source_location(), *this);
123119
tmp.operands().resize(integer2size_t(array_size), zero);
124120
}
125121
}
@@ -171,12 +167,8 @@ exprt c_typecheck_baset::do_initializer_rec(
171167
{
172168
// fill up
173169
tmp2.type()=type;
174-
exprt zero=
175-
zero_initializer(
176-
full_type.subtype(),
177-
value.source_location(),
178-
*this,
179-
get_message_handler());
170+
exprt zero =
171+
zero_initializer(full_type.subtype(), value.source_location(), *this);
180172
tmp2.operands().resize(integer2size_t(array_size), zero);
181173
}
182174
}
@@ -399,12 +391,8 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
399391
to_array_type(full_type).size().is_nil()))
400392
{
401393
// we are willing to grow an incomplete or zero-sized array
402-
exprt zero=
403-
zero_initializer(
404-
full_type.subtype(),
405-
value.source_location(),
406-
*this,
407-
get_message_handler());
394+
exprt zero = zero_initializer(
395+
full_type.subtype(), value.source_location(), *this);
408396
dest->operands().resize(integer2size_t(index)+1, zero);
409397

410398
// todo: adjust type!
@@ -466,12 +454,8 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
466454
// Note that gcc issues a warning if the union component is switched.
467455
// Build a union expression from given component.
468456
union_exprt union_expr(type);
469-
union_expr.op()=
470-
zero_initializer(
471-
component.type(),
472-
value.source_location(),
473-
*this,
474-
get_message_handler());
457+
union_expr.op() =
458+
zero_initializer(component.type(), value.source_location(), *this);
475459
union_expr.add_source_location()=value.source_location();
476460
union_expr.set_component_name(component.get_name());
477461
*dest=union_expr;
@@ -529,12 +513,8 @@ exprt::operandst::const_iterator c_typecheck_baset::do_designated_initializer(
529513
union_type.components().front();
530514

531515
union_exprt union_expr(type);
532-
union_expr.op()=
533-
zero_initializer(
534-
component.type(),
535-
value.source_location(),
536-
*this,
537-
get_message_handler());
516+
union_expr.op() =
517+
zero_initializer(component.type(), value.source_location(), *this);
538518
union_expr.add_source_location()=value.source_location();
539519
union_expr.set_component_name(component.get_name());
540520
*dest=union_expr;
@@ -836,9 +816,7 @@ exprt c_typecheck_baset::do_initializer_list(
836816
full_type.id()==ID_vector)
837817
{
838818
// start with zero everywhere
839-
result=
840-
zero_initializer(
841-
type, value.source_location(), *this, get_message_handler());
819+
result = zero_initializer(type, value.source_location(), *this);
842820
}
843821
else if(full_type.id()==ID_array)
844822
{
@@ -851,9 +829,7 @@ exprt c_typecheck_baset::do_initializer_list(
851829
else
852830
{
853831
// start with zero everywhere
854-
result=
855-
zero_initializer(
856-
type, value.source_location(), *this, get_message_handler());
832+
result = zero_initializer(type, value.source_location(), *this);
857833
}
858834

859835
// 6.7.9, 14: An array of character type may be initialized by a character

src/cpp/cpp_typecheck_expr.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -892,12 +892,8 @@ void cpp_typecheckt::typecheck_expr_explicit_typecast(exprt &expr)
892892
{
893893
// Default value, e.g., int()
894894
typecheck_type(expr.type());
895-
exprt new_expr=
896-
::zero_initializer(
897-
expr.type(),
898-
expr.find_source_location(),
899-
*this,
900-
get_message_handler());
895+
exprt new_expr =
896+
::zero_initializer(expr.type(), expr.find_source_location(), *this);
901897

902898
new_expr.add_source_location()=expr.source_location();
903899
expr=new_expr;

src/cpp/cpp_typecheck_initializer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ void cpp_typecheckt::zero_initializer(
313313
}
314314
else
315315
{
316-
exprt value=
317-
::zero_initializer(
318-
final_type, source_location, *this, get_message_handler());
316+
exprt value = ::zero_initializer(final_type, source_location, *this);
319317

320318
code_assignt assign;
321319
assign.lhs()=object;

src/goto-cc/linker_script_merge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ int linker_script_merget::ls_data2instructions(
525525
// Push the array initialization to the front now, so that it happens before
526526
// the initialization of the symbols that point to it.
527527
namespacet ns(symbol_table);
528-
exprt zi=zero_initializer(array_type, array_loc, ns, *message_handler);
528+
exprt zi = zero_initializer(array_type, array_loc, ns);
529529
code_assignt array_assign(array_expr, zi);
530530
array_assign.add_source_location()=array_loc;
531531
goto_programt::instructiont array_assign_i;

src/goto-programs/builtin_functions.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,12 +1202,8 @@ void goto_convertt::do_function_call_symbol(
12021202
{
12031203
goto_programt::targett t=dest.add_instruction(ASSIGN);
12041204
t->source_location=function.source_location();
1205-
exprt zero=
1206-
zero_initializer(
1207-
dest_expr.type(),
1208-
function.source_location(),
1209-
ns,
1210-
get_message_handler());
1205+
exprt zero =
1206+
zero_initializer(dest_expr.type(), function.source_location(), ns);
12111207
t->code=code_assignt(dest_expr, zero);
12121208
}
12131209
}

src/goto-symex/symex_builtin_functions.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,8 @@ void goto_symext::symex_allocate(
167167

168168
if(!zero_init.is_zero() && !zero_init.is_false())
169169
{
170-
null_message_handlert null_message;
171-
exprt zero_value=
172-
zero_initializer(
173-
object_type,
174-
code.source_location(),
175-
ns,
176-
null_message);
170+
exprt zero_value =
171+
zero_initializer(object_type, code.source_location(), ns);
177172

178173
if(zero_value.is_not_nil())
179174
{

src/linking/static_lifetime_init.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,10 @@ bool static_lifetime_init(
117117

118118
if(symbol.value.is_nil())
119119
{
120-
try
121-
{
122-
namespacet ns(symbol_table);
123-
rhs=zero_initializer(symbol.type, symbol.location, ns, message_handler);
124-
assert(rhs.is_not_nil());
125-
}
126-
catch(...)
127-
{
128-
return true;
129-
}
120+
namespacet ns(symbol_table);
121+
rhs = zero_initializer(symbol.type, symbol.location, ns);
122+
INVARIANT(
123+
rhs.is_not_nil(), "result of zero-initialization must be non-nil");
130124
}
131125
else
132126
rhs=symbol.value;

src/util/expr_initializer.cpp

Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ Author: Daniel Kroening, [email protected]
2323
#include "std_expr.h"
2424

2525
template <bool nondet>
26-
class expr_initializert : public messaget
26+
class expr_initializert
2727
{
2828
public:
29-
expr_initializert(
30-
const namespacet &_ns,
31-
message_handlert &_message_handler):
32-
messaget(_message_handler),
33-
ns(_ns)
29+
expr_initializert(const namespacet &_ns) : ns(_ns)
3430
{
3531
}
3632

@@ -56,6 +52,10 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
5652
{
5753
const irep_idt &type_id=type.id();
5854

55+
PRECONDITION(
56+
type_id != ID_code,
57+
source_location.as_string() + ": cannot initialize code type");
58+
5959
if(type_id==ID_unsignedbv ||
6060
type_id==ID_signedbv ||
6161
type_id==ID_pointer ||
@@ -119,12 +119,6 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
119119
result.add_source_location()=source_location;
120120
return result;
121121
}
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-
}
128122
else if(type_id==ID_array)
129123
{
130124
const array_typet &array_type=to_array_type(type);
@@ -160,10 +154,15 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
160154
if(nondet)
161155
return side_effect_expr_nondett(type, source_location);
162156

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;
157+
std::ostringstream oss;
158+
oss << format(array_type.size());
159+
160+
INVARIANT(
161+
false,
162+
"non-infinite array size expression must be convertible to an "
163+
"integer",
164+
source_location.as_string() +
165+
": failed to zero-initialize array of size `" + oss.str() + "'");
167166
}
168167

169168
DATA_INVARIANT(
@@ -188,10 +187,14 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
188187
if(nondet)
189188
return side_effect_expr_nondett(type, source_location);
190189

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;
190+
std::ostringstream oss;
191+
oss << format(vector_type.size());
192+
193+
INVARIANT(
194+
false,
195+
"vector size must be convertible to an integer",
196+
source_location.as_string() +
197+
": failed to zero-initialize vector of size `" + oss.str() + "'");
195198
}
196199

197200
DATA_INVARIANT(
@@ -323,66 +326,29 @@ exprt expr_initializert<nondet>::expr_initializer_rec(
323326
}
324327
else
325328
{
326-
error().source_location=source_location;
327-
error() << "failed to initialize `" << format(type) << "'" << eom;
328-
throw 0;
329-
}
330-
}
331-
332-
exprt zero_initializer(
333-
const typet &type,
334-
const source_locationt &source_location,
335-
const namespacet &ns,
336-
message_handlert &message_handler)
337-
{
338-
expr_initializert<false> z_i(ns, message_handler);
339-
return z_i(type, source_location);
340-
}
329+
std::ostringstream oss;
330+
oss << format(type);
341331

342-
exprt nondet_initializer(
343-
const typet &type,
344-
const source_locationt &source_location,
345-
const namespacet &ns,
346-
message_handlert &message_handler)
347-
{
348-
expr_initializert<true> z_i(ns, message_handler);
349-
return z_i(type, source_location);
332+
PRECONDITION(
333+
false,
334+
source_location.as_string() + ": cannot initialize " + oss.str() + "'");
335+
}
350336
}
351337

352338
exprt zero_initializer(
353339
const typet &type,
354340
const source_locationt &source_location,
355341
const namespacet &ns)
356342
{
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-
}
343+
expr_initializert<false> z_i(ns);
344+
return z_i(type, source_location);
369345
}
370346

371347
exprt nondet_initializer(
372348
const typet &type,
373349
const source_locationt &source_location,
374350
const namespacet &ns)
375351
{
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-
}
352+
expr_initializert<true> z_i(ns);
353+
return z_i(type, source_location);
388354
}

0 commit comments

Comments
 (0)