Skip to content

Commit 9e43500

Browse files
author
Daniel Kroening
committed
remove assert()
1 parent 29f1e2a commit 9e43500

File tree

2 files changed

+70
-105
lines changed

2 files changed

+70
-105
lines changed

src/goto-programs/goto_inline.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,42 @@ void goto_inline(
4949

5050
typedef goto_functionst::goto_functiont goto_functiont;
5151

52-
// find entry point
53-
goto_functionst::function_mapt::iterator it=
54-
goto_functions.function_map.find(goto_functionst::entry_point());
52+
// find entry point
53+
goto_functionst::function_mapt::iterator it=
54+
goto_functions.function_map.find(goto_functionst::entry_point());
5555

56-
if(it==goto_functions.function_map.end())
57-
return;
56+
if(it==goto_functions.function_map.end())
57+
return;
5858

59-
goto_functiont &goto_function=it->second;
60-
assert(goto_function.body_available());
59+
goto_functiont &goto_function=it->second;
60+
DATA_INVARIANT(
61+
goto_function.body_available(),
62+
"body of entry point function must be available");
6163

62-
// gather all calls
63-
// we use non-transitive inlining to avoid the goto program
64-
// copying that goto_inlinet would do otherwise
65-
goto_inlinet::inline_mapt inline_map;
64+
// gather all calls
65+
// we use non-transitive inlining to avoid the goto program
66+
// copying that goto_inlinet would do otherwise
67+
goto_inlinet::inline_mapt inline_map;
6668

67-
Forall_goto_functions(f_it, goto_functions)
68-
{
69-
goto_functiont &goto_function=f_it->second;
69+
Forall_goto_functions(f_it, goto_functions)
70+
{
71+
goto_functiont &goto_function=f_it->second;
7072

71-
if(!goto_function.body_available())
72-
continue;
73+
if(!goto_function.body_available())
74+
continue;
7375

74-
goto_inlinet::call_listt &call_list=inline_map[f_it->first];
76+
goto_inlinet::call_listt &call_list=inline_map[f_it->first];
7577

76-
goto_programt &goto_program=goto_function.body;
78+
goto_programt &goto_program=goto_function.body;
7779

78-
Forall_goto_program_instructions(i_it, goto_program)
79-
{
80-
if(!i_it->is_function_call())
81-
continue;
80+
Forall_goto_program_instructions(i_it, goto_program)
81+
{
82+
if(!i_it->is_function_call())
83+
continue;
8284

83-
call_list.push_back(goto_inlinet::callt(i_it, false));
84-
}
85+
call_list.push_back(goto_inlinet::callt(i_it, false));
8586
}
87+
}
8688

8789
goto_inline.goto_inline(
8890
goto_functionst::entry_point(), goto_function, inline_map, true);
@@ -198,7 +200,7 @@ void goto_partial_inline(
198200
if(goto_function.is_inlined() ||
199201
goto_program.instructions.size()<=smallfunc_limit)
200202
{
201-
assert(i_it->is_function_call());
203+
INVARIANT(i_it->is_function_call(), "is a call");
202204
call_list.push_back(goto_inlinet::callt(i_it, false));
203205
}
204206
}

src/goto-programs/goto_inline_class.cpp

Lines changed: 43 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ Author: Daniel Kroening, [email protected]
1717

1818
#include <cassert>
1919

20-
#include <util/prefix.h>
21-
#include <util/cprover_prefix.h>
2220
#include <util/base_type.h>
21+
#include <util/cprover_prefix.h>
22+
#include <util/expr_util.h>
23+
#include <util/invariant.h>
24+
#include <util/prefix.h>
2325
#include <util/std_code.h>
2426
#include <util/std_expr.h>
25-
#include <util/expr_util.h>
2627

2728
#include "remove_skip.h"
2829
#include "goto_inline.h"
@@ -34,8 +35,8 @@ void goto_inlinet::parameter_assignments(
3435
const exprt::operandst &arguments, // arguments of call
3536
goto_programt &dest)
3637
{
37-
assert(target->is_function_call());
38-
assert(dest.empty());
38+
PRECONDITION(target->is_function_call());
39+
PRECONDITION(dest.empty());
3940

4041
const source_locationt &source_location=target->source_location;
4142

@@ -160,8 +161,8 @@ void goto_inlinet::parameter_destruction(
160161
const code_typet &code_type, // type of called function
161162
goto_programt &dest)
162163
{
163-
assert(target->is_function_call());
164-
assert(dest.empty());
164+
PRECONDITION(target->is_function_call());
165+
PRECONDITION(dest.empty());
165166

166167
const source_locationt &source_location=target->source_location;
167168

@@ -204,51 +205,6 @@ void goto_inlinet::replace_return(
204205
{
205206
if(it->is_return())
206207
{
207-
#if 0
208-
if(lhs.is_not_nil())
209-
{
210-
if(it->code.operands().size()!=1)
211-
{
212-
error().source_location=it->code.find_source_location();
213-
str << "return expects one operand!";
214-
warning_msg();
215-
continue;
216-
}
217-
218-
goto_programt tmp;
219-
goto_programt::targett assignment=tmp.add_instruction(ASSIGN);
220-
221-
code_assignt code_assign(lhs, it->code.op0());
222-
223-
// this may happen if the declared return type at the call site
224-
// differs from the defined return type
225-
if(code_assign.lhs().type()!=
226-
code_assign.rhs().type())
227-
code_assign.rhs().make_typecast(code_assign.lhs().type());
228-
229-
assignment->code=code_assign;
230-
assignment->source_location=it->source_location;
231-
assignment->function=it->function;
232-
233-
dest.insert_before_swap(it, *assignment);
234-
it++;
235-
}
236-
else if(!it->code.operands().empty())
237-
{
238-
goto_programt tmp;
239-
goto_programt::targett expression=tmp.add_instruction(OTHER);
240-
241-
expression->code=codet(ID_expression);
242-
expression->code.move_to_operands(it->code.op0());
243-
expression->source_location=it->source_location;
244-
expression->function=it->function;
245-
246-
dest.insert_before_swap(it, *expression);
247-
it++;
248-
}
249-
250-
it->make_goto(--dest.instructions.end());
251-
#else
252208
if(lhs.is_not_nil())
253209
{
254210
if(it->code.operands().size()!=1)
@@ -280,7 +236,6 @@ void goto_inlinet::replace_return(
280236
it->type=OTHER;
281237
it++;
282238
}
283-
#endif
284239
}
285240
}
286241
}
@@ -327,9 +282,9 @@ void goto_inlinet::insert_function_body(
327282
const symbol_exprt &function,
328283
const exprt::operandst &arguments)
329284
{
330-
assert(target->is_function_call());
331-
assert(!dest.empty());
332-
assert(goto_function.body_available());
285+
PRECONDITION(target->is_function_call());
286+
PRECONDITION(!dest.empty());
287+
PRECONDITION(goto_function.body_available());
333288

334289
const irep_idt identifier=function.get_identifier();
335290

@@ -338,7 +293,9 @@ void goto_inlinet::insert_function_body(
338293
inline_log.copy_from(goto_function.body, body);
339294

340295
goto_programt::instructiont &end=body.instructions.back();
341-
assert(end.is_end_function());
296+
DATA_INVARIANT(
297+
end.is_end_function(),
298+
"final instruction of a function must be an END_FUNCTION");
342299
end.type=LOCATION;
343300

344301
if(adjust_function)
@@ -367,7 +324,9 @@ void goto_inlinet::insert_function_body(
367324
t_it=goto_function.body.instructions.begin();
368325
unsigned begin_location_number=t_it->location_number;
369326
t_it=--goto_function.body.instructions.end();
370-
assert(t_it->is_end_function());
327+
DATA_INVARIANT(
328+
t_it->is_end_function(),
329+
"final instruction of a function must be an END_FUNCTION");
371330
unsigned end_location_number=t_it->location_number;
372331

373332
unsigned call_location_number=target->location_number;
@@ -426,8 +385,8 @@ void goto_inlinet::insert_function_nobody(
426385
const symbol_exprt &function,
427386
const exprt::operandst &arguments)
428387
{
429-
assert(target->is_function_call());
430-
assert(!dest.empty());
388+
PRECONDITION(target->is_function_call());
389+
PRECONDITION(!dest.empty());
431390

432391
const irep_idt identifier=function.get_identifier();
433392

@@ -479,9 +438,9 @@ void goto_inlinet::expand_function_call(
479438
const bool force_full,
480439
goto_programt::targett target)
481440
{
482-
assert(target->is_function_call());
483-
assert(!dest.empty());
484-
assert(!transitive || inline_map.empty());
441+
PRECONDITION(target->is_function_call());
442+
PRECONDITION(!dest.empty());
443+
PRECONDITION(!transitive || inline_map.empty());
485444

486445
#ifdef DEBUG
487446
std::cout << "Expanding call:\n";
@@ -600,7 +559,7 @@ void goto_inlinet::get_call(
600559
exprt &function,
601560
exprt::operandst &arguments)
602561
{
603-
assert(it->is_function_call());
562+
PRECONDITION(it->is_function_call());
604563

605564
const code_function_callt &call=to_code_function_call(it->code);
606565

@@ -613,12 +572,12 @@ void goto_inlinet::goto_inline(
613572
const inline_mapt &inline_map,
614573
const bool force_full)
615574
{
616-
assert(check_inline_map(inline_map));
575+
PRECONDITION(check_inline_map(inline_map));
617576

618577
Forall_goto_functions(f_it, goto_functions)
619578
{
620579
const irep_idt identifier=f_it->first;
621-
assert(!identifier.empty());
580+
DATA_INVARIANT(!identifier.empty(), "function name must not be empty");
622581
goto_functiont &goto_function=f_it->second;
623582

624583
if(!goto_function.body_available())
@@ -649,14 +608,14 @@ void goto_inlinet::goto_inline_nontransitive(
649608
const inline_mapt &inline_map,
650609
const bool force_full)
651610
{
652-
assert(goto_function.body_available());
611+
PRECONDITION(goto_function.body_available());
653612

654613
finished_sett::const_iterator f_it=finished_set.find(identifier);
655614

656615
if(f_it!=finished_set.end())
657616
return;
658617

659-
assert(check_inline_map(identifier, inline_map));
618+
PRECONDITION(check_inline_map(identifier, inline_map));
660619

661620
goto_programt &goto_program=goto_function.body;
662621

@@ -700,19 +659,23 @@ const goto_inlinet::goto_functiont &goto_inlinet::goto_inline_transitive(
700659
const goto_functiont &goto_function,
701660
const bool force_full)
702661
{
703-
assert(goto_function.body_available());
662+
PRECONDITION(goto_function.body_available());
704663

705664
cachet::const_iterator c_it=cache.find(identifier);
706665

707666
if(c_it!=cache.end())
708667
{
709668
const goto_functiont &cached=c_it->second;
710-
assert(cached.body_available());
669+
DATA_INVARIANT(
670+
cached.body_available(),
671+
"body of cached functions must be available");
711672
return cached;
712673
}
713674

714675
goto_functiont &cached=cache[identifier];
715-
assert(cached.body.empty());
676+
INVARIANT(
677+
cached.body.empty(),
678+
"body of new function in cache must be empty");
716679

717680
progress() << "Creating copy of " << identifier << eom;
718681
progress() << "Number of instructions: "
@@ -772,7 +735,7 @@ bool goto_inlinet::check_inline_map(
772735
goto_functionst::function_mapt::const_iterator f_it=
773736
goto_functions.function_map.find(identifier);
774737

775-
assert(f_it!=goto_functions.function_map.end());
738+
PRECONDITION(f_it!=goto_functions.function_map.end());
776739

777740
inline_mapt::const_iterator im_it=inline_map.find(identifier);
778741

@@ -824,11 +787,11 @@ void goto_inlinet::output_inline_map(
824787
std::ostream &out,
825788
const inline_mapt &inline_map)
826789
{
827-
assert(check_inline_map(inline_map));
790+
PRECONDITION(check_inline_map(inline_map));
828791

829792
for(const auto &it : inline_map)
830793
{
831-
const irep_idt id=it.first;
794+
const irep_idt &id=it.first;
832795
const call_listt &call_list=it.second;
833796

834797
out << "Function: " << id << "\n";
@@ -842,7 +805,7 @@ void goto_inlinet::output_inline_map(
842805
!call_list.empty())
843806
{
844807
const goto_functiont &goto_function=f_it->second;
845-
assert(goto_function.body_available());
808+
PRECONDITION(goto_function.body_available());
846809

847810
const goto_programt &goto_program=goto_function.body;
848811

@@ -903,12 +866,12 @@ void goto_inlinet::goto_inline_logt::add_segment(
903866
const unsigned call_location_number,
904867
const irep_idt function)
905868
{
906-
assert(!goto_program.empty());
907-
assert(!function.empty());
908-
assert(end_location_number>=begin_location_number);
869+
PRECONDITION(!goto_program.empty());
870+
PRECONDITION(!function.empty());
871+
PRECONDITION(end_location_number>=begin_location_number);
909872

910873
goto_programt::const_targett start=goto_program.instructions.begin();
911-
assert(log_map.find(start)==log_map.end());
874+
PRECONDITION(log_map.find(start)==log_map.end());
912875

913876
goto_programt::const_targett end=goto_program.instructions.end();
914877
end--;
@@ -927,7 +890,7 @@ void goto_inlinet::goto_inline_logt::copy_from(
927890
const goto_programt &from,
928891
const goto_programt &to)
929892
{
930-
assert(from.instructions.size()==to.instructions.size());
893+
PRECONDITION(from.instructions.size()==to.instructions.size());
931894

932895
goto_programt::const_targett it1=from.instructions.begin();
933896
goto_programt::const_targett it2=to.instructions.begin();

0 commit comments

Comments
 (0)