Path: blob/master/Utilities/cmzstd/lib/common/error_private.h
5000 views
/*1* Copyright (c) Meta Platforms, Inc. and affiliates.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910/* Note : this module is expected to remain private, do not expose it */1112#ifndef ERROR_H_MODULE13#define ERROR_H_MODULE1415/* ****************************************16* Dependencies17******************************************/18#include "../zstd_errors.h" /* enum list */19#include "compiler.h"20#include "debug.h"21#include "zstd_deps.h" /* size_t */2223/* ****************************************24* Compiler-specific25******************************************/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 inline30#elif defined(_MSC_VER)31# define ERR_STATIC static __inline32#else33# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */34#endif353637/*-****************************************38* Customization (error_public.h)39******************************************/40typedef ZSTD_ErrorCode ERR_enum;41#define PREFIX(name) ZSTD_error_##name424344/*-****************************************45* Error codes handling46******************************************/47#undef ERROR /* already defined on Visual Studio */48#define ERROR(name) ZSTD_ERROR(name)49#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))5051ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }5253ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }5455/* check and forward error code */56#define CHECK_V_F(e, f) \57size_t const e = f; \58do { \59if (ERR_isError(e)) \60return e; \61} while (0)62#define CHECK_F(f) do { CHECK_V_F(_var_err__, f); } while (0)636465/*-****************************************66* Error Strings67******************************************/6869const char* ERR_getErrorString(ERR_enum code); /* error_private.c */7071ERR_STATIC const char* ERR_getErrorName(size_t code)72{73return ERR_getErrorString(ERR_getErrorCode(code));74}7576/**77* Ignore: this is an internal helper.78*79* This is a helper function to help force C99-correctness during compilation.80* Under strict compilation modes, variadic macro arguments can't be empty.81* However, variadic function arguments can be. Using a function therefore lets82* us statically check that at least one (string) argument was passed,83* independent of the compilation flags.84*/85static INLINE_KEYWORD UNUSED_ATTR86void _force_has_format_string(const char *format, ...) {87(void)format;88}8990/**91* Ignore: this is an internal helper.92*93* We want to force this function invocation to be syntactically correct, but94* we don't want to force runtime evaluation of its arguments.95*/96#define _FORCE_HAS_FORMAT_STRING(...) \97do { \98if (0) { \99_force_has_format_string(__VA_ARGS__); \100} \101} while (0)102103#define ERR_QUOTE(str) #str104105/**106* Return the specified error if the condition evaluates to true.107*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().111*/112#define RETURN_ERROR_IF(cond, err, ...) \113do { \114if (cond) { \115RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \116__FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \117_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \118RAWLOG(3, ": " __VA_ARGS__); \119RAWLOG(3, "\n"); \120return ERROR(err); \121} \122} while (0)123124/**125* Unconditionally return the specified error.126*127* In debug modes, prints additional information.128*/129#define RETURN_ERROR(err, ...) \130do { \131RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \132__FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \133_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \134RAWLOG(3, ": " __VA_ARGS__); \135RAWLOG(3, "\n"); \136return ERROR(err); \137} while(0)138139/**140* If the provided expression evaluates to an error code, returns that error code.141*142* In debug modes, prints additional information.143*/144#define FORWARD_IF_ERROR(err, ...) \145do { \146size_t const err_code = (err); \147if (ERR_isError(err_code)) { \148RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \149__FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \150_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \151RAWLOG(3, ": " __VA_ARGS__); \152RAWLOG(3, "\n"); \153return err_code; \154} \155} while(0)156157#endif /* ERROR_H_MODULE */158159160