Skip to content

Commit b0bab14

Browse files
committed
Error handling cleanup in expr_initializer.cpp (and implied changes)
1 parent 9929e74 commit b0bab14

File tree

3 files changed

+38
-53
lines changed

3 files changed

+38
-53
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/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, message_handler);
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 & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

371372
exprt 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

Comments
 (0)