Let SPIRV_ASSERT() exit the program if the assertion fails.
diff --git a/source/opt/log.h b/source/opt/log.h
index 2b033fe..8432d4a 100644
--- a/source/opt/log.h
+++ b/source/opt/log.h
@@ -22,8 +22,9 @@
#include "message.h"
-// Asserts the given condition is true. Otherwise, send a message to the
-// consumer. Accepts the following formats:
+// Asserts the given condition is true. Otherwise, sends a message to the
+// consumer and exits the problem with failure code. Accepts the following
+// formats:
//
// SPIRV_ASSERT(<message-consumer>, <condition-expression>);
// SPIRV_ASSERT(<message-consumer>, <condition-expression>, <message>);
@@ -133,24 +134,30 @@
#define SPIRV_ASSERT_1(consumer, condition) \
do { \
- if (!(condition)) \
+ if (!(condition)) { \
spvtools::Log(consumer, MessageLevel::InternalError, __FILE__, \
{__LINE__, 0, 0}, "assertion failed: " #condition); \
+ std::exit(EXIT_FAILURE); \
+ } \
} while (0)
#define SPIRV_ASSERT_2(consumer, condition, message) \
do { \
- if (!(condition)) \
+ if (!(condition)) { \
spvtools::Log(consumer, MessageLevel::InternalError, __FILE__, \
{__LINE__, 0, 0}, "assertion failed: " message); \
+ std::exit(EXIT_FAILURE); \
+ } \
} while (0)
#define SPIRV_ASSERT_more(consumer, condition, format, ...) \
do { \
- if (!(condition)) \
+ if (!(condition)) { \
spvtools::Logf(consumer, MessageLevel::InternalError, __FILE__, \
{__LINE__, 0, 0}, "assertion failed: " format, \
__VA_ARGS__); \
+ std::exit(EXIT_FAILURE); \
+ } \
} while (0)
#define SPIRV_ASSERT_3(consumer, condition, format, ...) \
diff --git a/test/test_log.cpp b/test/test_log.cpp
index ac19549..47a0f5c 100644
--- a/test/test_log.cpp
+++ b/test/test_log.cpp
@@ -23,63 +23,6 @@
using namespace spvtools;
using ::testing::MatchesRegex;
-TEST(Log, AssertStatement) {
- int invocation = 0;
- auto consumer = [&invocation](MessageLevel level, const char* source,
- const spv_position_t&, const char* message) {
- ++invocation;
- EXPECT_EQ(MessageLevel::InternalError, level);
- EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
- EXPECT_STREQ("assertion failed: 1 + 2 == 5", message);
- };
-
- SPIRV_ASSERT(consumer, 1 + 2 == 5);
-#if defined(NDEBUG)
- (void)consumer;
- EXPECT_EQ(0, invocation);
-#else
- EXPECT_EQ(1, invocation);
-#endif
-}
-
-TEST(Log, AssertMessage) {
- int invocation = 0;
- auto consumer = [&invocation](MessageLevel level, const char* source,
- const spv_position_t&, const char* message) {
- ++invocation;
- EXPECT_EQ(MessageLevel::InternalError, level);
- EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
- EXPECT_STREQ("assertion failed: happy asserting!", message);
- };
-
- SPIRV_ASSERT(consumer, 1 + 2 == 5, "happy asserting!");
-#if defined(NDEBUG)
- (void)consumer;
- EXPECT_EQ(0, invocation);
-#else
- EXPECT_EQ(1, invocation);
-#endif
-}
-
-TEST(Log, AssertFormattedMessage) {
- int invocation = 0;
- auto consumer = [&invocation](MessageLevel level, const char* source,
- const spv_position_t&, const char* message) {
- ++invocation;
- EXPECT_EQ(MessageLevel::InternalError, level);
- EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
- EXPECT_STREQ("assertion failed: 1 + 2 actually is 3", message);
- };
-
- SPIRV_ASSERT(consumer, 1 + 2 == 5, "1 + 2 actually is %d", 1 + 2);
-#if defined(NDEBUG)
- (void)consumer;
- EXPECT_EQ(0, invocation);
-#else
- EXPECT_EQ(1, invocation);
-#endif
-}
-
TEST(Log, Unimplemented) {
int invocation = 0;
auto consumer = [&invocation](MessageLevel level, const char* source,