kernel: bump 4.9 to 4.9.154
[openwrt/openwrt.git] / target / linux / generic / backport-4.9 / 012-kbuild-add-macro-for-controlling-warnings-to-linux-c.patch
1 From: Arnd Bergmann <arnd@arndb.de>
2 Date: Tue, 19 Jun 2018 13:14:56 -0700
3 Subject: [PATCH] kbuild: add macro for controlling warnings to
4 linux/compiler.h
5
6 I have occasionally run into a situation where it would make sense to
7 control a compiler warning from a source file rather than doing so from
8 a Makefile using the $(cc-disable-warning, ...) or $(cc-option, ...)
9 helpers.
10
11 The approach here is similar to what glibc uses, using __diag() and
12 related macros to encapsulate a _Pragma("GCC diagnostic ...") statement
13 that gets turned into the respective "#pragma GCC diagnostic ..." by
14 the preprocessor when the macro gets expanded.
15
16 Like glibc, I also have an argument to pass the affected compiler
17 version, but decided to actually evaluate that one. For now, this
18 supports GCC_4_6, GCC_4_7, GCC_4_8, GCC_4_9, GCC_5, GCC_6, GCC_7,
19 GCC_8 and GCC_9. Adding support for CLANG_5 and other interesting
20 versions is straightforward here. GNU compilers starting with gcc-4.2
21 could support it in principle, but "#pragma GCC diagnostic push"
22 was only added in gcc-4.6, so it seems simpler to not deal with those
23 at all. The same versions show a large number of warnings already,
24 so it seems easier to just leave it at that and not do a more
25 fine-grained control for them.
26
27 The use cases I found so far include:
28
29 - turning off the gcc-8 -Wattribute-alias warning inside of the
30 SYSCALL_DEFINEx() macro without having to do it globally.
31
32 - Reducing the build time for a simple re-make after a change,
33 once we move the warnings from ./Makefile and
34 ./scripts/Makefile.extrawarn into linux/compiler.h
35
36 - More control over the warnings based on other configurations,
37 using preprocessor syntax instead of Makefile syntax. This should make
38 it easier for the average developer to understand and change things.
39
40 - Adding an easy way to turn the W=1 option on unconditionally
41 for a subdirectory or a specific file. This has been requested
42 by several developers in the past that want to have their subsystems
43 W=1 clean.
44
45 - Integrating clang better into the build systems. Clang supports
46 more warnings than GCC, and we probably want to classify them
47 as default, W=1, W=2 etc, but there are cases in which the
48 warnings should be classified differently due to excessive false
49 positives from one or the other compiler.
50
51 - Adding a way to turn the default warnings into errors (e.g. using
52 a new "make E=0" tag) while not also turning the W=1 warnings into
53 errors.
54
55 This patch for now just adds the minimal infrastructure in order to
56 do the first of the list above. As the #pragma GCC diagnostic
57 takes precedence over command line options, the next step would be
58 to convert a lot of the individual Makefiles that set nonstandard
59 options to use __diag() instead.
60
61 [paul.burton@mips.com:
62 - Rebase atop current master.
63 - Add __diag_GCC, or more generally __diag_<compiler>, abstraction to
64 avoid code outside of linux/compiler-gcc.h needing to duplicate
65 knowledge about different GCC versions.
66 - Add a comment argument to __diag_{ignore,warn,error} which isn't
67 used in the expansion of the macros but serves to push people to
68 document the reason for using them - per feedback from Kees Cook.
69 - Translate severity to GCC-specific pragmas in linux/compiler-gcc.h
70 rather than using GCC-specific in linux/compiler_types.h.
71 - Drop all but GCC 8 macros, since we only need to define macros for
72 versions that we need to introduce pragmas for, and as of this
73 series that's just GCC 8.
74 - Capitalize comments in linux/compiler-gcc.h to match the style of
75 the rest of the file.
76 - Line up macro definitions with tabs in linux/compiler-gcc.h.]
77
78 Signed-off-by: Arnd Bergmann <arnd@arndb.de>
79 Signed-off-by: Paul Burton <paul.burton@mips.com>
80 Tested-by: Christophe Leroy <christophe.leroy@c-s.fr>
81 Tested-by: Stafford Horne <shorne@gmail.com>
82 Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
83 ---
84
85 --- a/include/linux/compiler-gcc.h
86 +++ b/include/linux/compiler-gcc.h
87 @@ -338,3 +338,30 @@
88 #if GCC_VERSION >= 50100
89 #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
90 #endif
91 +
92 +
93 +/*
94 + * Turn individual warnings and errors on and off locally, depending
95 + * on version.
96 + */
97 +#define __diag_GCC(version, severity, s) \
98 + __diag_GCC_ ## version(__diag_GCC_ ## severity s)
99 +
100 +/* Severity used in pragma directives */
101 +#define __diag_GCC_ignore ignored
102 +#define __diag_GCC_warn warning
103 +#define __diag_GCC_error error
104 +
105 +/* Compilers before gcc-4.6 do not understand "#pragma GCC diagnostic push" */
106 +#if GCC_VERSION >= 40600
107 +#define __diag_str1(s) #s
108 +#define __diag_str(s) __diag_str1(s)
109 +#define __diag(s) _Pragma(__diag_str(GCC diagnostic s))
110 +#endif
111 +
112 +#if GCC_VERSION >= 80000
113 +#define __diag_GCC_8(s) __diag(s)
114 +#else
115 +#define __diag_GCC_8(s)
116 +#endif
117 +
118 --- a/include/linux/compiler.h
119 +++ b/include/linux/compiler.h
120 @@ -578,4 +578,23 @@ static __always_inline void __write_once
121 # define __kprobes
122 # define nokprobe_inline inline
123 #endif
124 +
125 +#ifndef __diag
126 +#define __diag(string)
127 +#endif
128 +
129 +#ifndef __diag_GCC
130 +#define __diag_GCC(version, severity, string)
131 +#endif
132 +
133 +#define __diag_push() __diag(push)
134 +#define __diag_pop() __diag(pop)
135 +
136 +#define __diag_ignore(compiler, version, option, comment) \
137 + __diag_ ## compiler(version, ignore, option)
138 +#define __diag_warn(compiler, version, option, comment) \
139 + __diag_ ## compiler(version, warn, option)
140 +#define __diag_error(compiler, version, option, comment) \
141 + __diag_ ## compiler(version, error, option)
142 +
143 #endif /* __LINUX_COMPILER_H */