attributes.h 35 KB

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