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