Skip to contentSkip to conversation

checkpatch: warn on uppercase N/Y/M as Kconfig tristate literals

PATCH Superseded Andrew Jones 2 participants 2 messages May 21 Checkpatch

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 checkpatch: warn on uppercase N/Y/M as Kconfig tristate literals

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].

---

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 | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
scripts/checkpatch.pl +14 -0
index 0d18771f1b01..e3244c270d26 100755
@@ -3732,6 +3732,20 @@ 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/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'//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
Signature · 1 line
2.43.0
#1 lore raw
Quoted 17 lines · Andrew Jones

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].

---

Quoted 10 lines

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

I need to send a v4 to also handle quoted N/M/Y as pointed out in the review of the corresponding static checker patch[1]. I'll wait a bit before sending in case other comments come in in the meantime.

[1] https://lore.kernel.org/all/numrmmjpnheiawifcjhc5zustrlk6bfv5wtr6ibdzbf7mq5o53@x6wf4zv7jhsg/

Thanks,
drew