solidity-upgrade can now change now to block.timestamp

This commit is contained in:
Harikrishnan Mulackal 2020-05-05 16:34:01 +05:30
parent a207613f17
commit aed6c22318
7 changed files with 61 additions and 2 deletions

View File

@ -15,3 +15,4 @@ This section gives detailed instructions on how to update prior code for every b
* Change ``f.value(...)()`` to ``f{value: ...}()``. Similarly ``(new C).value(...)()`` to
``(new C){value: ...}()`` and ``f.gas(...)()`` to ``f{gas: ...}()``.
* Change ``now`` to ``block.timestamp``.

View File

@ -580,6 +580,12 @@ Available upgrade modules
| | | ``f{gas: ..., value: ...}()`` and |
| | | ``(new C){value: ...}()``. |
+-----------------+---------+--------------------------------------------------+
| ``now`` | 0.7.0 | The ``now`` keyword is deprecated. Use |
| | | ``block.timestamp`` instead. |
| | | |
| | | |
| | | |
+-----------------+---------+--------------------------------------------------+
Please read :doc:`0.5.0 release notes <050-breaking-changes>`,
:doc:`0.6.0 release notes <060-breaking-changes>` and

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 "";
}
};
}

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;

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
};
};

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())
);
}
}
}

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;
};
}