YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
yac_assert.h
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef YAC_ASSERT_H
6#define YAC_ASSERT_H
7
8#include <stdio.h>
9
10// YAC PUBLIC HEADER START
11
12void yac_abort_message(char const*, char const*, int);
13
14#define die(msg) \
15 yac_abort_message((msg), __FILE__, __LINE__)
16
17// if YAC is built for code coverage test
18#if defined(YAC_CODE_COVERAGE_TEST)
19
20// to avoid reduction of code coverage due to error handling code not being
21// executed, redefine respective macros
22#define YAC_ASSERT(exp, msg) {(void)(exp);}
23#define YAC_ASSERT_FUNC(exp, func, msg) {(void)(exp);(void)(func);}
24#define YAC_ASSERT_F(exp, format, ...) {(void)(exp);}
25#define YAC_ASSERT_F_FUNC(exp, func, format, ...) {(void)(exp);(void)(func);}
26#define YAC_UNREACHABLE_DEFAULT(msg) default: {(void)(msg);}
27#define YAC_UNREACHABLE_DEFAULT_F(format, ...) default: {(void)(format);}
28#define YAC_UNREACHABLE_DEFAULT_F_FUNC(func, format, ...) \
29 default: {(void)(func);(void)(format);}
30
31#else // defined(YAC_CODE_COVERAGE_TEST)
32
33#define YAC_ASSERT(exp, msg) \
34 YAC_ASSERT_F((exp), "%s", (msg))
35
36#define YAC_ASSERT_FUNC(exp, func, msg) \
37 YAC_ASSERT_F_FUNC((exp), (func), "%s", (msg))
38
39#define YAC_ASSERT_F(exp, format, ...) \
40 YAC_ASSERT_F_FUNC((exp), __func__, format, ##__VA_ARGS__)
41
42#define YAC_ASSERT_F_FUNC(exp, func, format, ...) \
43 { \
44 if(!((exp))) { \
45 char msg_buffer[1024]; \
46 int ret = snprintf( \
47 msg_buffer, sizeof(msg_buffer), \
48 "ERROR(%s): " format, (func), ##__VA_ARGS__); \
49 if ((ret >= 0) && ((size_t)ret < sizeof(msg_buffer))) \
50 die(msg_buffer); \
51 else \
52 die("an error occurred, but error message could not be generated"); \
53 } \
54 }
55
56#define YAC_UNREACHABLE_DEFAULT(msg) \
57 default: \
58 { \
59 YAC_UNREACHABLE(msg); \
60 __attribute__((fallthrough)); \
61 }
62
63#define YAC_UNREACHABLE_DEFAULT_F_FUNC(func, format, ...) \
64 default: \
65 { \
66 char msg_buffer[1024]; \
67 int ret = snprintf( \
68 msg_buffer, sizeof(msg_buffer), \
69 "ERROR(%s): " format, (func), ##__VA_ARGS__); \
70 if ((ret >= 0) && ((size_t)ret < sizeof(msg_buffer))) \
71 die(msg_buffer); \
72 else \
73 die("an error occurred, but error message could not be generated"); \
74 __attribute__((fallthrough)); \
75 }
76
77#define YAC_UNREACHABLE_DEFAULT_F(format, ...) \
78 YAC_UNREACHABLE_DEFAULT_F_FUNC(__func__, format, ##__VA_ARGS__)
79
80#endif // defined(YAC_CODE_COVERAGE_TEST)
81
82#define YAC_UNREACHABLE(msg) \
83 YAC_ASSERT(0, (msg));
84
85#define YAC_UNREACHABLE_F(format, ...) \
86 YAC_ASSERT_F(0, format, ##__VA_ARGS__)
87
88// YAC PUBLIC HEADER STOP
89
90#endif // YAC_ASSERT_H
91
void yac_abort_message(char const *, char const *, int)