attributes.h 40 KB

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