You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error_private.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /* Note : this module is expected to remain private, do not expose it */
  11. #ifndef ERROR_H_MODULE
  12. #define ERROR_H_MODULE
  13. #if defined (__cplusplus)
  14. extern "C" {
  15. #endif
  16. /* ****************************************
  17. * Dependencies
  18. ******************************************/
  19. #include "zstd_errors.h" /* enum list */
  20. #include "compiler.h"
  21. #include "debug.h"
  22. #include "zstd_deps.h" /* size_t */
  23. /* ****************************************
  24. * Compiler-specific
  25. ******************************************/
  26. #if defined(__GNUC__)
  27. # define ERR_STATIC static __attribute__((unused))
  28. #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  29. # define ERR_STATIC static inline
  30. #elif defined(_MSC_VER)
  31. # define ERR_STATIC static __inline
  32. #else
  33. # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
  34. #endif
  35. /*-****************************************
  36. * Customization (error_public.h)
  37. ******************************************/
  38. typedef ZSTD_ErrorCode ERR_enum;
  39. #define PREFIX(name) ZSTD_error_##name
  40. /*-****************************************
  41. * Error codes handling
  42. ******************************************/
  43. #undef ERROR /* already defined on Visual Studio */
  44. #define ERROR(name) ZSTD_ERROR(name)
  45. #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
  46. ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
  47. ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
  48. /* check and forward error code */
  49. #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
  50. #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
  51. /*-****************************************
  52. * Error Strings
  53. ******************************************/
  54. const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
  55. ERR_STATIC const char* ERR_getErrorName(size_t code)
  56. {
  57. return ERR_getErrorString(ERR_getErrorCode(code));
  58. }
  59. /**
  60. * Ignore: this is an internal helper.
  61. *
  62. * This is a helper function to help force C99-correctness during compilation.
  63. * Under strict compilation modes, variadic macro arguments can't be empty.
  64. * However, variadic function arguments can be. Using a function therefore lets
  65. * us statically check that at least one (string) argument was passed,
  66. * independent of the compilation flags.
  67. */
  68. static INLINE_KEYWORD UNUSED_ATTR
  69. void _force_has_format_string(const char *format, ...) {
  70. (void)format;
  71. }
  72. /**
  73. * Ignore: this is an internal helper.
  74. *
  75. * We want to force this function invocation to be syntactically correct, but
  76. * we don't want to force runtime evaluation of its arguments.
  77. */
  78. #define _FORCE_HAS_FORMAT_STRING(...) \
  79. if (0) { \
  80. _force_has_format_string(__VA_ARGS__); \
  81. }
  82. #define ERR_QUOTE(str) #str
  83. /**
  84. * Return the specified error if the condition evaluates to true.
  85. *
  86. * In debug modes, prints additional information.
  87. * In order to do that (particularly, printing the conditional that failed),
  88. * this can't just wrap RETURN_ERROR().
  89. */
  90. #define RETURN_ERROR_IF(cond, err, ...) \
  91. if (cond) { \
  92. RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
  93. __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
  94. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  95. RAWLOG(3, ": " __VA_ARGS__); \
  96. RAWLOG(3, "\n"); \
  97. return ERROR(err); \
  98. }
  99. /**
  100. * Unconditionally return the specified error.
  101. *
  102. * In debug modes, prints additional information.
  103. */
  104. #define RETURN_ERROR(err, ...) \
  105. do { \
  106. RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
  107. __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
  108. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  109. RAWLOG(3, ": " __VA_ARGS__); \
  110. RAWLOG(3, "\n"); \
  111. return ERROR(err); \
  112. } while(0);
  113. /**
  114. * If the provided expression evaluates to an error code, returns that error code.
  115. *
  116. * In debug modes, prints additional information.
  117. */
  118. #define FORWARD_IF_ERROR(err, ...) \
  119. do { \
  120. size_t const err_code = (err); \
  121. if (ERR_isError(err_code)) { \
  122. RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
  123. __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
  124. _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
  125. RAWLOG(3, ": " __VA_ARGS__); \
  126. RAWLOG(3, "\n"); \
  127. return err_code; \
  128. } \
  129. } while(0);
  130. #if defined (__cplusplus)
  131. }
  132. #endif
  133. #endif /* ERROR_H_MODULE */