Skip to content

Commit 4ef4cdc

Browse files
authored
Merge pull request #4543 from diffblue/exprt_visit_pre
introduce exprt::visit_pre
2 parents 9f3b058 + 58736e3 commit 4ef4cdc

File tree

7 files changed

+54
-72
lines changed

7 files changed

+54
-72
lines changed

src/goto-programs/mm_io.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ void collect_deref_expr(
2323
const exprt &src,
2424
std::set<dereference_exprt> &dest)
2525
{
26-
if(src.id()==ID_dereference)
27-
dest.insert(to_dereference_expr(src));
28-
29-
for(const auto & op : src.operands())
30-
collect_deref_expr(op, dest); // recursive call
26+
src.visit_pre([&dest](const exprt &e) {
27+
if(e.id() == ID_dereference)
28+
dest.insert(to_dereference_expr(e));
29+
});
3130
}
3231

3332
void mm_io(

src/goto-programs/validate_goto_model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void validate_goto_modelt::check_called_functions()
179179

180180
// check functions of which the address is taken
181181
const auto &src = static_cast<const exprt &>(instr.code);
182-
src.visit(test_for_function_address);
182+
src.visit_pre(test_for_function_address);
183183
}
184184
}
185185
}

src/goto-symex/auto_objects.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,27 @@ void goto_symext::initialize_auto_object(const exprt &expr, statet &state)
7777

7878
void goto_symext::trigger_auto_object(const exprt &expr, statet &state)
7979
{
80-
if(expr.id()==ID_symbol &&
81-
expr.get_bool(ID_C_SSA_symbol))
82-
{
83-
const ssa_exprt &ssa_expr=to_ssa_expr(expr);
84-
const irep_idt &obj_identifier=ssa_expr.get_object_name();
85-
86-
if(obj_identifier != statet::guard_identifier())
80+
expr.visit_pre([&state, this](const exprt &e) {
81+
if(e.id() == ID_symbol && e.get_bool(ID_C_SSA_symbol))
8782
{
88-
const symbolt &symbol=
89-
ns.lookup(obj_identifier);
83+
const ssa_exprt &ssa_expr = to_ssa_expr(e);
84+
const irep_idt &obj_identifier = ssa_expr.get_object_name();
9085

91-
if(has_prefix(id2string(symbol.base_name), "auto_object"))
86+
if(obj_identifier != statet::guard_identifier())
9287
{
93-
// done already?
94-
if(
95-
state.get_level2().current_names.find(ssa_expr.get_identifier()) ==
96-
state.get_level2().current_names.end())
88+
const symbolt &symbol = ns.lookup(obj_identifier);
89+
90+
if(has_prefix(id2string(symbol.base_name), "auto_object"))
9791
{
98-
initialize_auto_object(expr, state);
92+
// done already?
93+
if(
94+
state.get_level2().current_names.find(ssa_expr.get_identifier()) ==
95+
state.get_level2().current_names.end())
96+
{
97+
initialize_auto_object(e, state);
98+
}
9999
}
100100
}
101101
}
102-
}
103-
104-
// recursive call
105-
forall_operands(it, expr)
106-
trigger_auto_object(*it, state);
102+
});
107103
}

src/solvers/smt2/letify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ exprt letifyt::substitute_let(const exprt &expr, const seen_expressionst &map)
9696

9797
for(auto &op : tmp.operands())
9898
{
99-
op.visit([&map](exprt &expr) {
99+
op.visit_pre([&map](exprt &expr) {
100100
seen_expressionst::const_iterator it = map.find(expr);
101101

102102
// replace subexpression by let symbol if used more than once

src/util/expr.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,16 @@ const source_locationt &exprt::find_source_location() const
243243
return static_cast<const source_locationt &>(get_nil_irep());
244244
}
245245

246-
void exprt::visit(std::function<void(exprt &)> visitor)
246+
template <typename T>
247+
static void visit_pre_template(std::function<void(T &)> visitor, T *_expr)
247248
{
248-
std::stack<exprt *> stack;
249+
std::stack<T *> stack;
249250

250-
stack.push(this);
251+
stack.push(_expr);
251252

252253
while(!stack.empty())
253254
{
254-
exprt &expr=*stack.top();
255+
T &expr = *stack.top();
255256
stack.pop();
256257

257258
visitor(expr);
@@ -261,32 +262,24 @@ void exprt::visit(std::function<void(exprt &)> visitor)
261262
}
262263
}
263264

264-
void exprt::visit(std::function<void(const exprt &)> visitor) const
265+
void exprt::visit_pre(std::function<void(exprt &)> visitor)
265266
{
266-
std::stack<const exprt *> stack;
267-
268-
stack.push(this);
269-
270-
while(!stack.empty())
271-
{
272-
const exprt &expr=*stack.top();
273-
stack.pop();
274-
275-
visitor(expr);
267+
visit_pre_template(visitor, this);
268+
}
276269

277-
for(auto &op : expr.operands())
278-
stack.push(&op);
279-
}
270+
void exprt::visit_pre(std::function<void(const exprt &)> visitor) const
271+
{
272+
visit_pre_template(visitor, this);
280273
}
281274

282275
void exprt::visit(expr_visitort &visitor)
283276
{
284-
visit([&visitor](exprt &e) { visitor(e); });
277+
visit_pre([&visitor](exprt &e) { visitor(e); });
285278
}
286279

287280
void exprt::visit(const_expr_visitort &visitor) const
288281
{
289-
visit([&visitor](const exprt &e) { visitor(e); });
282+
visit_pre([&visitor](const exprt &e) { visitor(e); });
290283
}
291284

292285
depth_iteratort exprt::depth_begin()

src/util/expr.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,13 @@ class exprt:public irept
324324
}
325325

326326
public:
327+
/// These are pre-order traversal visitors, i.e.,
328+
/// the visitor is executed on a node _before_ its children
329+
/// have been visited.
327330
void visit(class expr_visitort &visitor);
328331
void visit(class const_expr_visitort &visitor) const;
329-
void visit(std::function<void(exprt &)>);
330-
void visit(std::function<void(const exprt &)>) const;
332+
void visit_pre(std::function<void(exprt &)>);
333+
void visit_pre(std::function<void(const exprt &)>) const;
331334

332335
depth_iteratort depth_begin();
333336
depth_iteratort depth_end();

src/util/find_symbols.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ void find_symbols(
2626
bool current,
2727
bool next)
2828
{
29-
if(src.id() == ID_symbol && current)
30-
dest.insert(to_symbol_expr(src).get_identifier());
31-
else if(src.id() == ID_next_symbol && next)
32-
dest.insert(src.get(ID_identifier));
33-
else
34-
{
35-
forall_operands(it, src)
36-
find_symbols(*it, dest, current, next);
37-
}
29+
src.visit_pre([&dest, current, next](const exprt &e) {
30+
if(e.id() == ID_symbol && current)
31+
dest.insert(to_symbol_expr(e).get_identifier());
32+
else if(e.id() == ID_next_symbol && next)
33+
dest.insert(e.get(ID_identifier));
34+
});
3835
}
3936

4037
bool has_symbol(
@@ -68,26 +65,20 @@ void find_symbols(
6865
const exprt &src,
6966
std::set<exprt> &dest)
7067
{
71-
if(src.id()==ID_symbol || src.id()==ID_next_symbol)
72-
dest.insert(src);
73-
else
74-
{
75-
forall_operands(it, src)
76-
find_symbols(*it, dest);
77-
}
68+
src.visit_pre([&dest](const exprt &e) {
69+
if(e.id() == ID_symbol || e.id() == ID_next_symbol)
70+
dest.insert(e);
71+
});
7872
}
7973

8074
void find_symbols(
8175
const exprt &src,
8276
std::set<symbol_exprt> &dest)
8377
{
84-
if(src.id()==ID_symbol)
85-
dest.insert(to_symbol_expr(src));
86-
else
87-
{
88-
forall_operands(it, src)
89-
find_symbols(*it, dest);
90-
}
78+
src.visit_pre([&dest](const exprt &e) {
79+
if(e.id() == ID_symbol)
80+
dest.insert(to_symbol_expr(e));
81+
});
9182
}
9283

9384
void find_symbols(kindt kind, const typet &src, find_symbols_sett &dest);

0 commit comments

Comments
 (0)