xref: /linux/include/linux/compiler-clang.h (revision 4f553c1e2c7b81e957b5463bd7efad2465a586f8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_COMPILER_TYPES_H
3 #error "Please do not include <linux/compiler-clang.h> directly, include <linux/compiler.h> instead."
4 #endif
5 
6 /* Compiler specific definitions for Clang compiler */
7 
8 /*
9  * Clang prior to 17 is being silly and considers many __cleanup() variables
10  * as unused (because they are, their sole purpose is to go out of scope).
11  *
12  * https://github.com/llvm/llvm-project/commit/877210faa447f4cc7db87812f8ed80e398fedd61
13  */
14 #undef __cleanup
15 #define __cleanup(func) __maybe_unused __attribute__((__cleanup__(func)))
16 
17 /* all clang versions usable with the kernel support KASAN ABI version 5 */
18 #define KASAN_ABI_VERSION 5
19 
20 /*
21  * Clang 22 added preprocessor macros to match GCC, in hopes of eventually
22  * dropping __has_feature support for sanitizers:
23  * https://github.com/llvm/llvm-project/commit/568c23bbd3303518c5056d7f03444dae4fdc8a9c
24  * Create these macros for older versions of clang so that it is easy to clean
25  * up once the minimum supported version of LLVM for building the kernel always
26  * creates these macros.
27  *
28  * Note: Checking __has_feature(*_sanitizer) is only true if the feature is
29  * enabled. Therefore it is not required to additionally check defined(CONFIG_*)
30  * to avoid adding redundant attributes in other configurations.
31  */
32 #if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
33 #define __SANITIZE_ADDRESS__
34 #endif
35 #if __has_feature(hwaddress_sanitizer) && !defined(__SANITIZE_HWADDRESS__)
36 #define __SANITIZE_HWADDRESS__
37 #endif
38 #if __has_feature(thread_sanitizer) && !defined(__SANITIZE_THREAD__)
39 #define __SANITIZE_THREAD__
40 #endif
41 
42 /*
43  * Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel.
44  */
45 #ifdef __SANITIZE_HWADDRESS__
46 #define __SANITIZE_ADDRESS__
47 #endif
48 
49 #ifdef __SANITIZE_ADDRESS__
50 #define __no_sanitize_address \
51 		__attribute__((no_sanitize("address", "hwaddress")))
52 #else
53 #define __no_sanitize_address
54 #endif
55 
56 #ifdef __SANITIZE_THREAD__
57 #define __no_sanitize_thread \
58 		__attribute__((no_sanitize("thread")))
59 #else
60 #define __no_sanitize_thread
61 #endif
62 
63 #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
64 #define __HAVE_BUILTIN_BSWAP32__
65 #define __HAVE_BUILTIN_BSWAP64__
66 #define __HAVE_BUILTIN_BSWAP16__
67 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
68 
69 #if __has_feature(undefined_behavior_sanitizer)
70 /* GCC does not have __SANITIZE_UNDEFINED__ */
71 #define __no_sanitize_undefined \
72 		__attribute__((no_sanitize("undefined")))
73 #else
74 #define __no_sanitize_undefined
75 #endif
76 
77 #if __has_feature(memory_sanitizer)
78 #define __SANITIZE_MEMORY__
79 /*
80  * Unlike other sanitizers, KMSAN still inserts code into functions marked with
81  * no_sanitize("kernel-memory"). Using disable_sanitizer_instrumentation
82  * provides the behavior consistent with other __no_sanitize_ attributes,
83  * guaranteeing that __no_sanitize_memory functions remain uninstrumented.
84  */
85 #define __no_sanitize_memory __disable_sanitizer_instrumentation
86 
87 /*
88  * The __no_kmsan_checks attribute ensures that a function does not produce
89  * false positive reports by:
90  *  - initializing all local variables and memory stores in this function;
91  *  - skipping all shadow checks;
92  *  - passing initialized arguments to this function's callees.
93  */
94 #define __no_kmsan_checks __attribute__((no_sanitize("kernel-memory")))
95 #else
96 #define __no_sanitize_memory
97 #define __no_kmsan_checks
98 #endif
99 
100 /*
101  * Support for __has_feature(coverage_sanitizer) was added in Clang 13 together
102  * with no_sanitize("coverage"). Prior versions of Clang support coverage
103  * instrumentation, but cannot be queried for support by the preprocessor.
104  */
105 #if __has_feature(coverage_sanitizer)
106 #define __no_sanitize_coverage __attribute__((no_sanitize("coverage")))
107 #else
108 #define __no_sanitize_coverage
109 #endif
110 
111 /* Only Clang needs to disable the coverage sanitizer for kstack_erase. */
112 #define __no_kstack_erase	__no_sanitize_coverage
113 
114 #if __has_feature(shadow_call_stack)
115 # define __noscs	__attribute__((__no_sanitize__("shadow-call-stack")))
116 #endif
117 
118 #if __has_feature(kcfi)
119 /* Disable CFI checking inside a function. */
120 #define __nocfi		__attribute__((__no_sanitize__("kcfi")))
121 #endif
122 
123 /*
124  * Turn individual warnings and errors on and off locally, depending
125  * on version.
126  */
127 #define __diag_clang(version, severity, s) \
128 	__diag_clang_ ## version(__diag_clang_ ## severity s)
129 
130 /* Severity used in pragma directives */
131 #define __diag_clang_ignore	ignored
132 #define __diag_clang_warn	warning
133 #define __diag_clang_error	error
134 
135 #define __diag_str1(s)		#s
136 #define __diag_str(s)		__diag_str1(s)
137 #define __diag(s)		_Pragma(__diag_str(clang diagnostic s))
138 
139 #define __diag_clang_13(s)	__diag(s)
140 
141 #define __diag_ignore_all(option, comment) \
142 	__diag_clang(13, ignore, option)
143 
144 /*
145  * clang has horrible behavior with "g" or "rm" constraints for asm
146  * inputs, turning them into something worse than "m". Avoid using
147  * constraints with multiple possible uses (but "ir" seems to be ok):
148  *
149  *	https://github.com/llvm/llvm-project/issues/20571
150  */
151 #define ASM_INPUT_G "ir"
152 #define ASM_INPUT_RM "r"
153 
154 /*
155  * Declare compiler support for __typeof_unqual__() operator.
156  *
157  * Bindgen uses LLVM even if our C compiler is GCC, so we cannot
158  * rely on the auto-detected CONFIG_CC_HAS_TYPEOF_UNQUAL.
159  */
160 #define CC_HAS_TYPEOF_UNQUAL (__clang_major__ >= 19)
161