Skip to content

Commit 50a9ef1

Browse files
Refactor path strategy chooser into free functions
Since keeping an instance is not an option, we should avoid having to create an instance on every stateless function call.
1 parent 6ce9bf4 commit 50a9ef1

File tree

7 files changed

+58
-82
lines changed

7 files changed

+58
-82
lines changed

jbmc/src/jbmc/jbmc_parse_options.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void jbmc_parse_optionst::get_command_line_options(optionst &options)
119119

120120
if(cmdline.isset("show-symex-strategies"))
121121
{
122-
status() << path_strategy_choosert().show_strategies() << eom;
122+
status() << show_path_strategies() << eom;
123123
exit(CPROVER_EXIT_SUCCESS);
124124
}
125125

@@ -573,7 +573,6 @@ int jbmc_parse_optionst::doit()
573573
// The `configure_bmc` callback passed will enable enum-unwind-static if
574574
// applicable.
575575
return bmct::do_language_agnostic_bmc(
576-
path_strategy_choosert(),
577576
options,
578577
goto_model,
579578
ui_message_handler,
@@ -618,7 +617,6 @@ int jbmc_parse_optionst::doit()
618617
// The `configure_bmc` callback passed will enable enum-unwind-static if
619618
// applicable.
620619
return bmct::do_language_agnostic_bmc(
621-
path_strategy_choosert(),
622620
options,
623621
lazy_goto_model,
624622
ui_message_handler,

src/cbmc/bmc.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ safety_checkert::resultt bmct::stop_on_fail()
255255

256256
/// Perform core BMC, using an abstract model to supply GOTO function bodies
257257
/// (perhaps created on demand).
258-
/// \param path_strategy_chooser: controls whether symex generates a single
259-
/// large equation for the whole program or an equation per path
260258
/// \param opts: command-line options affecting BMC
261259
/// \param model: provides goto function bodies and the symbol table, perhaps
262260
/// creating those function bodies on demand.
@@ -266,7 +264,6 @@ safety_checkert::resultt bmct::stop_on_fail()
266264
/// \param callback_after_symex: optional callback to be run after symex.
267265
/// See class member `bmct::driver_callback_after_symex` for details.
268266
int bmct::do_language_agnostic_bmc(
269-
const path_strategy_choosert &path_strategy_chooser,
270267
const optionst &opts,
271268
abstract_goto_modelt &model,
272269
ui_message_handlert &ui,
@@ -279,10 +276,7 @@ int bmct::do_language_agnostic_bmc(
279276
messaget message(ui);
280277
std::unique_ptr<path_storaget> worklist;
281278
std::string strategy = opts.get_option("exploration-strategy");
282-
INVARIANT(
283-
path_strategy_chooser.is_valid_strategy(strategy),
284-
"Front-end passed us invalid path strategy '" + strategy + "'");
285-
worklist = path_strategy_chooser.get(strategy);
279+
worklist = get_path_strategy(strategy);
286280
try
287281
{
288282
{

src/cbmc/bmc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ class bmct:public safety_checkert
118118
}
119119

120120
static int do_language_agnostic_bmc(
121-
const path_strategy_choosert &path_strategy_chooser,
122121
const optionst &opts,
123122
abstract_goto_modelt &goto_model,
124123
ui_message_handlert &ui,

src/cbmc/cbmc_parse_options.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void cbmc_parse_optionst::get_command_line_options(optionst &options)
143143

144144
if(cmdline.isset("show-symex-strategies"))
145145
{
146-
status() << path_strategy_choosert().show_strategies() << eom;
146+
status() << show_path_strategies() << eom;
147147
exit(CPROVER_EXIT_SUCCESS);
148148
}
149149

@@ -554,7 +554,7 @@ int cbmc_parse_optionst::doit()
554554
}
555555

556556
return bmct::do_language_agnostic_bmc(
557-
path_strategy_choosert(), options, goto_model, ui_message_handler);
557+
options, goto_model, ui_message_handler);
558558
}
559559

560560
bool cbmc_parse_optionst::set_properties()

src/goto-symex/path_storage.cpp

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,66 @@ void path_fifot::clear()
8282
// _____________________________________________________________________________
8383
// path_strategy_choosert
8484

85-
std::string path_strategy_choosert::show_strategies() const
85+
static const std::map<
86+
const std::string,
87+
std::pair<
88+
const std::string,
89+
const std::function<std::unique_ptr<path_storaget>()>>>
90+
path_strategies(
91+
{{"lifo",
92+
{" lifo next instruction is pushed before\n"
93+
" goto target; paths are popped in\n"
94+
" last-in, first-out order. Explores\n"
95+
" the program tree depth-first.\n",
96+
[]() { // NOLINT(whitespace/braces)
97+
return util_make_unique<path_lifot>();
98+
}}},
99+
{"fifo",
100+
{" fifo next instruction is pushed before\n"
101+
" goto target; paths are popped in\n"
102+
" first-in, first-out order. Explores\n"
103+
" the program tree breadth-first.\n",
104+
[]() { // NOLINT(whitespace/braces)
105+
return util_make_unique<path_fifot>();
106+
}}}});
107+
108+
std::string show_path_strategies()
86109
{
87110
std::stringstream ss;
88-
for(auto &pair : strategies)
111+
for(auto &pair : path_strategies)
89112
ss << pair.second.first;
90113
return ss.str();
91114
}
92115

116+
std::string default_path_strategy()
117+
{
118+
return "lifo";
119+
}
120+
121+
bool is_valid_path_strategy(const std::string strategy)
122+
{
123+
return path_strategies.find(strategy) != path_strategies.end();
124+
}
125+
126+
std::unique_ptr<path_storaget> get_path_strategy(const std::string strategy)
127+
{
128+
auto found = path_strategies.find(strategy);
129+
INVARIANT(
130+
found != path_strategies.end(), "Unknown strategy '" + strategy + "'.");
131+
return found->second.second();
132+
}
133+
93134
void parse_path_strategy_options(
94135
const cmdlinet &cmdline,
95136
optionst &options,
96137
message_handlert &message_handler)
97138
{
98139
messaget log(message_handler);
99-
path_strategy_choosert path_strategy_chooser;
100140
if(cmdline.isset("paths"))
101141
{
102142
options.set_option("paths", true);
103143
std::string strategy = cmdline.get_value("paths");
104-
if(!path_strategy_chooser.is_valid_strategy(strategy))
144+
if(!is_valid_path_strategy(strategy))
105145
{
106146
log.error() << "Unknown strategy '" << strategy
107147
<< "'. Pass the --show-symex-strategies flag to list "
@@ -113,28 +153,6 @@ void parse_path_strategy_options(
113153
}
114154
else
115155
{
116-
options.set_option(
117-
"exploration-strategy", path_strategy_chooser.default_strategy());
156+
options.set_option("exploration-strategy", default_path_strategy());
118157
}
119158
}
120-
121-
path_strategy_choosert::path_strategy_choosert()
122-
: strategies(
123-
{{"lifo",
124-
{" lifo next instruction is pushed before\n"
125-
" goto target; paths are popped in\n"
126-
" last-in, first-out order. Explores\n"
127-
" the program tree depth-first.\n",
128-
[]() { // NOLINT(whitespace/braces)
129-
return util_make_unique<path_lifot>();
130-
}}},
131-
{"fifo",
132-
{" fifo next instruction is pushed before\n"
133-
" goto target; paths are popped in\n"
134-
" first-in, first-out order. Explores\n"
135-
" the program tree breadth-first.\n",
136-
[]() { // NOLINT(whitespace/braces)
137-
return util_make_unique<path_fifot>();
138-
}}}})
139-
{
140-
}

src/goto-symex/path_storage.h

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -125,47 +125,15 @@ class path_fifot : public path_storaget
125125
void private_pop() override;
126126
};
127127

128-
/// \brief Factory and information for path_storaget
129-
class path_strategy_choosert
130-
{
131-
public:
132-
path_strategy_choosert();
133-
134-
/// \brief suitable for displaying as a front-end help message
135-
std::string show_strategies() const;
136-
137-
/// \brief is there a factory constructor for the named strategy?
138-
bool is_valid_strategy(const std::string strategy) const
139-
{
140-
return strategies.find(strategy) != strategies.end();
141-
}
142-
143-
/// \brief Factory for a path_storaget
144-
///
145-
/// Ensure that path_strategy_choosert::is_valid_strategy() returns true for a
146-
/// particular string before calling this function on that string.
147-
std::unique_ptr<path_storaget> get(const std::string strategy) const
148-
{
149-
auto found = strategies.find(strategy);
150-
INVARIANT(
151-
found != strategies.end(), "Unknown strategy '" + strategy + "'.");
152-
return found->second.second();
153-
}
128+
/// \brief suitable for displaying as a front-end help message
129+
std::string show_path_strategies();
154130

155-
std::string default_strategy() const
156-
{
157-
return "lifo";
158-
}
131+
/// \brief is there a factory constructor for the named strategy?
132+
bool is_valid_path_strategy(const std::string strategy);
159133

160-
protected:
161-
/// Map from the name of a strategy (to be supplied on the command line), to
162-
/// the help text for that strategy and a factory thunk returning a pointer to
163-
/// a derived class of path_storaget that implements that strategy.
164-
std::map<const std::string,
165-
std::pair<const std::string,
166-
const std::function<std::unique_ptr<path_storaget>()>>>
167-
strategies;
168-
};
134+
/// Ensure that is_valid_strategy() returns true for a
135+
/// particular string before calling this function on that string.
136+
std::unique_ptr<path_storaget> get_path_strategy(const std::string strategy);
169137

170138
/// \brief add `paths` and `exploration-strategy` option, suitable to be
171139
/// invoked from front-ends.

unit/path_strategies.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,8 @@ void _check_with_strategy(
368368
mh.set_verbosity(0);
369369
messaget log(mh);
370370

371-
path_strategy_choosert chooser;
372-
REQUIRE(chooser.is_valid_strategy(strategy));
373-
std::unique_ptr<path_storaget> worklist = chooser.get(strategy);
371+
REQUIRE(is_valid_path_strategy(strategy));
372+
std::unique_ptr<path_storaget> worklist = get_path_strategy(strategy);
374373

375374
goto_modelt gm;
376375
int ret;

0 commit comments

Comments
 (0)