Skip to content

Commit ef24d8e

Browse files
committed
Refactor replace_history_parameter
This function had some in/out parameters that were really only used as out parameters, and also an undocumented (but crucial) `id` parameter. Said `id` can only have one of two permitted values, so replace one function with a parameter by two functions that have said `id` encoded in the name. Return out-parameters by value rather than via references.
1 parent ba47c86 commit ef24d8e

File tree

4 files changed

+79
-50
lines changed

4 files changed

+79
-50
lines changed

src/goto-instrument/contracts/contracts.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,16 @@ void code_contractst::check_apply_loop_contracts(
125125
//
126126
// Assertions for assigns clause checking are inserted in the loop body.
127127

128-
// an intermediate goto_program to store generated instructions
129-
// to be inserted before the loop head
130-
goto_programt pre_loop_head_instrs;
131-
132128
// Process "loop_entry" history variables.
133129
// We find and replace all "__CPROVER_loop_entry" subexpressions in invariant.
134-
std::map<exprt, exprt> history_var_map;
135-
replace_history_parameter(
136-
symbol_table,
137-
invariant,
138-
history_var_map,
139-
loop_head_location,
140-
mode,
141-
pre_loop_head_instrs,
142-
ID_loop_entry);
130+
auto replace_history_result = replace_history_loop_entry(
131+
symbol_table, invariant, loop_head_location, mode);
132+
invariant.swap(replace_history_result.expression_after_replacement);
133+
const auto &history_var_map = replace_history_result.parameter_to_history;
134+
// an intermediate goto_program to store generated instructions
135+
// to be inserted before the loop head
136+
goto_programt &pre_loop_head_instrs =
137+
replace_history_result.history_construction;
143138

144139
// Create a temporary to track if we entered the loop,
145140
// i.e., the loop guard was satisfied.

src/goto-instrument/contracts/dynamic-frames/dfcc_instrument_loop.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,22 +269,13 @@ std::map<exprt, exprt> dfcc_instrument_loopt::add_prehead_instructions(
269269
// GOTO HEAD;
270270
// ```
271271

272-
goto_programt pre_loop_head_instrs;
273-
std::map<exprt, exprt> history_var_map;
274272

275273
// initialize loop_entry history vars;
276-
{
277-
// Process "loop_entry" history variables.
278-
// We find and replace all "__CPROVER_loop_entry" in invariant.
279-
replace_history_parameter(
280-
symbol_table,
281-
invariant,
282-
history_var_map,
283-
loop_head_location,
284-
language_mode,
285-
pre_loop_head_instrs,
286-
ID_loop_entry);
287-
}
274+
auto replace_history_result = replace_history_loop_entry(
275+
symbol_table, invariant, loop_head_location, language_mode);
276+
invariant.swap(replace_history_result.expression_after_replacement);
277+
goto_programt &pre_loop_head_instrs =
278+
replace_history_result.history_construction;
288279

289280
// entered_loop = false
290281
{
@@ -354,7 +345,7 @@ std::map<exprt, exprt> dfcc_instrument_loopt::add_prehead_instructions(
354345
goto_programt::make_goto(loop_head, true_exprt{}, loop_head_location));
355346

356347
goto_function.body.destructive_insert(loop_head, pre_loop_head_instrs);
357-
return history_var_map;
348+
return replace_history_result.parameter_to_history;
358349
}
359350

360351
goto_programt::instructiont::targett

src/goto-instrument/contracts/utils.cpp

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -450,18 +450,18 @@ void add_quantified_variable(
450450
}
451451
}
452452

453-
void replace_history_parameter(
453+
static void replace_history_parameter_rec(
454454
symbol_table_baset &symbol_table,
455455
exprt &expr,
456456
std::map<exprt, exprt> &parameter2history,
457-
source_locationt location,
457+
const source_locationt &location,
458458
const irep_idt &mode,
459459
goto_programt &history,
460460
const irep_idt &id)
461461
{
462462
for(auto &op : expr.operands())
463463
{
464-
replace_history_parameter(
464+
replace_history_parameter_rec(
465465
symbol_table, op, parameter2history, location, mode, history, id);
466466
}
467467

@@ -527,25 +527,56 @@ void replace_history_parameter(
527527
}
528528
}
529529

530+
replace_history_parametert replace_history_old(
531+
symbol_table_baset &symbol_table,
532+
const exprt &expr,
533+
const source_locationt &location,
534+
const irep_idt &mode)
535+
{
536+
replace_history_parametert result;
537+
result.expression_after_replacement = expr;
538+
replace_history_parameter_rec(
539+
symbol_table,
540+
result.expression_after_replacement,
541+
result.parameter_to_history,
542+
location,
543+
mode,
544+
result.history_construction,
545+
ID_old);
546+
return result;
547+
}
548+
549+
replace_history_parametert replace_history_loop_entry(
550+
symbol_table_baset &symbol_table,
551+
const exprt &expr,
552+
const source_locationt &location,
553+
const irep_idt &mode)
554+
{
555+
replace_history_parametert result;
556+
result.expression_after_replacement = expr;
557+
replace_history_parameter_rec(
558+
symbol_table,
559+
result.expression_after_replacement,
560+
result.parameter_to_history,
561+
location,
562+
mode,
563+
result.history_construction,
564+
ID_loop_entry);
565+
return result;
566+
}
567+
530568
void generate_history_variables_initialization(
531569
symbol_table_baset &symbol_table,
532570
exprt &clause,
533571
const irep_idt &mode,
534572
goto_programt &program)
535573
{
536-
std::map<exprt, exprt> parameter2history;
537-
goto_programt history;
538574
// Find and replace "old" expression in the "expression" variable
539-
replace_history_parameter(
540-
symbol_table,
541-
clause,
542-
parameter2history,
543-
clause.source_location(),
544-
mode,
545-
history,
546-
ID_old);
575+
auto result =
576+
replace_history_old(symbol_table, clause, clause.source_location(), mode);
577+
clause.swap(result.expression_after_replacement);
547578
// Add all the history variable initialization instructions
548-
program.destructive_append(history);
579+
program.destructive_append(result.history_construction);
549580
}
550581

551582
bool is_transformed_loop_head(const goto_programt::const_targett &target)

src/goto-instrument/contracts/utils.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,28 @@ void add_quantified_variable(
242242
exprt &expression,
243243
const irep_idt &mode);
244244

245-
/// This function recursively identifies the "old" expressions within expr
245+
struct replace_history_parametert
246+
{
247+
exprt expression_after_replacement;
248+
std::map<exprt, exprt> parameter_to_history;
249+
goto_programt history_construction;
250+
};
251+
252+
/// This function recursively identifies the "old" expressions within \p expr
246253
/// and replaces them with corresponding history variables.
247-
void replace_history_parameter(
254+
replace_history_parametert replace_history_old(
248255
symbol_table_baset &symbol_table,
249-
exprt &expr,
250-
std::map<exprt, exprt> &parameter2history,
251-
source_locationt location,
252-
const irep_idt &mode,
253-
goto_programt &history,
254-
const irep_idt &id);
256+
const exprt &expr,
257+
const source_locationt &location,
258+
const irep_idt &mode);
259+
260+
/// This function recursively identifies the "loop_entry" expressions within
261+
/// \p expr and replaces them with corresponding history variables.
262+
replace_history_parametert replace_history_loop_entry(
263+
symbol_table_baset &symbol_table,
264+
const exprt &expr,
265+
const source_locationt &location,
266+
const irep_idt &mode);
255267

256268
/// This function generates all the instructions required to initialize
257269
/// history variables.

0 commit comments

Comments
 (0)