YAC 3.12.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// YAC PUBLIC HEADER START
9
10void yac_abort_message(char const*, char const*, int);
11
12#define die(msg) \
13 yac_abort_message((msg), __FILE__, __LINE__)
14
15#ifndef YAC_CODE_COVERAGE_TEST
16#define YAC_ASSERT(exp, msg) \
17 {if(!((exp))) yac_abort_message(((msg)), __FILE__, __LINE__);}
18
19#define YAC_ASSERT_F(exp, format, ...) \
20 { \
21 if(!((exp))) { \
22 char msg_buffer[1024]; \
23 int ret = snprintf( \
24 msg_buffer, sizeof(msg_buffer), ((format)), __VA_ARGS__); \
25 if ((ret >= 0) && ((size_t)ret < sizeof(msg_buffer))) \
26 yac_abort_message(((msg_buffer)), __FILE__, __LINE__); \
27 else \
28 yac_abort_message( \
29 "an error occured, but error message could not be " \
30 "generated", __FILE__, __LINE__); \
31 } \
32 }
33
34#define YAC_UNREACHABLE_DEFAULT(msg) \
35 default: \
36 { \
37 yac_abort_message(((msg)), __FILE__, __LINE__); \
38 __attribute__((fallthrough)); \
39 }
40
41#define YAC_UNREACHABLE_DEFAULT_F(format, ...) \
42 default: \
43 { \
44 char msg_buffer[1024]; \
45 int ret = snprintf( \
46 msg_buffer, sizeof(msg_buffer), ((format)), __VA_ARGS__); \
47 if ((ret >= 0) && ((size_t)ret < sizeof(msg_buffer))) \
48 yac_abort_message(((msg_buffer)), __FILE__, __LINE__); \
49 else \
50 yac_abort_message( \
51 "an error occured, but error message could not be " \
52 "generated", __FILE__, __LINE__); \
53 __attribute__((fallthrough)); \
54 }
55
56#else
57#define YAC_ASSERT(exp, msg) {(void)(exp);}
58#define YAC_ASSERT_F(exp, format, ...) {(void)(exp);}
59#define YAC_UNREACHABLE_DEFAULT(msg) {(void)(exp);}
60#define YAC_UNREACHABLE_DEFAULT_F(format, ...) {(void)(exp);}
61#endif
62
63#define YAC_UNREACHABLE(msg) \
64 YAC_ASSERT(0, (msg));
65
66#define YAC_UNREACHABLE_F(format, ...) \
67 YAC_ASSERT_F(0, (format), __VA_ARGS__)
68
69// YAC PUBLIC HEADER STOP
70
71#endif // YAC_ASSERT_H
72
void yac_abort_message(char const *, char const *, int)