mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add a warning about a varialbe of the name of an instruction
This commit is contained in:
parent
a7241df4b7
commit
8775e77305
@ -26,6 +26,8 @@
|
|||||||
#include <libsolidity/analysis/TypeChecker.h>
|
#include <libsolidity/analysis/TypeChecker.h>
|
||||||
#include <libsolidity/interface/ErrorReporter.h>
|
#include <libsolidity/interface/ErrorReporter.h>
|
||||||
|
|
||||||
|
#include <boost/algorithm/string.hpp>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace dev
|
namespace dev
|
||||||
@ -232,6 +234,26 @@ vector<Declaration const*> NameAndTypeResolver::cleanedDeclarations(
|
|||||||
return uniqueFunctions;
|
return uniqueFunctions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NameAndTypeResolver::warnVariablesNamedLikeInstructions()
|
||||||
|
{
|
||||||
|
for (auto const& instruction: c_instructions)
|
||||||
|
{
|
||||||
|
string const instructionName{boost::algorithm::to_lower_copy(instruction.first)};
|
||||||
|
auto declarations = nameFromCurrentScope(instructionName);
|
||||||
|
for (Declaration const* const declaration: declarations)
|
||||||
|
{
|
||||||
|
solAssert(!!declaration, "");
|
||||||
|
if (dynamic_cast<MagicVariableDeclaration const* const>(declaration))
|
||||||
|
// Don't warn the user for what the user did not.
|
||||||
|
continue;
|
||||||
|
m_errorReporter.warning(
|
||||||
|
declaration->location(),
|
||||||
|
"Variable is shadowed in inline assembly by an instruction of the same name"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode)
|
bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode)
|
||||||
{
|
{
|
||||||
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(&_node))
|
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(&_node))
|
||||||
|
@ -90,6 +90,9 @@ public:
|
|||||||
std::vector<Declaration const*> const& _declarations
|
std::vector<Declaration const*> const& _declarations
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// Generate and store warnings about variables that are named like instructions.
|
||||||
|
void warnVariablesNamedLikeInstructions();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Internal version of @a resolveNamesAndTypes (called from there) throws exceptions on fatal errors.
|
/// Internal version of @a resolveNamesAndTypes (called from there) throws exceptions on fatal errors.
|
||||||
bool resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode = true);
|
bool resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode = true);
|
||||||
|
@ -162,6 +162,8 @@ void ReferencesResolver::endVisit(ArrayTypeName const& _typeName)
|
|||||||
|
|
||||||
bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
|
bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
|
||||||
{
|
{
|
||||||
|
m_resolver.warnVariablesNamedLikeInstructions();
|
||||||
|
|
||||||
// Errors created in this stage are completely ignored because we do not yet know
|
// Errors created in this stage are completely ignored because we do not yet know
|
||||||
// the type and size of external identifiers, which would result in false errors.
|
// the type and size of external identifiers, which would result in false errors.
|
||||||
// The only purpose of this step is to fill the inline assembly annotation with
|
// The only purpose of this step is to fill the inline assembly annotation with
|
||||||
|
@ -5790,7 +5790,7 @@ BOOST_AUTO_TEST_CASE(returndatacopy_as_variable)
|
|||||||
char const* text = R"(
|
char const* text = R"(
|
||||||
contract c { function f() { uint returndatasize; assembly { returndatasize }}}
|
contract c { function f() { uint returndatasize; assembly { returndatasize }}}
|
||||||
)";
|
)";
|
||||||
CHECK_WARNING_ALLOW_MULTI(text, "shadowed by an insturction of the same name");
|
CHECK_WARNING_ALLOW_MULTI(text, "Variable is shadowed in inline assembly by an instruction of the same name");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(shadowing_warning_can_be_removed)
|
BOOST_AUTO_TEST_CASE(shadowing_warning_can_be_removed)
|
||||||
|
Loading…
Reference in New Issue
Block a user