This is an archive of the discontinued LLVM Phabricator instance.

Ease dealing with tagged enum ErrorDescription with some macros.
ClosedPublic

Authored by filcab on Aug 25 2016, 7:39 AM.

Details

Summary

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).

Diff Detail

Repository
rL LLVM

Event Timeline

filcab updated this revision to Diff 69246.Aug 25 2016, 7:39 AM
filcab retitled this revision from to Ease dealing with tagged enum ErrorDescription with some macros..
filcab updated this object.
filcab added reviewers: kcc, samsonov.
filcab added a subscriber: llvm-commits.
filcab edited reviewers, added: eugenis, vitalybuka; removed: samsonov.Sep 8 2016, 7:07 AM
vitalybuka edited edge metadata.Sep 8 2016, 10:42 AM

It saves 9 line of code, but makes it less readable.
I strongly prefer existing code.

vitalybuka requested changes to this revision.Sep 9 2016, 2:41 PM
vitalybuka edited edge metadata.
lib/asan/asan_errors.h
122 ↗(On Diff #69246)

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
This revision now requires changes to proceed.Sep 9 2016, 2:41 PM
filcab updated this revision to Diff 70999.Sep 12 2016, 5:43 AM
filcab edited edge metadata.

Updated macro with review comments.

vitalybuka accepted this revision.Sep 12 2016, 9:59 AM
vitalybuka edited edge metadata.

LGTM

This revision is now accepted and ready to land.Sep 12 2016, 9:59 AM
This revision was automatically updated to reflect the committed changes.