2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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.
11 /* Note : this module is expected to remain private, do not expose it */
13 #ifndef ERROR_H_MODULE
14 #define ERROR_H_MODULE
16 #if defined (__cplusplus)
21 /* ****************************************
23 ******************************************/
24 #include "../zstd_errors.h" /* enum list */
27 #include "zstd_deps.h" /* size_t */
30 /* ****************************************
32 ******************************************/
34 # define ERR_STATIC static __attribute__((unused))
35 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
36 # define ERR_STATIC static inline
37 #elif defined(_MSC_VER)
38 # define ERR_STATIC static __inline
40 # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
44 /*-****************************************
45 * Customization (error_public.h)
46 ******************************************/
47 typedef ZSTD_ErrorCode ERR_enum;
48 #define PREFIX(name) ZSTD_error_##name
51 /*-****************************************
52 * Error codes handling
53 ******************************************/
54 #undef ERROR /* already defined on Visual Studio */
55 #define ERROR(name) ZSTD_ERROR(name)
56 #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
58 ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
60 ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
62 /* check and forward error code */
63 #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
64 #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
67 /*-****************************************
69 ******************************************/
71 const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
73 ERR_STATIC const char* ERR_getErrorName(size_t code)
75 return ERR_getErrorString(ERR_getErrorCode(code));
79 * Ignore: this is an internal helper.
81 * This is a helper function to help force C99-correctness during compilation.
82 * Under strict compilation modes, variadic macro arguments can't be empty.
83 * However, variadic function arguments can be. Using a function therefore lets
84 * us statically check that at least one (string) argument was passed,
85 * independent of the compilation flags.
87 static INLINE_KEYWORD UNUSED_ATTR
88 void _force_has_format_string(const char *format, ...) {
93 * Ignore: this is an internal helper.
95 * We want to force this function invocation to be syntactically correct, but
96 * we don't want to force runtime evaluation of its arguments.
98 #define _FORCE_HAS_FORMAT_STRING(...) \
100 _force_has_format_string(__VA_ARGS__); \
103 #define ERR_QUOTE(str) #str
106 * Return the specified error if the condition evaluates to true.
108 * In debug modes, prints additional information.
109 * In order to do that (particularly, printing the conditional that failed),
110 * this can't just wrap RETURN_ERROR().
112 #define RETURN_ERROR_IF(cond, err, ...) \
114 RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \
115 __FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \
116 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
117 RAWLOG(3, ": " __VA_ARGS__); \
123 * Unconditionally return the specified error.
125 * In debug modes, prints additional information.
127 #define RETURN_ERROR(err, ...) \
129 RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \
130 __FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \
131 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
132 RAWLOG(3, ": " __VA_ARGS__); \
138 * If the provided expression evaluates to an error code, returns that error code.
140 * In debug modes, prints additional information.
142 #define FORWARD_IF_ERROR(err, ...) \
144 size_t const err_code = (err); \
145 if (ERR_isError(err_code)) { \
146 RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \
147 __FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \
148 _FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \
149 RAWLOG(3, ": " __VA_ARGS__); \
155 #if defined (__cplusplus)
159 #endif /* ERROR_H_MODULE */