Debian Codegraph @

crt/aws-c-common/include/aws/common/macros.h

raw symbols SCIP
1 #ifndef AWS_COMMON_MACROS_H
2 #define AWS_COMMON_MACROS_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 /* clang-format off */
9
10 /* Use these macros in public header files to suppress unreasonable compiler
11 * warnings. Public header files are included by external applications,
12 * which may set their warning levels pedantically high.
13 *
14 * Developers of AWS libraries should hesitate before adding more warnings to this macro.
15 * Prefer disabling the warning within a .c file, or in the library's CFLAGS,
16 * or push/pop the warning around a single problematic declaration. */
17 #if defined(_MSC_VER)
18 # define AWS_PUSH_SANE_WARNING_LEVEL \
19 __pragma(warning(push)) \
20 __pragma(warning(disable : 4820)) /* padding added to struct */ \
21 __pragma(warning(disable : 4514)) /* unreferenced inline function has been removed */ \
22 __pragma(warning(disable : 5039)) /* reference to potentially throwing function passed to extern C function */
23 # define AWS_POP_SANE_WARNING_LEVEL __pragma(warning(pop))
24 #else
25 # define AWS_PUSH_SANE_WARNING_LEVEL
26 # define AWS_POP_SANE_WARNING_LEVEL
27 #endif
28 /* clang-format on */
29
30 #ifdef __cplusplus
31 # define AWS_EXTERN_C_BEGIN extern "C" {
32 # define AWS_EXTERN_C_END }
33 #else
34 # define AWS_EXTERN_C_BEGIN
35 # define AWS_EXTERN_C_END
36 #endif /* __cplusplus */
37
38 #define AWS_CONCAT(A, B) A##B
39 #define AWS_STATIC_ASSERT0(cond, msg) typedef char AWS_CONCAT(static_assertion_, msg)[(!!(cond)) * 2 - 1]
40 #define AWS_STATIC_ASSERT1(cond, line) AWS_STATIC_ASSERT0(cond, AWS_CONCAT(at_line_, line))
41 #define AWS_STATIC_ASSERT(cond) AWS_STATIC_ASSERT1(cond, __LINE__)
42
43 /* https://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion */
44 #define GLUE(x, y) x y
45
46 #define RETURN_ARG_COUNT(_1_, _2_, _3_, _4_, _5_, count, ...) count
47 #define EXPAND_ARGS(args) RETURN_ARG_COUNT args
48 #define COUNT_ARGS_MAX5(...) EXPAND_ARGS((__VA_ARGS__, 5, 4, 3, 2, 1, 0))
49
50 #define OVERLOAD_MACRO2(name, count) name##count
51 #define OVERLOAD_MACRO1(name, count) OVERLOAD_MACRO2(name, count)
52 #define OVERLOAD_MACRO(name, count) OVERLOAD_MACRO1(name, count)
53
54 #define CALL_OVERLOAD(name, ...) GLUE(OVERLOAD_MACRO(name, COUNT_ARGS_MAX5(__VA_ARGS__)), (__VA_ARGS__))
55
56 #define CALL_OVERLOAD_TEST1(x) x
57 #define CALL_OVERLOAD_TEST2(x, y) y
58 #define CALL_OVERLOAD_TEST3(x, y, z) z
59 #define CALL_OVERLOAD_TEST(...) CALL_OVERLOAD(CALL_OVERLOAD_TEST, __VA_ARGS__)
60 AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1) == 1);
61 AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2) == 2);
62 AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2, 3) == 3);
63
64 enum { AWS_CACHE_LINE = 64 };
65 /**
66 * Format macro for strings of a specified length.
67 * Allows non null-terminated strings to be used with the printf family of functions.
68 * Ex: printf("scheme is " PRInSTR, 4, "http://example.org"); // outputs: "scheme is http"
69 */
70 #define PRInSTR "%.*s"
71
72 #if defined(_MSC_VER)
73 # include <malloc.h>
74 # define AWS_ALIGNED_TYPEDEF(from, to, alignment) typedef __declspec(align(alignment)) from to
75 # define AWS_LIKELY(x) x
76 # define AWS_UNLIKELY(x) x
77 # define AWS_FORCE_INLINE __forceinline
78 # define AWS_NO_INLINE __declspec(noinline)
79 # define AWS_VARIABLE_LENGTH_ARRAY(type, name, length) type *name = _alloca(sizeof(type) * (length))
80 # define AWS_DECLSPEC_NORETURN __declspec(noreturn)
81 # define AWS_ATTRIBUTE_NORETURN
82 #else
83 # if defined(__GNUC__) || defined(__clang__)
84 # define AWS_ALIGNED_TYPEDEF(from, to, alignment) typedef from to __attribute__((aligned(alignment)))
85 # define AWS_TYPE_OF(a) __typeof__(a)
86 # define AWS_LIKELY(x) __builtin_expect(!!(x), 1)
87 # define AWS_UNLIKELY(x) __builtin_expect(!!(x), 0)
88 # define AWS_FORCE_INLINE __attribute__((always_inline))
89 # define AWS_NO_INLINE __attribute__((noinline))
90 # define AWS_DECLSPEC_NORETURN
91 # define AWS_ATTRIBUTE_NORETURN __attribute__((noreturn))
92 # if defined(__cplusplus)
93 # define AWS_VARIABLE_LENGTH_ARRAY(type, name, length) type *name = alloca(sizeof(type) * (length))
94 # else
95 # define AWS_VARIABLE_LENGTH_ARRAY(type, name, length) type name[length]
96 # endif /* defined(__cplusplus) */
97 # endif /* defined(__GNUC__) || defined(__clang__) */
98 #endif /* defined(_MSC_VER) */
99
100 #if defined(__has_feature)
101 # if __has_feature(address_sanitizer)
102 # define AWS_SUPPRESS_ASAN __attribute__((no_sanitize("address")))
103 # endif
104 #elif defined(__SANITIZE_ADDRESS__)
105 # if defined(__GNUC__)
106 # define AWS_SUPPRESS_ASAN __attribute__((no_sanitize_address))
107 # elif defined(_MSC_VER)
108 # define AWS_SUPPRESS_ASAN __declspec(no_sanitize_address)
109 # endif
110 #endif
111
112 #if !defined(AWS_SUPPRESS_ASAN)
113 # define AWS_SUPPRESS_ASAN
114 #endif
115
116 #if defined(__has_feature)
117 # if __has_feature(thread_sanitizer)
118 # define AWS_SUPPRESS_TSAN __attribute__((no_sanitize("thread")))
119 # endif
120 #elif defined(__SANITIZE_THREAD__)
121 # if defined(__GNUC__)
122 # define AWS_SUPPRESS_TSAN __attribute__((no_sanitize_thread))
123 # else
124 # define AWS_SUPPRESS_TSAN
125 # endif
126 #else
127 # define AWS_SUPPRESS_TSAN
128 #endif
129
130 #if !defined(AWS_SUPPRESS_TSAN)
131 # define AWS_SUPPRESS_TSAN
132 #endif
133
134 #if defined(__has_feature)
135 # if __has_feature(undefined_behavior_sanitizer)
136 # define AWS_SUPPRESS_UBSAN __attribute__((no_sanitize("undefined")))
137 # endif
138 #elif defined(__SANITIZE_UNDEFINED__)
139 # if defined(__GNUC__)
140 # define AWS_SUPPRESS_UBSAN __attribute__((no_sanitize_undefined))
141 # else
142 # define AWS_SUPPRESS_UBSAN
143 # endif
144 #else
145 # define AWS_SUPPRESS_UBSAN
146 #endif
147
148 #if !defined(AWS_SUPPRESS_UBSAN)
149 # define AWS_SUPPRESS_UBSAN
150 #endif
151
152 /* If this is C++, restrict isn't supported. If this is not at least C99 on gcc and clang, it isn't supported.
153 * If visual C++ building in C mode, the restrict definition is __restrict.
154 * This just figures all of that out based on who's including this header file. */
155 #if defined(__cplusplus)
156 # define AWS_RESTRICT
157 #else
158 # if defined(_MSC_VER)
159 # define AWS_RESTRICT __restrict
160 # else
161 # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
162 # define AWS_RESTRICT restrict
163 # else
164 # define AWS_RESTRICT
165 # endif /* defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L */
166 # endif /* defined(_MSC_VER) */
167 #endif /* defined(__cplusplus) */
168
169 #if defined(_MSC_VER)
170 # define AWS_THREAD_LOCAL __declspec(thread)
171 #else
172 # define AWS_THREAD_LOCAL __thread
173 #endif
174
175 #define AWS_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
176 /**
177 * from a pointer and a type of the struct containing the node
178 * this will get you back to the pointer of the object. member is the name of
179 * the instance of struct aws_linked_list_node in your struct.
180 */
181 #define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
182
183 #endif /* AWS_COMMON_MACROS_H */