diff --git a/libsolidity/analysis/PostTypeChecker.cpp b/libsolidity/analysis/PostTypeChecker.cpp index b68167c1d..0634cdc66 100644 --- a/libsolidity/analysis/PostTypeChecker.cpp +++ b/libsolidity/analysis/PostTypeChecker.cpp @@ -435,18 +435,21 @@ struct ReservedErrorSelector: public PostTypeChecker::Checker class LValueChecker: public ASTConstVisitor { public: - LValueChecker(Identifier const& _identifier): m_declaration(_identifier.annotation().referencedDeclaration) {} + LValueChecker(Identifier const& _identifier): + m_declaration(_identifier.annotation().referencedDeclaration) + {} bool willBeWrittenTo() const { return m_willBeWrittenTo; } void endVisit(Identifier const& _identifier) override { + solAssert(_identifier.annotation().referencedDeclaration); if ( - _identifier.annotation().referencedDeclaration == m_declaration && + *_identifier.annotation().referencedDeclaration == *m_declaration && _identifier.annotation().willBeWrittenTo ) m_willBeWrittenTo = true; } private: - Declaration const* m_declaration; + Declaration const* m_declaration{}; bool m_willBeWrittenTo = false; }; @@ -460,29 +463,27 @@ struct SimpleCounterForLoopChecker: public PostTypeChecker::Checker } bool isSimpleCounterLoop(ForStatement const& _forStatement) const { - auto const* cond = dynamic_cast(_forStatement.condition()); - if (!cond || cond->getOperator() != Token::LessThan || cond->userDefinedFunctionType()) + auto const* simpleCondition = dynamic_cast(_forStatement.condition()); + if (!simpleCondition || simpleCondition->getOperator() != Token::LessThan || simpleCondition->userDefinedFunctionType()) return false; if (!_forStatement.loopExpression()) return false; - auto const* post = dynamic_cast(&_forStatement.loopExpression()->expression()); + auto const* simplePostExpression = dynamic_cast(&_forStatement.loopExpression()->expression()); // This matches both operators ++i and i++ - if (!post || post->getOperator() != Token::Inc || post->userDefinedFunctionType()) + if (!simplePostExpression || simplePostExpression->getOperator() != Token::Inc || simplePostExpression->userDefinedFunctionType()) return false; - auto const* lhsIdentifier = dynamic_cast(&cond->leftExpression()); - auto const* lhsType = dynamic_cast(cond->leftExpression().annotation().type); - auto const *commonType = dynamic_cast(cond->annotation().commonType); + auto const* lhsIdentifier = dynamic_cast(&simpleCondition->leftExpression()); + auto const* lhsIntegerType = dynamic_cast(simpleCondition->leftExpression().annotation().type); + auto const* commonIntegerType = dynamic_cast(simpleCondition->annotation().commonType); - if (lhsIdentifier && lhsType && commonType && *lhsType == *commonType) - { - LValueChecker lValueChecker{*lhsIdentifier}; - _forStatement.body().accept(lValueChecker); - if (!lValueChecker.willBeWrittenTo()) - return true; - } - return false; + if (!lhsIdentifier || !lhsIntegerType || !commonIntegerType || *lhsIntegerType != *commonIntegerType) + return false; + + LValueChecker lhsLValueChecker{*lhsIdentifier}; + _forStatement.body().accept(lhsLValueChecker); + return !lhsLValueChecker.willBeWrittenTo(); } }; @@ -491,11 +492,11 @@ struct SimpleCounterForLoopChecker: public PostTypeChecker::Checker PostTypeChecker::PostTypeChecker(langutil::ErrorReporter& _errorReporter): m_errorReporter(_errorReporter) { - m_checkers.push_back(make_shared(_errorReporter)); - m_checkers.push_back(make_shared(_errorReporter)); - m_checkers.push_back(make_shared(_errorReporter)); - m_checkers.push_back(make_shared(_errorReporter)); - m_checkers.push_back(make_shared(_errorReporter)); - m_checkers.push_back(make_shared(_errorReporter)); - m_checkers.push_back(make_shared(_errorReporter)); + m_checkers.push_back(std::make_shared(_errorReporter)); + m_checkers.push_back(std::make_shared(_errorReporter)); + m_checkers.push_back(std::make_shared(_errorReporter)); + m_checkers.push_back(std::make_shared(_errorReporter)); + m_checkers.push_back(std::make_shared(_errorReporter)); + m_checkers.push_back(std::make_shared(_errorReporter)); + m_checkers.push_back(std::make_shared(_errorReporter)); } diff --git a/libsolidity/ast/ASTJsonExporter.cpp b/libsolidity/ast/ASTJsonExporter.cpp index 251622195..cd346ca06 100644 --- a/libsolidity/ast/ASTJsonExporter.cpp +++ b/libsolidity/ast/ASTJsonExporter.cpp @@ -743,15 +743,15 @@ bool ASTJsonExporter::visit(WhileStatement const& _node) bool ASTJsonExporter::visit(ForStatement const& _node) { - std::vector> attributes = { - make_pair("initializationExpression", toJsonOrNull(_node.initializationExpression())), - make_pair("condition", toJsonOrNull(_node.condition())), - make_pair("loopExpression", toJsonOrNull(_node.loopExpression())), - make_pair("body", toJson(_node.body())) + std::vector> attributes = { + std::make_pair("initializationExpression", toJsonOrNull(_node.initializationExpression())), + std::make_pair("condition", toJsonOrNull(_node.condition())), + std::make_pair("loopExpression", toJsonOrNull(_node.loopExpression())), + std::make_pair("body", toJson(_node.body())) }; if (_node.annotation().isSimpleCounterLoop.set()) - attributes.emplace_back(make_pair("isSimpleCounterLoop", *_node.annotation().isSimpleCounterLoop)); + attributes.emplace_back(std::make_pair("isSimpleCounterLoop", *_node.annotation().isSimpleCounterLoop)); setJsonNode(_node, "ForStatement", std::move(attributes)); return false; diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index ffcb1409b..724fe66fd 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -618,7 +618,7 @@ bool IRGeneratorForStatements::visit(ForStatement const& _forStatement) _forStatement.condition(), _forStatement.initializationExpression(), _forStatement.loopExpression(), - false, + false, // _isDoWhile *_forStatement.annotation().isSimpleCounterLoop );