misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame^] | 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 | // http://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 | // Various macros for C++ attributes |
| 16 | // This file is used for both C and C++! |
| 17 | // |
| 18 | // Most macros here are exposing GCC or Clang features, and are stubbed out for |
| 19 | // other compilers. |
| 20 | // GCC attributes documentation: |
| 21 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html |
| 22 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html |
| 23 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html |
| 24 | // |
| 25 | // Most attributes in this file are already supported by GCC 4.7. |
| 26 | // However, some of them are not supported in older version of Clang. |
| 27 | // Thus, we check __has_attribute() first. If the check fails, we check if we |
| 28 | // are on GCC and assume the attribute exists on GCC (which is verified on GCC |
| 29 | // 4.7). |
| 30 | // |
| 31 | // For sanitizer-related attributes, define the following macros |
| 32 | // using -D along with the given value for -fsanitize: |
| 33 | // - ADDRESS_SANITIZER with -fsanitize=address (GCC 4.8+, Clang) |
| 34 | // - MEMORY_SANITIZER with -fsanitize=memory (Clang) |
| 35 | // - THREAD_SANITIZER with -fsanitize=thread (GCC 4.8+, Clang) |
| 36 | // - UNDEFINED_BEHAVIOR_SANITIZER with -fsanitize=undefined (GCC 4.9+, Clang) |
| 37 | // - CONTROL_FLOW_INTEGRITY with -fsanitize=cfi (Clang) |
| 38 | // Since these are only supported by GCC and Clang now, we only check for |
| 39 | // __GNUC__ (GCC or Clang) and the above macros. |
| 40 | #ifndef ABSL_BASE_ATTRIBUTES_H_ |
| 41 | #define ABSL_BASE_ATTRIBUTES_H_ |
| 42 | |
| 43 | // ABSL_HAVE_ATTRIBUTE is a function-like feature checking macro. |
| 44 | // It's a wrapper around __has_attribute, which is defined by GCC 5+ and Clang. |
| 45 | // It evaluates to a nonzero constant integer if the attribute is supported |
| 46 | // or 0 if not. |
| 47 | // It evaluates to zero if __has_attribute is not defined by the compiler. |
| 48 | // GCC: https://gcc.gnu.org/gcc-5/changes.html |
| 49 | // Clang: https://clang.llvm.org/docs/LanguageExtensions.html |
| 50 | #ifdef __has_attribute |
| 51 | #define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x) |
| 52 | #else |
| 53 | #define ABSL_HAVE_ATTRIBUTE(x) 0 |
| 54 | #endif |
| 55 | |
| 56 | // ABSL_HAVE_CPP_ATTRIBUTE is a function-like feature checking macro that |
| 57 | // accepts C++11 style attributes. It's a wrapper around __has_cpp_attribute, |
| 58 | // defined by ISO C++ SD-6 |
| 59 | // (http://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 | // GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 73 | // Clang: https://clang.llvm.org/docs/AttributeReference.html |
| 74 | |
| 75 | // ABSL_PRINTF_ATTRIBUTE, ABSL_SCANF_ATTRIBUTE |
| 76 | // Tell the compiler to do printf format std::string checking if the |
| 77 | // compiler supports it; see the 'format' attribute in |
| 78 | // <http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>. |
| 79 | // |
| 80 | // N.B.: As the GCC manual states, "[s]ince non-static C++ methods |
| 81 | // have an implicit 'this' argument, the arguments of such methods |
| 82 | // should be counted from two, not one." |
| 83 | #if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__)) |
| 84 | #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \ |
| 85 | __attribute__((__format__(__printf__, string_index, first_to_check))) |
| 86 | #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \ |
| 87 | __attribute__((__format__(__scanf__, string_index, first_to_check))) |
| 88 | #else |
| 89 | #define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) |
| 90 | #define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) |
| 91 | #endif |
| 92 | |
| 93 | // ABSL_ATTRIBUTE_ALWAYS_INLINE, ABSL_ATTRIBUTE_NOINLINE |
| 94 | // For functions we want to force inline or not inline. |
| 95 | // Introduced in gcc 3.1. |
| 96 | #if ABSL_HAVE_ATTRIBUTE(always_inline) || \ |
| 97 | (defined(__GNUC__) && !defined(__clang__)) |
| 98 | #define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline)) |
| 99 | #define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1 |
| 100 | #else |
| 101 | #define ABSL_ATTRIBUTE_ALWAYS_INLINE |
| 102 | #endif |
| 103 | |
| 104 | #if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__)) |
| 105 | #define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline)) |
| 106 | #define ABSL_HAVE_ATTRIBUTE_NOINLINE 1 |
| 107 | #else |
| 108 | #define ABSL_ATTRIBUTE_NOINLINE |
| 109 | #endif |
| 110 | |
| 111 | // ABSL_ATTRIBUTE_NO_TAIL_CALL |
| 112 | // Prevent the compiler from optimizing away stack frames for functions which |
| 113 | // end in a call to another function. |
| 114 | #if ABSL_HAVE_ATTRIBUTE(disable_tail_calls) |
| 115 | #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 |
| 116 | #define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls)) |
| 117 | #elif defined(__GNUC__) && !defined(__clang__) |
| 118 | #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1 |
| 119 | #define ABSL_ATTRIBUTE_NO_TAIL_CALL \ |
| 120 | __attribute__((optimize("no-optimize-sibling-calls"))) |
| 121 | #else |
| 122 | #define ABSL_ATTRIBUTE_NO_TAIL_CALL |
| 123 | #define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0 |
| 124 | #endif |
| 125 | // ABSL_ATTRIBUTE_WEAK |
| 126 | // For weak functions |
| 127 | #if ABSL_HAVE_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__)) |
| 128 | #undef ABSL_ATTRIBUTE_WEAK |
| 129 | #define ABSL_ATTRIBUTE_WEAK __attribute__((weak)) |
| 130 | #define ABSL_HAVE_ATTRIBUTE_WEAK 1 |
| 131 | #else |
| 132 | #define ABSL_ATTRIBUTE_WEAK |
| 133 | #define ABSL_HAVE_ATTRIBUTE_WEAK 0 |
| 134 | #endif |
| 135 | // ABSL_ATTRIBUTE_NONNULL |
| 136 | // Tell the compiler either that a particular function parameter |
| 137 | // should be a non-null pointer, or that all pointer arguments should |
| 138 | // be non-null. |
| 139 | // |
| 140 | // Note: As the GCC manual states, "[s]ince non-static C++ methods |
| 141 | // have an implicit 'this' argument, the arguments of such methods |
| 142 | // should be counted from two, not one." |
| 143 | // |
| 144 | // Args are indexed starting at 1. |
| 145 | // For non-static class member functions, the implicit "this" argument |
| 146 | // is arg 1, and the first explicit argument is arg 2. |
| 147 | // For static class member functions, there is no implicit "this", and |
| 148 | // the first explicit argument is arg 1. |
| 149 | // |
| 150 | // /* arg_a cannot be null, but arg_b can */ |
| 151 | // void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1); |
| 152 | // |
| 153 | // class C { |
| 154 | // /* arg_a cannot be null, but arg_b can */ |
| 155 | // void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2); |
| 156 | // |
| 157 | // /* arg_a cannot be null, but arg_b can */ |
| 158 | // static void StaticMethod(void* arg_a, void* arg_b) |
| 159 | // ABSL_ATTRIBUTE_NONNULL(1); |
| 160 | // }; |
| 161 | // |
| 162 | // If no arguments are provided, then all pointer arguments should be non-null. |
| 163 | // |
| 164 | // /* No pointer arguments may be null. */ |
| 165 | // void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL(); |
| 166 | // |
| 167 | // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but |
| 168 | // ABSL_ATTRIBUTE_NONNULL does not. |
| 169 | #if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__)) |
| 170 | #define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index))) |
| 171 | #else |
| 172 | #define ABSL_ATTRIBUTE_NONNULL(...) |
| 173 | #endif |
| 174 | // ABSL_ATTRIBUTE_NORETURN |
| 175 | // Tell the compiler that a given function never returns |
| 176 | #if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__)) |
| 177 | #define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn)) |
| 178 | #elif defined(_MSC_VER) |
| 179 | #define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn) |
| 180 | #else |
| 181 | #define ABSL_ATTRIBUTE_NORETURN |
| 182 | #endif |
| 183 | // ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS |
| 184 | // Tell AddressSanitizer (or other memory testing tools) to ignore a given |
| 185 | // function. Useful for cases when a function reads random locations on stack, |
| 186 | // calls _exit from a cloned subprocess, deliberately accesses buffer |
| 187 | // out of bounds or does other scary things with memory. |
| 188 | // NOTE: GCC supports AddressSanitizer(asan) since 4.8. |
| 189 | // https://gcc.gnu.org/gcc-4.8/changes.html |
| 190 | #if defined(__GNUC__) && defined(ADDRESS_SANITIZER) |
| 191 | #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) |
| 192 | #else |
| 193 | #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS |
| 194 | #endif |
| 195 | |
| 196 | // ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY |
| 197 | // Tell MemorySanitizer to relax the handling of a given function. All "Use of |
| 198 | // uninitialized value" warnings from such functions will be suppressed, and all |
| 199 | // values loaded from memory will be considered fully initialized. |
| 200 | // This is similar to the ADDRESS_SANITIZER attribute above, but deals with |
| 201 | // initializedness rather than addressability issues. |
| 202 | // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC. |
| 203 | #if defined(__GNUC__) && defined(MEMORY_SANITIZER) |
| 204 | #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory)) |
| 205 | #else |
| 206 | #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY |
| 207 | #endif |
| 208 | |
| 209 | // ABSL_ATTRIBUTE_NO_SANITIZE_THREAD |
| 210 | // Tell ThreadSanitizer to not instrument a given function. |
| 211 | // If you are adding this attribute, please cc dynamic-tools@ on the cl. |
| 212 | // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8. |
| 213 | // https://gcc.gnu.org/gcc-4.8/changes.html |
| 214 | #if defined(__GNUC__) && defined(THREAD_SANITIZER) |
| 215 | #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread)) |
| 216 | #else |
| 217 | #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD |
| 218 | #endif |
| 219 | |
| 220 | // ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED |
| 221 | // Tell UndefinedSanitizer to ignore a given function. Useful for cases |
| 222 | // where certain behavior (eg. devision by zero) is being used intentionally. |
| 223 | // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9. |
| 224 | // https://gcc.gnu.org/gcc-4.9/changes.html |
| 225 | #if defined(__GNUC__) && \ |
| 226 | (defined(UNDEFINED_BEHAVIOR_SANITIZER) || defined(ADDRESS_SANITIZER)) |
| 227 | #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \ |
| 228 | __attribute__((no_sanitize("undefined"))) |
| 229 | #else |
| 230 | #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED |
| 231 | #endif |
| 232 | |
| 233 | // ABSL_ATTRIBUTE_NO_SANITIZE_CFI |
| 234 | // Tell ControlFlowIntegrity sanitizer to not instrument a given function. |
| 235 | // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details. |
| 236 | #if defined(__GNUC__) && defined(CONTROL_FLOW_INTEGRITY) |
| 237 | #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi"))) |
| 238 | #else |
| 239 | #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI |
| 240 | #endif |
| 241 | |
| 242 | // ABSL_ATTRIBUTE_SECTION |
| 243 | // Labeled sections are not supported on Darwin/iOS. |
| 244 | #ifdef ABSL_HAVE_ATTRIBUTE_SECTION |
| 245 | #error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set |
| 246 | #elif (ABSL_HAVE_ATTRIBUTE(section) || \ |
| 247 | (defined(__GNUC__) && !defined(__clang__))) && \ |
| 248 | !defined(__APPLE__) |
| 249 | #define ABSL_HAVE_ATTRIBUTE_SECTION 1 |
| 250 | // |
| 251 | // Tell the compiler/linker to put a given function into a section and define |
| 252 | // "__start_ ## name" and "__stop_ ## name" symbols to bracket the section. |
| 253 | // This functionality is supported by GNU linker. |
| 254 | // Any function with ABSL_ATTRIBUTE_SECTION must not be inlined, or it will |
| 255 | // be placed into whatever section its caller is placed into. |
| 256 | // |
| 257 | #ifndef ABSL_ATTRIBUTE_SECTION |
| 258 | #define ABSL_ATTRIBUTE_SECTION(name) \ |
| 259 | __attribute__((section(#name))) __attribute__((noinline)) |
| 260 | #endif |
| 261 | // Tell the compiler/linker to put a given variable into a section and define |
| 262 | // "__start_ ## name" and "__stop_ ## name" symbols to bracket the section. |
| 263 | // This functionality is supported by GNU linker. |
| 264 | #ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE |
| 265 | #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name))) |
| 266 | #endif |
| 267 | // |
| 268 | // Weak section declaration to be used as a global declaration |
| 269 | // for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link |
| 270 | // even without functions with ABSL_ATTRIBUTE_SECTION(name). |
| 271 | // ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's |
| 272 | // a no-op on ELF but not on Mach-O. |
| 273 | // |
| 274 | #ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS |
| 275 | #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \ |
| 276 | extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \ |
| 277 | extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK |
| 278 | #endif |
| 279 | #ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS |
| 280 | #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) |
| 281 | #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) |
| 282 | #endif |
| 283 | |
| 284 | // Return void* pointers to start/end of a section of code with |
| 285 | // functions having ABSL_ATTRIBUTE_SECTION(name). |
| 286 | // Returns 0 if no such functions exist. |
| 287 | // One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and |
| 288 | // link. |
| 289 | // |
| 290 | #define ABSL_ATTRIBUTE_SECTION_START(name) \ |
| 291 | (reinterpret_cast<void *>(__start_##name)) |
| 292 | #define ABSL_ATTRIBUTE_SECTION_STOP(name) \ |
| 293 | (reinterpret_cast<void *>(__stop_##name)) |
| 294 | #else // !ABSL_HAVE_ATTRIBUTE_SECTION |
| 295 | |
| 296 | #define ABSL_HAVE_ATTRIBUTE_SECTION 0 |
| 297 | |
| 298 | // provide dummy definitions |
| 299 | #define ABSL_ATTRIBUTE_SECTION(name) |
| 300 | #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) |
| 301 | #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name) |
| 302 | #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name) |
| 303 | #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) |
| 304 | #define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0)) |
| 305 | #define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0)) |
| 306 | #endif // ABSL_ATTRIBUTE_SECTION |
| 307 | |
| 308 | // ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 309 | // Support for aligning the stack on 32-bit x86. |
| 310 | #if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \ |
| 311 | (defined(__GNUC__) && !defined(__clang__)) |
| 312 | #if defined(__i386__) |
| 313 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \ |
| 314 | __attribute__((force_align_arg_pointer)) |
| 315 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
| 316 | #elif defined(__x86_64__) |
| 317 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1) |
| 318 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 319 | #else // !__i386__ && !__x86_64 |
| 320 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
| 321 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 322 | #endif // __i386__ |
| 323 | #else |
| 324 | #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC |
| 325 | #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0) |
| 326 | #endif |
| 327 | |
| 328 | // ABSL_MUST_USE_RESULT |
| 329 | // Tell the compiler to warn about unused return values for functions declared |
| 330 | // with this macro. The macro must appear as the very first part of a function |
| 331 | // declaration or definition: |
| 332 | // |
| 333 | // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket(); |
| 334 | // |
| 335 | // This placement has the broadest compatibility with GCC, Clang, and MSVC, with |
| 336 | // both defs and decls, and with GCC-style attributes, MSVC declspec, C++11 |
| 337 | // and C++17 attributes. |
| 338 | // |
| 339 | // ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result |
| 340 | // warning. For that, warn_unused_result is used only for clang but not for gcc. |
| 341 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 |
| 342 | // |
| 343 | // Note: past advice was to place the macro after the argument list. |
| 344 | #if ABSL_HAVE_ATTRIBUTE(nodiscard) |
| 345 | #define ABSL_MUST_USE_RESULT [[nodiscard]] |
| 346 | #elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result) |
| 347 | #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result)) |
| 348 | #else |
| 349 | #define ABSL_MUST_USE_RESULT |
| 350 | #endif |
| 351 | |
| 352 | // ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD |
| 353 | // Tell GCC that a function is hot or cold. GCC can use this information to |
| 354 | // improve static analysis, i.e. a conditional branch to a cold function |
| 355 | // is likely to be not-taken. |
| 356 | // This annotation is used for function declarations, e.g.: |
| 357 | // int foo() ABSL_ATTRIBUTE_HOT; |
| 358 | #if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__)) |
| 359 | #define ABSL_ATTRIBUTE_HOT __attribute__((hot)) |
| 360 | #else |
| 361 | #define ABSL_ATTRIBUTE_HOT |
| 362 | #endif |
| 363 | |
| 364 | #if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__)) |
| 365 | #define ABSL_ATTRIBUTE_COLD __attribute__((cold)) |
| 366 | #else |
| 367 | #define ABSL_ATTRIBUTE_COLD |
| 368 | #endif |
| 369 | |
| 370 | // ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS |
| 371 | // |
| 372 | // We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT |
| 373 | // macro used as an attribute to mark functions that must always or never be |
| 374 | // instrumented by XRay. Currently, this is only supported in Clang/LLVM. |
| 375 | // |
| 376 | // For reference on the LLVM XRay instrumentation, see |
| 377 | // http://llvm.org/docs/XRay.html. |
| 378 | // |
| 379 | // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration |
| 380 | // will always get the XRay instrumentation sleds. These sleds may introduce |
| 381 | // some binary size and runtime overhead and must be used sparingly. |
| 382 | // |
| 383 | // These attributes only take effect when the following conditions are met: |
| 384 | // |
| 385 | // - The file/target is built in at least C++11 mode, with a Clang compiler |
| 386 | // that supports XRay attributes. |
| 387 | // - The file/target is built with the -fxray-instrument flag set for the |
| 388 | // Clang/LLVM compiler. |
| 389 | // - The function is defined in the translation unit (the compiler honors the |
| 390 | // attribute in either the definition or the declaration, and must match). |
| 391 | // |
| 392 | // There are cases when, even when building with XRay instrumentation, users |
| 393 | // might want to control specifically which functions are instrumented for a |
| 394 | // particular build using special-case lists provided to the compiler. These |
| 395 | // special case lists are provided to Clang via the |
| 396 | // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The |
| 397 | // attributes in source take precedence over these special-case lists. |
| 398 | // |
| 399 | // To disable the XRay attributes at build-time, users may define |
| 400 | // ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific |
| 401 | // packages/targets, as this may lead to conflicting definitions of functions at |
| 402 | // link-time. |
| 403 | // |
| 404 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \ |
| 405 | !defined(ABSL_NO_XRAY_ATTRIBUTES) |
| 406 | #define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]] |
| 407 | #define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]] |
| 408 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args) |
| 409 | #define ABSL_XRAY_LOG_ARGS(N) \ |
| 410 | [[clang::xray_always_instrument, clang::xray_log_args(N)]] |
| 411 | #else |
| 412 | #define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]] |
| 413 | #endif |
| 414 | #else |
| 415 | #define ABSL_XRAY_ALWAYS_INSTRUMENT |
| 416 | #define ABSL_XRAY_NEVER_INSTRUMENT |
| 417 | #define ABSL_XRAY_LOG_ARGS(N) |
| 418 | #endif |
| 419 | |
| 420 | // ----------------------------------------------------------------------------- |
| 421 | // Variable Attributes |
| 422 | // ----------------------------------------------------------------------------- |
| 423 | |
| 424 | // ABSL_ATTRIBUTE_UNUSED |
| 425 | // Prevent the compiler from complaining about or optimizing away variables |
| 426 | // that appear unused. |
| 427 | #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__)) |
| 428 | #undef ABSL_ATTRIBUTE_UNUSED |
| 429 | #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__)) |
| 430 | #else |
| 431 | #define ABSL_ATTRIBUTE_UNUSED |
| 432 | #endif |
| 433 | // ABSL_ATTRIBUTE_INITIAL_EXEC |
| 434 | // Tell the compiler to use "initial-exec" mode for a thread-local variable. |
| 435 | // See http://people.redhat.com/drepper/tls.pdf for the gory details. |
| 436 | #if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__)) |
| 437 | #define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec"))) |
| 438 | #else |
| 439 | #define ABSL_ATTRIBUTE_INITIAL_EXEC |
| 440 | #endif |
| 441 | |
| 442 | // ABSL_ATTRIBUTE_PACKED |
| 443 | // Prevent the compiler from padding a structure to natural alignment |
| 444 | #if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__)) |
| 445 | #define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__)) |
| 446 | #else |
| 447 | #define ABSL_ATTRIBUTE_PACKED |
| 448 | #endif |
| 449 | |
| 450 | // ABSL_CONST_INIT |
| 451 | // A variable declaration annotated with the ABSL_CONST_INIT attribute will |
| 452 | // not compile (on supported platforms) unless the variable has a constant |
| 453 | // initializer. This is useful for variables with static and thread storage |
| 454 | // duration, because it guarantees that they will not suffer from the so-called |
| 455 | // "static init order fiasco". |
| 456 | // |
| 457 | // Sample usage: |
| 458 | // |
| 459 | // ABSL_CONST_INIT static MyType my_var = MakeMyType(...); |
| 460 | // |
| 461 | // Note that this attribute is redundant if the variable is declared constexpr. |
| 462 | #if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) |
| 463 | // NOLINTNEXTLINE(whitespace/braces) (b/36288871) |
| 464 | #define ABSL_CONST_INIT [[clang::require_constant_initialization]] |
| 465 | #else |
| 466 | #define ABSL_CONST_INIT |
| 467 | #endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) |
| 468 | |
| 469 | #endif // ABSL_BASE_ATTRIBUTES_H_ |