Debian Codegraph @

common/include/dxxerror.h

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Header for error handling/printing/exiting code
23 *
24 */
25
26 #pragma once
27
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdio>
31 #include <span>
32 #include "dsx-ns.h"
33 #include "fmtcheck.h"
34
35 namespace dcx {
36
37 dxx_compiler_attribute_cold
38 void Warning_puts(const char *str) dxx_compiler_attribute_nonnull();
39 void Warning(const char *fmt) = delete;
40 dxx_compiler_attribute_cold
41 void Warning(const char *fmt,...) dxx_compiler_attribute_format_printf(1, 2); //print out warning message to user
42 #if DXX_USE_EDITOR
43 void set_warn_func(void (*f)(std::span<const char> s)); //specifies the function to call with warning messages
44 #if !(defined(WIN32) || defined(__APPLE__) || defined(__MACH__))
45 void clear_warn_func();//say this function no longer valid
46 #endif
47 #endif
48 [[noreturn]]
49 dxx_compiler_attribute_nonnull()
50 void Error_puts(const char *file, unsigned line, const char *func, const char *str);
51 #define Error_puts(F) Error_puts(__FILE__, __LINE__, __func__, F)
52 [[noreturn]]
53 dxx_compiler_attribute_format_printf(4, 5)
54 dxx_compiler_attribute_nonnull()
55 void Error(const char *file, unsigned line, const char *func, const char *fmt,...); //exit with error code=1, print message
56 #define Error(F,...) dxx_call_printf_checked(Error,(Error_puts),(__FILE__, __LINE__, __func__),(F),##__VA_ARGS__)
57
58 [[noreturn]]
59 void UserError_puts(const char *str, std::size_t);
60
61 template <std::size_t len>
62 [[noreturn]]
63 static inline void UserError_puts(const char (&str)[len])
64 {
65 UserError_puts(str, len - 1);
66 }
67
68 void UserError(const char *) = delete;
69 [[noreturn]]
70 void UserError(const char *fmt, ...) dxx_compiler_attribute_format_printf(1, 2);
71
72 }
73 #define DXX_STRINGIZE_FL2(F,L,S) F ":" #L ": " S
74 #define DXX_STRINGIZE_FL(F,L,S) DXX_STRINGIZE_FL2(F, L, S)
75 #define UserError(F,...) UserError(DXX_STRINGIZE_FL(__FILE__, __LINE__, F),##__VA_ARGS__)
76 #define Assert assert
77
78 #define LevelErrorV(V,F,...) con_printf(V, DXX_STRINGIZE_FL(__FILE__, __LINE__, F " Please report this to the level author, not to the Rebirth maintainers."), ##__VA_ARGS__)
79 #define LevelError(F,...) LevelErrorV(CON_URGENT, F, ##__VA_ARGS__)
80
81 /* Compatibility with x86-specific name */
82 #define Int3() d_debugbreak()
83
84 #ifndef NDEBUG //macros for debugging
85 # define DXX_ENABLE_DEBUGBREAK_TRAP
86 #endif
87
88 /* Allow macro override */
89
90 #if defined(DXX_ENABLE_DEBUGBREAK_TRAP) && !defined(DXX_DEBUGBREAK_TRAP)
91
92 #if defined __clang__
93 /* Must be first, since clang also defines __GNUC__ */
94 # define DXX_DEBUGBREAK_TRAP() __builtin_debugtrap()
95 #elif defined __GNUC__
96 # if defined(__i386__) || defined(__amd64__)
97 # define DXX_DEBUGBREAK_TRAP() __asm__ __volatile__("int3" ::: "cc", "memory")
98 # endif
99 #elif defined _MSC_VER
100 # define DXX_DEBUGBREAK_TRAP() __debugbreak()
101 #endif
102
103 #ifndef DXX_DEBUGBREAK_TRAP
104 # if defined __unix__
105 /* for raise */
106 # include <signal.h>
107 # define DXX_DEBUGBREAK_TRAP() raise(SIGTRAP)
108 # elif defined __GNUC__
109 /* May terminate execution */
110 # define DXX_DEBUGBREAK_TRAP() __builtin_trap()
111 # endif
112 #endif
113
114 #endif
115
116 namespace dcx {
117
118 // Encourage optimizer to treat d_debugbreak paths as unlikely
119 dxx_compiler_attribute_cold
120 // Requested by btb to force Xcode to stay in the calling function
121 dxx_compiler_attribute_always_inline()
122 static inline void d_debugbreak()
123 {
124 /* Allow explicit activation in NDEBUG builds */
125 #ifdef DXX_DEBUGBREAK_TRAP
126 DXX_DEBUGBREAK_TRAP();
127 #endif
128
129 }
130
131 }