Ada
2.9.2
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
common_defs.h
Go to the documentation of this file.
1
5
#ifndef ADA_COMMON_DEFS_H
6
#define ADA_COMMON_DEFS_H
7
8
#ifdef _MSC_VER
9
#define ADA_VISUAL_STUDIO 1
15
#ifdef __clang__
16
// clang under visual studio
17
#define ADA_CLANG_VISUAL_STUDIO 1
18
#else
19
// just regular visual studio (best guess)
20
#define ADA_REGULAR_VISUAL_STUDIO 1
21
#endif
// __clang__
22
#endif
// _MSC_VER
23
24
#if defined(__GNUC__)
25
// Marks a block with a name so that MCA analysis can see it.
26
#define ADA_BEGIN_DEBUG_BLOCK(name) __asm volatile("# LLVM-MCA-BEGIN " #name);
27
#define ADA_END_DEBUG_BLOCK(name) __asm volatile("# LLVM-MCA-END " #name);
28
#define ADA_DEBUG_BLOCK(name, block) \
29
BEGIN_DEBUG_BLOCK(name); \
30
block; \
31
END_DEBUG_BLOCK(name);
32
#else
33
#define ADA_BEGIN_DEBUG_BLOCK(name)
34
#define ADA_END_DEBUG_BLOCK(name)
35
#define ADA_DEBUG_BLOCK(name, block)
36
#endif
37
38
// Align to N-byte boundary
39
#define ADA_ROUNDUP_N(a, n) (((a) + ((n)-1)) & ~((n)-1))
40
#define ADA_ROUNDDOWN_N(a, n) ((a) & ~((n)-1))
41
42
#define ADA_ISALIGNED_N(ptr, n) (((uintptr_t)(ptr) & ((n)-1)) == 0)
43
44
#if defined(ADA_REGULAR_VISUAL_STUDIO)
45
46
#define ada_really_inline __forceinline
47
#define ada_never_inline __declspec(noinline)
48
49
#define ada_unused
50
#define ada_warn_unused
51
52
#define ADA_PUSH_DISABLE_WARNINGS __pragma(warning(push))
53
#define ADA_PUSH_DISABLE_ALL_WARNINGS __pragma(warning(push, 0))
54
#define ADA_DISABLE_VS_WARNING(WARNING_NUMBER) \
55
__pragma(warning(disable : WARNING_NUMBER))
56
// Get rid of Intellisense-only warnings (Code Analysis)
57
// Though __has_include is C++17, it is supported in Visual Studio 2017 or
58
// better (_MSC_VER>=1910).
59
#ifdef __has_include
60
#if __has_include(<CppCoreCheck\Warnings.h>)
61
#include <CppCoreCheck\Warnings.h>
62
#define ADA_DISABLE_UNDESIRED_WARNINGS \
63
ADA_DISABLE_VS_WARNING(ALL_CPPCORECHECK_WARNINGS)
64
#endif
65
#endif
66
67
#ifndef ADA_DISABLE_UNDESIRED_WARNINGS
68
#define ADA_DISABLE_UNDESIRED_WARNINGS
69
#endif
70
71
#define ADA_DISABLE_DEPRECATED_WARNING ADA_DISABLE_VS_WARNING(4996)
72
#define ADA_DISABLE_STRICT_OVERFLOW_WARNING
73
#define ADA_POP_DISABLE_WARNINGS __pragma(warning(pop))
74
75
#else
// ADA_REGULAR_VISUAL_STUDIO
76
77
#define ada_really_inline inline __attribute__((always_inline))
78
#define ada_never_inline inline __attribute__((noinline))
79
80
#define ada_unused __attribute__((unused))
81
#define ada_warn_unused __attribute__((warn_unused_result))
82
83
#define ADA_PUSH_DISABLE_WARNINGS _Pragma("GCC diagnostic push")
84
// gcc doesn't seem to disable all warnings with all and extra, add warnings
85
// here as necessary
86
#define ADA_PUSH_DISABLE_ALL_WARNINGS \
87
ADA_PUSH_DISABLE_WARNINGS \
88
ADA_DISABLE_GCC_WARNING("-Weffc++") \
89
ADA_DISABLE_GCC_WARNING("-Wall") \
90
ADA_DISABLE_GCC_WARNING("-Wconversion") \
91
ADA_DISABLE_GCC_WARNING("-Wextra") \
92
ADA_DISABLE_GCC_WARNING("-Wattributes") \
93
ADA_DISABLE_GCC_WARNING("-Wimplicit-fallthrough") \
94
ADA_DISABLE_GCC_WARNING("-Wnon-virtual-dtor") \
95
ADA_DISABLE_GCC_WARNING("-Wreturn-type") \
96
ADA_DISABLE_GCC_WARNING("-Wshadow") \
97
ADA_DISABLE_GCC_WARNING("-Wunused-parameter") \
98
ADA_DISABLE_GCC_WARNING("-Wunused-variable")
99
#define ADA_PRAGMA(P) _Pragma(#P)
100
#define ADA_DISABLE_GCC_WARNING(WARNING) \
101
ADA_PRAGMA(GCC diagnostic ignored WARNING)
102
#if defined(ADA_CLANG_VISUAL_STUDIO)
103
#define ADA_DISABLE_UNDESIRED_WARNINGS \
104
ADA_DISABLE_GCC_WARNING("-Wmicrosoft-include")
105
#else
106
#define ADA_DISABLE_UNDESIRED_WARNINGS
107
#endif
108
#define ADA_DISABLE_DEPRECATED_WARNING \
109
ADA_DISABLE_GCC_WARNING("-Wdeprecated-declarations")
110
#define ADA_DISABLE_STRICT_OVERFLOW_WARNING \
111
ADA_DISABLE_GCC_WARNING("-Wstrict-overflow")
112
#define ADA_POP_DISABLE_WARNINGS _Pragma("GCC diagnostic pop")
113
114
#endif
// MSC_VER
115
116
#if defined(ADA_VISUAL_STUDIO)
122
#if ADA_USING_LIBRARY
123
#define ADA_DLLIMPORTEXPORT __declspec(dllimport)
124
#else
125
#define ADA_DLLIMPORTEXPORT __declspec(dllexport)
126
#endif
127
#else
128
#define ADA_DLLIMPORTEXPORT
129
#endif
130
132
#define ADA_TRY(EXPR) \
133
{ \
134
auto _err = (EXPR); \
135
if (_err) { \
136
return _err; \
137
} \
138
}
139
140
// __has_cpp_attribute is part of C++20
141
#if !defined(__has_cpp_attribute)
142
#define __has_cpp_attribute(x) 0
143
#endif
144
145
#if __has_cpp_attribute(gnu::noinline)
146
#define ADA_ATTRIBUTE_NOINLINE [[gnu::noinline]]
147
#else
148
#define ADA_ATTRIBUTE_NOINLINE
149
#endif
150
151
namespace
ada
{
152
[[noreturn]]
inline
void
unreachable
() {
153
#ifdef __GNUC__
154
__builtin_unreachable();
155
#elif defined(_MSC_VER)
156
__assume(
false
);
157
#else
158
#endif
159
}
160
}
// namespace ada
161
162
// Unless the programmer has already set ADA_DEVELOPMENT_CHECKS,
163
// we want to set it under debug builds. We detect a debug build
164
// under Visual Studio when the _DEBUG macro is set. Under the other
165
// compilers, we use the fact that they define __OPTIMIZE__ whenever
166
// they allow optimizations.
167
// It is possible that this could miss some cases where ADA_DEVELOPMENT_CHECKS
168
// is helpful, but the programmer can set the macro ADA_DEVELOPMENT_CHECKS.
169
// It could also wrongly set ADA_DEVELOPMENT_CHECKS (e.g., if the programmer
170
// sets _DEBUG in a release build under Visual Studio, or if some compiler fails
171
// to set the __OPTIMIZE__ macro).
172
#if !defined(ADA_DEVELOPMENT_CHECKS) && !defined(NDEBUG)
173
#ifdef _MSC_VER
174
// Visual Studio seems to set _DEBUG for debug builds.
175
#ifdef _DEBUG
176
#define ADA_DEVELOPMENT_CHECKS 1
177
#endif
// _DEBUG
178
#else
// _MSC_VER
179
// All other compilers appear to set __OPTIMIZE__ to a positive integer
180
// when the compiler is optimizing.
181
#ifndef __OPTIMIZE__
182
#define ADA_DEVELOPMENT_CHECKS 1
183
#endif
// __OPTIMIZE__
184
#endif
// _MSC_VER
185
#endif
// ADA_DEVELOPMENT_CHECKS
186
187
#define ADA_STR(x) #x
188
189
#if ADA_DEVELOPMENT_CHECKS
190
#define ADA_REQUIRE(EXPR) \
191
{ \
192
if (!(EXPR) { abort(); }) }
193
194
#define ADA_FAIL(MESSAGE) \
195
do { \
196
std::cerr << "FAIL: " << (MESSAGE) << std::endl; \
197
abort(); \
198
} while (0);
199
#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE) \
200
do { \
201
if (LHS != RHS) { \
202
std::cerr << "Mismatch: '" << LHS << "' - '" << RHS << "'" << std::endl; \
203
ADA_FAIL(MESSAGE); \
204
} \
205
} while (0);
206
#define ADA_ASSERT_TRUE(COND) \
207
do { \
208
if (!(COND)) { \
209
std::cerr << "Assert at line " << __LINE__ << " of file " << __FILE__ \
210
<< std::endl; \
211
ADA_FAIL(ADA_STR(COND)); \
212
} \
213
} while (0);
214
#else
215
#define ADA_FAIL(MESSAGE)
216
#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE)
217
#define ADA_ASSERT_TRUE(COND)
218
#endif
219
220
#ifdef ADA_VISUAL_STUDIO
221
#define ADA_ASSUME(COND) __assume(COND)
222
#else
223
#define ADA_ASSUME(COND) \
224
do { \
225
if (!(COND)) { \
226
__builtin_unreachable(); \
227
} \
228
} while (0)
229
#endif
230
231
#if defined(__SSE2__) || defined(__x86_64__) || defined(__x86_64) || \
232
(defined(_M_AMD64) || defined(_M_X64) || \
233
(defined(_M_IX86_FP) && _M_IX86_FP == 2))
234
#define ADA_SSE2 1
235
#endif
236
237
#if defined(__aarch64__) || defined(_M_ARM64)
238
#define ADA_NEON 1
239
#endif
240
241
#ifndef __has_cpp_attribute
242
#define ada_lifetime_bound
243
#elif __has_cpp_attribute(msvc::lifetimebound)
244
#define ada_lifetime_bound [[msvc::lifetimebound]]
245
#elif __has_cpp_attribute(clang::lifetimebound)
246
#define ada_lifetime_bound [[clang::lifetimebound]]
247
#elif __has_cpp_attribute(lifetimebound)
248
#define ada_lifetime_bound [[lifetimebound]]
249
#else
250
#define ada_lifetime_bound
251
#endif
252
253
#endif
// ADA_COMMON_DEFS_H
ada
Definition
ada_idna.h:13
ada::unreachable
void unreachable()
Definition
common_defs.h:152