Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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)))
54static inline void __chk_user_ptr(const volatile void __user *ptr) { }
55static 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
190struct 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
207struct 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/* Section for code which can't be instrumented at all */
436#define __noinstr_section(section) \
437 noinline notrace __attribute((__section__(section))) \
438 __no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage \
439 __no_sanitize_memory
440
441#define noinstr __noinstr_section(".noinstr.text")
442
443/*
444 * The __cpuidle section is used twofold:
445 *
446 * 1) the original use -- identifying if a CPU is 'stuck' in idle state based
447 * on it's instruction pointer. See cpu_in_idle().
448 *
449 * 2) supressing instrumentation around where cpuidle disables RCU; where the
450 * function isn't strictly required for #1, this is interchangeable with
451 * noinstr.
452 */
453#define __cpuidle __noinstr_section(".cpuidle.text")
454
455#endif /* __KERNEL__ */
456
457#endif /* __ASSEMBLY__ */
458
459/*
460 * The below symbols may be defined for one or more, but not ALL, of the above
461 * compilers. We don't consider that to be an error, so set them to nothing.
462 * For example, some of them are for compiler specific plugins.
463 */
464#ifndef __latent_entropy
465# define __latent_entropy
466#endif
467
468#if defined(RANDSTRUCT) && !defined(__CHECKER__)
469# define __randomize_layout __designated_init __attribute__((randomize_layout))
470# define __no_randomize_layout __attribute__((no_randomize_layout))
471/* This anon struct can add padding, so only enable it under randstruct. */
472# define randomized_struct_fields_start struct {
473# define randomized_struct_fields_end } __randomize_layout;
474#else
475# define __randomize_layout __designated_init
476# define __no_randomize_layout
477# define randomized_struct_fields_start
478# define randomized_struct_fields_end
479#endif
480
481#ifndef __no_kstack_erase
482# define __no_kstack_erase
483#endif
484
485#ifndef __noscs
486# define __noscs
487#endif
488
489#if defined(CONFIG_CFI)
490# define __nocfi __attribute__((__no_sanitize__("kcfi")))
491#else
492# define __nocfi
493#endif
494
495#if defined(CONFIG_ARCH_USES_CFI_GENERIC_LLVM_PASS)
496# define __nocfi_generic __nocfi
497#else
498# define __nocfi_generic
499#endif
500
501/*
502 * Any place that could be marked with the "alloc_size" attribute is also
503 * a place to be marked with the "malloc" attribute, except those that may
504 * be performing a _reallocation_, as that may alias the existing pointer.
505 * For these, use __realloc_size().
506 */
507#ifdef __alloc_size__
508# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc
509# define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__)
510#else
511# define __alloc_size(x, ...) __malloc
512# define __realloc_size(x, ...)
513#endif
514
515/*
516 * When the size of an allocated object is needed, use the best available
517 * mechanism to find it. (For cases where sizeof() cannot be used.)
518 *
519 * Optional: only supported since gcc >= 12
520 *
521 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
522 * clang: https://clang.llvm.org/docs/LanguageExtensions.html#evaluating-object-size
523 */
524#if __has_builtin(__builtin_dynamic_object_size)
525#define __struct_size(p) __builtin_dynamic_object_size(p, 0)
526#define __member_size(p) __builtin_dynamic_object_size(p, 1)
527#else
528#define __struct_size(p) __builtin_object_size(p, 0)
529#define __member_size(p) __builtin_object_size(p, 1)
530#endif
531
532/*
533 * Determine if an attribute has been applied to a variable.
534 * Using __annotated needs to check for __annotated being available,
535 * or negative tests may fail when annotation cannot be checked. For
536 * example, see the definition of __is_cstr().
537 */
538#if __has_builtin(__builtin_has_attribute)
539#define __annotated(var, attr) __builtin_has_attribute(var, attr)
540#endif
541
542/*
543 * Optional: only supported since gcc >= 15, clang >= 19
544 *
545 * gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fcounted_005fby_005fref
546 * clang: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-counted-by-ref
547 */
548#if __has_builtin(__builtin_counted_by_ref) && \
549 !defined(CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF)
550/**
551 * __flex_counter() - Get pointer to counter member for the given
552 * flexible array, if it was annotated with __counted_by()
553 * @FAM: Pointer to flexible array member of an addressable struct instance
554 *
555 * For example, with:
556 *
557 * struct foo {
558 * int counter;
559 * short array[] __counted_by(counter);
560 * } *p;
561 *
562 * __flex_counter(p->array) will resolve to &p->counter.
563 *
564 * Note that Clang may not allow this to be assigned to a separate
565 * variable; it must be used directly.
566 *
567 * If p->array is unannotated, this returns (void *)NULL.
568 */
569#define __flex_counter(FAM) __builtin_counted_by_ref(FAM)
570#else
571#define __flex_counter(FAM) ((void *)NULL)
572#endif
573
574/*
575 * Some versions of gcc do not mark 'asm goto' volatile:
576 *
577 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979
578 *
579 * We do it here by hand, because it doesn't hurt.
580 */
581#ifndef asm_goto_output
582#define asm_goto_output(x...) asm volatile goto(x)
583#endif
584
585/*
586 * Clang has trouble with constraints with multiple
587 * alternative behaviors ("g" , "rm" and "=rm").
588 */
589#ifndef ASM_INPUT_G
590 #define ASM_INPUT_G "g"
591 #define ASM_INPUT_RM "rm"
592 #define ASM_OUTPUT_RM "=rm"
593#endif
594
595#ifdef CONFIG_CC_HAS_ASM_INLINE
596#define asm_inline asm __inline
597#else
598#define asm_inline asm
599#endif
600
601#ifndef __ASSEMBLY__
602/*
603 * Use __typeof_unqual__() when available.
604 */
605#if CC_HAS_TYPEOF_UNQUAL || defined(__CHECKER__)
606# define USE_TYPEOF_UNQUAL 1
607#endif
608
609/* Are two types/vars the same type (ignoring qualifiers)? */
610#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
611
612/*
613 * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
614 * non-scalar types unchanged.
615 */
616#ifndef USE_TYPEOF_UNQUAL
617/*
618 * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
619 * is not type-compatible with 'signed char', and we define a separate case.
620 */
621#define __scalar_type_to_expr_cases(type) \
622 unsigned type: (unsigned type)0, \
623 signed type: (signed type)0
624
625#define __unqual_scalar_typeof(x) typeof( \
626 _Generic((x), \
627 char: (char)0, \
628 __scalar_type_to_expr_cases(char), \
629 __scalar_type_to_expr_cases(short), \
630 __scalar_type_to_expr_cases(int), \
631 __scalar_type_to_expr_cases(long), \
632 __scalar_type_to_expr_cases(long long), \
633 default: (x)))
634#else
635#define __unqual_scalar_typeof(x) __typeof_unqual__(x)
636#endif
637#endif /* !__ASSEMBLY__ */
638
639/*
640 * __signed_scalar_typeof(x) - Declare a signed scalar type, leaving
641 * non-scalar types unchanged.
642 */
643
644#define __scalar_type_to_signed_cases(type) \
645 unsigned type: (signed type)0, \
646 signed type: (signed type)0
647
648#define __signed_scalar_typeof(x) typeof( \
649 _Generic((x), \
650 char: (signed char)0, \
651 __scalar_type_to_signed_cases(char), \
652 __scalar_type_to_signed_cases(short), \
653 __scalar_type_to_signed_cases(int), \
654 __scalar_type_to_signed_cases(long), \
655 __scalar_type_to_signed_cases(long long), \
656 default: (x)))
657
658/* Is this type a native word size -- useful for atomic operations */
659#define __native_word(t) \
660 (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
661 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
662
663#ifdef __OPTIMIZE__
664/*
665 * #ifdef __OPTIMIZE__ is only a good approximation; for instance "make
666 * CFLAGS_foo.o=-Og" defines __OPTIMIZE__, does not elide the conditional code
667 * and can break compilation with wrong error message(s). Combine with
668 * -U__OPTIMIZE__ when needed.
669 */
670# define __compiletime_assert(condition, msg, prefix, suffix) \
671 do { \
672 /* \
673 * __noreturn is needed to give the compiler enough \
674 * information to avoid certain possibly-uninitialized \
675 * warnings (regardless of the build failing). \
676 */ \
677 __noreturn extern void prefix ## suffix(void) \
678 __compiletime_error(msg); \
679 if (!(condition)) \
680 prefix ## suffix(); \
681 } while (0)
682#else
683# define __compiletime_assert(condition, msg, prefix, suffix) ((void)(condition))
684#endif
685
686#define _compiletime_assert(condition, msg, prefix, suffix) \
687 __compiletime_assert(condition, msg, prefix, suffix)
688
689/**
690 * compiletime_assert - break build and emit msg if condition is false
691 * @condition: a compile-time constant condition to check
692 * @msg: a message to emit if condition is false
693 *
694 * In tradition of POSIX assert, this macro will break the build if the
695 * supplied condition is *false*, emitting the supplied error message if the
696 * compiler has support to do so.
697 */
698#define compiletime_assert(condition, msg) \
699 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
700
701#define compiletime_assert_atomic_type(t) \
702 compiletime_assert(__native_word(t), \
703 "Need native word sized stores/loads for atomicity.")
704
705/* Helpers for emitting diagnostics in pragmas. */
706#ifndef __diag
707#define __diag(string)
708#endif
709
710#ifndef __diag_GCC
711#define __diag_GCC(version, severity, string)
712#endif
713
714#define __diag_push() __diag(push)
715#define __diag_pop() __diag(pop)
716
717#define __diag_ignore(compiler, version, option, comment) \
718 __diag_ ## compiler(version, ignore, option)
719#define __diag_warn(compiler, version, option, comment) \
720 __diag_ ## compiler(version, warn, option)
721#define __diag_error(compiler, version, option, comment) \
722 __diag_ ## compiler(version, error, option)
723
724#ifndef __diag_ignore_all
725#define __diag_ignore_all(option, comment)
726#endif
727
728#endif /* __LINUX_COMPILER_TYPES_H */