solidity-upgrade can now change now to block.timestamp

This commit is contained in:
Harikrishnan Mulackal
2020-05-05 21:11:15 +05:30
parent a207613f17
commit aed6c22318
7 changed files with 61 additions and 2 deletions
+18
View File
@@ -267,6 +267,24 @@ public:
return "";
}
static std::string nowUpdate(langutil::SourceLocation const& _location)
{
std::regex nowRegex{"now"};
if (regex_search(_location.text(), nowRegex))
{
return regex_replace(_location.text(), nowRegex, "block.timestamp");
}
else
solAssert(
false,
LocationHelper() << "Could not fix: " << _location.text() << " at " << _location <<
"\nNeeds to be fixed manually."
);
return "";
}
};
}
+2
View File
@@ -196,6 +196,8 @@ Allowed options)",
m_suite.activateModule(Module::VirtualFunction);
else if (module == "dotsyntax")
m_suite.activateModule(Module::DotSyntax);
else if (module == "now")
m_suite.activateModule(Module::NowKeyword);
else
{
error() << "Unknown upgrade module \"" + module + "\"" << endl;
+6 -2
View File
@@ -58,7 +58,8 @@ private:
AbstractContract,
OverridingFunction,
VirtualFunction,
DotSyntax
DotSyntax,
NowKeyword
};
/// Upgrade suite that hosts all available modules.
@@ -84,6 +85,8 @@ private:
/// Solidity 0.7.0
if (isActivated(Module::DotSyntax))
DotSyntax{m_changes}.analyze(_sourceUnit);
if (isActivated(Module::NowKeyword))
NowKeyword{m_changes}.analyze(_sourceUnit);
}
void activateModule(Module _module) { m_modules.insert(_module); }
@@ -103,7 +106,8 @@ private:
Module::AbstractContract,
Module::OverridingFunction,
Module::VirtualFunction,
Module::DotSyntax
Module::DotSyntax,
Module::NowKeyword
};
};
+19
View File
@@ -40,3 +40,22 @@ void DotSyntax::endVisit(FunctionCall const& _functionCall)
);
}
}
void NowKeyword::endVisit(Identifier const& _identifier)
{
IdentifierAnnotation& annotation = _identifier.annotation();
if (MagicVariableDeclaration const* magicVar
= dynamic_cast<MagicVariableDeclaration const*>(annotation.referencedDeclaration))
{
if (magicVar->type()->category() == Type::Category::Integer)
{
solAssert(_identifier.name() == "now", "");
m_changes.emplace_back(
UpgradeChange::Level::Safe,
_identifier.location(),
SourceTransform::nowUpdate(_identifier.location())
);
}
}
}
+9
View File
@@ -32,4 +32,13 @@ private:
void endVisit(frontend::FunctionCall const& _expression) override;
};
class NowKeyword: public AnalysisUpgrade
{
public:
using AnalysisUpgrade::AnalysisUpgrade;
void analyze(frontend::SourceUnit const& _sourceUnit) { _sourceUnit.accept(*this); }
private:
void endVisit(frontend::Identifier const& _expression) override;
};
}