Skip to content

Commit a151280

Browse files
authored
Merge pull request #4267 from diffblue/instructiont-apply
instructiont::apply
2 parents 5284894 + 11e0f6c commit a151280

File tree

9 files changed

+73
-46
lines changed

9 files changed

+73
-46
lines changed

src/analyses/dirty.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ Date: March 2013
1717

1818
void dirtyt::build(const goto_functiont &goto_function)
1919
{
20-
forall_goto_program_instructions(it, goto_function.body)
21-
{
22-
find_dirty(it->code);
23-
24-
if(it->has_condition())
25-
find_dirty(it->get_condition());
26-
}
20+
for(const auto &i : goto_function.body.instructions)
21+
i.apply([this](const exprt &e) { find_dirty(e); });
2722
}
2823

2924
void dirtyt::find_dirty(const exprt &expr)

src/analyses/interval_analysis.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ void instrument_intervals(
3232
{
3333
std::set<symbol_exprt> symbols;
3434

35-
forall_goto_program_instructions(i_it, goto_function.body)
36-
{
37-
find_symbols(i_it->code, symbols);
38-
find_symbols(i_it->guard, symbols);
39-
}
35+
for(const auto &i : goto_function.body.instructions)
36+
i.apply([&symbols](const exprt &e) { find_symbols(e, symbols); });
4037

4138
Forall_goto_program_instructions(i_it, goto_function.body)
4239
{

src/analyses/local_safe_pointers.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,20 @@ void local_safe_pointerst::output_safe_dereferences(
240240
{
241241
out << "{";
242242
bool first = true;
243-
for(auto subexpr_it = i_it->code.depth_begin(),
244-
subexpr_end = i_it->code.depth_end();
245-
subexpr_it != subexpr_end;
246-
++subexpr_it)
247-
{
248-
if(subexpr_it->id() == ID_dereference)
243+
i_it->apply([&first, &out](const exprt &e) {
244+
for(auto subexpr_it = e.depth_begin(), subexpr_end = e.depth_end();
245+
subexpr_it != subexpr_end;
246+
++subexpr_it)
249247
{
250-
if(!first)
251-
out << ", ";
252-
first = true;
253-
format_rec(out, subexpr_it->op0());
248+
if(subexpr_it->id() == ID_dereference)
249+
{
250+
if(!first)
251+
out << ", ";
252+
first = true;
253+
format_rec(out, to_dereference_expr(*subexpr_it).pointer());
254+
}
254255
}
255-
}
256+
});
256257
out << "}";
257258
}
258259

src/goto-instrument/concurrency.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,7 @@ void concurrency_instrumentationt::collect(
186186
forall_goto_program_instructions(i_it, goto_program)
187187
{
188188
if(is_threaded(i_it))
189-
{
190-
if(i_it->is_assign())
191-
collect(i_it->code);
192-
else if(i_it->is_assume() || i_it->is_assert() || i_it->is_goto())
193-
collect(i_it->guard);
194-
else if(i_it->is_function_call())
195-
collect(i_it->code);
196-
}
189+
i_it->apply([this](const exprt &e) { collect(e); });
197190
}
198191
}
199192

src/goto-instrument/full_slicer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ void full_slicert::add_decl_dead(
6666
return;
6767

6868
find_symbols_sett syms;
69-
find_symbols(node.PC->code, syms);
70-
find_symbols(node.PC->guard, syms);
69+
70+
node.PC->apply([&syms](const exprt &e) { find_symbols(e, syms); });
7171

7272
for(find_symbols_sett::const_iterator
7373
it=syms.begin();

src/goto-programs/compute_called_functions.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ void compute_address_taken_functions(
5454
const goto_programt &goto_program,
5555
std::unordered_set<irep_idt> &address_taken)
5656
{
57-
forall_goto_program_instructions(it, goto_program)
57+
for(const auto &i : goto_program.instructions)
5858
{
59-
if(it->has_condition())
60-
compute_address_taken_functions(it->get_condition(), address_taken);
61-
62-
compute_address_taken_functions(it->code, address_taken);
59+
i.apply([&address_taken](const exprt &expr) {
60+
compute_address_taken_functions(expr, address_taken);
61+
});
6362
}
6463
}
6564

src/goto-programs/goto_program.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,48 @@ void goto_programt::instructiont::transform(
992992
}
993993
}
994994

995+
void goto_programt::instructiont::apply(
996+
std::function<void(const exprt &)> f) const
997+
{
998+
switch(type)
999+
{
1000+
case OTHER:
1001+
if(get_other().get_statement() == ID_expression)
1002+
f(to_code_expression(get_other()).expression());
1003+
break;
1004+
1005+
case RETURN:
1006+
f(get_return().return_value());
1007+
break;
1008+
1009+
case ASSIGN:
1010+
f(get_assign().lhs());
1011+
f(get_assign().rhs());
1012+
break;
1013+
1014+
case DECL:
1015+
f(get_decl().symbol());
1016+
break;
1017+
1018+
case DEAD:
1019+
f(get_dead().symbol());
1020+
break;
1021+
1022+
case FUNCTION_CALL:
1023+
{
1024+
const auto &call = get_function_call();
1025+
f(call.lhs());
1026+
for(auto &a : call.arguments())
1027+
f(a);
1028+
}
1029+
break;
1030+
1031+
default:
1032+
if(has_condition())
1033+
f(get_condition());
1034+
}
1035+
}
1036+
9951037
bool goto_programt::equals(const goto_programt &other) const
9961038
{
9971039
if(instructions.size() != other.instructions.size())

src/goto-programs/goto_program.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ class goto_programt
559559
/// Apply given transformer to all expressions; no return value
560560
/// means no change needed.
561561
void transform(std::function<optionalt<exprt>(exprt)>);
562+
563+
/// Apply given function to all expressions
564+
void apply(std::function<void(const exprt &)>) const;
562565
};
563566

564567
// Never try to change this to vector-we mutate the list while iterating

src/goto-programs/slice_global_inits.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,19 @@ void slice_global_inits(goto_modelt &goto_model)
6161
if(it == goto_functions.function_map.end())
6262
continue;
6363

64-
const goto_programt &goto_program = it->second.body;
65-
66-
forall_goto_program_instructions(i_it, goto_program)
64+
for(const auto &i : it->second.body.instructions)
6765
{
68-
const codet &code = i_it->code;
69-
find_symbols(code, symbols, true, false);
70-
71-
if(i_it->has_condition())
72-
find_symbols(i_it->get_condition(), symbols, true, false);
66+
i.apply([&symbols](const exprt &expr) {
67+
find_symbols(expr, symbols, true, false);
68+
});
7369
}
7470
}
7571

7672
// now remove unnecessary initializations
7773

7874
goto_functionst::function_mapt::iterator f_it;
7975
f_it=goto_functions.function_map.find(INITIALIZE_FUNCTION);
76+
8077
if(f_it == goto_functions.function_map.end())
8178
throw incorrect_goto_program_exceptiont("initialize function not found");
8279

0 commit comments

Comments
 (0)