Skip to content

Commit e794caf

Browse files
committed
Minor refactor and cleanup on code contracts
We can maintain a `protected` `goto_convertt` field in our class, which would be reusable, rather than trying to instantiate an object for each codet -> goto_programt transformation.
1 parent 8e651e4 commit e794caf

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

src/goto-instrument/code_contracts.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Date: February 2016
1717

1818
#include <analyses/local_may_alias.h>
1919

20-
#include <goto-programs/goto_convert_class.h>
2120
#include <goto-programs/remove_skip.h>
2221

2322
#include <util/arith_tools.h>
@@ -155,15 +154,6 @@ static void check_apply_invariants(
155154
loop_end->set_condition(boolean_negate(loop_end->get_condition()));
156155
}
157156

158-
void code_contractst::convert_to_goto(
159-
const codet &code,
160-
const irep_idt &mode,
161-
goto_programt &program)
162-
{
163-
goto_convertt converter(symbol_table, log.get_message_handler());
164-
converter.goto_convert(code, program, mode);
165-
}
166-
167157
bool code_contractst::has_contract(const irep_idt fun_name)
168158
{
169159
const symbolt &function_symbol = ns.lookup(fun_name);
@@ -335,10 +325,10 @@ bool code_contractst::apply_function_contract(
335325
if(requires.is_not_nil())
336326
{
337327
goto_programt assertion;
338-
convert_to_goto(
328+
converter.goto_convert(
339329
code_assertt(requires),
340-
symbol_table.lookup_ref(function).mode,
341-
assertion);
330+
assertion,
331+
symbol_table.lookup_ref(function).mode);
342332
auto lines_to_iterate = assertion.instructions.size();
343333
goto_program.insert_before_swap(target, assertion);
344334
std::advance(target, lines_to_iterate);
@@ -364,10 +354,10 @@ bool code_contractst::apply_function_contract(
364354
if(ensures.is_not_nil())
365355
{
366356
goto_programt assumption;
367-
convert_to_goto(
357+
converter.goto_convert(
368358
code_assumet(ensures),
369-
symbol_table.lookup_ref(function).mode,
370-
assumption);
359+
assumption,
360+
symbol_table.lookup_ref(function).mode);
371361
auto lines_to_iterate = assumption.instructions.size();
372362
goto_program.insert_before_swap(target, assumption);
373363
std::advance(target, lines_to_iterate);
@@ -858,8 +848,8 @@ void code_contractst::add_contract_check(
858848
replace(requires_cond);
859849

860850
goto_programt assumption;
861-
convert_to_goto(
862-
code_assumet(requires_cond), function_symbol.mode, assumption);
851+
converter.goto_convert(
852+
code_assumet(requires_cond), assumption, function_symbol.mode);
863853
check.destructive_append(assumption);
864854
}
865855

@@ -874,8 +864,8 @@ void code_contractst::add_contract_check(
874864
if(ensures.is_not_nil())
875865
{
876866
goto_programt assertion;
877-
convert_to_goto(
878-
code_assertt(ensures_cond), function_symbol.mode, assertion);
867+
converter.goto_convert(
868+
code_assertt(ensures_cond), assertion, function_symbol.mode);
879869
check.destructive_append(assertion);
880870
}
881871

src/goto-instrument/code_contracts.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Date: February 2016
1818
#include <string>
1919
#include <unordered_set>
2020

21+
#include <goto-programs/goto_convert_class.h>
2122
#include <goto-programs/goto_functions.h>
2223
#include <goto-programs/goto_model.h>
2324

@@ -35,8 +36,9 @@ class code_contractst
3536
: ns(goto_model.symbol_table),
3637
symbol_table(goto_model.symbol_table),
3738
goto_functions(goto_model.goto_functions),
38-
temporary_counter(0),
39-
log(log)
39+
log(log),
40+
converter(symbol_table, log.get_message_handler())
41+
4042
{
4143
}
4244

@@ -94,20 +96,14 @@ class code_contractst
9496
symbol_tablet &symbol_table;
9597
goto_functionst &goto_functions;
9698

97-
unsigned temporary_counter;
9899
messaget &log;
100+
goto_convertt converter;
99101

100102
std::unordered_set<irep_idt> summarized;
101103

102104
/// \brief Enforce contract of a single function
103105
bool enforce_contract(const std::string &);
104106

105-
/// \brief Create goto instructions based on code and add them to program.
106-
void convert_to_goto(
107-
const codet &code,
108-
const irep_idt &mode,
109-
goto_programt &program);
110-
111107
/// Insert assertion statements into the goto program to ensure that
112108
/// assigned memory is within the assignable memory frame.
113109
bool add_pointer_checks(const std::string &);

0 commit comments

Comments
 (0)