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

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 9 14:41:43 PDT 2016


vitalybuka requested changes to this revision.
vitalybuka added a comment.
This revision now requires changes to proceed.




================
Comment at: lib/asan/asan_errors.h:122
@@ -121,1 +121,3 @@
 
+#define FOR_EACH_ERROR_KIND_MEMBER_NAME_PAIR(macro) \
+  macro(StackOverflow, stack_overflow)              \
----------------
Seems like no one on our side likes this macros, but I don't yet see a better solution.
I can just advice small improvements:

We can sacrifice snake case for a simpler macros with just one argument.
Also moving #defines makes code better structured.

```
// 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,
  ASAN_FOR_EACH_ERROR_KIND(ASAN_DEFINE_ERROR_KIND)
};

struct ErrorDescription {
  ErrorKind kind;
  union {
    ASAN_FOR_EACH_ERROR_KIND(ASAN_ERROR_DESCRIPTION_MEMBER)
  };

  ErrorDescription() { internal_memset(this, 0, sizeof(*this)); }
  ASAN_FOR_EACH_ERROR_KIND(ASAN_ERROR_DESCRIPTION_CONSTRUCTOR)

  bool IsValid() { return kind != kErrorKindInvalid; }
  void Print() {
    switch (kind) {
      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
```


https://reviews.llvm.org/D23875





More information about the llvm-commits mailing list