You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

attributes.h 30 kB

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