From 927da20ce4387404cb94028bf3ba94e399340bdb Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Tue, 7 Jun 2022 15:41:15 +0200 Subject: [PATCH 1/2] Adds solidity::util::unreachable() helper function. --- libsolutil/Assertions.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libsolutil/Assertions.h b/libsolutil/Assertions.h index 4924522d6..6735cda16 100644 --- a/libsolutil/Assertions.h +++ b/libsolutil/Assertions.h @@ -40,6 +40,28 @@ namespace solidity::util #define ETH_FUNC __func__ #endif +#if defined(__GNUC__) +// GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above) +[[noreturn]] inline __attribute__((always_inline)) void unreachable() +{ + __builtin_unreachable(); +} + +#elif defined(_MSC_VER) // MSVC + +[[noreturn]] __forceinline void unreachable() +{ + __assume(false); +} + +#else + +[[noreturn]] inline void unreachable() +{ + solThrow(Exception, "Unreachable"); +} +#endif + namespace assertions { From 4ae43884d0e4dd9ebfd1f4393fcd2f894f07c473 Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Tue, 7 Jun 2022 15:43:14 +0200 Subject: [PATCH 2/2] Apply a better way to annotate unreachability to the C++ compiler. --- libsmtutil/CVC4Interface.cpp | 2 +- libsmtutil/Z3Interface.cpp | 4 ++-- libsolidity/ast/ASTJsonImporter.cpp | 10 +++++----- libsolidity/parsing/Parser.cpp | 2 +- libyul/AsmJsonImporter.cpp | 4 ++-- tools/yulPhaser/Phaser.cpp | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libsmtutil/CVC4Interface.cpp b/libsmtutil/CVC4Interface.cpp index c335b18d1..ff356c025 100644 --- a/libsmtutil/CVC4Interface.cpp +++ b/libsmtutil/CVC4Interface.cpp @@ -292,7 +292,7 @@ CVC4::Expr CVC4Interface::toCVC4Expr(Expression const& _expr) smtAssert(false); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } CVC4::Type CVC4Interface::cvc4Sort(Sort const& _sort) diff --git a/libsmtutil/Z3Interface.cpp b/libsmtutil/Z3Interface.cpp index 74767d86c..4abeffa16 100644 --- a/libsmtutil/Z3Interface.cpp +++ b/libsmtutil/Z3Interface.cpp @@ -274,7 +274,7 @@ z3::expr Z3Interface::toZ3Expr(Expression const& _expr) smtAssert(false); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } Expression Z3Interface::fromZ3Expr(z3::expr const& _expr) @@ -385,7 +385,7 @@ Expression Z3Interface::fromZ3Expr(z3::expr const& _expr) smtAssert(false); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } z3::sort Z3Interface::z3Sort(Sort const& _sort) diff --git a/libsolidity/ast/ASTJsonImporter.cpp b/libsolidity/ast/ASTJsonImporter.cpp index 859a93350..4fcea9aff 100644 --- a/libsolidity/ast/ASTJsonImporter.cpp +++ b/libsolidity/ast/ASTJsonImporter.cpp @@ -231,7 +231,7 @@ ASTPointer ASTJsonImporter::convertJsonToASTNode(Json::Value const& _js astAssert(false, "Unknown type of ASTNode: " + nodeType); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } // ============ functions to instantiate the AST-Nodes from Json-Nodes ============== @@ -1078,7 +1078,7 @@ Visibility ASTJsonImporter::visibility(Json::Value const& _node) astAssert(false, "Unknown visibility declaration"); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } VariableDeclaration::Location ASTJsonImporter::location(Json::Value const& _node) @@ -1100,7 +1100,7 @@ VariableDeclaration::Location ASTJsonImporter::location(Json::Value const& _node astAssert(false, "Unknown location declaration"); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } Literal::SubDenomination ASTJsonImporter::subdenomination(Json::Value const& _node) @@ -1136,7 +1136,7 @@ Literal::SubDenomination ASTJsonImporter::subdenomination(Json::Value const& _no astAssert(false, "Unknown subdenomination"); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } StateMutability ASTJsonImporter::stateMutability(Json::Value const& _node) @@ -1156,7 +1156,7 @@ StateMutability ASTJsonImporter::stateMutability(Json::Value const& _node) astAssert(false, "Unknown stateMutability"); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } } diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 0299ff092..554ebf75c 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1655,7 +1655,7 @@ ASTPointer Parser::parseSimpleStatement(ASTPointer const& } // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } bool Parser::IndexAccessedPath::empty() const diff --git a/libyul/AsmJsonImporter.cpp b/libyul/AsmJsonImporter.cpp index 1f108145f..0a88cacf8 100644 --- a/libyul/AsmJsonImporter.cpp +++ b/libyul/AsmJsonImporter.cpp @@ -112,7 +112,7 @@ Statement AsmJsonImporter::createStatement(Json::Value const& _node) yulAssert(false, "Invalid nodeType as statement"); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } Expression AsmJsonImporter::createExpression(Json::Value const& _node) @@ -134,7 +134,7 @@ Expression AsmJsonImporter::createExpression(Json::Value const& _node) yulAssert(false, "Invalid nodeType as expression"); // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } vector AsmJsonImporter::createExpressionVector(Json::Value const& _array) diff --git a/tools/yulPhaser/Phaser.cpp b/tools/yulPhaser/Phaser.cpp index c49d9a2d8..213e6f15b 100644 --- a/tools/yulPhaser/Phaser.cpp +++ b/tools/yulPhaser/Phaser.cpp @@ -278,7 +278,7 @@ unique_ptr FitnessMetricFactory::build( } // FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794) - throw exception(); + util::unreachable(); } PopulationFactory::Options PopulationFactory::Options::fromCommandLine(po::variables_map const& _arguments)