assert: _a_ expression string shouldn't be part of the format string

The expression may contain the % operator.
This commit is contained in:
MerryMage 2016-12-11 15:29:13 +00:00 committed by Merry
parent 179a3388f9
commit dcc880a002

View file

@ -5,7 +5,6 @@
#pragma once #pragma once
#include <cstdio> #include <cstdio>
#include <cstdlib>
// For asserts we'd like to keep all the junk executed when an assert happens away from the // For asserts we'd like to keep all the junk executed when an assert happens away from the
// important code in the function. One way of doing this is to put all the relevant code inside a // important code in the function. One way of doing this is to put all the relevant code inside a
@ -22,19 +21,19 @@ __declspec(noinline, noreturn)
#endif #endif
static void assert_noinline_call(const Fn& fn) { static void assert_noinline_call(const Fn& fn) {
fn(); fn();
exit(EXIT_FAILURE); // Keeps GCC's mouth shut about this actually returning throw "";
} }
#define ASSERT(_a_) \ #define ASSERT(_a_) \
do if (!(_a_)) { assert_noinline_call([] { \ do if (!(_a_)) { assert_noinline_call([] { \
fprintf(stderr, "Assertion Failed!\n" #_a_); \ fprintf(stderr, "Assertion Failed!: %s\n", #_a_); \
throw ""; \
}); } while (false) }); } while (false)
#define ASSERT_MSG(_a_, ...) \ #define ASSERT_MSG(_a_, ...) \
do if (!(_a_)) { assert_noinline_call([&] { \ do if (!(_a_)) { assert_noinline_call([&] { \
fprintf(stderr, "Assertion Failed!\n" #_a_ "\n" __VA_ARGS__); \ fprintf(stderr, "Assertion Failed!: %s\n", #_a_); \
throw ""; \ fprintf(stderr, "Message: " __VA_ARGS__); \
fprintf(stderr, "\n"); \
}); } while (false) }); } while (false)
#define UNREACHABLE() ASSERT_MSG(false, "Unreachable code!") #define UNREACHABLE() ASSERT_MSG(false, "Unreachable code!")