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