From: Lei Zhang Date: Fri, 16 Sep 2016 16:10:47 +0000 (-0400) Subject: Let SPIRV_ASSERT() exit the program if the assertion fails. X-Git-Tag: upstream/2018.6~1051 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=869440ebd495f226440e738509f86221732e1942;p=platform%2Fupstream%2FSPIRV-Tools.git 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(, ); // SPIRV_ASSERT(, , ); @@ -133,24 +134,30 @@ void Logf(const MessageConsumer& consumer, MessageLevel level, const char* file, #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 @@ namespace { 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,