From 1057fd53558a5f64ca7b037d660c502c6673042c Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 18 Mar 2021 13:21:09 +0100 Subject: [PATCH] Take revert statement into account in control flow graph. --- libsolidity/analysis/ControlFlowBuilder.cpp | 10 +++++++++ libsolidity/analysis/ControlFlowBuilder.h | 1 + .../syntaxTests/errors/duplicate.sol | 21 +++++++++++++++++++ .../syntaxTests/errors/unreachable.sol | 10 +++++++++ 4 files changed, 42 insertions(+) create mode 100644 test/libsolidity/syntaxTests/errors/duplicate.sol create mode 100644 test/libsolidity/syntaxTests/errors/unreachable.sol diff --git a/libsolidity/analysis/ControlFlowBuilder.cpp b/libsolidity/analysis/ControlFlowBuilder.cpp index 975e4ec6d..8946e6ed3 100644 --- a/libsolidity/analysis/ControlFlowBuilder.cpp +++ b/libsolidity/analysis/ControlFlowBuilder.cpp @@ -236,6 +236,16 @@ bool ControlFlowBuilder::visit(Throw const& _throw) return false; } +bool ControlFlowBuilder::visit(RevertStatement const& _revert) +{ + solAssert(!!m_currentNode, ""); + solAssert(!!m_revertNode, ""); + visitNode(_revert); + connect(m_currentNode, m_revertNode); + m_currentNode = newLabel(); + return false; +} + bool ControlFlowBuilder::visit(PlaceholderStatement const&) { solAssert(!!m_currentNode, ""); diff --git a/libsolidity/analysis/ControlFlowBuilder.h b/libsolidity/analysis/ControlFlowBuilder.h index 0efd55d77..879d9b241 100644 --- a/libsolidity/analysis/ControlFlowBuilder.h +++ b/libsolidity/analysis/ControlFlowBuilder.h @@ -56,6 +56,7 @@ private: bool visit(Break const&) override; bool visit(Continue const&) override; bool visit(Throw const&) override; + bool visit(RevertStatement const&) override; bool visit(PlaceholderStatement const&) override; bool visit(FunctionCall const& _functionCall) override; bool visit(ModifierInvocation const& _modifierInvocation) override; diff --git a/test/libsolidity/syntaxTests/errors/duplicate.sol b/test/libsolidity/syntaxTests/errors/duplicate.sol new file mode 100644 index 000000000..a9ef4cc92 --- /dev/null +++ b/test/libsolidity/syntaxTests/errors/duplicate.sol @@ -0,0 +1,21 @@ +==== Source: A ==== + +error E(); + +==== Source: B ==== + +error E(); + +==== Source: C ==== + +import "A" as A; +import "B" as B; + +contract Test { + function f() public pure { + revert A.E(); + } + function g() public pure { + revert B.E(); + } +} \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/errors/unreachable.sol b/test/libsolidity/syntaxTests/errors/unreachable.sol new file mode 100644 index 000000000..c3ee54d17 --- /dev/null +++ b/test/libsolidity/syntaxTests/errors/unreachable.sol @@ -0,0 +1,10 @@ +contract C { + error E(); + uint x = 2; + function f() public { + revert E(); + x = 4; + } +} +// ---- +// Warning 5740: (98-103): Unreachable code.