Skip to content

Commit

Permalink
Refix MSVC Debug crash
Browse files Browse the repository at this point in the history
  • Loading branch information
a3d4 committed Apr 25, 2022
1 parent 5591740 commit 048b253
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
13 changes: 12 additions & 1 deletion libsolutil/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ struct VisitorFallback<R> { template<typename T> R operator()(T&&) const { retur
template<>
struct VisitorFallback<> { template<typename T> void operator()(T&&) const {} };

template <typename... Visitors> struct GenericVisitor: Visitors... { using Visitors::operator()...; };
// MSVC. Empty base class optimization does not happen in some scenarios.
// Enforcing it with __declspec(empty_bases) avoids MSVC Debug test crash
// (Run-Time Check Failure #2 - Stack around the variable '....' was corrupted).
// See https://docs.microsoft.com/en-us/cpp/cpp/empty-bases,
// https://developercommunity.visualstudio.com/t/10005513.
#if defined(_MSC_VER)
#define SOLC_EMPTY_BASES __declspec(empty_bases)
#else
#define SOLC_EMPTY_BASES
#endif

template <typename... Visitors> struct SOLC_EMPTY_BASES GenericVisitor: Visitors... { using Visitors::operator()...; };
template <typename... Visitors> GenericVisitor(Visitors...) -> GenericVisitor<Visitors...>;
}
43 changes: 19 additions & 24 deletions libyul/optimiser/StructuralSimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,27 @@ void StructuralSimplifier::operator()(Block& _block)

void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
{
// Explicit local variables ifLambda, switchLambda, forLoopLambda are created to avoid MSVC C++17 Debug test crash
// (Run-Time Check Failure #2 - Stack around the variable '....' was corrupted).
// As soon as the issue is fixed, this workaround can be removed.
auto ifLambda = [&](If& _ifStmt) -> OptionalStatements
{
if (expressionAlwaysTrue(*_ifStmt.condition))
return {std::move(_ifStmt.body.statements)};
else if (expressionAlwaysFalse(*_ifStmt.condition))
return {vector<Statement>{}};
return {};
};
auto switchLambda = [&](Switch& _switchStmt) -> OptionalStatements
{
if (std::optional<u256> const constExprVal = hasLiteralValue(*_switchStmt.expression))
return replaceConstArgSwitch(_switchStmt, constExprVal.value());
return {};
};
auto forLoopLambda = [&](ForLoop& _forLoop) -> OptionalStatements
{
if (expressionAlwaysFalse(*_forLoop.condition))
return {std::move(_forLoop.pre.statements)};
return {};
util::GenericVisitor visitor{
util::VisitorFallback<OptionalStatements>{},
[&](If& _ifStmt) -> OptionalStatements {
if (expressionAlwaysTrue(*_ifStmt.condition))
return {std::move(_ifStmt.body.statements)};
else if (expressionAlwaysFalse(*_ifStmt.condition))
return {vector<Statement>{}};
return {};
},
[&](Switch& _switchStmt) -> OptionalStatements {
if (std::optional<u256> const constExprVal = hasLiteralValue(*_switchStmt.expression))
return replaceConstArgSwitch(_switchStmt, constExprVal.value());
return {};
},
[&](ForLoop& _forLoop) -> OptionalStatements {
if (expressionAlwaysFalse(*_forLoop.condition))
return {std::move(_forLoop.pre.statements)};
return {};
}
};

util::GenericVisitor visitor{util::VisitorFallback<OptionalStatements>{}, ifLambda, switchLambda, forLoopLambda};

util::iterateReplacing(
_statements,
[&](Statement& _stmt) -> OptionalStatements
Expand Down

0 comments on commit 048b253

Please sign in to comment.