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/*
119 * Turn individual warnings and errors on and off locally, depending
120 * on version.
121 */
122#define __diag_clang(version, severity, s) \
123 __diag_clang_ ## version(__diag_clang_ ## severity s)
124
125/* Severity used in pragma directives */
126#define __diag_clang_ignore ignored
127#define __diag_clang_warn warning
128#define __diag_clang_error error
129
130#define __diag_str1(s) #s
131#define __diag_str(s) __diag_str1(s)
132#define __diag(s) _Pragma(__diag_str(clang diagnostic s))
133
134#define __diag_clang_13(s) __diag(s)
135
136#define __diag_ignore_all(option, comment) \
137 __diag_clang(13, ignore, option)
138
139/*
140 * clang has horrible behavior with "g" or "rm" constraints for asm
141 * inputs, turning them into something worse than "m". Avoid using
142 * constraints with multiple possible uses (but "ir" seems to be ok):
143 *
144 * https://github.com/llvm/llvm-project/issues/20571
145 */
146#define ASM_INPUT_G "ir"
147#define ASM_INPUT_RM "r"
148
149/*
150 * Declare compiler support for __typeof_unqual__() operator.
151 *
152 * Bindgen uses LLVM even if our C compiler is GCC, so we cannot
153 * rely on the auto-detected CONFIG_CC_HAS_TYPEOF_UNQUAL.
154 */
155#define CC_HAS_TYPEOF_UNQUAL (__clang_major__ >= 19)
156