Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constexpr DEBUG_ASSERT #4131

Merged
merged 3 commits into from
Jul 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/util/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

#include <QtDebug>


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_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);
Expand All @@ -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(!static_cast<bool>(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
Expand All @@ -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(!static_cast<bool>(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(!static_cast<bool>(cond)) && \
mixxx_debug_assert_return_true(#cond, __FILE__, __LINE__, ASSERT_FUNCTION))
#else
#define VERIFY_OR_DEBUG_ASSERT(cond) if (Q_UNLIKELY(!static_cast<bool>(cond)))
#endif