Skip to content
New issue

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

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

Already on GitHub? # to your account

Utility: deduplicate __FILE__ in internal assertion macros #182

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
26 changes: 18 additions & 8 deletions src/Corrade/Utility/Assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <cstdlib>

#include "Corrade/Utility/Debug.h"
#include "Corrade/Utility/Macros.h" /* CORRADE_LINE_STRING */
#elif !defined(NDEBUG)
#include <cassert>
#endif
Expand Down Expand Up @@ -408,10 +407,17 @@
#elif defined(CORRADE_STANDARD_ASSERT)
#define CORRADE_INTERNAL_ASSERT(condition) assert(condition)
#else
/* The __FILE__ is deliberately printed separately instead of joined with the
rest of the string literal to deduplicate it in the binary. As the full file
path is usually used by the buildsystem, it can get rather long, and the
savings from deduplicating significantly outweigh the extra code size,
especially in assertion-heavy code. OTOH the "Assertion" and "failed at" is
joined with the stringified condition, as they're relatively short and the
extra code size would likely be more than the savings. */
#define CORRADE_INTERNAL_ASSERT(condition) \
do { \
if(!(condition)) { \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput()} << "Assertion " #condition " failed at " __FILE__ ":" CORRADE_LINE_STRING; \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput(), Corrade::Utility::Debug::Flag::NoSpace} << "Assertion " #condition " failed at " << __FILE__ ":" << __LINE__; \
std::abort(); \
} \
} while(false)
Expand Down Expand Up @@ -451,9 +457,10 @@
assert(!#condition); \
}(), 0))
#else
/* See CORRADE_INTERNAL_ASSERT() for why __FILE__ is printed separately */
#define CORRADE_INTERNAL_CONSTEXPR_ASSERT(condition) \
static_cast<void>((condition) ? 0 : ([&]() { \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput()} << "Assertion " #condition " failed at " __FILE__ ":" CORRADE_LINE_STRING; \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput(), Corrade::Utility::Debug::Flag::NoSpace} << "Assertion " #condition " failed at " << __FILE__ ":" << __LINE__; \
std::abort(); \
}(), 0))
#endif
Expand Down Expand Up @@ -485,10 +492,11 @@
#elif defined(CORRADE_STANDARD_ASSERT)
#define CORRADE_INTERNAL_ASSERT_OUTPUT(call) assert(call)
#else
/* See CORRADE_INTERNAL_ASSERT() for why __FILE__ is printed separately */
#define CORRADE_INTERNAL_ASSERT_OUTPUT(call) \
do { \
if(!(call)) { \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput()} << "Assertion " #call " failed at " __FILE__ ":" CORRADE_LINE_STRING; \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput(), Corrade::Utility::Debug::Flag::NoSpace} << "Assertion " #call " failed at " << __FILE__ ":" << __LINE__; \
std::abort(); \
} \
} while(false)
Expand All @@ -503,9 +511,9 @@
return Corrade::Utility::forward<T>(value);
}
#else
template<class T> T assertExpression(T&& value, const char* message) {
template<class T> T assertExpression(T&& value, const char* message, const char* file, int line) {
if(!value) {
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput()} << message;
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput(), Corrade::Utility::Debug::Flag::NoSpace} << message << file << line;

Check warning on line 516 in src/Corrade/Utility/Assert.h

View check run for this annotation

Codecov / codecov/patch

src/Corrade/Utility/Assert.h#L516

Added line #L516 was not covered by tests
std::abort();
}

Expand Down Expand Up @@ -559,7 +567,8 @@
#elif defined(CORRADE_STANDARD_ASSERT)
#define CORRADE_INTERNAL_ASSERT_EXPRESSION(...) Corrade::Utility::Implementation::assertExpression(__VA_ARGS__)
#else
#define CORRADE_INTERNAL_ASSERT_EXPRESSION(...) Corrade::Utility::Implementation::assertExpression(__VA_ARGS__, "Assertion " #__VA_ARGS__ " failed at " __FILE__ ":" CORRADE_LINE_STRING)
/* See CORRADE_INTERNAL_ASSERT() for why __FILE__ is printed separately */
#define CORRADE_INTERNAL_ASSERT_EXPRESSION(...) Corrade::Utility::Implementation::assertExpression(__VA_ARGS__, "Assertion " #__VA_ARGS__ " failed at ", __FILE__ ":", __LINE__)
#endif
#endif

Expand Down Expand Up @@ -602,9 +611,10 @@
#elif defined(CORRADE_STANDARD_ASSERT)
#define CORRADE_INTERNAL_ASSERT_UNREACHABLE() assert(!"unreachable code")
#else
/* See CORRADE_INTERNAL_ASSERT() for why __FILE__ is printed separately */
#define CORRADE_INTERNAL_ASSERT_UNREACHABLE() \
do { \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput()} << "Reached unreachable code at " __FILE__ ":" CORRADE_LINE_STRING; \
Corrade::Utility::Error{Corrade::Utility::Error::defaultOutput(), Corrade::Utility::Debug::Flag::NoSpace} << "Reached unreachable code at " << __FILE__ ":" << __LINE__; \
std::abort(); \
} while(false)
#endif
Expand Down