Skip to content

Commit c3aed85

Browse files
Define two helper functions for for_each_successor
These two functions deal with one type of node. Type safety ensures we don't mix the two kinds of node, so these two functions can be made public.
1 parent a45c64f commit c3aed85

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

src/solvers/refinement/string_refinement_util.cpp

+44-32
Original file line numberDiff line numberDiff line change
@@ -336,47 +336,59 @@ bool add_node(
336336
return true;
337337
}
338338

339-
void string_dependenciest::for_each_successor(
340-
const nodet &node,
341-
const std::function<void(const nodet &)> &f) const
339+
void string_dependenciest::for_each_dependency(
340+
const builtin_function_nodet &node,
341+
const std::function<void(const string_nodet &)> &f) const
342342
{
343-
if(node.kind == nodet::BUILTIN)
343+
for(const auto &s : node.data->string_arguments())
344344
{
345-
const auto &builtin = builtin_function_nodes[node.index];
346-
for(const auto &s : builtin.data->string_arguments())
345+
std::vector<std::reference_wrapper<const exprt>> stack({s});
346+
while(!stack.empty())
347347
{
348-
std::vector<std::reference_wrapper<const exprt>> stack({s});
349-
while(!stack.empty())
348+
const auto current = stack.back();
349+
stack.pop_back();
350+
if(const auto if_expr = expr_try_dynamic_cast<if_exprt>(current.get()))
350351
{
351-
const auto current = stack.back();
352-
stack.pop_back();
353-
if(const auto if_expr = expr_try_dynamic_cast<if_exprt>(current.get()))
354-
{
355-
stack.emplace_back(if_expr->true_case());
356-
stack.emplace_back(if_expr->false_case());
357-
}
358-
else if(const auto node = node_at(to_array_string_expr(current)))
359-
f(nodet(*node));
360-
else
361-
{
362-
std::cout << "Warning no node for " << format(current) << std::endl;
363-
//UNREACHABLE;
364-
}
352+
stack.emplace_back(if_expr->true_case());
353+
stack.emplace_back(if_expr->false_case());
354+
}
355+
else if(const auto string_node = node_at(to_array_string_expr(current)))
356+
f(*string_node);
357+
else
358+
{
359+
std::cout << "Warning no node for " << format(current) << std::endl;
360+
//UNREACHABLE;
365361
}
366362
}
367363
}
368-
else if(node.kind == nodet::STRING)
364+
}
365+
366+
void string_dependenciest::for_each_dependency(
367+
const string_nodet &node,
368+
const std::function<void(const builtin_function_nodet &)> &f) const
369+
{
370+
for(const std::size_t &index : node.dependencies)
371+
f(builtin_function_nodes[index]);
372+
}
373+
374+
void string_dependenciest::for_each_successor(
375+
const nodet &node,
376+
const std::function<void(const nodet &)> &f) const
377+
{
378+
switch(node.kind)
369379
{
370-
const auto &s_node = string_nodes[node.index];
371-
std::for_each(
372-
s_node.dependencies.begin(),
373-
s_node.dependencies.end(),
374-
[&](const std::size_t &index) { // NOLINT
375-
f(nodet(builtin_function_nodes[index]));
376-
});
380+
case nodet::BUILTIN:
381+
for_each_dependency(
382+
builtin_function_nodes[node.index],
383+
[&](const string_nodet &n) { return f(nodet(n)); });
384+
break;
385+
386+
case nodet::STRING:
387+
for_each_dependency(
388+
string_nodes[node.index],
389+
[&](const builtin_function_nodet &n) { return f(nodet(n)); });
390+
break;
377391
}
378-
else
379-
UNREACHABLE;
380392
}
381393

382394
void string_dependenciest::for_each_node(

src/solvers/refinement/string_refinement_util.h

+8
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ class string_dependenciest
217217
const array_string_exprt &e,
218218
const builtin_function_nodet &builtin_function);
219219

220+
/// Applies `f` to each node on which `node` depends
221+
void for_each_dependency(
222+
const string_nodet &node,
223+
const std::function<void(const builtin_function_nodet &)> &f) const;
224+
void for_each_dependency(
225+
const builtin_function_nodet &node,
226+
const std::function<void(const string_nodet &)> &f) const;
227+
220228
/// Attempt to evaluate the given string from the dependencies and valuation
221229
/// of strings on which it depends
222230
/// Warning: eval uses a cache which must be cleaned everytime the valuations

0 commit comments

Comments
 (0)