From b7d2c8bb0ae4d0de0b83aed4bd4ebaf9ff3094da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 8 Aug 2023 16:11:57 +0200 Subject: [PATCH 1/5] SourceReferenceFormatter: Line wrapping and unused includes --- liblangutil/SourceReferenceFormatter.h | 5 ++++- libsolidity/codegen/ir/IRGenerator.cpp | 2 -- test/libsolidity/GasTest.cpp | 1 - test/libyul/Common.cpp | 1 - test/libyul/ControlFlowGraphTest.cpp | 1 - tools/yulPhaser/Program.cpp | 1 - 6 files changed, 4 insertions(+), 7 deletions(-) diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index def216efa..c072e72e2 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -46,7 +46,10 @@ public: 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) {} /// Prints source location if it is given. diff --git a/libsolidity/codegen/ir/IRGenerator.cpp b/libsolidity/codegen/ir/IRGenerator.cpp index 223d712f0..67adb61bb 100644 --- a/libsolidity/codegen/ir/IRGenerator.cpp +++ b/libsolidity/codegen/ir/IRGenerator.cpp @@ -38,8 +38,6 @@ #include #include -#include - #include #include diff --git a/test/libsolidity/GasTest.cpp b/test/libsolidity/GasTest.cpp index 78a0ca907..0060c81e4 100644 --- a/test/libsolidity/GasTest.cpp +++ b/test/libsolidity/GasTest.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include diff --git a/test/libyul/Common.cpp b/test/libyul/Common.cpp index 800684f35..f10414a9f 100644 --- a/test/libyul/Common.cpp +++ b/test/libyul/Common.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include diff --git a/test/libyul/ControlFlowGraphTest.cpp b/test/libyul/ControlFlowGraphTest.cpp index dffc7ecbf..44fff4219 100644 --- a/test/libyul/ControlFlowGraphTest.cpp +++ b/test/libyul/ControlFlowGraphTest.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include diff --git a/tools/yulPhaser/Program.cpp b/tools/yulPhaser/Program.cpp index c62b8e130..3089591ed 100644 --- a/tools/yulPhaser/Program.cpp +++ b/tools/yulPhaser/Program.cpp @@ -20,7 +20,6 @@ #include #include -#include #include #include From 8407c8c615c39236db6c29d6c4f8ccbf071ee334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 8 Aug 2023 17:08:37 +0200 Subject: [PATCH 2/5] SourceReferenceFormatter: Support full range of options in formatErrorInformation() --- liblangutil/SourceReferenceFormatter.h | 26 ++++++++++++++----- libsolidity/interface/CompilerStack.cpp | 17 +++++------- test/libsolidity/InlineAssembly.cpp | 10 +++---- .../tools/ossfuzz/SolidityEvmoneInterface.cpp | 9 +++---- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index c072e72e2..02a38288a 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -90,14 +90,28 @@ public: static std::string formatErrorInformation( Error const& _error, - CharStreamProvider const& _charStreamProvider + 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(_error); + 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); diff --git a/libsolidity/interface/CompilerStack.cpp b/libsolidity/interface/CompilerStack.cpp index bbebc2508..84f886d72 100644 --- a/libsolidity/interface/CompilerStack.cpp +++ b/libsolidity/interface/CompilerStack.cpp @@ -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(); diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp index 4a8a9d617..d59b843dd 100644 --- a/test/libsolidity/InlineAssembly.cpp +++ b/test/libsolidity/InlineAssembly.cpp @@ -85,12 +85,10 @@ std::optional 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) diff --git a/test/tools/ossfuzz/SolidityEvmoneInterface.cpp b/test/tools/ossfuzz/SolidityEvmoneInterface.cpp index d2d18bcf5..341470908 100644 --- a/test/tools/ossfuzz/SolidityEvmoneInterface.cpp +++ b/test/tools/ossfuzz/SolidityEvmoneInterface.cpp @@ -42,11 +42,10 @@ optional 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 {}; } From 4020552e1d7146ae0373092a3d04c86d40db2b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 8 Aug 2023 17:54:05 +0200 Subject: [PATCH 3/5] SourceReferenceFormatter: Remove the ineffective _withErrorIds parameter from formatExceptionInformation() --- liblangutil/SourceReferenceFormatter.h | 10 ++++------ libsolidity/interface/StandardCompiler.cpp | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index 02a38288a..8d3121a7a 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -64,12 +64,11 @@ public: 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(); } @@ -78,12 +77,11 @@ public: util::Exception const& _exception, Error::Severity _severity, 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, _severity); return errorOutput.str(); } diff --git a/libsolidity/interface/StandardCompiler.cpp b/libsolidity/interface/StandardCompiler.cpp index 767792601..32d6cf8e8 100644 --- a/libsolidity/interface/StandardCompiler.cpp +++ b/libsolidity/interface/StandardCompiler.cpp @@ -125,8 +125,7 @@ Json::Value formatErrorWithException( _exception, _type, _charStreamProvider, - false, // colored - false // _withErrorIds + false // colored ); if (string const* description = _exception.comment()) From a59fc39f101449587bad937a92c0c9802c7d6438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 8 Aug 2023 17:17:37 +0200 Subject: [PATCH 4/5] Use printErrorInformation() over printExceptionInformation() where possible --- liblangutil/SourceReferenceFormatter.h | 4 ++++ solc/CommandLineInterface.cpp | 2 +- test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp | 5 +---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h index 8d3121a7a..91f1d7ddd 100644 --- a/liblangutil/SourceReferenceFormatter.h +++ b/liblangutil/SourceReferenceFormatter.h @@ -52,6 +52,10 @@ public: 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); diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 7a28900b3..509279a71 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -827,7 +827,7 @@ void CommandLineInterface::compile() else { m_hasOutput = true; - formatter.printExceptionInformation(_error, Error::errorSeverity(_error.type())); + formatter.printErrorInformation(_error); solThrow(CommandLineExecutionError, ""); } } diff --git a/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp b/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp index b1725a8fa..d1717a159 100644 --- a/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp +++ b/test/tools/ossfuzz/yulProto_diff_ossfuzz.cpp @@ -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"); } From 339053f185253fd50f3476dc75a57fd958544bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 8 Aug 2023 17:19:43 +0200 Subject: [PATCH 5/5] AnalysisFramework.formatErrors(): Fix _withErrorIds flag not having any effect --- test/libsolidity/AnalysisFramework.cpp | 15 ++++++++------- test/libsolidity/AnalysisFramework.h | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/test/libsolidity/AnalysisFramework.cpp b/test/libsolidity/AnalysisFramework.cpp index dc1e4fbcf..6c6ad0cb8 100644 --- a/test/libsolidity/AnalysisFramework.cpp +++ b/test/libsolidity/AnalysisFramework.cpp @@ -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( @@ -164,9 +166,8 @@ string AnalysisFramework::formatError( bool _withErrorIds ) const { - return SourceReferenceFormatter::formatExceptionInformation( + return SourceReferenceFormatter::formatErrorInformation( _error, - _error.type(), *m_compiler, _colored, _withErrorIds diff --git a/test/libsolidity/AnalysisFramework.h b/test/libsolidity/AnalysisFramework.h index 2e5d00e1d..d36884390 100644 --- a/test/libsolidity/AnalysisFramework.h +++ b/test/libsolidity/AnalysisFramework.h @@ -58,7 +58,7 @@ protected: 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;