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