Use BOOST_PP_OVERLOAD() to allow invoking the assertion macros without a message

This commit is contained in:
Kamil Śliwak 2021-09-23 16:52:08 +02:00 committed by chriseth
parent 4fe6aa1328
commit 0745842d46
3 changed files with 101 additions and 10 deletions

View File

@ -28,6 +28,10 @@
#include <libsolutil/CommonData.h> #include <libsolutil/CommonData.h>
#include <liblangutil/SourceLocation.h> #include <liblangutil/SourceLocation.h>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/preprocessor/facilities/overload.hpp>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -45,18 +49,68 @@ struct FatalError: virtual util::Exception {};
struct UnimplementedFeatureError: virtual util::Exception {}; struct UnimplementedFeatureError: virtual util::Exception {};
struct InvalidAstError: virtual util::Exception {}; struct InvalidAstError: virtual util::Exception {};
/// Assertion that throws an InternalCompilerError containing the given description if it is not met. /// Assertion that throws an InternalCompilerError containing the given description if it is not met.
#define solAssert(CONDITION, DESCRIPTION) \ #if !BOOST_PP_VARIADICS_MSVC
assertThrowWithDefaultDescription(CONDITION, ::solidity::langutil::InternalCompilerError, DESCRIPTION, "Solidity assertion failed") #define solAssert(...) BOOST_PP_OVERLOAD(solAssert_,__VA_ARGS__)(__VA_ARGS__)
#else
#define solAssert(...) BOOST_PP_CAT(BOOST_PP_OVERLOAD(solAssert_,__VA_ARGS__)(__VA_ARGS__),BOOST_PP_EMPTY())
#endif
#define solUnimplementedAssert(CONDITION, DESCRIPTION) \ #define solAssert_1(CONDITION) \
assertThrowWithDefaultDescription(CONDITION, ::solidity::langutil::UnimplementedFeatureError, DESCRIPTION, "Unimplemented feature") solAssert_2(CONDITION, "")
#define solAssert_2(CONDITION, DESCRIPTION) \
assertThrowWithDefaultDescription( \
CONDITION, \
::solidity::langutil::InternalCompilerError, \
DESCRIPTION, \
"Solidity assertion failed" \
)
/// Assertion that throws an UnimplementedFeatureError containing the given description if it is not met.
#if !BOOST_PP_VARIADICS_MSVC
#define solUnimplementedAssert(...) BOOST_PP_OVERLOAD(solUnimplementedAssert_,__VA_ARGS__)(__VA_ARGS__)
#else
#define solUnimplementedAssert(...) BOOST_PP_CAT(BOOST_PP_OVERLOAD(solUnimplementedAssert_,__VA_ARGS__)(__VA_ARGS__),BOOST_PP_EMPTY())
#endif
#define solUnimplementedAssert_1(CONDITION) \
solUnimplementedAssert_2(CONDITION, "")
#define solUnimplementedAssert_2(CONDITION, DESCRIPTION) \
assertThrowWithDefaultDescription( \
CONDITION, \
::solidity::langutil::UnimplementedFeatureError, \
DESCRIPTION, \
"Unimplemented feature" \
)
/// Helper that unconditionally reports an unimplemented feature.
#define solUnimplemented(DESCRIPTION) \ #define solUnimplemented(DESCRIPTION) \
solUnimplementedAssert(false, DESCRIPTION) solUnimplementedAssert(false, DESCRIPTION)
#define astAssert(CONDITION, DESCRIPTION) \
assertThrowWithDefaultDescription(CONDITION, ::solidity::langutil::InvalidAstError, DESCRIPTION, "AST assertion failed") /// Assertion that throws an InvalidAstError containing the given description if it is not met.
#if !BOOST_PP_VARIADICS_MSVC
#define astAssert(...) BOOST_PP_OVERLOAD(astAssert_,__VA_ARGS__)(__VA_ARGS__)
#else
#define astAssert(...) BOOST_PP_CAT(BOOST_PP_OVERLOAD(astAssert_,__VA_ARGS__)(__VA_ARGS__),BOOST_PP_EMPTY())
#endif
#define astAssert_1(CONDITION) \
astAssert_2(CONDITION, "")
#define astAssert_2(CONDITION, DESCRIPTION) \
assertThrowWithDefaultDescription( \
CONDITION, \
::solidity::langutil::InvalidAstError, \
DESCRIPTION, \
"AST assertion failed" \
)
using errorSourceLocationInfo = std::pair<std::string, SourceLocation>; using errorSourceLocationInfo = std::pair<std::string, SourceLocation>;

View File

@ -21,12 +21,31 @@
#include <libsolutil/Assertions.h> #include <libsolutil/Assertions.h>
#include <libsolutil/Exceptions.h> #include <libsolutil/Exceptions.h>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/preprocessor/facilities/overload.hpp>
namespace solidity::smtutil namespace solidity::smtutil
{ {
struct SMTLogicError: virtual util::Exception {}; struct SMTLogicError: virtual util::Exception {};
#define smtAssert(CONDITION, DESCRIPTION) \ /// Assertion that throws an SMTLogicError containing the given description if it is not met.
assertThrowWithDefaultDescription(CONDITION, SMTLogicError, DESCRIPTION, "SMT assertion failed") #if !BOOST_PP_VARIADICS_MSVC
#define smtAssert(...) BOOST_PP_OVERLOAD(smtAssert_,__VA_ARGS__)(__VA_ARGS__)
#else
#define smtAssert(...) BOOST_PP_CAT(BOOST_PP_OVERLOAD(smtAssert_,__VA_ARGS__)(__VA_ARGS__),BOOST_PP_EMPTY())
#endif
#define smtAssert_1(CONDITION) \
smtAssert_2(CONDITION, "")
#define smtAssert_2(CONDITION, DESCRIPTION) \
assertThrowWithDefaultDescription( \
CONDITION, \
::solidity::smtutil::SMTLogicError, \
DESCRIPTION, \
"SMT assertion failed" \
)
} }

View File

@ -26,6 +26,10 @@
#include <libyul/YulString.h> #include <libyul/YulString.h>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/preprocessor/facilities/overload.hpp>
namespace solidity::yul namespace solidity::yul
{ {
@ -52,7 +56,21 @@ struct StackTooDeepError: virtual YulException
}; };
/// Assertion that throws an YulAssertion containing the given description if it is not met. /// Assertion that throws an YulAssertion containing the given description if it is not met.
#define yulAssert(CONDITION, DESCRIPTION) \ #if !BOOST_PP_VARIADICS_MSVC
assertThrowWithDefaultDescription(CONDITION, ::solidity::yul::YulAssertion, DESCRIPTION, "Yul assertion failed") #define yulAssert(...) BOOST_PP_OVERLOAD(yulAssert_,__VA_ARGS__)(__VA_ARGS__)
#else
#define yulAssert(...) BOOST_PP_CAT(BOOST_PP_OVERLOAD(yulAssert_,__VA_ARGS__)(__VA_ARGS__),BOOST_PP_EMPTY())
#endif
#define yulAssert_1(CONDITION) \
yulAssert_2(CONDITION, "")
#define yulAssert_2(CONDITION, DESCRIPTION) \
assertThrowWithDefaultDescription( \
CONDITION, \
::solidity::yul::YulAssertion, \
DESCRIPTION, \
"Yul assertion failed" \
)
} }