Blame source/uds/permassert.h

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/uds-releases/jasper/src/uds/permassert.h#1 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#ifndef PERMASSERT_H
Packit Service 310c69
#define PERMASSERT_H
Packit Service 310c69
Packit Service 310c69
#include "compiler.h"
Packit Service 310c69
#include "errors.h"
Packit Service 310c69
#include "uds-error.h"
Packit Service 310c69
Packit Service 310c69
#define STRINGIFY(X) #X
Packit Service 310c69
#define STRINGIFY_VALUE(X) STRINGIFY(X)
Packit Service 310c69
Packit Service 310c69
/*
Packit Service 310c69
 * A hack to apply the "warn if unused" attribute to an integral expression.
Packit Service 310c69
 *
Packit Service 310c69
 * Since GCC doesn't propagate the warn_unused_result attribute to
Packit Service 310c69
 * conditional expressions incorporating calls to functions with that
Packit Service 310c69
 * attribute, this function can be used to wrap such an expression.
Packit Service 310c69
 * With optimization enabled, this function contributes no additional
Packit Service 310c69
 * instructions, but the warn_unused_result attribute still applies to
Packit Service 310c69
 * the code calling it.
Packit Service 310c69
 *
Packit Service 310c69
 * @param value  The value to return
Packit Service 310c69
 *
Packit Service 310c69
 * @return       The supplied value
Packit Service 310c69
 */
Packit Service 310c69
__attribute__((warn_unused_result))
Packit Service 310c69
static INLINE int mustUse(int value)
Packit Service 310c69
{
Packit Service 310c69
  return value;
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/*
Packit Service 310c69
 * A replacement for assert() from assert.h.
Packit Service 310c69
 *
Packit Service 310c69
 * @param expr      The boolean expression being asserted
Packit Service 310c69
 * @param code      The error code to return on non-fatal assertion
Packit Service 310c69
 *                  failure
Packit Service 310c69
 * @param format    A printf() style format for the message to log on
Packit Service 310c69
 *                  assertion failure
Packit Service 310c69
 * @param arguments Any additional arguments required by the format
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS If expr is true, code if expr is false and
Packit Service 310c69
 *         exitOnAssertionFailure is false. When exitOnAssertionFailure
Packit Service 310c69
 *         is true and expr is false, the program will exit from within
Packit Service 310c69
 *         this macro.
Packit Service 310c69
 */
Packit Service 310c69
#define ASSERT_WITH_ERROR_CODE(expr, code, ...)                         \
Packit Service 310c69
  mustUse(__builtin_expect(!!(expr), 1)                                 \
Packit Service 310c69
          ? UDS_SUCCESS                                                 \
Packit Service 310c69
          : assertionFailed(STRINGIFY(expr), code, __FILE__, __LINE__,  \
Packit Service 310c69
                            __VA_ARGS__))
Packit Service 310c69
Packit Service 310c69
/*
Packit Service 310c69
 * A replacement for assert() from assert.h.
Packit Service 310c69
 *
Packit Service 310c69
 * @param expr      The boolean expression being asserted
Packit Service 310c69
 * @param format    A printf() style format for the message to log on
Packit Service 310c69
 *                  assertion failure
Packit Service 310c69
 * @param arguments Any additional arguments required by the format
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS If expr is true, UDS_ASSERTION_FAILED if expr is
Packit Service 310c69
 *         false and exitOnAssertionFailure is false. When
Packit Service 310c69
 *         exitOnAssertionFailure is true and expr is false, the
Packit Service 310c69
 *         program will exit from within this macro.
Packit Service 310c69
 */
Packit Service 310c69
#define ASSERT(expr, ...)                                         \
Packit Service 310c69
  ASSERT_WITH_ERROR_CODE(expr, UDS_ASSERTION_FAILED, __VA_ARGS__)
Packit Service 310c69
Packit Service 310c69
/*
Packit Service 310c69
 * A replacement for assert() which logs on failure, but does not return an
Packit Service 310c69
 * error code. This should be used sparingly. If the expression is false and
Packit Service 310c69
 * exitOnAssertionFailure is true, the program will exit from within this macro.
Packit Service 310c69
 *
Packit Service 310c69
 * @param expr      The boolean expression being asserted
Packit Service 310c69
 * @param format    A printf() syle format for the message to log on
Packit Service 310c69
 *                  assertion failure
Packit Service 310c69
 * @param arguments Any additional arguments required by the format
Packit Service 310c69
 */
Packit Service 310c69
#define ASSERT_LOG_ONLY(expr, ...)                                           \
Packit Service 310c69
  (__builtin_expect(!!(expr), 1)                                             \
Packit Service 310c69
   ? UDS_SUCCESS                                                             \
Packit Service 310c69
   : assertionFailedLogOnly(STRINGIFY(expr), __FILE__, __LINE__, __VA_ARGS__))
Packit Service 310c69
Packit Service 310c69
/*
Packit Service 310c69
 * This macro is a convenient wrapper for ASSERT(false, ...).
Packit Service 310c69
 */
Packit Service 310c69
#define ASSERT_FALSE(...) \
Packit Service 310c69
  ASSERT(false, __VA_ARGS__)
Packit Service 310c69
Packit Service 310c69
#define STATIC_ASSERT(expr) \
Packit Service 310c69
  do {                      \
Packit Service 310c69
    switch (0) {            \
Packit Service 310c69
    case 0:                 \
Packit Service 310c69
    case expr:              \
Packit Service 310c69
      ;                     \
Packit Service 310c69
    default:                \
Packit Service 310c69
      ;                     \
Packit Service 310c69
    }                       \
Packit Service 310c69
  } while(0)
Packit Service 310c69
Packit Service 310c69
#define STATIC_ASSERT_SIZEOF(type, expectedSize) \
Packit Service 310c69
  STATIC_ASSERT(sizeof(type) == (expectedSize))
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Set whether or not to exit on an assertion failure.
Packit Service 310c69
 *
Packit Service 310c69
 * @param shouldExit If true assertion failures will cause
Packit Service 310c69
 *                   the program to exit
Packit Service 310c69
 *
Packit Service 310c69
 * @return The previous setting
Packit Service 310c69
 **/
Packit Service 310c69
bool setExitOnAssertionFailure(bool shouldExit);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log an assertion failure.
Packit Service 310c69
 *
Packit Service 310c69
 * @param expressionString The assertion
Packit Service 310c69
 * @param errorCode        The error code to return
Packit Service 310c69
 * @param fileName         The file in which the assertion appears
Packit Service 310c69
 * @param lineNumber       The line number on which the assertion
Packit Service 310c69
 *                         appears
Packit Service 310c69
 * @param format           A printf() style format describing the
Packit Service 310c69
 *                         assertion
Packit Service 310c69
 *
Packit Service 310c69
 * @return The supplied errorCode unless exitOnAssertionFailure is
Packit Service 310c69
 *         true, in which case the process will be aborted
Packit Service 310c69
 **/
Packit Service 310c69
int assertionFailed(const char *expressionString,
Packit Service 310c69
                    int         errorCode,
Packit Service 310c69
                    const char *fileName,
Packit Service 310c69
                    int         lineNumber,
Packit Service 310c69
                    const char *format,
Packit Service 310c69
                    ...)
Packit Service 310c69
  __attribute__((format(printf, 5, 6), warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log an assertion failure. This function is different from
Packit Service 310c69
 * assertionFailed() in that its return value may be ignored, and so should
Packit Service 310c69
 * only be used in cases where the return value will be ignored.
Packit Service 310c69
 *
Packit Service 310c69
 * @param expressionString The assertion
Packit Service 310c69
 * @param fileName         The file in which the assertion appears
Packit Service 310c69
 * @param lineNumber       The line number on which the assertion
Packit Service 310c69
 *                         appears
Packit Service 310c69
 * @param format           A printf() style format describing the
Packit Service 310c69
 *                         assertion
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_ASSERTION_FAILED unless exitOnAssertionFailure is
Packit Service 310c69
 *         true, in which case the process will be aborted
Packit Service 310c69
 **/
Packit Service 310c69
int assertionFailedLogOnly(const char *expressionString,
Packit Service 310c69
                           const char *fileName,
Packit Service 310c69
                           int         lineNumber,
Packit Service 310c69
                           const char *format,
Packit Service 310c69
                           ...)
Packit Service 310c69
  __attribute__((format(printf, 4, 5)));
Packit Service 310c69
Packit Service 310c69
#endif /* PERMASSERT_H */