Skip to content

Commit 64d5dd2

Browse files
authored
Merge pull request #1538 from smowton/smowton/cleanup/remove-returns-asserts-to-invariants
Remove-returns: asserts -> invariants
2 parents fca7c04 + dec5ddf commit 64d5dd2

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/goto-programs/remove_returns.cpp

+27-15
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ void remove_returnst::replace_returns(
9292
{
9393
if(i_it->is_return())
9494
{
95-
assert(i_it->code.operands().size()==1);
95+
INVARIANT(
96+
i_it->code.operands().size()==1,
97+
"return instructions should have one operand");
9698

9799
// replace "return x;" by "fkt#return_value=x;"
98100
symbol_exprt lhs_expr;
@@ -127,7 +129,10 @@ void remove_returnst::do_function_calls(
127129
{
128130
// replace "lhs=f(...)" by
129131
// "f(...); lhs=f#return_value; DEAD f#return_value;"
130-
assert(function_call.function().id()==ID_symbol);
132+
INVARIANT(
133+
function_call.function().id()==ID_symbol,
134+
"indirect function calls should have been removed prior to running "
135+
"remove-returns");
131136

132137
const irep_idt function_id=
133138
to_symbol_expr(function_call.function()).get_identifier();
@@ -226,12 +231,9 @@ code_typet original_return_type(
226231
if(rv_it!=symbol_table.symbols.end())
227232
{
228233
// look up the function symbol
229-
symbol_tablet::symbolst::const_iterator s_it=
230-
symbol_table.symbols.find(function_id);
234+
const symbolt &function_symbol=symbol_table.lookup_ref(function_id);
231235

232-
assert(s_it!=symbol_table.symbols.end());
233-
234-
type=to_code_type(s_it->second.type);
236+
type=to_code_type(function_symbol.type);
235237
type.return_type()=rv_it->second.type;
236238
}
237239

@@ -284,18 +286,25 @@ bool remove_returnst::restore_returns(
284286

285287
while(!i_it->is_goto() && !i_it->is_end_function())
286288
{
287-
assert(i_it->is_dead());
289+
INVARIANT(
290+
i_it->is_dead(),
291+
"only dead statements should appear between "
292+
"a return and the next goto or function end");
288293
i_it++;
289294
}
290295

291296
if(i_it->is_goto())
292297
{
293-
goto_programt::const_targett target=i_it->get_target();
294-
assert(target->is_end_function());
298+
INVARIANT(
299+
i_it->get_target()->is_end_function(),
300+
"GOTO following return should target end of function");
295301
}
296302
else
297303
{
298-
assert(i_it->is_end_function());
304+
INVARIANT(
305+
i_it->is_end_function(),
306+
"control-flow after assigning return value should lead directly "
307+
"to end of function");
299308
i_it=goto_program.instructions.insert(i_it, *i_it);
300309
}
301310

@@ -307,7 +316,7 @@ bool remove_returnst::restore_returns(
307316
return false;
308317
}
309318

310-
/// turns f(...); lhs=f#return_value; into x=f(...)
319+
/// turns f(...); lhs=f#return_value; into lhs=f(...)
311320
void remove_returnst::undo_function_calls(
312321
goto_functionst &goto_functions,
313322
goto_programt &goto_program)
@@ -337,7 +346,9 @@ void remove_returnst::undo_function_calls(
337346
// and revert to "lhs=f(...);"
338347
goto_programt::instructionst::iterator next=i_it;
339348
++next;
340-
assert(next!=goto_program.instructions.end());
349+
INVARIANT(
350+
next!=goto_program.instructions.end(),
351+
"non-void function call must be followed by #return_value read");
341352

342353
if(!next->is_assign())
343354
continue;
@@ -358,8 +369,9 @@ void remove_returnst::undo_function_calls(
358369
// remove the assignment and subsequent dead
359370
// i_it remains valid
360371
next=goto_program.instructions.erase(next);
361-
assert(next!=goto_program.instructions.end());
362-
assert(next->is_dead());
372+
INVARIANT(
373+
next!=goto_program.instructions.end() && next->is_dead(),
374+
"read from #return_value should be followed by DEAD #return_value");
363375
// i_it remains valid
364376
goto_program.instructions.erase(next);
365377
}

0 commit comments

Comments
 (0)