attribute.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* ATTRIBUTE_* macros for using attributes in GCC and similar compilers
  2. Copyright 2020-2024 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. /* Provide public ATTRIBUTE_* names for the private _GL_ATTRIBUTE_*
  15. macros used within Gnulib. */
  16. /* These attributes can be placed in two ways:
  17. - At the start of a declaration (i.e. even before storage-class
  18. specifiers!); then they apply to all entities that are declared
  19. by the declaration.
  20. - Immediately after the name of an entity being declared by the
  21. declaration; then they apply to that entity only. */
  22. #ifndef _GL_ATTRIBUTE_H
  23. #define _GL_ATTRIBUTE_H
  24. /* This file defines two types of attributes:
  25. * C23 standard attributes. These have macro names that do not begin with
  26. 'ATTRIBUTE_'.
  27. * Selected GCC attributes; see:
  28. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
  29. https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
  30. https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html
  31. These names begin with 'ATTRIBUTE_' to avoid name clashes. */
  32. /* This file uses _GL_ATTRIBUTE_ALLOC_SIZE, _GL_ATTRIBUTE_ALWAYS_INLINE,
  33. _GL_ATTRIBUTE_ARTIFICIAL, _GL_ATTRIBUTE_COLD, _GL_ATTRIBUTE_CONST,
  34. _GL_ATTRIBUTE_DEALLOC, _GL_ATTRIBUTE_DEPRECATED, _GL_ATTRIBUTE_ERROR,
  35. _GL_ATTRIBUTE_WARNING, _GL_ATTRIBUTE_EXTERNALLY_VISIBLE,
  36. _GL_ATTRIBUTE_FALLTHROUGH, _GL_ATTRIBUTE_FORMAT, _GL_ATTRIBUTE_LEAF,
  37. _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_MAY_ALIAS, _GL_ATTRIBUTE_MAYBE_UNUSED,
  38. _GL_ATTRIBUTE_NODISCARD, _GL_ATTRIBUTE_NOINLINE, _GL_ATTRIBUTE_NONNULL,
  39. _GL_ATTRIBUTE_NONSTRING, _GL_ATTRIBUTE_NOTHROW, _GL_ATTRIBUTE_PACKED,
  40. _GL_ATTRIBUTE_PURE, _GL_ATTRIBUTE_RETURNS_NONNULL,
  41. _GL_ATTRIBUTE_SENTINEL. */
  42. #if !_GL_CONFIG_H_INCLUDED
  43. #error "Please include config.h first."
  44. #endif
  45. /* =============== Attributes for specific kinds of functions =============== */
  46. /* Attributes for functions that should not be used. */
  47. /* Warn if the entity is used. */
  48. /* Applies to:
  49. - function, variable,
  50. - struct, union, struct/union member,
  51. - enumeration, enumeration item,
  52. - typedef,
  53. in C++ also: namespace, class, template specialization. */
  54. #define DEPRECATED _GL_ATTRIBUTE_DEPRECATED
  55. /* If a function call is not optimized way, warn with MSG. */
  56. /* Applies to: functions. */
  57. #define ATTRIBUTE_WARNING(msg) _GL_ATTRIBUTE_WARNING (msg)
  58. /* If a function call is not optimized way, report an error with MSG. */
  59. /* Applies to: functions. */
  60. #define ATTRIBUTE_ERROR(msg) _GL_ATTRIBUTE_ERROR (msg)
  61. /* Attributes for memory-allocating functions. */
  62. /* The function returns a pointer to freshly allocated memory. */
  63. /* Applies to: functions. */
  64. #define ATTRIBUTE_MALLOC _GL_ATTRIBUTE_MALLOC
  65. /* ATTRIBUTE_ALLOC_SIZE ((N)) - The Nth argument of the function
  66. is the size of the returned memory block.
  67. ATTRIBUTE_ALLOC_SIZE ((M, N)) - Multiply the Mth and Nth arguments
  68. to determine the size of the returned memory block. */
  69. /* Applies to: function, pointer to function, function types. */
  70. #define ATTRIBUTE_ALLOC_SIZE(args) _GL_ATTRIBUTE_ALLOC_SIZE (args)
  71. /* ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers
  72. that can be freed by passing them as the Ith argument to the
  73. function F.
  74. ATTRIBUTE_DEALLOC_FREE declares that the function returns pointers that
  75. can be freed via 'free'; it can be used only after declaring 'free'. */
  76. /* Applies to: functions. Cannot be used on inline functions. */
  77. #define ATTRIBUTE_DEALLOC(f, i) _GL_ATTRIBUTE_DEALLOC(f, i)
  78. #define ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_DEALLOC_FREE
  79. /* Attributes for variadic functions. */
  80. /* The variadic function expects a trailing NULL argument.
  81. ATTRIBUTE_SENTINEL () - The last argument is NULL (requires C99).
  82. ATTRIBUTE_SENTINEL ((N)) - The (N+1)st argument from the end is NULL. */
  83. /* Applies to: functions. */
  84. #define ATTRIBUTE_SENTINEL(pos) _GL_ATTRIBUTE_SENTINEL (pos)
  85. /* ================== Attributes for compiler diagnostics ================== */
  86. /* Attributes that help the compiler diagnose programmer mistakes.
  87. Some of them may also help for some compiler optimizations. */
  88. /* ATTRIBUTE_FORMAT ((ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)) -
  89. The STRING-INDEXth function argument is a format string of style
  90. ARCHETYPE, which is one of:
  91. printf, gnu_printf
  92. scanf, gnu_scanf,
  93. strftime, gnu_strftime,
  94. strfmon,
  95. or the same thing prefixed and suffixed with '__'.
  96. If FIRST-TO-CHECK is not 0, arguments starting at FIRST-TO_CHECK
  97. are suitable for the format string. */
  98. /* Applies to: functions. */
  99. #define ATTRIBUTE_FORMAT(spec) _GL_ATTRIBUTE_FORMAT (spec)
  100. /* ATTRIBUTE_NONNULL ((N1, N2,...)) - Arguments N1, N2,... must not be NULL.
  101. ATTRIBUTE_NONNULL () - All pointer arguments must not be null. */
  102. /* Applies to: functions. */
  103. #define ATTRIBUTE_NONNULL(args) _GL_ATTRIBUTE_NONNULL (args)
  104. /* The function's return value is a non-NULL pointer. */
  105. /* Applies to: functions. */
  106. #define ATTRIBUTE_RETURNS_NONNULL _GL_ATTRIBUTE_RETURNS_NONNULL
  107. /* Warn if the caller does not use the return value,
  108. unless the caller uses something like ignore_value. */
  109. /* Applies to: function, enumeration, class. */
  110. #define NODISCARD _GL_ATTRIBUTE_NODISCARD
  111. /* Attributes that disable false alarms when the compiler diagnoses
  112. programmer "mistakes". */
  113. /* Do not warn if the entity is not used. */
  114. /* Applies to:
  115. - function, variable,
  116. - struct, union, struct/union member,
  117. - enumeration, enumeration item,
  118. - typedef,
  119. in C++ also: class. */
  120. #define MAYBE_UNUSED _GL_ATTRIBUTE_MAYBE_UNUSED
  121. /* The contents of a character array is not meant to be NUL-terminated. */
  122. /* Applies to: struct/union members and variables that are arrays of element
  123. type '[[un]signed] char'. */
  124. #define ATTRIBUTE_NONSTRING _GL_ATTRIBUTE_NONSTRING
  125. /* Do not warn if control flow falls through to the immediately
  126. following 'case' or 'default' label. */
  127. /* Applies to: Empty statement (;), inside a 'switch' statement. */
  128. #define FALLTHROUGH _GL_ATTRIBUTE_FALLTHROUGH
  129. /* ================== Attributes for debugging information ================== */
  130. /* Attributes regarding debugging information emitted by the compiler. */
  131. /* Omit the function from stack traces when debugging. */
  132. /* Applies to: function. */
  133. #define ATTRIBUTE_ARTIFICIAL _GL_ATTRIBUTE_ARTIFICIAL
  134. /* Make the entity visible to debuggers etc., even with '-fwhole-program'. */
  135. /* Applies to: functions, variables. */
  136. #define ATTRIBUTE_EXTERNALLY_VISIBLE _GL_ATTRIBUTE_EXTERNALLY_VISIBLE
  137. /* ========== Attributes that mainly direct compiler optimizations ========== */
  138. /* The function does not throw exceptions. */
  139. /* Applies to: functions. */
  140. /* After a function's parameter list, this attribute must come first, before
  141. other attributes. */
  142. #define ATTRIBUTE_NOTHROW _GL_ATTRIBUTE_NOTHROW
  143. /* Do not inline the function. */
  144. /* Applies to: functions. */
  145. #define ATTRIBUTE_NOINLINE _GL_ATTRIBUTE_NOINLINE
  146. /* Always inline the function, and report an error if the compiler
  147. cannot inline. */
  148. /* Applies to: function. */
  149. #define ATTRIBUTE_ALWAYS_INLINE _GL_ATTRIBUTE_ALWAYS_INLINE
  150. /* It is OK for a compiler to omit duplicate calls with the same arguments.
  151. This attribute is safe for a function that neither depends on
  152. nor affects observable state, and always returns exactly once -
  153. e.g., does not loop forever, and does not call longjmp.
  154. (This attribute is stricter than ATTRIBUTE_PURE.) */
  155. /* Applies to: functions. */
  156. #define ATTRIBUTE_CONST _GL_ATTRIBUTE_CONST
  157. /* It is OK for a compiler to omit duplicate calls with the same
  158. arguments if observable state is not changed between calls.
  159. This attribute is safe for a function that does not affect
  160. observable state, and always returns exactly once.
  161. (This attribute is looser than ATTRIBUTE_CONST.) */
  162. /* Applies to: functions. */
  163. #define ATTRIBUTE_PURE _GL_ATTRIBUTE_PURE
  164. /* The function is rarely executed. */
  165. /* Applies to: functions. */
  166. #define ATTRIBUTE_COLD _GL_ATTRIBUTE_COLD
  167. /* If called from some other compilation unit, the function executes
  168. code from that unit only by return or by exception handling,
  169. letting the compiler optimize that unit more aggressively. */
  170. /* Applies to: functions. */
  171. #define ATTRIBUTE_LEAF _GL_ATTRIBUTE_LEAF
  172. /* For struct members: The member has the smallest possible alignment.
  173. For struct, union, class: All members have the smallest possible alignment,
  174. minimizing the memory required. */
  175. /* Applies to: struct members, struct, union,
  176. in C++ also: class. */
  177. #define ATTRIBUTE_PACKED _GL_ATTRIBUTE_PACKED
  178. /* ================ Attributes that make invalid code valid ================ */
  179. /* Attributes that prevent fatal compiler optimizations for code that is not
  180. fully ISO C compliant. */
  181. /* Pointers to the type may point to the same storage as pointers to
  182. other types, thus disabling strict aliasing optimization. */
  183. /* Applies to: types. */
  184. #define ATTRIBUTE_MAY_ALIAS _GL_ATTRIBUTE_MAY_ALIAS
  185. #endif /* _GL_ATTRIBUTE_H */