Skip to content

Commit 42ad6d2

Browse files
committed
Extend global has_subexpr to take a predicate as has_subtype does
1 parent d66d542 commit 42ad6d2

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/solvers/refinement/string_refinement_util.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ bool has_char_pointer_subtype(const typet &type, const namespacet &ns)
5353

5454
bool has_char_array_subexpr(const exprt &expr, const namespacet &ns)
5555
{
56-
const auto it = std::find_if(
57-
expr.depth_begin(), expr.depth_end(), [&](const exprt &e) { // NOLINT
58-
return is_char_array_type(e.type(), ns);
59-
});
60-
return it != expr.depth_end();
56+
return has_subexpr(
57+
expr, [&](const exprt &e) { return is_char_array_type(e.type(), ns); });
6158
}
6259

6360
sparse_arrayt::sparse_arrayt(const with_exprt &expr)

src/util/expr_util.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Kroening, [email protected]
1010
#include "expr_util.h"
1111

1212
#include "expr.h"
13+
#include "expr_iterator.h"
1314
#include "fixedbv.h"
1415
#include "ieee_float.h"
1516
#include "std_expr.h"
@@ -120,16 +121,19 @@ exprt boolean_negate(const exprt &src)
120121
return not_exprt(src);
121122
}
122123

123-
bool has_subexpr(const exprt &src, const irep_idt &id)
124+
bool has_subexpr(
125+
const exprt &expr,
126+
const std::function<bool(const exprt &)> &pred)
124127
{
125-
if(src.id()==id)
126-
return true;
128+
const auto it = std::find_if(expr.depth_begin(), expr.depth_end(), pred);
129+
return it != expr.depth_end();
127130

128-
forall_operands(it, src)
129-
if(has_subexpr(*it, id))
130-
return true;
131+
}
131132

132-
return false;
133+
bool has_subexpr(const exprt &src, const irep_idt &id)
134+
{
135+
return has_subexpr(
136+
src, [&](const exprt &subexpr) { return subexpr.id() == id; });
133137
}
134138

135139
bool has_subtype(

src/util/expr_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ exprt is_not_zero(const exprt &, const namespacet &ns);
4141
/// and swapping false and true
4242
exprt boolean_negate(const exprt &);
4343

44+
/// returns true if the expression has a subexpression that satisfies pred
45+
bool has_subexpr(const exprt &, const std::function<bool(const exprt &)> &pred);
46+
4447
/// returns true if the expression has a subexpression with given ID
4548
bool has_subexpr(const exprt &, const irep_idt &);
4649

0 commit comments

Comments
 (0)