Skip to content

Commit

Permalink
Merge pull request ethereum#13409 from tcoyvwac/fix/refactor/reduce-c…
Browse files Browse the repository at this point in the history
…ode-duplication-ast

Reduce duplication in AST helpers for finding try/catch clauses
  • Loading branch information
Marenz authored Aug 30, 2022
2 parents 19e3c73 + 3d54bfd commit fa5ea38
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions libsolidity/ast/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <libsolidity/ast/TypeProvider.h>
#include <libsolutil/Keccak256.h>

#include <range/v3/view/tail.hpp>

#include <boost/algorithm/string.hpp>

#include <functional>
Expand All @@ -38,6 +40,17 @@ using namespace std;
using namespace solidity;
using namespace solidity::frontend;

namespace
{
TryCatchClause const* findClause(vector<ASTPointer<TryCatchClause>> const& _clauses, optional<string> _errorName = {})
{
for (auto const& clause: ranges::views::tail(_clauses))
if (_errorName.has_value() ? clause->errorName() == _errorName : clause->errorName().empty())
return clause.get();
return nullptr;
}
}

ASTNode::ASTNode(int64_t _id, SourceLocation _location):
m_id(static_cast<size_t>(_id)),
m_location(std::move(_location))
Expand Down Expand Up @@ -981,26 +994,14 @@ TryCatchClause const* TryStatement::successClause() const
return m_clauses[0].get();
}

TryCatchClause const* TryStatement::panicClause() const
{
for (size_t i = 1; i < m_clauses.size(); ++i)
if (m_clauses[i]->errorName() == "Panic")
return m_clauses[i].get();
return nullptr;
TryCatchClause const* TryStatement::panicClause() const {
return findClause(m_clauses, "Panic");
}

TryCatchClause const* TryStatement::errorClause() const
{
for (size_t i = 1; i < m_clauses.size(); ++i)
if (m_clauses[i]->errorName() == "Error")
return m_clauses[i].get();
return nullptr;
TryCatchClause const* TryStatement::errorClause() const {
return findClause(m_clauses, "Error");
}

TryCatchClause const* TryStatement::fallbackClause() const
{
for (size_t i = 1; i < m_clauses.size(); ++i)
if (m_clauses[i]->errorName().empty())
return m_clauses[i].get();
return nullptr;
TryCatchClause const* TryStatement::fallbackClause() const {
return findClause(m_clauses);
}

0 comments on commit fa5ea38

Please sign in to comment.