From 09e821619e20943af7bbd61ad10fb5ae40d2558c Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 13 Jul 2017 11:33:06 +0200 Subject: [PATCH] Refactor exceptions and provide comment function. --- libdevcore/CommonData.h | 3 +- libdevcore/Exceptions.cpp | 49 ++++++++++++++++++++++++ libdevcore/Exceptions.h | 20 ++++------ libevmasm/AssemblyItem.cpp | 11 +++--- libevmasm/GasMeter.cpp | 10 ++--- libsolidity/ast/AST.h | 17 +++++--- libsolidity/inlineasm/AsmScopeFiller.cpp | 2 + libsolidity/interface/Exceptions.cpp | 13 ------- test/libsolidity/ErrorCheck.cpp | 2 +- 9 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 libdevcore/Exceptions.cpp diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 98ad548d7..4297f606f 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -23,13 +23,14 @@ #pragma once +#include + #include #include #include #include #include #include -#include "Common.h" namespace dev { diff --git a/libdevcore/Exceptions.cpp b/libdevcore/Exceptions.cpp new file mode 100644 index 000000000..f422d9265 --- /dev/null +++ b/libdevcore/Exceptions.cpp @@ -0,0 +1,49 @@ +/* + 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 . +*/ + +#include + +#include + +using namespace std; +using namespace dev; + +char const* Exception::what() const noexcept +{ + if (string const* cmt = comment()) + return cmt->c_str(); + else + return nullptr; +} + +string Exception::lineInfo() const +{ + char const* const* file = boost::get_error_info(*this); + int const* line = boost::get_error_info(*this); + string ret; + if (file) + ret += *file; + ret += ':'; + if (line) + ret += boost::lexical_cast(*line); + return ret; +} + +string const* Exception::comment() const noexcept +{ + return boost::get_error_info(*this); +} diff --git a/libdevcore/Exceptions.h b/libdevcore/Exceptions.h index 4817e9e3e..a3e638bff 100644 --- a/libdevcore/Exceptions.h +++ b/libdevcore/Exceptions.h @@ -14,23 +14,16 @@ You should have received a copy of the GNU General Public License along with solidity. If not, see . */ -/** @file Exceptions.h - * @author Gav Wood - * @date 2014 - */ #pragma once -#include -#include #include #include #include #include -#include -#include -#include "CommonData.h" -#include "FixedHash.h" + +#include +#include namespace dev { @@ -38,14 +31,15 @@ namespace dev /// Base class for all exceptions. struct Exception: virtual std::exception, virtual boost::exception { - Exception(std::string _message = std::string()): m_message(std::move(_message)) {} - const char* what() const noexcept override { return m_message.empty() ? std::exception::what() : m_message.c_str(); } + const char* what() const noexcept override; /// @returns "FileName:LineNumber" referring to the point where the exception was thrown. std::string lineInfo() const; + /// @returns the errinfo_comment of this exception. + std::string const* comment() const noexcept; + private: - std::string m_message; }; #define DEV_SIMPLE_EXCEPTION(X) struct X: virtual Exception { const char* what() const noexcept override { return #X; } } diff --git a/libevmasm/AssemblyItem.cpp b/libevmasm/AssemblyItem.cpp index e69b59323..761048666 100644 --- a/libevmasm/AssemblyItem.cpp +++ b/libevmasm/AssemblyItem.cpp @@ -14,13 +14,14 @@ You should have received a copy of the GNU General Public License along with solidity. If not, see . */ -/** @file Assembly.cpp - * @author Gav Wood - * @date 2014 - */ -#include "AssemblyItem.h" +#include + #include + +#include +#include + #include using namespace std; diff --git a/libevmasm/GasMeter.cpp b/libevmasm/GasMeter.cpp index c96c6ca54..6a7c80e00 100644 --- a/libevmasm/GasMeter.cpp +++ b/libevmasm/GasMeter.cpp @@ -14,13 +14,13 @@ You should have received a copy of the GNU General Public License along with solidity. If not, see . */ -/** @file GasMeter.cpp - * @author Christian - * @date 2015 - */ -#include "GasMeter.h" +#include + #include + +#include + using namespace std; using namespace dev; using namespace dev::eth; diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index f90a9b2f1..cde14ea09 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -23,19 +23,24 @@ #pragma once -#include -#include -#include -#include -#include -#include #include #include #include #include #include + +#include +#include + +#include #include +#include + +#include +#include +#include + namespace dev { namespace solidity diff --git a/libsolidity/inlineasm/AsmScopeFiller.cpp b/libsolidity/inlineasm/AsmScopeFiller.cpp index 5b3174b86..b70ae9ac5 100644 --- a/libsolidity/inlineasm/AsmScopeFiller.cpp +++ b/libsolidity/inlineasm/AsmScopeFiller.cpp @@ -27,6 +27,8 @@ #include #include +#include + #include #include diff --git a/libsolidity/interface/Exceptions.cpp b/libsolidity/interface/Exceptions.cpp index 9f2a2d061..a837dce65 100644 --- a/libsolidity/interface/Exceptions.cpp +++ b/libsolidity/interface/Exceptions.cpp @@ -67,16 +67,3 @@ Error::Error(Error::Type _type, const std::string& _description, const SourceLoc *this << errinfo_sourceLocation(_location); *this << errinfo_comment(_description); } - -string Exception::lineInfo() const -{ - char const* const* file = boost::get_error_info(*this); - int const* line = boost::get_error_info(*this); - string ret; - if (file) - ret += *file; - ret += ':'; - if (line) - ret += boost::lexical_cast(*line); - return ret; -} diff --git a/test/libsolidity/ErrorCheck.cpp b/test/libsolidity/ErrorCheck.cpp index 9b0f9fb7f..00424b4ca 100644 --- a/test/libsolidity/ErrorCheck.cpp +++ b/test/libsolidity/ErrorCheck.cpp @@ -28,7 +28,7 @@ using namespace std; bool dev::solidity::searchErrorMessage(Error const& _err, std::string const& _substr) { - if (string const* errorMessage = boost::get_error_info(_err)) + if (string const* errorMessage = _err.comment()) { if (errorMessage->find(_substr) == std::string::npos) {