CommandLineInterface: Accept output streams as parameters

This commit is contained in:
Kamil Śliwak 2021-06-10 20:08:53 +02:00
parent 6c33fbcb6a
commit bb64d366ea
3 changed files with 35 additions and 25 deletions

View File

@ -70,7 +70,6 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <iostream>
#include <fstream> #include <fstream>
#if !defined(STDERR_FILENO) #if !defined(STDERR_FILENO)
@ -86,25 +85,18 @@ using namespace solidity::langutil;
namespace solidity::frontend namespace solidity::frontend
{ {
namespace ostream& CommandLineInterface::sout(bool _markAsUsed)
{ {
if (_markAsUsed)
static bool g_hasOutput = false; m_hasOutput = true;
return m_sout;
std::ostream& sout(bool _used = true)
{
if (_used)
g_hasOutput = true;
return cout;
} }
std::ostream& serr(bool _used = true) ostream& CommandLineInterface::serr(bool _markAsUsed)
{ {
if (_used) if (_markAsUsed)
g_hasOutput = true; m_hasOutput = true;
return cerr; return m_serr;
}
} }
#define cout #define cout
@ -497,11 +489,11 @@ void CommandLineInterface::createJson(string const& _fileName, string const& _js
bool CommandLineInterface::parseArguments(int _argc, char const* const* _argv) bool CommandLineInterface::parseArguments(int _argc, char const* const* _argv)
{ {
CommandLineParser parser(sout(false), serr(false)); CommandLineParser parser(sout(/* _markAsUsed */ false), serr(/* _markAsUsed */ false));
bool success = parser.parse(_argc, _argv, isatty(fileno(stdin))); bool success = parser.parse(_argc, _argv, isatty(fileno(stdin)));
if (!success) if (!success)
return false; return false;
g_hasOutput = g_hasOutput || parser.hasOutput(); m_hasOutput = m_hasOutput || parser.hasOutput();
m_options = parser.options(); m_options = parser.options();
return true; return true;
@ -626,7 +618,7 @@ bool CommandLineInterface::compile()
for (auto const& error: m_compiler->errors()) for (auto const& error: m_compiler->errors())
{ {
g_hasOutput = true; m_hasOutput = true;
formatter.printErrorInformation(*error); formatter.printErrorInformation(*error);
} }
@ -635,7 +627,7 @@ bool CommandLineInterface::compile()
} }
catch (CompilerError const& _exception) catch (CompilerError const& _exception)
{ {
g_hasOutput = true; m_hasOutput = true;
formatter.printExceptionInformation(_exception, "Compiler error"); formatter.printExceptionInformation(_exception, "Compiler error");
return false; return false;
} }
@ -669,7 +661,7 @@ bool CommandLineInterface::compile()
serr() << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_error) << endl; serr() << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_error) << endl;
else else
{ {
g_hasOutput = true; m_hasOutput = true;
formatter.printExceptionInformation(_error, _error.typeName()); formatter.printExceptionInformation(_error, _error.typeName());
} }
@ -985,7 +977,7 @@ bool CommandLineInterface::assemble(
for (auto const& error: stack.errors()) for (auto const& error: stack.errors())
{ {
g_hasOutput = true; m_hasOutput = true;
formatter.printErrorInformation(*error); formatter.printErrorInformation(*error);
} }
if (!Error::containsOnlyWarnings(stack.errors())) if (!Error::containsOnlyWarnings(stack.errors()))
@ -1134,7 +1126,7 @@ void CommandLineInterface::outputCompilationResults()
handleNatspec(false, contract); handleNatspec(false, contract);
} // end of contracts iteration } // end of contracts iteration
if (!g_hasOutput) if (!m_hasOutput)
{ {
if (!m_options.output.dir.empty()) if (!m_options.output.dir.empty())
sout() << "Compiler run successful. Artifact(s) can be found in directory " << m_options.output.dir << "." << endl; sout() << "Compiler run successful. Artifact(s) can be found in directory " << m_options.output.dir << "." << endl;

View File

@ -29,6 +29,7 @@
#include <libsolidity/interface/FileReader.h> #include <libsolidity/interface/FileReader.h>
#include <libyul/AssemblyStack.h> #include <libyul/AssemblyStack.h>
#include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
@ -38,7 +39,13 @@ namespace solidity::frontend
class CommandLineInterface class CommandLineInterface
{ {
public: public:
explicit CommandLineInterface(CommandLineOptions const& _options = CommandLineOptions{}): explicit CommandLineInterface(
std::ostream& _sout,
std::ostream& _serr,
CommandLineOptions const& _options = CommandLineOptions{}
):
m_sout(_sout),
m_serr(_serr),
m_options(_options) m_options(_options)
{} {}
@ -106,6 +113,17 @@ private:
/// @arg _json json string to be written /// @arg _json json string to be written
void createJson(std::string const& _fileName, std::string const& _json); void createJson(std::string const& _fileName, std::string const& _json);
/// Returns the stream that should receive normal output. Sets m_hasOutput to true if the
/// stream has ever been used unless @arg _markAsUsed is set to false.
std::ostream& sout(bool _markAsUsed = true);
/// Returns the stream that should receive error output. Sets m_hasOutput to true if the
/// stream has ever been used unless @arg _markAsUsed is set to false.
std::ostream& serr(bool _markAsUsed = true);
std::ostream& m_sout;
std::ostream& m_serr;
bool m_hasOutput = false;
bool m_error = false; ///< If true, some error occurred. bool m_error = false; ///< If true, some error occurred.
FileReader m_fileReader; FileReader m_fileReader;
std::unique_ptr<frontend::CompilerStack> m_compiler; std::unique_ptr<frontend::CompilerStack> m_compiler;

View File

@ -54,7 +54,7 @@ static void setDefaultOrCLocale()
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
setDefaultOrCLocale(); setDefaultOrCLocale();
solidity::frontend::CommandLineInterface cli; solidity::frontend::CommandLineInterface cli(cout, cerr);
if (!cli.parseArguments(argc, argv)) if (!cli.parseArguments(argc, argv))
return 1; return 1;
if (!cli.processInput()) if (!cli.processInput())