Skip to content

Commit 9e8dd91

Browse files
committed
Removes duplicate code with insert_before_swap_and_advance
Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent 444e753 commit 9e8dd91

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

src/goto-instrument/contracts/contracts.cpp

+13-31
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,7 @@ bool code_contractst::apply_function_contract(
557557
assertion.instructions.back().source_location.set_property_class(
558558
ID_precondition);
559559
is_fresh.update_requires(assertion);
560-
auto lines_to_iterate = assertion.instructions.size();
561-
goto_program.insert_before_swap(target, assertion);
562-
std::advance(target, lines_to_iterate);
560+
insert_before_swap_and_advance(goto_program, target, assertion);
563561
}
564562

565563
// Gather all the instructions required to handle history variables
@@ -579,9 +577,7 @@ bool code_contractst::apply_function_contract(
579577

580578
// add all the history variable initialization instructions
581579
// to the goto program
582-
auto lines_to_iterate = ensures_pair.second.instructions.size();
583-
goto_program.insert_before_swap(target, ensures_pair.second);
584-
std::advance(target, lines_to_iterate);
580+
insert_before_swap_and_advance(goto_program, target, ensures_pair.second);
585581
}
586582

587583
// Create a series of non-deterministic assignments to havoc the variables
@@ -592,19 +588,15 @@ bool code_contractst::apply_function_contract(
592588
goto_programt assigns_havoc = assigns_cause.havoc_code();
593589

594590
// Insert the non-deterministic assignment immediately before the call site.
595-
std::size_t lines_to_iterate = assigns_havoc.instructions.size();
596-
goto_program.insert_before_swap(target, assigns_havoc);
597-
std::advance(target, lines_to_iterate);
591+
insert_before_swap_and_advance(goto_program, target, assigns_havoc);
598592
}
599593

600594
// To remove the function call, insert statements related to the assumption.
601595
// Then, replace the function call with a SKIP statement.
602596
if(!ensures.is_false())
603597
{
604598
is_fresh.update_ensures(ensures_pair.first);
605-
auto lines_to_iterate = ensures_pair.first.instructions.size();
606-
goto_program.insert_before_swap(target, ensures_pair.first);
607-
std::advance(target, lines_to_iterate);
599+
insert_before_swap_and_advance(goto_program, target, ensures_pair.first);
608600
}
609601
*target = goto_programt::make_skip();
610602

@@ -690,9 +682,8 @@ void code_contractst::instrument_assign_statement(
690682
instruction_iterator->source_location));
691683
alias_assertion.instructions.back().source_location.set_comment(
692684
"Check that " + from_expr(ns, lhs.id(), lhs) + " is assignable");
693-
size_t lines_to_iterate = alias_assertion.instructions.size();
694-
program.insert_before_swap(instruction_iterator, alias_assertion);
695-
std::advance(instruction_iterator, lines_to_iterate);
685+
insert_before_swap_and_advance(
686+
program, instruction_iterator, alias_assertion);
696687
}
697688

698689
void code_contractst::instrument_call_statement(
@@ -721,15 +712,13 @@ void code_contractst::instrument_call_statement(
721712

722713
if(called_name == "malloc")
723714
{
724-
goto_programt::instructionst::iterator local_instruction_iterator =
725-
instruction_iterator;
726715
// malloc statments return a void pointer, which is then cast and assigned
727716
// to a result variable. We iterate one line forward to grab the result of
728717
// the malloc once it is cast.
729-
local_instruction_iterator++;
730-
if(local_instruction_iterator->is_assign())
718+
instruction_iterator++;
719+
if(instruction_iterator->is_assign())
731720
{
732-
const exprt &rhs = local_instruction_iterator->assign_rhs();
721+
const exprt &rhs = instruction_iterator->assign_rhs();
733722
INVARIANT(
734723
rhs.id() == ID_typecast,
735724
"malloc is called but the result is not cast. Excluding result from "
@@ -740,10 +729,8 @@ void code_contractst::instrument_call_statement(
740729
assigns_clause_targett *new_target =
741730
assigns_clause.add_target(dereference_exprt(rhs));
742731
goto_programt &pointer_capture = new_target->get_init_block();
743-
744-
size_t lines_to_iterate = pointer_capture.instructions.size();
745-
program.insert_before_swap(local_instruction_iterator, pointer_capture);
746-
std::advance(instruction_iterator, lines_to_iterate + 1);
732+
insert_before_swap_and_advance(
733+
program, instruction_iterator, pointer_capture);
747734
}
748735
return; // assume malloc edits no pre-existing memory objects.
749736
}
@@ -924,12 +911,8 @@ void code_contractst::check_frame_conditions(
924911
goto_programt mark_dead = assigns.dead_stmts();
925912

926913
// Skip lines with temporary variable declarations
927-
size_t lines_to_iterate = standin_decls.instructions.size();
928-
929-
goto_programt::instructionst::iterator instruction_it =
930-
program.instructions.begin();
931-
program.insert_before_swap(instruction_it, standin_decls);
932-
std::advance(instruction_it, lines_to_iterate);
914+
auto instruction_it = program.instructions.begin();
915+
insert_before_swap_and_advance(program, instruction_it, standin_decls);
933916

934917
for(; instruction_it != program.instructions.end(); ++instruction_it)
935918
{
@@ -942,7 +925,6 @@ void code_contractst::check_frame_conditions(
942925
assigns.add_target(instruction_it->get_decl().symbol());
943926
goto_programt &pointer_capture = new_target->get_init_block();
944927

945-
lines_to_iterate = pointer_capture.instructions.size();
946928
for(auto in : pointer_capture.instructions)
947929
{
948930
program.insert_after(instruction_it, in);

0 commit comments

Comments
 (0)