Skip to content

Commit fa5ea38

Browse files
authored
Merge pull request #13409 from tcoyvwac/fix/refactor/reduce-code-duplication-ast
Reduce duplication in AST helpers for finding try/catch clauses
2 parents 19e3c73 + 3d54bfd commit fa5ea38

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

libsolidity/ast/AST.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <libsolidity/ast/TypeProvider.h>
3030
#include <libsolutil/Keccak256.h>
3131

32+
#include <range/v3/view/tail.hpp>
33+
3234
#include <boost/algorithm/string.hpp>
3335

3436
#include <functional>
@@ -38,6 +40,17 @@ using namespace std;
3840
using namespace solidity;
3941
using namespace solidity::frontend;
4042

43+
namespace
44+
{
45+
TryCatchClause const* findClause(vector<ASTPointer<TryCatchClause>> const& _clauses, optional<string> _errorName = {})
46+
{
47+
for (auto const& clause: ranges::views::tail(_clauses))
48+
if (_errorName.has_value() ? clause->errorName() == _errorName : clause->errorName().empty())
49+
return clause.get();
50+
return nullptr;
51+
}
52+
}
53+
4154
ASTNode::ASTNode(int64_t _id, SourceLocation _location):
4255
m_id(static_cast<size_t>(_id)),
4356
m_location(std::move(_location))
@@ -981,26 +994,14 @@ TryCatchClause const* TryStatement::successClause() const
981994
return m_clauses[0].get();
982995
}
983996

984-
TryCatchClause const* TryStatement::panicClause() const
985-
{
986-
for (size_t i = 1; i < m_clauses.size(); ++i)
987-
if (m_clauses[i]->errorName() == "Panic")
988-
return m_clauses[i].get();
989-
return nullptr;
997+
TryCatchClause const* TryStatement::panicClause() const {
998+
return findClause(m_clauses, "Panic");
990999
}
9911000

992-
TryCatchClause const* TryStatement::errorClause() const
993-
{
994-
for (size_t i = 1; i < m_clauses.size(); ++i)
995-
if (m_clauses[i]->errorName() == "Error")
996-
return m_clauses[i].get();
997-
return nullptr;
1001+
TryCatchClause const* TryStatement::errorClause() const {
1002+
return findClause(m_clauses, "Error");
9981003
}
9991004

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

0 commit comments

Comments
 (0)