Merge pull request #2567 from ethereum/refactorExceptions

Refactor exceptions and provide comment function.
This commit is contained in:
chriseth 2017-07-13 16:56:03 +02:00 committed by GitHub
commit d1c3ecc94a
9 changed files with 83 additions and 44 deletions

View File

@ -23,13 +23,14 @@
#pragma once #pragma once
#include <libdevcore/Common.h>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <unordered_set>
#include <type_traits> #include <type_traits>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include "Common.h"
namespace dev namespace dev
{ {

49
libdevcore/Exceptions.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <libdevcore/Exceptions.h>
#include <boost/lexical_cast.hpp>
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<boost::throw_file>(*this);
int const* line = boost::get_error_info<boost::throw_line>(*this);
string ret;
if (file)
ret += *file;
ret += ':';
if (line)
ret += boost::lexical_cast<string>(*line);
return ret;
}
string const* Exception::comment() const noexcept
{
return boost::get_error_info<errinfo_comment>(*this);
}

View File

@ -14,23 +14,16 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file Exceptions.h
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#pragma once #pragma once
#include <exception>
#include <string>
#include <boost/exception/exception.hpp> #include <boost/exception/exception.hpp>
#include <boost/exception/info.hpp> #include <boost/exception/info.hpp>
#include <boost/exception/info_tuple.hpp> #include <boost/exception/info_tuple.hpp>
#include <boost/exception/diagnostic_information.hpp> #include <boost/exception/diagnostic_information.hpp>
#include <boost/throw_exception.hpp>
#include <boost/tuple/tuple.hpp> #include <exception>
#include "CommonData.h" #include <string>
#include "FixedHash.h"
namespace dev namespace dev
{ {
@ -38,14 +31,15 @@ namespace dev
/// Base class for all exceptions. /// Base class for all exceptions.
struct Exception: virtual std::exception, virtual boost::exception 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;
const char* what() const noexcept override { return m_message.empty() ? std::exception::what() : m_message.c_str(); }
/// @returns "FileName:LineNumber" referring to the point where the exception was thrown. /// @returns "FileName:LineNumber" referring to the point where the exception was thrown.
std::string lineInfo() const; std::string lineInfo() const;
/// @returns the errinfo_comment of this exception.
std::string const* comment() const noexcept;
private: private:
std::string m_message;
}; };
#define DEV_SIMPLE_EXCEPTION(X) struct X: virtual Exception { const char* what() const noexcept override { return #X; } } #define DEV_SIMPLE_EXCEPTION(X) struct X: virtual Exception { const char* what() const noexcept override { return #X; } }

View File

@ -14,13 +14,14 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file Assembly.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "AssemblyItem.h" #include <libevmasm/AssemblyItem.h>
#include <libevmasm/SemanticInformation.h> #include <libevmasm/SemanticInformation.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/FixedHash.h>
#include <fstream> #include <fstream>
using namespace std; using namespace std;

View File

@ -14,13 +14,13 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file GasMeter.cpp
* @author Christian <c@ethdev.com>
* @date 2015
*/
#include "GasMeter.h" #include <libevmasm/GasMeter.h>
#include <libevmasm/KnownState.h> #include <libevmasm/KnownState.h>
#include <libdevcore/FixedHash.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;

View File

@ -23,19 +23,24 @@
#pragma once #pragma once
#include <string>
#include <vector>
#include <memory>
#include <boost/noncopyable.hpp>
#include <libevmasm/SourceLocation.h>
#include <libevmasm/Instruction.h>
#include <libsolidity/ast/ASTForward.h> #include <libsolidity/ast/ASTForward.h>
#include <libsolidity/parsing/Token.h> #include <libsolidity/parsing/Token.h>
#include <libsolidity/ast/Types.h> #include <libsolidity/ast/Types.h>
#include <libsolidity/interface/Exceptions.h> #include <libsolidity/interface/Exceptions.h>
#include <libsolidity/ast/ASTAnnotations.h> #include <libsolidity/ast/ASTAnnotations.h>
#include <libevmasm/SourceLocation.h>
#include <libevmasm/Instruction.h>
#include <libdevcore/FixedHash.h>
#include <json/json.h> #include <json/json.h>
#include <boost/noncopyable.hpp>
#include <string>
#include <vector>
#include <memory>
namespace dev namespace dev
{ {
namespace solidity namespace solidity

View File

@ -27,6 +27,8 @@
#include <libsolidity/interface/ErrorReporter.h> #include <libsolidity/interface/ErrorReporter.h>
#include <libsolidity/interface/Exceptions.h> #include <libsolidity/interface/Exceptions.h>
#include <libdevcore/CommonData.h>
#include <boost/range/adaptor/reversed.hpp> #include <boost/range/adaptor/reversed.hpp>
#include <memory> #include <memory>

View File

@ -67,16 +67,3 @@ Error::Error(Error::Type _type, const std::string& _description, const SourceLoc
*this << errinfo_sourceLocation(_location); *this << errinfo_sourceLocation(_location);
*this << errinfo_comment(_description); *this << errinfo_comment(_description);
} }
string Exception::lineInfo() const
{
char const* const* file = boost::get_error_info<boost::throw_file>(*this);
int const* line = boost::get_error_info<boost::throw_line>(*this);
string ret;
if (file)
ret += *file;
ret += ':';
if (line)
ret += boost::lexical_cast<string>(*line);
return ret;
}

View File

@ -28,7 +28,7 @@ using namespace std;
bool dev::solidity::searchErrorMessage(Error const& _err, std::string const& _substr) bool dev::solidity::searchErrorMessage(Error const& _err, std::string const& _substr)
{ {
if (string const* errorMessage = boost::get_error_info<dev::errinfo_comment>(_err)) if (string const* errorMessage = _err.comment())
{ {
if (errorMessage->find(_substr) == std::string::npos) if (errorMessage->find(_substr) == std::string::npos)
{ {