Skip to content

Commit adfd9a4

Browse files
Merge pull request #1024 from cristina-david/feature/switch-to-invariants-in-exception-handling
Switch from assertions to invariants/preconditions in exception handling
2 parents 2e115f1 + 45e0b97 commit adfd9a4

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/analyses/uncaught_exceptions_analysis.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Author: Cristina David
1717
/// Returns the compile type of an exception
1818
irep_idt uncaught_exceptions_domaint::get_exception_type(const typet &type)
1919
{
20-
assert(type.id()==ID_pointer);
20+
PRECONDITION(type.id()==ID_pointer);
2121

2222
if(type.subtype().id()==ID_symbol)
2323
{
@@ -122,7 +122,9 @@ void uncaught_exceptions_domaint::transform(
122122
{
123123
const exprt &function_expr=
124124
to_code_function_call(instruction.code).function();
125-
assert(function_expr.id()==ID_symbol);
125+
DATA_INVARIANT(
126+
function_expr.id()==ID_symbol,
127+
"identifier expected to be a symbol");
126128
const irep_idt &function_name=
127129
to_symbol_expr(function_expr).get_identifier();
128130
// use the current information about the callee
@@ -193,7 +195,9 @@ void uncaught_exceptions_analysist::output(
193195
{
194196
std::cout << "Uncaught exceptions in function " <<
195197
it->first << ": " << std::endl;
196-
assert(exceptions_map.find(it->first)!=exceptions_map.end());
198+
INVARIANT(
199+
exceptions_map.find(it->first)!=exceptions_map.end(),
200+
"each function expected to be recorded in `exceptions_map`");
197201
for(auto exc_id : exceptions_map[it->first])
198202
std::cout << id2string(exc_id) << " ";
199203
std::cout << std::endl;

src/goto-programs/remove_exceptions.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ void remove_exceptionst::add_exceptional_returns(
7777
const irep_idt &function_id=func_it->first;
7878
goto_programt &goto_program=func_it->second.body;
7979

80-
assert(symbol_table.has_symbol(function_id));
80+
INVARIANT(
81+
symbol_table.has_symbol(function_id),
82+
"functions should be recorded in the symbol table");
8183
const symbolt &function_symbol=symbol_table.lookup(function_id);
8284

8385
// for now only add exceptional returns for Java
@@ -116,7 +118,9 @@ void remove_exceptionst::add_exceptional_returns(
116118
{
117119
const exprt &function_expr=
118120
to_code_function_call(instr_it->code).function();
119-
assert(function_expr.id()==ID_symbol);
121+
DATA_INVARIANT(
122+
function_expr.id()==ID_symbol,
123+
"identifier expected to be a symbol");
120124
const irep_idt &function_name=
121125
to_symbol_expr(function_expr).get_identifier();
122126
has_uncaught_exceptions=!exceptions_map[function_name].empty();
@@ -142,7 +146,9 @@ void remove_exceptionst::add_exceptional_returns(
142146
symbol_tablet::symbolst::iterator s_it=
143147
symbol_table.symbols.find(function_id);
144148

145-
assert(s_it!=symbol_table.symbols.end());
149+
INVARIANT(
150+
s_it!=symbol_table.symbols.end(),
151+
"functions should be recorded in the symbol table");
146152

147153
auxiliary_symbolt new_symbol;
148154
new_symbol.is_static_lifetime=true;
@@ -180,7 +186,7 @@ void remove_exceptionst::instrument_exception_handler(
180186
const irep_idt &function_id=func_it->first;
181187
goto_programt &goto_program=func_it->second.body;
182188

183-
assert(instr_it->type==CATCH && instr_it->code.has_operands());
189+
PRECONDITION(instr_it->type==CATCH && instr_it->code.has_operands());
184190

185191
// retrieve the exception variable
186192
const exprt &exception=instr_it->code.op0();
@@ -226,9 +232,13 @@ static goto_programt::targett get_exceptional_output(
226232
const irep_idt &statement=it->code.get_statement();
227233
if(statement==ID_output)
228234
{
229-
assert(it->code.operands().size()>=2);
235+
DATA_INVARIANT(
236+
it->code.operands().size()>=2,
237+
"output expected to have at least 2 operands");
230238
const exprt &expr=it->code.op1();
231-
assert(expr.id()==ID_symbol);
239+
DATA_INVARIANT(
240+
expr.id()==ID_symbol,
241+
"identifier expected to be a symbol");
232242
const symbol_exprt &symbol=to_symbol_expr(expr);
233243
if(id2string(symbol.get_identifier()).find(EXC_SUFFIX)
234244
!=std::string::npos)
@@ -246,7 +256,7 @@ void remove_exceptionst::instrument_throw(
246256
const remove_exceptionst::stack_catcht &stack_catch,
247257
std::vector<exprt> &locals)
248258
{
249-
assert(instr_it->type==THROW);
259+
PRECONDITION(instr_it->type==THROW);
250260

251261
const exprt &exc_expr=
252262
uncaught_exceptions_domaint::get_exception_symbol(instr_it->code);
@@ -263,8 +273,6 @@ void remove_exceptionst::instrument_throw(
263273
goto_programt &goto_program=func_it->second.body;
264274
const irep_idt &function_id=func_it->first;
265275

266-
assert(instr_it->code.operands().size()==1);
267-
268276
// find the end of the function
269277
goto_programt::targett exceptional_output=
270278
get_exceptional_output(goto_program);
@@ -334,7 +342,7 @@ void remove_exceptionst::instrument_function_call(
334342
const stack_catcht &stack_catch,
335343
std::vector<exprt> &locals)
336344
{
337-
assert(instr_it->type==FUNCTION_CALL);
345+
PRECONDITION(instr_it->type==FUNCTION_CALL);
338346

339347
goto_programt &goto_program=func_it->second.body;
340348
const irep_idt &function_id=func_it->first;
@@ -344,7 +352,9 @@ void remove_exceptionst::instrument_function_call(
344352
next_it++;
345353

346354
code_function_callt &function_call=to_code_function_call(instr_it->code);
347-
assert(function_call.function().id()==ID_symbol);
355+
DATA_INVARIANT(
356+
function_call.function().id()==ID_symbol,
357+
"identified expected to be a symbol");
348358
const irep_idt &callee_id=
349359
to_symbol_expr(function_call.function()).get_identifier();
350360

@@ -480,7 +490,9 @@ void remove_exceptionst::instrument_exceptions(
480490
// copy targets
481491
const irept::subt &exception_list=
482492
instr_it->code.find(ID_exception_list).get_sub();
483-
assert(exception_list.size()==instr_it->targets.size());
493+
INVARIANT(
494+
exception_list.size()==instr_it->targets.size(),
495+
"`exception_list` should contain current instruction's targets");
484496

485497
// Fill the map with the catch type and the target
486498
unsigned i=0;

0 commit comments

Comments
 (0)