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

Filipe Cabecinhas via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 25 07:39:18 PDT 2016


filcab created this revision.
filcab added reviewers: kcc, samsonov.
filcab added a subscriber: llvm-commits.
Herald added a subscriber: kubabrecka.

Added a macro to enumerate the (error name, error member name) pairs. This way,
when adding an error, we only need to add the pair to one place (plus add its
implementation, or course).

https://reviews.llvm.org/D23875

Files:
  lib/asan/asan_errors.h

Index: lib/asan/asan_errors.h
===================================================================
--- lib/asan/asan_errors.h
+++ lib/asan/asan_errors.h
@@ -119,51 +119,42 @@
   void Print();
 };
 
+#define FOR_EACH_ERROR_KIND_MEMBER_NAME_PAIR(macro) \
+  macro(StackOverflow, stack_overflow)              \
+  macro(DeadlySignal, deadly_signal)                \
+  macro(DoubleFree, double_free)                    \
+  macro(NewDeleteSizeMismatch, new_delete_size_mismatch)
+
 enum ErrorKind {
   kErrorKindInvalid = 0,
-  kErrorKindStackOverflow,
-  kErrorKindDeadlySignal,
-  kErrorKindDoubleFree,
-  kErrorKindNewDeleteSizeMismatch,
+#define X(kind, name) kErrorKind##kind,
+  FOR_EACH_ERROR_KIND_MEMBER_NAME_PAIR(X)
+#undef X
 };
 
 struct ErrorDescription {
   ErrorKind kind;
   union {
-    ErrorStackOverflow stack_overflow;
-    ErrorDeadlySignal deadly_signal;
-    ErrorDoubleFree double_free;
-    ErrorNewDeleteSizeMismatch new_delete_size_mismatch;
+#define X(kind, name) Error##kind name;
+    FOR_EACH_ERROR_KIND_MEMBER_NAME_PAIR(X)
+#undef X
   };
+
   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) {}
+#define X(kind_, name_) \
+  ErrorDescription(const Error##kind_ &e) : kind(kErrorKind##kind_), name_(e) {}
+  FOR_EACH_ERROR_KIND_MEMBER_NAME_PAIR(X)
+#undef X
 
   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;
+#define X(kind_, name_)   \
+  case kErrorKind##kind_: \
+    name_.Print();        \
+    return;
+      FOR_EACH_ERROR_KIND_MEMBER_NAME_PAIR(X)
+#undef X
       case kErrorKindInvalid:
         CHECK(0);
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23875.69246.patch
Type: text/x-patch
Size: 2500 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160825/b823c248/attachment.bin>


More information about the llvm-commits mailing list