Skip to content

Commit 8c87f58

Browse files
Merge pull request #13102 from ethereum/cpp-unreachable
Better way to annotate unreachability in C++
2 parents 87f5865 + 4ae4388 commit 8c87f58

File tree

7 files changed

+34
-12
lines changed

7 files changed

+34
-12
lines changed

libsmtutil/CVC4Interface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ CVC4::Expr CVC4Interface::toCVC4Expr(Expression const& _expr)
292292
smtAssert(false);
293293

294294
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
295-
throw exception();
295+
util::unreachable();
296296
}
297297

298298
CVC4::Type CVC4Interface::cvc4Sort(Sort const& _sort)

libsmtutil/Z3Interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ z3::expr Z3Interface::toZ3Expr(Expression const& _expr)
274274
smtAssert(false);
275275

276276
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
277-
throw exception();
277+
util::unreachable();
278278
}
279279

280280
Expression Z3Interface::fromZ3Expr(z3::expr const& _expr)
@@ -385,7 +385,7 @@ Expression Z3Interface::fromZ3Expr(z3::expr const& _expr)
385385
smtAssert(false);
386386

387387
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
388-
throw exception();
388+
util::unreachable();
389389
}
390390

391391
z3::sort Z3Interface::z3Sort(Sort const& _sort)

libsolidity/ast/ASTJsonImporter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ ASTPointer<ASTNode> ASTJsonImporter::convertJsonToASTNode(Json::Value const& _js
231231
astAssert(false, "Unknown type of ASTNode: " + nodeType);
232232

233233
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
234-
throw exception();
234+
util::unreachable();
235235
}
236236

237237
// ============ functions to instantiate the AST-Nodes from Json-Nodes ==============
@@ -1078,7 +1078,7 @@ Visibility ASTJsonImporter::visibility(Json::Value const& _node)
10781078
astAssert(false, "Unknown visibility declaration");
10791079

10801080
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
1081-
throw exception();
1081+
util::unreachable();
10821082
}
10831083

10841084
VariableDeclaration::Location ASTJsonImporter::location(Json::Value const& _node)
@@ -1100,7 +1100,7 @@ VariableDeclaration::Location ASTJsonImporter::location(Json::Value const& _node
11001100
astAssert(false, "Unknown location declaration");
11011101

11021102
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
1103-
throw exception();
1103+
util::unreachable();
11041104
}
11051105

11061106
Literal::SubDenomination ASTJsonImporter::subdenomination(Json::Value const& _node)
@@ -1136,7 +1136,7 @@ Literal::SubDenomination ASTJsonImporter::subdenomination(Json::Value const& _no
11361136
astAssert(false, "Unknown subdenomination");
11371137

11381138
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
1139-
throw exception();
1139+
util::unreachable();
11401140
}
11411141

11421142
StateMutability ASTJsonImporter::stateMutability(Json::Value const& _node)
@@ -1156,7 +1156,7 @@ StateMutability ASTJsonImporter::stateMutability(Json::Value const& _node)
11561156
astAssert(false, "Unknown stateMutability");
11571157

11581158
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
1159-
throw exception();
1159+
util::unreachable();
11601160
}
11611161

11621162
}

libsolidity/parsing/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ ASTPointer<Statement> Parser::parseSimpleStatement(ASTPointer<ASTString> const&
16551655
}
16561656

16571657
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
1658-
throw exception();
1658+
util::unreachable();
16591659
}
16601660

16611661
bool Parser::IndexAccessedPath::empty() const

libsolutil/Assertions.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ namespace solidity::util
4040
#define ETH_FUNC __func__
4141
#endif
4242

43+
#if defined(__GNUC__)
44+
// GCC 4.8+, Clang, Intel and other compilers compatible with GCC (-std=c++0x or above)
45+
[[noreturn]] inline __attribute__((always_inline)) void unreachable()
46+
{
47+
__builtin_unreachable();
48+
}
49+
50+
#elif defined(_MSC_VER) // MSVC
51+
52+
[[noreturn]] __forceinline void unreachable()
53+
{
54+
__assume(false);
55+
}
56+
57+
#else
58+
59+
[[noreturn]] inline void unreachable()
60+
{
61+
solThrow(Exception, "Unreachable");
62+
}
63+
#endif
64+
4365
namespace assertions
4466
{
4567

libyul/AsmJsonImporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Statement AsmJsonImporter::createStatement(Json::Value const& _node)
112112
yulAssert(false, "Invalid nodeType as statement");
113113

114114
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
115-
throw exception();
115+
util::unreachable();
116116
}
117117

118118
Expression AsmJsonImporter::createExpression(Json::Value const& _node)
@@ -134,7 +134,7 @@ Expression AsmJsonImporter::createExpression(Json::Value const& _node)
134134
yulAssert(false, "Invalid nodeType as expression");
135135

136136
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
137-
throw exception();
137+
util::unreachable();
138138
}
139139

140140
vector<Expression> AsmJsonImporter::createExpressionVector(Json::Value const& _array)

tools/yulPhaser/Phaser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
278278
}
279279

280280
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
281-
throw exception();
281+
util::unreachable();
282282
}
283283

284284
PopulationFactory::Options PopulationFactory::Options::fromCommandLine(po::variables_map const& _arguments)

0 commit comments

Comments
 (0)