-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallofmacros.h
178 lines (150 loc) · 4.84 KB
/
wallofmacros.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#pragma once
#ifndef DBJ_WALL_OF_MACROS_INCLUDED
#define DBJ_WALL_OF_MACROS_INCLUDED
/* (c) 2019-2021 by dbj.org -- https://dbj.org/license_dbj/ */
#ifdef __clang__
#pragma clang system_header
#endif
// thus this is active only on not msvc C builds aka clang-cl
#undef DBJ_UNUSED_F
#if defined(__clang__) || defined(__GNUC__)
#define DBJ_UNUSED_F __attribute__((unused))
#endif
// avoid strdup/strndup garden path
// https://stackoverflow.com/questions/46013382/c-strndup-implicit-declaration?
#ifdef __STDC_ALLOC_LIB__
#define __STDC_WANT_LIB_EXT2__ 1
#else // ! __STDC_ALLOC_LIB__
#define _POSIX_C_SOURCE 200809L
#endif // ! __STDC_ALLOC_LIB__
#include <crtdbg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "printing_macros.h"
/*-------------------------------------------------------------------------------*/
#if defined(__clang__)
#define DBJ_PURE_FUNCTION __attribute__((const))
#define DBJ_CTOR_FUNCTION __attribute__((constructor))
#define DBJ_DTOR_FUNCTION __attribute__((destructor))
#define DBJ_NRET_FUNCTION __attribute__((noreturn))
#else
#define DBJ_PURE_FUNCTION
#define DBJ_CTOR_FUNCTION
#define DBJ_DTOR_FUNCTION
#define DBJ_NRET_FUNCTION
#endif
// Here's a better C version (from Google's Chromium project):
#undef DBJ_COUNT_OF
#define DBJ_COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
/// https://stackoverflow.com/a/29253284/10870835
// NDEBUG *is* standard macro and has to exist.
// it is also crucial to assert.h
#if ! defined (_DEBUG)
#if ! defined (NDEBUG)
#define NDEBUG
#endif
#endif
#undef DBJ_RELEASE_BUILD
#ifdef NDEBUG
#define DBJ_RELEASE_BUILD
#endif
/// -------------------------------------------------------------------------------
#undef DBJ_PERROR
#ifndef NDEBUG
#define DBJ_PERROR (perror(__FILE__ " # " _CRT_STRINGIZE(__LINE__)))
#else
#define DBJ_PERROR (perror(" "))
#endif // NDEBUG
#undef DBJ_FERROR
#ifdef _DEBUG
#define DBJ_FERROR( FP_) \
do { \
if (ferror(FP_) != 0) {\
DBJ_PERROR ;\
clearerr_s(FP_);\
} \
} while(0)
#else
#define DBJ_FERROR( FP_ )
#endif // _DEBUG
/// -------------------------------------------------------------------------------
/// stolen from vcruntime.h
#define _DBJ_STRINGIZE_(x) #x
#define _DBJ_STRINGIZE(x) _DBJ_STRINGIZE_(x)
#define _DBJ_WIDE_(s) L##s
#define _DBJ_WIDE(s) _DBJ_WIDE_(s)
#define _DBJ_CONCATENATE_(a, b) a##b
#define _DBJ_CONCATENATE(a, b) _DBJ_CONCATENATE_(a, b)
#define _DBJ_EXPAND_(s) s
#define _DBJ_EXPAND(s) _DBJ_EXPAND_(s)
#ifdef _MSVC_LANG
// https://developercommunity.visualstudio.com/content/problem/195665/-line-cannot-be-used-as-an-argument-for-constexpr.html
#define DBJ_CONSTEXPR_LINE long(_DBJ_CONCATENATE(__LINE__, U))
#else
#define DBJ_CONSTEXPR_LINE __LINE__
#endif
///-----------------------------------------------------------------------------------------
#ifdef _KERNEL_MODE
#define DBJ_NONPAGESECTION __declspec(code_seg("$dbj_fwk_kerneltext$"))
#else
#define DBJ_NONPAGESECTION
#endif // _KERNEL_MODE
// -----------------------------------------------------------------------------
#undef DBJ_ASSERT
#ifdef _DEBUG
#define DBJ_ASSERT _ASSERTE
#else
#define DBJ_ASSERT(X_) ((void)(X_))
#endif
//
// CAUTION! DBJ_VERIFY affects release builds too
// _ASSERT_AND_INVOKE_WATSON asserts in debug builds
// in release builds it invokes watson
#undef DBJ_VERIFY
#define DBJ_VERIFY(x) _ASSERT_AND_INVOKE_WATSON(x)
/*
we use the macros bellow to create ever needed location info always
associated with the offending expression
timestamp included
*/
#undef DBJ_FILE_LINE
#define DBJ_FILE_LINE __FILE__ "(" _CRT_STRINGIZE(__LINE__) ")"
#undef DBJ_FILE_LINE_TSTAMP
#define DBJ_FILE_LINE_TSTAMP __FILE__ "(" _CRT_STRINGIZE(__LINE__) ")[" __TIMESTAMP__ "] "
#undef DBJ_FLT_PROMPT
#define DBJ_FLT_PROMPT(x) DBJ_FILE_LINE_TSTAMP _CRT_STRINGIZE(x)
/* will not compile if MSG_ is not string literal */
#undef DBJ_ERR_PROMPT
#define DBJ_ERR_PROMPT(MSG_) DBJ_FILE_LINE_TSTAMP MSG_
// -------------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
// -----------------------------------------------------------------------------
/*
dbj_terror == terminating error
NOTE: std::exit *is* different to C API exit()
NOTE: all the bets are of so no point of using some logging
*/
inline void dbj_terror(const char* msg_, const char* file_, const int line_)
{
/// DBJ_ASSERT(msg_ && file_ && line_);
/// all the bets are of so no point of using some logging
perror("\n\n" DBJ_ERR_PROMPT("\n\nTerminating error!"));
exit(EXIT_FAILURE);
}
/*
2020 SEP 03 -- it turns out (again) we need to initialize WIN10 terminal
to show us VT100 colours
*/
inline void dbj_win_vt100_initor_() {
// and ... this is it ... awfull really ;)
// ps: make sure it is not empty string!
system(" ");
}
#ifdef __cplusplus
} // extern "C"
#endif
#endif // DBJ_WALL_OF_MACROS_INCLUDED