diff options
author | Andreas Nilsson <andreas.nilsson@mongodb.com> | 2016-07-08 16:13:15 -0400 |
---|---|---|
committer | Andreas Nilsson <andreas.nilsson@mongodb.com> | 2016-07-11 13:16:34 -0400 |
commit | c73a719b1c43e0b6b9c26e4e686ad5b1f940c67b (patch) | |
tree | 5bab18c0d07ae5bddd8acc9e0a733b0a7854bce0 /src/mongo/util/assert_util.h | |
parent | 4d9e3e4cb09805dfb5e181536a8830f4f878275c (diff) | |
download | mongo-c73a719b1c43e0b6b9c26e4e686ad5b1f940c67b.tar.gz |
SERVER-24523 Include file and line numbers in assertions
Diffstat (limited to 'src/mongo/util/assert_util.h')
-rw-r--r-- | src/mongo/util/assert_util.h | 200 |
1 files changed, 150 insertions, 50 deletions
diff --git a/src/mongo/util/assert_util.h b/src/mongo/util/assert_util.h index 3189639bb94..ab7dfab85a0 100644 --- a/src/mongo/util/assert_util.h +++ b/src/mongo/util/assert_util.h @@ -179,26 +179,82 @@ MONGO_COMPILER_NORETURN void invariantOKFailed(const char* expr, const char* file, unsigned line) noexcept; void wasserted(const char* expr, const char* file, unsigned line); -MONGO_COMPILER_NORETURN void fassertFailed(int msgid) noexcept; -MONGO_COMPILER_NORETURN void fassertFailedNoTrace(int msgid) noexcept; -MONGO_COMPILER_NORETURN void fassertFailedWithStatus(int msgid, const Status& status) noexcept; -MONGO_COMPILER_NORETURN void fassertFailedWithStatusNoTrace(int msgid, - const Status& status) noexcept; + +#define fassertFailed MONGO_fassertFailed +#define MONGO_fassertFailed(...) ::mongo::fassertFailedWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedWithLocation(int msgid, + const char* file, + unsigned line) noexcept; + +#define fassertFailedNoTrace MONGO_fassertFailedNoTrace +#define MONGO_fassertFailedNoTrace(...) \ + ::mongo::fassertFailedNoTraceWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedNoTraceWithLocation(int msgid, + const char* file, + unsigned line) noexcept; + +#define fassertFailedWithStatus MONGO_fassertFailedWithStatus +#define MONGO_fassertFailedWithStatus(...) \ + ::mongo::fassertFailedWithStatusWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedWithStatusWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) noexcept; + +#define fassertFailedWithStatusNoTrace MONGO_fassertFailedWithStatusNoTrace +#define MONGO_fassertFailedWithStatusNoTrace(...) \ + ::mongo::fassertFailedWithStatusNoTraceWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void fassertFailedWithStatusNoTraceWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) noexcept; /** a "user assertion". throws UserAssertion. logs. typically used for errors that a user could cause, such as duplicate key, disk full, etc. */ -MONGO_COMPILER_NORETURN void uasserted(int msgid, const char* msg); -MONGO_COMPILER_NORETURN void uasserted(int msgid, const std::string& msg); +MONGO_COMPILER_NORETURN void uassertedWithLocation(int msgid, + const char* msg, + const char* file, + unsigned line); +MONGO_COMPILER_NORETURN void uassertedWithLocation(int msgid, + const std::string& msg, + const char* file, + unsigned line); /** msgassert and massert are for errors that are internal but have a well defined error text std::string. a stack trace is logged. */ -MONGO_COMPILER_NORETURN void msgassertedNoTrace(int msgid, const char* msg); -MONGO_COMPILER_NORETURN void msgassertedNoTrace(int msgid, const std::string& msg); -MONGO_COMPILER_NORETURN void msgassertedNoTraceWithStatus(int msgid, const Status& status); -MONGO_COMPILER_NORETURN void msgasserted(int msgid, const char* msg); -MONGO_COMPILER_NORETURN void msgasserted(int msgid, const std::string& msg); + +#define msgassertedNoTrace MONGO_msgassertedNoTrace +#define MONGO_msgassertedNoTrace(...) \ + ::mongo::msgassertedNoTraceWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void msgassertedNoTraceWithLocation(int msgid, + const char* msg, + const char* file, + unsigned line); +MONGO_COMPILER_NORETURN void msgassertedNoTraceWithLocation(int msgid, + const std::string& msg, + const char* file, + unsigned line); + +#define msgassertedNoTraceWithStatus MONGO_msgassertedNoTraceWithStatus +#define MONGO_msgassertedNoTraceWithStatus(...) \ + ::mongo::msgassertedNoTraceWithStatusWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void msgassertedNoTraceWithStatusWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line); + +#define msgasserted MONGO_msgasserted +#define MONGO_msgasserted(...) ::mongo::msgassertedWithLocation(__VA_ARGS__, __FILE__, __LINE__) +MONGO_COMPILER_NORETURN void msgassertedWithLocation(int msgid, + const char* msg, + const char* file, + unsigned line); +MONGO_COMPILER_NORETURN void msgassertedWithLocation(int msgid, + const std::string& msg, + const char* file, + unsigned line); /* convert various types of exceptions to strings */ std::string causedBy(const char* e); @@ -208,27 +264,37 @@ std::string causedBy(const std::string& e); std::string causedBy(const std::string* e); std::string causedBy(const Status& e); +#define fassert MONGO_fassert +#define MONGO_fassert(...) ::mongo::fassertWithLocation(__VA_ARGS__, __FILE__, __LINE__) + /** aborts on condition failure */ -inline void fassert(int msgid, bool testOK) { - if (MONGO_unlikely(!testOK)) - fassertFailed(msgid); +inline void fassertWithLocation(int msgid, bool testOK, const char* file, unsigned line) { + if (MONGO_unlikely(!testOK)) { + fassertFailedWithLocation(msgid, file, line); + } } -inline void fassert(int msgid, const Status& status) { +inline void fassertWithLocation(int msgid, const Status& status, const char* file, unsigned line) { if (MONGO_unlikely(!status.isOK())) { - fassertFailedWithStatus(msgid, status); + fassertFailedWithStatusWithLocation(msgid, status, file, line); } } -inline void fassertNoTrace(int msgid, bool testOK) { +#define fassertNoTrace MONGO_fassertNoTrace +#define MONGO_fassertNoTrace(...) \ + ::mongo::fassertNoTraceWithLocation(__VA_ARGS__, __FILE__, __LINE__) +inline void fassertNoTraceWithLocation(int msgid, bool testOK, const char* file, unsigned line) { if (MONGO_unlikely(!testOK)) { - fassertFailedNoTrace(msgid); + fassertFailedNoTraceWithLocation(msgid, file, line); } } -inline void fassertNoTrace(int msgid, const Status& status) { +inline void fassertNoTraceWithLocation(int msgid, + const Status& status, + const char* file, + unsigned line) { if (MONGO_unlikely(!status.isOK())) { - fassertFailedWithStatusNoTrace(msgid, status); + fassertFailedWithStatusNoTraceWithLocation(msgid, status, file, line); } } @@ -241,37 +307,54 @@ inline void fassertNoTrace(int msgid, const Status& status) { * MONGO_COMPILER_UNREACHABLE as it is impossible to mark a lambda noreturn. */ #define uassert MONGO_uassert -#define MONGO_uassert(msgid, msg, expr) \ - do { \ - if (MONGO_unlikely(!(expr))) { \ - [&]() MONGO_COMPILER_COLD_FUNCTION { ::mongo::uasserted(msgid, msg); }(); \ - MONGO_COMPILER_UNREACHABLE; \ - } \ +#define MONGO_uassert(msgid, msg, expr) \ + do { \ + if (MONGO_unlikely(!(expr))) { \ + [&]() MONGO_COMPILER_COLD_FUNCTION { \ + ::mongo::uassertedWithLocation(msgid, msg, __FILE__, __LINE__); \ + }(); \ + MONGO_COMPILER_UNREACHABLE; \ + } \ } while (false) -inline void uassertStatusOK(const Status& status) { - uassert((status.location() != 0 ? status.location() : status.code()), - status.reason(), - status.isOK()); +#define uasserted MONGO_uasserted +#define MONGO_uasserted(...) ::mongo::uassertedWithLocation(__VA_ARGS__, __FILE__, __LINE__) + +#define uassertStatusOK MONGO_uassertStatusOK +#define MONGO_uassertStatusOK(...) \ + ::mongo::uassertStatusOKWithLocation(__VA_ARGS__, __FILE__, __LINE__) +inline void uassertStatusOKWithLocation(const Status& status, const char* file, unsigned line) { + if (MONGO_unlikely(!status.isOK())) { + uassertedWithLocation((status.location() != 0 ? status.location() : status.code()), + status.reason(), + file, + line); + } } template <typename T> -inline T uassertStatusOK(StatusWith<T> sw) { - uassertStatusOK(sw.getStatus()); +inline T uassertStatusOKWithLocation(StatusWith<T> sw, const char* file, unsigned line) { + uassertStatusOKWithLocation(sw.getStatus(), file, line); return std::move(sw.getValue()); } +#define fassertStatusOK MONGO_fassertStatusOK +#define MONGO_fassertStatusOK(...) \ + ::mongo::fassertStatusOKWithLocation(__VA_ARGS__, __FILE__, __LINE__) template <typename T> -inline T fassertStatusOK(int msgid, StatusWith<T> sw) { +inline T fassertStatusOKWithLocation(int msgid, StatusWith<T> sw, const char* file, unsigned line) { if (MONGO_unlikely(!sw.isOK())) { - fassertFailedWithStatus(msgid, sw.getStatus()); + fassertFailedWithStatusWithLocation(msgid, sw.getStatus(), file, line); } return std::move(sw.getValue()); } -inline void fassertStatusOK(int msgid, const Status& s) { +inline void fassertStatusOKWithLocation(int msgid, + const Status& s, + const char* file, + unsigned line) { if (MONGO_unlikely(!s.isOK())) { - fassertFailedWithStatus(msgid, s); + fassertFailedWithStatusWithLocation(msgid, s, file, line); } } @@ -290,25 +373,42 @@ inline void fassertStatusOK(int msgid, const Status& s) { display happening. */ #define massert MONGO_massert -#define MONGO_massert(msgid, msg, expr) \ - do { \ - if (MONGO_unlikely(!(expr))) { \ - [&]() MONGO_COMPILER_COLD_FUNCTION { ::mongo::msgasserted(msgid, msg); }(); \ - MONGO_COMPILER_UNREACHABLE; \ - } \ +#define MONGO_massert(msgid, msg, expr) \ + do { \ + if (MONGO_unlikely(!(expr))) { \ + [&]() MONGO_COMPILER_COLD_FUNCTION { \ + ::mongo::msgassertedWithLocation(msgid, msg, __FILE__, __LINE__); \ + }(); \ + MONGO_COMPILER_UNREACHABLE; \ + } \ } while (false) -inline void massertStatusOK(const Status& status) { - massert((status.location() != 0 ? status.location() : status.code()), - status.reason(), - status.isOK()); + +#define massertStatusOK MONGO_massertStatusOK +#define MONGO_massertStatusOK(...) \ + ::mongo::massertStatusOKWithLocation(__VA_ARGS__, __FILE__, __LINE__) +inline void massertStatusOKWithLocation(const Status& status, const char* file, unsigned line) { + if (MONGO_unlikely(!status.isOK())) { + msgassertedWithLocation((status.location() != 0 ? status.location() : status.code()), + status.reason(), + file, + line); + } } -inline void massertNoTraceStatusOK(const Status& status) { +#define massertNoTraceStatusOK MONGO_massertNoTraceStatusOK +#define MONGO_massertNoTraceStatusOK(...) \ + ::mongo::massertNoTraceStatusOKWithLocation(__VA_ARGS__, __FILE__, __LINE__) +inline void massertNoTraceStatusOKWithLocation(const Status& status, + const char* file, + unsigned line) { if (MONGO_unlikely(!status.isOK())) { [&]() MONGO_COMPILER_COLD_FUNCTION { - msgassertedNoTrace((status.location() != 0 ? status.location() : status.code()), - status.reason()); + msgassertedNoTraceWithLocation( + (status.location() != 0 ? status.location() : status.code()), + status.reason(), + file, + line); }(); MONGO_COMPILER_UNREACHABLE; } |