Change to use enum instead of enum class for MessageLevel.
authorLei Zhang <antiagainst@google.com>
Wed, 21 Sep 2016 15:30:41 +0000 (11:30 -0400)
committerLei Zhang <antiagainst@google.com>
Wed, 21 Sep 2016 15:30:41 +0000 (11:30 -0400)
This allows us to create C interface with MessageLevel later.

source/diagnostic.cpp
source/message.cpp
source/message.h
source/opt/log.h
test/BinaryParse.cpp
test/c_interface.cpp
test/cpp_interface.cpp
test/test_log.cpp

index 16bc33a..b92ff00 100644 (file)
@@ -71,22 +71,22 @@ namespace libspirv {
 DiagnosticStream::~DiagnosticStream() {
   using spvtools::MessageLevel;
   if (error_ != SPV_FAILED_MATCH && consumer_ != nullptr) {
-    auto level = MessageLevel::Error;
+    auto level = MessageLevel::kError;
     switch (error_) {
       case SPV_SUCCESS:
       case SPV_REQUESTED_TERMINATION:  // Essentially success.
-        level = MessageLevel::Info;
+        level = MessageLevel::kInfo;
         break;
       case SPV_WARNING:
-        level = MessageLevel::Warning;
+        level = MessageLevel::kWarning;
         break;
       case SPV_UNSUPPORTED:
       case SPV_ERROR_INTERNAL:
       case SPV_ERROR_INVALID_TABLE:
-        level = MessageLevel::InternalError;
+        level = MessageLevel::kInternalError;
         break;
       case SPV_ERROR_OUT_OF_MEMORY:
-        level = MessageLevel::Fatal;
+        level = MessageLevel::kFatal;
         break;
       default:
         break;
index 9d28c8c..efbd65c 100644 (file)
@@ -23,22 +23,22 @@ std::string StringifyMessage(MessageLevel level, const char* source,
                              const char* message) {
   const char* level_string = nullptr;
   switch (level) {
-    case MessageLevel::Fatal:
+    case MessageLevel::kFatal:
       level_string = "fatal";
       break;
-    case MessageLevel::InternalError:
+    case MessageLevel::kInternalError:
       level_string = "internal error";
       break;
-    case MessageLevel::Error:
+    case MessageLevel::kError:
       level_string = "error";
       break;
-    case MessageLevel::Warning:
+    case MessageLevel::kWarning:
       level_string = "warning";
       break;
-    case MessageLevel::Info:
+    case MessageLevel::kInfo:
       level_string = "info";
       break;
-    case MessageLevel::Debug:
+    case MessageLevel::kDebug:
       level_string = "debug";
       break;
   }
index 7af4297..9eecacf 100644 (file)
@@ -25,15 +25,17 @@ namespace spvtools {
 // TODO(antiagainst): This eventually should be in the C++ interface.
 
 // Severity levels of messages communicated to the consumer.
-enum class MessageLevel {
-  Fatal,  // Unrecoverable error due to environment. Will abort the program
-          // immediately. E.g., out of memory.
-  InternalError,  // Unrecoverable error due to SPIRV-Tools internals. Will
-                  // abort the program immediately. E.g., unimplemented feature.
-  Error,          // Normal error due to user input.
-  Warning,        // Warning information.
-  Info,           // General information.
-  Debug,          // Debug information.
+enum MessageLevel {
+  kFatal,          // Unrecoverable error due to environment.
+                   // Will exit the program immediately. E.g.,
+                   // out of memory.
+  kInternalError,  // Unrecoverable error due to SPIRV-Tools internals.
+                   // Will exit the program immediately. E.g.,
+                   // unimplemented feature.
+  kError,          // Normal error due to user input.
+  kWarning,        // Warning information.
+  kInfo,           // General information.
+  kDebug,          // Debug information.
 };
 
 // Message consumer. The C strings for source and message are only alive for the
index 8432d4a..9ad24d5 100644 (file)
 
 // Logs an error message to the consumer saying the given feature is
 // unimplemented.
-#define SPIRV_UNIMPLEMENTED(consumer, feature)                     \
-  do {                                                             \
-    spvtools::Log(consumer, MessageLevel::InternalError, __FILE__, \
-                  {__LINE__, 0, 0}, "unimplemented: " feature);    \
+#define SPIRV_UNIMPLEMENTED(consumer, feature)                      \
+  do {                                                              \
+    spvtools::Log(consumer, MessageLevel::kInternalError, __FILE__, \
+                  {__LINE__, 0, 0}, "unimplemented: " feature);     \
   } while (0)
 
 // Logs an error message to the consumer saying the code location
 // should be unreachable.
-#define SPIRV_UNREACHABLE(consumer)                                \
-  do {                                                             \
-    spvtools::Log(consumer, MessageLevel::InternalError, __FILE__, \
-                  {__LINE__, 0, 0}, "unreachable");                \
+#define SPIRV_UNREACHABLE(consumer)                                 \
+  do {                                                              \
+    spvtools::Log(consumer, MessageLevel::kInternalError, __FILE__, \
+                  {__LINE__, 0, 0}, "unreachable");                 \
   } while (0)
 
 // Helper macros for concatenating arguments.
@@ -135,31 +135,31 @@ void Logf(const MessageConsumer& consumer, MessageLevel level, const char* file,
 #define SPIRV_ASSERT_1(consumer, condition)                             \
   do {                                                                  \
     if (!(condition)) {                                                 \
-      spvtools::Log(consumer, MessageLevel::InternalError, __FILE__,    \
+      spvtools::Log(consumer, MessageLevel::kInternalError, __FILE__,   \
                     {__LINE__, 0, 0}, "assertion failed: " #condition); \
       std::exit(EXIT_FAILURE);                                          \
     }                                                                   \
   } while (0)
 
-#define SPIRV_ASSERT_2(consumer, condition, message)                 \
-  do {                                                               \
-    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, ...)           \
+#define SPIRV_ASSERT_2(consumer, condition, message)                  \
   do {                                                                \
     if (!(condition)) {                                               \
-      spvtools::Logf(consumer, MessageLevel::InternalError, __FILE__, \
-                     {__LINE__, 0, 0}, "assertion failed: " format,   \
-                     __VA_ARGS__);                                    \
+      spvtools::Log(consumer, MessageLevel::kInternalError, __FILE__, \
+                    {__LINE__, 0, 0}, "assertion failed: " message);  \
       std::exit(EXIT_FAILURE);                                        \
     }                                                                 \
   } while (0)
 
+#define SPIRV_ASSERT_more(consumer, condition, format, ...)            \
+  do {                                                                 \
+    if (!(condition)) {                                                \
+      spvtools::Logf(consumer, MessageLevel::kInternalError, __FILE__, \
+                     {__LINE__, 0, 0}, "assertion failed: " format,    \
+                     __VA_ARGS__);                                     \
+      std::exit(EXIT_FAILURE);                                         \
+    }                                                                  \
+  } while (0)
+
 #define SPIRV_ASSERT_3(consumer, condition, format, ...) \
   SPIRV_ASSERT_more(consumer, condition, format, __VA_ARGS__)
 
index cb79461..ceb169a 100644 (file)
@@ -306,7 +306,7 @@ TEST_F(BinaryParseTest, SpecifyConsumerNullDiagnosticsForBadParse) {
       ctx, [&invocation](spvtools::MessageLevel level, const char* source,
                          const spv_position_t& position, const char* message) {
         ++invocation;
-        EXPECT_EQ(spvtools::MessageLevel::Error, level);
+        EXPECT_EQ(spvtools::MessageLevel::kError, level);
         EXPECT_STREQ("input", source);
         EXPECT_EQ(0u, position.line);
         EXPECT_EQ(0u, position.column);
index 0fecda1..0f8992f 100644 (file)
@@ -109,7 +109,7 @@ TEST(CInterface, SpecifyConsumerNullDiagnosticForAssembling) {
       [&invocation](MessageLevel level, const char* source,
                     const spv_position_t& position, const char* message) {
         ++invocation;
-        EXPECT_EQ(MessageLevel::Error, level);
+        EXPECT_EQ(MessageLevel::kError, level);
         // The error happens at scanning the begining of second line.
         EXPECT_STREQ("input", source);
         EXPECT_EQ(1u, position.line);
@@ -137,7 +137,7 @@ TEST(CInterface, SpecifyConsumerNullDiagnosticForDisassembling) {
       [&invocation](MessageLevel level, const char* source,
                     const spv_position_t& position, const char* message) {
         ++invocation;
-        EXPECT_EQ(MessageLevel::Error, level);
+        EXPECT_EQ(MessageLevel::kError, level);
         EXPECT_STREQ("input", source);
         EXPECT_EQ(0u, position.line);
         EXPECT_EQ(0u, position.column);
@@ -172,7 +172,7 @@ TEST(CInterface, SpecifyConsumerNullDiagnosticForValidating) {
       [&invocation](MessageLevel level, const char* source,
                     const spv_position_t& position, const char* message) {
         ++invocation;
-        EXPECT_EQ(MessageLevel::Error, level);
+        EXPECT_EQ(MessageLevel::kError, level);
         EXPECT_STREQ("input", source);
         EXPECT_EQ(0u, position.line);
         EXPECT_EQ(0u, position.column);
index c24c35a..f464fcf 100644 (file)
@@ -36,7 +36,7 @@ TEST(CppInterface, SuccessfulRoundTrip) {
   // This cannot pass validation since %1 is not defined.
   t.SetMessageConsumer([](MessageLevel level, const char* source,
                           const spv_position_t& position, const char* message) {
-    EXPECT_EQ(MessageLevel::Error, level);
+    EXPECT_EQ(MessageLevel::kError, level);
     EXPECT_STREQ("input", source);
     EXPECT_EQ(0u, position.line);
     EXPECT_EQ(0u, position.column);
@@ -68,7 +68,7 @@ TEST(CppInterface, AssembleWithWrongTargetEnv) {
       [&invocation_count](MessageLevel level, const char* source,
                           const spv_position_t& position, const char* message) {
         ++invocation_count;
-        EXPECT_EQ(MessageLevel::Error, level);
+        EXPECT_EQ(MessageLevel::kError, level);
         EXPECT_STREQ("input", source);
         EXPECT_EQ(0u, position.line);
         EXPECT_EQ(5u, position.column);
@@ -90,7 +90,7 @@ TEST(CppInterface, DisassembleEmptyModule) {
       [&invocation_count](MessageLevel level, const char* source,
                           const spv_position_t& position, const char* message) {
         ++invocation_count;
-        EXPECT_EQ(MessageLevel::Error, level);
+        EXPECT_EQ(MessageLevel::kError, level);
         EXPECT_STREQ("input", source);
         EXPECT_EQ(0u, position.line);
         EXPECT_EQ(0u, position.column);
@@ -111,7 +111,7 @@ TEST(CppInterface, DisassembleWithWrongTargetEnv) {
       [&invocation_count](MessageLevel level, const char* source,
                           const spv_position_t& position, const char* message) {
         ++invocation_count;
-        EXPECT_EQ(MessageLevel::Error, level);
+        EXPECT_EQ(MessageLevel::kError, level);
         EXPECT_STREQ("input", source);
         EXPECT_EQ(0u, position.line);
         EXPECT_EQ(0u, position.column);
@@ -149,7 +149,7 @@ TEST(CppInterface, ValidateEmptyModule) {
       [&invocation_count](MessageLevel level, const char* source,
                           const spv_position_t& position, const char* message) {
         ++invocation_count;
-        EXPECT_EQ(MessageLevel::Error, level);
+        EXPECT_EQ(MessageLevel::kError, level);
         EXPECT_STREQ("input", source);
         EXPECT_EQ(0u, position.line);
         EXPECT_EQ(0u, position.column);
index 47a0f5c..2d4cb7d 100644 (file)
@@ -28,7 +28,7 @@ TEST(Log, Unimplemented) {
   auto consumer = [&invocation](MessageLevel level, const char* source,
                                 const spv_position_t&, const char* message) {
     ++invocation;
-    EXPECT_EQ(MessageLevel::InternalError, level);
+    EXPECT_EQ(MessageLevel::kInternalError, level);
     EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
     EXPECT_STREQ("unimplemented: the-ultimite-feature", message);
   };
@@ -42,7 +42,7 @@ TEST(Log, Unreachable) {
   auto consumer = [&invocation](MessageLevel level, const char* source,
                                 const spv_position_t&, const char* message) {
     ++invocation;
-    EXPECT_EQ(MessageLevel::InternalError, level);
+    EXPECT_EQ(MessageLevel::kInternalError, level);
     EXPECT_THAT(source, MatchesRegex(".*test_log.cpp$"));
     EXPECT_STREQ("unreachable", message);
   };