attribute.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* ATTRIBUTE_* macros for using attributes in GCC and similar compilers
  2. Copyright 2020-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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 GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU 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. * C2X 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. /* =============== Attributes for specific kinds of functions =============== */
  33. /* Attributes for functions that should not be used. */
  34. /* Warn if the entity is used. */
  35. /* Applies to:
  36. - function, variable,
  37. - struct, union, struct/union member,
  38. - enumeration, enumeration item,
  39. - typedef,
  40. in C++ also: namespace, class, template specialization. */
  41. #define DEPRECATED _GL_ATTRIBUTE_DEPRECATED
  42. /* If a function call is not optimized way, warn with MSG. */
  43. /* Applies to: functions. */
  44. #define ATTRIBUTE_WARNING(msg) _GL_ATTRIBUTE_WARNING (msg)
  45. /* If a function call is not optimized way, report an error with MSG. */
  46. /* Applies to: functions. */
  47. #define ATTRIBUTE_ERROR(msg) _GL_ATTRIBUTE_ERROR (msg)
  48. /* Attributes for memory-allocating functions. */
  49. /* The function returns a pointer to freshly allocated memory. */
  50. /* Applies to: functions. */
  51. #define ATTRIBUTE_MALLOC _GL_ATTRIBUTE_MALLOC
  52. /* ATTRIBUTE_ALLOC_SIZE ((N)) - The Nth argument of the function
  53. is the size of the returned memory block.
  54. ATTRIBUTE_ALLOC_SIZE ((M, N)) - Multiply the Mth and Nth arguments
  55. to determine the size of the returned memory block. */
  56. /* Applies to: function, pointer to function, function types. */
  57. #define ATTRIBUTE_ALLOC_SIZE(args) _GL_ATTRIBUTE_ALLOC_SIZE (args)
  58. /* Attributes for variadic functions. */
  59. /* The variadic function expects a trailing NULL argument.
  60. ATTRIBUTE_SENTINEL () - The last argument is NULL (requires C99).
  61. ATTRIBUTE_SENTINEL ((N)) - The (N+1)st argument from the end is NULL. */
  62. /* Applies to: functions. */
  63. #define ATTRIBUTE_SENTINEL(pos) _GL_ATTRIBUTE_SENTINEL (pos)
  64. /* ================== Attributes for compiler diagnostics ================== */
  65. /* Attributes that help the compiler diagnose programmer mistakes.
  66. Some of them may also help for some compiler optimizations. */
  67. /* ATTRIBUTE_FORMAT ((ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)) -
  68. The STRING-INDEXth function argument is a format string of style
  69. ARCHETYPE, which is one of:
  70. printf, gnu_printf
  71. scanf, gnu_scanf,
  72. strftime, gnu_strftime,
  73. strfmon,
  74. or the same thing prefixed and suffixed with '__'.
  75. If FIRST-TO-CHECK is not 0, arguments starting at FIRST-TO_CHECK
  76. are suitable for the format string. */
  77. /* Applies to: functions. */
  78. #define ATTRIBUTE_FORMAT(spec) _GL_ATTRIBUTE_FORMAT (spec)
  79. /* ATTRIBUTE_NONNULL ((N1, N2,...)) - Arguments N1, N2,... must not be NULL.
  80. ATTRIBUTE_NONNULL () - All pointer arguments must not be null. */
  81. /* Applies to: functions. */
  82. #define ATTRIBUTE_NONNULL(args) _GL_ATTRIBUTE_NONNULL (args)
  83. /* The function's return value is a non-NULL pointer. */
  84. /* Applies to: functions. */
  85. #define ATTRIBUTE_RETURNS_NONNULL _GL_ATTRIBUTE_RETURNS_NONNULL
  86. /* Warn if the caller does not use the return value,
  87. unless the caller uses something like ignore_value. */
  88. /* Applies to: function, enumeration, class. */
  89. #define NODISCARD _GL_ATTRIBUTE_NODISCARD
  90. /* Attributes that disable false alarms when the compiler diagnoses
  91. programmer "mistakes". */
  92. /* Do not warn if the entity is not used. */
  93. /* Applies to:
  94. - function, variable,
  95. - struct, union, struct/union member,
  96. - enumeration, enumeration item,
  97. - typedef,
  98. in C++ also: class. */
  99. #define MAYBE_UNUSED _GL_ATTRIBUTE_MAYBE_UNUSED
  100. /* The contents of a character array is not meant to be NUL-terminated. */
  101. /* Applies to: struct/union members and variables that are arrays of element
  102. type '[[un]signed] char'. */
  103. #define ATTRIBUTE_NONSTRING _GL_ATTRIBUTE_NONSTRING
  104. /* Do not warn if control flow falls through to the immediately
  105. following 'case' or 'default' label. */
  106. /* Applies to: Empty statement (;), inside a 'switch' statement. */
  107. #define FALLTHROUGH _GL_ATTRIBUTE_FALLTHROUGH
  108. /* ================== Attributes for debugging information ================== */
  109. /* Attributes regarding debugging information emitted by the compiler. */
  110. /* Omit the function from stack traces when debugging. */
  111. /* Applies to: function. */
  112. #define ATTRIBUTE_ARTIFICIAL _GL_ATTRIBUTE_ARTIFICIAL
  113. /* Make the entity visible to debuggers etc., even with '-fwhole-program'. */
  114. /* Applies to: functions, variables. */
  115. #define ATTRIBUTE_EXTERNALLY_VISIBLE _GL_ATTRIBUTE_EXTERNALLY_VISIBLE
  116. /* ========== Attributes that mainly direct compiler optimizations ========== */
  117. /* The function does not throw exceptions. */
  118. /* Applies to: functions. */
  119. #define ATTRIBUTE_NOTHROW _GL_ATTRIBUTE_NOTHROW
  120. /* Do not inline the function. */
  121. /* Applies to: functions. */
  122. #define ATTRIBUTE_NOINLINE _GL_ATTRIBUTE_NOINLINE
  123. /* Always inline the function, and report an error if the compiler
  124. cannot inline. */
  125. /* Applies to: function. */
  126. #define ATTRIBUTE_ALWAYS_INLINE _GL_ATTRIBUTE_ALWAYS_INLINE
  127. /* It is OK for a compiler to omit duplicate calls with the same arguments.
  128. This attribute is safe for a function that neither depends on
  129. nor affects observable state, and always returns exactly once -
  130. e.g., does not loop forever, and does not call longjmp.
  131. (This attribute is stricter than ATTRIBUTE_PURE.) */
  132. /* Applies to: functions. */
  133. #define ATTRIBUTE_CONST _GL_ATTRIBUTE_CONST
  134. /* It is OK for a compiler to omit duplicate calls with the same
  135. arguments if observable state is not changed between calls.
  136. This attribute is safe for a function that does not affect
  137. observable state, and always returns exactly once.
  138. (This attribute is looser than ATTRIBUTE_CONST.) */
  139. /* Applies to: functions. */
  140. #define ATTRIBUTE_PURE _GL_ATTRIBUTE_PURE
  141. /* The function is rarely executed. */
  142. /* Applies to: functions. */
  143. #define ATTRIBUTE_COLD _GL_ATTRIBUTE_COLD
  144. /* If called from some other compilation unit, the function executes
  145. code from that unit only by return or by exception handling,
  146. letting the compiler optimize that unit more aggressively. */
  147. /* Applies to: functions. */
  148. #define ATTRIBUTE_LEAF _GL_ATTRIBUTE_LEAF
  149. /* For struct members: The member has the smallest possible alignment.
  150. For struct, union, class: All members have the smallest possible alignment,
  151. minimizing the memory required. */
  152. /* Applies to: struct members, struct, union,
  153. in C++ also: class. */
  154. #define ATTRIBUTE_PACKED _GL_ATTRIBUTE_PACKED
  155. /* ================ Attributes that make invalid code valid ================ */
  156. /* Attributes that prevent fatal compiler optimizations for code that is not
  157. fully ISO C compliant. */
  158. /* Pointers to the type may point to the same storage as pointers to
  159. other types, thus disabling strict aliasing optimization. */
  160. /* Applies to: types. */
  161. #define ATTRIBUTE_MAY_ALIAS _GL_ATTRIBUTE_MAY_ALIAS
  162. #endif /* _GL_ATTRIBUTE_H */