xref: /linux/include/linux/compiler_types.h (revision 8934827db5403eae57d4537114a9ff88b0a8460f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_COMPILER_TYPES_H
3 #define __LINUX_COMPILER_TYPES_H
4 
5 /*
6  * __has_builtin is supported on gcc >= 10, clang >= 3 and icc >= 21.
7  * In the meantime, to support gcc < 10, we implement __has_builtin
8  * by hand.
9  */
10 #ifndef __has_builtin
11 #define __has_builtin(x) (0)
12 #endif
13 
14 /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
15 #define ___PASTE(a, b) a##b
16 #define __PASTE(a, b) ___PASTE(a, b)
17 
18 #ifndef __ASSEMBLY__
19 
20 /*
21  * C23 introduces "auto" as a standard way to define type-inferred
22  * variables, but "auto" has been a (useless) keyword even since K&R C,
23  * so it has always been "namespace reserved."
24  *
25  * Until at some future time we require C23 support, we need the gcc
26  * extension __auto_type, but there is no reason to put that elsewhere
27  * in the source code.
28  */
29 #if __STDC_VERSION__ < 202311L
30 # define auto __auto_type
31 #endif
32 
33 /*
34  * Skipped when running bindgen due to a libclang issue;
35  * see https://github.com/rust-lang/rust-bindgen/issues/2244.
36  */
37 #if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) && \
38 	__has_attribute(btf_type_tag) && !defined(__BINDGEN__)
39 # define BTF_TYPE_TAG(value) __attribute__((btf_type_tag(#value)))
40 #else
41 # define BTF_TYPE_TAG(value) /* nothing */
42 #endif
43 
44 #include <linux/compiler-context-analysis.h>
45 
46 /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
47 #ifdef __CHECKER__
48 /* address spaces */
49 # define __kernel	__attribute__((address_space(0)))
50 # define __user		__attribute__((noderef, address_space(__user)))
51 # define __iomem	__attribute__((noderef, address_space(__iomem)))
52 # define __percpu	__attribute__((noderef, address_space(__percpu)))
53 # define __rcu		__attribute__((noderef, address_space(__rcu)))
__chk_user_ptr(const volatile void __user * ptr)54 static inline void __chk_user_ptr(const volatile void __user *ptr) { }
__chk_io_ptr(const volatile void __iomem * ptr)55 static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
56 /* other */
57 # define __force	__attribute__((force))
58 # define __nocast	__attribute__((nocast))
59 # define __safe		__attribute__((safe))
60 # define __private	__attribute__((noderef))
61 # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
62 #else /* __CHECKER__ */
63 /* address spaces */
64 # define __kernel
65 # ifdef STRUCTLEAK_PLUGIN
66 #  define __user	__attribute__((user))
67 # else
68 #  define __user	BTF_TYPE_TAG(user)
69 # endif
70 # define __iomem
71 # define __percpu	__percpu_qual BTF_TYPE_TAG(percpu)
72 # define __rcu		BTF_TYPE_TAG(rcu)
73 
74 # define __chk_user_ptr(x)	(void)0
75 # define __chk_io_ptr(x)	(void)0
76 /* other */
77 # define __force
78 # define __nocast
79 # define __safe
80 # define __private
81 # define ACCESS_PRIVATE(p, member) ((p)->member)
82 # define __builtin_warning(x, y...) (1)
83 #endif /* __CHECKER__ */
84 
85 #ifdef __KERNEL__
86 
87 /* Attributes */
88 #include <linux/compiler_attributes.h>
89 
90 #if CONFIG_FUNCTION_ALIGNMENT > 0
91 #define __function_aligned		__aligned(CONFIG_FUNCTION_ALIGNMENT)
92 #else
93 #define __function_aligned
94 #endif
95 
96 /*
97  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
98  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
99  *
100  * When -falign-functions=N is in use, we must avoid the cold attribute as
101  * GCC drops the alignment for cold functions. Worse, GCC can implicitly mark
102  * callees of cold functions as cold themselves, so it's not sufficient to add
103  * __function_aligned here as that will not ensure that callees are correctly
104  * aligned.
105  *
106  * See:
107  *
108  *   https://lore.kernel.org/lkml/Y77%2FqVgvaJidFpYt@FVFF77S0Q05N
109  *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c9
110  */
111 #if defined(CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT) || (CONFIG_FUNCTION_ALIGNMENT == 0)
112 #define __cold				__attribute__((__cold__))
113 #else
114 #define __cold
115 #endif
116 
117 /*
118  * On x86-64 and arm64 targets, __preserve_most changes the calling convention
119  * of a function to make the code in the caller as unintrusive as possible. This
120  * convention behaves identically to the C calling convention on how arguments
121  * and return values are passed, but uses a different set of caller- and callee-
122  * saved registers.
123  *
124  * The purpose is to alleviates the burden of saving and recovering a large
125  * register set before and after the call in the caller.  This is beneficial for
126  * rarely taken slow paths, such as error-reporting functions that may be called
127  * from hot paths.
128  *
129  * Note: This may conflict with instrumentation inserted on function entry which
130  * does not use __preserve_most or equivalent convention (if in assembly). Since
131  * function tracing assumes the normal C calling convention, where the attribute
132  * is supported, __preserve_most implies notrace.  It is recommended to restrict
133  * use of the attribute to functions that should or already disable tracing.
134  *
135  * Optional: not supported by gcc.
136  *
137  * clang: https://clang.llvm.org/docs/AttributeReference.html#preserve-most
138  */
139 #if __has_attribute(__preserve_most__) && (defined(CONFIG_X86_64) || defined(CONFIG_ARM64))
140 # define __preserve_most notrace __attribute__((__preserve_most__))
141 #else
142 # define __preserve_most
143 #endif
144 
145 /*
146  * Annotating a function/variable with __retain tells the compiler to place
147  * the object in its own section and set the flag SHF_GNU_RETAIN. This flag
148  * instructs the linker to retain the object during garbage-cleanup or LTO
149  * phases.
150  *
151  * Note that the __used macro is also used to prevent functions or data
152  * being optimized out, but operates at the compiler/IR-level and may still
153  * allow unintended removal of objects during linking.
154  *
155  * Optional: only supported since gcc >= 11, clang >= 13
156  *
157  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-retain-function-attribute
158  * clang: https://clang.llvm.org/docs/AttributeReference.html#retain
159  */
160 #if __has_attribute(__retain__) && \
161 	(defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || \
162 	 defined(CONFIG_LTO_CLANG))
163 # define __retain			__attribute__((__retain__))
164 #else
165 # define __retain
166 #endif
167 
168 /* Compiler specific macros. */
169 #ifdef __clang__
170 #include <linux/compiler-clang.h>
171 #elif defined(__GNUC__)
172 /* The above compilers also define __GNUC__, so order is important here. */
173 #include <linux/compiler-gcc.h>
174 #else
175 #error "Unknown compiler"
176 #endif
177 
178 /*
179  * Some architectures need to provide custom definitions of macros provided
180  * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
181  * conditionally rather than using an asm-generic wrapper in order to avoid
182  * build failures if any C compilation, which will include this file via an
183  * -include argument in c_flags, occurs prior to the asm-generic wrappers being
184  * generated.
185  */
186 #ifdef CONFIG_HAVE_ARCH_COMPILER_H
187 #include <asm/compiler.h>
188 #endif
189 
190 struct ftrace_branch_data {
191 	const char *func;
192 	const char *file;
193 	unsigned line;
194 	union {
195 		struct {
196 			unsigned long correct;
197 			unsigned long incorrect;
198 		};
199 		struct {
200 			unsigned long miss;
201 			unsigned long hit;
202 		};
203 		unsigned long miss_hit[2];
204 	};
205 };
206 
207 struct ftrace_likely_data {
208 	struct ftrace_branch_data	data;
209 	unsigned long			constant;
210 };
211 
212 #if defined(CC_USING_HOTPATCH)
213 #define notrace			__attribute__((hotpatch(0, 0)))
214 #elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
215 #define notrace			__attribute__((patchable_function_entry(0, 0)))
216 #else
217 #define notrace			__attribute__((__no_instrument_function__))
218 #endif
219 
220 /*
221  * it doesn't make sense on ARM (currently the only user of __naked)
222  * to trace naked functions because then mcount is called without
223  * stack and frame pointer being set up and there is no chance to
224  * restore the lr register to the value before mcount was called.
225  */
226 #define __naked			__attribute__((__naked__)) notrace
227 
228 /*
229  * Prefer gnu_inline, so that extern inline functions do not emit an
230  * externally visible function. This makes extern inline behave as per gnu89
231  * semantics rather than c99. This prevents multiple symbol definition errors
232  * of extern inline functions at link time.
233  * A lot of inline functions can cause havoc with function tracing.
234  */
235 #define inline inline __gnu_inline __inline_maybe_unused notrace
236 
237 /*
238  * gcc provides both __inline__ and __inline as alternate spellings of
239  * the inline keyword, though the latter is undocumented. New kernel
240  * code should only use the inline spelling, but some existing code
241  * uses __inline__. Since we #define inline above, to ensure
242  * __inline__ has the same semantics, we need this #define.
243  *
244  * However, the spelling __inline is strictly reserved for referring
245  * to the bare keyword.
246  */
247 #define __inline__ inline
248 
249 /*
250  * GCC does not warn about unused static inline functions for -Wunused-function.
251  * Suppress the warning in clang as well by using __maybe_unused, but enable it
252  * for W=2 build. This will allow clang to find unused functions.
253  */
254 #ifdef KBUILD_EXTRA_WARN2
255 #define __inline_maybe_unused
256 #else
257 #define __inline_maybe_unused __maybe_unused
258 #endif
259 
260 /*
261  * Rather then using noinline to prevent stack consumption, use
262  * noinline_for_stack instead.  For documentation reasons.
263  */
264 #define noinline_for_stack noinline
265 
266 /*
267  * Use noinline_for_tracing for functions that should not be inlined.
268  * For tracing reasons.
269  */
270 #define noinline_for_tracing noinline
271 
272 /*
273  * Sanitizer helper attributes: Because using __always_inline and
274  * __no_sanitize_* conflict, provide helper attributes that will either expand
275  * to __no_sanitize_* in compilation units where instrumentation is enabled
276  * (__SANITIZE_*__), or __always_inline in compilation units without
277  * instrumentation (__SANITIZE_*__ undefined).
278  */
279 #ifdef __SANITIZE_ADDRESS__
280 /*
281  * We can't declare function 'inline' because __no_sanitize_address conflicts
282  * with inlining. Attempt to inline it may cause a build failure.
283  *     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
284  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
285  */
286 # define __no_kasan_or_inline __no_sanitize_address notrace __maybe_unused
287 # define __no_sanitize_or_inline __no_kasan_or_inline
288 #else
289 # define __no_kasan_or_inline __always_inline
290 #endif
291 
292 #ifdef CONFIG_KCSAN
293 /*
294  * Type qualifier to mark variables where all data-racy accesses should be
295  * ignored by KCSAN. Note, the implementation simply marks these variables as
296  * volatile, since KCSAN will treat such accesses as "marked".
297  *
298  * Defined here because defining __data_racy as volatile for KCSAN objects only
299  * causes problems in BPF Type Format (BTF) generation since struct members
300  * of core kernel data structs will be volatile in some objects and not in
301  * others.  Instead define it globally for KCSAN kernels.
302  */
303 # define __data_racy volatile
304 #else
305 # define __data_racy
306 #endif
307 
308 #ifdef __SANITIZE_THREAD__
309 /*
310  * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin
311  * atomics even with __no_sanitize_thread (to avoid false positives in userspace
312  * ThreadSanitizer). The kernel's requirements are stricter and we really do not
313  * want any instrumentation with __no_kcsan.
314  *
315  * Therefore we add __disable_sanitizer_instrumentation where available to
316  * disable all instrumentation. See Kconfig.kcsan where this is mandatory.
317  */
318 # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation
319 # define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused
320 #else
321 # define __no_kcsan
322 #endif
323 
324 #ifdef __SANITIZE_MEMORY__
325 /*
326  * Similarly to KASAN and KCSAN, KMSAN loses function attributes of inlined
327  * functions, therefore disabling KMSAN checks also requires disabling inlining.
328  *
329  * __no_sanitize_or_inline effectively prevents KMSAN from reporting errors
330  * within the function and marks all its outputs as initialized.
331  */
332 # define __no_sanitize_or_inline __no_kmsan_checks notrace __maybe_unused
333 #endif
334 
335 #ifndef __no_sanitize_or_inline
336 #define __no_sanitize_or_inline __always_inline
337 #endif
338 
339 /*
340  * The assume attribute is used to indicate that a certain condition is
341  * assumed to be true. If this condition is violated at runtime, the behavior
342  * is undefined. Compilers may or may not use this indication to generate
343  * optimized code.
344  *
345  * Note that the clang documentation states that optimizers may react
346  * differently to this attribute, and this may even have a negative
347  * performance impact. Therefore this attribute should be used with care.
348  *
349  * Optional: only supported since gcc >= 13
350  * Optional: only supported since clang >= 19
351  *
352  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-assume-statement-attribute
353  * clang: https://clang.llvm.org/docs/AttributeReference.html#id13
354  *
355  */
356 #ifdef CONFIG_CC_HAS_ASSUME
357 # define __assume(expr)			__attribute__((__assume__(expr)))
358 #else
359 # define __assume(expr)
360 #endif
361 
362 /*
363  * Optional: only supported since gcc >= 15
364  * Optional: only supported since clang >= 18
365  *
366  *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
367  * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
368  *
369  * __bdos on clang < 19.1.2 can erroneously return 0:
370  * https://github.com/llvm/llvm-project/pull/110497
371  *
372  * __bdos on clang < 19.1.3 can be off by 4:
373  * https://github.com/llvm/llvm-project/pull/112636
374  */
375 #ifdef CONFIG_CC_HAS_COUNTED_BY
376 # define __counted_by(member)		__attribute__((__counted_by__(member)))
377 #else
378 # define __counted_by(member)
379 #endif
380 
381 /*
382  * Runtime track number of objects pointed to by a pointer member for use by
383  * CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS.
384  *
385  * Optional: only supported since gcc >= 16
386  * Optional: only supported since clang >= 22
387  *
388  *   gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html
389  * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
390  */
391 #ifdef CONFIG_CC_HAS_COUNTED_BY_PTR
392 #define __counted_by_ptr(member)	__attribute__((__counted_by__(member)))
393 #else
394 #define __counted_by_ptr(member)
395 #endif
396 
397 /*
398  * Optional: only supported since gcc >= 15
399  * Optional: not supported by Clang
400  *
401  * gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178
402  */
403 #ifdef CONFIG_CC_HAS_MULTIDIMENSIONAL_NONSTRING
404 # define __nonstring_array		__attribute__((__nonstring__))
405 #else
406 # define __nonstring_array
407 #endif
408 
409 /*
410  * Apply __counted_by() when the Endianness matches to increase test coverage.
411  */
412 #ifdef __LITTLE_ENDIAN
413 #define __counted_by_le(member)	__counted_by(member)
414 #define __counted_by_be(member)
415 #else
416 #define __counted_by_le(member)
417 #define __counted_by_be(member)	__counted_by(member)
418 #endif
419 
420 /*
421  * This designates the minimum number of elements a passed array parameter must
422  * have. For example:
423  *
424  *     void some_function(u8 param[at_least 7]);
425  *
426  * If a caller passes an array with fewer than 7 elements, the compiler will
427  * emit a warning.
428  */
429 #ifndef __CHECKER__
430 #define at_least static
431 #else
432 #define at_least
433 #endif
434 
435 /* Do not trap wrapping arithmetic within an annotated function. */
436 #ifdef CONFIG_UBSAN_INTEGER_WRAP
437 # define __signed_wrap __attribute__((no_sanitize("signed-integer-overflow")))
438 #else
439 # define __signed_wrap
440 #endif
441 
442 /* Section for code which can't be instrumented at all */
443 #define __noinstr_section(section)					\
444 	noinline notrace __attribute((__section__(section)))		\
445 	__no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage \
446 	__no_sanitize_memory __signed_wrap
447 
448 #define noinstr __noinstr_section(".noinstr.text")
449 
450 /*
451  * The __cpuidle section is used twofold:
452  *
453  *  1) the original use -- identifying if a CPU is 'stuck' in idle state based
454  *     on it's instruction pointer. See cpu_in_idle().
455  *
456  *  2) supressing instrumentation around where cpuidle disables RCU; where the
457  *     function isn't strictly required for #1, this is interchangeable with
458  *     noinstr.
459  */
460 #define __cpuidle __noinstr_section(".cpuidle.text")
461 
462 #endif /* __KERNEL__ */
463 
464 #endif /* __ASSEMBLY__ */
465 
466 /*
467  * The below symbols may be defined for one or more, but not ALL, of the above
468  * compilers. We don't consider that to be an error, so set them to nothing.
469  * For example, some of them are for compiler specific plugins.
470  */
471 #ifndef __latent_entropy
472 # define __latent_entropy
473 #endif
474 
475 #if defined(RANDSTRUCT) && !defined(__CHECKER__)
476 # define __randomize_layout __designated_init __attribute__((randomize_layout))
477 # define __no_randomize_layout __attribute__((no_randomize_layout))
478 /* This anon struct can add padding, so only enable it under randstruct. */
479 # define randomized_struct_fields_start	struct {
480 # define randomized_struct_fields_end	} __randomize_layout;
481 #else
482 # define __randomize_layout __designated_init
483 # define __no_randomize_layout
484 # define randomized_struct_fields_start
485 # define randomized_struct_fields_end
486 #endif
487 
488 #ifndef __no_kstack_erase
489 # define __no_kstack_erase
490 #endif
491 
492 #ifndef __noscs
493 # define __noscs
494 #endif
495 
496 #if defined(CONFIG_CFI)
497 # define __nocfi		__attribute__((__no_sanitize__("kcfi")))
498 #else
499 # define __nocfi
500 #endif
501 
502 #if defined(CONFIG_ARCH_USES_CFI_GENERIC_LLVM_PASS)
503 # define __nocfi_generic	__nocfi
504 #else
505 # define __nocfi_generic
506 #endif
507 
508 /*
509  * Any place that could be marked with the "alloc_size" attribute is also
510  * a place to be marked with the "malloc" attribute, except those that may
511  * be performing a _reallocation_, as that may alias the existing pointer.
512  * For these, use __realloc_size().
513  */
514 #ifdef __alloc_size__
515 # define __alloc_size(x, ...)	__alloc_size__(x, ## __VA_ARGS__) __malloc
516 # define __realloc_size(x, ...)	__alloc_size__(x, ## __VA_ARGS__)
517 #else
518 # define __alloc_size(x, ...)	__malloc
519 # define __realloc_size(x, ...)
520 #endif
521 
522 /*
523  * When the size of an allocated object is needed, use the best available
524  * mechanism to find it. (For cases where sizeof() cannot be used.)
525  *
526  * Optional: only supported since gcc >= 12
527  *
528  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
529  * clang: https://clang.llvm.org/docs/LanguageExtensions.html#evaluating-object-size
530  */
531 #if __has_builtin(__builtin_dynamic_object_size)
532 #define __struct_size(p)	__builtin_dynamic_object_size(p, 0)
533 #define __member_size(p)	__builtin_dynamic_object_size(p, 1)
534 #else
535 #define __struct_size(p)	__builtin_object_size(p, 0)
536 #define __member_size(p)	__builtin_object_size(p, 1)
537 #endif
538 
539 /*
540  * Determine if an attribute has been applied to a variable.
541  * Using __annotated needs to check for __annotated being available,
542  * or negative tests may fail when annotation cannot be checked. For
543  * example, see the definition of __is_cstr().
544  */
545 #if __has_builtin(__builtin_has_attribute)
546 #define __annotated(var, attr)	__builtin_has_attribute(var, attr)
547 #endif
548 
549 /*
550  * Optional: only supported since gcc >= 15, clang >= 19
551  *
552  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fcounted_005fby_005fref
553  * clang: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-counted-by-ref
554  */
555 #if __has_builtin(__builtin_counted_by_ref) && \
556     !defined(CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF)
557 /**
558  * __flex_counter() - Get pointer to counter member for the given
559  *                    flexible array, if it was annotated with __counted_by()
560  * @FAM: Pointer to flexible array member of an addressable struct instance
561  *
562  * For example, with:
563  *
564  *	struct foo {
565  *		int counter;
566  *		short array[] __counted_by(counter);
567  *	} *p;
568  *
569  * __flex_counter(p->array) will resolve to &p->counter.
570  *
571  * Note that Clang may not allow this to be assigned to a separate
572  * variable; it must be used directly.
573  *
574  * If p->array is unannotated, this returns (void *)NULL.
575  */
576 #define __flex_counter(FAM)	__builtin_counted_by_ref(FAM)
577 #else
578 #define __flex_counter(FAM)	((void *)NULL)
579 #endif
580 
581 /*
582  * Some versions of gcc do not mark 'asm goto' volatile:
583  *
584  *  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979
585  *
586  * We do it here by hand, because it doesn't hurt.
587  */
588 #ifndef asm_goto_output
589 #define asm_goto_output(x...) asm volatile goto(x)
590 #endif
591 
592 /*
593  * Clang has trouble with constraints with multiple
594  * alternative behaviors ("g" , "rm" and "=rm").
595  */
596 #ifndef ASM_INPUT_G
597   #define ASM_INPUT_G "g"
598   #define ASM_INPUT_RM "rm"
599   #define ASM_OUTPUT_RM "=rm"
600 #endif
601 
602 #ifdef CONFIG_CC_HAS_ASM_INLINE
603 #define asm_inline asm __inline
604 #else
605 #define asm_inline asm
606 #endif
607 
608 #ifndef __ASSEMBLY__
609 /*
610  * Use __typeof_unqual__() when available.
611  */
612 #if CC_HAS_TYPEOF_UNQUAL || defined(__CHECKER__)
613 # define USE_TYPEOF_UNQUAL 1
614 #endif
615 
616 /* Are two types/vars the same type (ignoring qualifiers)? */
617 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
618 
619 /*
620  * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
621  *			       non-scalar types unchanged.
622  */
623 #ifndef USE_TYPEOF_UNQUAL
624 /*
625  * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
626  * is not type-compatible with 'signed char', and we define a separate case.
627  */
628 #define __scalar_type_to_expr_cases(type)				\
629 		unsigned type:	(unsigned type)0,			\
630 		signed type:	(signed type)0
631 
632 #define __unqual_scalar_typeof(x) typeof(				\
633 		_Generic((x),						\
634 			 char:	(char)0,				\
635 			 __scalar_type_to_expr_cases(char),		\
636 			 __scalar_type_to_expr_cases(short),		\
637 			 __scalar_type_to_expr_cases(int),		\
638 			 __scalar_type_to_expr_cases(long),		\
639 			 __scalar_type_to_expr_cases(long long),	\
640 			 default: (x)))
641 #else
642 #define __unqual_scalar_typeof(x) __typeof_unqual__(x)
643 #endif
644 #endif /* !__ASSEMBLY__ */
645 
646 /*
647  * __signed_scalar_typeof(x) - Declare a signed scalar type, leaving
648  *			       non-scalar types unchanged.
649  */
650 
651 #define __scalar_type_to_signed_cases(type)				\
652 		unsigned type:	(signed type)0,				\
653 		signed type:	(signed type)0
654 
655 #define __signed_scalar_typeof(x) typeof(				\
656 		_Generic((x),						\
657 			 char:	(signed char)0,				\
658 			 __scalar_type_to_signed_cases(char),		\
659 			 __scalar_type_to_signed_cases(short),		\
660 			 __scalar_type_to_signed_cases(int),		\
661 			 __scalar_type_to_signed_cases(long),		\
662 			 __scalar_type_to_signed_cases(long long),	\
663 			 default: (x)))
664 
665 /* Is this type a native word size -- useful for atomic operations */
666 #define __native_word(t) \
667 	(sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
668 	 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
669 
670 #ifdef __OPTIMIZE__
671 /*
672  * #ifdef __OPTIMIZE__ is only a good approximation; for instance "make
673  * CFLAGS_foo.o=-Og" defines __OPTIMIZE__, does not elide the conditional code
674  * and can break compilation with wrong error message(s). Combine with
675  * -U__OPTIMIZE__ when needed.
676  */
677 # define __compiletime_assert(condition, msg, prefix, suffix)		\
678 	do {								\
679 		/*							\
680 		 * __noreturn is needed to give the compiler enough	\
681 		 * information to avoid certain possibly-uninitialized	\
682 		 * warnings (regardless of the build failing).		\
683 		 */							\
684 		__noreturn extern void prefix ## suffix(void)		\
685 			__compiletime_error(msg);			\
686 		if (!(condition))					\
687 			prefix ## suffix();				\
688 	} while (0)
689 #else
690 # define __compiletime_assert(condition, msg, prefix, suffix) ((void)(condition))
691 #endif
692 
693 #define _compiletime_assert(condition, msg, prefix, suffix) \
694 	__compiletime_assert(condition, msg, prefix, suffix)
695 
696 /**
697  * compiletime_assert - break build and emit msg if condition is false
698  * @condition: a compile-time constant condition to check
699  * @msg:       a message to emit if condition is false
700  *
701  * In tradition of POSIX assert, this macro will break the build if the
702  * supplied condition is *false*, emitting the supplied error message if the
703  * compiler has support to do so.
704  */
705 #define compiletime_assert(condition, msg) \
706 	_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
707 
708 #define compiletime_assert_atomic_type(t)				\
709 	compiletime_assert(__native_word(t),				\
710 		"Need native word sized stores/loads for atomicity.")
711 
712 /* Helpers for emitting diagnostics in pragmas. */
713 #ifndef __diag
714 #define __diag(string)
715 #endif
716 
717 #ifndef __diag_GCC
718 #define __diag_GCC(version, severity, string)
719 #endif
720 
721 #define __diag_push()	__diag(push)
722 #define __diag_pop()	__diag(pop)
723 
724 #define __diag_ignore(compiler, version, option, comment) \
725 	__diag_ ## compiler(version, ignore, option)
726 #define __diag_warn(compiler, version, option, comment) \
727 	__diag_ ## compiler(version, warn, option)
728 #define __diag_error(compiler, version, option, comment) \
729 	__diag_ ## compiler(version, error, option)
730 
731 #ifndef __diag_ignore_all
732 #define __diag_ignore_all(option, comment)
733 #endif
734 
735 #endif /* __LINUX_COMPILER_TYPES_H */
736