SourceReferenceFormatter: Support full range of options in formatErrorInformation()

This commit is contained in:
Kamil Śliwak 2023-08-08 17:08:37 +02:00
parent b7d2c8bb0a
commit 8407c8c615
4 changed files with 35 additions and 27 deletions

View File

@ -90,14 +90,28 @@ public:
static std::string formatErrorInformation( static std::string formatErrorInformation(
Error const& _error, Error const& _error,
CharStreamProvider const& _charStreamProvider CharStreamProvider const& _charStreamProvider,
bool _colored = false,
bool _withErrorIds = false
) )
{ {
return formatExceptionInformation( std::ostringstream errorOutput;
_error, SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
Error::errorSeverity(_error.type()), formatter.printErrorInformation(_error);
_charStreamProvider return errorOutput.str();
); }
static std::string formatErrorInformation(
langutil::ErrorList const& _errors,
CharStreamProvider const& _charStreamProvider,
bool _colored = false,
bool _withErrorIds = false
)
{
std::ostringstream errorOutput;
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
formatter.printErrorInformation(_errors);
return errorOutput.str();
} }
static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream); static std::string formatErrorInformation(Error const& _error, CharStream const& _charStream);

View File

@ -1486,16 +1486,13 @@ void CompilerStack::generateIR(ContractDefinition const& _contract)
m_optimiserSettings, m_optimiserSettings,
m_debugInfoSelection m_debugInfoSelection
); );
if (!stack.parseAndAnalyze("", compiledContract.yulIR)) bool yulAnalysisSuccessful = stack.parseAndAnalyze("", compiledContract.yulIR);
{ solAssert(
string errorMessage; yulAnalysisSuccessful,
for (auto const& error: stack.errors()) compiledContract.yulIR + "\n\n"
errorMessage += langutil::SourceReferenceFormatter::formatErrorInformation( "Invalid IR generated:\n" +
*error, langutil::SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack) + "\n"
stack.charStream("") );
);
solAssert(false, compiledContract.yulIR + "\n\nInvalid IR generated:\n" + errorMessage + "\n");
}
compiledContract.yulIRAst = stack.astJson(); compiledContract.yulIRAst = stack.astJson();
stack.optimize(); stack.optimize();

View File

@ -85,12 +85,10 @@ std::optional<Error> parseAndReturnFirstError(
if (_allowWarnings && e->type() == Error::Type::Warning) if (_allowWarnings && e->type() == Error::Type::Warning)
continue; continue;
if (error) if (error)
{ BOOST_FAIL(
string errors; "Found more than one error:\n" +
for (auto const& err: stack.errors()) SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack)
errors += SourceReferenceFormatter::formatErrorInformation(*err, stack); );
BOOST_FAIL("Found more than one error:\n" + errors);
}
error = e; error = e;
} }
if (!success) if (!success)

View File

@ -42,11 +42,10 @@ optional<CompilerOutput> SolidityCompilationFramework::compileContract()
if (m_compilerInput.debugFailure) if (m_compilerInput.debugFailure)
{ {
cerr << "Compiling contract failed" << endl; cerr << "Compiling contract failed" << endl;
for (auto const& error: m_compiler.errors()) cerr << SourceReferenceFormatter::formatErrorInformation(
cerr << SourceReferenceFormatter::formatErrorInformation( m_compiler.errors(),
*error, m_compiler
m_compiler );
);
} }
return {}; return {};
} }