attributes.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. //
  187. // Deprecated: Prefer the `[[noreturn]]` attribute standardized by C++11 over
  188. // this macro.
  189. #if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
  190. #define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
  191. #elif defined(_MSC_VER)
  192. #define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
  193. #else
  194. #define ABSL_ATTRIBUTE_NORETURN
  195. #endif
  196. // ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
  197. //
  198. // Tells the AddressSanitizer (or other memory testing tools) to ignore a given
  199. // function. Useful for cases when a function reads random locations on stack,
  200. // calls _exit from a cloned subprocess, deliberately accesses buffer
  201. // out of bounds or does other scary things with memory.
  202. // NOTE: GCC supports AddressSanitizer(asan) since 4.8.
  203. // https://gcc.gnu.org/gcc-4.8/changes.html
  204. #if defined(ABSL_HAVE_ADDRESS_SANITIZER) && \
  205. ABSL_HAVE_ATTRIBUTE(no_sanitize_address)
  206. #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
  207. #elif defined(ABSL_HAVE_ADDRESS_SANITIZER) && defined(_MSC_VER) && \
  208. _MSC_VER >= 1928
  209. // https://docs.microsoft.com/en-us/cpp/cpp/no-sanitize-address
  210. #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __declspec(no_sanitize_address)
  211. #elif defined(ABSL_HAVE_HWADDRESS_SANITIZER) && ABSL_HAVE_ATTRIBUTE(no_sanitize)
  212. // HWAddressSanitizer is a sanitizer similar to AddressSanitizer, which uses CPU
  213. // features to detect similar bugs with less CPU and memory overhead.
  214. // NOTE: GCC supports HWAddressSanitizer(hwasan) since 11.
  215. // https://gcc.gnu.org/gcc-11/changes.html
  216. #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS \
  217. __attribute__((no_sanitize("hwaddress")))
  218. #else
  219. #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
  220. #endif
  221. // ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
  222. //
  223. // Tells the MemorySanitizer to relax the handling of a given function. All "Use
  224. // of uninitialized value" warnings from such functions will be suppressed, and
  225. // all values loaded from memory will be considered fully initialized. This
  226. // attribute is similar to the ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute
  227. // above, but deals with initialized-ness rather than addressability issues.
  228. // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
  229. #if ABSL_HAVE_ATTRIBUTE(no_sanitize_memory)
  230. #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
  231. #else
  232. #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
  233. #endif
  234. // ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
  235. //
  236. // Tells the ThreadSanitizer to not instrument a given function.
  237. // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
  238. // https://gcc.gnu.org/gcc-4.8/changes.html
  239. #if ABSL_HAVE_ATTRIBUTE(no_sanitize_thread)
  240. #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
  241. #else
  242. #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
  243. #endif
  244. // ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
  245. //
  246. // Tells the UndefinedSanitizer to ignore a given function. Useful for cases
  247. // where certain behavior (eg. division by zero) is being used intentionally.
  248. // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
  249. // https://gcc.gnu.org/gcc-4.9/changes.html
  250. #if ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined)
  251. #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
  252. __attribute__((no_sanitize_undefined))
  253. #elif ABSL_HAVE_ATTRIBUTE(no_sanitize)
  254. #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
  255. __attribute__((no_sanitize("undefined")))
  256. #else
  257. #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
  258. #endif
  259. // ABSL_ATTRIBUTE_NO_SANITIZE_CFI
  260. //
  261. // Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
  262. // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
  263. #if ABSL_HAVE_ATTRIBUTE(no_sanitize) && defined(__llvm__)
  264. #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
  265. #else
  266. #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
  267. #endif
  268. // ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
  269. //
  270. // Tells the SafeStack to not instrument a given function.
  271. // See https://clang.llvm.org/docs/SafeStack.html for details.
  272. #if ABSL_HAVE_ATTRIBUTE(no_sanitize)
  273. #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \
  274. __attribute__((no_sanitize("safe-stack")))
  275. #else
  276. #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
  277. #endif
  278. // ABSL_ATTRIBUTE_RETURNS_NONNULL
  279. //
  280. // Tells the compiler that a particular function never returns a null pointer.
  281. #if ABSL_HAVE_ATTRIBUTE(returns_nonnull)
  282. #define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
  283. #else
  284. #define ABSL_ATTRIBUTE_RETURNS_NONNULL
  285. #endif
  286. // ABSL_HAVE_ATTRIBUTE_SECTION
  287. //
  288. // Indicates whether labeled sections are supported. Weak symbol support is
  289. // a prerequisite. Labeled sections are not supported on Darwin/iOS.
  290. #ifdef ABSL_HAVE_ATTRIBUTE_SECTION
  291. #error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
  292. #elif (ABSL_HAVE_ATTRIBUTE(section) || \
  293. (defined(__GNUC__) && !defined(__clang__))) && \
  294. !defined(__APPLE__) && ABSL_HAVE_ATTRIBUTE_WEAK
  295. #define ABSL_HAVE_ATTRIBUTE_SECTION 1
  296. // ABSL_ATTRIBUTE_SECTION
  297. //
  298. // Tells the compiler/linker to put a given function into a section and define
  299. // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
  300. // This functionality is supported by GNU linker. Any function annotated with
  301. // `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
  302. // whatever section its caller is placed into.
  303. //
  304. #ifndef ABSL_ATTRIBUTE_SECTION
  305. #define ABSL_ATTRIBUTE_SECTION(name) \
  306. __attribute__((section(#name))) __attribute__((noinline))
  307. #endif
  308. // ABSL_ATTRIBUTE_SECTION_VARIABLE
  309. //
  310. // Tells the compiler/linker to put a given variable into a section and define
  311. // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
  312. // This functionality is supported by GNU linker.
  313. #ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
  314. #ifdef _AIX
  315. // __attribute__((section(#name))) on AIX is achieved by using the `.csect`
  316. // psudo op which includes an additional integer as part of its syntax indcating
  317. // alignment. If data fall under different alignments then you might get a
  318. // compilation error indicating a `Section type conflict`.
  319. #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
  320. #else
  321. #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
  322. #endif
  323. #endif
  324. // ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
  325. //
  326. // A weak section declaration to be used as a global declaration
  327. // for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
  328. // even without functions with ABSL_ATTRIBUTE_SECTION(name).
  329. // ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
  330. // a no-op on ELF but not on Mach-O.
  331. //
  332. #ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
  333. #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
  334. extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
  335. extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
  336. #endif
  337. #ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
  338. #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
  339. #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
  340. #endif
  341. // ABSL_ATTRIBUTE_SECTION_START
  342. //
  343. // Returns `void*` pointers to start/end of a section of code with
  344. // functions having ABSL_ATTRIBUTE_SECTION(name).
  345. // Returns 0 if no such functions exist.
  346. // One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
  347. // link.
  348. //
  349. #define ABSL_ATTRIBUTE_SECTION_START(name) \
  350. (reinterpret_cast<void *>(__start_##name))
  351. #define ABSL_ATTRIBUTE_SECTION_STOP(name) \
  352. (reinterpret_cast<void *>(__stop_##name))
  353. #else // !ABSL_HAVE_ATTRIBUTE_SECTION
  354. #define ABSL_HAVE_ATTRIBUTE_SECTION 0
  355. // provide dummy definitions
  356. #define ABSL_ATTRIBUTE_SECTION(name)
  357. #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
  358. #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
  359. #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
  360. #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
  361. #define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
  362. #define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
  363. #endif // ABSL_ATTRIBUTE_SECTION
  364. // ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  365. //
  366. // Support for aligning the stack on 32-bit x86.
  367. #if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
  368. (defined(__GNUC__) && !defined(__clang__))
  369. #if defined(__i386__)
  370. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
  371. __attribute__((force_align_arg_pointer))
  372. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  373. #elif defined(__x86_64__)
  374. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
  375. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  376. #else // !__i386__ && !__x86_64
  377. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  378. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  379. #endif // __i386__
  380. #else
  381. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  382. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  383. #endif
  384. // ABSL_MUST_USE_RESULT
  385. //
  386. // Tells the compiler to warn about unused results.
  387. //
  388. // For code or headers that are assured to only build with C++17 and up, prefer
  389. // just using the standard `[[nodiscard]]` directly over this macro.
  390. //
  391. // When annotating a function, it must appear as the first part of the
  392. // declaration or definition. The compiler will warn if the return value from
  393. // such a function is unused:
  394. //
  395. // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
  396. // AllocateSprocket(); // Triggers a warning.
  397. //
  398. // When annotating a class, it is equivalent to annotating every function which
  399. // returns an instance.
  400. //
  401. // class ABSL_MUST_USE_RESULT Sprocket {};
  402. // Sprocket(); // Triggers a warning.
  403. //
  404. // Sprocket MakeSprocket();
  405. // MakeSprocket(); // Triggers a warning.
  406. //
  407. // Note that references and pointers are not instances:
  408. //
  409. // Sprocket* SprocketPointer();
  410. // SprocketPointer(); // Does *not* trigger a warning.
  411. //
  412. // ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
  413. // warning. For that, warn_unused_result is used only for clang but not for gcc.
  414. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
  415. //
  416. // Note: past advice was to place the macro after the argument list.
  417. //
  418. // TODO(b/176172494): Use ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) when all code is
  419. // compliant with the stricter [[nodiscard]].
  420. #if defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
  421. #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
  422. #else
  423. #define ABSL_MUST_USE_RESULT
  424. #endif
  425. // ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
  426. //
  427. // Tells GCC that a function is hot or cold. GCC can use this information to
  428. // improve static analysis, i.e. a conditional branch to a cold function
  429. // is likely to be not-taken.
  430. // This annotation is used for function declarations.
  431. //
  432. // Example:
  433. //
  434. // int foo() ABSL_ATTRIBUTE_HOT;
  435. #if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
  436. #define ABSL_ATTRIBUTE_HOT __attribute__((hot))
  437. #else
  438. #define ABSL_ATTRIBUTE_HOT
  439. #endif
  440. #if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
  441. #define ABSL_ATTRIBUTE_COLD __attribute__((cold))
  442. #else
  443. #define ABSL_ATTRIBUTE_COLD
  444. #endif
  445. // ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
  446. //
  447. // We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
  448. // macro used as an attribute to mark functions that must always or never be
  449. // instrumented by XRay. Currently, this is only supported in Clang/LLVM.
  450. //
  451. // For reference on the LLVM XRay instrumentation, see
  452. // http://llvm.org/docs/XRay.html.
  453. //
  454. // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
  455. // will always get the XRay instrumentation sleds. These sleds may introduce
  456. // some binary size and runtime overhead and must be used sparingly.
  457. //
  458. // These attributes only take effect when the following conditions are met:
  459. //
  460. // * The file/target is built in at least C++11 mode, with a Clang compiler
  461. // that supports XRay attributes.
  462. // * The file/target is built with the -fxray-instrument flag set for the
  463. // Clang/LLVM compiler.
  464. // * The function is defined in the translation unit (the compiler honors the
  465. // attribute in either the definition or the declaration, and must match).
  466. //
  467. // There are cases when, even when building with XRay instrumentation, users
  468. // might want to control specifically which functions are instrumented for a
  469. // particular build using special-case lists provided to the compiler. These
  470. // special case lists are provided to Clang via the
  471. // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
  472. // attributes in source take precedence over these special-case lists.
  473. //
  474. // To disable the XRay attributes at build-time, users may define
  475. // ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
  476. // packages/targets, as this may lead to conflicting definitions of functions at
  477. // link-time.
  478. //
  479. // XRay isn't currently supported on Android:
  480. // https://github.com/android/ndk/issues/368
  481. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
  482. !defined(ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__)
  483. #define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
  484. #define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
  485. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
  486. #define ABSL_XRAY_LOG_ARGS(N) \
  487. [[clang::xray_always_instrument, clang::xray_log_args(N)]]
  488. #else
  489. #define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
  490. #endif
  491. #else
  492. #define ABSL_XRAY_ALWAYS_INSTRUMENT
  493. #define ABSL_XRAY_NEVER_INSTRUMENT
  494. #define ABSL_XRAY_LOG_ARGS(N)
  495. #endif
  496. // ABSL_ATTRIBUTE_REINITIALIZES
  497. //
  498. // Indicates that a member function reinitializes the entire object to a known
  499. // state, independent of the previous state of the object.
  500. //
  501. // The clang-tidy check bugprone-use-after-move allows member functions marked
  502. // with this attribute to be called on objects that have been moved from;
  503. // without the attribute, this would result in a use-after-move warning.
  504. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
  505. #define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
  506. #else
  507. #define ABSL_ATTRIBUTE_REINITIALIZES
  508. #endif
  509. // -----------------------------------------------------------------------------
  510. // Variable Attributes
  511. // -----------------------------------------------------------------------------
  512. // ABSL_ATTRIBUTE_UNUSED
  513. //
  514. // Prevents the compiler from complaining about variables that appear unused.
  515. //
  516. // For code or headers that are assured to only build with C++17 and up, prefer
  517. // just using the standard '[[maybe_unused]]' directly over this macro.
  518. //
  519. // Due to differences in positioning requirements between the old, compiler
  520. // specific __attribute__ syntax and the now standard [[maybe_unused]], this
  521. // macro does not attempt to take advantage of '[[maybe_unused]]'.
  522. #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
  523. #undef ABSL_ATTRIBUTE_UNUSED
  524. #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
  525. #else
  526. #define ABSL_ATTRIBUTE_UNUSED
  527. #endif
  528. // ABSL_ATTRIBUTE_INITIAL_EXEC
  529. //
  530. // Tells the compiler to use "initial-exec" mode for a thread-local variable.
  531. // See http://people.redhat.com/drepper/tls.pdf for the gory details.
  532. #if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
  533. #define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
  534. #else
  535. #define ABSL_ATTRIBUTE_INITIAL_EXEC
  536. #endif
  537. // ABSL_ATTRIBUTE_PACKED
  538. //
  539. // Instructs the compiler not to use natural alignment for a tagged data
  540. // structure, but instead to reduce its alignment to 1.
  541. //
  542. // Therefore, DO NOT APPLY THIS ATTRIBUTE TO STRUCTS CONTAINING ATOMICS. Doing
  543. // so can cause atomic variables to be mis-aligned and silently violate
  544. // atomicity on x86.
  545. //
  546. // This attribute can either be applied to members of a structure or to a
  547. // structure in its entirety. Applying this attribute (judiciously) to a
  548. // structure in its entirety to optimize the memory footprint of very
  549. // commonly-used structs is fine. Do not apply this attribute to a structure in
  550. // its entirety if the purpose is to control the offsets of the members in the
  551. // structure. Instead, apply this attribute only to structure members that need
  552. // it.
  553. //
  554. // When applying ABSL_ATTRIBUTE_PACKED only to specific structure members the
  555. // natural alignment of structure members not annotated is preserved. Aligned
  556. // member accesses are faster than non-aligned member accesses even if the
  557. // targeted microprocessor supports non-aligned accesses.
  558. #if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
  559. #define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
  560. #else
  561. #define ABSL_ATTRIBUTE_PACKED
  562. #endif
  563. // ABSL_ATTRIBUTE_FUNC_ALIGN
  564. //
  565. // Tells the compiler to align the function start at least to certain
  566. // alignment boundary
  567. #if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
  568. #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
  569. #else
  570. #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
  571. #endif
  572. // ABSL_FALLTHROUGH_INTENDED
  573. //
  574. // Annotates implicit fall-through between switch labels, allowing a case to
  575. // indicate intentional fallthrough and turn off warnings about any lack of a
  576. // `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
  577. // a semicolon and can be used in most places where `break` can, provided that
  578. // no statements exist between it and the next switch label.
  579. //
  580. // Example:
  581. //
  582. // switch (x) {
  583. // case 40:
  584. // case 41:
  585. // if (truth_is_out_there) {
  586. // ++x;
  587. // ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
  588. // // in comments
  589. // } else {
  590. // return x;
  591. // }
  592. // case 42:
  593. // ...
  594. //
  595. // Notes: When supported, GCC and Clang can issue a warning on switch labels
  596. // with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See
  597. // clang documentation on language extensions for details:
  598. // https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
  599. //
  600. // When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro has
  601. // no effect on diagnostics. In any case this macro has no effect on runtime
  602. // behavior and performance of code.
  603. #ifdef ABSL_FALLTHROUGH_INTENDED
  604. #error "ABSL_FALLTHROUGH_INTENDED should not be defined."
  605. #elif ABSL_HAVE_CPP_ATTRIBUTE(fallthrough)
  606. #define ABSL_FALLTHROUGH_INTENDED [[fallthrough]]
  607. #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough)
  608. #define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
  609. #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough)
  610. #define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
  611. #else
  612. #define ABSL_FALLTHROUGH_INTENDED \
  613. do { \
  614. } while (0)
  615. #endif
  616. // ABSL_DEPRECATED()
  617. //
  618. // Marks a deprecated class, struct, enum, function, method and variable
  619. // declarations. The macro argument is used as a custom diagnostic message (e.g.
  620. // suggestion of a better alternative).
  621. //
  622. // For code or headers that are assured to only build with C++14 and up, prefer
  623. // just using the standard `[[deprecated("message")]]` directly over this macro.
  624. //
  625. // Examples:
  626. //
  627. // class ABSL_DEPRECATED("Use Bar instead") Foo {...};
  628. //
  629. // ABSL_DEPRECATED("Use Baz() instead") void Bar() {...}
  630. //
  631. // template <typename T>
  632. // ABSL_DEPRECATED("Use DoThat() instead")
  633. // void DoThis();
  634. //
  635. // enum FooEnum {
  636. // kBar ABSL_DEPRECATED("Use kBaz instead"),
  637. // };
  638. //
  639. // Every usage of a deprecated entity will trigger a warning when compiled with
  640. // GCC/Clang's `-Wdeprecated-declarations` option. Google's production toolchain
  641. // turns this warning off by default, instead relying on clang-tidy to report
  642. // new uses of deprecated code.
  643. #if ABSL_HAVE_ATTRIBUTE(deprecated)
  644. #define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
  645. #else
  646. #define ABSL_DEPRECATED(message)
  647. #endif
  648. // When deprecating Abseil code, it is sometimes necessary to turn off the
  649. // warning within Abseil, until the deprecated code is actually removed. The
  650. // deprecated code can be surrounded with these directives to achieve that
  651. // result.
  652. //
  653. // class ABSL_DEPRECATED("Use Bar instead") Foo;
  654. //
  655. // ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
  656. // Baz ComputeBazFromFoo(Foo f);
  657. // ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING
  658. #if defined(__GNUC__) || defined(__clang__)
  659. // Clang also supports these GCC pragmas.
  660. #define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING \
  661. _Pragma("GCC diagnostic push") \
  662. _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
  663. #define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING \
  664. _Pragma("GCC diagnostic pop")
  665. #elif defined(_MSC_VER)
  666. #define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING \
  667. _Pragma("warning(push)") _Pragma("warning(disable: 4996)")
  668. #define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING \
  669. _Pragma("warning(pop)")
  670. #else
  671. #define ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
  672. #define ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING
  673. #endif // defined(__GNUC__) || defined(__clang__)
  674. // ABSL_CONST_INIT
  675. //
  676. // A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
  677. // not compile (on supported platforms) unless the variable has a constant
  678. // initializer. This is useful for variables with static and thread storage
  679. // duration, because it guarantees that they will not suffer from the so-called
  680. // "static init order fiasco".
  681. //
  682. // This attribute must be placed on the initializing declaration of the
  683. // variable. Some compilers will give a -Wmissing-constinit warning when this
  684. // attribute is placed on some other declaration but missing from the
  685. // initializing declaration.
  686. //
  687. // In some cases (notably with thread_local variables), `ABSL_CONST_INIT` can
  688. // also be used in a non-initializing declaration to tell the compiler that a
  689. // variable is already initialized, reducing overhead that would otherwise be
  690. // incurred by a hidden guard variable. Thus annotating all declarations with
  691. // this attribute is recommended to potentially enhance optimization.
  692. //
  693. // Example:
  694. //
  695. // class MyClass {
  696. // public:
  697. // ABSL_CONST_INIT static MyType my_var;
  698. // };
  699. //
  700. // ABSL_CONST_INIT MyType MyClass::my_var = MakeMyType(...);
  701. //
  702. // For code or headers that are assured to only build with C++20 and up, prefer
  703. // just using the standard `constinit` keyword directly over this macro.
  704. //
  705. // Note that this attribute is redundant if the variable is declared constexpr.
  706. #if defined(__cpp_constinit) && __cpp_constinit >= 201907L
  707. #define ABSL_CONST_INIT constinit
  708. #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
  709. #define ABSL_CONST_INIT [[clang::require_constant_initialization]]
  710. #else
  711. #define ABSL_CONST_INIT
  712. #endif
  713. // ABSL_ATTRIBUTE_PURE_FUNCTION
  714. //
  715. // ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure"
  716. // functions. A function is pure if its return value is only a function of its
  717. // arguments. The pure attribute prohibits a function from modifying the state
  718. // of the program that is observable by means other than inspecting the
  719. // function's return value. Declaring such functions with the pure attribute
  720. // allows the compiler to avoid emitting some calls in repeated invocations of
  721. // the function with the same argument values.
  722. //
  723. // Example:
  724. //
  725. // ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(Time t);
  726. #if ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure)
  727. #define ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]]
  728. #elif ABSL_HAVE_ATTRIBUTE(pure)
  729. #define ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure))
  730. #else
  731. // If the attribute isn't defined, we'll fallback to ABSL_MUST_USE_RESULT since
  732. // pure functions are useless if its return is ignored.
  733. #define ABSL_ATTRIBUTE_PURE_FUNCTION ABSL_MUST_USE_RESULT
  734. #endif
  735. // ABSL_ATTRIBUTE_CONST_FUNCTION
  736. //
  737. // ABSL_ATTRIBUTE_CONST_FUNCTION is used to annotate declarations of "const"
  738. // functions. A const function is similar to a pure function, with one
  739. // exception: Pure functions may return value that depend on a non-volatile
  740. // object that isn't provided as a function argument, while the const function
  741. // is guaranteed to return the same result given the same arguments.
  742. //
  743. // Example:
  744. //
  745. // ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Milliseconds(Duration d);
  746. #if defined(_MSC_VER) && !defined(__clang__)
  747. // Put the MSVC case first since MSVC seems to parse const as a C++ keyword.
  748. #define ABSL_ATTRIBUTE_CONST_FUNCTION ABSL_ATTRIBUTE_PURE_FUNCTION
  749. #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::const)
  750. #define ABSL_ATTRIBUTE_CONST_FUNCTION [[gnu::const]]
  751. #elif ABSL_HAVE_ATTRIBUTE(const)
  752. #define ABSL_ATTRIBUTE_CONST_FUNCTION __attribute__((const))
  753. #else
  754. // Since const functions are more restrictive pure function, we'll fallback to a
  755. // pure function if the const attribute is not handled.
  756. #define ABSL_ATTRIBUTE_CONST_FUNCTION ABSL_ATTRIBUTE_PURE_FUNCTION
  757. #endif
  758. // ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
  759. // parameter or implicit object parameter is retained by the return value of the
  760. // annotated function (or, for a parameter of a constructor, in the value of the
  761. // constructed object). This attribute causes warnings to be produced if a
  762. // temporary object does not live long enough.
  763. //
  764. // When applied to a reference parameter, the referenced object is assumed to be
  765. // retained by the return value of the function. When applied to a non-reference
  766. // parameter (for example, a pointer or a class type), all temporaries
  767. // referenced by the parameter are assumed to be retained by the return value of
  768. // the function.
  769. //
  770. // See also the upstream documentation:
  771. // https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
  772. // https://learn.microsoft.com/en-us/cpp/code-quality/c26816?view=msvc-170
  773. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
  774. #define ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
  775. #elif ABSL_HAVE_CPP_ATTRIBUTE(msvc::lifetimebound)
  776. #define ABSL_ATTRIBUTE_LIFETIME_BOUND [[msvc::lifetimebound]]
  777. #elif ABSL_HAVE_ATTRIBUTE(lifetimebound)
  778. #define ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
  779. #else
  780. #define ABSL_ATTRIBUTE_LIFETIME_BOUND
  781. #endif
  782. // ABSL_INTERNAL_ATTRIBUTE_VIEW indicates that a type acts like a view i.e. a
  783. // raw (non-owning) pointer. This enables diagnoses similar to those enabled by
  784. // ABSL_ATTRIBUTE_LIFETIME_BOUND.
  785. //
  786. // See the following links for details:
  787. // https://reviews.llvm.org/D64448
  788. // https://lists.llvm.org/pipermail/cfe-dev/2018-November/060355.html
  789. #if ABSL_HAVE_CPP_ATTRIBUTE(gsl::Pointer)
  790. #define ABSL_INTERNAL_ATTRIBUTE_VIEW [[gsl::Pointer]]
  791. #else
  792. #define ABSL_INTERNAL_ATTRIBUTE_VIEW
  793. #endif
  794. // ABSL_INTERNAL_ATTRIBUTE_OWNER indicates that a type acts like a smart
  795. // (owning) pointer. This enables diagnoses similar to those enabled by
  796. // ABSL_ATTRIBUTE_LIFETIME_BOUND.
  797. //
  798. // See the following links for details:
  799. // https://reviews.llvm.org/D64448
  800. // https://lists.llvm.org/pipermail/cfe-dev/2018-November/060355.html
  801. #if ABSL_HAVE_CPP_ATTRIBUTE(gsl::Owner)
  802. #define ABSL_INTERNAL_ATTRIBUTE_OWNER [[gsl::Owner]]
  803. #else
  804. #define ABSL_INTERNAL_ATTRIBUTE_OWNER
  805. #endif
  806. // ABSL_ATTRIBUTE_TRIVIAL_ABI
  807. // Indicates that a type is "trivially relocatable" -- meaning it can be
  808. // relocated without invoking the constructor/destructor, using a form of move
  809. // elision.
  810. //
  811. // From a memory safety point of view, putting aside destructor ordering, it's
  812. // safe to apply ABSL_ATTRIBUTE_TRIVIAL_ABI if an object's location
  813. // can change over the course of its lifetime: if a constructor can be run one
  814. // place, and then the object magically teleports to another place where some
  815. // methods are run, and then the object teleports to yet another place where it
  816. // is destroyed. This is notably not true for self-referential types, where the
  817. // move-constructor must keep the self-reference up to date. If the type changed
  818. // location without invoking the move constructor, it would have a dangling
  819. // self-reference.
  820. //
  821. // The use of this teleporting machinery means that the number of paired
  822. // move/destroy operations can change, and so it is a bad idea to apply this to
  823. // a type meant to count the number of moves.
  824. //
  825. // Warning: applying this can, rarely, break callers. Objects passed by value
  826. // will be destroyed at the end of the call, instead of the end of the
  827. // full-expression containing the call. In addition, it changes the ABI
  828. // of functions accepting this type by value (e.g. to pass in registers).
  829. //
  830. // See also the upstream documentation:
  831. // https://clang.llvm.org/docs/AttributeReference.html#trivial-abi
  832. //
  833. // b/321691395 - This is currently disabled in open-source builds since
  834. // compiler support differs. If system libraries compiled with GCC are mixed
  835. // with libraries compiled with Clang, types will have different ideas about
  836. // their ABI, leading to hard to debug crashes.
  837. #define ABSL_ATTRIBUTE_TRIVIAL_ABI
  838. // ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS
  839. //
  840. // Indicates a data member can be optimized to occupy no space (if it is empty)
  841. // and/or its tail padding can be used for other members.
  842. //
  843. // For code that is assured to only build with C++20 or later, prefer using
  844. // the standard attribute `[[no_unique_address]]` directly instead of this
  845. // macro.
  846. //
  847. // https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/#c20-no_unique_address
  848. // Current versions of MSVC have disabled `[[no_unique_address]]` since it
  849. // breaks ABI compatibility, but offers `[[msvc::no_unique_address]]` for
  850. // situations when it can be assured that it is desired. Since Abseil does not
  851. // claim ABI compatibility in mixed builds, we can offer it unconditionally.
  852. #if defined(_MSC_VER) && _MSC_VER >= 1929
  853. #define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
  854. #elif ABSL_HAVE_CPP_ATTRIBUTE(no_unique_address)
  855. #define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS [[no_unique_address]]
  856. #else
  857. #define ABSL_ATTRIBUTE_NO_UNIQUE_ADDRESS
  858. #endif
  859. // ABSL_ATTRIBUTE_UNINITIALIZED
  860. //
  861. // GCC and Clang support a flag `-ftrivial-auto-var-init=<option>` (<option>
  862. // can be "zero" or "pattern") that can be used to initialize automatic stack
  863. // variables. Variables with this attribute will be left uninitialized,
  864. // overriding the compiler flag.
  865. //
  866. // See https://clang.llvm.org/docs/AttributeReference.html#uninitialized
  867. // and https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-uninitialized-variable-attribute
  868. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::uninitialized)
  869. #define ABSL_ATTRIBUTE_UNINITIALIZED [[clang::uninitialized]]
  870. #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::uninitialized)
  871. #define ABSL_ATTRIBUTE_UNINITIALIZED [[gnu::uninitialized]]
  872. #elif ABSL_HAVE_ATTRIBUTE(uninitialized)
  873. #define ABSL_ATTRIBUTE_UNINITIALIZED __attribute__((uninitialized))
  874. #else
  875. #define ABSL_ATTRIBUTE_UNINITIALIZED
  876. #endif
  877. // ABSL_ATTRIBUTE_WARN_UNUSED
  878. //
  879. // Compilers routinely warn about trivial variables that are unused. For
  880. // non-trivial types, this warning is suppressed since the
  881. // constructor/destructor may be intentional and load-bearing, for example, with
  882. // a RAII scoped lock.
  883. //
  884. // For example:
  885. //
  886. // class ABSL_ATTRIBUTE_WARN_UNUSED MyType {
  887. // public:
  888. // MyType();
  889. // ~MyType();
  890. // };
  891. //
  892. // void foo() {
  893. // // Warns with ABSL_ATTRIBUTE_WARN_UNUSED attribute present.
  894. // MyType unused;
  895. // }
  896. //
  897. // See https://clang.llvm.org/docs/AttributeReference.html#warn-unused and
  898. // https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#index-warn_005funused-type-attribute
  899. #if ABSL_HAVE_CPP_ATTRIBUTE(gnu::warn_unused)
  900. #define ABSL_ATTRIBUTE_WARN_UNUSED [[gnu::warn_unused]]
  901. #else
  902. #define ABSL_ATTRIBUTE_WARN_UNUSED
  903. #endif
  904. #endif // ABSL_BASE_ATTRIBUTES_H_