[PATCH] D23875: Ease dealing with tagged enum ErrorDescription with some macros.

Filipe Cabecinhas via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 12 10:20:35 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL281237: [asan] Ease dealing with tagged enum ErrorDescription with some macros. (authored by filcab).

Changed prior to commit:
  https://reviews.llvm.org/D23875?vs=70999&id=71034#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D23875

Files:
  compiler-rt/trunk/lib/asan/asan_errors.h

Index: compiler-rt/trunk/lib/asan/asan_errors.h
===================================================================
--- compiler-rt/trunk/lib/asan/asan_errors.h
+++ compiler-rt/trunk/lib/asan/asan_errors.h
@@ -122,12 +122,25 @@
   void Print();
 };
 
+// clang-format off
+#define ASAN_FOR_EACH_ERROR_KIND(macro) \
+  macro(StackOverflow)                  \
+  macro(DeadlySignal)                   \
+  macro(DoubleFree)                     \
+  macro(NewDeleteSizeMismatch)
+// clang-format on
+
+#define ASAN_DEFINE_ERROR_KIND(name) kErrorKind##name,
+#define ASAN_ERROR_DESCRIPTION_MEMBER(name) Error##name name;
+#define ASAN_ERROR_DESCRIPTION_CONSTRUCTOR(name) \
+  ErrorDescription(Error##name const &e) : kind(kErrorKind##name), name(e) {}
+#define ASAN_ERROR_DESCRIPTION_PRINT(name) \
+  case kErrorKind##name:                   \
+    return name.Print();
+
 enum ErrorKind {
   kErrorKindInvalid = 0,
-  kErrorKindStackOverflow,
-  kErrorKindDeadlySignal,
-  kErrorKindDoubleFree,
-  kErrorKindNewDeleteSizeMismatch,
+  ASAN_FOR_EACH_ERROR_KIND(ASAN_DEFINE_ERROR_KIND)
 };
 
 struct ErrorDescription {
@@ -138,47 +151,29 @@
   // We can add a wrapper around it to make it "more c++-like", but that would
   // add a lot of code and the benefit wouldn't be that big.
   union {
-    ErrorStackOverflow stack_overflow;
-    ErrorDeadlySignal deadly_signal;
-    ErrorDoubleFree double_free;
-    ErrorNewDeleteSizeMismatch new_delete_size_mismatch;
+    ASAN_FOR_EACH_ERROR_KIND(ASAN_ERROR_DESCRIPTION_MEMBER)
   };
+
   ErrorDescription() { internal_memset(this, 0, sizeof(*this)); }
-  ErrorDescription(const ErrorStackOverflow &e)  // NOLINT
-      : kind(kErrorKindStackOverflow),
-        stack_overflow(e) {}
-  ErrorDescription(const ErrorDeadlySignal &e)  // NOLINT
-      : kind(kErrorKindDeadlySignal),
-        deadly_signal(e) {}
-  ErrorDescription(const ErrorDoubleFree &e)  // NOLINT
-      : kind(kErrorKindDoubleFree),
-        double_free(e) {}
-  ErrorDescription(const ErrorNewDeleteSizeMismatch &e)  // NOLINT
-      : kind(kErrorKindNewDeleteSizeMismatch),
-        new_delete_size_mismatch(e) {}
+  ASAN_FOR_EACH_ERROR_KIND(ASAN_ERROR_DESCRIPTION_CONSTRUCTOR)
 
   bool IsValid() { return kind != kErrorKindInvalid; }
   void Print() {
     switch (kind) {
-      case kErrorKindStackOverflow:
-        stack_overflow.Print();
-        return;
-      case kErrorKindDeadlySignal:
-        deadly_signal.Print();
-        return;
-      case kErrorKindDoubleFree:
-        double_free.Print();
-        return;
-      case kErrorKindNewDeleteSizeMismatch:
-        new_delete_size_mismatch.Print();
-        return;
+      ASAN_FOR_EACH_ERROR_KIND(ASAN_ERROR_DESCRIPTION_PRINT)
       case kErrorKindInvalid:
         CHECK(0);
     }
     CHECK(0);
   }
 };
 
+#undef ASAN_FOR_EACH_ERROR_KIND
+#undef ASAN_DEFINE_ERROR_KIND
+#undef ASAN_ERROR_DESCRIPTION_MEMBER
+#undef ASAN_ERROR_DESCRIPTION_CONSTRUCTOR
+#undef ASAN_ERROR_DESCRIPTION_PRINT
+
 }  // namespace __asan
 
 #endif  // ASAN_ERRORS_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23875.71034.patch
Type: text/x-patch
Size: 3028 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160912/36e78e56/attachment.bin>


More information about the llvm-commits mailing list