From patchwork Tue Oct 11 03:23:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zeng Heng X-Patchwork-Id: 13003576 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBC65C433F5 for ; Tue, 11 Oct 2022 03:24:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229764AbiJKDYX (ORCPT ); Mon, 10 Oct 2022 23:24:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229641AbiJKDYW (ORCPT ); Mon, 10 Oct 2022 23:24:22 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5986974BBD for ; Mon, 10 Oct 2022 20:24:20 -0700 (PDT) Received: from kwepemi500024.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Mmgxs0NfkzmV6b; Tue, 11 Oct 2022 11:19:45 +0800 (CST) Received: from huawei.com (10.175.103.91) by kwepemi500024.china.huawei.com (7.221.188.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 11 Oct 2022 11:24:17 +0800 From: Zeng Heng To: , , , , CC: , , , Subject: [PATCH -next] Makefile: add implicit enum-conversion check for compile build Date: Tue, 11 Oct 2022 11:23:27 +0800 Message-ID: <20221011032327.2761241-1-zengheng4@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.103.91] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemi500024.china.huawei.com (7.221.188.100) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org 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 ’ 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 Suggested-by: Nick Desaulniers Suggested-by: Nathan Chancellor Reviewed-by: Nick Desaulniers --- 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)