From 28593839d9913a740e9c8514d2ba607241d54398 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 15 Mar 2022 14:24:42 +0100 Subject: [PATCH] Add helper to see if a switch has a default case. --- libsolidity/analysis/ControlFlowBuilder.cpp | 3 +-- libyul/AST.h | 9 +++++++++ libyul/optimiser/ControlFlowSimplifier.cpp | 8 +------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/libsolidity/analysis/ControlFlowBuilder.cpp b/libsolidity/analysis/ControlFlowBuilder.cpp index 3de1ecca5..7c568b18e 100644 --- a/libsolidity/analysis/ControlFlowBuilder.cpp +++ b/libsolidity/analysis/ControlFlowBuilder.cpp @@ -457,8 +457,7 @@ void ControlFlowBuilder::operator()(yul::Switch const& _switch) } mergeFlow(nodes); - bool hasDefault = util::contains_if(_switch.cases, [](yul::Case const& _case) { return !_case.value; }); - if (!hasDefault) + if (!hasDefaultCase(_switch)) connect(beforeSwitch, m_currentNode); } diff --git a/libyul/AST.h b/libyul/AST.h index 1c7b8e02a..71419d807 100644 --- a/libyul/AST.h +++ b/libyul/AST.h @@ -144,4 +144,13 @@ template inline std::shared_ptr debugDataOf(std return std::visit([](auto const& _arg) { return debugDataOf(_arg); }, _node); } +inline bool hasDefaultCase(Switch const& _switch) +{ + return std::any_of( + _switch.cases.begin(), + _switch.cases.end(), + [](Case const& _case) { return !_case.value; } + ); +} + } diff --git a/libyul/optimiser/ControlFlowSimplifier.cpp b/libyul/optimiser/ControlFlowSimplifier.cpp index 443a2a9be..cfd59d09a 100644 --- a/libyul/optimiser/ControlFlowSimplifier.cpp +++ b/libyul/optimiser/ControlFlowSimplifier.cpp @@ -60,13 +60,7 @@ void removeEmptyDefaultFromSwitch(Switch& _switchStmt) void removeEmptyCasesFromSwitch(Switch& _switchStmt) { - bool hasDefault = std::any_of( - _switchStmt.cases.begin(), - _switchStmt.cases.end(), - [](Case const& _case) { return !_case.value; } - ); - - if (hasDefault) + if (hasDefaultCase(_switchStmt)) return; ranges::actions::remove_if(