mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] kbuild: move gcc's -Wconstant-logical-operand to W=2
@ 2026-11-05 17:57 Vineet Gupta
  2026-11-05 18:03 ` Nick Desaulniers
  2026-11-05 19:45 ` Nathan Chancellor
  0 siblings, 2 replies; 3+ messages in thread
From: Vineet Gupta @ 2026-11-05 17:57 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier
  Cc: Vineet Gupta, Borislav Petkov, Peter Zijlstra, Jakub Jelinek,
	Julian Braha, Nick Desaulniers, Bill Wendling, Justin Stitt,
	linux-kbuild, linux-kernel, llvm

GCC 17 adds -Wconstant-logical-operand to -Wall. It warns when an operand
of && or || folds to a constant other than 0 or 1. Unlike clang's version,
it also checks constant left operands and operators that come from a
macro expansion. With CONFIG_WERROR=y, an x86 defconfig build fails in
prepare0:

  arch/x86/include/asm/cpufeature.h:72:36: error: use of logical '&&' with constant operand '4194304' [-Werror=constant-logical-operand]

The warning is meant to catch && written for &. Kernel history has no
such catch. Of the 13 earlier kernel fixes for clang's version, only
commit dd7fe5d9fd6a ("rtc: max31335: Fix comparison in
max31335_volatile_reg()") fixed a bug that the warning found. Commit
394b52462020 ("drm/i915/gem: fix bitwise and logical AND mixup") fixed a
& written for &&, which the warning cannot see. The other fixes rewrote
correct code. In GCC's own tree, the warning found two sites since it
was added, and both were false positives.

Disable the warning for GCC unless W=2 is given. Keep clang's version,
which skips macros and constant left operands.

Suggested-by: Borislav Petkov <bp@alien8.de>
Link: https://lore.kernel.org/all/20261104160722.1637541-1-vineet.gupta@linux.dev/
Signed-off-by: Vineet Gupta <vineet.gupta@linux.dev>
---
v1 -> v2:
 - Replace the four per-site fixes with this one change (Borislav, Peter)
 - Link to v1: https://lore.kernel.org/all/20261104160722.1637541-1-vineet.gupta@linux.dev/

 scripts/Kconfig.toolchain | 3 +++
 scripts/Makefile.warn     | 1 +
 2 files changed, 4 insertions(+)

diff --git a/scripts/Kconfig.toolchain b/scripts/Kconfig.toolchain
index 2acbd40a38f1..1d68075f0469 100644
--- a/scripts/Kconfig.toolchain
+++ b/scripts/Kconfig.toolchain
@@ -233,6 +233,9 @@ config CC_OPT_WNO_FORMAT_OVERFLOW
 config CC_OPT_WNO_STRINGOP_TRUNCATION
 	def_string "$(cc-option-str,-Wno-stringop-truncation)"
 
+config CC_OPT_WNO_CONSTANT_LOGICAL_OPERAND
+	def_string "$(cc-option-str,-Wno-constant-logical-operand)"
+
 config CC_HAS_COUNTED_BY
 	bool
 	# clang needs to be at least 20.1.0 to avoid potential crashes
diff --git a/scripts/Makefile.warn b/scripts/Makefile.warn
index 556fa249ca07..03b677bdbb0a 100644
--- a/scripts/Makefile.warn
+++ b/scripts/Makefile.warn
@@ -167,6 +167,7 @@ endif
 
 ifdef CONFIG_CC_IS_GCC
 KBUILD_CFLAGS += -Wno-maybe-uninitialized
+KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_CONSTANT_LOGICAL_OPERAND)
 endif
 
 endif
-- 
2.53.0-Meta


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

end of thread, other threads:[~2026-11-05 19:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-05 17:57 [PATCH v2] kbuild: move gcc's -Wconstant-logical-operand to W=2 Vineet Gupta
2026-11-05 18:03 ` Nick Desaulniers
2026-11-05 19:45 ` Nathan Chancellor

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

all inboxes | Powered by JetHome®