attributes.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 Y_ABSL_BASE_ATTRIBUTES_H_
  34. #define Y_ABSL_BASE_ATTRIBUTES_H_
  35. #include "y_absl/base/config.h"
  36. // Y_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 Y_ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
  48. #else
  49. #define Y_ABSL_HAVE_ATTRIBUTE(x) 0
  50. #endif
  51. // Y_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 Y_ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
  61. #else
  62. #define Y_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. // Y_ABSL_PRINTF_ATTRIBUTE
  71. // Y_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 Y_ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
  81. #define Y_ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
  82. __attribute__((__format__(__printf__, string_index, first_to_check)))
  83. #define Y_ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
  84. __attribute__((__format__(__scanf__, string_index, first_to_check)))
  85. #else
  86. #define Y_ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
  87. #define Y_ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
  88. #endif
  89. // Y_ABSL_ATTRIBUTE_ALWAYS_INLINE
  90. // Y_ABSL_ATTRIBUTE_NOINLINE
  91. //
  92. // Forces functions to either inline or not inline. Introduced in gcc 3.1.
  93. #if Y_ABSL_HAVE_ATTRIBUTE(always_inline) || \
  94. (defined(__GNUC__) && !defined(__clang__))
  95. #define Y_ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
  96. #define Y_ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
  97. #else
  98. #define Y_ABSL_ATTRIBUTE_ALWAYS_INLINE
  99. #endif
  100. #if Y_ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
  101. #define Y_ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
  102. #define Y_ABSL_HAVE_ATTRIBUTE_NOINLINE 1
  103. #else
  104. #define Y_ABSL_ATTRIBUTE_NOINLINE
  105. #endif
  106. // Y_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 Y_ABSL_HAVE_ATTRIBUTE(disable_tail_calls)
  111. #define Y_ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
  112. #define Y_ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
  113. #elif defined(__GNUC__) && !defined(__clang__) && !defined(__e2k__)
  114. #define Y_ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
  115. #define Y_ABSL_ATTRIBUTE_NO_TAIL_CALL \
  116. __attribute__((optimize("no-optimize-sibling-calls")))
  117. #else
  118. #define Y_ABSL_ATTRIBUTE_NO_TAIL_CALL
  119. #define Y_ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
  120. #endif
  121. // Y_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 (Y_ABSL_HAVE_ATTRIBUTE(weak) || \
  130. (defined(__GNUC__) && !defined(__clang__))) && \
  131. (!defined(_WIN32) || __clang_major__ < 9) && !defined(__MINGW32__)
  132. #undef Y_ABSL_ATTRIBUTE_WEAK
  133. #define Y_ABSL_ATTRIBUTE_WEAK __attribute__((weak))
  134. #define Y_ABSL_HAVE_ATTRIBUTE_WEAK 1
  135. #else
  136. #define Y_ABSL_ATTRIBUTE_WEAK
  137. #define Y_ABSL_HAVE_ATTRIBUTE_WEAK 0
  138. #endif
  139. // Y_ABSL_ATTRIBUTE_NONNULL
  140. //
  141. // Tells the compiler either (a) that a particular function parameter
  142. // should be a non-null pointer, or (b) that all pointer arguments should
  143. // be non-null.
  144. //
  145. // Note: As the GCC manual states, "[s]ince non-static C++ methods
  146. // have an implicit 'this' argument, the arguments of such methods
  147. // should be counted from two, not one."
  148. //
  149. // Args are indexed starting at 1.
  150. //
  151. // For non-static class member functions, the implicit `this` argument
  152. // is arg 1, and the first explicit argument is arg 2. For static class member
  153. // functions, there is no implicit `this`, and the first explicit argument is
  154. // arg 1.
  155. //
  156. // Example:
  157. //
  158. // /* arg_a cannot be null, but arg_b can */
  159. // void Function(void* arg_a, void* arg_b) Y_ABSL_ATTRIBUTE_NONNULL(1);
  160. //
  161. // class C {
  162. // /* arg_a cannot be null, but arg_b can */
  163. // void Method(void* arg_a, void* arg_b) Y_ABSL_ATTRIBUTE_NONNULL(2);
  164. //
  165. // /* arg_a cannot be null, but arg_b can */
  166. // static void StaticMethod(void* arg_a, void* arg_b)
  167. // Y_ABSL_ATTRIBUTE_NONNULL(1);
  168. // };
  169. //
  170. // If no arguments are provided, then all pointer arguments should be non-null.
  171. //
  172. // /* No pointer arguments may be null. */
  173. // void Function(void* arg_a, void* arg_b, int arg_c) Y_ABSL_ATTRIBUTE_NONNULL();
  174. //
  175. // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
  176. // Y_ABSL_ATTRIBUTE_NONNULL does not.
  177. #if Y_ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
  178. #define Y_ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
  179. #else
  180. #define Y_ABSL_ATTRIBUTE_NONNULL(...)
  181. #endif
  182. // Y_ABSL_ATTRIBUTE_NORETURN
  183. //
  184. // Tells the compiler that a given function never returns.
  185. #if Y_ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
  186. #define Y_ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
  187. #elif defined(_MSC_VER)
  188. #define Y_ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
  189. #else
  190. #define Y_ABSL_ATTRIBUTE_NORETURN
  191. #endif
  192. // Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
  193. //
  194. // Tells the AddressSanitizer (or other memory testing tools) to ignore a given
  195. // function. Useful for cases when a function reads random locations on stack,
  196. // calls _exit from a cloned subprocess, deliberately accesses buffer
  197. // out of bounds or does other scary things with memory.
  198. // NOTE: GCC supports AddressSanitizer(asan) since 4.8.
  199. // https://gcc.gnu.org/gcc-4.8/changes.html
  200. #if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_address)
  201. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
  202. #else
  203. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
  204. #endif
  205. // Y_ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
  206. //
  207. // Tells the MemorySanitizer to relax the handling of a given function. All "Use
  208. // of uninitialized value" warnings from such functions will be suppressed, and
  209. // all values loaded from memory will be considered fully initialized. This
  210. // attribute is similar to the Y_ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute
  211. // above, but deals with initialized-ness rather than addressability issues.
  212. // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
  213. #if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_memory)
  214. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
  215. #else
  216. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
  217. #endif
  218. // Y_ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
  219. //
  220. // Tells the ThreadSanitizer to not instrument a given function.
  221. // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
  222. // https://gcc.gnu.org/gcc-4.8/changes.html
  223. #if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_thread)
  224. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
  225. #else
  226. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
  227. #endif
  228. // Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
  229. //
  230. // Tells the UndefinedSanitizer to ignore a given function. Useful for cases
  231. // where certain behavior (eg. division by zero) is being used intentionally.
  232. // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
  233. // https://gcc.gnu.org/gcc-4.9/changes.html
  234. #if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined)
  235. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
  236. __attribute__((no_sanitize_undefined))
  237. #elif Y_ABSL_HAVE_ATTRIBUTE(no_sanitize)
  238. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
  239. __attribute__((no_sanitize("undefined")))
  240. #else
  241. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
  242. #endif
  243. // Y_ABSL_ATTRIBUTE_NO_SANITIZE_CFI
  244. //
  245. // Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
  246. // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
  247. #if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize)
  248. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
  249. #else
  250. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_CFI
  251. #endif
  252. // Y_ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
  253. //
  254. // Tells the SafeStack to not instrument a given function.
  255. // See https://clang.llvm.org/docs/SafeStack.html for details.
  256. #if Y_ABSL_HAVE_ATTRIBUTE(no_sanitize)
  257. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \
  258. __attribute__((no_sanitize("safe-stack")))
  259. #else
  260. #define Y_ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
  261. #endif
  262. // Y_ABSL_ATTRIBUTE_RETURNS_NONNULL
  263. //
  264. // Tells the compiler that a particular function never returns a null pointer.
  265. #if Y_ABSL_HAVE_ATTRIBUTE(returns_nonnull)
  266. #define Y_ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
  267. #else
  268. #define Y_ABSL_ATTRIBUTE_RETURNS_NONNULL
  269. #endif
  270. // Y_ABSL_HAVE_ATTRIBUTE_SECTION
  271. //
  272. // Indicates whether labeled sections are supported. Weak symbol support is
  273. // a prerequisite. Labeled sections are not supported on Darwin/iOS.
  274. #ifdef Y_ABSL_HAVE_ATTRIBUTE_SECTION
  275. #error Y_ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
  276. #elif (Y_ABSL_HAVE_ATTRIBUTE(section) || \
  277. (defined(__GNUC__) && !defined(__clang__))) && \
  278. !defined(__APPLE__) && Y_ABSL_HAVE_ATTRIBUTE_WEAK
  279. #define Y_ABSL_HAVE_ATTRIBUTE_SECTION 1
  280. // Y_ABSL_ATTRIBUTE_SECTION
  281. //
  282. // Tells the compiler/linker to put a given function into a section and define
  283. // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
  284. // This functionality is supported by GNU linker. Any function annotated with
  285. // `Y_ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
  286. // whatever section its caller is placed into.
  287. //
  288. #ifndef Y_ABSL_ATTRIBUTE_SECTION
  289. #define Y_ABSL_ATTRIBUTE_SECTION(name) \
  290. __attribute__((section(#name))) __attribute__((noinline))
  291. #endif
  292. // Y_ABSL_ATTRIBUTE_SECTION_VARIABLE
  293. //
  294. // Tells the compiler/linker to put a given variable into a section and define
  295. // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
  296. // This functionality is supported by GNU linker.
  297. #ifndef Y_ABSL_ATTRIBUTE_SECTION_VARIABLE
  298. #ifdef _AIX
  299. // __attribute__((section(#name))) on AIX is achived by using the `.csect` psudo
  300. // op which includes an additional integer as part of its syntax indcating
  301. // alignment. If data fall under different alignments then you might get a
  302. // compilation error indicating a `Section type conflict`.
  303. #define Y_ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
  304. #else
  305. #define Y_ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
  306. #endif
  307. #endif
  308. // Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
  309. //
  310. // A weak section declaration to be used as a global declaration
  311. // for Y_ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
  312. // even without functions with Y_ABSL_ATTRIBUTE_SECTION(name).
  313. // Y_ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
  314. // a no-op on ELF but not on Mach-O.
  315. //
  316. #ifndef Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
  317. #define Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
  318. extern char __start_##name[] Y_ABSL_ATTRIBUTE_WEAK; \
  319. extern char __stop_##name[] Y_ABSL_ATTRIBUTE_WEAK
  320. #endif
  321. #ifndef Y_ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
  322. #define Y_ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
  323. #define Y_ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
  324. #endif
  325. // Y_ABSL_ATTRIBUTE_SECTION_START
  326. //
  327. // Returns `void*` pointers to start/end of a section of code with
  328. // functions having Y_ABSL_ATTRIBUTE_SECTION(name).
  329. // Returns 0 if no such functions exist.
  330. // One must Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
  331. // link.
  332. //
  333. #define Y_ABSL_ATTRIBUTE_SECTION_START(name) \
  334. (reinterpret_cast<void *>(__start_##name))
  335. #define Y_ABSL_ATTRIBUTE_SECTION_STOP(name) \
  336. (reinterpret_cast<void *>(__stop_##name))
  337. #else // !Y_ABSL_HAVE_ATTRIBUTE_SECTION
  338. #define Y_ABSL_HAVE_ATTRIBUTE_SECTION 0
  339. // provide dummy definitions
  340. #define Y_ABSL_ATTRIBUTE_SECTION(name)
  341. #define Y_ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
  342. #define Y_ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
  343. #define Y_ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
  344. #define Y_ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
  345. #define Y_ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
  346. #define Y_ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
  347. #endif // Y_ABSL_ATTRIBUTE_SECTION
  348. // Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  349. //
  350. // Support for aligning the stack on 32-bit x86.
  351. #if Y_ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
  352. (defined(__GNUC__) && !defined(__clang__))
  353. #if defined(__i386__)
  354. #define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
  355. __attribute__((force_align_arg_pointer))
  356. #define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  357. #elif defined(__x86_64__)
  358. #define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
  359. #define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  360. #else // !__i386__ && !__x86_64
  361. #define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  362. #define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  363. #endif // __i386__
  364. #else
  365. #define Y_ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  366. #define Y_ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  367. #endif
  368. // Y_ABSL_MUST_USE_RESULT
  369. //
  370. // Tells the compiler to warn about unused results.
  371. //
  372. // When annotating a function, it must appear as the first part of the
  373. // declaration or definition. The compiler will warn if the return value from
  374. // such a function is unused:
  375. //
  376. // Y_ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
  377. // AllocateSprocket(); // Triggers a warning.
  378. //
  379. // When annotating a class, it is equivalent to annotating every function which
  380. // returns an instance.
  381. //
  382. // class Y_ABSL_MUST_USE_RESULT Sprocket {};
  383. // Sprocket(); // Triggers a warning.
  384. //
  385. // Sprocket MakeSprocket();
  386. // MakeSprocket(); // Triggers a warning.
  387. //
  388. // Note that references and pointers are not instances:
  389. //
  390. // Sprocket* SprocketPointer();
  391. // SprocketPointer(); // Does *not* trigger a warning.
  392. //
  393. // Y_ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
  394. // warning. For that, warn_unused_result is used only for clang but not for gcc.
  395. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
  396. //
  397. // Note: past advice was to place the macro after the argument list.
  398. #if Y_ABSL_HAVE_ATTRIBUTE(nodiscard)
  399. #define Y_ABSL_MUST_USE_RESULT [[nodiscard]]
  400. #elif defined(__clang__) && Y_ABSL_HAVE_ATTRIBUTE(warn_unused_result)
  401. #define Y_ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
  402. #else
  403. #define Y_ABSL_MUST_USE_RESULT
  404. #endif
  405. // Y_ABSL_ATTRIBUTE_HOT, Y_ABSL_ATTRIBUTE_COLD
  406. //
  407. // Tells GCC that a function is hot or cold. GCC can use this information to
  408. // improve static analysis, i.e. a conditional branch to a cold function
  409. // is likely to be not-taken.
  410. // This annotation is used for function declarations.
  411. //
  412. // Example:
  413. //
  414. // int foo() Y_ABSL_ATTRIBUTE_HOT;
  415. #if Y_ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
  416. #define Y_ABSL_ATTRIBUTE_HOT __attribute__((hot))
  417. #else
  418. #define Y_ABSL_ATTRIBUTE_HOT
  419. #endif
  420. #if Y_ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
  421. #define Y_ABSL_ATTRIBUTE_COLD __attribute__((cold))
  422. #else
  423. #define Y_ABSL_ATTRIBUTE_COLD
  424. #endif
  425. // Y_ABSL_XRAY_ALWAYS_INSTRUMENT, Y_ABSL_XRAY_NEVER_INSTRUMENT, Y_ABSL_XRAY_LOG_ARGS
  426. //
  427. // We define the Y_ABSL_XRAY_ALWAYS_INSTRUMENT and Y_ABSL_XRAY_NEVER_INSTRUMENT
  428. // macro used as an attribute to mark functions that must always or never be
  429. // instrumented by XRay. Currently, this is only supported in Clang/LLVM.
  430. //
  431. // For reference on the LLVM XRay instrumentation, see
  432. // http://llvm.org/docs/XRay.html.
  433. //
  434. // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
  435. // will always get the XRay instrumentation sleds. These sleds may introduce
  436. // some binary size and runtime overhead and must be used sparingly.
  437. //
  438. // These attributes only take effect when the following conditions are met:
  439. //
  440. // * The file/target is built in at least C++11 mode, with a Clang compiler
  441. // that supports XRay attributes.
  442. // * The file/target is built with the -fxray-instrument flag set for the
  443. // Clang/LLVM compiler.
  444. // * The function is defined in the translation unit (the compiler honors the
  445. // attribute in either the definition or the declaration, and must match).
  446. //
  447. // There are cases when, even when building with XRay instrumentation, users
  448. // might want to control specifically which functions are instrumented for a
  449. // particular build using special-case lists provided to the compiler. These
  450. // special case lists are provided to Clang via the
  451. // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
  452. // attributes in source take precedence over these special-case lists.
  453. //
  454. // To disable the XRay attributes at build-time, users may define
  455. // Y_ABSL_NO_XRAY_ATTRIBUTES. Do NOT define Y_ABSL_NO_XRAY_ATTRIBUTES on specific
  456. // packages/targets, as this may lead to conflicting definitions of functions at
  457. // link-time.
  458. //
  459. // XRay isn't currently supported on Android:
  460. // https://github.com/android/ndk/issues/368
  461. #if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
  462. !defined(Y_ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__)
  463. #define Y_ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
  464. #define Y_ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
  465. #if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
  466. #define Y_ABSL_XRAY_LOG_ARGS(N) \
  467. [[clang::xray_always_instrument, clang::xray_log_args(N)]]
  468. #else
  469. #define Y_ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
  470. #endif
  471. #else
  472. #define Y_ABSL_XRAY_ALWAYS_INSTRUMENT
  473. #define Y_ABSL_XRAY_NEVER_INSTRUMENT
  474. #define Y_ABSL_XRAY_LOG_ARGS(N)
  475. #endif
  476. // Y_ABSL_ATTRIBUTE_REINITIALIZES
  477. //
  478. // Indicates that a member function reinitializes the entire object to a known
  479. // state, independent of the previous state of the object.
  480. //
  481. // The clang-tidy check bugprone-use-after-move allows member functions marked
  482. // with this attribute to be called on objects that have been moved from;
  483. // without the attribute, this would result in a use-after-move warning.
  484. #if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
  485. #define Y_ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
  486. #else
  487. #define Y_ABSL_ATTRIBUTE_REINITIALIZES
  488. #endif
  489. // -----------------------------------------------------------------------------
  490. // Variable Attributes
  491. // -----------------------------------------------------------------------------
  492. // Y_ABSL_ATTRIBUTE_UNUSED
  493. //
  494. // Prevents the compiler from complaining about variables that appear unused.
  495. //
  496. // For code or headers that are assured to only build with C++17 and up, prefer
  497. // just using the standard '[[maybe_unused]]' directly over this macro.
  498. //
  499. // Due to differences in positioning requirements between the old, compiler
  500. // specific __attribute__ syntax and the now standard [[maybe_unused]], this
  501. // macro does not attempt to take advantage of '[[maybe_unused]]'.
  502. #if Y_ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
  503. #undef Y_ABSL_ATTRIBUTE_UNUSED
  504. #define Y_ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
  505. #else
  506. #define Y_ABSL_ATTRIBUTE_UNUSED
  507. #endif
  508. // Y_ABSL_ATTRIBUTE_INITIAL_EXEC
  509. //
  510. // Tells the compiler to use "initial-exec" mode for a thread-local variable.
  511. // See http://people.redhat.com/drepper/tls.pdf for the gory details.
  512. #if Y_ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
  513. #define Y_ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
  514. #else
  515. #define Y_ABSL_ATTRIBUTE_INITIAL_EXEC
  516. #endif
  517. // Y_ABSL_ATTRIBUTE_PACKED
  518. //
  519. // Instructs the compiler not to use natural alignment for a tagged data
  520. // structure, but instead to reduce its alignment to 1.
  521. //
  522. // Therefore, DO NOT APPLY THIS ATTRIBUTE TO STRUCTS CONTAINING ATOMICS. Doing
  523. // so can cause atomic variables to be mis-aligned and silently violate
  524. // atomicity on x86.
  525. //
  526. // This attribute can either be applied to members of a structure or to a
  527. // structure in its entirety. Applying this attribute (judiciously) to a
  528. // structure in its entirety to optimize the memory footprint of very
  529. // commonly-used structs is fine. Do not apply this attribute to a structure in
  530. // its entirety if the purpose is to control the offsets of the members in the
  531. // structure. Instead, apply this attribute only to structure members that need
  532. // it.
  533. //
  534. // When applying Y_ABSL_ATTRIBUTE_PACKED only to specific structure members the
  535. // natural alignment of structure members not annotated is preserved. Aligned
  536. // member accesses are faster than non-aligned member accesses even if the
  537. // targeted microprocessor supports non-aligned accesses.
  538. #if Y_ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
  539. #define Y_ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
  540. #else
  541. #define Y_ABSL_ATTRIBUTE_PACKED
  542. #endif
  543. // Y_ABSL_ATTRIBUTE_FUNC_ALIGN
  544. //
  545. // Tells the compiler to align the function start at least to certain
  546. // alignment boundary
  547. #if Y_ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
  548. #define Y_ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
  549. #else
  550. #define Y_ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
  551. #endif
  552. // Y_ABSL_FALLTHROUGH_INTENDED
  553. //
  554. // Annotates implicit fall-through between switch labels, allowing a case to
  555. // indicate intentional fallthrough and turn off warnings about any lack of a
  556. // `break` statement. The Y_ABSL_FALLTHROUGH_INTENDED macro should be followed by
  557. // a semicolon and can be used in most places where `break` can, provided that
  558. // no statements exist between it and the next switch label.
  559. //
  560. // Example:
  561. //
  562. // switch (x) {
  563. // case 40:
  564. // case 41:
  565. // if (truth_is_out_there) {
  566. // ++x;
  567. // Y_ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
  568. // // in comments
  569. // } else {
  570. // return x;
  571. // }
  572. // case 42:
  573. // ...
  574. //
  575. // Notes: When supported, GCC and Clang can issue a warning on switch labels
  576. // with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See
  577. // clang documentation on language extensions for details:
  578. // https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
  579. //
  580. // When used with unsupported compilers, the Y_ABSL_FALLTHROUGH_INTENDED macro has
  581. // no effect on diagnostics. In any case this macro has no effect on runtime
  582. // behavior and performance of code.
  583. #ifdef Y_ABSL_FALLTHROUGH_INTENDED
  584. #error "Y_ABSL_FALLTHROUGH_INTENDED should not be defined."
  585. #elif Y_ABSL_HAVE_CPP_ATTRIBUTE(fallthrough)
  586. #define Y_ABSL_FALLTHROUGH_INTENDED [[fallthrough]]
  587. #elif Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough)
  588. #define Y_ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
  589. #elif Y_ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough)
  590. #define Y_ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
  591. #else
  592. #define Y_ABSL_FALLTHROUGH_INTENDED \
  593. do { \
  594. } while (0)
  595. #endif
  596. // Y_ABSL_DEPRECATED()
  597. //
  598. // Marks a deprecated class, struct, enum, function, method and variable
  599. // declarations. The macro argument is used as a custom diagnostic message (e.g.
  600. // suggestion of a better alternative).
  601. //
  602. // Examples:
  603. //
  604. // class Y_ABSL_DEPRECATED("Use Bar instead") Foo {...};
  605. //
  606. // Y_ABSL_DEPRECATED("Use Baz() instead") void Bar() {...}
  607. //
  608. // template <typename T>
  609. // Y_ABSL_DEPRECATED("Use DoThat() instead")
  610. // void DoThis();
  611. //
  612. // Every usage of a deprecated entity will trigger a warning when compiled with
  613. // clang's `-Wdeprecated-declarations` option. This option is turned off by
  614. // default, but the warnings will be reported by clang-tidy.
  615. #if defined(__clang__) && defined(__cplusplus) && __cplusplus >= 201103L
  616. #define Y_ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
  617. #endif
  618. #ifndef Y_ABSL_DEPRECATED
  619. #define Y_ABSL_DEPRECATED(message)
  620. #endif
  621. // Y_ABSL_CONST_INIT
  622. //
  623. // A variable declaration annotated with the `Y_ABSL_CONST_INIT` attribute will
  624. // not compile (on supported platforms) unless the variable has a constant
  625. // initializer. This is useful for variables with static and thread storage
  626. // duration, because it guarantees that they will not suffer from the so-called
  627. // "static init order fiasco". Prefer to put this attribute on the most visible
  628. // declaration of the variable, if there's more than one, because code that
  629. // accesses the variable can then use the attribute for optimization.
  630. //
  631. // Example:
  632. //
  633. // class MyClass {
  634. // public:
  635. // Y_ABSL_CONST_INIT static MyType my_var;
  636. // };
  637. //
  638. // MyType MyClass::my_var = MakeMyType(...);
  639. //
  640. // Note that this attribute is redundant if the variable is declared constexpr.
  641. #if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
  642. #define Y_ABSL_CONST_INIT [[clang::require_constant_initialization]]
  643. #else
  644. #define Y_ABSL_CONST_INIT
  645. #endif // Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
  646. // Y_ABSL_ATTRIBUTE_PURE_FUNCTION
  647. //
  648. // Y_ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure"
  649. // functions. A function is pure if its return value is only a function of its
  650. // arguments. The pure attribute prohibits a function from modifying the state
  651. // of the program that is observable by means other than inspecting the
  652. // function's return value. Declaring such functions with the pure attribute
  653. // allows the compiler to avoid emitting some calls in repeated invocations of
  654. // the function with the same argument values.
  655. //
  656. // Example:
  657. //
  658. // Y_ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Milliseconds(Duration d);
  659. #if Y_ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure)
  660. #define Y_ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]]
  661. #elif Y_ABSL_HAVE_ATTRIBUTE(pure)
  662. #define Y_ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure))
  663. #else
  664. #define Y_ABSL_ATTRIBUTE_PURE_FUNCTION
  665. #endif
  666. // Y_ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
  667. // parameter or implicit object parameter is retained by the return value of the
  668. // annotated function (or, for a parameter of a constructor, in the value of the
  669. // constructed object). This attribute causes warnings to be produced if a
  670. // temporary object does not live long enough.
  671. //
  672. // When applied to a reference parameter, the referenced object is assumed to be
  673. // retained by the return value of the function. When applied to a non-reference
  674. // parameter (for example, a pointer or a class type), all temporaries
  675. // referenced by the parameter are assumed to be retained by the return value of
  676. // the function.
  677. //
  678. // See also the upstream documentation:
  679. // https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
  680. #if Y_ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
  681. #define Y_ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
  682. #elif Y_ABSL_HAVE_ATTRIBUTE(lifetimebound)
  683. #define Y_ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
  684. #else
  685. #define Y_ABSL_ATTRIBUTE_LIFETIME_BOUND
  686. #endif
  687. #endif // Y_ABSL_BASE_ATTRIBUTES_H_