History

Merge tag 'compiler-attributes-for-linus-4.20-rc1' of https://github.com/ojeda/linux

Pull compiler attribute updates from Miguel Ojeda: "This is an effort to disentangle the include/linux/compiler*.h headers and bring them up to date. The main idea behind the series is to use feature checking macros (i.e. __has_attribute) instead of compiler version checks (e.g. GCC_VERSION), which are compiler-agnostic (so they can be shared, reducing the size of compiler-specific headers) and version-agnostic. Other related improvements have been performed in the headers as well, which on top of the use of __has_attribute it has amounted to a significant simplification of these headers (e.g. GCC_VERSION is now only guarding a few non-attribute macros). This series should also help the efforts to support compiling the kernel with clang and icc. A fair amount of documentation and comments have also been added, clarified or removed; and the headers are now more readable, which should help kernel developers in general. The series was triggered due to the move to gcc >= 4.6. In turn, this series has also triggered Sparse to gain the ability to recognize __has_attribute on its own. Finally, the __nonstring variable attribute series has been also applied on top; plus two related patches from Nick Desaulniers for unreachable() that came a bit afterwards" * tag 'compiler-attributes-for-linus-4.20-rc1' of https://github.com/ojeda/linux: compiler-gcc: remove comment about gcc 4.5 from unreachable() compiler.h: update definition of unreachable() Compiler Attributes: ext4: remove local __nonstring definition Compiler Attributes: auxdisplay: panel: use __nonstring Compiler Attributes: enable -Wstringop-truncation on W=1 (gcc >= 8) Compiler Attributes: add support for __nonstring (gcc >= 8) Compiler Attributes: add MAINTAINERS entry Compiler Attributes: add Doc/process/programming-language.rst Compiler Attributes: remove uses of __attribute__ from compiler.h Compiler Attributes: KENTRY used twice the "used" attribute Compiler Attributes: use feature checks instead of version checks Compiler Attributes: add missing SPDX ID in compiler_types.h Compiler Attributes: remove unneeded sparse (__CHECKER__) tests Compiler Attributes: homogenize __must_be_array Compiler Attributes: remove unneeded tests Compiler Attributes: always use the extra-underscores syntax Compiler Attributes: remove unused attributes

Linus Torvalds committed 8 years ago

Commit e468f5c2 parents baa888d 1ff2fea12 files · +348 −191Browse files

Unified · Split

12 files
  1. Documentation/process/index.rst +1 −0
  2. Documentation/process/programming-language.rst +45 −0
  3. MAINTAINERS +5 −0
  4. drivers/auxdisplay/panel.c +3 −4
  5. fs/ext4/ext4.h +0 −9
  6. include/linux/compiler-clang.h +0 −5
  7. include/linux/compiler-gcc.h +7 −68
  8. include/linux/compiler-intel.h +0 −9
  9. include/linux/compiler.h +13 −11
  10. include/linux/compiler_attributes.h +258 −0
  11. include/linux/compiler_types.h +15 −85
  12. scripts/Makefile.extrawarn +1 −0
Documentation/process/index.rst+1−0
@@ -25,6 +25,7 @@ Below are the essential guides that every developer should read. · 24 unmodified lines
2525 code-of-conduct-interpretation
2626 development-process
2727 submitting-patches
28+ programming-language
2829 coding-style
2930 maintainer-pgp-guide
3031 email-clients
Documentation/process/programming-language.rst+45−0
@@ -0,0 +1,45 @@
1+.. _programming_language:
2+
3+Programming Language
4+====================
5+
6+The kernel is written in the C programming language [c-language]_.
7+More precisely, the kernel is typically compiled with ``gcc`` [gcc]_
8+under ``-std=gnu89`` [gcc-c-dialect-options]_: the GNU dialect of ISO C90
9+(including some C99 features).
10+
11+This dialect contains many extensions to the language [gnu-extensions]_,
12+and many of them are used within the kernel as a matter of course.
13+
14+There is some support for compiling the kernel with ``clang`` [clang]_
15+and ``icc`` [icc]_ for several of the architectures, although at the time
16+of writing it is not completed, requiring third-party patches.
17+
18+Attributes
19+----------
20+
21+One of the common extensions used throughout the kernel are attributes
22+[gcc-attribute-syntax]_. Attributes allow to introduce
23+implementation-defined semantics to language entities (like variables,
24+functions or types) without having to make significant syntactic changes
25+to the language (e.g. adding a new keyword) [n2049]_.
26+
27+In some cases, attributes are optional (i.e. a compiler not supporting them
28+should still produce proper code, even if it is slower or does not perform
29+as many compile-time checks/diagnostics).
30+
31+The kernel defines pseudo-keywords (e.g. ``__pure``) instead of using
32+directly the GNU attribute syntax (e.g. ``__attribute__((__pure__))``)
33+in order to feature detect which ones can be used and/or to shorten the code.
34+
35+Please refer to ``include/linux/compiler_attributes.h`` for more information.
36+
37+.. [c-language] http://www.open-std.org/jtc1/sc22/wg14/www/standards
38+.. [gcc] https://gcc.gnu.org
39+.. [clang] https://clang.llvm.org
40+.. [icc] https://software.intel.com/en-us/c-compilers
41+.. [gcc-c-dialect-options] https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html
42+.. [gnu-extensions] https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html
43+.. [gcc-attribute-syntax] https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
44+.. [n2049] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2049.pdf
45+
MAINTAINERS+5−0
@@ -3737,6 +3737,11 @@ L: platform-driver-x86@vger.kernel.org · 3736 unmodified lines
37373737 S: Maintained
37383738 F: drivers/platform/x86/compal-laptop.c
37393739
3740+COMPILER ATTRIBUTES
3741+M: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
3742+S: Maintained
3743+F: include/linux/compiler_attributes.h
3744+
37403745 CONEXANT ACCESSRUNNER USB DRIVER
37413746 L: accessrunner-general@lists.sourceforge.net
37423747 W: http://accessrunner.sourceforge.net/
drivers/auxdisplay/panel.c+3−4
@@ -155,10 +155,9 @@ struct logical_input { · 154 unmodified lines
155155 int release_data;
156156 } std;
157157 struct { /* valid when type == INPUT_TYPE_KBD */
158− /* strings can be non null-terminated */
159− char press_str[sizeof(void *) + sizeof(int)];
160− char repeat_str[sizeof(void *) + sizeof(int)];
161− char release_str[sizeof(void *) + sizeof(int)];
158+ char press_str[sizeof(void *) + sizeof(int)] __nonstring;
159+ char repeat_str[sizeof(void *) + sizeof(int)] __nonstring;
160+ char release_str[sizeof(void *) + sizeof(int)] __nonstring;
162161 } kbd;
163162 } u;
164163 };
fs/ext4/ext4.h+0−9
@@ -45,15 +45,6 @@ · 44 unmodified lines
4545
4646 #include <linux/compiler.h>
4747
48−/* Until this gets included into linux/compiler-gcc.h */
49−#ifndef __nonstring
50−#if defined(GCC_VERSION) && (GCC_VERSION >= 80000)
51−#define __nonstring __attribute__((nonstring))
52−#else
53−#define __nonstring
54−#endif
55−#endif
56−
5748 /*
5849 * The fourth extended filesystem constants/structures
5950 */
include/linux/compiler-clang.h+0−5
@@ -21,8 +21,6 @@ · 20 unmodified lines
2121 #define __SANITIZE_ADDRESS__
2222 #endif
2323
24−#define __no_sanitize_address __attribute__((no_sanitize("address")))
25−
2624 /*
2725 * Not all versions of clang implement the the type-generic versions
2826 * of the builtin overflow checkers. Fortunately, clang implements
@@ -41,6 +39,3 @@ · 12 unmodified lines
4139 * compilers, like ICC.
4240 */
4341 #define barrier() __asm__ __volatile__("" : : : "memory")
44−#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
45−#define __assume_aligned(a, ...) \
46− __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
include/linux/compiler-gcc.h+7−68
@@ -68,31 +68,20 @@ · 67 unmodified lines
6868 */
6969 #define uninitialized_var(x) x = x
7070
71−#ifdef __CHECKER__
72−#define __must_be_array(a) 0
73−#else
74−/* &a[0] degrades to a pointer: a different type from an array */
75−#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
76−#endif
77−
7871 #ifdef RETPOLINE
79−#define __noretpoline __attribute__((indirect_branch("keep")))
72+#define __noretpoline __attribute__((__indirect_branch__("keep")))
8073 #endif
8174
8275 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
8376
84−#define __optimize(level) __attribute__((__optimize__(level)))
85−
8677 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
8778
88−#ifndef __CHECKER__
89−#define __compiletime_warning(message) __attribute__((warning(message)))
90−#define __compiletime_error(message) __attribute__((error(message)))
79+#define __compiletime_warning(message) __attribute__((__warning__(message)))
80+#define __compiletime_error(message) __attribute__((__error__(message)))
9181
92−#ifdef LATENT_ENTROPY_PLUGIN
82+#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
9383 #define __latent_entropy __attribute__((latent_entropy))
9484 #endif
95−#endif /* __CHECKER__ */
9685
9786 /*
9887 * calling noreturn functions, __builtin_unreachable() and __builtin_trap()
@@ -107,10 +96,6 @@ · 8 unmodified lines
10796 * Mark a position in code as unreachable. This can be used to
10897 * suppress control flow warnings after asm blocks that transfer
10998 * control elsewhere.
110− *
111− * Early snapshots of gcc 4.5 don't support this and we can't detect
112− * this in the preprocessor, but we can live with this because they're
113− * unreleased. Really, we need to have autoconf for the kernel.
11499 */
115100 #define unreachable() \
116101 do { \
@@ -119,9 +104,6 @@ · 2 unmodified lines
119104 __builtin_unreachable(); \
120105 } while (0)
121106
122−/* Mark a function definition as prohibited from being cloned. */
123−#define __noclone __attribute__((__noclone__, __optimize__("no-tracer")))
124−
125107 #if defined(RANDSTRUCT_PLUGIN) && !defined(__CHECKER__)
126108 #define __randomize_layout __attribute__((randomize_layout))
127109 #define __no_randomize_layout __attribute__((no_randomize_layout))
@@ -130,32 +112,6 @@ · 2 unmodified lines
130112 #define randomized_struct_fields_end } __randomize_layout;
131113 #endif
132114
133−/*
134− * When used with Link Time Optimization, gcc can optimize away C functions or
135− * variables which are referenced only from assembly code. __visible tells the
136− * optimizer that something else uses this function or variable, thus preventing
137− * this.
138− */
139−#define __visible __attribute__((externally_visible))
140−
141−/* gcc version specific checks */
142−
143−#if GCC_VERSION >= 40900 && !defined(__CHECKER__)
144−/*
145− * __assume_aligned(n, k): Tell the optimizer that the returned
146− * pointer can be assumed to be k modulo n. The second argument is
147− * optional (default 0), so we use a variadic macro to make the
148− * shorthand.
149− *
150− * Beware: Do not apply this to functions which may return
151− * ERR_PTRs. Also, it is probably unwise to apply it to functions
152− * returning extra information in the low bits (but in that case the
153− * compiler should see some alignment anyway, when the return value is
154− * massaged by 'flags = ptr & 3; ptr &= ~3;').
155− */
156−#define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
157−#endif
158−
159115 /*
160116 * GCC 'asm goto' miscompiles certain code sequences:
161117 *
@@ -187,39 +143,22 @@ · 25 unmodified lines
187143 #define KASAN_ABI_VERSION 3
188144 #endif
189145
190−#if GCC_VERSION >= 40902
191146 /*
192− * Tell the compiler that address safety instrumentation (KASAN)
193− * should not be applied to that function.
194− * Conflicts with inlining: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
147+ * Because __no_sanitize_address conflicts with inlining:
148+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
149+ * we do one or the other.
195150 */
196−#define __no_sanitize_address __attribute__((no_sanitize_address))
197151 #ifdef CONFIG_KASAN
198152 #define __no_sanitize_address_or_inline \
199153 __no_sanitize_address __maybe_unused notrace
200154 #else
201155 #define __no_sanitize_address_or_inline inline
202156 #endif
203−#endif
204157
205158 #if GCC_VERSION >= 50100
206−/*
207− * Mark structures as requiring designated initializers.
208− * https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
209− */
210−#define __designated_init __attribute__((designated_init))
211159 #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
212160 #endif
213161
214−#if !defined(__noclone)
215−#define __noclone /* not needed */
216−#endif
217−
218−#if !defined(__no_sanitize_address)
219−#define __no_sanitize_address
220−#define __no_sanitize_address_or_inline inline
221−#endif
222−
223162 /*
224163 * Turn individual warnings and errors on and off locally, depending
225164 * on version.
include/linux/compiler-intel.h+0−9
@@ -29,17 +29,8 @@ · 28 unmodified lines
2929 */
3030 #define OPTIMIZER_HIDE_VAR(var) barrier()
3131
32−/* Intel ECC compiler doesn't support __builtin_types_compatible_p() */
33−#define __must_be_array(a) 0
34−
3532 #endif
3633
3734 /* icc has this, but it's called _bswap16 */
3835 #define __HAVE_BUILTIN_BSWAP16__
3936 #define __builtin_bswap16 _bswap16
40−
41−/* The following are for compatibility with GCC, from compiler-gcc.h,
42− * and may be redefined here because they should not be shared with other
43− * compilers, like clang.
44− */
45−#define __visible __attribute__((externally_visible))
include/linux/compiler.h+13−11
@@ -23,8 +23,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 22 unmodified lines
2323 #define __branch_check__(x, expect, is_constant) ({ \
2424 long ______r; \
2525 static struct ftrace_likely_data \
26− __attribute__((__aligned__(4))) \
27− __attribute__((section("_ftrace_annotated_branch"))) \
26+ __aligned(4) \
27+ __section("_ftrace_annotated_branch") \
2828 ______f = { \
2929 .data.func = __func__, \
3030 .data.file = __FILE__, \
@@ -59,8 +59,8 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 28 unmodified lines
5959 ({ \
6060 int ______r; \
6161 static struct ftrace_branch_data \
62− __attribute__((__aligned__(4))) \
63− __attribute__((section("_ftrace_branch"))) \
62+ __aligned(4) \
63+ __section("_ftrace_branch") \
6464 ______f = { \
6565 .func = __func__, \
6666 .file = __FILE__, \
@@ -115,7 +115,10 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 48 unmodified lines
115115 # define ASM_UNREACHABLE
116116 #endif
117117 #ifndef unreachable
118−# define unreachable() do { annotate_reachable(); do { } while (1); } while (0)
118+# define unreachable() do { \
119+ annotate_unreachable(); \
120+ __builtin_unreachable(); \
121+} while (0)
119122 #endif
120123
121124 /*
@@ -137,7 +140,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, · 15 unmodified lines
137140 extern typeof(sym) sym; \
138141 static const unsigned long __kentry_##sym \
139142 __used \
140− __attribute__((section("___kentry" "+" #sym ), used)) \
143+ __section("___kentry" "+" #sym ) \
141144 = (unsigned long)&sym;
142145 #endif
143146
@@ -278,7 +281,7 @@ unsigned long read_word_at_a_time(const void *addr) · 134 unmodified lines
278281 * visible to the compiler.
279282 */
280283 #define __ADDRESSABLE(sym) \
281− static void * __attribute__((section(".discard.addressable"), used)) \
284+ static void * __section(".discard.addressable") __used \
282285 __PASTE(__addressable_##sym, __LINE__) = (void *)&sym;
283286
284287 /**
@@ -331,10 +334,6 @@ static inline void *offset_to_ptr(const int *off) · 46 unmodified lines
331334 #endif /* __KERNEL__ */
332335 #endif /* __ASSEMBLY__ */
333336
334−#ifndef __optimize
335−# define __optimize(level)
336−#endif
337−
338337 /* Compile time object size, -1 for unknown */
339338 #ifndef __compiletime_object_size
340339 # define __compiletime_object_size(obj) -1
@@ -376,4 +375,7 @@ static inline void *offset_to_ptr(const int *off) · 35 unmodified lines
376375 compiletime_assert(__native_word(t), \
377376 "Need native word sized stores/loads for atomicity.")
378377
378+/* &a[0] degrades to a pointer: a different type from an array */
379+#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
380+
379381 #endif /* __LINUX_COMPILER_H */
include/linux/compiler_attributes.h+258−0
@@ -0,0 +1,258 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+#ifndef __LINUX_COMPILER_ATTRIBUTES_H
3+#define __LINUX_COMPILER_ATTRIBUTES_H
4+
5+/*
6+ * The attributes in this file are unconditionally defined and they directly
7+ * map to compiler attribute(s) -- except those that are optional.
8+ *
9+ * Any other "attributes" (i.e. those that depend on a configuration option,
10+ * on a compiler, on an architecture, on plugins, on other attributes...)
11+ * should be defined elsewhere (e.g. compiler_types.h or compiler-*.h).
12+ *
13+ * This file is meant to be sorted (by actual attribute name,
14+ * not by #define identifier). Use the __attribute__((__name__)) syntax
15+ * (i.e. with underscores) to avoid future collisions with other macros.
16+ * If an attribute is optional, state the reason in the comment.
17+ */
18+
19+/*
20+ * To check for optional attributes, we use __has_attribute, which is supported
21+ * on gcc >= 5, clang >= 2.9 and icc >= 17. In the meantime, to support
22+ * 4.6 <= gcc < 5, we implement __has_attribute by hand.
23+ *
24+ * sparse does not support __has_attribute (yet) and defines __GNUC_MINOR__
25+ * depending on the compiler used to build it; however, these attributes have
26+ * no semantic effects for sparse, so it does not matter. Also note that,
27+ * in order to avoid sparse's warnings, even the unsupported ones must be
28+ * defined to 0.
29+ */
30+#ifndef __has_attribute
31+# define __has_attribute(x) __GCC4_has_attribute_##x
32+# define __GCC4_has_attribute___assume_aligned__ (__GNUC_MINOR__ >= 9)
33+# define __GCC4_has_attribute___designated_init__ 0
34+# define __GCC4_has_attribute___externally_visible__ 1
35+# define __GCC4_has_attribute___noclone__ 1
36+# define __GCC4_has_attribute___optimize__ 1
37+# define __GCC4_has_attribute___nonstring__ 0
38+# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
39+#endif
40+
41+/*
42+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute
43+ */
44+#define __alias(symbol) __attribute__((__alias__(#symbol)))
45+
46+/*
47+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-aligned-function-attribute
48+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-aligned-type-attribute
49+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-aligned-variable-attribute
50+ */
51+#define __aligned(x) __attribute__((__aligned__(x)))
52+#define __aligned_largest __attribute__((__aligned__))
53+
54+/*
55+ * Note: users of __always_inline currently do not write "inline" themselves,
56+ * which seems to be required by gcc to apply the attribute according
57+ * to its docs (and also "warning: always_inline function might not be
58+ * inlinable [-Wattributes]" is emitted).
59+ *
60+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-always_005finline-function-attribute
61+ * clang: mentioned
62+ */
63+#define __always_inline inline __attribute__((__always_inline__))
64+
65+/*
66+ * The second argument is optional (default 0), so we use a variadic macro
67+ * to make the shorthand.
68+ *
69+ * Beware: Do not apply this to functions which may return
70+ * ERR_PTRs. Also, it is probably unwise to apply it to functions
71+ * returning extra information in the low bits (but in that case the
72+ * compiler should see some alignment anyway, when the return value is
73+ * massaged by 'flags = ptr & 3; ptr &= ~3;').
74+ *
75+ * Optional: only supported since gcc >= 4.9
76+ * Optional: not supported by icc
77+ *
78+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-assume_005faligned-function-attribute
79+ * clang: https://clang.llvm.org/docs/AttributeReference.html#assume-aligned
80+ */
81+#if __has_attribute(__assume_aligned__)
82+# define __assume_aligned(a, ...) __attribute__((__assume_aligned__(a, ## __VA_ARGS__)))
83+#else
84+# define __assume_aligned(a, ...)
85+#endif
86+
87+/*
88+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
89+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
90+ */
91+#define __cold __attribute__((__cold__))
92+
93+/*
94+ * Note the long name.
95+ *
96+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute
97+ */
98+#define __attribute_const__ __attribute__((__const__))
99+
100+/*
101+ * Don't. Just don't. See commit 771c035372a0 ("deprecate the '__deprecated'
102+ * attribute warnings entirely and for good") for more information.
103+ *
104+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-deprecated-function-attribute
105+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-deprecated-type-attribute
106+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-deprecated-variable-attribute
107+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html#index-deprecated-enumerator-attribute
108+ * clang: https://clang.llvm.org/docs/AttributeReference.html#deprecated
109+ */
110+#define __deprecated
111+
112+/*
113+ * Optional: only supported since gcc >= 5.1
114+ * Optional: not supported by clang
115+ * Optional: not supported by icc
116+ *
117+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-designated_005finit-type-attribute
118+ */
119+#if __has_attribute(__designated_init__)
120+# define __designated_init __attribute__((__designated_init__))
121+#else
122+# define __designated_init
123+#endif
124+
125+/*
126+ * Optional: not supported by clang
127+ *
128+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-externally_005fvisible-function-attribute
129+ */
130+#if __has_attribute(__externally_visible__)
131+# define __visible __attribute__((__externally_visible__))
132+#else
133+# define __visible
134+#endif
135+
136+/*
137+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-format-function-attribute
138+ * clang: https://clang.llvm.org/docs/AttributeReference.html#format
139+ */
140+#define __printf(a, b) __attribute__((__format__(printf, a, b)))
141+#define __scanf(a, b) __attribute__((__format__(scanf, a, b)))
142+
143+/*
144+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-gnu_005finline-function-attribute
145+ * clang: https://clang.llvm.org/docs/AttributeReference.html#gnu-inline
146+ */
147+#define __gnu_inline __attribute__((__gnu_inline__))
148+
149+/*
150+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
151+ */
152+#define __malloc __attribute__((__malloc__))
153+
154+/*
155+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute
156+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-mode-variable-attribute
157+ */
158+#define __mode(x) __attribute__((__mode__(x)))
159+
160+/*
161+ * Optional: not supported by clang
162+ * Note: icc does not recognize gcc's no-tracer
163+ *
164+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noclone-function-attribute
165+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-optimize-function-attribute
166+ */
167+#if __has_attribute(__noclone__)
168+# if __has_attribute(__optimize__)
169+# define __noclone __attribute__((__noclone__, __optimize__("no-tracer")))
170+# else
171+# define __noclone __attribute__((__noclone__))
172+# endif
173+#else
174+# define __noclone
175+#endif
176+
177+/*
178+ * Note the missing underscores.
179+ *
180+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noinline-function-attribute
181+ * clang: mentioned
182+ */
183+#define noinline __attribute__((__noinline__))
184+
185+/*
186+ * Optional: only supported since gcc >= 8
187+ * Optional: not supported by clang
188+ * Optional: not supported by icc
189+ *
190+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
191+ */
192+#if __has_attribute(__nonstring__)
193+# define __nonstring __attribute__((__nonstring__))
194+#else
195+# define __nonstring
196+#endif
197+
198+/*
199+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
200+ * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
201+ * clang: https://clang.llvm.org/docs/AttributeReference.html#id1
202+ */
203+#define __noreturn __attribute__((__noreturn__))
204+
205+/*
206+ * Optional: only supported since gcc >= 4.8
207+ * Optional: not supported by icc
208+ *
209+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-no_005fsanitize_005faddress-function-attribute
210+ * clang: https://clang.llvm.org/docs/AttributeReference.html#no-sanitize-address-no-address-safety-analysis
211+ */
212+#if __has_attribute(__no_sanitize_address__)
213+# define __no_sanitize_address __attribute__((__no_sanitize_address__))
214+#else
215+# define __no_sanitize_address
216+#endif
217+
218+/*
219+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-packed-type-attribute
220+ * clang: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-packed-variable-attribute
221+ */
222+#define __packed __attribute__((__packed__))
223+
224+/*
225+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute
226+ */
227+#define __pure __attribute__((__pure__))
228+
229+/*
230+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-section-function-attribute
231+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-section-variable-attribute
232+ * clang: https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate
233+ */
234+#define __section(S) __attribute__((__section__(#S)))
235+
236+/*
237+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
238+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
239+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute
240+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute
241+ * clang: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused
242+ */
243+#define __always_unused __attribute__((__unused__))
244+#define __maybe_unused __attribute__((__unused__))
245+
246+/*
247+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute
248+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute
249+ */
250+#define __used __attribute__((__used__))
251+
252+/*
253+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
254+ * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
255+ */
256+#define __weak __attribute__((__weak__))
257+
258+#endif /* __LINUX_COMPILER_ATTRIBUTES_H */
include/linux/compiler_types.h+15−85
@@ -1,3 +1,4 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
12 #ifndef __LINUX_COMPILER_TYPES_H
23 #define __LINUX_COMPILER_TYPES_H
34
@@ -54,6 +55,9 @@ extern void __chk_io_ptr(const volatile void __iomem *); · 50 unmodified lines
5455
5556 #ifdef __KERNEL__
5657
58+/* Attributes */
59+#include <linux/compiler_attributes.h>
60+
5761 /* Compiler specific macros. */
5862 #ifdef __clang__
5963 #include <linux/compiler-clang.h>
@@ -78,12 +82,6 @@ extern void __chk_io_ptr(const volatile void __iomem *); · 18 unmodified lines
7882 #include <asm/compiler.h>
7983 #endif
8084
81−/*
82− * Generic compiler-independent macros required for kernel
83− * build go below this comment. Actual compiler/compiler version
84− * specific implementations come from the above header files
85− */
86−
8785 struct ftrace_branch_data {
8886 const char *func;
8987 const char *file;
@@ -106,10 +104,6 @@ struct ftrace_likely_data { · 16 unmodified lines
106104 unsigned long constant;
107105 };
108106
109−/* Don't. Just don't. */
110−#define __deprecated
111−#define __deprecated_for_modules
112−
113107 #endif /* __KERNEL__ */
114108
115109 #endif /* __ASSEMBLY__ */
@@ -119,10 +113,6 @@ struct ftrace_likely_data { · 3 unmodified lines
119113 * compilers. We don't consider that to be an error, so set them to nothing.
120114 * For example, some of them are for compiler specific plugins.
121115 */
122−#ifndef __designated_init
123−# define __designated_init
124−#endif
125−
126116 #ifndef __latent_entropy
127117 # define __latent_entropy
128118 #endif
@@ -140,17 +130,6 @@ struct ftrace_likely_data { · 11 unmodified lines
140130 # define randomized_struct_fields_end
141131 #endif
142132
143−#ifndef __visible
144−#define __visible
145−#endif
146−
147−/*
148− * Assume alignment of return value.
149− */
150−#ifndef __assume_aligned
151−#define __assume_aligned(a, ...)
152−#endif
153−
154133 /* Are two types/vars the same type (ignoring qualifiers)? */
155134 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
156135
@@ -159,14 +138,6 @@ struct ftrace_likely_data { · 2 unmodified lines
159138 (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
160139 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
161140
162−#ifndef __attribute_const__
163−#define __attribute_const__ __attribute__((__const__))
164−#endif
165−
166−#ifndef __noclone
167−#define __noclone
168−#endif
169−
170141 /* Helpers for emitting diagnostics in pragmas. */
171142 #ifndef __diag
172143 #define __diag(string)
@@ -186,43 +157,16 @@ struct ftrace_likely_data { · 13 unmodified lines
186157 #define __diag_error(compiler, version, option, comment) \
187158 __diag_ ## compiler(version, error, option)
188159
189−/*
190− * From the GCC manual:
191− *
192− * Many functions have no effects except the return value and their
193− * return value depends only on the parameters and/or global
194− * variables. Such a function can be subject to common subexpression
195− * elimination and loop optimization just as an arithmetic operator
196− * would be.
197− * [...]
198− */
199−#define __pure __attribute__((pure))
200−#define __aligned(x) __attribute__((aligned(x)))
201−#define __printf(a, b) __attribute__((format(printf, a, b)))
202−#define __scanf(a, b) __attribute__((format(scanf, a, b)))
203−#define __maybe_unused __attribute__((unused))
204−#define __always_unused __attribute__((unused))
205−#define __mode(x) __attribute__((mode(x)))
206−#define __malloc __attribute__((__malloc__))
207−#define __used __attribute__((__used__))
208−#define __noreturn __attribute__((noreturn))
209−#define __packed __attribute__((packed))
210−#define __weak __attribute__((weak))
211−#define __alias(symbol) __attribute__((alias(#symbol)))
212−#define __cold __attribute__((cold))
213−#define __section(S) __attribute__((__section__(#S)))
214−
215−
216160 #ifdef CONFIG_ENABLE_MUST_CHECK
217−#define __must_check __attribute__((warn_unused_result))
161+#define __must_check __attribute__((__warn_unused_result__))
218162 #else
219163 #define __must_check
220164 #endif
221165
222−#if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
166+#if defined(CC_USING_HOTPATCH)
223167 #define notrace __attribute__((hotpatch(0, 0)))
224168 #else
225−#define notrace __attribute__((no_instrument_function))
169+#define notrace __attribute__((__no_instrument_function__))
226170 #endif
227171
228172 /*
@@ -231,22 +175,10 @@ struct ftrace_likely_data { · 2 unmodified lines
231175 * stack and frame pointer being set up and there is no chance to
232176 * restore the lr register to the value before mcount was called.
233177 */
234−#define __naked __attribute__((naked)) notrace
178+#define __naked __attribute__((__naked__)) notrace
235179
236180 #define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
237181
238−/*
239− * Feature detection for gnu_inline (gnu89 extern inline semantics). Either
240− * __GNUC_STDC_INLINE__ is defined (not using gnu89 extern inline semantics,
241− * and we opt in to the gnu89 semantics), or __GNUC_STDC_INLINE__ is not
242− * defined so the gnu89 semantics are the default.
243− */
244−#ifdef __GNUC_STDC_INLINE__
245−# define __gnu_inline __attribute__((gnu_inline))
246−#else
247−# define __gnu_inline
248−#endif
249−
250182 /*
251183 * Force always-inline if the user requests it so via the .config.
252184 * GCC does not warn about unused static inline functions for
@@ -258,22 +190,20 @@ struct ftrace_likely_data { · 5 unmodified lines
258190 * semantics rather than c99. This prevents multiple symbol definition errors
259191 * of extern inline functions at link time.
260192 * A lot of inline functions can cause havoc with function tracing.
193+ * Do not use __always_inline here, since currently it expands to inline again
194+ * (which would break users of __always_inline).
261195 */
262196 #if !defined(CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING) || \
263197 !defined(CONFIG_OPTIMIZE_INLINING)
264−#define inline \
265− inline __attribute__((always_inline, unused)) notrace __gnu_inline
198+#define inline inline __attribute__((__always_inline__)) __gnu_inline \
199+ __maybe_unused notrace
266200 #else
267−#define inline inline __attribute__((unused)) notrace __gnu_inline
201+#define inline inline __gnu_inline \
202+ __maybe_unused notrace
268203 #endif
269204
270205 #define __inline__ inline
271−#define __inline inline
272−#define noinline __attribute__((noinline))
273−
274−#ifndef __always_inline
275−#define __always_inline inline __attribute__((always_inline))
276−#endif
206+#define __inline inline
277207
278208 /*
279209 * Rather then using noinline to prevent stack consumption, use
scripts/Makefile.extrawarn+1−0
@@ -29,6 +29,7 @@ warning-1 += $(call cc-option, -Wmissing-include-dirs) · 28 unmodified lines
2929 warning-1 += $(call cc-option, -Wunused-but-set-variable)
3030 warning-1 += $(call cc-option, -Wunused-const-variable)
3131 warning-1 += $(call cc-option, -Wpacked-not-aligned)
32+warning-1 += $(call cc-option, -Wstringop-truncation)
3233 warning-1 += $(call cc-disable-warning, missing-field-initializers)
3334 warning-1 += $(call cc-disable-warning, sign-compare)
3435

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t