common.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef COMMON_H_
  6. #define COMMON_H_
  7. #include "string_format.h"
  8. #define ANALYZER_FATAL(...) fprintf(stderr, __VA_ARGS__), exit(1)
  9. #if WITH_EXCEPTIONS
  10. #define ANALYZER_TRY try {
  11. #define ANALYZER_CATCH_BAD_ALLOC \
  12. } \
  13. catch (std::bad_alloc &) {}
  14. #define ANALYZER_CATCH_BAD_ALLOC_AND_EXIT \
  15. } \
  16. catch (std::bad_alloc &) { ANALYZER_FATAL("Memory allocation failure.\n"); }
  17. #else
  18. #define ANALYZER_TRY
  19. #define ANALYZER_CATCH_BAD_ALLOC
  20. #define ANALYZER_CATCH_BAD_ALLOC_AND_EXIT
  21. #endif
  22. namespace analyzer {
  23. struct ObjdumpOptions {
  24. bool info;
  25. bool text_size;
  26. bool details;
  27. bool compare;
  28. const char *file_name;
  29. };
  30. struct Result {
  31. enum Enum {
  32. Ok,
  33. Error,
  34. };
  35. Result()
  36. : Result(Ok)
  37. {}
  38. Result(Enum e)
  39. : enum_(e)
  40. {}
  41. operator Enum() const { return enum_; }
  42. Result &operator|=(Result rhs);
  43. private:
  44. Enum enum_;
  45. };
  46. inline Result
  47. operator|(Result lhs, Result rhs)
  48. {
  49. return (lhs == Result::Error || rhs == Result::Error) ? Result::Error
  50. : Result::Ok;
  51. }
  52. inline Result &
  53. Result::operator|=(Result rhs)
  54. {
  55. enum_ = *this | rhs;
  56. return *this;
  57. }
  58. inline bool
  59. Succeeded(Result result)
  60. {
  61. return result == Result::Ok;
  62. }
  63. inline bool
  64. Failed(Result result)
  65. {
  66. return result == Result::Error;
  67. }
  68. #define CHECK_RESULT(expr) \
  69. do { \
  70. if (Failed(expr)) { \
  71. return Result::Error; \
  72. } \
  73. } while (0)
  74. #define ERROR_IF(expr, ...) \
  75. do { \
  76. if (expr) { \
  77. PrintError(__VA_ARGS__); \
  78. return Result::Error; \
  79. } \
  80. } while (0)
  81. #define ERROR_UNLESS(expr, ...) ERROR_IF(!(expr), __VA_ARGS__)
  82. } // namespace analyzer
  83. #endif