Skip to content

Commit

Permalink
Merge pull request #14482 from ethereum/fix-error-ids-in-format-errors
Browse files Browse the repository at this point in the history
Fix error IDs in `AnalysisFramework::formatErrors()`
  • Loading branch information
cameel authored Aug 11, 2023
2 parents 3edf91a + 339053f commit c50c9b2
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 54 deletions.
43 changes: 31 additions & 12 deletions liblangutil/SourceReferenceFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ class SourceReferenceFormatter
bool _colored,
bool _withErrorIds
):
m_stream(_stream), m_charStreamProvider(_charStreamProvider), m_colored(_colored), m_withErrorIds(_withErrorIds)
m_stream(_stream),
m_charStreamProvider(_charStreamProvider),
m_colored(_colored),
m_withErrorIds(_withErrorIds)
{}

// WARNING: Use the xyzErrorInformation() variants over xyzExceptionInformation() when you
// do have access to an Error instance. Error is implicitly convertible to util::Exception
// but the conversion loses the error ID.

/// Prints source location if it is given.
void printSourceLocation(SourceReference const& _ref);
void printExceptionInformation(SourceReferenceExtractor::Message const& _msg);
Expand All @@ -61,12 +68,11 @@ class SourceReferenceFormatter
util::Exception const& _exception,
Error::Type _type,
CharStreamProvider const& _charStreamProvider,
bool _colored = false,
bool _withErrorIds = false
bool _colored = false
)
{
std::ostringstream errorOutput;
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, false /* _withErrorIds */);
formatter.printExceptionInformation(_exception, _type);
return errorOutput.str();
}
Expand All @@ -75,26 +81,39 @@ class SourceReferenceFormatter
util::Exception const& _exception,
Error::Severity _severity,
CharStreamProvider const& _charStreamProvider,
bool _colored = false
)
{
std::ostringstream errorOutput;
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, false /* _withErrorIds */);
formatter.printExceptionInformation(_exception, _severity);
return errorOutput.str();
}

static std::string formatErrorInformation(
Error const& _error,
CharStreamProvider const& _charStreamProvider,
bool _colored = false,
bool _withErrorIds = false
)
{
std::ostringstream errorOutput;
SourceReferenceFormatter formatter(errorOutput, _charStreamProvider, _colored, _withErrorIds);
formatter.printExceptionInformation(_exception, _severity);
formatter.printErrorInformation(_error);
return errorOutput.str();
}

static std::string formatErrorInformation(
Error const& _error,
CharStreamProvider const& _charStreamProvider
langutil::ErrorList const& _errors,
CharStreamProvider const& _charStreamProvider,
bool _colored = false,
bool _withErrorIds = false
)
{
return formatExceptionInformation(
_error,
Error::errorSeverity(_error.type()),
_charStreamProvider
);
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);
Expand Down
2 changes: 0 additions & 2 deletions libsolidity/codegen/ir/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
#include <libsolutil/StringUtils.h>
#include <libsolutil/Whiskers.h>

#include <liblangutil/SourceReferenceFormatter.h>

#include <json/json.h>

#include <sstream>
Expand Down
17 changes: 7 additions & 10 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,16 +1486,13 @@ void CompilerStack::generateIR(ContractDefinition const& _contract)
m_optimiserSettings,
m_debugInfoSelection
);
if (!stack.parseAndAnalyze("", compiledContract.yulIR))
{
string errorMessage;
for (auto const& error: stack.errors())
errorMessage += langutil::SourceReferenceFormatter::formatErrorInformation(
*error,
stack.charStream("")
);
solAssert(false, compiledContract.yulIR + "\n\nInvalid IR generated:\n" + errorMessage + "\n");
}
bool yulAnalysisSuccessful = stack.parseAndAnalyze("", compiledContract.yulIR);
solAssert(
yulAnalysisSuccessful,
compiledContract.yulIR + "\n\n"
"Invalid IR generated:\n" +
langutil::SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack) + "\n"
);

compiledContract.yulIRAst = stack.astJson();
stack.optimize();
Expand Down
3 changes: 1 addition & 2 deletions libsolidity/interface/StandardCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ Json::Value formatErrorWithException(
_exception,
_type,
_charStreamProvider,
false, // colored
false // _withErrorIds
false // colored
);

if (string const* description = _exception.comment())
Expand Down
2 changes: 1 addition & 1 deletion solc/CommandLineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ void CommandLineInterface::compile()
else
{
m_hasOutput = true;
formatter.printExceptionInformation(_error, Error::errorSeverity(_error.type()));
formatter.printErrorInformation(_error);
solThrow(CommandLineExecutionError, "");
}
}
Expand Down
15 changes: 8 additions & 7 deletions test/libsolidity/AnalysisFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,17 @@ ErrorList AnalysisFramework::expectError(std::string const& _source, bool _warni
}

string AnalysisFramework::formatErrors(
langutil::ErrorList _errors,
langutil::ErrorList const& _errors,
bool _colored,
bool _withErrorIds
) const
{
string message;
for (auto const& error: _errors)
message += formatError(*error, _colored, _withErrorIds);
return message;
return SourceReferenceFormatter::formatErrorInformation(
_errors,
*m_compiler,
_colored,
_withErrorIds
);
}

string AnalysisFramework::formatError(
Expand All @@ -164,9 +166,8 @@ string AnalysisFramework::formatError(
bool _withErrorIds
) const
{
return SourceReferenceFormatter::formatExceptionInformation(
return SourceReferenceFormatter::formatErrorInformation(
_error,
_error.type(),
*m_compiler,
_colored,
_withErrorIds
Expand Down
2 changes: 1 addition & 1 deletion test/libsolidity/AnalysisFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AnalysisFramework
langutil::ErrorList expectError(std::string const& _source, bool _warning = false, bool _allowMultiple = false);

std::string formatErrors(
langutil::ErrorList _errors,
langutil::ErrorList const& _errors,
bool _colored = false,
bool _withErrorIds = false
) const;
Expand Down
1 change: 0 additions & 1 deletion test/libsolidity/GasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <test/Common.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/JSON.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
Expand Down
10 changes: 4 additions & 6 deletions test/libsolidity/InlineAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ std::optional<Error> parseAndReturnFirstError(
if (_allowWarnings && e->type() == Error::Type::Warning)
continue;
if (error)
{
string errors;
for (auto const& err: stack.errors())
errors += SourceReferenceFormatter::formatErrorInformation(*err, stack);
BOOST_FAIL("Found more than one error:\n" + errors);
}
BOOST_FAIL(
"Found more than one error:\n" +
SourceReferenceFormatter::formatErrorInformation(stack.errors(), stack)
);
error = e;
}
if (!success)
Expand Down
1 change: 0 additions & 1 deletion test/libyul/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/SourceReferenceFormatter.h>

#include <boost/test/unit_test.hpp>

Expand Down
1 change: 0 additions & 1 deletion test/libyul/ControlFlowGraphTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <libyul/backends/evm/ControlFlowGraphBuilder.h>
#include <libyul/backends/evm/StackHelpers.h>
#include <libyul/Object.h>
#include <liblangutil/SourceReferenceFormatter.h>

#include <libsolutil/AnsiColorized.h>
#include <libsolutil/Visitor.h>
Expand Down
9 changes: 4 additions & 5 deletions test/tools/ossfuzz/SolidityEvmoneInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ optional<CompilerOutput> SolidityCompilationFramework::compileContract()
if (m_compilerInput.debugFailure)
{
cerr << "Compiling contract failed" << endl;
for (auto const& error: m_compiler.errors())
cerr << SourceReferenceFormatter::formatErrorInformation(
*error,
m_compiler
);
cerr << SourceReferenceFormatter::formatErrorInformation(
m_compiler.errors(),
m_compiler
);
}
return {};
}
Expand Down
5 changes: 1 addition & 4 deletions test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ DEFINE_PROTO_FUZZER(Program const& _input)
Error::containsErrors(stack.errors())
)
{
SourceReferenceFormatter formatter(std::cout, stack, false, false);

for (auto const& error: stack.errors())
formatter.printExceptionInformation(*error, Error::errorSeverity(error->type()));
SourceReferenceFormatter{std::cout, stack, false, false}.printErrorInformation(stack.errors());
yulAssert(false, "Proto fuzzer generated malformed program");
}

Expand Down
1 change: 0 additions & 1 deletion tools/yulPhaser/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include <liblangutil/CharStream.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/SourceReferenceFormatter.h>

#include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h>
Expand Down

0 comments on commit c50c9b2

Please sign in to comment.