summaryrefslogtreecommitdiffstats
path: root/gl/attribute.h
diff options
context:
space:
mode:
Diffstat (limited to 'gl/attribute.h')
-rw-r--r--gl/attribute.h154
1 files changed, 131 insertions, 23 deletions
diff --git a/gl/attribute.h b/gl/attribute.h
index 710341ba..c50befdf 100644
--- a/gl/attribute.h
+++ b/gl/attribute.h
@@ -1,6 +1,6 @@
1/* ATTRIBUTE_* macros for using attributes in GCC and similar compilers 1/* ATTRIBUTE_* macros for using attributes in GCC and similar compilers
2 2
3 Copyright 2020-2024 Free Software Foundation, Inc. 3 Copyright 2020-2026 Free Software Foundation, Inc.
4 4
5 This file is free software: you can redistribute it and/or modify 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 6 it under the terms of the GNU Lesser General Public License as
@@ -20,12 +20,50 @@
20/* Provide public ATTRIBUTE_* names for the private _GL_ATTRIBUTE_* 20/* Provide public ATTRIBUTE_* names for the private _GL_ATTRIBUTE_*
21 macros used within Gnulib. */ 21 macros used within Gnulib. */
22 22
23/* These attributes can be placed in two ways: 23/* The placement of these attributes depends on the kind of declaration
24 - At the start of a declaration (i.e. even before storage-class 24 and, in some cases, also on the programming language (C vs. C++).
25 specifiers!); then they apply to all entities that are declared 25
26 by the declaration. 26 In function declarations and function definitions:
27 - Immediately after the name of an entity being declared by the 27
28 declaration; then they apply to that entity only. */ 28 * ATTRIBUTE_NOTHROW must come after the parameter list.
29
30 * The macros
31 ATTRIBUTE_CONST
32 ATTRIBUTE_PURE
33 DEPRECATED
34 MAYBE_UNUSED
35 NODISCARD
36 REPRODUCIBLE
37 UNSEQUENCED
38 must come before the return type, and more precisely:
39 - In a function declaration/definition without a storage-class
40 specifier: at the beginning of the declaration/definition.
41 - In a function declaration/definition with a storage-class
42 specifier:
43 - In C: before the storage-class specifier.
44 - In C++: between the storage-class specifier and the return type.
45
46 * The other macros can be placed
47 - Either
48 - In a function declaration/definition without a storage-class
49 specifier: at the beginning of the declaration/definition.
50 - In a function declaration/definition with a storage-class
51 specifier: between the storage-class specifier and the return
52 type.
53 - Or, in a function declaration:
54 after the parameter list,
55 ∙ but after ATTRIBUTE_NOTHROW if present.
56
57 In other declarations, such as variable declarations:
58
59 * Either
60 - In C: before the storage-class specifier.
61 - In C++: between the storage-class specifier and the return type.
62 Then they apply to all entities that are declared by the declaration.
63
64 * Or immediately after the name of an entity being declared by the
65 declaration. Then they apply to that entity only.
66 */
29 67
30#ifndef _GL_ATTRIBUTE_H 68#ifndef _GL_ATTRIBUTE_H
31#define _GL_ATTRIBUTE_H 69#define _GL_ATTRIBUTE_H
@@ -48,9 +86,10 @@
48 _GL_ATTRIBUTE_FALLTHROUGH, _GL_ATTRIBUTE_FORMAT, _GL_ATTRIBUTE_LEAF, 86 _GL_ATTRIBUTE_FALLTHROUGH, _GL_ATTRIBUTE_FORMAT, _GL_ATTRIBUTE_LEAF,
49 _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_MAY_ALIAS, _GL_ATTRIBUTE_MAYBE_UNUSED, 87 _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_MAY_ALIAS, _GL_ATTRIBUTE_MAYBE_UNUSED,
50 _GL_ATTRIBUTE_NODISCARD, _GL_ATTRIBUTE_NOINLINE, _GL_ATTRIBUTE_NONNULL, 88 _GL_ATTRIBUTE_NODISCARD, _GL_ATTRIBUTE_NOINLINE, _GL_ATTRIBUTE_NONNULL,
51 _GL_ATTRIBUTE_NONSTRING, _GL_ATTRIBUTE_NOTHROW, _GL_ATTRIBUTE_PACKED, 89 _GL_ATTRIBUTE_NONNULL_IF_NONZERO, _GL_ATTRIBUTE_NONSTRING,
52 _GL_ATTRIBUTE_PURE, _GL_ATTRIBUTE_RETURNS_NONNULL, 90 _GL_ATTRIBUTE_NOTHROW, _GL_ATTRIBUTE_PACKED, _GL_ATTRIBUTE_PURE,
53 _GL_ATTRIBUTE_SENTINEL. */ 91 _GL_ATTRIBUTE_REPRODUCIBLE, _GL_ATTRIBUTE_RETURNS_NONNULL,
92 _GL_ATTRIBUTE_SENTINEL, _GL_ATTRIBUTE_UNSEQUENCED, _GL_UNNAMED. */
54#if !_GL_CONFIG_H_INCLUDED 93#if !_GL_CONFIG_H_INCLUDED
55 #error "Please include config.h first." 94 #error "Please include config.h first."
56#endif 95#endif
@@ -88,7 +127,7 @@
88 is the size of the returned memory block. 127 is the size of the returned memory block.
89 ATTRIBUTE_ALLOC_SIZE ((M, N)) - Multiply the Mth and Nth arguments 128 ATTRIBUTE_ALLOC_SIZE ((M, N)) - Multiply the Mth and Nth arguments
90 to determine the size of the returned memory block. */ 129 to determine the size of the returned memory block. */
91/* Applies to: function, pointer to function, function types. */ 130/* Applies to: functions, pointer to functions, function types. */
92#define ATTRIBUTE_ALLOC_SIZE(args) _GL_ATTRIBUTE_ALLOC_SIZE (args) 131#define ATTRIBUTE_ALLOC_SIZE(args) _GL_ATTRIBUTE_ALLOC_SIZE (args)
93 132
94/* ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers 133/* ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers
@@ -132,6 +171,12 @@
132/* Applies to: functions. */ 171/* Applies to: functions. */
133#define ATTRIBUTE_NONNULL(args) _GL_ATTRIBUTE_NONNULL (args) 172#define ATTRIBUTE_NONNULL(args) _GL_ATTRIBUTE_NONNULL (args)
134 173
174/* ATTRIBUTE_NONNULL_IF_NONZERO (NP, NI) - Argument NP (a pointer)
175 must not be NULL if the argument NI (an integer) is != 0. */
176/* Applies to: functions. */
177#define ATTRIBUTE_NONNULL_IF_NONZERO(np, ni) _GL_ATTRIBUTE_NONNULL_IF_NONZERO (np, ni)
178
179
135/* The function's return value is a non-NULL pointer. */ 180/* The function's return value is a non-NULL pointer. */
136/* Applies to: functions. */ 181/* Applies to: functions. */
137#define ATTRIBUTE_RETURNS_NONNULL _GL_ATTRIBUTE_RETURNS_NONNULL 182#define ATTRIBUTE_RETURNS_NONNULL _GL_ATTRIBUTE_RETURNS_NONNULL
@@ -170,7 +215,7 @@
170/* Attributes regarding debugging information emitted by the compiler. */ 215/* Attributes regarding debugging information emitted by the compiler. */
171 216
172/* Omit the function from stack traces when debugging. */ 217/* Omit the function from stack traces when debugging. */
173/* Applies to: function. */ 218/* Applies to: functions. */
174#define ATTRIBUTE_ARTIFICIAL _GL_ATTRIBUTE_ARTIFICIAL 219#define ATTRIBUTE_ARTIFICIAL _GL_ATTRIBUTE_ARTIFICIAL
175 220
176/* Make the entity visible to debuggers etc., even with '-fwhole-program'. */ 221/* Make the entity visible to debuggers etc., even with '-fwhole-program'. */
@@ -192,25 +237,74 @@
192 237
193/* Always inline the function, and report an error if the compiler 238/* Always inline the function, and report an error if the compiler
194 cannot inline. */ 239 cannot inline. */
195/* Applies to: function. */ 240/* Applies to: functions. */
196#define ATTRIBUTE_ALWAYS_INLINE _GL_ATTRIBUTE_ALWAYS_INLINE 241#define ATTRIBUTE_ALWAYS_INLINE _GL_ATTRIBUTE_ALWAYS_INLINE
197 242
198/* It is OK for a compiler to omit duplicate calls with the same arguments. 243/* It is OK for a compiler to move a call, or omit a duplicate call
199 This attribute is safe for a function that neither depends on 244 and reuse a cached return value, even if the state changes between calls.
200 nor affects observable state, and always returns exactly once - 245 It is also OK to omit a call if the result is not used.
201 e.g., does not loop forever, and does not call longjmp. 246 This attribute is safe if the function does not change observable state,
202 (This attribute is stricter than ATTRIBUTE_PURE.) */ 247 returns a value determined solely by its arguments' values
248 without examining state, and always returns exactly once -
249 e.g., does not raise an exception, call longjmp, or loop forever.
250 (This attribute is stricter than _GL_ATTRIBUTE_PURE because the
251 function cannot observe state. Unlike _GL_ATTRIBUTE_UNSEQUENCED
252 the function must return exactly once and cannot access state
253 addressed by its pointer arguments or that happens to have the same
254 value for all calls to the function, but the function is allowed to
255 return a pointer to storage that can be modified later. */
203/* Applies to: functions. */ 256/* Applies to: functions. */
204#define ATTRIBUTE_CONST _GL_ATTRIBUTE_CONST 257#define ATTRIBUTE_CONST _GL_ATTRIBUTE_CONST
205 258
206/* It is OK for a compiler to omit duplicate calls with the same 259/* It is OK for a compiler to move a call, or omit a duplicate call
207 arguments if observable state is not changed between calls. 260 and reuse a cached value returned either directly or indirectly via
208 This attribute is safe for a function that does not affect 261 a pointer, if the state addressed by its pointer arguments is the same;
209 observable state, and always returns exactly once. 262 however, pointer arguments cannot alias.
210 (This attribute is looser than ATTRIBUTE_CONST.) */ 263 This attribute is safe for a function that is effectless, idempotent,
264 stateless, and independent; see ISO C 23 § 6.7.13.8 for a definition of
265 these terms.
266 (This attribute is stricter than _GL_ATTRIBUTE_REPRODUCIBLE because
267 the function must be stateless and independent. Unlike
268 _GL_ATTRIBUTE_CONST the function need not return exactly once, and
269 can depend on state accessed via its pointer arguments or that
270 happens to have the same value for all calls to the function, but
271 the function cannot return a pointer to storage whose contents
272 change later.)
273 See also <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2956.htm> and
274 <https://stackoverflow.com/questions/76847905/>. */
275/* Applies to: functions, pointer to functions, function type. */
276#define UNSEQUENCED _GL_ATTRIBUTE_UNSEQUENCED
277
278/* It is OK for a compiler to move a call, or omit a duplicate call
279 and reuse a cached return value, if observable state is the same.
280 It is also OK to omit a call if the return value is not used.
281 This attribute is safe if the function does not change observable state,
282 returns a value determined solely by its arguments's values
283 together with observable state, and always returns exactly once.
284 (This attribute is looser than _GL_ATTRIBUTE_CONST because the function
285 can depend on observable state.
286 Unlike _GL_ATTRIBUTE_REPRODUCIBLE the function must return exactly
287 once and cannot change state addressed by its arguments, but the
288 function can return a pointer to storage whose contents change later.) */
211/* Applies to: functions. */ 289/* Applies to: functions. */
212#define ATTRIBUTE_PURE _GL_ATTRIBUTE_PURE 290#define ATTRIBUTE_PURE _GL_ATTRIBUTE_PURE
213 291
292/* It is OK for a compiler to move a call, or omit a duplicate call
293 and reuse a cached value returned either directly or indirectly via
294 a pointer, if other observable state is the same;
295 however, pointer arguments cannot alias.
296 This attribute is safe for a function that is effectless and idempotent;
297 see ISO C 23 § 6.7.13.8 for a definition of these terms.
298 (This attribute is looser than _GL_ATTRIBUTE_UNSEQUENCED because
299 the function need not be stateless or independent.
300 Unlike _GL_ATTRIBUTE_PURE the function need not return exactly once
301 and can change state addressed by its pointer arguments, but the
302 function cannot return a pointer to storage whose contents change later.)
303 See also <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2956.htm> and
304 <https://stackoverflow.com/questions/76847905/>. */
305/* Applies to: functions, pointer to functions, function type. */
306#define REPRODUCIBLE _GL_ATTRIBUTE_REPRODUCIBLE
307
214/* The function is rarely executed. */ 308/* The function is rarely executed. */
215/* Applies to: functions. */ 309/* Applies to: functions. */
216#define ATTRIBUTE_COLD _GL_ATTRIBUTE_COLD 310#define ATTRIBUTE_COLD _GL_ATTRIBUTE_COLD
@@ -240,4 +334,18 @@
240#define ATTRIBUTE_MAY_ALIAS _GL_ATTRIBUTE_MAY_ALIAS 334#define ATTRIBUTE_MAY_ALIAS _GL_ATTRIBUTE_MAY_ALIAS
241 335
242 336
337/* ==================== Unnamed function parameters ======================== */
338
339/* Although UNNAMED is not an attribute, it is related to MAYBE_UNUSED
340 and so is defined here for convenience. */
341
342/* UNNAMED (ID) is the "name" of an unnamed function parameter.
343 Each of the function's unnamed parameters should have a unique "name".
344 The "name" cannot be used. This ports both to C17 and earlier, which
345 lack unnamed parameters, and to C++ and later C, which have them. */
346/* Applies to:
347 - function parameters. */
348#define UNNAMED(id) _GL_UNNAMED (id)
349
350
243#endif /* _GL_ATTRIBUTE_H */ 351#endif /* _GL_ATTRIBUTE_H */