From 5a15a4a6bd44252dec9081e30e4181aa56ed667c Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Thu, 26 Nov 2020 20:10:05 +0100 Subject: [PATCH] Fix a bug in NameSimplifier. A new name created by NameSimplifier could also be created by NameDispenser, since the knowledge of the new name was not stored in NameSimplifier. --- Changelog.md | 2 ++ libyul/optimiser/NameSimplifier.cpp | 30 +++++++++++++---------------- libyul/optimiser/NameSimplifier.h | 8 ++------ 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/Changelog.md b/Changelog.md index 0851a79d2..90f20f4ba 100644 --- a/Changelog.md +++ b/Changelog.md @@ -12,6 +12,8 @@ Compiler Features: Bugfixes: * SMTChecker: Fix internal compiler error when doing bitwise compound assignment with string literals. + * Yul Optimizer: Fix a bug in NameSimplifier where a new name created by NameSimplifier could also be created by NameDispenser. + * Yul Optimizer: Removed NameSimplifier from optimization steps available to users. ### 0.7.5 (2020-11-18) diff --git a/libyul/optimiser/NameSimplifier.cpp b/libyul/optimiser/NameSimplifier.cpp index 2e1bb822a..9054a9c39 100644 --- a/libyul/optimiser/NameSimplifier.cpp +++ b/libyul/optimiser/NameSimplifier.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -28,19 +30,13 @@ using namespace solidity::yul; using namespace std; -NameSimplifier::NameSimplifier( - OptimiserStepContext& _context, - Block const& _ast -): - m_context(_context), - m_usedNames(_context.reservedIdentifiers) +NameSimplifier::NameSimplifier(OptimiserStepContext& _context, Block const& _ast): + m_context(_context) { - for (YulString name: m_usedNames) + for (YulString name: _context.reservedIdentifiers) m_translations[name] = name; - set allNames = NameCollector(_ast).names(); - m_usedNames += allNames; - for (YulString name: allNames) + for (YulString const& name: NameCollector(_ast).names()) findSimplification(name); } @@ -77,7 +73,7 @@ void NameSimplifier::operator()(FunctionCall& _funCall) ASTModifier::operator()(_funCall); } -void NameSimplifier::findSimplification(YulString _name) +void NameSimplifier::findSimplification(YulString const& _name) { if (m_translations.count(_name)) return; @@ -98,19 +94,19 @@ void NameSimplifier::findSimplification(YulString _name) {regex("index_access_t_array"), "index_access"}, {regex("[0-9]*_$"), ""} }; + for (auto const& [pattern, substitute]: replacements) { string candidate = regex_replace(name, pattern, substitute); - if ( - !isRestrictedIdentifier(m_context.dialect, YulString(candidate)) && - !m_usedNames.count(YulString(candidate)) - ) + if (!m_context.dispenser.illegalName(YulString(candidate))) name = candidate; } + if (name != _name.str()) { - m_usedNames.insert(YulString(name)); - m_translations[_name] = YulString(name); + YulString newName{name}; + m_context.dispenser.markUsed(newName); + m_translations[_name] = move(newName); } } diff --git a/libyul/optimiser/NameSimplifier.h b/libyul/optimiser/NameSimplifier.h index 924e734f9..593a3bf5c 100644 --- a/libyul/optimiser/NameSimplifier.h +++ b/libyul/optimiser/NameSimplifier.h @@ -57,19 +57,15 @@ public: void operator()(FunctionDefinition& _funDef) override; private: - NameSimplifier( - OptimiserStepContext& _context, - Block const& _ast - ); + NameSimplifier(OptimiserStepContext& _context, Block const& _ast); /// Tries to rename a list of variables. void renameVariables(std::vector& _variables); - void findSimplification(YulString _name); + void findSimplification(YulString const& _name); void translate(YulString& _name); OptimiserStepContext& m_context; - std::set m_usedNames; std::map m_translations; };