libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
Macros
assert.h File Reference
#include <assert.h>
#include <stdlib.h>
#include "log.h"
#include "sysmacros.h"
Include dependency graph for assert.h:

Go to the source code of this file.

Macros

#define EXCEPTION_ASSERTION_FAILED   0x8000
 
#define LACF_CRASH()
 
#define VERIFY(x)   VERIFY_MSG(x, "%s", "")
 
#define VERIFY_MSG(x, fmt, ...)
 
#define VERIFY3_impl(x, op, y, type, fmt)
 
#define VERIFY3S(x, op, y)   VERIFY3_impl(x, op, y, long, "%lu")
 
#define VERIFY3U(x, op, y)    VERIFY3_impl(x, op, y, unsigned long long, "0x%llx")
 
#define VERIFY3F(x, op, y)   VERIFY3_impl(x, op, y, double, "%f")
 
#define VERIFY3P(x, op, y)   VERIFY3_impl(x, op, y, void *, "%p")
 
#define VERIFY0(x)   VERIFY3S((x), ==, 0)
 
#define VERIFY_FAIL()
 
#define ASSERT(x)   LACF_UNUSED(x)
 
#define ASSERT3S(x, op, y)   do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)
 
#define ASSERT3U(x, op, y)   do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)
 
#define ASSERT3F(x, op, y)   do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)
 
#define ASSERT3P(x, op, y)   do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)
 
#define ASSERT0(x)   LACF_UNUSED(x)
 
#define ASSERT_MSG(x, fmt, ...)   LACF_UNUSED(x)
 

Detailed Description

This is the master assertion checking machinery of libacfutils.

The macros in this module are designed to provide error checking and crash log generation. The majority of the time, you will be using the ASSERT* family of macros, to create assertion checks. If the condition in the macro argument fails, the check generates a crash, file + line number reference and backtrace, all of which will be logged. After this, the application exits. ASSERT macros are only compiled into your code if the DEBUG macro is defined during compilation. If you want to generate a an assertion check that is always compiled in, use the VERIFY* family of macros.

Definition in file assert.h.

Macro Definition Documentation

◆ ASSERT

#define ASSERT (   x)    LACF_UNUSED(x)

Same as VERIFY, but only active when compiling with DEBUG defined.

Definition at line 208 of file assert.h.

◆ ASSERT0

#define ASSERT0 (   x)    LACF_UNUSED(x)

Same as VERIFY0, but only active when compiling with DEBUG defined.

Definition at line 213 of file assert.h.

◆ ASSERT3F

#define ASSERT3F (   x,
  op,
 
)    do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)

Same as VERIFY3F, but only active when compiling with DEBUG defined.

Definition at line 211 of file assert.h.

◆ ASSERT3P

#define ASSERT3P (   x,
  op,
 
)    do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)

Same as VERIFY3P, but only active when compiling with DEBUG defined.

Definition at line 212 of file assert.h.

◆ ASSERT3S

#define ASSERT3S (   x,
  op,
 
)    do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)

Same as VERIFY3S, but only active when compiling with DEBUG defined.

Definition at line 209 of file assert.h.

◆ ASSERT3U

#define ASSERT3U (   x,
  op,
 
)    do { LACF_UNUSED(x); LACF_UNUSED(y); } while (0)

Same as VERIFY3U, but only active when compiling with DEBUG defined.

Definition at line 210 of file assert.h.

◆ ASSERT_MSG

#define ASSERT_MSG (   x,
  fmt,
  ... 
)    LACF_UNUSED(x)

Same as VERIFY_MSG, but only active when compiling with DEBUG defined.

Definition at line 214 of file assert.h.

◆ EXCEPTION_ASSERTION_FAILED

#define EXCEPTION_ASSERTION_FAILED   0x8000

Definition at line 56 of file assert.h.

◆ LACF_CRASH

#define LACF_CRASH ( )
Value:
do { \
RaiseException(EXCEPTION_ASSERTION_FAILED, \
EXCEPTION_NONCONTINUABLE, 0, NULL); \
/* Needed to avoid no-return-value warnings */ \
abort(); \
} while (0)

Definition at line 57 of file assert.h.

◆ VERIFY

#define VERIFY (   x)    VERIFY_MSG(x, "%s", "")

ASSERT() and VERIFY() are assertion test macros. If the condition expression provided as the argument to the macro evaluates as non-true, the program prints a debug message specifying exactly where and what condition was violated, a stack backtrace and a dumps core by calling LACF_CRASH() (which calls abort() on macOS/Linux and generates an assertion failure exception on Windows).

The difference between ASSERT and VERIFY is that ASSERT compiles to a no-op unless -DDEBUG is provided to the compiler. VERIFY always checks its condition and dumps if it is non-true.

Definition at line 78 of file assert.h.

◆ VERIFY0

#define VERIFY0 (   x)    VERIFY3S((x), ==, 0)

Similar to VERIFY3S, but only takes a single integer argument and checks that it is zero.

Definition at line 154 of file assert.h.

◆ VERIFY3_impl

#define VERIFY3_impl (   x,
  op,
  y,
  type,
  fmt 
)
Value:
do { \
type tmp_x = (type)(x); \
type tmp_y = (type)(y); \
if (COND_UNLIKELY(!(tmp_x op tmp_y))) { \
log_impl(log_basename(__FILE__), __LINE__, \
"assertion %s %s %s failed (" fmt " %s " \
fmt ")", #x, #op, #y, tmp_x, #op, tmp_y); \
LACF_CRASH(); \
} \
} while (0)
#define log_basename(f)
Definition log.h:89

Definition at line 100 of file assert.h.

◆ VERIFY3F

#define VERIFY3F (   x,
  op,
 
)    VERIFY3_impl(x, op, y, double, "%f")

Same as VERIFY3S, but operates on floating point and double values ("3F" = 3 arguments, Floating point).

Definition at line 144 of file assert.h.

◆ VERIFY3P

#define VERIFY3P (   x,
  op,
 
)    VERIFY3_impl(x, op, y, void *, "%p")

Same as VERIFY3S, but operates on pointer values ("3P" = 3 arguments, Pointer).

Definition at line 149 of file assert.h.

◆ VERIFY3S

#define VERIFY3S (   x,
  op,
 
)    VERIFY3_impl(x, op, y, long, "%lu")

Provides a more convenient macro for assertions checks of signed integer comparisons ("3S" = 3 arguments, Signed integer). The first and last argument are expected to be integer values, and the middle a comparison operator, such as == or >, placed between the two operands. If the comparison fails, this macro prints not only the condition that failed, but also what the numerical values of the first and last argument were, to aid in crash analysis. For example:

int foo = 100, bar = 50;
VERIFY3S(foo, <, bar);
#define VERIFY3S(x, op, y)
Definition assert.h:125

will print "assertion foo < bar failed (100 < 50)".

Definition at line 125 of file assert.h.

◆ VERIFY3U

#define VERIFY3U (   x,
  op,
 
)     VERIFY3_impl(x, op, y, unsigned long long, "0x%llx")

Same as VERIFY3S, but operates on unsigned integer values ("3U" = 3 arguments, Unsigned integer).

Definition at line 136 of file assert.h.

◆ VERIFY_FAIL

#define VERIFY_FAIL ( )
Value:
do { \
log_impl(log_basename(__FILE__), __LINE__, "Internal error"); \
LACF_CRASH(); \
} while (0)

Hard-crash generator. This always crashes if it is reached. Use this to mark invalid branches of conditional/case statements. This will generate a log message that says "Internal error".

Definition at line 160 of file assert.h.

◆ VERIFY_MSG

#define VERIFY_MSG (   x,
  fmt,
  ... 
)
Value:
do { \
if (COND_UNLIKELY(!(x))) { \
log_impl(log_basename(__FILE__), __LINE__, \
"assertion \"%s\" failed: " fmt, #x, __VA_ARGS__); \
LACF_CRASH(); \
} \
} while (0)

Same as the VERIFY() macro, but lets you pass a custom printf-like format string with arguments, to append to the message "assertion <condition> failed:". Use this if you need to provide more context why the assertion check failed.

Example usage of VERIFY_MSG():

VERIFY_MSG(is_valid(foo), "foo was set to %d", foo);
#define VERIFY_MSG(x, fmt,...)
Definition assert.h:91

Definition at line 91 of file assert.h.