| 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // This header file defines macros for declaring attributes for functions, |
| 16 | // types, and variables. |
| 17 | // |
| 18 | // These macros are used within Abseil and allow the compiler to optimize, where |
| 19 | // applicable, certain function calls. |
| 20 | // |
| 21 | // Most macros here are exposing GCC or Clang features, and are stubbed out for |
| 22 | // other compilers. |
| 23 | // |
| 24 | // GCC attributes documentation: |
| 25 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html |
| 26 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html |
| 27 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html |
| 28 | // |
| 29 | // Most attributes in this file are already supported by GCC 4.7. However, some |
| 30 | // of them are not supported in older version of Clang. Thus, we check |
| 31 | // `__has_attribute()` first. If the check fails, we check if we are on GCC and |
| 32 | // assume the attribute exists on GCC (which is verified on GCC 4.7). |
| 33 | |
| 34 | // SKIP_ABSL_INLINE_NAMESPACE_CHECK |
| 35 | |
| 36 | #ifndef ABSL_BASE_ATTRIBUTES_H_ |
| 37 | #define ABSL_BASE_ATTRIBUTES_H_ |
| 38 | |
| 39 | #include "absl/base/config.h" |
| 40 | |
| 41 | // ABSL_HAVE_ATTRIBUTE |
| 42 | // |
| 43 | // A function-like feature checking macro that is a wrapper around |
| 44 | // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a |
| 45 | // nonzero constant integer if the attribute is supported or 0 if not. |
| 46 | // |
| 47 | // It evaluates to zero if `__has_attribute` is not defined by the compiler. |
| 48 | // |
| 49 | // GCC: https://gcc.gnu.org/gcc-5/changes.html |
| 50 | // Clang: https://clang.llvm.org/docs/LanguageExtensions.html |
| 51 | #ifdef __has_attribute |
| 52 | #define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x) |
| 53 | #else |
| 54 | #define ABSL_HAVE_ATTRIBUTE(x) 0 |
| 55 | #endif |
| 56 | |
| 57 | // ABSL_HAVE_CPP_ATTRIBUTE |
| 58 | // |
| 59 | // A function-like feature checking macro that accepts C++11 style attributes. |
| 60 | // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6 |
| 61 | // (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't |
| 62 | // find `__has_cpp_attribute`, will evaluate to 0. |
| 63 | #if defined(__cplusplus) && defined(__has_cpp_attribute) |
| 64 | // NOTE: requiring __cplusplus above should not be necessary, but |
| 65 | // works around https://bugs.llvm.org/show_bug.cgi?id=23435. |
| 66 | #define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) |
| 67 | #else |
| 68 | #define ABSL_HAVE_CPP_ATTRIBUTE(x) 0 |
| 69 | #endif |
| 70 | |
| 71 | // ----------------------------------------------------------------------------- |
| 72 | // Function Attributes |
| 73 | // ----------------------------------------------------------------------------- |
| 74 | // |
| 75 | // GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 76 | // Clang: https://clang.llvm.org/docs/AttributeReference.html |
| 77 | |
| 78 | // ABSL_PRINTF_ATTRIBUTE |
| 79 | // ABSL_SCANF_ATTRIBUTE |
| 80 | // |
| 81 | // Tells the compiler to perform `printf` format string checking if the |
| 82 | // compiler supports it; see the 'format' attribute in |
| 83 | // <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>. |
| 84 | // |
| 85 | // Note: As the GCC manual states, "[s]ince non-static C++ methods |
| 86 | // have an implicit 'this' argument, the arguments of such methods |
| 87 | // should be counted from two, not one." |
| 88 | #if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__)) |
| 89 | #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \ |
| 90 | __attribute__((__format__(__printf__, string_index, first_to_check))) |
| 91 | #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \ |
| 92 | __attribute__((__format__(__scanf__, string_index, first_to_check))) |
| 93 | #else |
| 94 | #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) |
| 95 | #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) |
| 96 | #endif |
| 97 | |
| 98 | // ABSL_ATTRIBUTE_ALWAYS_INLINE |
| 99 | // ABSL_ATTRIBUTE_NOINLINE |
| 100 | // |
| 101 | // Forces functions to either inline or not inline. Introduced in gcc 3.1. |
| 102 | #if ABSL_HAVE_ATTRIBUTE(always_inline) || \ |
| 103 | (defined(__GNUC__) && !defined(__clang__)) |
| 104 | #define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) |
| 105 | #define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1 |
| 106 | #else |
| 107 | #define ABSL_ATTRIBUTE_ALWAYS_INLINE |
| 108 | #endif |
| 109 | |
| 110 | #if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__)) |
| 111 | #define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline)) |
| 112 | #define ABSL_HAVE_ATTRIBUTE_NOINLINE 1 |
| 113 | #else |
| 114 | #define ABSL_ATTRIBUTE_NOINLINE |
| 115 | #endif |
| 116 | |
| 117 | // ABSL_ATTRIBUTE_NO_TAIL_CALL |
| 118 | // |
| 119 | // Prevents the compiler from optimizing away stack frames for functions which |
| 120 | // end in a call to another function. |
| 121 | #if ABSL_HAVE_ATTRIBUTE(disable_tail_calls) |
| 122 | #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 |
| 123 | #define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls)) |
| 124 | #elif defined(__GNUC__) && !defined(__clang__) && !defined(__e2k__) |
| 125 | #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 |
| 126 | #define ABSL_ATTRIBUTE_NO_TAIL_CALL \ |
| 127 | __attribute__((optimize("no-optimize-sibling-calls"))) |
| 128 | #else |
| 129 | #define ABSL_ATTRIBUTE_NO_TAIL_CALL |
| 130 | #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0 |
| 131 | #endif |
| 132 | |
| 133 | // ABSL_ATTRIBUTE_WEAK |
| 134 | // |
| 135 | // Tags a function as weak for the purposes of compilation and linking. |
| 136 | // Weak attributes did not work properly in LLVM's Windows backend before |
| 137 | // 9.0.0, so disable them there. See https://bugs.llvm.org/show_bug.cgi?id=37598 |
| 138 | // for further information. Weak attributes do not work across DLL boundary. |
| 139 | // The MinGW compiler doesn't complain about the weak attribute until the link |
| 140 | // step, presumably because Windows doesn't use ELF binaries. |
| 141 | #if (ABSL_HAVE_ATTRIBUTE(weak) || \ |
| 142 | (defined(__GNUC__) && !defined(__clang__))) && \ |
| 143 | (!defined(_WIN32) || \ |
| 144 | (defined(__clang__) && __clang_major__ >= 9 && \ |
| 145 | !defined(ABSL_BUILD_DLL) && !defined(ABSL_CONSUME_DLL))) && \ |
| 146 | !defined(__MINGW32__) |
| 147 | #undef ABSL_ATTRIBUTE_WEAK |
| 148 | #define ABSL_ATTRIBUTE_WEAK __attribute__((weak)) |
| 149 | #define ABSL_HAVE_ATTRIBUTE_WEAK 1 |
| 150 | #else |
| 151 | #define ABSL_ATTRIBUTE_WEAK |
| 152 | #define ABSL_HAVE_ATTRIBUTE_WEAK 0 |
| 153 | #endif |
| 154 | |
| 155 | // ABSL_ATTRIBUTE_NONNULL |
| 156 | // |
| 157 | // Tells the compiler either (a) that a particular function parameter |
| 158 | // should be a non-null pointer, or (b) that all pointer arguments should |
| 159 | // be non-null. |
| 160 | // |
| 161 | // Note: As the GCC manual states, "[s]ince non-static C++ methods |
| 162 | // have an implicit 'this' argument, the arguments of such methods |
| 163 | // should be counted from two, not one." |
| 164 | // |
| 165 | // Args are indexed starting at 1. |
| 166 | // |
| 167 | // For non-static class member functions, the implicit `this` argument |
| 168 | // is arg 1, and the first explicit argument is arg 2. For static class member |
| 169 | // functions, there is no implicit `this`, and the first explicit argument is |
| 170 | // arg 1. |
| 171 | // |
| 172 | // Example: |
| 173 | // |
| 174 | // /* arg_a cannot be null, but arg_b can */ |
| 175 | // void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1); |
| 176 | // |
| 177 | // class C { |
| 178 | // /* arg_a cannot be null, but arg_b can */ |
| 179 | // void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2); |
| 180 | // |
| 181 | // /* arg_a cannot be null, but arg_b can */ |
| 182 | // static void StaticMethod(void* arg_a, void* arg_b) |
| 183 | // ABSL_ATTRIBUTE_NONNULL(1); |
| 184 | // }; |
| 185 | // |
| 186 | // If no arguments are provided, then all pointer arguments should be non-null. |
| 187 | // |
| 188 | // /* No pointer arguments may be null. */ |
| 189 | // void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL(); |
| 190 | // |
| 191 | // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but |
| 192 | // ABSL_ATTRIBUTE_NONNULL does not. |
| 193 | #if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__)) |
| 194 | #define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index))) |
| 195 | #else |
| 196 | #define ABSL_ATTRIBUTE_NONNULL(...) |
| 197 | #endif |
| 198 | |
| 199 | // ABSL_ATTRIBUTE_NORETURN |
| 200 | // |
| 201 | // Tells the compiler that a given function never returns. |
| 202 | // |
| 203 | // Deprecated: Prefer the `[[noreturn]]` attribute standardized by C++11 over |
| 204 | // this macro. |
| 205 | #if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__)) |
| 206 | #define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn)) |
| 207 | #elif defined(_MSC_VER) |
| 208 | #define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn) |
| 209 | #else |
| 210 | #define ABSL_ATTRIBUTE_NORETURN |
| 211 | #endif |
| 212 | |
| 213 | // ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS |
| 214 | // |
| 215 | // Tells the AddressSanitizer (or other memory testing tools) to ignore a given |
| 216 | // function. Useful for cases when a function reads random locations on stack, |
| 217 | // calls _exit from a cloned subprocess, deliberately accesses buffer |
| 218 | // out of bounds or does other scary things with memory. |
| 219 | // NOTE: GCC supports AddressSanitizer(asan) since 4.8. |
| 220 | // https://gcc.gnu.org/gcc-4.8/changes.html |
| 221 | #if defined(ABSL_HAVE_ADDRESS_SANITIZER) && \ |
| 222 | ABSL_HAVE_ATTRIBUTE(no_sanitize_address) |
| 223 | #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) |
| 224 | #elif defined(ABSL_HAVE_ADDRESS_SANITIZER) && defined(_MSC_VER) && \ |
| 225 | _MSC_VER >= 1928 |
| 226 | // https://docs.microsoft.com/en-us/cpp/cpp/no-sanitize-address |
| 227 | #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __declspec(no_sanitize_address) |
| 228 | #elif defined(ABSL_HAVE_HWADDRESS_SANITIZER) && ABSL_HAVE_ATTRIBUTE(no_sanitize) |
| 229 | // HWAddressSanitizer is a sanitizer similar to AddressSanitizer, which uses CPU |
| 230 | // features to detect similar bugs with less CPU and memory overhead. |
| 231 | // NOTE: GCC supports HWAddressSanitizer(hwasan) since 11. |
| 232 | // https://gcc.gnu.org/gcc-11/changes.html |
| 233 | #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS \ |
| 234 | __attribute__((no_sanitize("hwaddress"))) |
| 235 | #else |
| 236 | #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS |
| 237 | #endif |
| 238 | |
| 239 | // ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY |
| 240 | // |
| 241 | // Tells the MemorySanitizer to relax the handling of a given function. All "Use |
| 242 | // of uninitialized value" warnings from such functions will be suppressed, and |
| 243 | // all values loaded from memory will be considered fully initialized. This |
| 244 | // attribute is similar to the ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute |
| 245 | // above, but deals with initialized-ness rather than addressability issues. |
| 246 | // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC. |
| 247 | #if ABSL_HAVE_ATTRIBUTE(no_sanitize_memory) |
| 248 | #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) |
| 249 | #else |
| 250 | #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY |
| 251 | #endif |
| 252 | |
| 253 | // ABSL_ATTRIBUTE_NO_SANITIZE_THREAD |
| 254 | // |
| 255 | // Tells the ThreadSanitizer to not instrument a given function. |
| 256 | // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8. |
| 257 | // https://gcc.gnu.org/gcc-4.8/changes.html |
| 258 | #if ABSL_HAVE_ATTRIBUTE(no_sanitize_thread) |
| 259 | #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread)) |
| 260 | #else |
| 261 | #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD |
| 262 | #endif |
| 263 | |
| 264 | // ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED |
| 265 | // |
| 266 | // Tells the UndefinedSanitizer to ignore a given function. Useful for cases |
| 267 | // where certain behavior (eg. division by zero) is being used intentionally. |
| 268 | // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9. |
| 269 | // https://gcc.gnu.org/gcc-4.9/changes.html |
| 270 | #if ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined) |
| 271 | #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \ |
| 272 | __attribute__((no_sanitize_undefined)) |
| 273 | #elif ABSL_HAVE_ATTRIBUTE(no_sanitize) |
| 274 | #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \ |
| 275 | __attribute__((no_sanitize("undefined"))) |
| 276 | #else |
| 277 | #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED |
| 278 | #endif |
| 279 | |
| 280 | // ABSL_ATTRIBUTE_NO_SANITIZE_CFI |
| 281 | // |
| 282 | // Tells the ControlFlowIntegrity sanitizer to not instrument a given function. |
| 283 | // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details. |
| 284 | #if ABSL_HAVE_ATTRIBUTE(no_sanitize) && defined(__llvm__) |
| 285 | #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi"))) |
| 286 | #else |
| 287 | #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI |
| 288 | #endif |
| 289 | |
| 290 | // ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK |
| 291 | // |
| 292 | // Tells the SafeStack to not instrument a given function. |
| 293 | // See https://clang.llvm.org/docs/SafeStack.html for details. |
| 294 | #if ABSL_HAVE_ATTRIBUTE(no_sanitize) |
| 295 | #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \ |
| 296 | __attribute__((no_sanitize("safe-stack"))) |
| 297 | #else |
| 298 | #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK |
| 299 | #endif |
| 300 | |
| 301 | // ABSL_ATTRIBUTE_RETURNS_NONNULL |
| 302 | // |
| 303 | // Tells the compiler that a particular function never returns a null pointer. |
| 304 | #if ABSL_HAVE_ATTRIBUTE(returns_nonnull) |
| 305 | #define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull)) |
| 306 | #else |
| 307 | #define ABSL_ATTRIBUTE_RETURNS_NONNULL |
| 308 | #endif |
| 309 | |
| 310 | // ABSL_HAVE_ATTRIBUTE_SECTION |
| 311 | // |
| 312 | // Indicates whether labeled sections are supported. Weak symbol support is |
| 313 | // a prerequisite. Labeled sections are not supported on Darwin/iOS. |
| 314 | #ifdef ABSL_HAVE_ATTRIBUTE_SECTION |
| 315 | #error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set |
| 316 | #elif (ABSL_HAVE_ATTRIBUTE(section) || \ |
| 317 | (defined(__GNUC__) && !defined(__clang__))) && \ |
| 318 | !defined(__APPLE__) && ABSL_HAVE_ATTRIBUTE_WEAK |
| 319 | #define ABSL_HAVE_ATTRIBUTE_SECTION 1 |
| 320 | |
| 321 | // ABSL_ATTRIBUTE_SECTION |
| 322 | // |
| 323 | // Tells the compiler/linker to put a given function into a section and define |
| 324 | // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section. |
| 325 | // This functionality is supported by GNU linker. Any function annotated with |
| 326 | // `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into |
| 327 | // whatever section its caller is placed into. |
| 328 | // |
| 329 | #ifndef ABSL_ATTRIBUTE_SECTION |
| 330 | #define ABSL_ATTRIBUTE_SECTION(name) \ |
| 331 | __attribute__((section(#name))) __attribute__((noinline)) |
| 332 | #endif |
| 333 | |
| 334 | // ABSL_ATTRIBUTE_SECTION_VARIABLE |
| 335 | // |
| 336 | // Tells the compiler/linker to put a given variable into a section and define |
| 337 | // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section. |
| 338 | // This functionality is supported by GNU linker. |
| 339 | #ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE |
| 340 | #ifdef _AIX |
| 341 | // __attribute__((section(#name))) on AIX is achieved by using the `.csect` |
| 342 | // psudo op which includes an additional integer as part of its syntax indcating |
| 343 | // alignment. If data fall under different alignments then you might get a |
| 344 | // compilation error indicating a `Section type conflict`. |
| 345 | #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) |
| 346 | #else |
| 347 | #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name))) |
| 348 | #endif |
| 349 | #endif |
| 350 | |
| 351 | // ABSL_DECLARE_ATTRIBUTE_SECTION_VARS |
| 352 | // |
| 353 | // A weak section declaration to be used as a global declaration |
| 354 | // for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link |
| 355 | // even without functions with ABSL_ATTRIBUTE_SECTION(name). |
| 356 | // ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's |
| 357 | // a no-op on ELF but not on Mach-O. |
| 358 | // |
| 359 | #ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS |
| 360 | #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \ |
| 361 | extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \ |
| 362 | extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK |
| 363 | #endif |
| 364 | #ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS |
| 365 | #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) |
| 366 | #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) |
| 367 | #endif |
| 368 | |
| 369 | // ABSL_ATTRIBUTE_SECTION_START |
| 370 | // |
| 371 | // Returns `void*` pointers to start/end of a section of code with |
| 372 | // functions having ABSL_ATTRIBUTE_SECTION(name). |
| 373 | // Returns 0 if no such functions exist. |
| 374 | // One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and |
| 375 | // link. |
| 376 | // |
| 377 | #define ABSL_ATTRIBUTE_SECTION_START(name) \ |
| 378 | (reinterpret_cast<void *>(__start_##name)) |
| 379 | #define ABSL_ATTRIBUTE_SECTION_STOP(name) \ |
| 380 | (reinterpret_cast<void *>(__stop_##name)) |
| 381 | |
| 382 | #else // !ABSL_HAVE_ATTRIBUTE_SECTION |
| 383 | |
| 384 | #define ABSL_HAVE_ATTRIBUTE_SECTION 0 |
| 385 | |
| 386 | // provide dummy definitions |
| 387 | #define ABSL_ATTRIBUTE_SECTION(name) |
| 388 | #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) |
| 389 | #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) |
| 390 | #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) |
| 391 | #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) |
| 392 | #define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0)) |
| 393 | #define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0)) |
| 394 | |
| 395 | #endif // ABSL_ATTRIBUTE_SECTION |
| 396 | |
| 397 | // ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 398 | // |
| 399 | // Support for aligning the stack on 32-bit x86. |
| 400 | #if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \ |
| 401 | (defined(__GNUC__) && !defined(__clang__)) |
| 402 | #if defined(__i386__) |
| 403 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \ |
| 404 | __attribute__((force_align_arg_pointer)) |
| 405 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
| 406 | #elif defined(__x86_64__) |
| 407 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1) |
| 408 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 409 | #else // !__i386__ && !__x86_64 |
| 410 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
| 411 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 412 | #endif // __i386__ |
| 413 | #else |
| 414 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 415 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
| 416 | #endif |
| 417 | |
| 418 | // ABSL_MUST_USE_RESULT |
| 419 | // |
| 420 | // Tells the compiler to warn about unused results. |
| 421 | // |
| 422 | // For code or headers that are assured to only build with C++17 and up, prefer |
| 423 | // just using the standard `[[nodiscard]]` directly over this macro. |
| 424 | // |
| 425 | // When annotating a function, it must appear as the first part of the |
| 426 | // declaration or definition. The compiler will warn if the return value from |
| 427 | // such a function is unused: |
| 428 | // |
| 429 | // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket(); |
| 430 | // AllocateSprocket(); // Triggers a warning. |
| 431 | // |
| 432 | // When annotating a class, it is equivalent to annotating every function which |
| 433 | // returns an instance. |
| 434 | // |
| 435 | // class ABSL_MUST_USE_RESULT Sprocket {}; |
| 436 | // Sprocket(); // Triggers a warning. |
| 437 | // |
| 438 | // Sprocket MakeSprocket(); |
| 439 | // MakeSprocket(); // Triggers a warning. |
| 440 | // |
| 441 | // Note that references and pointers are not instances: |
| 442 | // |
| 443 | // Sprocket* SprocketPointer(); |
| 444 | // SprocketPointer(); // Does *not* trigger a warning. |
| 445 | // |
| 446 | // ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result |
| 447 | // warning. For that, warn_unused_result is used only for clang but not for gcc. |
| 448 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 |
| 449 | // |
| 450 | // Note: past advice was to place the macro after the argument list. |
| 451 | // |
| 452 | // TODO(b/176172494): Use ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) when all code is |
| 453 | // compliant with the stricter [[nodiscard]]. |
| 454 | #if defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result) |
| 455 | #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result)) |
| 456 | #else |
| 457 | #define ABSL_MUST_USE_RESULT |
| 458 | #endif |
| 459 | |
| 460 | // ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD |
| 461 | // |
| 462 | // Tells GCC that a function is hot or cold. GCC can use this information to |
| 463 | // improve static analysis, i.e. a conditional branch to a cold function |
| 464 | // is likely to be not-taken. |
| 465 | // This annotation is used for function declarations. |
| 466 | // |
| 467 | // Example: |
| 468 | // |
| 469 | // int foo() ABSL_ATTRIBUTE_HOT; |
| 470 | #if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__)) |
| 471 | #define ABSL_ATTRIBUTE_HOT __attribute__((hot)) |
| 472 | #else |
| 473 | #define ABSL_ATTRIBUTE_HOT |
| 474 | #endif |
| 475 | |
| 476 | #if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__)) |
| 477 | #define ABSL_ATTRIBUTE_COLD __attribute__((cold)) |
| 478 | #else |
| 479 | #define ABSL_ATTRIBUTE_COLD |
| 480 | #endif |
| 481 | |
| 482 | // ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS |
| 483 | // |
| 484 | // We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT |
| 485 | // macro used as an attribute to mark functions that must always or never be |
| 486 | // instrumented by XRay. Currently, this is only supported in Clang/LLVM. |
| 487 | // |
| 488 | // For reference on the LLVM XRay instrumentation, see |
| 489 | // http://llvm.org/docs/XRay.html. |
| 490 | // |
| 491 | // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration |
| 492 | // will always get the XRay instrumentation sleds. These sleds may introduce |
| 493 | // some binary size and runtime overhead and must be used sparingly. |
| 494 | // |
| 495 | // These attributes only take effect when the following conditions are met: |
| 496 | // |
| 497 | // * The file/target is built in at least C++11 mode, with a Clang compiler |
| 498 | // that supports XRay attributes. |
| 499 | // * The file/target is built with the -fxray-instrument flag set for the |
| 500 | // Clang/LLVM compiler. |
| 501 | // * The function is defined in the translation unit (the compiler honors the |
| 502 | // attribute in either the definition or the declaration, and must match). |
| 503 | // |
| 504 | // There are cases when, even when building with XRay instrumentation, users |
| 505 | // might want to control specifically which functions are instrumented for a |
| 506 | // particular build using special-case lists provided to the compiler. These |
| 507 | // special case lists are provided to Clang via the |
| 508 | // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The |
| 509 | // attributes in source take precedence over these special-case lists. |
| 510 | // |
| 511 | // To disable the XRay attributes at build-time, users may define |
| 512 | // ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific |
| 513 | // packages/targets, as this may lead to conflicting definitions of functions at |
| 514 | // link-time. |
| 515 | // |
| 516 | // XRay isn't currently supported on Android: |
| 517 | // https://github.com/android/ndk/issues/368 |
| 518 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \ |
| 519 | !defined(ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__) |
| 520 | #define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]] |
| 521 | #define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]] |
| 522 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args) |
| 523 | #define ABSL_XRAY_LOG_ARGS(N) \ |
| 524 | [[clang::xray_always_instrument, clang::xray_log_args(N)]] |
| 525 | #else |
| 526 | #define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]] |
| 527 | #endif |
| 528 | #else |
| 529 | #define ABSL_XRAY_ALWAYS_INSTRUMENT |
| 530 | #define ABSL_XRAY_NEVER_INSTRUMENT |
| 531 | #define ABSL_XRAY_LOG_ARGS(N) |
| 532 | #endif |
| 533 | |
| 534 | // ABSL_ATTRIBUTE_REINITIALIZES |
| 535 | // |
| 536 | // Indicates that a member function reinitializes the entire object to a known |
| 537 | // state, independent of the previous state of the object. |
| 538 | // |
| 539 | // The clang-tidy check bugprone-use-after-move allows member functions marked |
| 540 | // with this attribute to be called on objects that have been moved from; |
| 541 | // without the attribute, this would result in a use-after-move warning. |
| 542 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes) |
| 543 | #define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]] |
| 544 | #else |
| 545 | #define ABSL_ATTRIBUTE_REINITIALIZES |
| 546 | #endif |
| 547 | |
| 548 | // ----------------------------------------------------------------------------- |
| 549 | // Variable Attributes |
| 550 | // ----------------------------------------------------------------------------- |
| 551 | |
| 552 | // ABSL_ATTRIBUTE_UNUSED |
| 553 | // |
| 554 | // Prevents the compiler from complaining about variables that appear unused. |
| 555 | // |
| 556 | // For code or headers that are assured to only build with C++17 and up, prefer |
| 557 | // just using the standard '[[maybe_unused]]' directly over this macro. |
| 558 | // |
| 559 | // Due to differences in positioning requirements between the old, compiler |
| 560 | // specific __attribute__ syntax and the now standard [[maybe_unused]], this |
| 561 | // macro does not attempt to take advantage of '[[maybe_unused]]'. |
| 562 | #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__)) |
| 563 | #undef ABSL_ATTRIBUTE_UNUSED |
| 564 | #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__)) |
| 565 | #else |
| 566 | #define ABSL_ATTRIBUTE_UNUSED |
| 567 | #endif |
| 568 | |
| 569 | // ABSL_ATTRIBUTE_INITIAL_EXEC |
| 570 | // |
| 571 | // Tells the compiler to use "initial-exec" mode for a thread-local variable. |
| 572 | // See http://people.redhat.com/drepper/tls.pdf for the gory details. |
| 573 | #if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__)) |
| 574 | #define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec"))) |
| 575 | #else |
| 576 | #define ABSL_ATTRIBUTE_INITIAL_EXEC |
| 577 | #endif |
| 578 | |
| 579 | // ABSL_ATTRIBUTE_PACKED |
| 580 | // |
| 581 | // Instructs the compiler not to use natural alignment for a tagged data |
| 582 | // structure, but instead to reduce its alignment to 1. |
| 583 | // |
| 584 | // Therefore, DO NOT APPLY THIS ATTRIBUTE TO STRUCTS CONTAINING ATOMICS. Doing |
| 585 | // so can cause atomic variables to be mis-aligned and silently violate |
| 586 | // atomicity on x86. |
| 587 | // |
| 588 | // This attribute can either be applied to members of a structure or to a |
| 589 | // structure in its entirety. Applying this attribute (judiciously) to a |
| 590 | // structure in its entirety to optimize the memory footprint of very |
| 591 | // commonly-used structs is fine. Do not apply this attribute to a structure in |
| 592 | // its entirety if the purpose is to control the offsets of the members in the |
| 593 | // structure. Instead, apply this attribute only to structure members that need |
| 594 | // it. |
| 595 | // |
| 596 | // When applying ABSL_ATTRIBUTE_PACKED only to specific structure members the |
| 597 | // natural alignment of structure members not annotated is preserved. Aligned |
| 598 | // member accesses are faster than non-aligned member accesses even if the |
| 599 | // targeted microprocessor supports non-aligned accesses. |
| 600 | #if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__)) |
| 601 | #define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__)) |
| 602 | #else |
| 603 | #define ABSL_ATTRIBUTE_PACKED |
| 604 | #endif |
| 605 | |
| 606 | // ABSL_ATTRIBUTE_FUNC_ALIGN |
| 607 | // |
| 608 | // Tells the compiler to align the function start at least to certain |
| 609 | // alignment boundary |
| 610 | #if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__)) |
| 611 | #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes))) |
| 612 | #else |
| 613 | #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) |
| 614 | #endif |
| 615 | |
| 616 | // ABSL_FALLTHROUGH_INTENDED |
| 617 | // |
| 618 | // Annotates implicit fall-through between switch labels, allowing a case to |
| 619 | // indicate intentional fallthrough and turn off warnings about any lack of a |
| 620 | // `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by |
| 621 | // a semicolon and can be used in most places where `break` can, provided that |
| 622 | // no statements exist between it and the next switch label. |
| 623 | // |
| 624 | // Example: |
| 625 | // |
| 626 | // switch (x) { |
| 627 | // case 40: |
| 628 | // case 41: |
| 629 | // if (truth_is_out_there) { |
| 630 | // ++x; |
| 631 | // ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations |
| 632 | // // in comments |
| 633 | // } else { |
| 634 | // return x; |
| 635 | // } |
| 636 | // case 42: |
| 637 | // ... |
| 638 | // |
| 639 | // Notes: When supported, GCC and Clang can issue a warning on switch labels |
| 640 | // with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See |
| 641 | // clang documentation on language extensions for details: |
| 642 | // https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough |
| 643 | // |
| 644 | // When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro has |
| 645 | // no effect on diagnostics. In any case this macro has no effect on runtime |
| 646 | // behavior and performance of code. |
| 647 | |
| 648 | #ifdef ABSL_FALLTHROUGH_INTENDED |
| 649 | #error "ABSL_FALLTHROUGH_INTENDED should not be defined." |
| 650 | #elif ABSL_HAVE_CPP_ATTRIBUTE(fallthrough) |
| 651 | #define ABSL_FALLTHROUGH_INTENDED [[fallthrough]] |
| 652 | #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough) |
| 653 | #define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]] |
| 654 | #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough) |
| 655 | #define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]] |
| 656 | #else |
| 657 | #define ABSL_FALLTHROUGH_INTENDED \ |
| 658 | do { \ |
| 659 | } while (0) |
| 660 | #endif |
| 661 | |
| 662 | // ABSL_DEPRECATED() |
| 663 | // |
| 664 | // Marks a deprecated class, struct, enum, function, method and variable |
| 665 | // declarations. The macro argument is used as a custom diagnostic message (e.g. |
| 666 | // suggestion of a better alternative). |
| 667 | // |
| 668 | // For code or headers that are assured to only build with C++14 and up, prefer |
| 669 | // just using the standard `[[deprecated("message")]]` directly over this macro. |
| 670 | // |
| 671 | // Examples: |
| 672 | // |
| 673 | // class ABSL_DEPRECATED("Use Bar instead") Foo {...}; |
| 674 | // |
| 675 | // ABSL_DEPRECATED("Use Baz() instead") void Bar() {...} |
| 676 | // |
| 677 | // template <typename T> |
| 678 | // ABSL_DEPRECATED("Use DoThat() instead") |
| 679 | // void DoThis(); |
| 680 | // |
| 681 | // enum FooEnum { |
| 682 | // kBar ABSL_DEPRECATED("Use kBaz instead"), |
| 683 | // }; |
| 684 | // |
| 685 | // Every usage of a deprecated entity will trigger a warning when compiled with |
| 686 | // GCC/Clang's `-Wdeprecated-declarations` option. Google's production toolchain |
| 687 | // turns this warning off by default, instead relying on clang-tidy to report |
| 688 | // new uses of deprecated code. |
| 689 | #if ABSL_HAVE_ATTRIBUTE(deprecated) |
| 690 | #define ABSL_DEPRECATED(message) __attribute__((deprecated(message))) |
| 691 | #else |
| 692 | #define ABSL_DEPRECATED(message) |
| 693 | #endif |
| 694 | |
| 695 | // When deprecating Abseil code, it is sometimes necessary to turn off the |
| 696 | // warning within Abseil, until the deprecated code is actually removed. The |
| 697 | // deprecated code can be surrounded with these directives to achieve that |
| 698 | // result. |
| 699 | // |
| 700 | // class ABSL_DEPRECATED("Use Bar instead") Foo; |
| 701 | // |
| 702 | // ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
| 703 | // Baz ComputeBazFromFoo(Foo f); |
| 704 | // ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
| 705 | #if defined(__GNUC__) || defined(__clang__) |
| 706 | // Clang also supports these GCC pragmas. |
| 707 | #define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING \ |
| 708 | _Pragma("GCC diagnostic push") \ |
| 709 | _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") |
| 710 | #define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING \ |
| 711 | _Pragma("GCC diagnostic pop") |
| 712 | #elif defined(_MSC_VER) |
| 713 | #define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING \ |
| 714 | _Pragma("warning(push)") _Pragma("warning(disable: 4996)") |
| 715 | #define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING \ |
| 716 | _Pragma("warning(pop)") |
| 717 | #else |
| 718 | #define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
| 719 | #define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
| 720 | #endif // defined(__GNUC__) || defined(__clang__) |
| 721 | |
| 722 | // ABSL_CONST_INIT |
| 723 | // |
| 724 | // A variable declaration annotated with the `ABSL_CONST_INIT` attribute will |
| 725 | // not compile (on supported platforms) unless the variable has a constant |
| 726 | // initializer. This is useful for variables with static and thread storage |
| 727 | // duration, because it guarantees that they will not suffer from the so-called |
| 728 | // "static init order fiasco". |
| 729 | // |
| 730 | // This attribute must be placed on the initializing declaration of the |
| 731 | // variable. Some compilers will give a -Wmissing-constinit warning when this |
| 732 | // attribute is placed on some other declaration but missing from the |
| 733 | // initializing declaration. |
| 734 | // |
| 735 | // In some cases (notably with thread_local variables), `ABSL_CONST_INIT` can |
| 736 | // also be used in a non-initializing declaration to tell the compiler that a |
| 737 | // variable is already initialized, reducing overhead that would otherwise be |
| 738 | // incurred by a hidden guard variable. Thus annotating all declarations with |
| 739 | // this attribute is recommended to potentially enhance optimization. |
| 740 | // |
| 741 | // Example: |
| 742 | // |
| 743 | // class MyClass { |
| 744 | // public: |
| 745 | // ABSL_CONST_INIT static MyType my_var; |
| 746 | // }; |
| 747 | // |
| 748 | // ABSL_CONST_INIT MyType MyClass::my_var = MakeMyType(...); |
| 749 | // |
| 750 | // For code or headers that are assured to only build with C++20 and up, prefer |
| 751 | // just using the standard `constinit` keyword directly over this macro. |
| 752 | // |
| 753 | // Note that this attribute is redundant if the variable is declared constexpr. |
| 754 | #if defined(__cpp_constinit) && __cpp_constinit >= 201907L |
| 755 | #define ABSL_CONST_INIT constinit |
| 756 | #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) |
| 757 | #define ABSL_CONST_INIT [[clang::require_constant_initialization]] |
| 758 | #else |
| 759 | #define ABSL_CONST_INIT |
| 760 | #endif |
| 761 | |
| 762 | // ABSL_ATTRIBUTE_PURE_FUNCTION |
| 763 | // |
| 764 | // ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure" |
| 765 | // functions. A function is pure if its return value is only a function of its |
| 766 | // arguments. The pure attribute prohibits a function from modifying the state |
| 767 | // of the program that is observable by means other than inspecting the |
| 768 | // function's return value. Declaring such functions with the pure attribute |
| 769 | // allows the compiler to avoid emitting some calls in repeated invocations of |
| 770 | // the function with the same argument values. |
| 771 | // |
| 772 | // Example: |
| 773 | // |
| 774 | // ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(Time t); |
| 775 | #if ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure) |
| 776 | #define ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]] |
| 777 | #elif ABSL_HAVE_ATTRIBUTE(pure) |
| 778 | #define ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure)) |
| 779 | #else |
| 780 | // If the attribute isn't defined, we'll fallback to ABSL_MUST_USE_RESULT since |
| 781 | // pure functions are useless if its return is ignored. |
| 782 | #define ABSL_ATTRIBUTE_PURE_FUNCTION ABSL_MUST_USE_RESULT |
| 783 | #endif |
| 784 | |
| 785 | // ABSL_ATTRIBUTE_CONST_FUNCTION |
| 786 | // |
| 787 | // ABSL_ATTRIBUTE_CONST_FUNCTION is used to annotate declarations of "const" |
| 788 | // functions. A const function is similar to a pure function, with one |
| 789 | // exception: Pure functions may return value that depend on a non-volatile |
| 790 | // object that isn't provided as a function argument, while the const function |
| 791 | // is guaranteed to return the same result given the same arguments. |
| 792 | // |
| 793 | // Example: |
| 794 | // |
| 795 | // ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Milliseconds(Duration d); |
| 796 | #if defined(_MSC_VER) && !defined(__clang__) |
| 797 | // Put the MSVC case first since MSVC seems to parse const as a C++ keyword. |
| 798 | #define ABSL_ATTRIBUTE_CONST_FUNCTION ABSL_ATTRIBUTE_PURE_FUNCTION |
| 799 | #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::const) |
| 800 | #define ABSL_ATTRIBUTE_CONST_FUNCTION [[gnu::const]] |
| 801 | #elif ABSL_HAVE_ATTRIBUTE(const) |
| 802 | #define ABSL_ATTRIBUTE_CONST_FUNCTION __attribute__((const)) |
| 803 | #else |
| 804 | // Since const functions are more restrictive pure function, we'll fallback to a |
| 805 | // pure function if the const attribute is not handled. |
| 806 | #define ABSL_ATTRIBUTE_CONST_FUNCTION ABSL_ATTRIBUTE_PURE_FUNCTION |
| 807 | #endif |
| 808 | |
| 809 | // ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function |
| 810 | // parameter or implicit object parameter is retained by the return value of the |
| 811 | // annotated function (or, for a parameter of a constructor, in the value of the |
| 812 | // constructed object). This attribute causes warnings to be produced if a |
| 813 | // temporary object does not live long enough. |
| 814 | // |
| 815 | // When applied to a reference parameter, the referenced object is assumed to be |
| 816 | // retained by the return value of the function. When applied to a non-reference |
| 817 | // parameter (for example, a pointer or a class type), all temporaries |
| 818 | // referenced by the parameter are assumed to be retained by the return value of |
| 819 | // the function. |
| 820 | // |
| 821 | // See also the upstream documentation: |
| 822 | // https://clang.llvm.org/docs/AttributeReference.html#lifetimebound |
| 823 | // https://learn.microsoft.com/en-us/cpp/code-quality/c26816?view=msvc-170 |
| 824 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound) |
| 825 | #define ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]] |
| 826 | #elif ABSL_HAVE_CPP_ATTRIBUTE(msvc::lifetimebound) |
| 827 | #define ABSL_ATTRIBUTE_LIFETIME_BOUND [[msvc::lifetimebound]] |
| 828 | #elif ABSL_HAVE_ATTRIBUTE(lifetimebound) |
| 829 | #define ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound)) |
| 830 | #else |
| 831 | #define ABSL_ATTRIBUTE_LIFETIME_BOUND |
| 832 | #endif |
| 833 | |
| 834 | // Internal attribute; name and documentation TBD. |
| 835 | // |
| 836 | // See the upstream documentation: |
| 837 | // https://clang.llvm.org/docs/AttributeReference.html#lifetime_capture_by |
| 838 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetime_capture_by) |
| 839 | #define ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(Owner) \ |
| 840 | [[clang::lifetime_capture_by(Owner)]] |
| 841 | #else |
| 842 | #define ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(Owner) |
| 843 | #endif |
| 844 | |
| 845 | // ABSL_ATTRIBUTE_VIEW indicates that a type is solely a "view" of data that it |
| 846 | // points to, similarly to a span, string_view, or other non-owning reference |
| 847 | // type. |
| 848 | // This enables diagnosing certain lifetime issues similar to those enabled by |
| 849 | // ABSL_ATTRIBUTE_LIFETIME_BOUND, such as: |
| 850 | // |
| 851 | // struct ABSL_ATTRIBUTE_VIEW StringView { |
| 852 | // template<class R> |
| 853 | // StringView(const R&); |
| 854 | // }; |
| 855 | // |
| 856 | // StringView f(std::string s) { |
| 857 | // return s; // warning: address of stack memory returned |
| 858 | // } |
| 859 | // |
| 860 | // We disable this on Clang versions < 13 because of the following |
| 861 | // false-positive: |
| 862 | // |
| 863 | // absl::string_view f(absl::optional<absl::string_view> sv) { return *sv; } |
| 864 | // |
| 865 | // See the following links for details: |
| 866 | // https://reviews.llvm.org/D64448 |
| 867 | // https://lists.llvm.org/pipermail/cfe-dev/2018-November/060355.html |
| 868 | #if ABSL_HAVE_CPP_ATTRIBUTE(gsl::Pointer) && \ |
| 869 | (!defined(__clang_major__) || __clang_major__ >= 13) |
| 870 | #define ABSL_ATTRIBUTE_VIEW [[gsl::Pointer]] |
| 871 | #else |
| 872 | #define ABSL_ATTRIBUTE_VIEW |
| 873 | #endif |
| 874 | |
| 875 | // ABSL_ATTRIBUTE_OWNER indicates that a type is a container, smart pointer, or |
| 876 | // similar class that owns all the data that it points to. |
| 877 | // This enables diagnosing certain lifetime issues similar to those enabled by |
| 878 | // ABSL_ATTRIBUTE_LIFETIME_BOUND, such as: |
| 879 | // |
| 880 | // struct ABSL_ATTRIBUTE_VIEW StringView { |
| 881 | // template<class R> |
| 882 | // StringView(const R&); |
| 883 | // }; |
| 884 | // |
| 885 | // struct ABSL_ATTRIBUTE_OWNER String {}; |
| 886 | // |
| 887 | // StringView f(String s) { |
| 888 | // return s; // warning: address of stack memory returned |
| 889 | // } |
| 890 | // |
| 891 | // We disable this on Clang versions < 13 because of the following |
| 892 | // false-positive: |
| 893 | // |
| 894 | // absl::string_view f(absl::optional<absl::string_view> sv) { return *sv; } |
| 895 | // |
| 896 | // See the following links for details: |
| 897 | // https://reviews.llvm.org/D64448 |
| 898 | // https://lists.llvm.org/pipermail/cfe-dev/2018-November/060355.html |
| 899 | #if ABSL_HAVE_CPP_ATTRIBUTE(gsl::Owner) && \ |
| 900 | (!defined(__clang_major__) || __clang_major__ >= 13) |
| 901 | #define ABSL_ATTRIBUTE_OWNER [[gsl::Owner]] |
| 902 | #else |
| 903 | #define ABSL_ATTRIBUTE_OWNER |
| 904 | #endif |
| 905 | |
| 906 | // ABSL_ATTRIBUTE_TRIVIAL_ABI |
| 907 | // Indicates that a type is "trivially relocatable" -- meaning it can be |
| 908 | // relocated without invoking the constructor/destructor, using a form of move |
| 909 | // elision. |
| 910 | // |
| 911 | // From a memory safety point of view, putting aside destructor ordering, it's |
| 912 | // safe to apply ABSL_ATTRIBUTE_TRIVIAL_ABI if an object's location |
| 913 | // can change over the course of its lifetime: if a constructor can be run one |
| 914 | // place, and then the object magically teleports to another place where some |
| 915 | // methods are run, and then the object teleports to yet another place where it |
| 916 | // is destroyed. This is notably not true for self-referential types, where the |
| 917 | // move-constructor must keep the self-reference up to date. If the type changed |
| 918 | // location without invoking the move constructor, it would have a dangling |
| 919 | // self-reference. |
| 920 | // |
| 921 | // The use of this teleporting machinery means that the number of paired |
| 922 | // move/destroy operations can change, and so it is a bad idea to apply this to |
| 923 | // a type meant to count the number of moves. |
| 924 | // |
| 925 | // Warning: applying this can, rarely, break callers. Objects passed by value |
| 926 | // will be destroyed at the end of the call, instead of the end of the |
| 927 | // full-expression containing the call. In addition, it changes the ABI |
| 928 | // of functions accepting this type by value (e.g. to pass in registers). |
| 929 | // |
| 930 | // See also the upstream documentation: |
| 931 | // https://clang.llvm.org/docs/AttributeReference.html#trivial-abi |
| 932 | // |
| 933 | // b/321691395 - This is currently disabled in open-source builds since |
| 934 | // compiler support differs. If system libraries compiled with GCC are mixed |
| 935 | // with libraries compiled with Clang, types will have different ideas about |
| 936 | // their ABI, leading to hard to debug crashes. |
| 937 | #define ABSL_ATTRIBUTE_TRIVIAL_ABI |
| 938 | |
| 939 | // ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS |
| 940 | // |
| 941 | // Indicates a data member can be optimized to occupy no space (if it is empty) |
| 942 | // and/or its tail padding can be used for other members. |
| 943 | // |
| 944 | // For code that is assured to only build with C++20 or later, prefer using |
| 945 | // the standard attribute `[[no_unique_address]]` directly instead of this |
| 946 | // macro. |
| 947 | // |
| 948 | // https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/#c20-no_unique_address |
| 949 | // Current versions of MSVC have disabled `[[no_unique_address]]` since it |
| 950 | // breaks ABI compatibility, but offers `[[msvc::no_unique_address]]` for |
| 951 | // situations when it can be assured that it is desired. Since Abseil does not |
| 952 | // claim ABI compatibility in mixed builds, we can offer it unconditionally. |
| 953 | #if defined(_MSC_VER) && _MSC_VER >= 1929 |
| 954 | #define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] |
| 955 | #elif ABSL_HAVE_CPP_ATTRIBUTE(no_unique_address) |
| 956 | #define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS [[no_unique_address]] |
| 957 | #else |
| 958 | #define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS |
| 959 | #endif |
| 960 | |
| 961 | // ABSL_ATTRIBUTE_UNINITIALIZED |
| 962 | // |
| 963 | // GCC and Clang support a flag `-ftrivial-auto-var-init=<option>` (<option> |
| 964 | // can be "zero" or "pattern") that can be used to initialize automatic stack |
| 965 | // variables. Variables with this attribute will be left uninitialized, |
| 966 | // overriding the compiler flag. |
| 967 | // |
| 968 | // See https://clang.llvm.org/docs/AttributeReference.html#uninitialized |
| 969 | // and https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-uninitialized-variable-attribute |
| 970 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::uninitialized) |
| 971 | #define ABSL_ATTRIBUTE_UNINITIALIZED [[clang::uninitialized]] |
| 972 | #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::uninitialized) |
| 973 | #define ABSL_ATTRIBUTE_UNINITIALIZED [[gnu::uninitialized]] |
| 974 | #elif ABSL_HAVE_ATTRIBUTE(uninitialized) |
| 975 | #define ABSL_ATTRIBUTE_UNINITIALIZED __attribute__((uninitialized)) |
| 976 | #else |
| 977 | #define ABSL_ATTRIBUTE_UNINITIALIZED |
| 978 | #endif |
| 979 | |
| 980 | // ABSL_ATTRIBUTE_WARN_UNUSED |
| 981 | // |
| 982 | // Compilers routinely warn about trivial variables that are unused. For |
| 983 | // non-trivial types, this warning is suppressed since the |
| 984 | // constructor/destructor may be intentional and load-bearing, for example, with |
| 985 | // a RAII scoped lock. |
| 986 | // |
| 987 | // For example: |
| 988 | // |
| 989 | // class ABSL_ATTRIBUTE_WARN_UNUSED MyType { |
| 990 | // public: |
| 991 | // MyType(); |
| 992 | // ~MyType(); |
| 993 | // }; |
| 994 | // |
| 995 | // void foo() { |
| 996 | // // Warns with ABSL_ATTRIBUTE_WARN_UNUSED attribute present. |
| 997 | // MyType unused; |
| 998 | // } |
| 999 | // |
| 1000 | // See https://clang.llvm.org/docs/AttributeReference.html#warn-unused and |
| 1001 | // https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#index-warn_005funused-type-attribute |
| 1002 | #if ABSL_HAVE_CPP_ATTRIBUTE(gnu::warn_unused) |
| 1003 | #define ABSL_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]] |
| 1004 | #else |
| 1005 | #define ABSL_ATTRIBUTE_WARN_UNUSED |
| 1006 | #endif |
| 1007 | |
| 1008 | #endif // ABSL_BASE_ATTRIBUTES_H_ |
| 1009 | |