public inbox for linux-kbuild@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH -next] Makefile: add implicit enum-conversion check for compile build
@ 2022-09-27 15:31 Zeng Heng
  2022-09-27 16:45 ` Nick Desaulniers
  0 siblings, 1 reply; 7+ messages in thread
From: Zeng Heng @ 2022-09-27 15:31 UTC (permalink / raw)
  To: masahiroy, michal.lkml, ndesaulniers, akpm, peterz, keescook,
	davidgow, nathan, jpoimboe, dan.j.williams, ojeda, isabbasso,
	dmitrii.bundin.a, vbabka, linux
  Cc: linux-kbuild, linux-kernel, liwei391, weiyongjun1, zengheng4

Provide implicit enum-conversion warning option
in general build. When it set enabled, it can
detect implicit enum type conversion and find
potential conversion errors like below
(use "allmodconfig"):

drivers/gpu/drm/amd/amdgpu/../display/dc/dml/dcn20/display_mode_vba_20.c:3904:46:
error: implicit conversion from ‘enum <anonymous>’ to ‘enum odm_combine_mode’ [-Werror=enum-conversion]
 3904 |       locals->ODMCombineEnablePerState[i][k] = true;
      |                                              ^

The '-Wenum-conversion' could be regarded as
effective check on compile runtime and
call attention on potential mistakes.

Anothor practical example could be referred to:
https://lore.kernel.org/all/CADnq5_OE0yZvEYGu82QJHL9wvVcTFZrmeTgX7URgh7FVA=jqYg@mail.gmail.com

"-Wenum-conversion" was firstly introduced from
GNU gcc-10.

Although "-Wenum-conversion" could be enabled
by "-Wextra" when compiling with 'W=1' option,
there are many warnings generated by '-Wextra'
that cause too much noise in a build.

Seeing the details from the following link:
https://gcc.gnu.org/onlinedocs/gcc-11.3.0/gcc/Warning-Options.html

Because there are still some concerned warnings
exist, the patch marks the option disabled in default
for avoiding compile failed like using "allmodconfig".

Signed-off-by: Zeng Heng <zengheng4@huawei.com>
---
 Makefile          | 5 +++++
 lib/Kconfig.debug | 7 +++++++
 2 files changed, 12 insertions(+)

diff --git a/Makefile b/Makefile
index ebd48fc956a3..1790a3624358 100644
--- a/Makefile
+++ b/Makefile
@@ -880,6 +880,11 @@ endif
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
 
+# check implicit enum conversion
+ifdef CONFIG_ENUM_CONVERSION
+KBUILD_CFLAGS += -Wenum-conversion
+endif
+
 # These result in bogus false positives
 KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
 
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 4f2b81229a2f..a64e06a747d8 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -417,6 +417,13 @@ config FRAME_WARN
 	  Setting this too low will cause a lot of warnings.
 	  Setting it to 0 disables the warning.
 
+config ENUM_CONVERSION
+	bool "Warn for implicit enum conversion"
+	depends on GCC_VERSION >= 100300
+	default n
+	help
+	  Tell gcc to warn at build time for implicit enum conversion.
+
 config STRIP_ASM_SYMS
 	bool "Strip assembler-generated symbols during link"
 	default n
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread
* [PATCH -next] Makefile: add implicit enum-conversion check for compile build
@ 2022-10-11  3:23 Zeng Heng
  2022-10-11 21:10 ` Nick Desaulniers
  0 siblings, 1 reply; 7+ messages in thread
From: Zeng Heng @ 2022-10-11  3:23 UTC (permalink / raw)
  To: masahiroy, michal.lkml, ndesaulniers, nathan, trix
  Cc: linux-kbuild, llvm, liwei391, zengheng4

Enable implicit enum-conversion warning option in kernel gcc build.
When it set enabled, it can detect implicit enum type conversion
and locate conversion errors like below (use "allmodconfig"):

drivers/gpu/drm/amd/amdgpu/../display/dc/dml/dcn20/display_mode_vba_20.c:3904:46:
error: implicit conversion from ‘enum <anonymous>’ to ‘enum odm_combine_mode’
[-Werror=enum-conversion]
 3904 |       locals->ODMCombineEnablePerState[i][k] = true;
      |                                              ^

The '-Wenum-conversion' could be regarded as effective check in
compile runtime and call attention on potential mistakes, which is
firstly introduced from GNU gcc-10.

Although "-Wenum-conversion" could be enabled by "-Wextra"
when compiling with 'W=[123]' option, there are many warnings
generated by '-Wextra' that cause too much noise in the build.

Seeing the more details from the following link:
https://gcc.gnu.org/onlinedocs/gcc-11.3.0/gcc/Warning-Options.html

Therefore, "-Wenum-conversion" warning option needs to be
explicitly requested for GCC when compilation process is only
companied with '-Wall'.

With clang, -Wenum-conversion is just default enabled, not even
behind -Wall.

Provide a couple examples for reference as below:

$ cat test.c
enum enum1 { A = 1 };
enum enum2 { B = 2 };

enum enum1 foo(enum enum2 bar)
{
    return bar;
}

$ gcc -Wall -fsyntax-only test.c

$ gcc -Wall -Wenum-conversion -fsyntax-only test.c
test.c: In function ‘foo’:
test.c:6:9: warning: implicit conversion from ‘enum enum2’ to ‘enum enum1’ [-Wenum-conversion]
    6 |  return bar;
      |         ^~~

$ gcc -Wextra -fsyntax-only test.c
test.c: In function ‘foo’:
test.c:6:9: warning: implicit conversion from ‘enum enum2’ to ‘enum enum1’ [-Wenum-conversion]
    6 |  return bar;
      |         ^~~

$ clang -fsyntax-only test.c
test.c:6:9: warning: implicit conversion from enumeration type 'enum enum2' to different enumeration type 'enum enum1' [-Wenum-conversion]
        return bar;
        ~~~~~~ ^~~
1 warning generated.

Signed-off-by: Zeng Heng <zengheng4@huawei.com>
Suggested-by: Nick Desaulniers <ndesaulniers@google.com>
Suggested-by: Nathan Chancellor <nathan@kernel.org>
---
 Makefile | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/Makefile b/Makefile
index 8f6ed52fa08f..72103d22df23 100644
--- a/Makefile
+++ b/Makefile
@@ -880,6 +880,10 @@ endif
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
 
+# implicit enum conversion checking is supported since from gcc-10
+# this warning option has to be explicitly requested for GCC
+KBUILD_CFLAGS += $(call cc-option, -Wenum-conversion)
+
 # These result in bogus false positives
 KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-10-11 21:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-27 15:31 [PATCH -next] Makefile: add implicit enum-conversion check for compile build Zeng Heng
2022-09-27 16:45 ` Nick Desaulniers
2022-09-27 17:02   ` Nathan Chancellor
2022-09-28  7:17     ` Zeng Heng
  -- strict thread matches above, loose matches on Subject: below --
2022-10-11  3:23 Zeng Heng
2022-10-11 21:10 ` Nick Desaulniers
2022-10-11 21:35   ` Masahiro Yamada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox