From 40448172554baff0e1623e123c8fe4ecb59537aa Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 4 Nov 2020 00:15:01 +0000 Subject: [PATCH] Drop old SourceReferenceFormatter --- liblangutil/CMakeLists.txt | 2 - liblangutil/SourceReferenceFormatter.cpp | 101 ------------------ liblangutil/SourceReferenceFormatter.h | 86 --------------- liblangutil/SourceReferenceFormatterHuman.cpp | 10 ++ liblangutil/SourceReferenceFormatterHuman.h | 17 +-- 5 files changed, 20 insertions(+), 196 deletions(-) delete mode 100644 liblangutil/SourceReferenceFormatter.cpp delete mode 100644 liblangutil/SourceReferenceFormatter.h diff --git a/liblangutil/CMakeLists.txt b/liblangutil/CMakeLists.txt index ab30ac34e..ff6303d1f 100644 --- a/liblangutil/CMakeLists.txt +++ b/liblangutil/CMakeLists.txt @@ -19,8 +19,6 @@ set(sources SourceLocation.cpp SourceReferenceExtractor.cpp SourceReferenceExtractor.h - SourceReferenceFormatter.cpp - SourceReferenceFormatter.h SourceReferenceFormatterHuman.cpp SourceReferenceFormatterHuman.h Token.cpp diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp deleted file mode 100644 index d7a0a2169..000000000 --- a/liblangutil/SourceReferenceFormatter.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - This file is part of solidity. - - solidity is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - solidity is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with solidity. If not, see . -*/ -// SPDX-License-Identifier: GPL-3.0 -/** - * @author Christian - * @date 2014 - * Formatting functions for errors referencing positions and locations in the source. - */ - -#include -#include -#include - -using namespace std; -using namespace solidity; -using namespace solidity::util; -using namespace solidity::langutil; - -void SourceReferenceFormatter::printSourceLocation(SourceLocation const* _location) -{ - printSourceLocation(SourceReferenceExtractor::extract(_location)); -} - -void SourceReferenceFormatter::printSourceLocation(SourceReference const& _ref) -{ - if (_ref.position.line < 0) - return; // Nothing we can print here - - if (!_ref.multiline) - { - m_stream << _ref.text << endl; - - // mark the text-range like this: ^-----^ - for_each( - _ref.text.cbegin(), - _ref.text.cbegin() + _ref.startColumn, - [this](char ch) { m_stream << (ch == '\t' ? '\t' : ' '); } - ); - m_stream << "^"; - if (_ref.endColumn > _ref.startColumn + 2) - m_stream << string(static_cast(_ref.endColumn - _ref.startColumn - 2), '-'); - if (_ref.endColumn > _ref.startColumn + 1) - m_stream << "^"; - m_stream << endl; - } - else - m_stream << - _ref.text << - endl << - string(static_cast(_ref.startColumn), ' ') << - "^ (Relevant source part starts here and spans across multiple lines)." << - endl; -} - -void SourceReferenceFormatter::printSourceName(SourceReference const& _ref) -{ - if (_ref.position.line != -1) - m_stream << _ref.sourceName << ":" << (_ref.position.line + 1) << ":" << (_ref.position.column + 1) << ": "; - else if (!_ref.sourceName.empty()) - m_stream << _ref.sourceName << ": "; -} - -void SourceReferenceFormatter::printExceptionInformation(util::Exception const& _exception, std::string const& _category) -{ - printExceptionInformation(SourceReferenceExtractor::extract(_exception, _category)); -} - -void SourceReferenceFormatter::printErrorInformation(Error const& _error) -{ - printExceptionInformation(SourceReferenceExtractor::extract(_error)); -} - -void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtractor::Message const& _msg) -{ - printSourceName(_msg.primary); - - m_stream << _msg.category << ": " << _msg.primary.message << endl; - - printSourceLocation(_msg.primary); - - for (auto const& ref: _msg.secondary) - { - printSourceName(ref); - m_stream << ref.message << endl; - printSourceLocation(ref); - } -} diff --git a/liblangutil/SourceReferenceFormatter.h b/liblangutil/SourceReferenceFormatter.h deleted file mode 100644 index 636ddfcb0..000000000 --- a/liblangutil/SourceReferenceFormatter.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - This file is part of solidity. - - solidity is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - solidity is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with solidity. If not, see . -*/ -// SPDX-License-Identifier: GPL-3.0 -/** - * @author Christian - * @date 2014 - * Formatting functions for errors referencing positions and locations in the source. - */ - -#pragma once - -#include -#include -#include -#include -#include - -namespace solidity::util -{ -struct Exception; // forward -} - -namespace solidity::langutil -{ -struct SourceLocation; -class Scanner; - -class SourceReferenceFormatter -{ -public: - explicit SourceReferenceFormatter(std::ostream& _stream): - m_stream(_stream) - {} - - virtual ~SourceReferenceFormatter() = default; - - /// Prints source location if it is given. - virtual void printSourceLocation(SourceReference const& _ref); - virtual void printExceptionInformation(SourceReferenceExtractor::Message const& _msg); - - virtual void printSourceLocation(SourceLocation const* _location); - virtual void printExceptionInformation(util::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( - util::Exception const& _exception, - std::string const& _name - ) - { - std::ostringstream errorOutput; - - SourceReferenceFormatter formatter(errorOutput); - formatter.printExceptionInformation(_exception, _name); - return errorOutput.str(); - } - -protected: - /// Prints source name if location is given. - void printSourceName(SourceReference const& _ref); - - std::ostream& m_stream; -}; - -} diff --git a/liblangutil/SourceReferenceFormatterHuman.cpp b/liblangutil/SourceReferenceFormatterHuman.cpp index 282f0a0c9..bdcd64c16 100644 --- a/liblangutil/SourceReferenceFormatterHuman.cpp +++ b/liblangutil/SourceReferenceFormatterHuman.cpp @@ -171,3 +171,13 @@ void SourceReferenceFormatterHuman::printExceptionInformation(SourceReferenceExt m_stream << '\n'; } + +void SourceReferenceFormatterHuman::printExceptionInformation(util::Exception const& _exception, std::string const& _category) +{ + printExceptionInformation(SourceReferenceExtractor::extract(_exception, _category)); +} + +void SourceReferenceFormatterHuman::printErrorInformation(Error const& _error) +{ + printExceptionInformation(SourceReferenceExtractor::extract(_error)); +} diff --git a/liblangutil/SourceReferenceFormatterHuman.h b/liblangutil/SourceReferenceFormatterHuman.h index 53b26f36f..f7c96aafa 100644 --- a/liblangutil/SourceReferenceFormatterHuman.h +++ b/liblangutil/SourceReferenceFormatterHuman.h @@ -21,8 +21,8 @@ #pragma once +#include #include -#include // SourceReferenceFormatterBase #include @@ -32,17 +32,20 @@ namespace solidity::langutil { +struct SourceLocation; -class SourceReferenceFormatterHuman: public SourceReferenceFormatter +class SourceReferenceFormatterHuman { public: SourceReferenceFormatterHuman(std::ostream& _stream, bool _colored, bool _withErrorIds): - SourceReferenceFormatter{_stream}, m_colored{_colored}, m_withErrorIds(_withErrorIds) + m_stream(_stream), m_colored(_colored), m_withErrorIds(_withErrorIds) {} - void printSourceLocation(SourceReference const& _ref) override; - void printExceptionInformation(SourceReferenceExtractor::Message const& _msg) override; - using SourceReferenceFormatter::printExceptionInformation; + /// Prints source location if it is given. + void printSourceLocation(SourceReference const& _ref); + void printExceptionInformation(SourceReferenceExtractor::Message const& _msg); + void printExceptionInformation(util::Exception const& _exception, std::string const& _category); + void printErrorInformation(Error const& _error); static std::string formatExceptionInformation( util::Exception const& _exception, @@ -52,7 +55,6 @@ public: ) { std::ostringstream errorOutput; - SourceReferenceFormatterHuman formatter(errorOutput, _colored, _withErrorIds); formatter.printExceptionInformation(_exception, _name); return errorOutput.str(); @@ -76,6 +78,7 @@ private: util::AnsiColorized diagColored() const; private: + std::ostream& m_stream; bool m_colored; bool m_withErrorIds; };