-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-assert.h
executable file
·386 lines (316 loc) · 11.6 KB
/
util-assert.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#ifndef CROSSPLATFORM_UTILITY_CODE_CASSERT_WRAPPER
#define CROSSPLATFORM_UTILITY_CODE_CASSERT_WRAPPER
//#define FLEX_DISABLE_ASSERT // <--- disabling should be done in a release build
// If NDEBUG is defined as a macro name at the point in the source code where <cassert> is included,
// then assert does nothing.
#ifdef NDEBUG
// Hard-enabling of assert() even if NDEBUG is defined
# define NDEBUG_WAS_SET_FASSERT_HEADER
# undef NDEBUG
#
# include <cassert>
#endif
#include <signal.h> // for raise
#include <stdio.h> // for stderr
#include <stdlib.h> // for getenv
#include <string.h>
#if defined( _WIN32 )
# include <Windows.h>
# include <assert.h>
#endif // #if defined(_WIN32)
#if defined( __APPLE__ )
# include <CoreFoundation/CoreFoundation.h>
# include <TargetConditionals.h>
# define _putenv putenv
#endif // defined(__APPLE__)
#if defined( __linux__ )
# define _putenv putenv
# include <unistd.h> // for isatty
#endif // defined(__linux__)
static inline void Suppress_All_Assertions()
{
// if asserts have been BUILD-TIME compiled away, then do NOT bother calling putenv
#ifndef FLEX_DISABLE_ASSERT
static const char* setting = "FLEX_SUPALL_ASRT=1";
// should return 0 for SUCCESS:
/*const int ret =*/_putenv( (char*) setting );
#endif // FLEX_DISABLE_ASSERT
}
static inline void UnSuppress_All_Assertions()
{
// if asserts have been BUILD-TIME compiled away, then do NOT bother calling putenv
#ifndef FLEX_DISABLE_ASSERT
static const char* negation = "FLEX_SUPALL_ASRT=";
// should return 0 for SUCCESS:
/*const int ret =*/_putenv( (char*) negation ); // on win32, this is enough to nullify the var.
# if !defined( _WIN32 )
/*const int ret =*/unsetenv( "FLEX_SUPALL_ASRT" ); // on posix, the above set it to "", and HERE we nullify it
# endif //#if ! defined(_WIN32)
#endif // FLEX_DISABLE_ASSERT
}
// break into the debugger
static inline void TrapDebug()
{
#if defined( _WIN32 )
// If just-in-time (JIT) debugging is working properly, then one might
// reasonbly choose to remove the 'IsDebugger' check and just always provoke
// the EXCEPTION_BREAKPOINT. When just-in-time debugging works properly,
// you would then get a pop-up offering to attach a debugger, despite you
// having launched the process outside of a debugger. If you trust
// just-in-time debugging, then remove the check. However, on Windows 10
// many people struggle to get just-in-time working reliably.
if( IsDebuggerPresent() )
{
// if this is called OUTSIDE of a debugger (and when JIT is
// uncooperative), then it will abort the program. When called in a
// debugger, it behaves just as though you had manually set a
// breakpoint.
__debugbreak();
}
#elif defined( __APPLE__ )
raise( SIGTRAP );
#elif defined( __linux__ )
raise( SIGTRAP );
#else
// FUTURE_PLATFORMS_TBD
#endif // Win/Apple
}
static inline void ShowOnStderr(
const char* title,
const char* message,
const char* filename,
const int line,
const char* funcname )
{
fprintf( stderr, "%s:\n", title );
fprintf( stderr, "%s\n", funcname );
fprintf( stderr, "%s:%d\n", filename, line );
fprintf( stderr, "%s\n", message );
}
static inline void OptionToContinue(
const char* title,
const char* message,
const char* filename,
const int line,
const char* funcname )
{
#if defined( _WIN32 )
// suppress warnings about unused parameters on win32:
(void) title;
(void) message;
(void) filename;
(void) line;
(void) funcname;
#elif defined( __APPLE__ ) && !( TARGET_OS_IPHONE )
ShowOnStderr( title, message, filename, line, funcname );
CFStringRef headerRef = CFStringCreateWithCString( NULL, title, kCFStringEncodingUTF8 );
CFStringRef messageRef = CFStringCreateWithCString( NULL, message, kCFStringEncodingUTF8 );
CFStringRef button1 = CFStringCreateWithCString( NULL, "Break", kCFStringEncodingUTF8 ); // defaultButtonTitle
CFStringRef button2 = CFStringCreateWithCString( NULL, "Continue", kCFStringEncodingUTF8 ); // alternateButtonTitle
CFOptionFlags response;
CFUserNotificationDisplayAlert( 0, // timeout. (apparently in seconds) The amount of time to wait for the user to dismiss
// the notification dialog before the dialog dismisses
// itself. Pass 0 to have the dialog never time out.
kCFUserNotificationCautionAlertLevel,
NULL, // iconURL
NULL, // soundURL
NULL, // localizationURL
headerRef,
messageRef,
button1, // defaultButtonTitle
button2, // alternateButtonTitle
NULL, // otherButtonTitle
&response );
CFRelease( headerRef );
CFRelease( messageRef );
CFRelease( button1 );
CFRelease( button2 );
if( response == kCFUserNotificationDefaultResponse )
{
// choice was "Break"
TrapDebug();
}
else if( response == kCFUserNotificationAlternateResponse )
{
// choice was "Continue"
// do nothing
}
else // if you add a 3rd button, this would be kCFUserNotificationOtherResponse
{
// either the notification timed out by itself (no user interaction),
// or else the user hit the ESCAPE key
fprintf( stderr, "ignoring opportunity to debug the FAIL (either due to inaction or ESC key)\n" );
}
#elif defined( __unix__ ) || TARGET_OS_IPHONE
ShowOnStderr( title, message, filename, line, funcname );
char buf[ 16 ];
bool retry = true;
while( retry )
{
retry = false;
fprintf( stderr, "[t] to trap/debug,\n"
"[x] to exit/abort,\n"
"[c] to continue,\n"
"[s] to suppress further assertions\n" );
if( isatty( 0 ) && isatty( 1 ) )
{
fgets( buf, 8, stdin );
}
else
{
strcpy( buf, "X\n" );
}
if( ( buf[ 0 ] == 'X' || buf[ 0 ] == 'x' ) && buf[ 1 ] == '\n' )
{
abort();
}
else if( ( buf[ 0 ] == 'T' || buf[ 0 ] == 't' ) && buf[ 1 ] == '\n' )
{
TrapDebug();
}
else if( ( buf[ 0 ] == 'C' || buf[ 0 ] == 'c' ) && buf[ 1 ] == '\n' )
{
// just let things proceed
}
else if( ( buf[ 0 ] == 'S' || buf[ 0 ] == 's' ) && buf[ 1 ] == '\n' )
{
Suppress_All_Assertions();
}
else
{
retry = true;
}
}
#else
// FUTURE_PLATFORMS_TBD
#endif // Win/Apple
}
#if defined( __APPLE__ ) || defined( __linux__ )
static inline void Flex_Asrt_Unix( const char* message,
const char* filename,
const int line,
const char* funcname )
{
if( getenv( "FLEX_SUPALL_ASRT" ) )
{
return;
}
OptionToContinue( "FASSERT",
message,
filename,
line,
funcname );
}
static inline void Flex_Fail_Unix( const char* message,
const char* filename,
const int line,
const char* funcname )
{
if( getenv( "FLEX_SUPALL_ASRT" ) )
{
return;
}
OptionToContinue( "FFAIL",
message,
filename,
line,
funcname );
}
#endif //#if defined(__APPLE__)
static inline bool GetEnv_WinOnly( const char* name )
{
#if !defined( _WIN32 )
// suppress warnings about unused parameters on mac:
(void) name;
return false;
#else
size_t converted = 0;
# ifdef _UNICODE
wchar_t wtext[ 500 ];
mbstowcs_s( &converted, wtext, 500, name, 480 );
LPCWSTR ptr = wtext;
# else
LPCSTR ptr = name;
# endif
bool rslt = false;
// get the size of the buffer
DWORD dwRet = ::GetEnvironmentVariable( ptr, NULL, 0 );
if( dwRet )
{
rslt = true;
}
return rslt;
#endif // #if defined(_WIN32)
}
/**
INTERESTING ITEM ABOUT BAD INTERACTIONS OF IF/ELSE
STRUCTURES AND PREPROCESSOR MACROS:
http://stackoverflow.com/questions/154136/do-while-and-if-else-statements-in-c-c-macros
*/
/*
If you have a Qt QString, then you need to pass it to FASSERT
by doing something like this:
QString message;
FASSERT( 1 == 2, message.toUtf8() );
If you are using std::string, then you will want to pass it in the following
way:
std::string message;
FASSERT( 1 == 2, message.c_str() );
*/
#ifdef FLEX_DISABLE_ASSERT
// define it as "nothing." they will be "compiled out"
# define FASSERT( cond, msg )
# define FFAIL( msg )
#else // the ELSE is when we _ENABLE_ our assertions
# if defined( _WIN32 )
// when assertions are enabled on Win:
# define FASSERT( cond, msg ) \
__pragma( warning( push ) ) \
__pragma( warning( disable : 4127 ) ) do \
{ \
if( !GetEnv_WinOnly( "FLEX_SUPALL_ASRT" ) ) \
{ \
if( !( cond ) ) \
{ \
ShowOnStderr( "FASSERT", msg, __FILE__, __LINE__, __func__ ); \
TrapDebug(); \
} \
assert( ( cond ) && ( msg ) ); \
} \
} \
while( 0 ) \
__pragma( warning( pop ) )
// when assertions are enabled on Win:
# define FFAIL( msg ) \
__pragma( warning( push ) ) \
__pragma( warning( disable : 4127 ) ) do \
{ \
if( !GetEnv_WinOnly( "FLEX_SUPALL_ASRT" ) ) \
{ \
ShowOnStderr( "FFAIL", msg, __FILE__, __LINE__, __func__ ); \
TrapDebug(); \
assert( !msg ); \
} \
} \
while( 0 ) \
__pragma( warning( pop ) )
# elif defined( __APPLE__ ) || defined( __linux__ )
// when assertions are enabled on Mac:
# define FASSERT( cond, msg ) \
if( ( cond ) ) \
{ \
} \
else \
Flex_Asrt_Unix( msg, __FILE__, __LINE__, __func__ )
// when assertions are enabled on Mac:
# define FFAIL( msg ) \
Flex_Fail_Unix( msg, __FILE__, __LINE__, __func__ )
# else
// FUTURE_PLATFORMS_TBD
# endif // Win/Apple
#endif // #ifdef FLEX_DISABLE_ASSERT
#ifdef NDEBUG_WAS_SET_FASSERT_HEADER
// Restoring NDEBUG if it was enabled originally
# define NDEBUG
#endif
#endif // CROSSPLATFORM_UTILITY_CODE_CASSERT_WRAPPER