From 4f95a6d5e0eb4b449431c49fa76db2c3628eb687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Wed, 21 Jul 2021 02:06:19 +0200 Subject: [PATCH 1/3] make use of constant lambdas to make our assert macros suitable for constexpr --- src/util/assert.h | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/util/assert.h b/src/util/assert.h index 0983e03e9d0..867c6a513bd 100644 --- a/src/util/assert.h +++ b/src/util/assert.h @@ -2,26 +2,23 @@ #include - static constexpr const char* kDebugAssertPrefix = "DEBUG ASSERT"; -inline void mixxx_noop(void) {} - +#ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED inline void mixxx_debug_assert(const char* assertion, const char* file, int line, const char* function) { qCritical("%s: \"%s\" in function %s at %s:%d", kDebugAssertPrefix, assertion, function, file, line); } +#endif -inline bool mixxx_maybe_debug_assert_return_true(const char* assertion, const char* file, int line, const char* function) { #ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED +inline bool mixxx_maybe_debug_assert_return_true(const char* assertion, + const char* file, + int line, + const char* function) { mixxx_debug_assert(assertion, file, line, function); -#else - Q_UNUSED(assertion); - Q_UNUSED(file); - Q_UNUSED(line); - Q_UNUSED(function); -#endif return true; } +#endif inline void mixxx_release_assert(const char* assertion, const char* file, int line, const char* function) { qFatal("ASSERT: \"%s\" in function %s at %s:%d", assertion, function, file, line); @@ -40,7 +37,10 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li /// very hard before using this -- this should only be for the most dire of /// situations where we know Mixxx cannot take any action without potentially /// corrupting user data. Handle errors gracefully whenever possible. -#define RELEASE_ASSERT(cond) ((!(cond)) ? mixxx_release_assert(#cond, __FILE__, __LINE__, ASSERT_FUNCTION) : mixxx_noop()) +#define RELEASE_ASSERT(cond) \ + if (Q_UNLIKELY(!(cond))) { \ + mixxx_release_assert(#cond, __FILE__, __LINE__, ASSERT_FUNCTION); \ + } // Checks that cond is true in debug builds. If cond is false then prints a // warning message to the console. If Mixxx is built with @@ -52,11 +52,20 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li // // In release builds, doSomething() is never called! #ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED -#define DEBUG_ASSERT(cond) ((!(cond)) ? mixxx_debug_assert(#cond, __FILE__, __LINE__, ASSERT_FUNCTION) : mixxx_noop()) +#define DEBUG_ASSERT(cond) \ + if (Q_UNLIKELY(!(cond))) { \ + mixxx_debug_assert(#cond, __FILE__, __LINE__, ASSERT_FUNCTION); \ + } #else #define DEBUG_ASSERT(cond) #endif /// Same as DEBUG_ASSERT, but if MIXXX_DEBUG_ASSERTIONS_FATAL is disabled run the specified fallback function. /// In most cases you should probably use this rather than DEBUG_ASSERT. Only use DEBUG_ASSERT if there is no appropriate fallback. -#define VERIFY_OR_DEBUG_ASSERT(cond) if ((!(cond)) && mixxx_maybe_debug_assert_return_true(#cond, __FILE__, __LINE__, ASSERT_FUNCTION)) +#ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED +#define VERIFY_OR_DEBUG_ASSERT(cond) \ + if (Q_UNLIKELY(!(cond)) && \ + mixxx_maybe_debug_assert_return_true(#cond, __FILE__, __LINE__, ASSERT_FUNCTION)) +#else +#define VERIFY_OR_DEBUG_ASSERT(cond) if (Q_UNLIKELY(!(cond))) +#endif From d0d37731ea4f51d091970bb4bb71066b6fa5b43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Fri, 23 Jul 2021 01:21:01 +0200 Subject: [PATCH 2/3] Cast cond to bool to to prevent mistakes like using an assignment operator instead of equality comparison --- src/util/assert.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/assert.h b/src/util/assert.h index 867c6a513bd..00189b5dbf8 100644 --- a/src/util/assert.h +++ b/src/util/assert.h @@ -38,7 +38,7 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li /// situations where we know Mixxx cannot take any action without potentially /// corrupting user data. Handle errors gracefully whenever possible. #define RELEASE_ASSERT(cond) \ - if (Q_UNLIKELY(!(cond))) { \ + if (Q_UNLIKELY(!static_cast(cond))) { \ mixxx_release_assert(#cond, __FILE__, __LINE__, ASSERT_FUNCTION); \ } @@ -53,7 +53,7 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li // In release builds, doSomething() is never called! #ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED #define DEBUG_ASSERT(cond) \ - if (Q_UNLIKELY(!(cond))) { \ + if (Q_UNLIKELY(!static_cast(cond))) { \ mixxx_debug_assert(#cond, __FILE__, __LINE__, ASSERT_FUNCTION); \ } #else @@ -63,9 +63,9 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li /// Same as DEBUG_ASSERT, but if MIXXX_DEBUG_ASSERTIONS_FATAL is disabled run the specified fallback function. /// In most cases you should probably use this rather than DEBUG_ASSERT. Only use DEBUG_ASSERT if there is no appropriate fallback. #ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED -#define VERIFY_OR_DEBUG_ASSERT(cond) \ - if (Q_UNLIKELY(!(cond)) && \ +#define VERIFY_OR_DEBUG_ASSERT(cond) \ + if (Q_UNLIKELY(!static_cast(cond)) && \ mixxx_maybe_debug_assert_return_true(#cond, __FILE__, __LINE__, ASSERT_FUNCTION)) #else -#define VERIFY_OR_DEBUG_ASSERT(cond) if (Q_UNLIKELY(!(cond))) +#define VERIFY_OR_DEBUG_ASSERT(cond) if (Q_UNLIKELY(!static_cast(cond))) #endif From 32071a44a252feb0d41353928dca92408147a32b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Fri, 23 Jul 2021 01:51:33 +0200 Subject: [PATCH 3/3] remove obsolete maybe --- src/util/assert.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/assert.h b/src/util/assert.h index 00189b5dbf8..9ddc572214b 100644 --- a/src/util/assert.h +++ b/src/util/assert.h @@ -11,7 +11,7 @@ inline void mixxx_debug_assert(const char* assertion, const char* file, int line #endif #ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED -inline bool mixxx_maybe_debug_assert_return_true(const char* assertion, +inline bool mixxx_debug_assert_return_true(const char* assertion, const char* file, int line, const char* function) { @@ -65,7 +65,7 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li #ifdef MIXXX_DEBUG_ASSERTIONS_ENABLED #define VERIFY_OR_DEBUG_ASSERT(cond) \ if (Q_UNLIKELY(!static_cast(cond)) && \ - mixxx_maybe_debug_assert_return_true(#cond, __FILE__, __LINE__, ASSERT_FUNCTION)) + mixxx_debug_assert_return_true(#cond, __FILE__, __LINE__, ASSERT_FUNCTION)) #else #define VERIFY_OR_DEBUG_ASSERT(cond) if (Q_UNLIKELY(!static_cast(cond))) #endif