Ada 3.1.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// 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#define ADA_PRAGMA(P) _Pragma(#P)
104#define ADA_DISABLE_GCC_WARNING(WARNING) \
105 ADA_PRAGMA(GCC diagnostic ignored WARNING)
106#if defined(ADA_CLANG_VISUAL_STUDIO)
107#define ADA_DISABLE_UNDESIRED_WARNINGS \
108 ADA_DISABLE_GCC_WARNING("-Wmicrosoft-include")
109#else
110#define ADA_DISABLE_UNDESIRED_WARNINGS
111#endif
112#define ADA_DISABLE_DEPRECATED_WARNING \
113 ADA_DISABLE_GCC_WARNING("-Wdeprecated-declarations")
114#define ADA_DISABLE_STRICT_OVERFLOW_WARNING \
115 ADA_DISABLE_GCC_WARNING("-Wstrict-overflow")
116#define ADA_POP_DISABLE_WARNINGS _Pragma("GCC diagnostic pop")
117
118#endif // MSC_VER
119
120#if defined(ADA_VISUAL_STUDIO)
126#if ADA_USING_LIBRARY
127#define ADA_DLLIMPORTEXPORT __declspec(dllimport)
128#else
129#define ADA_DLLIMPORTEXPORT __declspec(dllexport)
130#endif
131#else
132#define ADA_DLLIMPORTEXPORT
133#endif
134
136#define ADA_TRY(EXPR) \
137 { \
138 auto _err = (EXPR); \
139 if (_err) { \
140 return _err; \
141 } \
142 }
143
144// __has_cpp_attribute is part of C++20
145#if !defined(__has_cpp_attribute)
146#define __has_cpp_attribute(x) 0
147#endif
148
149#if __has_cpp_attribute(gnu::noinline)
150#define ADA_ATTRIBUTE_NOINLINE [[gnu::noinline]]
151#else
152#define ADA_ATTRIBUTE_NOINLINE
153#endif
154
155namespace ada {
156[[noreturn]] inline void unreachable() {
157#ifdef __GNUC__
158 __builtin_unreachable();
159#elif defined(_MSC_VER)
160 __assume(false);
161#else
162#endif
163}
164} // namespace ada
165
166// Unless the programmer has already set ADA_DEVELOPMENT_CHECKS,
167// we want to set it under debug builds. We detect a debug build
168// under Visual Studio when the _DEBUG macro is set. Under the other
169// compilers, we use the fact that they define __OPTIMIZE__ whenever
170// they allow optimizations.
171// It is possible that this could miss some cases where ADA_DEVELOPMENT_CHECKS
172// is helpful, but the programmer can set the macro ADA_DEVELOPMENT_CHECKS.
173// It could also wrongly set ADA_DEVELOPMENT_CHECKS (e.g., if the programmer
174// sets _DEBUG in a release build under Visual Studio, or if some compiler fails
175// to set the __OPTIMIZE__ macro).
176#if !defined(ADA_DEVELOPMENT_CHECKS) && !defined(NDEBUG)
177#ifdef _MSC_VER
178// Visual Studio seems to set _DEBUG for debug builds.
179#ifdef _DEBUG
180#define ADA_DEVELOPMENT_CHECKS 1
181#endif // _DEBUG
182#else // _MSC_VER
183// All other compilers appear to set __OPTIMIZE__ to a positive integer
184// when the compiler is optimizing.
185#ifndef __OPTIMIZE__
186#define ADA_DEVELOPMENT_CHECKS 1
187#endif // __OPTIMIZE__
188#endif // _MSC_VER
189#endif // ADA_DEVELOPMENT_CHECKS
190
191#define ADA_STR(x) #x
192
193#if ADA_DEVELOPMENT_CHECKS
194#define ADA_REQUIRE(EXPR) \
195 { \
196 if (!(EXPR) { abort(); }) }
197
198#define ADA_FAIL(MESSAGE) \
199 do { \
200 std::cerr << "FAIL: " << (MESSAGE) << std::endl; \
201 abort(); \
202 } while (0);
203#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE) \
204 do { \
205 if (LHS != RHS) { \
206 std::cerr << "Mismatch: '" << LHS << "' - '" << RHS << "'" << std::endl; \
207 ADA_FAIL(MESSAGE); \
208 } \
209 } while (0);
210#define ADA_ASSERT_TRUE(COND) \
211 do { \
212 if (!(COND)) { \
213 std::cerr << "Assert at line " << __LINE__ << " of file " << __FILE__ \
214 << std::endl; \
215 ADA_FAIL(ADA_STR(COND)); \
216 } \
217 } while (0);
218#else
219#define ADA_FAIL(MESSAGE)
220#define ADA_ASSERT_EQUAL(LHS, RHS, MESSAGE)
221#define ADA_ASSERT_TRUE(COND)
222#endif
223
224#ifdef ADA_VISUAL_STUDIO
225#define ADA_ASSUME(COND) __assume(COND)
226#else
227#define ADA_ASSUME(COND) \
228 do { \
229 if (!(COND)) { \
230 __builtin_unreachable(); \
231 } \
232 } while (0)
233#endif
234
235#if defined(__SSE2__) || defined(__x86_64__) || defined(__x86_64) || \
236 (defined(_M_AMD64) || defined(_M_X64) || \
237 (defined(_M_IX86_FP) && _M_IX86_FP == 2))
238#define ADA_SSE2 1
239#endif
240
241#if defined(__aarch64__) || defined(_M_ARM64)
242#define ADA_NEON 1
243#endif
244
245#ifndef __has_cpp_attribute
246#define ada_lifetime_bound
247#elif __has_cpp_attribute(msvc::lifetimebound)
248#define ada_lifetime_bound [[msvc::lifetimebound]]
249#elif __has_cpp_attribute(clang::lifetimebound)
250#define ada_lifetime_bound [[clang::lifetimebound]]
251#elif __has_cpp_attribute(lifetimebound)
252#define ada_lifetime_bound [[lifetimebound]]
253#else
254#define ada_lifetime_bound
255#endif
256
257#ifdef __cpp_lib_format
258#if __cpp_lib_format >= 202110L
259#include <format>
260#define ADA_HAS_FORMAT 1
261#endif
262#endif
263
264#endif // ADA_COMMON_DEFS_H
Definition ada_idna.h:13
void unreachable()