7617c2e5f554af27e720ddf134d03dadbde4aaf0
[openwrt/staging/chunkeey.git] / target / linux / generic / backport-4.19 / 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 @@ -358,3 +358,28 @@
88 * code
89 */
90 #define uninitialized_var(x) x = x
91 +
92 +/*
93 + * Turn individual warnings and errors on and off locally, depending
94 + * on version.
95 + */
96 +#define __diag_GCC(version, severity, s) \
97 + __diag_GCC_ ## version(__diag_GCC_ ## severity s)
98 +
99 +/* Severity used in pragma directives */
100 +#define __diag_GCC_ignore ignored
101 +#define __diag_GCC_warn warning
102 +#define __diag_GCC_error error
103 +
104 +/* Compilers before gcc-4.6 do not understand "#pragma GCC diagnostic push" */
105 +#if GCC_VERSION >= 40600
106 +#define __diag_str1(s) #s
107 +#define __diag_str(s) __diag_str1(s)
108 +#define __diag(s) _Pragma(__diag_str(GCC diagnostic s))
109 +#endif
110 +
111 +#if GCC_VERSION >= 80000
112 +#define __diag_GCC_8(s) __diag(s)
113 +#else
114 +#define __diag_GCC_8(s)
115 +#endif
116 --- a/include/linux/compiler_types.h
117 +++ b/include/linux/compiler_types.h
118 @@ -283,4 +283,22 @@ struct ftrace_likely_data {
119 # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
120 #endif
121
122 +#ifndef __diag
123 +#define __diag(string)
124 +#endif
125 +
126 +#ifndef __diag_GCC
127 +#define __diag_GCC(version, severity, string)
128 +#endif
129 +
130 +#define __diag_push() __diag(push)
131 +#define __diag_pop() __diag(pop)
132 +
133 +#define __diag_ignore(compiler, version, option, comment) \
134 + __diag_ ## compiler(version, ignore, option)
135 +#define __diag_warn(compiler, version, option, comment) \
136 + __diag_ ## compiler(version, warn, option)
137 +#define __diag_error(compiler, version, option, comment) \
138 + __diag_ ## compiler(version, error, option)
139 +
140 #endif /* __LINUX_COMPILER_TYPES_H */