mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] checkpatch: warn on uppercase N/Y/M as Kconfig tristate literals
@ 2026-05-27 14:25 Andrew Jones
  2026-05-27 14:47 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Jones @ 2026-05-27 14:25 UTC (permalink / raw)
  To: linux-kernel
  Cc: apw, joe, dwaipayanray1, lukas.bulwahn, andriy.shevchenko, julianbraha

Kconfig tristate literals are always lowercase ('n', 'y', 'm') and
uppercase N/Y/M are not Kconfig reserved words. Since undefined
symbols evaluate to 'n', writing 'default Y' or 'default M' silently
produces 'n' instead of 'y'/'m'. 'default N' happens to produce the
right value but is still invalid syntax.

Add a warning for N/Y/M in Kconfig expressions found by following
the same preprocessing logic used by the Kconfig parser itself.

This new warning was inspired by work done for [1].

Link: https://bugzilla.kernel.org/show_bug.cgi?id=216748 [1]
Assisted-by: Claude:claude-sonnet-4-6
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---

v4:
  - handle quoted N/M/Y [Julian]
  - improved stripping of $(macro) expansions [Sashiko]

v3:
  - More changes from another sashiko review which required the
    Perl to get even uglier.
v2:
  - Added Andy's tag
  - Changes thanks to sashiko's review
    - strip quoted strings before inline comments to avoid '#' inside a string
    - use [^)]* instead of .* in macro strip regex to avoid greedy match
      eating tokens between adjacent $(macro) expansions

 scripts/checkpatch.pl | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3727156e4cca..5ed74102ac23 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3732,6 +3732,22 @@ sub process {
 			}
 		}
 
+# check for uppercase N/Y/M used as Kconfig tristate literals
+		if ($realfile =~ /Kconfig/ &&
+		    $line =~ /^\+\s*(?:default|def_bool|def_tristate|select|imply|depends\s+on|visible\s+if|range|if|bool|tristate|int|hex|string|prompt)\s+(.+)$/) {
+			my $expr = $1;
+			$expr =~ s/"([NYM])"/$1/g;				# unwrap quoted N/Y/M
+			$expr =~ s/'([NYM])'/$1/g;
+			$expr =~ s/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'//g;	# strip quoted strings
+			$expr =~ s/#.*//;					# strip inline comments
+			$expr =~ s/\$\((?:[^()]*|\((?:[^()]*|\([^()]*\))*\))*\)//g;	# strip $(macro) expansions
+			for my $tok (split /[^A-Za-z0-9_]+/, $expr) {
+				next unless ($tok eq 'Y' || $tok eq 'M' || $tok eq 'N');
+				WARN("KCONFIG_TRISTATE_UPPERCASE",
+				     "'$tok' is probably not what you want here; Kconfig tristate literals are always lowercase ('n', 'y', 'm')\n" . $herecurr);
+			}
+		}
+
 # check MAINTAINERS entries
 		if ($realfile =~ /^MAINTAINERS$/) {
 # check MAINTAINERS entries for the right form
-- 
2.43.0


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

* Re: [PATCH v4] checkpatch: warn on uppercase N/Y/M as Kconfig tristate literals
  2026-05-27 14:25 [PATCH v4] checkpatch: warn on uppercase N/Y/M as Kconfig tristate literals Andrew Jones
@ 2026-05-27 14:47 ` Joe Perches
  2026-05-27 17:34   ` Andrew Jones
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2026-05-27 14:47 UTC (permalink / raw)
  To: Andrew Jones, linux-kernel
  Cc: apw, dwaipayanray1, lukas.bulwahn, andriy.shevchenko, julianbraha

On Wed, 2026-05-27 at 09:25 -0500, Andrew Jones wrote:
> Kconfig tristate literals are always lowercase ('n', 'y', 'm') and
> uppercase N/Y/M are not Kconfig reserved words. Since undefined
> symbols evaluate to 'n', writing 'default Y' or 'default M' silently
> produces 'n' instead of 'y'/'m'. 'default N' happens to produce the
> right value but is still invalid syntax.

[]

> Link: https://bugzilla.kernel.org/show_bug.cgi?id=216748 [1]

Unless I misread the bug report:

All this for something that happened once and
when it happened was benign?


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

* Re: [PATCH v4] checkpatch: warn on uppercase N/Y/M as Kconfig tristate literals
  2026-05-27 14:47 ` Joe Perches
@ 2026-05-27 17:34   ` Andrew Jones
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Jones @ 2026-05-27 17:34 UTC (permalink / raw)
  To: Joe Perches
  Cc: Andrew Jones, linux-kernel, apw, dwaipayanray1, lukas.bulwahn,
	andriy.shevchenko, julianbraha

On Wed, May 27, 2026 at 07:47:51AM -0700, Joe Perches wrote:
> On Wed, 2026-05-27 at 09:25 -0500, Andrew Jones wrote:
> > Kconfig tristate literals are always lowercase ('n', 'y', 'm') and
> > uppercase N/Y/M are not Kconfig reserved words. Since undefined
> > symbols evaluate to 'n', writing 'default Y' or 'default M' silently
> > produces 'n' instead of 'y'/'m'. 'default N' happens to produce the
> > right value but is still invalid syntax.
> 
> []
> 
> > Link: https://bugzilla.kernel.org/show_bug.cgi?id=216748 [1]
> 
> Unless I misread the bug report:
> 
> All this for something that happened once and

that we know of

> when it happened was benign?
>

Correct, but 'default M' and 'default Y' would not be benign and they
would potentially lead to some head scratching. I also don't know if
any of those types of instances have ever occurred.

Thanks,
drew

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

end of thread, other threads:[~2026-05-27 17:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-27 14:25 [PATCH v4] checkpatch: warn on uppercase N/Y/M as Kconfig tristate literals Andrew Jones
2026-05-27 14:47 ` Joe Perches
2026-05-27 17:34   ` Andrew Jones

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®