attributes.h 26 KB

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