Add shortcuts for formatting error information

This commit is contained in:
mingchuan 2019-04-05 23:49:39 +08:00
parent ef3a18999c
commit f1374066af
No known key found for this signature in database
GPG Key ID: 607CD25FA2D03651
13 changed files with 34 additions and 55 deletions

View File

@ -70,9 +70,17 @@ void SourceReferenceFormatter::printSourceName(SourceReference const& _ref)
m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": "; m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": ";
} }
void SourceReferenceFormatter::printExceptionInformation(dev::Exception const& _error, std::string const& _category) void SourceReferenceFormatter::printExceptionInformation(dev::Exception const& _exception, std::string const& _category)
{ {
printExceptionInformation(SourceReferenceExtractor::extract(_error, _category)); printExceptionInformation(SourceReferenceExtractor::extract(_exception, _category));
}
void SourceReferenceFormatter::printErrorInformation(Error const& _error)
{
printExceptionInformation(
_error,
(_error.type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }
void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg) void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg)

View File

@ -25,6 +25,7 @@
#include <ostream> #include <ostream>
#include <sstream> #include <sstream>
#include <functional> #include <functional>
#include <liblangutil/Exceptions.h>
#include <liblangutil/SourceReferenceExtractor.h> #include <liblangutil/SourceReferenceExtractor.h>
namespace dev namespace dev
@ -51,7 +52,16 @@ public:
virtual void printExceptionInformation(SourceReferenceExtractor::Message const& _msg); virtual void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
virtual void printSourceLocation(SourceLocation const* _location); virtual void printSourceLocation(SourceLocation const* _location);
virtual void printExceptionInformation(dev::Exception const& _error, std::string const& _category); virtual void printExceptionInformation(dev::Exception const& _exception, std::string const& _category);
virtual void printErrorInformation(Error const& _error);
static std::string formatErrorInformation(Error const& _error)
{
return formatExceptionInformation(
_error,
(_error.type() == Error::Type::Warning) ? "Warning" : "Error"
);
}
static std::string formatExceptionInformation( static std::string formatExceptionInformation(
dev::Exception const& _exception, dev::Exception const& _exception,

View File

@ -396,10 +396,7 @@ void CompilerContext::appendInlineAssembly(
_assembly + "\n" _assembly + "\n"
"------------------ Errors: ----------------\n"; "------------------ Errors: ----------------\n";
for (auto const& error: errorReporter.errors()) for (auto const& error: errorReporter.errors())
message += SourceReferenceFormatter::formatExceptionInformation( message += SourceReferenceFormatter::formatErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
message += "-------------------------------------------\n"; message += "-------------------------------------------\n";
solAssert(false, message); solAssert(false, message);

View File

@ -50,10 +50,7 @@ pair<string, string> IRGenerator::run(ContractDefinition const& _contract)
{ {
string errorMessage; string errorMessage;
for (auto const& error: asmStack.errors()) for (auto const& error: asmStack.errors())
errorMessage += langutil::SourceReferenceFormatter::formatExceptionInformation( errorMessage += langutil::SourceReferenceFormatter::formatErrorInformation(*error);
*error,
(error->type() == langutil::Error::Type::Warning) ? "Warning" : "Error"
);
solAssert(false, "Invalid IR generated:\n" + errorMessage + "\n" + ir); solAssert(false, "Invalid IR generated:\n" + errorMessage + "\n" + ir);
} }
asmStack.optimize(); asmStack.optimize();

View File

@ -937,10 +937,7 @@ bool CommandLineInterface::processInput()
for (auto const& error: m_compiler->errors()) for (auto const& error: m_compiler->errors())
{ {
g_hasOutput = true; g_hasOutput = true;
formatter->printExceptionInformation( formatter->printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }
if (!successful) if (!successful)
@ -1294,10 +1291,7 @@ bool CommandLineInterface::assemble(
for (auto const& error: stack.errors()) for (auto const& error: stack.errors())
{ {
g_hasOutput = true; g_hasOutput = true;
formatter->printExceptionInformation( formatter->printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }
if (!Error::containsOnlyWarnings(stack.errors())) if (!Error::containsOnlyWarnings(stack.errors()))
successful = false; successful = false;

View File

@ -125,10 +125,7 @@ string AnalysisFramework::formatErrors() const
string AnalysisFramework::formatError(Error const& _error) const string AnalysisFramework::formatError(Error const& _error) const
{ {
return SourceReferenceFormatter::formatExceptionInformation( return SourceReferenceFormatter::formatErrorInformation(_error);
_error,
(_error.type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }
ContractDefinition const* AnalysisFramework::retrieveContractByName(SourceUnit const& _source, string const& _name) ContractDefinition const* AnalysisFramework::retrieveContractByName(SourceUnit const& _source, string const& _name)

View File

@ -83,10 +83,7 @@ public:
langutil::SourceReferenceFormatter formatter(std::cerr); langutil::SourceReferenceFormatter formatter(std::cerr);
for (auto const& error: m_compiler.errors()) for (auto const& error: m_compiler.errors())
formatter.printExceptionInformation( formatter.printErrorInformation(*error);
*error,
(error->type() == langutil::Error::Type::Warning) ? "Warning" : "Error"
);
BOOST_ERROR("Compiling contract failed"); BOOST_ERROR("Compiling contract failed");
} }
eth::LinkerObject obj; eth::LinkerObject obj;
@ -104,10 +101,7 @@ public:
langutil::SourceReferenceFormatter formatter(std::cerr); langutil::SourceReferenceFormatter formatter(std::cerr);
for (auto const& error: m_compiler.errors()) for (auto const& error: m_compiler.errors())
formatter.printExceptionInformation( formatter.printErrorInformation(*error);
*error,
(error->type() == langutil::Error::Type::Warning) ? "Warning" : "Error"
);
BOOST_ERROR("Assembly contract failed. IR: " + m_compiler.yulIROptimized({})); BOOST_ERROR("Assembly contract failed. IR: " + m_compiler.yulIROptimized({}));
} }
asmStack.optimize(); asmStack.optimize();

View File

@ -54,10 +54,7 @@ void yul::test::printErrors(ErrorList const& _errors)
SourceReferenceFormatter formatter(cout); SourceReferenceFormatter formatter(cout);
for (auto const& error: _errors) for (auto const& error: _errors)
formatter.printExceptionInformation( formatter.printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }

View File

@ -130,8 +130,5 @@ void ObjectCompilerTest::printErrors(ostream& _stream, ErrorList const& _errors)
SourceReferenceFormatter formatter(_stream); SourceReferenceFormatter formatter(_stream);
for (auto const& error: _errors) for (auto const& error: _errors)
formatter.printExceptionInformation( formatter.printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }

View File

@ -159,8 +159,5 @@ void YulInterpreterTest::printErrors(ostream& _stream, ErrorList const& _errors)
SourceReferenceFormatter formatter(_stream); SourceReferenceFormatter formatter(_stream);
for (auto const& error: _errors) for (auto const& error: _errors)
formatter.printExceptionInformation( formatter.printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }

View File

@ -353,8 +353,5 @@ void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors)
SourceReferenceFormatter formatter(_stream); SourceReferenceFormatter formatter(_stream);
for (auto const& error: _errors) for (auto const& error: _errors)
formatter.printExceptionInformation( formatter.printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }

View File

@ -80,10 +80,7 @@ public:
SourceReferenceFormatter formatter(cout); SourceReferenceFormatter formatter(cout);
for (auto const& error: m_errors) for (auto const& error: m_errors)
formatter.printExceptionInformation( formatter.printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }
bool parse(string const& _input) bool parse(string const& _input)

View File

@ -55,10 +55,7 @@ namespace
void printErrors(ErrorList const& _errors) void printErrors(ErrorList const& _errors)
{ {
for (auto const& error: _errors) for (auto const& error: _errors)
SourceReferenceFormatter(cout).printExceptionInformation( SourceReferenceFormatter(cout).printErrorInformation(*error);
*error,
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
);
} }
pair<shared_ptr<Block>, shared_ptr<AsmAnalysisInfo>> parse(string const& _source) pair<shared_ptr<Block>, shared_ptr<AsmAnalysisInfo>> parse(string const& _source)