00001 // Copyright 2017 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // 00015 // This header file defines macros for declaring attributes for functions, 00016 // types, and variables. 00017 // 00018 // These macros are used within Abseil and allow the compiler to optimize, where 00019 // applicable, certain function calls. 00020 // 00021 // This file is used for both C and C++! 00022 // 00023 // Most macros here are exposing GCC or Clang features, and are stubbed out for 00024 // other compilers. 00025 // 00026 // GCC attributes documentation: 00027 // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html 00028 // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html 00029 // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html 00030 // 00031 // Most attributes in this file are already supported by GCC 4.7. However, some 00032 // of them are not supported in older version of Clang. Thus, we check 00033 // `__has_attribute()` first. If the check fails, we check if we are on GCC and 00034 // assume the attribute exists on GCC (which is verified on GCC 4.7). 00035 // 00036 // ----------------------------------------------------------------------------- 00037 // Sanitizer Attributes 00038 // ----------------------------------------------------------------------------- 00039 // 00040 // Sanitizer-related attributes are not "defined" in this file (and indeed 00041 // are not defined as such in any file). To utilize the following 00042 // sanitizer-related attributes within your builds, define the following macros 00043 // within your build using a `-D` flag, along with the given value for 00044 // `-fsanitize`: 00045 // 00046 // * `ADDRESS_SANITIZER` + `-fsanitize=address` (Clang, GCC 4.8) 00047 // * `MEMORY_SANITIZER` + `-fsanitize=memory` (Clang-only) 00048 // * `THREAD_SANITIZER + `-fsanitize=thread` (Clang, GCC 4.8+) 00049 // * `UNDEFINED_BEHAVIOR_SANITIZER` + `-fsanitize=undefined` (Clang, GCC 4.9+) 00050 // * `CONTROL_FLOW_INTEGRITY` + -fsanitize=cfi (Clang-only) 00051 // 00052 // Example: 00053 // 00054 // // Enable branches in the Abseil code that are tagged for ASan: 00055 // $ bazel build --copt=-DADDRESS_SANITIZER --copt=-fsanitize=address 00056 // --linkopt=-fsanitize=address *target* 00057 // 00058 // Since these macro names are only supported by GCC and Clang, we only check 00059 // for `__GNUC__` (GCC or Clang) and the above macros. 00060 #ifndef ABSL_BASE_ATTRIBUTES_H_ 00061 #define ABSL_BASE_ATTRIBUTES_H_ 00062 00063 // ABSL_HAVE_ATTRIBUTE 00064 // 00065 // A function-like feature checking macro that is a wrapper around 00066 // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a 00067 // nonzero constant integer if the attribute is supported or 0 if not. 00068 // 00069 // It evaluates to zero if `__has_attribute` is not defined by the compiler. 00070 // 00071 // GCC: https://gcc.gnu.org/gcc-5/changes.html 00072 // Clang: https://clang.llvm.org/docs/LanguageExtensions.html 00073 #ifdef __has_attribute 00074 #define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x) 00075 #else 00076 #define ABSL_HAVE_ATTRIBUTE(x) 0 00077 #endif 00078 00079 // ABSL_HAVE_CPP_ATTRIBUTE 00080 // 00081 // A function-like feature checking macro that accepts C++11 style attributes. 00082 // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6 00083 // (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't 00084 // find `__has_cpp_attribute`, will evaluate to 0. 00085 #if defined(__cplusplus) && defined(__has_cpp_attribute) 00086 // NOTE: requiring __cplusplus above should not be necessary, but 00087 // works around https://bugs.llvm.org/show_bug.cgi?id=23435. 00088 #define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) 00089 #else 00090 #define ABSL_HAVE_CPP_ATTRIBUTE(x) 0 00091 #endif 00092 00093 // ----------------------------------------------------------------------------- 00094 // Function Attributes 00095 // ----------------------------------------------------------------------------- 00096 // 00097 // GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html 00098 // Clang: https://clang.llvm.org/docs/AttributeReference.html 00099 00100 // ABSL_PRINTF_ATTRIBUTE 00101 // ABSL_SCANF_ATTRIBUTE 00102 // 00103 // Tells the compiler to perform `printf` format string checking if the 00104 // compiler supports it; see the 'format' attribute in 00105 // <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>. 00106 // 00107 // Note: As the GCC manual states, "[s]ince non-static C++ methods 00108 // have an implicit 'this' argument, the arguments of such methods 00109 // should be counted from two, not one." 00110 #if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__)) 00111 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \ 00112 __attribute__((__format__(__printf__, string_index, first_to_check))) 00113 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \ 00114 __attribute__((__format__(__scanf__, string_index, first_to_check))) 00115 #else 00116 #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) 00117 #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) 00118 #endif 00119 00120 // ABSL_ATTRIBUTE_ALWAYS_INLINE 00121 // ABSL_ATTRIBUTE_NOINLINE 00122 // 00123 // Forces functions to either inline or not inline. Introduced in gcc 3.1. 00124 #if ABSL_HAVE_ATTRIBUTE(always_inline) || \ 00125 (defined(__GNUC__) && !defined(__clang__)) 00126 #define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) 00127 #define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1 00128 #else 00129 #define ABSL_ATTRIBUTE_ALWAYS_INLINE 00130 #endif 00131 00132 #if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__)) 00133 #define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline)) 00134 #define ABSL_HAVE_ATTRIBUTE_NOINLINE 1 00135 #else 00136 #define ABSL_ATTRIBUTE_NOINLINE 00137 #endif 00138 00139 // ABSL_ATTRIBUTE_NO_TAIL_CALL 00140 // 00141 // Prevents the compiler from optimizing away stack frames for functions which 00142 // end in a call to another function. 00143 #if ABSL_HAVE_ATTRIBUTE(disable_tail_calls) 00144 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 00145 #define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls)) 00146 #elif defined(__GNUC__) && !defined(__clang__) 00147 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 00148 #define ABSL_ATTRIBUTE_NO_TAIL_CALL \ 00149 __attribute__((optimize("no-optimize-sibling-calls"))) 00150 #else 00151 #define ABSL_ATTRIBUTE_NO_TAIL_CALL 00152 #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0 00153 #endif 00154 00155 // ABSL_ATTRIBUTE_WEAK 00156 // 00157 // Tags a function as weak for the purposes of compilation and linking. 00158 // Weak attributes currently do not work properly in LLVM's Windows backend, 00159 // so disable them there. See https://bugs.llvm.org/show_bug.cgi?id=37598 00160 // for futher information. 00161 #if (ABSL_HAVE_ATTRIBUTE(weak) || \ 00162 (defined(__GNUC__) && !defined(__clang__))) && \ 00163 !(defined(__llvm__) && defined(_WIN32)) 00164 #undef ABSL_ATTRIBUTE_WEAK 00165 #define ABSL_ATTRIBUTE_WEAK __attribute__((weak)) 00166 #define ABSL_HAVE_ATTRIBUTE_WEAK 1 00167 #else 00168 #define ABSL_ATTRIBUTE_WEAK 00169 #define ABSL_HAVE_ATTRIBUTE_WEAK 0 00170 #endif 00171 00172 // ABSL_ATTRIBUTE_NONNULL 00173 // 00174 // Tells the compiler either (a) that a particular function parameter 00175 // should be a non-null pointer, or (b) that all pointer arguments should 00176 // be non-null. 00177 // 00178 // Note: As the GCC manual states, "[s]ince non-static C++ methods 00179 // have an implicit 'this' argument, the arguments of such methods 00180 // should be counted from two, not one." 00181 // 00182 // Args are indexed starting at 1. 00183 // 00184 // For non-static class member functions, the implicit `this` argument 00185 // is arg 1, and the first explicit argument is arg 2. For static class member 00186 // functions, there is no implicit `this`, and the first explicit argument is 00187 // arg 1. 00188 // 00189 // Example: 00190 // 00191 // /* arg_a cannot be null, but arg_b can */ 00192 // void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1); 00193 // 00194 // class C { 00195 // /* arg_a cannot be null, but arg_b can */ 00196 // void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2); 00197 // 00198 // /* arg_a cannot be null, but arg_b can */ 00199 // static void StaticMethod(void* arg_a, void* arg_b) 00200 // ABSL_ATTRIBUTE_NONNULL(1); 00201 // }; 00202 // 00203 // If no arguments are provided, then all pointer arguments should be non-null. 00204 // 00205 // /* No pointer arguments may be null. */ 00206 // void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL(); 00207 // 00208 // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but 00209 // ABSL_ATTRIBUTE_NONNULL does not. 00210 #if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__)) 00211 #define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index))) 00212 #else 00213 #define ABSL_ATTRIBUTE_NONNULL(...) 00214 #endif 00215 00216 // ABSL_ATTRIBUTE_NORETURN 00217 // 00218 // Tells the compiler that a given function never returns. 00219 #if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__)) 00220 #define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn)) 00221 #elif defined(_MSC_VER) 00222 #define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn) 00223 #else 00224 #define ABSL_ATTRIBUTE_NORETURN 00225 #endif 00226 00227 // ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS 00228 // 00229 // Tells the AddressSanitizer (or other memory testing tools) to ignore a given 00230 // function. Useful for cases when a function reads random locations on stack, 00231 // calls _exit from a cloned subprocess, deliberately accesses buffer 00232 // out of bounds or does other scary things with memory. 00233 // NOTE: GCC supports AddressSanitizer(asan) since 4.8. 00234 // https://gcc.gnu.org/gcc-4.8/changes.html 00235 #if defined(__GNUC__) && defined(ADDRESS_SANITIZER) 00236 #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) 00237 #else 00238 #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS 00239 #endif 00240 00241 // ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY 00242 // 00243 // Tells the MemorySanitizer to relax the handling of a given function. All 00244 // "Use of uninitialized value" warnings from such functions will be suppressed, 00245 // and all values loaded from memory will be considered fully initialized. 00246 // This attribute is similar to the ADDRESS_SANITIZER attribute above, but deals 00247 // with initialized-ness rather than addressability issues. 00248 // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC. 00249 #if defined(__GNUC__) && defined(MEMORY_SANITIZER) 00250 #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) 00251 #else 00252 #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY 00253 #endif 00254 00255 // ABSL_ATTRIBUTE_NO_SANITIZE_THREAD 00256 // 00257 // Tells the ThreadSanitizer to not instrument a given function. 00258 // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8. 00259 // https://gcc.gnu.org/gcc-4.8/changes.html 00260 #if defined(__GNUC__) && defined(THREAD_SANITIZER) 00261 #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread)) 00262 #else 00263 #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD 00264 #endif 00265 00266 // ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED 00267 // 00268 // Tells the UndefinedSanitizer to ignore a given function. Useful for cases 00269 // where certain behavior (eg. division by zero) is being used intentionally. 00270 // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9. 00271 // https://gcc.gnu.org/gcc-4.9/changes.html 00272 #if defined(__GNUC__) && \ 00273 (defined(UNDEFINED_BEHAVIOR_SANITIZER) || defined(ADDRESS_SANITIZER)) 00274 #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \ 00275 __attribute__((no_sanitize("undefined"))) 00276 #else 00277 #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED 00278 #endif 00279 00280 // ABSL_ATTRIBUTE_NO_SANITIZE_CFI 00281 // 00282 // Tells the ControlFlowIntegrity sanitizer to not instrument a given function. 00283 // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details. 00284 #if defined(__GNUC__) && defined(CONTROL_FLOW_INTEGRITY) 00285 #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi"))) 00286 #else 00287 #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI 00288 #endif 00289 00290 // ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK 00291 // 00292 // Tells the SafeStack to not instrument a given function. 00293 // See https://clang.llvm.org/docs/SafeStack.html for details. 00294 #if defined(__GNUC__) && defined(SAFESTACK_SANITIZER) 00295 #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \ 00296 __attribute__((no_sanitize("safe-stack"))) 00297 #else 00298 #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK 00299 #endif 00300 00301 // ABSL_ATTRIBUTE_RETURNS_NONNULL 00302 // 00303 // Tells the compiler that a particular function never returns a null pointer. 00304 #if ABSL_HAVE_ATTRIBUTE(returns_nonnull) || \ 00305 (defined(__GNUC__) && \ 00306 (__GNUC__ > 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && \ 00307 !defined(__clang__)) 00308 #define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull)) 00309 #else 00310 #define ABSL_ATTRIBUTE_RETURNS_NONNULL 00311 #endif 00312 00313 // ABSL_HAVE_ATTRIBUTE_SECTION 00314 // 00315 // Indicates whether labeled sections are supported. Weak symbol support is 00316 // a prerequisite. Labeled sections are not supported on Darwin/iOS. 00317 #ifdef ABSL_HAVE_ATTRIBUTE_SECTION 00318 #error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set 00319 #elif (ABSL_HAVE_ATTRIBUTE(section) || \ 00320 (defined(__GNUC__) && !defined(__clang__))) && \ 00321 !defined(__APPLE__) && ABSL_HAVE_ATTRIBUTE_WEAK 00322 #define ABSL_HAVE_ATTRIBUTE_SECTION 1 00323 00324 // ABSL_ATTRIBUTE_SECTION 00325 // 00326 // Tells the compiler/linker to put a given function into a section and define 00327 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section. 00328 // This functionality is supported by GNU linker. Any function annotated with 00329 // `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into 00330 // whatever section its caller is placed into. 00331 // 00332 #ifndef ABSL_ATTRIBUTE_SECTION 00333 #define ABSL_ATTRIBUTE_SECTION(name) \ 00334 __attribute__((section(#name))) __attribute__((noinline)) 00335 #endif 00336 00337 00338 // ABSL_ATTRIBUTE_SECTION_VARIABLE 00339 // 00340 // Tells the compiler/linker to put a given variable into a section and define 00341 // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section. 00342 // This functionality is supported by GNU linker. 00343 #ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE 00344 #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name))) 00345 #endif 00346 00347 // ABSL_DECLARE_ATTRIBUTE_SECTION_VARS 00348 // 00349 // A weak section declaration to be used as a global declaration 00350 // for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link 00351 // even without functions with ABSL_ATTRIBUTE_SECTION(name). 00352 // ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's 00353 // a no-op on ELF but not on Mach-O. 00354 // 00355 #ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS 00356 #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \ 00357 extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \ 00358 extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK 00359 #endif 00360 #ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS 00361 #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) 00362 #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) 00363 #endif 00364 00365 // ABSL_ATTRIBUTE_SECTION_START 00366 // 00367 // Returns `void*` pointers to start/end of a section of code with 00368 // functions having ABSL_ATTRIBUTE_SECTION(name). 00369 // Returns 0 if no such functions exist. 00370 // One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and 00371 // link. 00372 // 00373 #define ABSL_ATTRIBUTE_SECTION_START(name) \ 00374 (reinterpret_cast<void *>(__start_##name)) 00375 #define ABSL_ATTRIBUTE_SECTION_STOP(name) \ 00376 (reinterpret_cast<void *>(__stop_##name)) 00377 00378 #else // !ABSL_HAVE_ATTRIBUTE_SECTION 00379 00380 #define ABSL_HAVE_ATTRIBUTE_SECTION 0 00381 00382 // provide dummy definitions 00383 #define ABSL_ATTRIBUTE_SECTION(name) 00384 #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) 00385 #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) 00386 #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) 00387 #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) 00388 #define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0)) 00389 #define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0)) 00390 00391 #endif // ABSL_ATTRIBUTE_SECTION 00392 00393 // ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 00394 // 00395 // Support for aligning the stack on 32-bit x86. 00396 #if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \ 00397 (defined(__GNUC__) && !defined(__clang__)) 00398 #if defined(__i386__) 00399 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \ 00400 __attribute__((force_align_arg_pointer)) 00401 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 00402 #elif defined(__x86_64__) 00403 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1) 00404 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 00405 #else // !__i386__ && !__x86_64 00406 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 00407 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 00408 #endif // __i386__ 00409 #else 00410 #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC 00411 #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) 00412 #endif 00413 00414 // ABSL_MUST_USE_RESULT 00415 // 00416 // Tells the compiler to warn about unused results. 00417 // 00418 // When annotating a function, it must appear as the first part of the 00419 // declaration or definition. The compiler will warn if the return value from 00420 // such a function is unused: 00421 // 00422 // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket(); 00423 // AllocateSprocket(); // Triggers a warning. 00424 // 00425 // When annotating a class, it is equivalent to annotating every function which 00426 // returns an instance. 00427 // 00428 // class ABSL_MUST_USE_RESULT Sprocket {}; 00429 // Sprocket(); // Triggers a warning. 00430 // 00431 // Sprocket MakeSprocket(); 00432 // MakeSprocket(); // Triggers a warning. 00433 // 00434 // Note that references and pointers are not instances: 00435 // 00436 // Sprocket* SprocketPointer(); 00437 // SprocketPointer(); // Does *not* trigger a warning. 00438 // 00439 // ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result 00440 // warning. For that, warn_unused_result is used only for clang but not for gcc. 00441 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 00442 // 00443 // Note: past advice was to place the macro after the argument list. 00444 #if ABSL_HAVE_ATTRIBUTE(nodiscard) 00445 #define ABSL_MUST_USE_RESULT [[nodiscard]] 00446 #elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result) 00447 #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result)) 00448 #else 00449 #define ABSL_MUST_USE_RESULT 00450 #endif 00451 00452 // ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD 00453 // 00454 // Tells GCC that a function is hot or cold. GCC can use this information to 00455 // improve static analysis, i.e. a conditional branch to a cold function 00456 // is likely to be not-taken. 00457 // This annotation is used for function declarations. 00458 // 00459 // Example: 00460 // 00461 // int foo() ABSL_ATTRIBUTE_HOT; 00462 #if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__)) 00463 #define ABSL_ATTRIBUTE_HOT __attribute__((hot)) 00464 #else 00465 #define ABSL_ATTRIBUTE_HOT 00466 #endif 00467 00468 #if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__)) 00469 #define ABSL_ATTRIBUTE_COLD __attribute__((cold)) 00470 #else 00471 #define ABSL_ATTRIBUTE_COLD 00472 #endif 00473 00474 // ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS 00475 // 00476 // We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT 00477 // macro used as an attribute to mark functions that must always or never be 00478 // instrumented by XRay. Currently, this is only supported in Clang/LLVM. 00479 // 00480 // For reference on the LLVM XRay instrumentation, see 00481 // http://llvm.org/docs/XRay.html. 00482 // 00483 // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration 00484 // will always get the XRay instrumentation sleds. These sleds may introduce 00485 // some binary size and runtime overhead and must be used sparingly. 00486 // 00487 // These attributes only take effect when the following conditions are met: 00488 // 00489 // * The file/target is built in at least C++11 mode, with a Clang compiler 00490 // that supports XRay attributes. 00491 // * The file/target is built with the -fxray-instrument flag set for the 00492 // Clang/LLVM compiler. 00493 // * The function is defined in the translation unit (the compiler honors the 00494 // attribute in either the definition or the declaration, and must match). 00495 // 00496 // There are cases when, even when building with XRay instrumentation, users 00497 // might want to control specifically which functions are instrumented for a 00498 // particular build using special-case lists provided to the compiler. These 00499 // special case lists are provided to Clang via the 00500 // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The 00501 // attributes in source take precedence over these special-case lists. 00502 // 00503 // To disable the XRay attributes at build-time, users may define 00504 // ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific 00505 // packages/targets, as this may lead to conflicting definitions of functions at 00506 // link-time. 00507 // 00508 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \ 00509 !defined(ABSL_NO_XRAY_ATTRIBUTES) 00510 #define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]] 00511 #define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]] 00512 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args) 00513 #define ABSL_XRAY_LOG_ARGS(N) \ 00514 [[clang::xray_always_instrument, clang::xray_log_args(N)]] 00515 #else 00516 #define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]] 00517 #endif 00518 #else 00519 #define ABSL_XRAY_ALWAYS_INSTRUMENT 00520 #define ABSL_XRAY_NEVER_INSTRUMENT 00521 #define ABSL_XRAY_LOG_ARGS(N) 00522 #endif 00523 00524 // ABSL_ATTRIBUTE_REINITIALIZES 00525 // 00526 // Indicates that a member function reinitializes the entire object to a known 00527 // state, independent of the previous state of the object. 00528 // 00529 // The clang-tidy check bugprone-use-after-move allows member functions marked 00530 // with this attribute to be called on objects that have been moved from; 00531 // without the attribute, this would result in a use-after-move warning. 00532 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes) 00533 #define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]] 00534 #else 00535 #define ABSL_ATTRIBUTE_REINITIALIZES 00536 #endif 00537 00538 // ----------------------------------------------------------------------------- 00539 // Variable Attributes 00540 // ----------------------------------------------------------------------------- 00541 00542 // ABSL_ATTRIBUTE_UNUSED 00543 // 00544 // Prevents the compiler from complaining about variables that appear unused. 00545 #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__)) 00546 #undef ABSL_ATTRIBUTE_UNUSED 00547 #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__)) 00548 #else 00549 #define ABSL_ATTRIBUTE_UNUSED 00550 #endif 00551 00552 // ABSL_ATTRIBUTE_INITIAL_EXEC 00553 // 00554 // Tells the compiler to use "initial-exec" mode for a thread-local variable. 00555 // See http://people.redhat.com/drepper/tls.pdf for the gory details. 00556 #if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__)) 00557 #define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec"))) 00558 #else 00559 #define ABSL_ATTRIBUTE_INITIAL_EXEC 00560 #endif 00561 00562 // ABSL_ATTRIBUTE_PACKED 00563 // 00564 // Prevents the compiler from padding a structure to natural alignment 00565 #if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__)) 00566 #define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__)) 00567 #else 00568 #define ABSL_ATTRIBUTE_PACKED 00569 #endif 00570 00571 // ABSL_ATTRIBUTE_FUNC_ALIGN 00572 // 00573 // Tells the compiler to align the function start at least to certain 00574 // alignment boundary 00575 #if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__)) 00576 #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes))) 00577 #else 00578 #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) 00579 #endif 00580 00581 // ABSL_CONST_INIT 00582 // 00583 // A variable declaration annotated with the `ABSL_CONST_INIT` attribute will 00584 // not compile (on supported platforms) unless the variable has a constant 00585 // initializer. This is useful for variables with static and thread storage 00586 // duration, because it guarantees that they will not suffer from the so-called 00587 // "static init order fiasco". Prefer to put this attribute on the most visible 00588 // declaration of the variable, if there's more than one, because code that 00589 // accesses the variable can then use the attribute for optimization. 00590 // 00591 // Example: 00592 // 00593 // class MyClass { 00594 // public: 00595 // ABSL_CONST_INIT static MyType my_var; 00596 // }; 00597 // 00598 // MyType MyClass::my_var = MakeMyType(...); 00599 // 00600 // Note that this attribute is redundant if the variable is declared constexpr. 00601 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) 00602 // NOLINTNEXTLINE(whitespace/braces) 00603 #define ABSL_CONST_INIT [[clang::require_constant_initialization]] 00604 #else 00605 #define ABSL_CONST_INIT 00606 #endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) 00607 00608 #endif // ABSL_BASE_ATTRIBUTES_H_