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 #ifndef ABSL_BASE_ATTRIBUTES_H_ 35 #define ABSL_BASE_ATTRIBUTES_H_ 36 37 #include "absl/base/config.h" 38 39 // ABSL_HAVE_ATTRIBUTE 40 // 41 // A function-like feature checking macro that is a wrapper around 42 // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a 43 // nonzero constant integer if the attribute is supported or 0 if not. 44 // 45 // It evaluates to zero if `__has_attribute` is not defined by the compiler. 46 // 47 // GCC: https://gcc.gnu.org/gcc-5/changes.html 48 // Clang: https://clang.llvm.org/docs/LanguageExtensions.html 49 #ifdef __has_attribute 50 #define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x) 51 #else 52 #define ABSL_HAVE_ATTRIBUTE(x) 0 53 #endif 54 55 // ABSL_HAVE_CPP_ATTRIBUTE 56 // 57 // A function-like feature checking macro that accepts C++11 style attributes. 58 // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6 59 // (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't 60 // find `__has_cpp_attribute`, will evaluate to 0. 61 #if defined(__cplusplus) && defined(__has_cpp_attribute) 62 // NOTE: requiring __cplusplus above should not be necessary, but 63 // works around https://bugs.llvm.org/show_bug.cgi?id=23435. 64 #define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) 65 #else 66 #define ABSL_HAVE_CPP_ATTRIBUTE(x) 0 67 #endif 68 69 // ----------------------------------------------------------------------------- 70 // Function Attributes 71 // ----------------------------------------------------------------------------- 72 // 73 // GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 74 // Clang: https://clang.llvm.org/docs/AttributeReference.html 75 76 // ABSL_PRINTF_ATTRIBUTE 77 // ABSL_SCANF_ATTRIBUTE 78 // 79 // Tells the compiler to perform `printf` format string checking if the 80 // compiler supports it; see the 'format' attribute in 81 // <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>. 82 // 83 // Note: As the GCC manual states, "[s]ince non-static C++ methods 84 // have an implicit 'this' argument, the arguments of such methods 85 // should be counted from two, not one." 86 #if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__)) 87 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \ 88 __attribute__((__format__(__printf__, string_index, first_to_check))) 89 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \ 90 __attribute__((__format__(__scanf__, string_index, first_to_check))) 91 #else 92 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) 93 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) 94 #endif 95 96 // ABSL_ATTRIBUTE_ALWAYS_INLINE 97 // ABSL_ATTRIBUTE_NOINLINE 98 // 99 // Forces functions to either inline or not inline. Introduced in gcc 3.1. 100 #if ABSL_HAVE_ATTRIBUTE(always_inline) || \ 101 (defined(__GNUC__) && !defined(__clang__)) 102 #define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) 103 #define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1 104 #else 105 #define ABSL_ATTRIBUTE_ALWAYS_INLINE 106 #endif 107 108 #if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__)) 109 #define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline)) 110 #define ABSL_HAVE_ATTRIBUTE_NOINLINE 1 111 #else 112 #define ABSL_ATTRIBUTE_NOINLINE 113 #endif 114 115 // ABSL_ATTRIBUTE_NO_TAIL_CALL 116 // 117 // Prevents the compiler from optimizing away stack frames for functions which 118 // end in a call to another function. 119 #if ABSL_HAVE_ATTRIBUTE(disable_tail_calls) 120 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 121 #define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls)) 122 #elif defined(__GNUC__) && !defined(__clang__) && !defined(__e2k__) 123 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 124 #define ABSL_ATTRIBUTE_NO_TAIL_CALL \ 125 __attribute__((optimize("no-optimize-sibling-calls"))) 126 #else 127 #define ABSL_ATTRIBUTE_NO_TAIL_CALL 128 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0 129 #endif 130 131 // ABSL_ATTRIBUTE_WEAK 132 // 133 // Tags a function as weak for the purposes of compilation and linking. 134 // Weak attributes did not work properly in LLVM's Windows backend before 135 // 9.0.0, so disable them there. See https://bugs.llvm.org/show_bug.cgi?id=37598 136 // for further information. 137 // The MinGW compiler doesn't complain about the weak attribute until the link 138 // step, presumably because Windows doesn't use ELF binaries. 139 #if (ABSL_HAVE_ATTRIBUTE(weak) || \ 140 (defined(__GNUC__) && !defined(__clang__))) && \ 141 (!defined(_WIN32) || (defined(__clang__) && __clang_major__ >= 9)) && \ 142 !defined(__MINGW32__) 143 #undef ABSL_ATTRIBUTE_WEAK 144 #define ABSL_ATTRIBUTE_WEAK __attribute__((weak)) 145 #define ABSL_HAVE_ATTRIBUTE_WEAK 1 146 #else 147 #define ABSL_ATTRIBUTE_WEAK 148 #define ABSL_HAVE_ATTRIBUTE_WEAK 0 149 #endif 150 151 // ABSL_ATTRIBUTE_NONNULL 152 // 153 // Tells the compiler either (a) that a particular function parameter 154 // should be a non-null pointer, or (b) that all pointer arguments should 155 // be non-null. 156 // 157 // Note: As the GCC manual states, "[s]ince non-static C++ methods 158 // have an implicit 'this' argument, the arguments of such methods 159 // should be counted from two, not one." 160 // 161 // Args are indexed starting at 1. 162 // 163 // For non-static class member functions, the implicit `this` argument 164 // is arg 1, and the first explicit argument is arg 2. For static class member 165 // functions, there is no implicit `this`, and the first explicit argument is 166 // arg 1. 167 // 168 // Example: 169 // 170 // /* arg_a cannot be null, but arg_b can */ 171 // void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1); 172 // 173 // class C { 174 // /* arg_a cannot be null, but arg_b can */ 175 // void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2); 176 // 177 // /* arg_a cannot be null, but arg_b can */ 178 // static void StaticMethod(void* arg_a, void* arg_b) 179 // ABSL_ATTRIBUTE_NONNULL(1); 180 // }; 181 // 182 // If no arguments are provided, then all pointer arguments should be non-null. 183 // 184 // /* No pointer arguments may be null. */ 185 // void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL(); 186 // 187 // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but 188 // ABSL_ATTRIBUTE_NONNULL does not. 189 #if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__)) 190 #define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index))) 191 #else 192 #define ABSL_ATTRIBUTE_NONNULL(...) 193 #endif 194 195 // ABSL_ATTRIBUTE_NORETURN 196 // 197 // Tells the compiler that a given function never returns. 198 #if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__)) 199 #define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn)) 200 #elif defined(_MSC_VER) 201 #define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn) 202 #else 203 #define ABSL_ATTRIBUTE_NORETURN 204 #endif 205 206 // ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS 207 // 208 // Tells the AddressSanitizer (or other memory testing tools) to ignore a given 209 // function. Useful for cases when a function reads random locations on stack, 210 // calls _exit from a cloned subprocess, deliberately accesses buffer 211 // out of bounds or does other scary things with memory. 212 // NOTE: GCC supports AddressSanitizer(asan) since 4.8. 213 // https://gcc.gnu.org/gcc-4.8/changes.html 214 #if ABSL_HAVE_ATTRIBUTE(no_sanitize_address) 215 #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) 216 #else 217 #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS 218 #endif 219 220 // ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY 221 // 222 // Tells the MemorySanitizer to relax the handling of a given function. All "Use 223 // of uninitialized value" warnings from such functions will be suppressed, and 224 // all values loaded from memory will be considered fully initialized. This 225 // attribute is similar to the ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute 226 // above, but deals with initialized-ness rather than addressability issues. 227 // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC. 228 #if ABSL_HAVE_ATTRIBUTE(no_sanitize_memory) 229 #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) 230 #else 231 #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY 232 #endif 233 234 // ABSL_ATTRIBUTE_NO_SANITIZE_THREAD 235 // 236 // Tells the ThreadSanitizer to not instrument a given function. 237 // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8. 238 // https://gcc.gnu.org/gcc-4.8/changes.html 239 #if ABSL_HAVE_ATTRIBUTE(no_sanitize_thread) 240 #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread)) 241 #else 242 #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD 243 #endif 244 245 // ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED 246 // 247 // Tells the UndefinedSanitizer to ignore a given function. Useful for cases 248 // where certain behavior (eg. division by zero) is being used intentionally. 249 // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9. 250 // https://gcc.gnu.org/gcc-4.9/changes.html 251 #if ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined) 252 #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \ 253 __attribute__((no_sanitize_undefined)) 254 #elif ABSL_HAVE_ATTRIBUTE(no_sanitize) 255 #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \ 256 __attribute__((no_sanitize("undefined"))) 257 #else 258 #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED 259 #endif 260 261 // ABSL_ATTRIBUTE_NO_SANITIZE_CFI 262 // 263 // Tells the ControlFlowIntegrity sanitizer to not instrument a given function. 264 // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details. 265 #if ABSL_HAVE_ATTRIBUTE(no_sanitize) 266 #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi"))) 267 #else 268 #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI 269 #endif 270 271 // ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK 272 // 273 // Tells the SafeStack to not instrument a given function. 274 // See https://clang.llvm.org/docs/SafeStack.html for details. 275 #if ABSL_HAVE_ATTRIBUTE(no_sanitize) 276 #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \ 277 __attribute__((no_sanitize("safe-stack"))) 278 #else 279 #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK 280 #endif 281 282 // ABSL_ATTRIBUTE_RETURNS_NONNULL 283 // 284 // Tells the compiler that a particular function never returns a null pointer. 285 #if ABSL_HAVE_ATTRIBUTE(returns_nonnull) 286 #define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull)) 287 #else 288 #define ABSL_ATTRIBUTE_RETURNS_NONNULL 289 #endif 290 291 // ABSL_HAVE_ATTRIBUTE_SECTION 292 // 293 // Indicates whether labeled sections are supported. Weak symbol support is 294 // a prerequisite. Labeled sections are not supported on Darwin/iOS. 295 #ifdef ABSL_HAVE_ATTRIBUTE_SECTION 296 #error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set 297 #elif (ABSL_HAVE_ATTRIBUTE(section) || \ 298 (defined(__GNUC__) && !defined(__clang__))) && \ 299 !defined(__APPLE__) && ABSL_HAVE_ATTRIBUTE_WEAK 300 #define ABSL_HAVE_ATTRIBUTE_SECTION 1 301 302 // ABSL_ATTRIBUTE_SECTION 303 // 304 // Tells the compiler/linker to put a given function into a section and define 305 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section. 306 // This functionality is supported by GNU linker. Any function annotated with 307 // `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into 308 // whatever section its caller is placed into. 309 // 310 #ifndef ABSL_ATTRIBUTE_SECTION 311 #define ABSL_ATTRIBUTE_SECTION(name) \ 312 __attribute__((section(#name))) __attribute__((noinline)) 313 #endif 314 315 // ABSL_ATTRIBUTE_SECTION_VARIABLE 316 // 317 // Tells the compiler/linker to put a given variable into a section and define 318 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section. 319 // This functionality is supported by GNU linker. 320 #ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE 321 #ifdef _AIX 322 // __attribute__((section(#name))) on AIX is achived by using the `.csect` psudo 323 // op which includes an additional integer as part of its syntax indcating 324 // alignment. If data fall under different alignments then you might get a 325 // compilation error indicating a `Section type conflict`. 326 #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) 327 #else 328 #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name))) 329 #endif 330 #endif 331 332 // ABSL_DECLARE_ATTRIBUTE_SECTION_VARS 333 // 334 // A weak section declaration to be used as a global declaration 335 // for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link 336 // even without functions with ABSL_ATTRIBUTE_SECTION(name). 337 // ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's 338 // a no-op on ELF but not on Mach-O. 339 // 340 #ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS 341 #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 342 extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \ 343 extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK 344 #endif 345 #ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS 346 #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) 347 #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) 348 #endif 349 350 // ABSL_ATTRIBUTE_SECTION_START 351 // 352 // Returns `void*` pointers to start/end of a section of code with 353 // functions having ABSL_ATTRIBUTE_SECTION(name). 354 // Returns 0 if no such functions exist. 355 // One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and 356 // link. 357 // 358 #define ABSL_ATTRIBUTE_SECTION_START(name) \ 359 (reinterpret_cast<void *>(__start_##name)) 360 #define ABSL_ATTRIBUTE_SECTION_STOP(name) \ 361 (reinterpret_cast<void *>(__stop_##name)) 362 363 #else // !ABSL_HAVE_ATTRIBUTE_SECTION 364 365 #define ABSL_HAVE_ATTRIBUTE_SECTION 0 366 367 // provide dummy definitions 368 #define ABSL_ATTRIBUTE_SECTION(name) 369 #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) 370 #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) 371 #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) 372 #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) 373 #define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0)) 374 #define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0)) 375 376 #endif // ABSL_ATTRIBUTE_SECTION 377 378 // ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 379 // 380 // Support for aligning the stack on 32-bit x86. 381 #if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \ 382 (defined(__GNUC__) && !defined(__clang__)) 383 #if defined(__i386__) 384 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \ 385 __attribute__((force_align_arg_pointer)) 386 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 387 #elif defined(__x86_64__) 388 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1) 389 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 390 #else // !__i386__ && !__x86_64 391 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 392 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 393 #endif // __i386__ 394 #else 395 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 396 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 397 #endif 398 399 // ABSL_MUST_USE_RESULT 400 // 401 // Tells the compiler to warn about unused results. 402 // 403 // For code or headers that are assured to only build with C++17 and up, prefer 404 // just using the standard `[[nodiscard]]` directly over this macro. 405 // 406 // When annotating a function, it must appear as the first part of the 407 // declaration or definition. The compiler will warn if the return value from 408 // such a function is unused: 409 // 410 // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket(); 411 // AllocateSprocket(); // Triggers a warning. 412 // 413 // When annotating a class, it is equivalent to annotating every function which 414 // returns an instance. 415 // 416 // class ABSL_MUST_USE_RESULT Sprocket {}; 417 // Sprocket(); // Triggers a warning. 418 // 419 // Sprocket MakeSprocket(); 420 // MakeSprocket(); // Triggers a warning. 421 // 422 // Note that references and pointers are not instances: 423 // 424 // Sprocket* SprocketPointer(); 425 // SprocketPointer(); // Does *not* trigger a warning. 426 // 427 // ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result 428 // warning. For that, warn_unused_result is used only for clang but not for gcc. 429 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 430 // 431 // Note: past advice was to place the macro after the argument list. 432 // 433 // TODO(b/176172494): Use ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) when all code is 434 // compliant with the stricter [[nodiscard]]. 435 #if defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result) 436 #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result)) 437 #else 438 #define ABSL_MUST_USE_RESULT 439 #endif 440 441 // ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD 442 // 443 // Tells GCC that a function is hot or cold. GCC can use this information to 444 // improve static analysis, i.e. a conditional branch to a cold function 445 // is likely to be not-taken. 446 // This annotation is used for function declarations. 447 // 448 // Example: 449 // 450 // int foo() ABSL_ATTRIBUTE_HOT; 451 #if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__)) 452 #define ABSL_ATTRIBUTE_HOT __attribute__((hot)) 453 #else 454 #define ABSL_ATTRIBUTE_HOT 455 #endif 456 457 #if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__)) 458 #define ABSL_ATTRIBUTE_COLD __attribute__((cold)) 459 #else 460 #define ABSL_ATTRIBUTE_COLD 461 #endif 462 463 // ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS 464 // 465 // We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT 466 // macro used as an attribute to mark functions that must always or never be 467 // instrumented by XRay. Currently, this is only supported in Clang/LLVM. 468 // 469 // For reference on the LLVM XRay instrumentation, see 470 // http://llvm.org/docs/XRay.html. 471 // 472 // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration 473 // will always get the XRay instrumentation sleds. These sleds may introduce 474 // some binary size and runtime overhead and must be used sparingly. 475 // 476 // These attributes only take effect when the following conditions are met: 477 // 478 // * The file/target is built in at least C++11 mode, with a Clang compiler 479 // that supports XRay attributes. 480 // * The file/target is built with the -fxray-instrument flag set for the 481 // Clang/LLVM compiler. 482 // * The function is defined in the translation unit (the compiler honors the 483 // attribute in either the definition or the declaration, and must match). 484 // 485 // There are cases when, even when building with XRay instrumentation, users 486 // might want to control specifically which functions are instrumented for a 487 // particular build using special-case lists provided to the compiler. These 488 // special case lists are provided to Clang via the 489 // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The 490 // attributes in source take precedence over these special-case lists. 491 // 492 // To disable the XRay attributes at build-time, users may define 493 // ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific 494 // packages/targets, as this may lead to conflicting definitions of functions at 495 // link-time. 496 // 497 // XRay isn't currently supported on Android: 498 // https://github.com/android/ndk/issues/368 499 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \ 500 !defined(ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__) 501 #define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]] 502 #define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]] 503 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args) 504 #define ABSL_XRAY_LOG_ARGS(N) \ 505 [[clang::xray_always_instrument, clang::xray_log_args(N)]] 506 #else 507 #define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]] 508 #endif 509 #else 510 #define ABSL_XRAY_ALWAYS_INSTRUMENT 511 #define ABSL_XRAY_NEVER_INSTRUMENT 512 #define ABSL_XRAY_LOG_ARGS(N) 513 #endif 514 515 // ABSL_ATTRIBUTE_REINITIALIZES 516 // 517 // Indicates that a member function reinitializes the entire object to a known 518 // state, independent of the previous state of the object. 519 // 520 // The clang-tidy check bugprone-use-after-move allows member functions marked 521 // with this attribute to be called on objects that have been moved from; 522 // without the attribute, this would result in a use-after-move warning. 523 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes) 524 #define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]] 525 #else 526 #define ABSL_ATTRIBUTE_REINITIALIZES 527 #endif 528 529 // ----------------------------------------------------------------------------- 530 // Variable Attributes 531 // ----------------------------------------------------------------------------- 532 533 // ABSL_ATTRIBUTE_UNUSED 534 // 535 // Prevents the compiler from complaining about variables that appear unused. 536 // 537 // For code or headers that are assured to only build with C++17 and up, prefer 538 // just using the standard '[[maybe_unused]]' directly over this macro. 539 // 540 // Due to differences in positioning requirements between the old, compiler 541 // specific __attribute__ syntax and the now standard [[maybe_unused]], this 542 // macro does not attempt to take advantage of '[[maybe_unused]]'. 543 #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__)) 544 #undef ABSL_ATTRIBUTE_UNUSED 545 #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__)) 546 #else 547 #define ABSL_ATTRIBUTE_UNUSED 548 #endif 549 550 // ABSL_ATTRIBUTE_INITIAL_EXEC 551 // 552 // Tells the compiler to use "initial-exec" mode for a thread-local variable. 553 // See http://people.redhat.com/drepper/tls.pdf for the gory details. 554 #if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__)) 555 #define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec"))) 556 #else 557 #define ABSL_ATTRIBUTE_INITIAL_EXEC 558 #endif 559 560 // ABSL_ATTRIBUTE_PACKED 561 // 562 // Instructs the compiler not to use natural alignment for a tagged data 563 // structure, but instead to reduce its alignment to 1. 564 // 565 // Therefore, DO NOT APPLY THIS ATTRIBUTE TO STRUCTS CONTAINING ATOMICS. Doing 566 // so can cause atomic variables to be mis-aligned and silently violate 567 // atomicity on x86. 568 // 569 // This attribute can either be applied to members of a structure or to a 570 // structure in its entirety. Applying this attribute (judiciously) to a 571 // structure in its entirety to optimize the memory footprint of very 572 // commonly-used structs is fine. Do not apply this attribute to a structure in 573 // its entirety if the purpose is to control the offsets of the members in the 574 // structure. Instead, apply this attribute only to structure members that need 575 // it. 576 // 577 // When applying ABSL_ATTRIBUTE_PACKED only to specific structure members the 578 // natural alignment of structure members not annotated is preserved. Aligned 579 // member accesses are faster than non-aligned member accesses even if the 580 // targeted microprocessor supports non-aligned accesses. 581 #if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__)) 582 #define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__)) 583 #else 584 #define ABSL_ATTRIBUTE_PACKED 585 #endif 586 587 // ABSL_ATTRIBUTE_FUNC_ALIGN 588 // 589 // Tells the compiler to align the function start at least to certain 590 // alignment boundary 591 #if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__)) 592 #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes))) 593 #else 594 #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) 595 #endif 596 597 // ABSL_FALLTHROUGH_INTENDED 598 // 599 // Annotates implicit fall-through between switch labels, allowing a case to 600 // indicate intentional fallthrough and turn off warnings about any lack of a 601 // `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by 602 // a semicolon and can be used in most places where `break` can, provided that 603 // no statements exist between it and the next switch label. 604 // 605 // Example: 606 // 607 // switch (x) { 608 // case 40: 609 // case 41: 610 // if (truth_is_out_there) { 611 // ++x; 612 // ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations 613 // // in comments 614 // } else { 615 // return x; 616 // } 617 // case 42: 618 // ... 619 // 620 // Notes: When supported, GCC and Clang can issue a warning on switch labels 621 // with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See 622 // clang documentation on language extensions for details: 623 // https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough 624 // 625 // When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro has 626 // no effect on diagnostics. In any case this macro has no effect on runtime 627 // behavior and performance of code. 628 629 #ifdef ABSL_FALLTHROUGH_INTENDED 630 #error "ABSL_FALLTHROUGH_INTENDED should not be defined." 631 #elif ABSL_HAVE_CPP_ATTRIBUTE(fallthrough) 632 #define ABSL_FALLTHROUGH_INTENDED [[fallthrough]] 633 #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough) 634 #define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]] 635 #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough) 636 #define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]] 637 #else 638 #define ABSL_FALLTHROUGH_INTENDED \ 639 do { \ 640 } while (0) 641 #endif 642 643 // ABSL_DEPRECATED() 644 // 645 // Marks a deprecated class, struct, enum, function, method and variable 646 // declarations. The macro argument is used as a custom diagnostic message (e.g. 647 // suggestion of a better alternative). 648 // 649 // For code or headers that are assured to only build with C++14 and up, prefer 650 // just using the standard `[[deprecated("message")]]` directly over this macro. 651 // 652 // Examples: 653 // 654 // class ABSL_DEPRECATED("Use Bar instead") Foo {...}; 655 // 656 // ABSL_DEPRECATED("Use Baz() instead") void Bar() {...} 657 // 658 // template <typename T> 659 // ABSL_DEPRECATED("Use DoThat() instead") 660 // void DoThis(); 661 // 662 // enum FooEnum { 663 // kBar ABSL_DEPRECATED("Use kBaz instead"), 664 // }; 665 // 666 // Every usage of a deprecated entity will trigger a warning when compiled with 667 // GCC/Clang's `-Wdeprecated-declarations` option. Google's production toolchain 668 // turns this warning off by default, instead relying on clang-tidy to report 669 // new uses of deprecated code. 670 #if ABSL_HAVE_ATTRIBUTE(deprecated) 671 #define ABSL_DEPRECATED(message) __attribute__((deprecated(message))) 672 #else 673 #define ABSL_DEPRECATED(message) 674 #endif 675 676 // ABSL_CONST_INIT 677 // 678 // A variable declaration annotated with the `ABSL_CONST_INIT` attribute will 679 // not compile (on supported platforms) unless the variable has a constant 680 // initializer. This is useful for variables with static and thread storage 681 // duration, because it guarantees that they will not suffer from the so-called 682 // "static init order fiasco". Prefer to put this attribute on the most visible 683 // declaration of the variable, if there's more than one, because code that 684 // accesses the variable can then use the attribute for optimization. 685 // 686 // Example: 687 // 688 // class MyClass { 689 // public: 690 // ABSL_CONST_INIT static MyType my_var; 691 // }; 692 // 693 // MyType MyClass::my_var = MakeMyType(...); 694 // 695 // Note that this attribute is redundant if the variable is declared constexpr. 696 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) 697 #define ABSL_CONST_INIT [[clang::require_constant_initialization]] 698 #else 699 #define ABSL_CONST_INIT 700 #endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) 701 702 // ABSL_ATTRIBUTE_PURE_FUNCTION 703 // 704 // ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure" 705 // functions. A function is pure if its return value is only a function of its 706 // arguments. The pure attribute prohibits a function from modifying the state 707 // of the program that is observable by means other than inspecting the 708 // function's return value. Declaring such functions with the pure attribute 709 // allows the compiler to avoid emitting some calls in repeated invocations of 710 // the function with the same argument values. 711 // 712 // Example: 713 // 714 // ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Milliseconds(Duration d); 715 #if ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure) 716 #define ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]] 717 #elif ABSL_HAVE_ATTRIBUTE(pure) 718 #define ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure)) 719 #else 720 #define ABSL_ATTRIBUTE_PURE_FUNCTION 721 #endif 722 723 // ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function 724 // parameter or implicit object parameter is retained by the return value of the 725 // annotated function (or, for a parameter of a constructor, in the value of the 726 // constructed object). This attribute causes warnings to be produced if a 727 // temporary object does not live long enough. 728 // 729 // When applied to a reference parameter, the referenced object is assumed to be 730 // retained by the return value of the function. When applied to a non-reference 731 // parameter (for example, a pointer or a class type), all temporaries 732 // referenced by the parameter are assumed to be retained by the return value of 733 // the function. 734 // 735 // See also the upstream documentation: 736 // https://clang.llvm.org/docs/AttributeReference.html#lifetimebound 737 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound) 738 #define ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]] 739 #elif ABSL_HAVE_ATTRIBUTE(lifetimebound) 740 #define ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound)) 741 #else 742 #define ABSL_ATTRIBUTE_LIFETIME_BOUND 743 #endif 744 745 #endif // ABSL_BASE_ATTRIBUTES_H_ 746