xref: /aosp_15_r20/external/coreboot/src/commonlib/bsd/include/commonlib/bsd/compiler.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only */
2 
3 #ifndef _COMMONLIB_BSD_COMPILER_H_
4 #define _COMMONLIB_BSD_COMPILER_H_
5 
6 #ifndef __packed
7 #if defined(__WIN32) || defined(__WIN64)
8 #define __packed __attribute__((__gcc_struct__, __packed__))
9 #else
10 #define __packed __attribute__((__packed__))
11 #endif
12 #endif
13 
14 #ifndef __aligned
15 #define __aligned(x) __attribute__((__aligned__(x)))
16 #endif
17 
18 /*
19  * Because there may be variables/parameters whose name contains "__unused" in
20  * header files of libc, namely musl, names consistent with the ones in the
21  * Linux kernel may be a better choice.
22  */
23 
24 /*
25  * This is used to mark identifiers unused in all conditions, e.g. a parameter
26  * completely unused in all code branch, only present to fit an API.
27  */
28 #ifndef __always_unused
29 #define __always_unused __attribute__((__unused__))
30 #endif
31 
32 /*
33  * This is used to mark identifiers unused in some conditions, e.g. a parameter
34  * only unused in some code branches, a global variable only accessed with code
35  * being conditionally preprocessed, etc.
36  */
37 #ifndef __maybe_unused
38 #define __maybe_unused __attribute__((__unused__))
39 #endif
40 
41 #ifndef __must_check
42 #define __must_check __attribute__((__warn_unused_result__))
43 #endif
44 
45 #ifndef __weak
46 #define __weak __attribute__((__weak__))
47 #endif
48 
49 #ifndef __noreturn
50 #define __noreturn __attribute__((__noreturn__))
51 #endif
52 
53 #ifndef __section
54 #define __section(section) __attribute__((__section__(section)))
55 #endif
56 
57 #ifndef __always_inline
58 #define __always_inline inline __attribute__((__always_inline__))
59 #endif
60 
61 #ifndef __fallthrough
62 #define __fallthrough __attribute__((__fallthrough__))
63 #endif
64 
65 #ifndef __printf
66 #define __printf(a, b) __attribute__((format(printf, a, b)))
67 #endif
68 
69 /*
70  * This evaluates to the type of the first expression, unless that is constant
71  * in which case it evaluates to the type of the second. This is useful when
72  * assigning macro parameters to temporary variables, because that would
73  * normally circumvent the special loosened type promotion rules for integer
74  * literals. By using this macro, the promotion can happen at the time the
75  * literal is assigned to the temporary variable. If the literal doesn't fit in
76  * the chosen type, -Werror=overflow will catch it, so this should be safe.
77  */
78 #define __TYPEOF_UNLESS_CONST(expr, fallback_expr) __typeof__( \
79 	__builtin_choose_expr(__builtin_constant_p(expr), fallback_expr, expr))
80 
81 /* This creates a unique local variable name for use in macros. */
82 #define __TMPNAME_3(i) __tmpname_##i
83 #define __TMPNAME_2(i) __TMPNAME_3(i)
84 #define __TMPNAME __TMPNAME_2(__COUNTER__)
85 
86 #endif
87