Skip to content

Commit 9312a53

Browse files
committed
Move combined smt sub-process and dumping into separate class
In order to deduplicate code which had been cut and paste into 2 classes.
1 parent 123f79b commit 9312a53

File tree

3 files changed

+83
-34
lines changed

3 files changed

+83
-34
lines changed

src/goto-checker/solver_factory.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,20 @@ solver_factoryt::get_incremental_smt2(std::string solver_command)
382382
solver_process = util_make_unique<smt_incremental_dry_run_solvert>(
383383
on_std_out ? std::cout : *outfile, std::move(outfile));
384384
}
385-
else
385+
else if(!dump_smt_formula.empty())
386386
{
387-
const auto out_filename = options.get_option("dump-smt-formula");
388-
389387
// If no out_filename is provided `open_outfile_and_check` will return
390388
// `nullptr`, and the solver will work normally without any logging.
391-
solver_process = util_make_unique<smt_piped_solver_processt>(
389+
solver_process = util_make_unique<smt_piped_solver_process_with_dumpt>(
392390
std::move(solver_command),
393391
message_handler,
394392
open_outfile_and_check(
395-
out_filename, message_handler, "--dump-smt-formula"));
393+
dump_smt_formula, message_handler, "--dump-smt-formula"));
394+
}
395+
else
396+
{
397+
solver_process = util_make_unique<smt_piped_solver_processt>(
398+
std::move(solver_command), message_handler);
396399
}
397400

398401
return util_make_unique<solvert>(

src/solvers/smt2_incremental/smt_solver_process.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
#include <util/invariant.h>
1010
#include <util/string_utils.h>
1111

12+
void smt_base_solver_processt::send(const smt_commandt &smt_command)
13+
{
14+
const std::string command_string = smt_to_smt2_string(smt_command);
15+
send(command_string);
16+
}
17+
1218
smt_piped_solver_processt::smt_piped_solver_processt(
1319
std::string command_line,
14-
message_handlert &message_handler,
15-
std::unique_ptr<std::ostream> out_stream)
20+
message_handlert &message_handler)
1621
: command_line_description{"\"" + command_line + "\""},
17-
out_stream(std::move(out_stream)),
1822
process{split_string(command_line, ' ', false, true), message_handler},
1923
log{message_handler}
2024
{
@@ -25,20 +29,10 @@ std::string smt_piped_solver_processt::description()
2529
return command_line_description;
2630
}
2731

28-
void smt_piped_solver_processt::send(const smt_commandt &smt_command)
32+
void smt_piped_solver_processt::send(const std::string &command_string)
2933
{
30-
const std::string command_string = smt_to_smt2_string(smt_command);
3134
log.debug() << "Sending command to SMT2 solver - " << command_string
3235
<< messaget::eom;
33-
34-
if(out_stream != nullptr)
35-
{
36-
// Using `std::endl` instead of '\n' to also flush the stream as it is a
37-
// debugging functionality, to guarantee a consistent output in case of
38-
// hanging after `(check-sat)`
39-
*out_stream << command_string << std::endl;
40-
}
41-
4236
const auto response = process.send(command_string + "\n");
4337
switch(response)
4438
{
@@ -92,9 +86,9 @@ std::string smt_incremental_dry_run_solvert::description()
9286
return "SMT2 incremental dry-run";
9387
}
9488

95-
void smt_incremental_dry_run_solvert::send(const smt_commandt &smt_command)
89+
void smt_incremental_dry_run_solvert::send(const std::string &smt_command)
9690
{
97-
out_stream << smt_to_smt2_string(smt_command) << '\n';
91+
out_stream << smt_command << '\n';
9892
}
9993

10094
smt_responset smt_incremental_dry_run_solvert::receive_response(
@@ -106,3 +100,29 @@ smt_responset smt_incremental_dry_run_solvert::receive_response(
106100
// (as a `smt_sat_responset` answer will trigger).
107101
return smt_check_sat_responset{smt_unsat_responset{}};
108102
}
103+
104+
smt_piped_solver_process_with_dumpt::smt_piped_solver_process_with_dumpt(
105+
std::string command_line,
106+
message_handlert &message_handler,
107+
std::unique_ptr<std::ostream> out_stream)
108+
: smt_piped_solver_processt{std::move(command_line), message_handler},
109+
smt_incremental_dry_run_solvert{*out_stream, std::move(out_stream)}
110+
{
111+
}
112+
113+
void smt_piped_solver_process_with_dumpt::send(const std::string &command)
114+
{
115+
smt_incremental_dry_run_solvert::send(command);
116+
smt_piped_solver_processt::send(command);
117+
}
118+
119+
std::string smt_piped_solver_process_with_dumpt::description()
120+
{
121+
return smt_piped_solver_processt::description();
122+
}
123+
124+
smt_responset smt_piped_solver_process_with_dumpt::receive_response(
125+
const std::unordered_map<irep_idt, smt_identifier_termt> &identifier_table)
126+
{
127+
return smt_piped_solver_processt::receive_response(identifier_table);
128+
}

src/solvers/smt2_incremental/smt_solver_process.h

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,44 @@ class smt_base_solver_processt
2121

2222
/// \brief Converts given SMT2 command to SMT2 string and sends it to the
2323
/// solver process.
24-
virtual void send(const smt_commandt &command) = 0;
24+
virtual void send(const smt_commandt &smt_command);
2525

2626
virtual smt_responset
2727
receive_response(const std::unordered_map<irep_idt, smt_identifier_termt>
2828
&identifier_table) = 0;
2929

3030
virtual ~smt_base_solver_processt() = default;
31+
32+
protected:
33+
virtual void send(const std::string &)
34+
{
35+
}
3136
};
3237

33-
class smt_piped_solver_processt : public smt_base_solver_processt
38+
class smt_piped_solver_processt : public virtual smt_base_solver_processt
3439
{
3540
public:
3641
/// \param command_line:
3742
/// The command and arguments for invoking the smt2 solver.
3843
/// \param message_handler:
3944
/// The messaging system to be used for logging purposes.
40-
/// \param out_stream:
41-
/// Pointer to the stream to print the SMT formula. `nullptr` if no output.
4245
smt_piped_solver_processt(
4346
std::string command_line,
44-
message_handlert &message_handler,
45-
std::unique_ptr<std::ostream> out_stream);
47+
message_handlert &message_handler);
4648

4749
std::string description() override;
4850

49-
void send(const smt_commandt &smt_command) override;
50-
5151
smt_responset receive_response(
5252
const std::unordered_map<irep_idt, smt_identifier_termt> &identifier_table)
5353
override;
5454

5555
~smt_piped_solver_processt() override = default;
5656

5757
protected:
58+
void send(const std::string &command_string) override;
59+
5860
/// The command line used to start the process.
5961
std::string command_line_description;
60-
/// Pointer to the stream to print the SMT formula. `nullptr` if no output.
61-
std::unique_ptr<std::ostream> out_stream;
6262
/// The raw solver sub process.
6363
piped_processt process;
6464
/// For buffering / combining communications from the solver to cbmc.
@@ -69,7 +69,7 @@ class smt_piped_solver_processt : public smt_base_solver_processt
6969

7070
/// Class for an incremental SMT solver used in combination with `--outfile`
7171
/// argument where the solver is never run.
72-
class smt_incremental_dry_run_solvert : public smt_base_solver_processt
72+
class smt_incremental_dry_run_solvert : public virtual smt_base_solver_processt
7373
{
7474
public:
7575
/// \param message_handler:
@@ -85,8 +85,6 @@ class smt_incremental_dry_run_solvert : public smt_base_solver_processt
8585

8686
std::string description() override;
8787

88-
void send(const smt_commandt &smt_command) override;
89-
9088
/// \note This function returns always a valid unsat response.
9189
smt_responset receive_response(
9290
const std::unordered_map<irep_idt, smt_identifier_termt> &identifier_table)
@@ -95,11 +93,39 @@ class smt_incremental_dry_run_solvert : public smt_base_solver_processt
9593
~smt_incremental_dry_run_solvert() override = default;
9694

9795
protected:
96+
void send(const std::string &smt_command) override;
97+
9898
/// Pointer to the file stream to print the SMT formula. `nullptr` if output
9999
/// is to `std::cout`.
100100
std::unique_ptr<std::ostream> file_stream;
101101
/// The output stream reference to print the SMT formula to.
102102
std::ostream &out_stream;
103103
};
104104

105+
class smt_piped_solver_process_with_dumpt : public smt_piped_solver_processt,
106+
smt_incremental_dry_run_solvert
107+
{
108+
public:
109+
/// \param command_line:
110+
/// The command and arguments for invoking the smt2 solver.
111+
/// \param message_handler:
112+
/// The messaging system to be used for logging purposes.
113+
/// \param out_stream:
114+
/// Pointer to the stream to print the SMT formula.
115+
smt_piped_solver_process_with_dumpt(
116+
std::string command_line,
117+
message_handlert &message_handler,
118+
std::unique_ptr<std::ostream> out_stream);
119+
120+
void send(const std::string &smt_command) override;
121+
122+
std::string description() override;
123+
124+
smt_responset receive_response(
125+
const std::unordered_map<irep_idt, smt_identifier_termt> &identifier_table)
126+
override;
127+
128+
~smt_piped_solver_process_with_dumpt() override = default;
129+
};
130+
105131
#endif // CPROVER_SOLVERS_SMT2_INCREMENTAL_SMT_SOLVER_PROCESS_H

0 commit comments

Comments
 (0)