Skip to content

Commit 6863f56

Browse files
committed
kbuild: allow Clang to find unused static inline functions for W=1 build
GCC and Clang have different policy for -Wunused-function; GCC does not warn unused static inline functions at all whereas Clang does if they are defined in source files instead of included headers although it has been suppressed since commit abb2ea7 ("compiler, clang: suppress warning for unused static inline functions"). We often miss to delete unused functions where 'static inline' is used in *.c files since there is no tool to detect them. Unused code remains until somebody notices. For example, commit 075ddd7 ("regulator: core: remove unused rdev_get_supply()"). Let's remove __maybe_unused from the inline macro to allow Clang to start finding unused static inline functions. For now, we do this only for W=1 build since it is not a good idea to sprinkle warnings for the normal build (e.g. 35 warnings for arch/x86/configs/x86_64_defconfig). My initial attempt was to add -Wno-unused-function for no W= build (https://lore.kernel.org/patchwork/patch/1120594/) Nathan Chancellor pointed out that would weaken Clang's checks since we would no longer get -Wunused-function without W=1. It is true GCC would catch unused static non-inline functions, but it would weaken Clang as a standalone compiler, at least. Hence, here is a counter implementation. The current problem is, W=... only controls compiler flags, which are globally effective. There is no way to address only 'static inline' functions. This commit defines KBUILD_EXTRA_WARN[123] corresponding to W=[123]. When KBUILD_EXTRA_WARN1 is defined, __maybe_unused is omitted from the 'inline' macro. The new macro __inline_maybe_unused makes the code a bit uglier, so I hope we can remove it entirely after fixing most of the warnings. If you contribute to code clean-up, please run "make CC=clang W=1" and check -Wunused-function warnings. You will find lots of unused functions. Some of them are false-positives because the call-sites are disabled by #ifdef. I do not like to abuse the inline keyword for suppressing unused-function warnings because it is intended to be a hint for the compiler optimization. I prefer #ifdef around the definition, or __maybe_unused if #ifdef would make the code too ugly. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> Reviewed-by: Nathan Chancellor <natechancellor@gmail.com> Tested-by: Nathan Chancellor <natechancellor@gmail.com>
1 parent e27128d commit 6863f56

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

include/linux/compiler_types.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ struct ftrace_likely_data {
130130

131131
/*
132132
* Force always-inline if the user requests it so via the .config.
133-
* GCC does not warn about unused static inline functions for
134-
* -Wunused-function. This turns out to avoid the need for complex #ifdef
135-
* directives. Suppress the warning in clang as well by using "unused"
136-
* function attribute, which is redundant but not harmful for gcc.
137133
* Prefer gnu_inline, so that extern inline functions do not emit an
138134
* externally visible function. This makes extern inline behave as per gnu89
139135
* semantics rather than c99. This prevents multiple symbol definition errors
@@ -144,15 +140,27 @@ struct ftrace_likely_data {
144140
*/
145141
#if !defined(CONFIG_OPTIMIZE_INLINING)
146142
#define inline inline __attribute__((__always_inline__)) __gnu_inline \
147-
__maybe_unused notrace
143+
__inline_maybe_unused notrace
148144
#else
149145
#define inline inline __gnu_inline \
150-
__maybe_unused notrace
146+
__inline_maybe_unused notrace
151147
#endif
152148

153149
#define __inline__ inline
154150
#define __inline inline
155151

152+
/*
153+
* GCC does not warn about unused static inline functions for -Wunused-function.
154+
* Suppress the warning in clang as well by using __maybe_unused, but enable it
155+
* for W=1 build. This will allow clang to find unused functions. Remove the
156+
* __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
157+
*/
158+
#ifdef KBUILD_EXTRA_WARN1
159+
#define __inline_maybe_unused
160+
#else
161+
#define __inline_maybe_unused __maybe_unused
162+
#endif
163+
156164
/*
157165
* Rather then using noinline to prevent stack consumption, use
158166
* noinline_for_stack instead. For documentation reasons.

scripts/Makefile.extrawarn

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
3636
KBUILD_CFLAGS += -Wno-missing-field-initializers
3737
KBUILD_CFLAGS += -Wno-sign-compare
3838

39+
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
40+
3941
else
4042

4143
# Some diagnostics enabled by default are noisy.
@@ -65,6 +67,8 @@ KBUILD_CFLAGS += -Wsign-compare
6567
KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
6668
KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
6769

70+
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
71+
6872
endif
6973

7074
#
@@ -82,4 +86,6 @@ KBUILD_CFLAGS += -Wredundant-decls
8286
KBUILD_CFLAGS += -Wswitch-default
8387
KBUILD_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
8488

89+
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
90+
8591
endif

0 commit comments

Comments
 (0)