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#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// Unless the programmer has already set ADA_DEVELOPMENT_CHECKS,
177// we want to set it under debug builds. We detect a debug build
178// under Visual Studio when the _DEBUG macro is set. Under the other
179// compilers, we use the fact that they define __OPTIMIZE__ whenever
180// they allow optimizations.
181// It is possible that this could miss some cases where ADA_DEVELOPMENT_CHECKS
182// is helpful, but the programmer can set the macro ADA_DEVELOPMENT_CHECKS.
183// It could also wrongly set ADA_DEVELOPMENT_CHECKS (e.g., if the programmer
184// sets _DEBUG in a release build under Visual Studio, or if some compiler fails
185// to set the __OPTIMIZE__ macro).
186#if !defined(ADA_DEVELOPMENT_CHECKS) && !defined(NDEBUG)
187#ifdef _MSC_VER
188// Visual Studio seems to set _DEBUG for debug builds.
189#ifdef _DEBUG
190#define ADA_DEVELOPMENT_CHECKS 1
191#endif // _DEBUG
192#else // _MSC_VER
193// All other compilers appear to set __OPTIMIZE__ to a positive integer
194// when the compiler is optimizing.
195#ifndef __OPTIMIZE__
196#define ADA_DEVELOPMENT_CHECKS 1
197#endif // __OPTIMIZE__
198#endif // _MSC_VER
199#endif // ADA_DEVELOPMENT_CHECKS
200
201#define ADA_STR(x) #x
202
203#if ADA_DEVELOPMENT_CHECKS
204#define ADA_REQUIRE(EXPR) \
205 { \
206 if (!(EXPR) { abort(); }) }
207
208#define ADA_FAIL(MESSAGE) \
209 do { \
210 std::cerr << "FAIL: " << (MESSAGE) << std::endl; \
211 abort(); \
212 } while (0);
213#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE) \
214 do { \
215 if (LHS != RHS) { \
216 std::cerr << "Mismatch: '" << LHS << "' - '" << RHS << "'" << std::endl; \
217 ADA_FAIL(MESSAGE); \
218 } \
219 } while (0);
220#define ADA_ASSERT_TRUE(COND) \
221 do { \
222 if (!(COND)) { \
223 std::cerr << "Assert at line " << __LINE__ << " of file " << __FILE__ \
224 << std::endl; \
225 ADA_FAIL(ADA_STR(COND)); \
226 } \
227 } while (0);
228#else
229#define ADA_FAIL(MESSAGE)
230#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE)
231#define ADA_ASSERT_TRUE(COND)
232#endif
233
234#ifdef ADA_VISUAL_STUDIO
235#define ADA_ASSUME(COND) __assume(COND)
236#else
237#define ADA_ASSUME(COND) \
238 do { \
239 if (!(COND)) { \
240 __builtin_unreachable(); \
241 } \
242 } while (0)
243#endif
244
245#if defined(__SSE2__) || defined(__x86_64__) || defined(__x86_64) || \
246 (defined(_M_AMD64) || defined(_M_X64) || \
247 (defined(_M_IX86_FP) && _M_IX86_FP == 2))
248#define ADA_SSE2 1
249#endif
250
251#if defined(__aarch64__) || defined(_M_ARM64)
252#define ADA_NEON 1
253#endif
254
255#ifndef __has_cpp_attribute
256#define ada_lifetime_bound
257#elif __has_cpp_attribute(msvc::lifetimebound)
258#define ada_lifetime_bound [[msvc::lifetimebound]]
259#elif __has_cpp_attribute(clang::lifetimebound)
260#define ada_lifetime_bound [[clang::lifetimebound]]
261#elif __has_cpp_attribute(lifetimebound)
262#define ada_lifetime_bound [[lifetimebound]]
263#else
264#define ada_lifetime_bound
265#endif
266
267#endif // ADA_COMMON_DEFS_H
Definition ada_idna.h:13
void unreachable()