From c2d2eec85cdd18d51a9963b24a930d173c630e40 Mon Sep 17 00:00:00 2001 From: Daniel Lupu Date: Fri, 28 Oct 2022 20:45:26 +0300 Subject: [PATCH] fix: apply coding style suggestions --- test/tools/yulopti.cpp | 114 ++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 48 deletions(-) diff --git a/test/tools/yulopti.cpp b/test/tools/yulopti.cpp index 710dab61d..57a473ce6 100644 --- a/test/tools/yulopti.cpp +++ b/test/tools/yulopti.cpp @@ -51,6 +51,7 @@ #include #include +#include #include #include #include @@ -100,10 +101,9 @@ public: const auto subObjectPath = _qualifiedPath.substr(_object->name.str().length() + 1); const auto subObjectName = subObjectPath.substr(0, subObjectPath.find_first_of('.')); - auto subObjectIt = find_if( - _object->subObjects.begin(), - _object->subObjects.end(), - [subObjectName](auto const& subObject) { return subObject->name.str() == subObjectName; } + auto subObjectIt = ranges::find_if( + _object->subObjects, + [subObjectName](auto const& _subObject) { return _subObject->name.str() == subObjectName; } ); yulAssert( @@ -125,13 +125,13 @@ public: { ErrorList errors; ErrorReporter errorReporter(errors); - CharStream _charStream(_input, ""); + CharStream charStream(_input, ""); try { ObjectParser parser(errorReporter, m_dialect); - auto scanner = make_shared(_charStream); + auto scanner = make_shared(charStream); if (!m_inputWasCodeBlock && scanner->currentToken() == Token::LBrace) m_inputWasCodeBlock = true; @@ -144,16 +144,16 @@ public: if (!m_object || !errorReporter.errors().empty()) { cerr << "Error parsing source." << endl; - printErrors(_charStream, errors); - throw std::runtime_error("Could not parse source."); + printErrors(charStream, errors); + throw runtime_error("Could not parse source."); } - analyze(errorReporter); + runCodeAnalyzer(errorReporter); } catch(...) { cerr << "Fatal error during parsing: " << endl; - printErrors(_charStream, errors); + printErrors(charStream, errors); throw; } } @@ -166,7 +166,7 @@ public: yulAssert(_columns > 0); auto const& optimiserSteps = OptimiserSuite::stepAbbreviationToNameMap(); auto hasShorterString = [](auto const& a, auto const& b) { return a.second.size() < b.second.size(); }; - size_t longestDescriptionLength = std::max( + size_t longestDescriptionLength = max( max_element(optimiserSteps.begin(), optimiserSteps.end(), hasShorterString)->second.size(), max_element(_extraOptions.begin(), _extraOptions.end(), hasShorterString)->second.size() ); @@ -207,66 +207,86 @@ public: } } - void objectApply(shared_ptr _object, function _fn) + void objectApplyFunction(shared_ptr _object, function _fn) { for (auto const& subObjectNode: _object->subObjects) { auto subObject = dynamic_pointer_cast(subObjectNode); if (subObject != nullptr) - objectApply(subObject, _fn); + objectApplyFunction(subObject, _fn); } _fn(*_object); } - void analyze(ErrorReporter& errorReporter) + void runCodeAnalyzer(ErrorReporter& _errorReporter) { - objectApply(m_object, [&](Object& object) { - object.analysisInfo = make_shared(); + objectApplyFunction( + m_object, + [&](Object& _object) + { + _object.analysisInfo = make_shared(); - AsmAnalyzer analyzer( - *object.analysisInfo, - errorReporter, - m_dialect, - {}, - object.qualifiedDataNames() - ); + AsmAnalyzer analyzer( + *_object.analysisInfo, + _errorReporter, + m_dialect, + {}, + _object.qualifiedDataNames() + ); - bool success = analyzer.analyze(*object.code); - yulAssert(success && !errorReporter.hasErrors(), "Invalid assembly/yul code."); - }); + bool success = analyzer.analyze(*_object.code); + yulAssert(success && !_errorReporter.hasErrors(), "Invalid assembly/yul code."); + } + ); } - void disambiguate() + void runCodeDisambiguator() { - objectApply(m_object, [&](Object& object) { - object.code = make_shared( - std::get(Disambiguator(m_dialect, *object.analysisInfo)(*object.code)) - ); + objectApplyFunction( + m_object, + [&](Object& _object) + { + _object.code = make_shared( + get(Disambiguator(m_dialect, *_object.analysisInfo)(*_object.code)) + ); - object.analysisInfo.reset(); - }); + _object.analysisInfo.reset(); + } + ); } void runSequence(string_view _steps) { - objectApply(m_object, [&](Object& object) { - OptimiserSuite{*m_context}.runSequence(_steps, *object.code); - }); + objectApplyFunction( + m_object, + [&](Object& _object) + { + OptimiserSuite{*m_context}.runSequence(_steps, *_object.code); + } + ); } void runVarNameCleaner() { - objectApply(m_object, [&](Object& object) { - VarNameCleaner::run(*m_context, *object.code); - }); + objectApplyFunction( + m_object, + [&](Object& _object) + { + VarNameCleaner::run(*m_context, *_object.code); + } + ); } void runStackCompressor() { - objectApply(m_object, [&](Object& object) { - StackCompressor::run(m_dialect, object, true, 16); - }); + objectApplyFunction( + m_object, + [&](Object& _object) + { + StackCompressor::run(m_dialect, _object, true, 16); + } + ); } void parseAndPrint(string _source, string _objectPath) @@ -303,7 +323,7 @@ public: void runSteps(string _source, string _objectPath, string _steps) { parse(_source, _objectPath); - disambiguate(); + runCodeDisambiguator(); runSequence(_steps); } @@ -317,7 +337,7 @@ public: while (true) { - disambiguated = disambiguated || (disambiguate(), true); + disambiguated = disambiguated || (runCodeDisambiguator(), true); map const& extraOptions = { // QUIT starts with a non-letter character on purpose to get it to show up on top of the list @@ -345,16 +365,14 @@ public: disambiguated = false; break; case ';': - { runStackCompressor(); break; - } default: - runSequence(std::string_view(&option, 1)); + runSequence(string_view(&option, 1)); } resetNameDispenser(); - analyze(errorReporter); + runCodeAnalyzer(errorReporter); } catch (...) {