Skip to content

Commit bb64d36

Browse files
committed
CommandLineInterface: Accept output streams as parameters
1 parent 6c33fbc commit bb64d36

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

solc/CommandLineInterface.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
#include <unistd.h>
7171
#endif
7272

73-
#include <iostream>
7473
#include <fstream>
7574

7675
#if !defined(STDERR_FILENO)
@@ -86,25 +85,18 @@ using namespace solidity::langutil;
8685
namespace solidity::frontend
8786
{
8887

89-
namespace
88+
ostream& CommandLineInterface::sout(bool _markAsUsed)
9089
{
91-
92-
static bool g_hasOutput = false;
93-
94-
std::ostream& sout(bool _used = true)
95-
{
96-
if (_used)
97-
g_hasOutput = true;
98-
return cout;
90+
if (_markAsUsed)
91+
m_hasOutput = true;
92+
return m_sout;
9993
}
10094

101-
std::ostream& serr(bool _used = true)
95+
ostream& CommandLineInterface::serr(bool _markAsUsed)
10296
{
103-
if (_used)
104-
g_hasOutput = true;
105-
return cerr;
106-
}
107-
97+
if (_markAsUsed)
98+
m_hasOutput = true;
99+
return m_serr;
108100
}
109101

110102
#define cout
@@ -497,11 +489,11 @@ void CommandLineInterface::createJson(string const& _fileName, string const& _js
497489

498490
bool CommandLineInterface::parseArguments(int _argc, char const* const* _argv)
499491
{
500-
CommandLineParser parser(sout(false), serr(false));
492+
CommandLineParser parser(sout(/* _markAsUsed */ false), serr(/* _markAsUsed */ false));
501493
bool success = parser.parse(_argc, _argv, isatty(fileno(stdin)));
502494
if (!success)
503495
return false;
504-
g_hasOutput = g_hasOutput || parser.hasOutput();
496+
m_hasOutput = m_hasOutput || parser.hasOutput();
505497
m_options = parser.options();
506498

507499
return true;
@@ -626,7 +618,7 @@ bool CommandLineInterface::compile()
626618

627619
for (auto const& error: m_compiler->errors())
628620
{
629-
g_hasOutput = true;
621+
m_hasOutput = true;
630622
formatter.printErrorInformation(*error);
631623
}
632624

@@ -635,7 +627,7 @@ bool CommandLineInterface::compile()
635627
}
636628
catch (CompilerError const& _exception)
637629
{
638-
g_hasOutput = true;
630+
m_hasOutput = true;
639631
formatter.printExceptionInformation(_exception, "Compiler error");
640632
return false;
641633
}
@@ -669,7 +661,7 @@ bool CommandLineInterface::compile()
669661
serr() << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_error) << endl;
670662
else
671663
{
672-
g_hasOutput = true;
664+
m_hasOutput = true;
673665
formatter.printExceptionInformation(_error, _error.typeName());
674666
}
675667

@@ -985,7 +977,7 @@ bool CommandLineInterface::assemble(
985977

986978
for (auto const& error: stack.errors())
987979
{
988-
g_hasOutput = true;
980+
m_hasOutput = true;
989981
formatter.printErrorInformation(*error);
990982
}
991983
if (!Error::containsOnlyWarnings(stack.errors()))
@@ -1134,7 +1126,7 @@ void CommandLineInterface::outputCompilationResults()
11341126
handleNatspec(false, contract);
11351127
} // end of contracts iteration
11361128

1137-
if (!g_hasOutput)
1129+
if (!m_hasOutput)
11381130
{
11391131
if (!m_options.output.dir.empty())
11401132
sout() << "Compiler run successful. Artifact(s) can be found in directory " << m_options.output.dir << "." << endl;

solc/CommandLineInterface.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <libsolidity/interface/FileReader.h>
3030
#include <libyul/AssemblyStack.h>
3131

32+
#include <iostream>
3233
#include <memory>
3334
#include <string>
3435

@@ -38,7 +39,13 @@ namespace solidity::frontend
3839
class CommandLineInterface
3940
{
4041
public:
41-
explicit CommandLineInterface(CommandLineOptions const& _options = CommandLineOptions{}):
42+
explicit CommandLineInterface(
43+
std::ostream& _sout,
44+
std::ostream& _serr,
45+
CommandLineOptions const& _options = CommandLineOptions{}
46+
):
47+
m_sout(_sout),
48+
m_serr(_serr),
4249
m_options(_options)
4350
{}
4451

@@ -106,6 +113,17 @@ class CommandLineInterface
106113
/// @arg _json json string to be written
107114
void createJson(std::string const& _fileName, std::string const& _json);
108115

116+
/// Returns the stream that should receive normal output. Sets m_hasOutput to true if the
117+
/// stream has ever been used unless @arg _markAsUsed is set to false.
118+
std::ostream& sout(bool _markAsUsed = true);
119+
120+
/// Returns the stream that should receive error output. Sets m_hasOutput to true if the
121+
/// stream has ever been used unless @arg _markAsUsed is set to false.
122+
std::ostream& serr(bool _markAsUsed = true);
123+
124+
std::ostream& m_sout;
125+
std::ostream& m_serr;
126+
bool m_hasOutput = false;
109127
bool m_error = false; ///< If true, some error occurred.
110128
FileReader m_fileReader;
111129
std::unique_ptr<frontend::CompilerStack> m_compiler;

solc/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void setDefaultOrCLocale()
5454
int main(int argc, char** argv)
5555
{
5656
setDefaultOrCLocale();
57-
solidity::frontend::CommandLineInterface cli;
57+
solidity::frontend::CommandLineInterface cli(cout, cerr);
5858
if (!cli.parseArguments(argc, argv))
5959
return 1;
6060
if (!cli.processInput())

0 commit comments

Comments
 (0)