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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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) || (defined(__GNUC__) && !defined(__clang__))) && \
  130. (!defined(_WIN32) || (defined(__clang__) && __clang_major__ >= 9)) && \
  131. !defined(__MINGW32__)
  132. #undef ABSL_ATTRIBUTE_WEAK
  133. #define ABSL_ATTRIBUTE_WEAK __attribute__((weak))
  134. #define ABSL_HAVE_ATTRIBUTE_WEAK 1
  135. #else
  136. #define ABSL_ATTRIBUTE_WEAK
  137. #define ABSL_HAVE_ATTRIBUTE_WEAK 0
  138. #endif
  139. // 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) 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) 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. // 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) ABSL_ATTRIBUTE_NONNULL();
  174. //
  175. // NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
  176. // ABSL_ATTRIBUTE_NONNULL does not.
  177. #if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
  178. #define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
  179. #else
  180. #define ABSL_ATTRIBUTE_NONNULL(...)
  181. #endif
  182. // ABSL_ATTRIBUTE_NORETURN
  183. //
  184. // Tells the compiler that a given function never returns.
  185. #if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
  186. #define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
  187. #elif defined(_MSC_VER)
  188. #define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
  189. #else
  190. #define ABSL_ATTRIBUTE_NORETURN
  191. #endif
  192. // 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 ABSL_HAVE_ATTRIBUTE(no_sanitize_address)
  201. #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
  202. #elif defined(_MSC_VER) && _MSC_VER >= 1928
  203. // https://docs.microsoft.com/en-us/cpp/cpp/no-sanitize-address
  204. #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __declspec(no_sanitize_address)
  205. #else
  206. #define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
  207. #endif
  208. // ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
  209. //
  210. // Tells the MemorySanitizer to relax the handling of a given function. All "Use
  211. // of uninitialized value" warnings from such functions will be suppressed, and
  212. // all values loaded from memory will be considered fully initialized. This
  213. // attribute is similar to the ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute
  214. // above, but deals with initialized-ness rather than addressability issues.
  215. // NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
  216. #if ABSL_HAVE_ATTRIBUTE(no_sanitize_memory)
  217. #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
  218. #else
  219. #define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
  220. #endif
  221. // ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
  222. //
  223. // Tells the ThreadSanitizer to not instrument a given function.
  224. // NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
  225. // https://gcc.gnu.org/gcc-4.8/changes.html
  226. #if ABSL_HAVE_ATTRIBUTE(no_sanitize_thread)
  227. #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
  228. #else
  229. #define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
  230. #endif
  231. // ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
  232. //
  233. // Tells the UndefinedSanitizer to ignore a given function. Useful for cases
  234. // where certain behavior (eg. division by zero) is being used intentionally.
  235. // NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
  236. // https://gcc.gnu.org/gcc-4.9/changes.html
  237. #if ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined)
  238. #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
  239. __attribute__((no_sanitize_undefined))
  240. #elif ABSL_HAVE_ATTRIBUTE(no_sanitize)
  241. #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
  242. __attribute__((no_sanitize("undefined")))
  243. #else
  244. #define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
  245. #endif
  246. // ABSL_ATTRIBUTE_NO_SANITIZE_CFI
  247. //
  248. // Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
  249. // See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
  250. #if ABSL_HAVE_ATTRIBUTE(no_sanitize)
  251. #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
  252. #else
  253. #define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
  254. #endif
  255. // ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
  256. //
  257. // Tells the SafeStack to not instrument a given function.
  258. // See https://clang.llvm.org/docs/SafeStack.html for details.
  259. #if ABSL_HAVE_ATTRIBUTE(no_sanitize)
  260. #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \
  261. __attribute__((no_sanitize("safe-stack")))
  262. #else
  263. #define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
  264. #endif
  265. // ABSL_ATTRIBUTE_RETURNS_NONNULL
  266. //
  267. // Tells the compiler that a particular function never returns a null pointer.
  268. #if ABSL_HAVE_ATTRIBUTE(returns_nonnull)
  269. #define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
  270. #else
  271. #define ABSL_ATTRIBUTE_RETURNS_NONNULL
  272. #endif
  273. // ABSL_HAVE_ATTRIBUTE_SECTION
  274. //
  275. // Indicates whether labeled sections are supported. Weak symbol support is
  276. // a prerequisite. Labeled sections are not supported on Darwin/iOS.
  277. #ifdef ABSL_HAVE_ATTRIBUTE_SECTION
  278. #error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
  279. #elif (ABSL_HAVE_ATTRIBUTE(section) || (defined(__GNUC__) && !defined(__clang__))) && \
  280. !defined(__APPLE__) && ABSL_HAVE_ATTRIBUTE_WEAK
  281. #define ABSL_HAVE_ATTRIBUTE_SECTION 1
  282. // ABSL_ATTRIBUTE_SECTION
  283. //
  284. // Tells the compiler/linker to put a given function into a section and define
  285. // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
  286. // This functionality is supported by GNU linker. Any function annotated with
  287. // `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
  288. // whatever section its caller is placed into.
  289. //
  290. #ifndef ABSL_ATTRIBUTE_SECTION
  291. #define ABSL_ATTRIBUTE_SECTION(name) \
  292. __attribute__((section(#name))) __attribute__((noinline))
  293. #endif
  294. // ABSL_ATTRIBUTE_SECTION_VARIABLE
  295. //
  296. // Tells the compiler/linker to put a given variable into a section and define
  297. // `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
  298. // This functionality is supported by GNU linker.
  299. #ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
  300. #ifdef _AIX
  301. // __attribute__((section(#name))) on AIX is achived by using the `.csect` psudo
  302. // op which includes an additional integer as part of its syntax indcating
  303. // alignment. If data fall under different alignments then you might get a
  304. // compilation error indicating a `Section type conflict`.
  305. #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
  306. #else
  307. #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
  308. #endif
  309. #endif
  310. // ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
  311. //
  312. // A weak section declaration to be used as a global declaration
  313. // for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
  314. // even without functions with ABSL_ATTRIBUTE_SECTION(name).
  315. // ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
  316. // a no-op on ELF but not on Mach-O.
  317. //
  318. #ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
  319. #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
  320. extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
  321. extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
  322. #endif
  323. #ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
  324. #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
  325. #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
  326. #endif
  327. // ABSL_ATTRIBUTE_SECTION_START
  328. //
  329. // Returns `void*` pointers to start/end of a section of code with
  330. // functions having ABSL_ATTRIBUTE_SECTION(name).
  331. // Returns 0 if no such functions exist.
  332. // One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
  333. // link.
  334. //
  335. #define ABSL_ATTRIBUTE_SECTION_START(name) \
  336. (reinterpret_cast<void*>(__start_##name))
  337. #define ABSL_ATTRIBUTE_SECTION_STOP(name) \
  338. (reinterpret_cast<void*>(__stop_##name))
  339. #else // !ABSL_HAVE_ATTRIBUTE_SECTION
  340. #define ABSL_HAVE_ATTRIBUTE_SECTION 0
  341. // provide dummy definitions
  342. #define ABSL_ATTRIBUTE_SECTION(name)
  343. #define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
  344. #define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
  345. #define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
  346. #define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
  347. #define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void*>(0))
  348. #define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void*>(0))
  349. #endif // ABSL_ATTRIBUTE_SECTION
  350. // ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  351. //
  352. // Support for aligning the stack on 32-bit x86.
  353. #if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
  354. (defined(__GNUC__) && !defined(__clang__))
  355. #if defined(__i386__)
  356. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
  357. __attribute__((force_align_arg_pointer))
  358. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  359. #elif defined(__x86_64__)
  360. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
  361. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  362. #else // !__i386__ && !__x86_64
  363. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  364. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  365. #endif // __i386__
  366. #else
  367. #define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
  368. #define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
  369. #endif
  370. // ABSL_MUST_USE_RESULT
  371. //
  372. // Tells the compiler to warn about unused results.
  373. //
  374. // For code or headers that are assured to only build with C++17 and up, prefer
  375. // just using the standard `[[nodiscard]]` directly over this macro.
  376. //
  377. // When annotating a function, it must appear as the first part of the
  378. // declaration or definition. The compiler will warn if the return value from
  379. // such a function is unused:
  380. //
  381. // ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
  382. // AllocateSprocket(); // Triggers a warning.
  383. //
  384. // When annotating a class, it is equivalent to annotating every function which
  385. // returns an instance.
  386. //
  387. // class ABSL_MUST_USE_RESULT Sprocket {};
  388. // Sprocket(); // Triggers a warning.
  389. //
  390. // Sprocket MakeSprocket();
  391. // MakeSprocket(); // Triggers a warning.
  392. //
  393. // Note that references and pointers are not instances:
  394. //
  395. // Sprocket* SprocketPointer();
  396. // SprocketPointer(); // Does *not* trigger a warning.
  397. //
  398. // ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
  399. // warning. For that, warn_unused_result is used only for clang but not for gcc.
  400. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
  401. //
  402. // Note: past advice was to place the macro after the argument list.
  403. //
  404. // TODO(b/176172494): Use ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) when all code is
  405. // compliant with the stricter [[nodiscard]].
  406. #if defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
  407. #define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
  408. #else
  409. #define ABSL_MUST_USE_RESULT
  410. #endif
  411. // ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
  412. //
  413. // Tells GCC that a function is hot or cold. GCC can use this information to
  414. // improve static analysis, i.e. a conditional branch to a cold function
  415. // is likely to be not-taken.
  416. // This annotation is used for function declarations.
  417. //
  418. // Example:
  419. //
  420. // int foo() ABSL_ATTRIBUTE_HOT;
  421. #if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
  422. #define ABSL_ATTRIBUTE_HOT __attribute__((hot))
  423. #else
  424. #define ABSL_ATTRIBUTE_HOT
  425. #endif
  426. #if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
  427. #define ABSL_ATTRIBUTE_COLD __attribute__((cold))
  428. #else
  429. #define ABSL_ATTRIBUTE_COLD
  430. #endif
  431. // ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
  432. //
  433. // We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
  434. // macro used as an attribute to mark functions that must always or never be
  435. // instrumented by XRay. Currently, this is only supported in Clang/LLVM.
  436. //
  437. // For reference on the LLVM XRay instrumentation, see
  438. // http://llvm.org/docs/XRay.html.
  439. //
  440. // A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
  441. // will always get the XRay instrumentation sleds. These sleds may introduce
  442. // some binary size and runtime overhead and must be used sparingly.
  443. //
  444. // These attributes only take effect when the following conditions are met:
  445. //
  446. // * The file/target is built in at least C++11 mode, with a Clang compiler
  447. // that supports XRay attributes.
  448. // * The file/target is built with the -fxray-instrument flag set for the
  449. // Clang/LLVM compiler.
  450. // * The function is defined in the translation unit (the compiler honors the
  451. // attribute in either the definition or the declaration, and must match).
  452. //
  453. // There are cases when, even when building with XRay instrumentation, users
  454. // might want to control specifically which functions are instrumented for a
  455. // particular build using special-case lists provided to the compiler. These
  456. // special case lists are provided to Clang via the
  457. // -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
  458. // attributes in source take precedence over these special-case lists.
  459. //
  460. // To disable the XRay attributes at build-time, users may define
  461. // ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
  462. // packages/targets, as this may lead to conflicting definitions of functions at
  463. // link-time.
  464. //
  465. // XRay isn't currently supported on Android:
  466. // https://github.com/android/ndk/issues/368
  467. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
  468. !defined(ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__)
  469. #define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
  470. #define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
  471. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
  472. #define ABSL_XRAY_LOG_ARGS(N) \
  473. [[clang::xray_always_instrument, clang::xray_log_args(N)]]
  474. #else
  475. #define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
  476. #endif
  477. #else
  478. #define ABSL_XRAY_ALWAYS_INSTRUMENT
  479. #define ABSL_XRAY_NEVER_INSTRUMENT
  480. #define ABSL_XRAY_LOG_ARGS(N)
  481. #endif
  482. // ABSL_ATTRIBUTE_REINITIALIZES
  483. //
  484. // Indicates that a member function reinitializes the entire object to a known
  485. // state, independent of the previous state of the object.
  486. //
  487. // The clang-tidy check bugprone-use-after-move allows member functions marked
  488. // with this attribute to be called on objects that have been moved from;
  489. // without the attribute, this would result in a use-after-move warning.
  490. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
  491. #define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
  492. #else
  493. #define ABSL_ATTRIBUTE_REINITIALIZES
  494. #endif
  495. // -----------------------------------------------------------------------------
  496. // Variable Attributes
  497. // -----------------------------------------------------------------------------
  498. // ABSL_ATTRIBUTE_UNUSED
  499. //
  500. // Prevents the compiler from complaining about variables that appear unused.
  501. //
  502. // For code or headers that are assured to only build with C++17 and up, prefer
  503. // just using the standard '[[maybe_unused]]' directly over this macro.
  504. //
  505. // Due to differences in positioning requirements between the old, compiler
  506. // specific __attribute__ syntax and the now standard [[maybe_unused]], this
  507. // macro does not attempt to take advantage of '[[maybe_unused]]'.
  508. #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
  509. #undef ABSL_ATTRIBUTE_UNUSED
  510. #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
  511. #else
  512. #define ABSL_ATTRIBUTE_UNUSED
  513. #endif
  514. // ABSL_ATTRIBUTE_INITIAL_EXEC
  515. //
  516. // Tells the compiler to use "initial-exec" mode for a thread-local variable.
  517. // See http://people.redhat.com/drepper/tls.pdf for the gory details.
  518. #if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
  519. #define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
  520. #else
  521. #define ABSL_ATTRIBUTE_INITIAL_EXEC
  522. #endif
  523. // ABSL_ATTRIBUTE_PACKED
  524. //
  525. // Instructs the compiler not to use natural alignment for a tagged data
  526. // structure, but instead to reduce its alignment to 1.
  527. //
  528. // Therefore, DO NOT APPLY THIS ATTRIBUTE TO STRUCTS CONTAINING ATOMICS. Doing
  529. // so can cause atomic variables to be mis-aligned and silently violate
  530. // atomicity on x86.
  531. //
  532. // This attribute can either be applied to members of a structure or to a
  533. // structure in its entirety. Applying this attribute (judiciously) to a
  534. // structure in its entirety to optimize the memory footprint of very
  535. // commonly-used structs is fine. Do not apply this attribute to a structure in
  536. // its entirety if the purpose is to control the offsets of the members in the
  537. // structure. Instead, apply this attribute only to structure members that need
  538. // it.
  539. //
  540. // When applying ABSL_ATTRIBUTE_PACKED only to specific structure members the
  541. // natural alignment of structure members not annotated is preserved. Aligned
  542. // member accesses are faster than non-aligned member accesses even if the
  543. // targeted microprocessor supports non-aligned accesses.
  544. #if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
  545. #define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
  546. #else
  547. #define ABSL_ATTRIBUTE_PACKED
  548. #endif
  549. // ABSL_ATTRIBUTE_FUNC_ALIGN
  550. //
  551. // Tells the compiler to align the function start at least to certain
  552. // alignment boundary
  553. #if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
  554. #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
  555. #else
  556. #define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
  557. #endif
  558. // ABSL_FALLTHROUGH_INTENDED
  559. //
  560. // Annotates implicit fall-through between switch labels, allowing a case to
  561. // indicate intentional fallthrough and turn off warnings about any lack of a
  562. // `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
  563. // a semicolon and can be used in most places where `break` can, provided that
  564. // no statements exist between it and the next switch label.
  565. //
  566. // Example:
  567. //
  568. // switch (x) {
  569. // case 40:
  570. // case 41:
  571. // if (truth_is_out_there) {
  572. // ++x;
  573. // ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
  574. // // in comments
  575. // } else {
  576. // return x;
  577. // }
  578. // case 42:
  579. // ...
  580. //
  581. // Notes: When supported, GCC and Clang can issue a warning on switch labels
  582. // with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See
  583. // clang documentation on language extensions for details:
  584. // https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
  585. //
  586. // When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro has
  587. // no effect on diagnostics. In any case this macro has no effect on runtime
  588. // behavior and performance of code.
  589. #ifdef ABSL_FALLTHROUGH_INTENDED
  590. #error "ABSL_FALLTHROUGH_INTENDED should not be defined."
  591. #elif ABSL_HAVE_CPP_ATTRIBUTE(fallthrough)
  592. #define ABSL_FALLTHROUGH_INTENDED [[fallthrough]]
  593. #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough)
  594. #define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
  595. #elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough)
  596. #define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
  597. #else
  598. #define ABSL_FALLTHROUGH_INTENDED \
  599. do \
  600. { \
  601. } while (0)
  602. #endif
  603. // ABSL_DEPRECATED()
  604. //
  605. // Marks a deprecated class, struct, enum, function, method and variable
  606. // declarations. The macro argument is used as a custom diagnostic message (e.g.
  607. // suggestion of a better alternative).
  608. //
  609. // For code or headers that are assured to only build with C++14 and up, prefer
  610. // just using the standard `[[deprecated("message")]]` directly over this macro.
  611. //
  612. // Examples:
  613. //
  614. // class ABSL_DEPRECATED("Use Bar instead") Foo {...};
  615. //
  616. // ABSL_DEPRECATED("Use Baz() instead") void Bar() {...}
  617. //
  618. // template <typename T>
  619. // ABSL_DEPRECATED("Use DoThat() instead")
  620. // void DoThis();
  621. //
  622. // enum FooEnum {
  623. // kBar ABSL_DEPRECATED("Use kBaz instead"),
  624. // };
  625. //
  626. // Every usage of a deprecated entity will trigger a warning when compiled with
  627. // GCC/Clang's `-Wdeprecated-declarations` option. Google's production toolchain
  628. // turns this warning off by default, instead relying on clang-tidy to report
  629. // new uses of deprecated code.
  630. #if ABSL_HAVE_ATTRIBUTE(deprecated)
  631. #define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
  632. #else
  633. #define ABSL_DEPRECATED(message)
  634. #endif
  635. // ABSL_CONST_INIT
  636. //
  637. // A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
  638. // not compile (on supported platforms) unless the variable has a constant
  639. // initializer. This is useful for variables with static and thread storage
  640. // duration, because it guarantees that they will not suffer from the so-called
  641. // "static init order fiasco".
  642. //
  643. // This attribute must be placed on the initializing declaration of the
  644. // variable. Some compilers will give a -Wmissing-constinit warning when this
  645. // attribute is placed on some other declaration but missing from the
  646. // initializing declaration.
  647. //
  648. // In some cases (notably with thread_local variables), `ABSL_CONST_INIT` can
  649. // also be used in a non-initializing declaration to tell the compiler that a
  650. // variable is already initialized, reducing overhead that would otherwise be
  651. // incurred by a hidden guard variable. Thus annotating all declarations with
  652. // this attribute is recommended to potentially enhance optimization.
  653. //
  654. // Example:
  655. //
  656. // class MyClass {
  657. // public:
  658. // ABSL_CONST_INIT static MyType my_var;
  659. // };
  660. //
  661. // ABSL_CONST_INIT MyType MyClass::my_var = MakeMyType(...);
  662. //
  663. // For code or headers that are assured to only build with C++20 and up, prefer
  664. // just using the standard `constinit` keyword directly over this macro.
  665. //
  666. // Note that this attribute is redundant if the variable is declared constexpr.
  667. #if defined(__cpp_constinit) && __cpp_constinit >= 201907L
  668. #define ABSL_CONST_INIT constinit
  669. #elif ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
  670. #define ABSL_CONST_INIT [[clang::require_constant_initialization]]
  671. #else
  672. #define ABSL_CONST_INIT
  673. #endif
  674. // ABSL_ATTRIBUTE_PURE_FUNCTION
  675. //
  676. // ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure"
  677. // functions. A function is pure if its return value is only a function of its
  678. // arguments. The pure attribute prohibits a function from modifying the state
  679. // of the program that is observable by means other than inspecting the
  680. // function's return value. Declaring such functions with the pure attribute
  681. // allows the compiler to avoid emitting some calls in repeated invocations of
  682. // the function with the same argument values.
  683. //
  684. // Example:
  685. //
  686. // ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Milliseconds(Duration d);
  687. #if ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure)
  688. #define ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]]
  689. #elif ABSL_HAVE_ATTRIBUTE(pure)
  690. #define ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure))
  691. #else
  692. #define ABSL_ATTRIBUTE_PURE_FUNCTION
  693. #endif
  694. // ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
  695. // parameter or implicit object parameter is retained by the return value of the
  696. // annotated function (or, for a parameter of a constructor, in the value of the
  697. // constructed object). This attribute causes warnings to be produced if a
  698. // temporary object does not live long enough.
  699. //
  700. // When applied to a reference parameter, the referenced object is assumed to be
  701. // retained by the return value of the function. When applied to a non-reference
  702. // parameter (for example, a pointer or a class type), all temporaries
  703. // referenced by the parameter are assumed to be retained by the return value of
  704. // the function.
  705. //
  706. // See also the upstream documentation:
  707. // https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
  708. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
  709. #define ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
  710. #elif ABSL_HAVE_ATTRIBUTE(lifetimebound)
  711. #define ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
  712. #else
  713. #define ABSL_ATTRIBUTE_LIFETIME_BOUND
  714. #endif
  715. #endif // ABSL_BASE_ATTRIBUTES_H_