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