Ada 2.9.0
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#ifndef ada_likely
53#define ada_likely(x) x
54#endif
55#ifndef ada_unlikely
56#define ada_unlikely(x) x
57#endif
58
59#define ADA_PUSH_DISABLE_WARNINGS __pragma(warning(push))
60#define ADA_PUSH_DISABLE_ALL_WARNINGS __pragma(warning(push, 0))
61#define ADA_DISABLE_VS_WARNING(WARNING_NUMBER) \
62 __pragma(warning(disable : WARNING_NUMBER))
63// Get rid of Intellisense-only warnings (Code Analysis)
64// Though __has_include is C++17, it is supported in Visual Studio 2017 or
65// better (_MSC_VER>=1910).
66#ifdef __has_include
67#if __has_include(<CppCoreCheck\Warnings.h>)
68#include <CppCoreCheck\Warnings.h>
69#define ADA_DISABLE_UNDESIRED_WARNINGS \
70 ADA_DISABLE_VS_WARNING(ALL_CPPCORECHECK_WARNINGS)
71#endif
72#endif
73
74#ifndef ADA_DISABLE_UNDESIRED_WARNINGS
75#define ADA_DISABLE_UNDESIRED_WARNINGS
76#endif
77
78#define ADA_DISABLE_DEPRECATED_WARNING ADA_DISABLE_VS_WARNING(4996)
79#define ADA_DISABLE_STRICT_OVERFLOW_WARNING
80#define ADA_POP_DISABLE_WARNINGS __pragma(warning(pop))
81
82#else // ADA_REGULAR_VISUAL_STUDIO
83
84#define ada_really_inline inline __attribute__((always_inline))
85#define ada_never_inline inline __attribute__((noinline))
86
87#define ada_unused __attribute__((unused))
88#define ada_warn_unused __attribute__((warn_unused_result))
89
90#ifndef ada_likely
91#define ada_likely(x) __builtin_expect(!!(x), 1)
92#endif
93#ifndef ada_unlikely
94#define ada_unlikely(x) __builtin_expect(!!(x), 0)
95#endif
96
97#define ADA_PUSH_DISABLE_WARNINGS _Pragma("GCC diagnostic push")
98// gcc doesn't seem to disable all warnings with all and extra, add warnings
99// here as necessary
100#define ADA_PUSH_DISABLE_ALL_WARNINGS \
101 ADA_PUSH_DISABLE_WARNINGS \
102 ADA_DISABLE_GCC_WARNING("-Weffc++") \
103 ADA_DISABLE_GCC_WARNING("-Wall") \
104 ADA_DISABLE_GCC_WARNING("-Wconversion") \
105 ADA_DISABLE_GCC_WARNING("-Wextra") \
106 ADA_DISABLE_GCC_WARNING("-Wattributes") \
107 ADA_DISABLE_GCC_WARNING("-Wimplicit-fallthrough") \
108 ADA_DISABLE_GCC_WARNING("-Wnon-virtual-dtor") \
109 ADA_DISABLE_GCC_WARNING("-Wreturn-type") \
110 ADA_DISABLE_GCC_WARNING("-Wshadow") \
111 ADA_DISABLE_GCC_WARNING("-Wunused-parameter") \
112 ADA_DISABLE_GCC_WARNING("-Wunused-variable")
113#define ADA_PRAGMA(P) _Pragma(#P)
114#define ADA_DISABLE_GCC_WARNING(WARNING) \
115 ADA_PRAGMA(GCC diagnostic ignored WARNING)
116#if defined(ADA_CLANG_VISUAL_STUDIO)
117#define ADA_DISABLE_UNDESIRED_WARNINGS \
118 ADA_DISABLE_GCC_WARNING("-Wmicrosoft-include")
119#else
120#define ADA_DISABLE_UNDESIRED_WARNINGS
121#endif
122#define ADA_DISABLE_DEPRECATED_WARNING \
123 ADA_DISABLE_GCC_WARNING("-Wdeprecated-declarations")
124#define ADA_DISABLE_STRICT_OVERFLOW_WARNING \
125 ADA_DISABLE_GCC_WARNING("-Wstrict-overflow")
126#define ADA_POP_DISABLE_WARNINGS _Pragma("GCC diagnostic pop")
127
128#endif // MSC_VER
129
130#if defined(ADA_VISUAL_STUDIO)
136#if ADA_USING_LIBRARY
137#define ADA_DLLIMPORTEXPORT __declspec(dllimport)
138#else
139#define ADA_DLLIMPORTEXPORT __declspec(dllexport)
140#endif
141#else
142#define ADA_DLLIMPORTEXPORT
143#endif
144
146#define ADA_TRY(EXPR) \
147 { \
148 auto _err = (EXPR); \
149 if (_err) { \
150 return _err; \
151 } \
152 }
153
154// __has_cpp_attribute is part of C++20
155#if !defined(__has_cpp_attribute)
156#define __has_cpp_attribute(x) 0
157#endif
158
159#if __has_cpp_attribute(gnu::noinline)
160#define ADA_ATTRIBUTE_NOINLINE [[gnu::noinline]]
161#else
162#define ADA_ATTRIBUTE_NOINLINE
163#endif
164
165namespace ada {
166[[noreturn]] inline void unreachable() {
167#ifdef __GNUC__
168 __builtin_unreachable();
169#elif defined(_MSC_VER)
170 __assume(false);
171#else
172#endif
173}
174} // namespace ada
175
176#if defined(__GNUC__) && !defined(__clang__)
177#if __GNUC__ <= 8
178#define ADA_OLD_GCC 1
179#endif // __GNUC__ <= 8
180#endif // defined(__GNUC__) && !defined(__clang__)
181
182#if ADA_OLD_GCC
183#define ada_constexpr
184#else
185#define ada_constexpr constexpr
186#endif
187
188#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
189#define ADA_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
190#elif defined(_WIN32)
191#define ADA_IS_BIG_ENDIAN 0
192#else
193#if defined(__APPLE__) || \
194 defined(__FreeBSD__) // defined __BYTE_ORDER__ && defined
195 // __ORDER_BIG_ENDIAN__
196#include <machine/endian.h>
197#elif defined(sun) || \
198 defined(__sun) // defined(__APPLE__) || defined(__FreeBSD__)
199#include <sys/byteorder.h>
200#else // defined(__APPLE__) || defined(__FreeBSD__)
201
202#ifdef __has_include
203#if __has_include(<endian.h>)
204#include <endian.h>
205#endif //__has_include(<endian.h>)
206#endif //__has_include
207
208#endif // defined(__APPLE__) || defined(__FreeBSD__)
209
210#ifndef !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__)
211#define ADA_IS_BIG_ENDIAN 0
212#endif
213
214#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
215#define ADA_IS_BIG_ENDIAN 0
216#else // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
217#define ADA_IS_BIG_ENDIAN 1
218#endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
219
220#endif // defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__
221
222// Unless the programmer has already set ADA_DEVELOPMENT_CHECKS,
223// we want to set it under debug builds. We detect a debug build
224// under Visual Studio when the _DEBUG macro is set. Under the other
225// compilers, we use the fact that they define __OPTIMIZE__ whenever
226// they allow optimizations.
227// It is possible that this could miss some cases where ADA_DEVELOPMENT_CHECKS
228// is helpful, but the programmer can set the macro ADA_DEVELOPMENT_CHECKS.
229// It could also wrongly set ADA_DEVELOPMENT_CHECKS (e.g., if the programmer
230// sets _DEBUG in a release build under Visual Studio, or if some compiler fails
231// to set the __OPTIMIZE__ macro).
232#if !defined(ADA_DEVELOPMENT_CHECKS) && !defined(NDEBUG)
233#ifdef _MSC_VER
234// Visual Studio seems to set _DEBUG for debug builds.
235#ifdef _DEBUG
236#define ADA_DEVELOPMENT_CHECKS 1
237#endif // _DEBUG
238#else // _MSC_VER
239// All other compilers appear to set __OPTIMIZE__ to a positive integer
240// when the compiler is optimizing.
241#ifndef __OPTIMIZE__
242#define ADA_DEVELOPMENT_CHECKS 1
243#endif // __OPTIMIZE__
244#endif // _MSC_VER
245#endif // ADA_DEVELOPMENT_CHECKS
246
247#define ADA_STR(x) #x
248
249#if ADA_DEVELOPMENT_CHECKS
250#define ADA_REQUIRE(EXPR) \
251 { \
252 if (!(EXPR) { abort(); }) }
253
254#define ADA_FAIL(MESSAGE) \
255 do { \
256 std::cerr << "FAIL: " << (MESSAGE) << std::endl; \
257 abort(); \
258 } while (0);
259#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE) \
260 do { \
261 if (LHS != RHS) { \
262 std::cerr << "Mismatch: '" << LHS << "' - '" << RHS << "'" << std::endl; \
263 ADA_FAIL(MESSAGE); \
264 } \
265 } while (0);
266#define ADA_ASSERT_TRUE(COND) \
267 do { \
268 if (!(COND)) { \
269 std::cerr << "Assert at line " << __LINE__ << " of file " << __FILE__ \
270 << std::endl; \
271 ADA_FAIL(ADA_STR(COND)); \
272 } \
273 } while (0);
274#else
275#define ADA_FAIL(MESSAGE)
276#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE)
277#define ADA_ASSERT_TRUE(COND)
278#endif
279
280#ifdef ADA_VISUAL_STUDIO
281#define ADA_ASSUME(COND) __assume(COND)
282#else
283#define ADA_ASSUME(COND) \
284 do { \
285 if (!(COND)) { \
286 __builtin_unreachable(); \
287 } \
288 } while (0)
289#endif
290
291#if defined(__SSE2__) || defined(__x86_64__) || defined(__x86_64) || \
292 (defined(_M_AMD64) || defined(_M_X64) || \
293 (defined(_M_IX86_FP) && _M_IX86_FP == 2))
294#define ADA_SSE2 1
295#endif
296
297#if defined(__aarch64__) || defined(_M_ARM64)
298#define ADA_NEON 1
299#endif
300
301#endif // ADA_COMMON_DEFS_H
Definition ada_idna.h:13
void unreachable()