Skip to content

Commit 4171f19

Browse files
Merge pull request #1300 from LAJW/refactor/string-refinement-constructors
Refactor string refinement constructors
2 parents 5bcaacd + 0838f61 commit 4171f19

22 files changed

+448
-399
lines changed

src/cbmc/cbmc_solvers.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -146,54 +146,49 @@ std::unique_ptr<cbmc_solverst::solvert> cbmc_solverst::get_bv_refinement()
146146

147147
prop->set_message_handler(get_message_handler());
148148

149-
auto bv_refinement=util_make_unique<bv_refinementt>(ns, *prop);
150-
bv_refinement->set_ui(ui);
149+
bv_refinementt::infot info;
150+
info.ns=&ns;
151+
info.prop=prop.get();
152+
info.ui=ui;
151153

152154
// we allow setting some parameters
153-
if(options.get_option("max-node-refinement")!="")
154-
bv_refinement->max_node_refinement =
155+
if(options.get_bool_option("max-node-refinement"))
156+
info.max_node_refinement=
155157
options.get_unsigned_int_option("max-node-refinement");
156158

157-
bv_refinement->do_array_refinement =
158-
options.get_bool_option("refine-arrays");
159-
bv_refinement->do_arithmetic_refinement =
160-
options.get_bool_option("refine-arithmetic");
159+
info.refine_arrays=options.get_bool_option("refine-arrays");
160+
info.refine_arithmetic=options.get_bool_option("refine-arithmetic");
161161

162-
return util_make_unique<solvert>(std::move(bv_refinement), std::move(prop));
162+
return util_make_unique<solvert>(
163+
util_make_unique<bv_refinementt>(info),
164+
std::move(prop));
163165
}
164166

165167
/// the string refinement adds to the bit vector refinement specifications for
166168
/// functions from the Java string library
167169
/// \return a solver for cbmc
168170
std::unique_ptr<cbmc_solverst::solvert> cbmc_solverst::get_string_refinement()
169171
{
172+
string_refinementt::infot info;
173+
info.ns=&ns;
170174
auto prop=util_make_unique<satcheck_no_simplifiert>();
171175
prop->set_message_handler(get_message_handler());
172-
173-
auto string_refinement=util_make_unique<string_refinementt>(
174-
ns, *prop, MAX_NB_REFINEMENT);
175-
string_refinement->set_ui(ui);
176-
177-
string_refinement->do_concretizing=options.get_bool_option("trace");
176+
info.prop=prop.get();
177+
info.refinement_bound=MAX_NB_REFINEMENT;
178+
info.ui=ui;
178179
if(options.get_bool_option("string-max-length"))
179-
string_refinement->set_max_string_length(
180-
options.get_signed_int_option("string-max-length"));
181-
if(options.get_bool_option("string-non-empty"))
182-
string_refinement->enforce_non_empty_string();
183-
if(options.get_bool_option("string-printable"))
184-
string_refinement->enforce_printable_characters();
185-
186-
if(options.get_option("max-node-refinement")!="")
187-
string_refinement->max_node_refinement=
180+
info.string_max_length=options.get_signed_int_option("string-max-length");
181+
info.string_non_empty=options.get_bool_option("string-non-empty");
182+
info.trace=options.get_bool_option("trace");
183+
info.string_printable=options.get_bool_option("string-printable");
184+
if(options.get_bool_option("max-node-refinement"))
185+
info.max_node_refinement=
188186
options.get_unsigned_int_option("max-node-refinement");
189-
190-
string_refinement->do_array_refinement=
191-
options.get_bool_option("refine-arrays");
192-
string_refinement->do_arithmetic_refinement=
193-
options.get_bool_option("refine-arithmetic");
187+
info.refine_arrays=options.get_bool_option("refine-arrays");
188+
info.refine_arithmetic=options.get_bool_option("refine-arithmetic");
194189

195190
return util_make_unique<solvert>(
196-
std::move(string_refinement), std::move(prop));
191+
util_make_unique<string_refinementt>(info), std::move(prop));
197192
}
198193

199194
std::unique_ptr<cbmc_solverst::solvert> cbmc_solverst::get_smt1(

src/solvers/refinement/bv_refinement.h

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,44 @@ Author: Daniel Kroening, [email protected]
2121
class bv_refinementt:public bv_pointerst
2222
{
2323
public:
24-
bv_refinementt(const namespacet &_ns, propt &_prop);
25-
~bv_refinementt();
24+
struct infot
25+
{
26+
const namespacet *ns=nullptr;
27+
propt *prop=nullptr;
28+
language_uit::uit ui=language_uit::uit::PLAIN;
29+
/// Max number of times we refine a formula node
30+
unsigned max_node_refinement=5;
31+
/// Enable array refinement
32+
bool refine_arrays=true;
33+
/// Enable arithmetic refinement
34+
bool refine_arithmetic=true;
35+
};
2636

27-
virtual decision_proceduret::resultt dec_solve();
37+
explicit bv_refinementt(const infot &info);
2838

29-
virtual std::string decision_procedure_text() const
39+
decision_proceduret::resultt dec_solve() override;
40+
41+
std::string decision_procedure_text() const override
3042
{
3143
return "refinement loop with "+prop.solver_text();
3244
}
3345

34-
// NOLINTNEXTLINE(readability/identifiers)
35-
typedef bv_pointerst SUB;
36-
37-
// maximal number of times we refine a formula node
38-
unsigned max_node_refinement;
39-
// enable/disable refinements
40-
bool do_array_refinement;
41-
bool do_arithmetic_refinement;
46+
protected:
4247

43-
using bv_pointerst::is_in_conflict;
48+
// Refine array
49+
void post_process_arrays() override;
4450

45-
void set_ui(language_uit::uit _ui) { ui=_ui; }
51+
// Refine arithmetic
52+
bvt convert_mult(const exprt &expr) override;
53+
bvt convert_div(const div_exprt &expr) override;
54+
bvt convert_mod(const mod_exprt &expr) override;
55+
bvt convert_floatbv_op(const exprt &expr) override;
4656

47-
protected:
48-
resultt prop_solve();
57+
void set_assumptions(const bvt &_assumptions) override;
4958

59+
private:
5060
// the list of operator approximations
51-
struct approximationt
61+
struct approximationt final
5262
{
5363
public:
5464
explicit approximationt(std::size_t _id_nr):
@@ -79,39 +89,30 @@ class bv_refinementt:public bv_pointerst
7989
std::size_t id_nr;
8090
};
8191

82-
typedef std::list<approximationt> approximationst;
83-
approximationst approximations;
84-
92+
resultt prop_solve();
8593
approximationt &add_approximation(const exprt &expr, bvt &bv);
94+
bool conflicts_with(approximationt &approximation);
8695
void check_SAT(approximationt &approximation);
8796
void check_UNSAT(approximationt &approximation);
8897
void initialize(approximationt &approximation);
8998
void get_values(approximationt &approximation);
90-
bool is_in_conflict(approximationt &approximation);
91-
92-
virtual void check_SAT();
93-
virtual void check_UNSAT();
94-
bool progress;
95-
96-
// we refine the theory of arrays
97-
virtual void post_process_arrays();
99+
void check_SAT();
100+
void check_UNSAT();
98101
void arrays_overapproximated();
99102
void freeze_lazy_constraints();
100103

101-
// we refine expensive arithmetic
102-
virtual bvt convert_mult(const exprt &expr);
103-
virtual bvt convert_div(const div_exprt &expr);
104-
virtual bvt convert_mod(const mod_exprt &expr);
105-
virtual bvt convert_floatbv_op(const exprt &expr);
106-
107-
// for collecting statistics
108-
virtual void set_to(const exprt &expr, bool value);
109-
110-
// overloading
111-
virtual void set_assumptions(const bvt &_assumptions);
104+
// MEMBERS
112105

106+
// Maximum number of times we refine a formula node
107+
const unsigned max_node_refinement;
108+
// Refinement toggles
109+
const bool do_array_refinement;
110+
const bool do_arithmetic_refinement;
111+
bool progress;
112+
std::vector<approximationt> approximations;
113113
bvt parent_assumptions;
114114

115+
protected:
115116
// use gui format
116117
language_uit::uit ui;
117118
};

src/solvers/refinement/bv_refinement_loop.cpp

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,20 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include <util/xml.h>
1414

15-
bv_refinementt::bv_refinementt(
16-
const namespacet &_ns, propt &_prop):
17-
bv_pointerst(_ns, _prop),
18-
max_node_refinement(5),
19-
do_array_refinement(true),
20-
do_arithmetic_refinement(true),
15+
bv_refinementt::bv_refinementt(const infot &info):
16+
bv_pointerst(*info.ns, *info.prop),
17+
max_node_refinement(info.max_node_refinement),
18+
do_array_refinement(info.refine_arrays),
19+
do_arithmetic_refinement(info.refine_arithmetic),
2120
progress(false),
22-
ui(ui_message_handlert::uit::PLAIN)
21+
ui(info.ui)
2322
{
2423
// check features we need
2524
PRECONDITION(prop.has_set_assumptions());
2625
PRECONDITION(prop.has_set_to());
2726
PRECONDITION(prop.has_is_in_conflict());
2827
}
2928

30-
bv_refinementt::~bv_refinementt()
31-
{
32-
}
33-
3429
decision_proceduret::resultt bv_refinementt::dec_solve()
3530
{
3631
// do the usual post-processing
@@ -96,17 +91,16 @@ decision_proceduret::resultt bv_refinementt::prop_solve()
9691
// this puts the underapproximations into effect
9792
bvt assumptions=parent_assumptions;
9893

99-
for(approximationst::const_iterator
100-
a_it=approximations.begin();
101-
a_it!=approximations.end();
102-
a_it++)
94+
for(const approximationt &approximation : approximations)
10395
{
10496
assumptions.insert(
10597
assumptions.end(),
106-
a_it->over_assumptions.begin(), a_it->over_assumptions.end());
98+
approximation.over_assumptions.begin(),
99+
approximation.over_assumptions.end());
107100
assumptions.insert(
108101
assumptions.end(),
109-
a_it->under_assumptions.begin(), a_it->under_assumptions.end());
102+
approximation.under_assumptions.begin(),
103+
approximation.under_assumptions.end());
110104
}
111105

112106
prop.set_assumptions(assumptions);
@@ -127,40 +121,16 @@ void bv_refinementt::check_SAT()
127121

128122
arrays_overapproximated();
129123

130-
for(approximationst::iterator
131-
a_it=approximations.begin();
132-
a_it!=approximations.end();
133-
a_it++)
134-
check_SAT(*a_it);
124+
for(approximationt &approximation : this->approximations)
125+
check_SAT(approximation);
135126
}
136127

137128
void bv_refinementt::check_UNSAT()
138129
{
139130
progress=false;
140131

141-
for(approximationst::iterator
142-
a_it=approximations.begin();
143-
a_it!=approximations.end();
144-
a_it++)
145-
check_UNSAT(*a_it);
146-
}
147-
148-
void bv_refinementt::set_to(const exprt &expr, bool value)
149-
{
150-
#if 0
151-
unsigned prev=prop.no_variables();
152-
SUB::set_to(expr, value);
153-
unsigned n=prop.no_variables()-prev;
154-
std::cout << n << " EEE " << expr.id() << "@" << expr.type().id();
155-
forall_operands(it, expr)
156-
std::cout << " " << it->id() << "@" << it->type().id();
157-
if(expr.id()=="=" && expr.operands().size()==2)
158-
forall_operands(it, expr.op1())
159-
std::cout << " " << it->id() << "@" << it->type().id();
160-
std::cout << '\n';
161-
#else
162-
SUB::set_to(expr, value);
163-
#endif
132+
for(approximationt &approximation : this->approximations)
133+
check_UNSAT(approximation);
164134
}
165135

166136
void bv_refinementt::set_assumptions(const bvt &_assumptions)

src/solvers/refinement/refine_arithmetic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ void bv_refinementt::check_SAT(approximationt &a)
373373
void bv_refinementt::check_UNSAT(approximationt &a)
374374
{
375375
// part of the conflict?
376-
if(!is_in_conflict(a))
376+
if(!this->conflicts_with(a))
377377
return;
378378

379379
status() << "Found assumption for `" << a.as_string()
@@ -458,7 +458,7 @@ void bv_refinementt::check_UNSAT(approximationt &a)
458458
}
459459

460460
/// check if an under-approximation is part of the conflict
461-
bool bv_refinementt::is_in_conflict(approximationt &a)
461+
bool bv_refinementt::conflicts_with(approximationt &a)
462462
{
463463
for(std::size_t i=0; i<a.under_assumptions.size(); i++)
464464
if(prop.is_in_conflict(a.under_assumptions[i]))

src/solvers/refinement/string_constraint.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ extern inline string_constraintt &to_string_constraint(exprt &expr)
144144
/// \param [in] identifier: identifier for `from_expr`
145145
/// \param [in] expr: constraint to render
146146
/// \return rendered string
147-
inline static std::string from_expr(
147+
inline std::string from_expr(
148148
const namespacet &ns,
149149
const irep_idt &identifier,
150150
const string_constraintt &expr)
@@ -218,7 +218,7 @@ class string_not_contains_constraintt: public exprt
218218
/// \param [in] identifier: identifier for `from_expr`
219219
/// \param [in] expr: constraint to render
220220
/// \return rendered string
221-
inline static std::string from_expr(
221+
inline std::string from_expr(
222222
const namespacet &ns,
223223
const irep_idt &identifier,
224224
const string_not_contains_constraintt &expr)

0 commit comments

Comments
 (0)