Path: blob/master/Utilities/cmzstd/lib/common/error_private.h
3158 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#if defined (__cplusplus)16extern "C" {17#endif181920/* ****************************************21* Dependencies22******************************************/23#include "../zstd_errors.h" /* enum list */24#include "compiler.h"25#include "debug.h"26#include "zstd_deps.h" /* size_t */272829/* ****************************************30* Compiler-specific31******************************************/32#if defined(__GNUC__)33# define ERR_STATIC static __attribute__((unused))34#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)35# define ERR_STATIC static inline36#elif defined(_MSC_VER)37# define ERR_STATIC static __inline38#else39# define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */40#endif414243/*-****************************************44* Customization (error_public.h)45******************************************/46typedef ZSTD_ErrorCode ERR_enum;47#define PREFIX(name) ZSTD_error_##name484950/*-****************************************51* Error codes handling52******************************************/53#undef ERROR /* already defined on Visual Studio */54#define ERROR(name) ZSTD_ERROR(name)55#define ZSTD_ERROR(name) ((size_t)-PREFIX(name))5657ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }5859ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }6061/* check and forward error code */62#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e63#define CHECK_F(f) { CHECK_V_F(_var_err__, f); }646566/*-****************************************67* Error Strings68******************************************/6970const char* ERR_getErrorString(ERR_enum code); /* error_private.c */7172ERR_STATIC const char* ERR_getErrorName(size_t code)73{74return ERR_getErrorString(ERR_getErrorCode(code));75}7677/**78* Ignore: this is an internal helper.79*80* This is a helper function to help force C99-correctness during compilation.81* Under strict compilation modes, variadic macro arguments can't be empty.82* However, variadic function arguments can be. Using a function therefore lets83* us statically check that at least one (string) argument was passed,84* independent of the compilation flags.85*/86static INLINE_KEYWORD UNUSED_ATTR87void _force_has_format_string(const char *format, ...) {88(void)format;89}9091/**92* Ignore: this is an internal helper.93*94* We want to force this function invocation to be syntactically correct, but95* we don't want to force runtime evaluation of its arguments.96*/97#define _FORCE_HAS_FORMAT_STRING(...) \98if (0) { \99_force_has_format_string(__VA_ARGS__); \100}101102#define ERR_QUOTE(str) #str103104/**105* Return the specified error if the condition evaluates to true.106*107* In debug modes, prints additional information.108* In order to do that (particularly, printing the conditional that failed),109* this can't just wrap RETURN_ERROR().110*/111#define RETURN_ERROR_IF(cond, err, ...) \112if (cond) { \113RAWLOG(3, "%s:%d: ERROR!: check %s failed, returning %s", \114__FILE__, __LINE__, ERR_QUOTE(cond), ERR_QUOTE(ERROR(err))); \115_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \116RAWLOG(3, ": " __VA_ARGS__); \117RAWLOG(3, "\n"); \118return ERROR(err); \119}120121/**122* Unconditionally return the specified error.123*124* In debug modes, prints additional information.125*/126#define RETURN_ERROR(err, ...) \127do { \128RAWLOG(3, "%s:%d: ERROR!: unconditional check failed, returning %s", \129__FILE__, __LINE__, ERR_QUOTE(ERROR(err))); \130_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \131RAWLOG(3, ": " __VA_ARGS__); \132RAWLOG(3, "\n"); \133return ERROR(err); \134} while(0);135136/**137* If the provided expression evaluates to an error code, returns that error code.138*139* In debug modes, prints additional information.140*/141#define FORWARD_IF_ERROR(err, ...) \142do { \143size_t const err_code = (err); \144if (ERR_isError(err_code)) { \145RAWLOG(3, "%s:%d: ERROR!: forwarding error in %s: %s", \146__FILE__, __LINE__, ERR_QUOTE(err), ERR_getErrorName(err_code)); \147_FORCE_HAS_FORMAT_STRING(__VA_ARGS__); \148RAWLOG(3, ": " __VA_ARGS__); \149RAWLOG(3, "\n"); \150return err_code; \151} \152} while(0);153154#if defined (__cplusplus)155}156#endif157158#endif /* ERROR_H_MODULE */159160161