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.
This commit is contained in:
hrkrshnn 2020-11-26 20:10:05 +01:00
parent cf6fe5a777
commit 5a15a4a6bd
3 changed files with 17 additions and 23 deletions

View File

@ -12,6 +12,8 @@ Compiler Features:
Bugfixes: Bugfixes:
* SMTChecker: Fix internal compiler error when doing bitwise compound assignment with string literals. * 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) ### 0.7.5 (2020-11-18)

View File

@ -19,6 +19,8 @@
#include <libyul/optimiser/NameCollector.h> #include <libyul/optimiser/NameCollector.h>
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <libyul/Dialect.h> #include <libyul/Dialect.h>
#include <libyul/YulString.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/optimiser/OptimizerUtilities.h> #include <libyul/optimiser/OptimizerUtilities.h>
#include <libsolutil/CommonData.h> #include <libsolutil/CommonData.h>
@ -28,19 +30,13 @@
using namespace solidity::yul; using namespace solidity::yul;
using namespace std; using namespace std;
NameSimplifier::NameSimplifier( NameSimplifier::NameSimplifier(OptimiserStepContext& _context, Block const& _ast):
OptimiserStepContext& _context, m_context(_context)
Block const& _ast
):
m_context(_context),
m_usedNames(_context.reservedIdentifiers)
{ {
for (YulString name: m_usedNames) for (YulString name: _context.reservedIdentifiers)
m_translations[name] = name; m_translations[name] = name;
set<YulString> allNames = NameCollector(_ast).names(); for (YulString const& name: NameCollector(_ast).names())
m_usedNames += allNames;
for (YulString name: allNames)
findSimplification(name); findSimplification(name);
} }
@ -77,7 +73,7 @@ void NameSimplifier::operator()(FunctionCall& _funCall)
ASTModifier::operator()(_funCall); ASTModifier::operator()(_funCall);
} }
void NameSimplifier::findSimplification(YulString _name) void NameSimplifier::findSimplification(YulString const& _name)
{ {
if (m_translations.count(_name)) if (m_translations.count(_name))
return; return;
@ -98,19 +94,19 @@ void NameSimplifier::findSimplification(YulString _name)
{regex("index_access_t_array"), "index_access"}, {regex("index_access_t_array"), "index_access"},
{regex("[0-9]*_$"), ""} {regex("[0-9]*_$"), ""}
}; };
for (auto const& [pattern, substitute]: replacements) for (auto const& [pattern, substitute]: replacements)
{ {
string candidate = regex_replace(name, pattern, substitute); string candidate = regex_replace(name, pattern, substitute);
if ( if (!m_context.dispenser.illegalName(YulString(candidate)))
!isRestrictedIdentifier(m_context.dialect, YulString(candidate)) &&
!m_usedNames.count(YulString(candidate))
)
name = candidate; name = candidate;
} }
if (name != _name.str()) if (name != _name.str())
{ {
m_usedNames.insert(YulString(name)); YulString newName{name};
m_translations[_name] = YulString(name); m_context.dispenser.markUsed(newName);
m_translations[_name] = move(newName);
} }
} }

View File

@ -57,19 +57,15 @@ public:
void operator()(FunctionDefinition& _funDef) override; void operator()(FunctionDefinition& _funDef) override;
private: private:
NameSimplifier( NameSimplifier(OptimiserStepContext& _context, Block const& _ast);
OptimiserStepContext& _context,
Block const& _ast
);
/// Tries to rename a list of variables. /// Tries to rename a list of variables.
void renameVariables(std::vector<TypedName>& _variables); void renameVariables(std::vector<TypedName>& _variables);
void findSimplification(YulString _name); void findSimplification(YulString const& _name);
void translate(YulString& _name); void translate(YulString& _name);
OptimiserStepContext& m_context; OptimiserStepContext& m_context;
std::set<YulString> m_usedNames;
std::map<YulString, YulString> m_translations; std::map<YulString, YulString> m_translations;
}; };