aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis
diff options
context:
space:
mode:
Diffstat (limited to 'libsolidity/analysis')
-rw-r--r--libsolidity/analysis/PostTypeChecker.cpp108
-rw-r--r--libsolidity/analysis/PostTypeChecker.h69
-rw-r--r--libsolidity/analysis/StaticAnalyzer.h3
-rw-r--r--libsolidity/analysis/TypeChecker.cpp35
4 files changed, 207 insertions, 8 deletions
diff --git a/libsolidity/analysis/PostTypeChecker.cpp b/libsolidity/analysis/PostTypeChecker.cpp
new file mode 100644
index 00000000..cae77c74
--- /dev/null
+++ b/libsolidity/analysis/PostTypeChecker.cpp
@@ -0,0 +1,108 @@
+/*
+ This file is part of solidity.
+
+ solidity is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ solidity is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with solidity. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <libsolidity/analysis/PostTypeChecker.h>
+#include <libsolidity/ast/AST.h>
+#include <libsolidity/analysis/SemVerHandler.h>
+#include <libsolidity/interface/Version.h>
+
+#include <boost/range/adaptor/map.hpp>
+
+#include <memory>
+
+using namespace std;
+using namespace dev;
+using namespace dev::solidity;
+
+
+bool PostTypeChecker::check(ASTNode const& _astRoot)
+{
+ _astRoot.accept(*this);
+ return Error::containsOnlyWarnings(m_errors);
+}
+
+void PostTypeChecker::typeError(SourceLocation const& _location, std::string const& _description)
+{
+ auto err = make_shared<Error>(Error::Type::TypeError);
+ *err <<
+ errinfo_sourceLocation(_location) <<
+ errinfo_comment(_description);
+
+ m_errors.push_back(err);
+}
+
+bool PostTypeChecker::visit(ContractDefinition const&)
+{
+ solAssert(!m_currentConstVariable, "");
+ solAssert(m_constVariableDependencies.empty(), "");
+ return true;
+}
+
+void PostTypeChecker::endVisit(ContractDefinition const&)
+{
+ solAssert(!m_currentConstVariable, "");
+ for (auto declaration: m_constVariables)
+ if (auto identifier = findCycle(declaration))
+ typeError(declaration->location(), "The value of the constant " + declaration->name() + " has a cyclic dependency via " + identifier->name() + ".");
+}
+
+bool PostTypeChecker::visit(VariableDeclaration const& _variable)
+{
+ solAssert(!m_currentConstVariable, "");
+ if (_variable.isConstant())
+ {
+ m_currentConstVariable = &_variable;
+ m_constVariables.push_back(&_variable);
+ }
+ return true;
+}
+
+void PostTypeChecker::endVisit(VariableDeclaration const& _variable)
+{
+ if (_variable.isConstant())
+ {
+ solAssert(m_currentConstVariable == &_variable, "");
+ m_currentConstVariable = nullptr;
+ }
+}
+
+bool PostTypeChecker::visit(Identifier const& _identifier)
+{
+ if (m_currentConstVariable)
+ if (auto var = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
+ if (var->isConstant())
+ m_constVariableDependencies[m_currentConstVariable].insert(var);
+ return true;
+}
+
+VariableDeclaration const* PostTypeChecker::findCycle(
+ VariableDeclaration const* _startingFrom,
+ set<VariableDeclaration const*> const& _seen
+)
+{
+ if (_seen.count(_startingFrom))
+ return _startingFrom;
+ else if (m_constVariableDependencies.count(_startingFrom))
+ {
+ set<VariableDeclaration const*> seen(_seen);
+ seen.insert(_startingFrom);
+ for (auto v: m_constVariableDependencies[_startingFrom])
+ if (findCycle(v, seen))
+ return v;
+ }
+ return nullptr;
+}
diff --git a/libsolidity/analysis/PostTypeChecker.h b/libsolidity/analysis/PostTypeChecker.h
new file mode 100644
index 00000000..8774f413
--- /dev/null
+++ b/libsolidity/analysis/PostTypeChecker.h
@@ -0,0 +1,69 @@
+/*
+ This file is part of solidity.
+
+ solidity is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ solidity is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with solidity. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#pragma once
+
+#include <libsolidity/analysis/TypeChecker.h>
+#include <libsolidity/ast/Types.h>
+#include <libsolidity/ast/ASTAnnotations.h>
+#include <libsolidity/ast/ASTForward.h>
+#include <libsolidity/ast/ASTVisitor.h>
+
+namespace dev
+{
+namespace solidity
+{
+
+/**
+ * This module performs analyses on the AST that are done after type checking and assignments of types:
+ * - whether there are circular references in constant state variables
+ * @TODO factor out each use-case into an individual class (but do the traversal only once)
+ */
+class PostTypeChecker: private ASTConstVisitor
+{
+public:
+ /// @param _errors the reference to the list of errors and warnings to add them found during type checking.
+ PostTypeChecker(ErrorList& _errors): m_errors(_errors) {}
+
+ bool check(ASTNode const& _astRoot);
+
+private:
+ /// Adds a new error to the list of errors.
+ void typeError(SourceLocation const& _location, std::string const& _description);
+
+ virtual bool visit(ContractDefinition const& _contract) override;
+ virtual void endVisit(ContractDefinition const& _contract) override;
+
+ virtual bool visit(VariableDeclaration const& _declaration) override;
+ virtual void endVisit(VariableDeclaration const& _declaration) override;
+
+ virtual bool visit(Identifier const& _identifier) override;
+
+ VariableDeclaration const* findCycle(
+ VariableDeclaration const* _startingFrom,
+ std::set<VariableDeclaration const*> const& _seen = {}
+ );
+
+ ErrorList& m_errors;
+
+ VariableDeclaration const* m_currentConstVariable = nullptr;
+ std::vector<VariableDeclaration const*> m_constVariables; ///< Required for determinism.
+ std::map<VariableDeclaration const*, std::set<VariableDeclaration const*>> m_constVariableDependencies;
+};
+
+}
+}
diff --git a/libsolidity/analysis/StaticAnalyzer.h b/libsolidity/analysis/StaticAnalyzer.h
index b6cf783e..0cb961bd 100644
--- a/libsolidity/analysis/StaticAnalyzer.h
+++ b/libsolidity/analysis/StaticAnalyzer.h
@@ -36,6 +36,9 @@ namespace solidity
/**
* The module that performs static analysis on the AST.
+ * In this context, static analysis is anything that can produce warnings which can help
+ * programmers write cleaner code. For every warning generated eher, it has to be possible to write
+ * equivalent code that does generate the warning.
*/
class StaticAnalyzer: private ASTConstVisitor
{
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp
index a9a2c82f..549cbcca 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -590,8 +590,12 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
// Inline assembly does not have its own type-checking phase, so we just run the
// code-generator and see whether it produces any errors.
// External references have already been resolved in a prior stage and stored in the annotation.
- assembly::CodeGenerator codeGen(_inlineAssembly.operations(), m_errors);
- codeGen.typeCheck([&](assembly::Identifier const& _identifier, eth::Assembly& _assembly, assembly::CodeGenerator::IdentifierContext _context) {
+ auto identifierAccess = [&](
+ assembly::Identifier const& _identifier,
+ eth::Assembly& _assembly,
+ assembly::CodeGenerator::IdentifierContext _context
+ )
+ {
auto ref = _inlineAssembly.annotation().externalReferences.find(&_identifier);
if (ref == _inlineAssembly.annotation().externalReferences.end())
return false;
@@ -639,8 +643,11 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
return false;
}
return true;
- });
- return false;
+ };
+ assembly::CodeGenerator codeGen(_inlineAssembly.operations(), m_errors);
+ if (!codeGen.typeCheck(identifierAccess))
+ return false;
+ return true;
}
bool TypeChecker::visit(IfStatement const& _ifStatement)
@@ -728,13 +735,16 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement)
if (auto ref = dynamic_cast<ReferenceType const*>(type(varDecl).get()))
{
if (ref->dataStoredIn(DataLocation::Storage))
- {
warning(
varDecl.location(),
"Uninitialized storage pointer. Did you mean '<type> memory " + varDecl.name() + "'?"
);
- }
}
+ else if (dynamic_cast<MappingType const*>(type(varDecl).get()))
+ typeError(
+ varDecl.location(),
+ "Uninitialized mapping. Mappings cannot be created dynamically, you have to assign them from a state variable."
+ );
varDecl.accept(*this);
return false;
}
@@ -935,6 +945,11 @@ bool TypeChecker::visit(Assignment const& _assignment)
_assignment.annotation().type = t;
if (TupleType const* tupleType = dynamic_cast<TupleType const*>(t.get()))
{
+ if (_assignment.assignmentOperator() != Token::Assign)
+ typeError(
+ _assignment.location(),
+ "Compound assignment is not allowed for tuple types."
+ );
// Sequenced assignments of tuples is not valid, make the result a "void" type.
_assignment.annotation().type = make_shared<TupleType>();
expectType(_assignment.rightHandSide(), *tupleType);
@@ -1012,7 +1027,7 @@ bool TypeChecker::visit(TupleExpression const& _tuple)
fatalTypeError(components[i]->location(), "Invalid mobile type.");
if (i == 0)
- inlineArrayType = types[i];
+ inlineArrayType = types[i]->mobileType();
else if (inlineArrayType)
inlineArrayType = Type::commonType(inlineArrayType, types[i]);
}
@@ -1578,7 +1593,11 @@ void TypeChecker::endVisit(Literal const& _literal)
return;
}
else
- warning(_literal.location(), "This looks like an address but has an invalid checksum.");
+ warning(
+ _literal.location(),
+ "This looks like an address but has an invalid checksum. "
+ "If this is not used as an address, please prepend '00'."
+ );
}
_literal.annotation().type = Type::forLiteral(_literal);
if (!_literal.annotation().type)