solidity-upgrade: added module to remove visibility of constructors

This commit is contained in:
Harikrishnan Mulackal 2020-06-24 15:13:31 +05:30
parent 64b6524bdb
commit 6b435b2192
6 changed files with 81 additions and 30 deletions

View File

@ -566,33 +566,36 @@ the latest version of the compiler.
Available upgrade modules Available upgrade modules
~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| Module | Version | Description | | Module | Version | Description |
+=================+=========+==================================================+ +============================+=========+==================================================+
| ``constructor`` | 0.5.0 | Constructors must now be defined using the | | ``constructor`` | 0.5.0 | Constructors must now be defined using the |
| | | ``constructor`` keyword. | | | | ``constructor`` keyword. |
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| ``visibility`` | 0.5.0 | Explicit function visibility is now mandatory, | | ``visibility`` | 0.5.0 | Explicit function visibility is now mandatory, |
| | | defaults to ``public``. | | | | defaults to ``public``. |
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| ``abstract`` | 0.6.0 | The keyword ``abstract`` has to be used if a | | ``abstract`` | 0.6.0 | The keyword ``abstract`` has to be used if a |
| | | contract does not implement all its functions. | | | | contract does not implement all its functions. |
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| ``virtual`` | 0.6.0 | Functions without implementation outside an | | ``virtual`` | 0.6.0 | Functions without implementation outside an |
| | | interface have to be marked ``virtual``. | | | | interface have to be marked ``virtual``. |
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| ``override`` | 0.6.0 | When overriding a function or modifier, the new | | ``override`` | 0.6.0 | When overriding a function or modifier, the new |
| | | keyword ``override`` must be used. | | | | keyword ``override`` must be used. |
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| ``dotsyntax`` | 0.7.0 | The following syntax is deprecated: | | ``dotsyntax`` | 0.7.0 | The following syntax is deprecated: |
| | | ``f.gas(...)()``, ``f.value(...)()`` and | | | | ``f.gas(...)()``, ``f.value(...)()`` and |
| | | ``(new C).value(...)()``. Replace these calls by | | | | ``(new C).value(...)()``. Replace these calls by |
| | | ``f{gas: ..., value: ...}()`` and | | | | ``f{gas: ..., value: ...}()`` and |
| | | ``(new C){value: ...}()``. | | | | ``(new C){value: ...}()``. |
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| ``now`` | 0.7.0 | The ``now`` keyword is deprecated. Use | | ``now`` | 0.7.0 | The ``now`` keyword is deprecated. Use |
| | | ``block.timestamp`` instead. | | | | ``block.timestamp`` instead. |
+-----------------+---------+--------------------------------------------------+ +----------------------------+---------+--------------------------------------------------+
| ``constructor-visibility`` | 0.7.0 | Removes visibility of constructors. |
| | | |
+----------------------------+---------+--------------------------------------------------+
Please read :doc:`0.5.0 release notes <050-breaking-changes>`, Please read :doc:`0.5.0 release notes <050-breaking-changes>`,
:doc:`0.6.0 release notes <060-breaking-changes>` and :doc:`0.6.0 release notes <060-breaking-changes>` and

View File

@ -132,7 +132,8 @@ public:
return regex_replace( return regex_replace(
_location.text(), _location.text(),
_regex, _regex,
_expression + " " + _keyword _expression + " " + _keyword,
std::regex_constants::format_first_only
); );
else else
solAssert( solAssert(
@ -274,6 +275,14 @@ public:
{ {
return regex_replace(_location.text(), std::regex{"now"}, "block.timestamp"); return regex_replace(_location.text(), std::regex{"now"}, "block.timestamp");
} }
static std::string removeVisibility(langutil::SourceLocation const& _location)
{
std::string replacement = _location.text();
for (auto const& replace: {"public ", "public", "internal ", "internal", "external ", "external"})
replacement = regex_replace(replacement, std::regex{replace}, "");
return replacement;
}
}; };
} }

View File

@ -198,6 +198,8 @@ Allowed options)",
m_suite.activateModule(Module::DotSyntax); m_suite.activateModule(Module::DotSyntax);
else if (module == "now") else if (module == "now")
m_suite.activateModule(Module::NowKeyword); m_suite.activateModule(Module::NowKeyword);
else if (module == "constructor-visibility")
m_suite.activateModule(Module::ConstrutorVisibility);
else else
{ {
error() << "Unknown upgrade module \"" + module + "\"" << endl; error() << "Unknown upgrade module \"" + module + "\"" << endl;

View File

@ -59,7 +59,8 @@ private:
OverridingFunction, OverridingFunction,
VirtualFunction, VirtualFunction,
DotSyntax, DotSyntax,
NowKeyword NowKeyword,
ConstrutorVisibility
}; };
/// Upgrade suite that hosts all available modules. /// Upgrade suite that hosts all available modules.
@ -87,6 +88,8 @@ private:
DotSyntax{m_changes}.analyze(_sourceUnit); DotSyntax{m_changes}.analyze(_sourceUnit);
if (isActivated(Module::NowKeyword)) if (isActivated(Module::NowKeyword))
NowKeyword{m_changes}.analyze(_sourceUnit); NowKeyword{m_changes}.analyze(_sourceUnit);
if (isActivated(Module::ConstrutorVisibility))
ConstructorVisibility{m_changes}.analyze(_sourceUnit);
} }
void activateModule(Module _module) { m_modules.insert(_module); } void activateModule(Module _module) { m_modules.insert(_module); }
@ -107,7 +110,8 @@ private:
Module::OverridingFunction, Module::OverridingFunction,
Module::VirtualFunction, Module::VirtualFunction,
Module::DotSyntax, Module::DotSyntax,
Module::NowKeyword Module::NowKeyword,
Module::ConstrutorVisibility
}; };
}; };

View File

@ -59,3 +59,27 @@ void NowKeyword::endVisit(Identifier const& _identifier)
); );
} }
} }
void ConstructorVisibility::endVisit(ContractDefinition const& _contract)
{
if (!_contract.abstract())
for (FunctionDefinition const* function: _contract.definedFunctions())
if (
function->isConstructor() &&
!function->noVisibilitySpecified() &&
function->visibility() == Visibility::Internal
)
m_changes.emplace_back(
UpgradeChange::Level::Safe,
_contract.location(),
SourceTransform::insertBeforeKeyword(_contract.location(), "contract", "abstract")
);
for (FunctionDefinition const* function: _contract.definedFunctions())
if (function->isConstructor() && !function->noVisibilitySpecified())
m_changes.emplace_back(
UpgradeChange::Level::Safe,
function->location(),
SourceTransform::removeVisibility(function->location())
);
}

View File

@ -41,4 +41,13 @@ private:
void endVisit(frontend::Identifier const& _expression) override; void endVisit(frontend::Identifier const& _expression) override;
}; };
class ConstructorVisibility: public AnalysisUpgrade
{
public:
using AnalysisUpgrade::AnalysisUpgrade;
void analyze(frontend::SourceUnit const& _sourceUnit) { _sourceUnit.accept(*this); }
private:
void endVisit(frontend::ContractDefinition const& _contract) override;
};
} }