Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce output_filet #588

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ebmc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SRC = \
live_signal.cpp \
main.cpp \
neural_liveness.cpp \
output_file.cpp \
output_verilog.cpp \
random_traces.cpp \
ranking_function.cpp \
Expand Down
18 changes: 5 additions & 13 deletions src/ebmc/ebmc_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Author: Daniel Kroening, [email protected]
#include <util/config.h>
#include <util/expr_util.h>
#include <util/string2int.h>
#include <util/unicode.h>

#include <solvers/prop/literal_expr.h>
#include <solvers/sat/satcheck.h>
Expand All @@ -29,10 +28,10 @@ Author: Daniel Kroening, [email protected]
#include "ebmc_error.h"
#include "ebmc_solver_factory.h"
#include "ebmc_version.h"
#include "output_file.h"
#include "report_results.h"

#include <chrono>
#include <fstream>
#include <iostream>

/*******************************************************************\
Expand Down Expand Up @@ -277,20 +276,13 @@ int ebmc_baset::do_bit_level_bmc()
{
if(cmdline.isset("outfile"))
{
const std::string filename = cmdline.get_value("outfile");
std::ofstream out(widen_if_needed(filename));
auto outfile = output_filet{cmdline.get_value("outfile")};

if(!out)
{
message.error() << "Failed to open `" << filename << "'"
<< messaget::eom;
return 1;
}

message.status() << "Writing DIMACS CNF to `" << filename << "'"
message.status() << "Writing DIMACS CNF to `" << outfile.name() << "'"
<< messaget::eom;

dimacs_cnf_writert dimacs_cnf_writer{out, message.get_message_handler()};
dimacs_cnf_writert dimacs_cnf_writer{
outfile.stream(), message.get_message_handler()};

return do_bit_level_bmc(dimacs_cnf_writer, true);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ebmc/ebmc_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ Author: Daniel Kroening, [email protected]
#ifndef EBMC_ERROR_H
#define EBMC_ERROR_H

#include <util/source_location.h>

#include <sstream>
#include <string>

class ebmc_errort
{
Expand Down
13 changes: 5 additions & 8 deletions src/ebmc/neural_liveness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Author: Daniel Kroening, [email protected]
#include <util/string2int.h>
#include <util/tempdir.h>
#include <util/tempfile.h>
#include <util/unicode.h>

#include <temporal-logic/temporal_expr.h>
#include <verilog/sva_expr.h>

#include "ebmc_error.h"
#include "ebmc_solver_factory.h"
#include "live_signal.h"
#include "output_file.h"
#include "random_traces.h"
#include "ranking_function.h"
#include "report_results.h"
Expand Down Expand Up @@ -199,13 +199,10 @@ neural_livenesst::dump_vcd_files(temp_dirt &temp_dir)
[&, trace_nr = 0ull, outfile_prefix](trans_tracet trace) mutable -> void {
namespacet ns(transition_system.symbol_table);
auto filename = outfile_prefix + std::to_string(trace_nr + 1);
std::ofstream out(widen_if_needed(filename));

if(!out)
throw ebmc_errort() << "failed to write trace to " << filename;

message.progress() << "*** Writing " << filename << messaget::eom;
show_trans_trace_vcd(trace, message, ns, out);
auto outfile = output_filet{filename};
message.progress() << "*** Writing to " << outfile.name()
<< messaget::eom;
show_trans_trace_vcd(trace, message, ns, outfile.stream());
};
}

Expand Down
40 changes: 40 additions & 0 deletions src/ebmc/output_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************\
Module: Output File Container
Author: Daniel Kroening, [email protected]
\*******************************************************************/

#include "output_file.h"

#include <util/unicode.h>

#include "ebmc_error.h"

#include <fstream>
#include <iostream>

output_filet::output_filet(std::string __file_name)
: _name(std::move(__file_name))
{
if(_name == "-")
{
_stream = &std::cout;
delete_required = false;
_name = "stdout";
}
else
{
_stream = new std::ofstream(widen_if_needed(_name));
if(!_stream)
throw ebmc_errort() << "failed to open " << _name;
delete_required = true;
}
}

output_filet::~output_filet()
{
if(delete_required)
delete _stream;
}
41 changes: 41 additions & 0 deletions src/ebmc/output_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************\
Module: Output File Container
Author: Daniel Kroening, [email protected]
\*******************************************************************/

#ifndef EBMC_OUTPUT_FILE_H
#define EBMC_OUTPUT_FILE_H

#include <iosfwd>
#include <string>

class output_filet final
{
public:
/// Create a stream to the given file name,
/// our stdout if "-".
/// Throws ebmc_errort() if the file cannot be opened.
explicit output_filet(std::string __file_name);
~output_filet();

std::ostream &stream()
{
return *_stream;
}

// the name of the file, or "stdout"
const std::string &name()
{
return _name;
}

protected:
std::string _name;
bool delete_required;
std::ostream *_stream;
};

#endif
36 changes: 10 additions & 26 deletions src/ebmc/random_traces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Author: Daniel Kroening, [email protected]

#include "ebmc_base.h"
#include "ebmc_error.h"
#include "output_file.h"
#include "waveform.h"

#include <algorithm>
Expand Down Expand Up @@ -182,15 +183,12 @@ int random_traces(const cmdlinet &cmdline, message_handlert &message_handler)
{
PRECONDITION(outfile_prefix.has_value());
auto filename = outfile_prefix.value() + std::to_string(trace_nr + 1);
std::ofstream out(widen_if_needed(filename));
auto outfile = output_filet{filename};

if(!out)
throw ebmc_errort() << "failed to write trace to " << filename;

consolet::out() << "*** Writing " << filename << '\n';
consolet::out() << "*** Writing " << outfile.name() << '\n';

messaget message(message_handler);
show_trans_trace_vcd(trace, message, ns, out);
show_trans_trace_vcd(trace, message, ns, outfile.stream());
}
else if(cmdline.isset("waveform"))
{
Expand Down Expand Up @@ -294,23 +292,13 @@ int random_trace(const cmdlinet &cmdline, message_handlert &message_handler)
else if(cmdline.isset("vcd"))
{
auto filename = cmdline.get_value("vcd");
messaget message(message_handler);

if(filename == "-")
{
show_trans_trace_vcd(trace, message, ns, std::cout);
}
else
{
std::ofstream out(widen_if_needed(filename));

if(!out)
throw ebmc_errort() << "failed to write trace to " << filename;
auto outfile = output_filet{filename};

if(filename != "-")
consolet::out() << "*** Writing " << filename << '\n';

show_trans_trace_vcd(trace, message, ns, out);
}
messaget message(message_handler);
show_trans_trace_vcd(trace, message, ns, outfile.stream());
}
else // default
{
Expand Down Expand Up @@ -352,13 +340,9 @@ void random_traces(
auto consumer = [&, trace_nr = 0ull](trans_tracet trace) mutable -> void {
namespacet ns(transition_system.symbol_table);
auto filename = outfile_prefix + std::to_string(trace_nr + 1);
std::ofstream out(widen_if_needed(filename));

if(!out)
throw ebmc_errort() << "failed to write trace to " << filename;

auto outfile = output_filet{filename};
messaget message(message_handler);
show_trans_trace_vcd(trace, message, ns, out);
show_trans_trace_vcd(trace, message, ns, outfile.stream());

trace_nr++;
};
Expand Down
14 changes: 6 additions & 8 deletions src/ebmc/report_results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ Author: Daniel Kroening, [email protected]
#include "report_results.h"

#include <util/json.h>
#include <util/unicode.h>
#include <util/xml.h>

#include "ebmc_error.h"
#include "output_file.h"
#include "waveform.h"

#include <fstream>
#include <iostream>

/*******************************************************************\
Expand All @@ -42,9 +41,7 @@ void report_results(
if(cmdline.isset("json-result"))
{
auto filename = cmdline.get_value("json-result");
std::ofstream out(widen_if_needed(filename));
if(!out)
throw ebmc_errort() << "failed to open " << filename;
auto outfile = output_filet{filename};

json_objectt json_results;
auto &json_properties = json_results["properties"].make_array();
Expand All @@ -65,7 +62,7 @@ void report_results(
json_properties.push_back(std::move(json_property));
}

out << json_results;
outfile.stream() << json_results;
}

if(
Expand Down Expand Up @@ -167,10 +164,11 @@ void report_results(
if(property.has_witness_trace())
{
std::string vcdfile = cmdline.get_value("vcd");
std::ofstream vcd(widen_if_needed(vcdfile));
auto outfile = output_filet{vcdfile};

messaget message(message_handler);
show_trans_trace_vcd(property.witness_trace.value(), message, ns, vcd);
show_trans_trace_vcd(
property.witness_trace.value(), message, ns, outfile.stream());

break;
}
Expand Down
16 changes: 3 additions & 13 deletions src/ebmc/show_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Author: Daniel Kroening, [email protected]

#include <util/json.h>
#include <util/json_irep.h>
#include <util/unicode.h>
#include <util/xml.h>
#include <util/xml_irep.h>

#include "ebmc_base.h"
#include "ebmc_error.h"
#include "output_file.h"

#include <iostream>

Expand Down Expand Up @@ -97,16 +97,6 @@ void ebmc_baset::json_properties(const std::string &file_name)
json.push_back(std::move(json_property));
}

if(file_name == "-")
{
std::cout << json;
}
else
{
std::ofstream out(widen_if_needed(file_name));
if(!out)
throw ebmc_errort() << "failed to open " << file_name;

out << json;
}
auto outfile = output_filet{file_name};
outfile.stream() << json;
}
30 changes: 5 additions & 25 deletions src/ebmc/show_trans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ Author: Daniel Kroening, [email protected]

#include "show_trans.h"

#include <util/unicode.h>

#include <verilog/expr2verilog.h>

#include "ebmc_base.h"
#include "ebmc_version.h"
#include "output_file.h"
#include "output_verilog.h"

#include <fstream>
#include <iostream>

/*******************************************************************\
Expand Down Expand Up @@ -268,17 +266,8 @@ int show_transt::show_trans_verilog_rtl()
if(cmdline.isset("outfile"))
{
const std::string filename=cmdline.get_value("outfile");
std::ofstream out(widen_if_needed(filename));

if(!out)
{
std::cerr << "Failed to open `"
<< filename
<< "'" << '\n';
return 1;
}

show_trans_verilog_rtl(out);
auto outfile = output_filet{filename};
show_trans_verilog_rtl(outfile.stream());
}
else
show_trans_verilog_rtl(std::cout);
Expand Down Expand Up @@ -306,17 +295,8 @@ int show_transt::show_trans_verilog_netlist()
if(cmdline.isset("outfile"))
{
const std::string filename=cmdline.get_value("outfile");
std::ofstream out(widen_if_needed(filename));

if(!out)
{
std::cerr << "Failed to open `"
<< filename
<< "'" << '\n';
return 1;
}

show_trans_verilog_netlist(out);
auto outfile = output_filet{filename};
show_trans_verilog_netlist(outfile.stream());
}
else
show_trans_verilog_netlist(std::cout);
Expand Down
Loading