* [PATCH v2] kconfig: add kconfig-sym-check static checker
@ 2026-05-19 21:58 Andrew Jones
2026-05-19 23:59 ` Julian Braha
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Jones @ 2026-05-19 21:58 UTC (permalink / raw)
To: linux-kbuild, linux-kernel
Cc: nathan, nsc, andriy.shevchenko, rdunlap, julianbraha
Add 'make kconfig-sym-check', a static checker that finds Kconfig
symbols referenced in expressions (select, depends on, default, etc.)
but never defined via config/menuconfig anywhere in the tree. New
dangling symbols are reported as errors (exit 1) unless they are
listed in an exclusion file, e.g.
KCONFIG_SYM_CHECK_EXCLUDES=sym-check-excludes make kconfig-sym-check
The checker also warns about uppercase N/Y/M used as tristate literal
values following the same logic as checkpatch.
This new static checker is the script used for [1] with a few
improvements to avoid some false positives.
Link: https://bugzilla.kernel.org/show_bug.cgi?id=216748 [1]
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Randy Dunlap <rdunlap@infradead.org>
Tested-by: Randy Dunlap <rdunlap@infradead.org>
---
v2:
- Added Andy's and Randy's tags
- Accept srctree as first argument so the Makefile can drop 'cd $(srctree)' [Nathan]
- Replace git ls-files with git+find fallback [Nathan and Andy]
- 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
Makefile | 7 +-
scripts/kconfig/kconfig-sym-check.pl | 97 ++++++++++++++++++++++++++++
2 files changed, 103 insertions(+), 1 deletion(-)
create mode 100755 scripts/kconfig/kconfig-sym-check.pl
diff --git a/Makefile b/Makefile
index fbc67fcb6cdb..b86ccc1e08cd 100644
--- a/Makefile
+++ b/Makefile
@@ -293,6 +293,7 @@ version_h := include/generated/uapi/linux/version.h
clean-targets := %clean mrproper cleandocs
no-dot-config-targets := $(clean-targets) \
cscope gtags TAGS tags help% %docs check% coccicheck \
+ kconfig-sym-check \
$(version_h) headers headers_% archheaders archscripts \
%asm-generic kernelversion %src-pkg dt_binding_check \
outputmakefile rustavailable rustfmt rustfmtcheck \
@@ -1811,6 +1812,7 @@ help:
@echo ' includecheck - Check for duplicate included header files'
@echo ' headerdep - Detect inclusion cycles in headers'
@echo ' coccicheck - Check with Coccinelle'
+ @echo ' kconfig-sym-check - Check for dangling Kconfig symbol references'
@echo ' clang-analyzer - Check with clang static analyzer'
@echo ' clang-tidy - Check with clang-tidy'
@echo ''
@@ -2232,7 +2234,7 @@ endif
# Scripts to check various things for consistency
# ---------------------------------------------------------------------------
-PHONY += includecheck versioncheck coccicheck
+PHONY += includecheck versioncheck coccicheck kconfig-sym-check
includecheck:
find $(srctree)/* $(RCS_FIND_IGNORE) \
@@ -2247,6 +2249,9 @@ versioncheck:
coccicheck:
$(Q)$(BASH) $(srctree)/scripts/$@
+kconfig-sym-check:
+ $(Q)$(PERL) $(srctree)/scripts/kconfig/kconfig-sym-check.pl $(srctree) $(KCONFIG_SYM_CHECK_EXCLUDES)
+
PHONY += checkstack kernelrelease kernelversion image_name
# UML needs a little special treatment here. It wants to use the host
diff --git a/scripts/kconfig/kconfig-sym-check.pl b/scripts/kconfig/kconfig-sym-check.pl
new file mode 100755
index 000000000000..6bf32fb37929
--- /dev/null
+++ b/scripts/kconfig/kconfig-sym-check.pl
@@ -0,0 +1,97 @@
+#!/usr/bin/env perl
+# SPDX-License-Identifier: GPL-2.0
+
+use warnings;
+use strict;
+
+my $srctree = shift @ARGV;
+my $kconfig_sym_check_excludes = defined $ARGV[0] ? $ARGV[0] : undef;
+
+my @files = `git -C \Q$srctree\E ls-files '*Kconfig*' 2>/dev/null`;
+if (@files) {
+ chomp @files;
+ @files = map { "$srctree/$_" } @files;
+} else {
+ @files = `find \Q$srctree\E -name '*Kconfig*'`;
+ chomp @files;
+}
+
+my %configs = ();
+my %refs = ();
+
+foreach my $file (@files) {
+ open F, $file or die "Cannot open $file: $!";
+
+ my $help = 0;
+ my $help_level;
+ my $level;
+
+ while (<F>) {
+ chomp;
+
+ next if /^\s*$/;
+ next if /^\s*#/;
+
+ /^(\s*)/;
+ $level = length $1;
+
+ if ($help && $level < $help_level) {
+ $help = 0;
+ }
+
+ next if ($help);
+
+ if (/^\s*(help|\-\-\-help\-\-\-)$/) {
+ $help = 1;
+ $_ = <F>;
+ /^(\s*)/;
+ $help_level = length $1;
+ next;
+ }
+
+ if (/^\s*(config|menuconfig)\s+([a-zA-Z0-9_]+)\s*(#.*)?$/) {
+ $configs{$2}++;
+ next;
+ }
+
+ if (/^\s*(default|def_bool|def_tristate|select|depends\s+on|imply|visible\s+if|range|if)\s+(.+)\s*$/) {
+ my $s = $2;
+ $s =~ s/"[^"]*"//g;
+ $s =~ s/'[^']*'//g;
+ $s =~ s/#.*//;
+ $s =~ s/\$\([^)]*\)//g;
+ $s =~ s/%%[^%]*%%//g;
+ my @syms = split /[^a-zA-Z0-9_]+/, $s;
+ map {
+ $refs{$_}++ if (/[a-zA-Z]/ && $_ ne "if" && $_ ne "y" && $_ ne "n" && $_ ne "m" && !(/^0[xX]/ && !/[g-wy-zG-WY-Z]/));
+ } @syms
+ }
+ }
+
+ close F;
+}
+
+my %known_syms = ();
+if (defined $kconfig_sym_check_excludes) {
+ my $file = $kconfig_sym_check_excludes;
+ open F, $file or die "Cannot open $file: $!";
+ while (<F>) {
+ chomp;
+ next if /^\s*$/;
+ next if /^\s*#/;
+ $known_syms{$1}++ if (/^\s*([a-zA-Z0-9_]+)\s*(#.*)?$/);
+ }
+}
+
+my $ret = 0;
+foreach my $k (sort keys %refs) {
+ next if (exists $configs{$k} || exists $known_syms{$k});
+
+ print "$k";
+ print " - warning: '$k' is probably not what you want; Kconfig tristate literals are always lowercase ('n', 'y', 'm')" if ($k eq "N" || $k eq "Y" || $k eq "M");
+ print "\n";
+
+ $ret = 1;
+}
+
+exit $ret;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v2] kconfig: add kconfig-sym-check static checker
2026-05-19 21:58 [PATCH v2] kconfig: add kconfig-sym-check static checker Andrew Jones
@ 2026-05-19 23:59 ` Julian Braha
2026-05-20 0:39 ` Randy Dunlap
0 siblings, 1 reply; 4+ messages in thread
From: Julian Braha @ 2026-05-19 23:59 UTC (permalink / raw)
To: Andrew Jones, linux-kbuild, linux-kernel
Cc: nathan, nsc, andriy.shevchenko, rdunlap
On 5/19/26 22:58, Andrew Jones wrote:
> The checker also warns about uppercase N/Y/M used as tristate literal
> values following the same logic as checkpatch.
Hm, I tested this out by manually adding a:
```
config KCONFIG_SYM_TEST
default Y
```
to the end of the root Kconfig file, but the script didn't actually
catch it. The hint about casing only appeared with the 'N'.
Could this be a bug, or is the Y symbol actually defined somewhere...?
- Julian Braha
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] kconfig: add kconfig-sym-check static checker
2026-05-19 23:59 ` Julian Braha
@ 2026-05-20 0:39 ` Randy Dunlap
2026-05-20 13:19 ` Andrew Jones
0 siblings, 1 reply; 4+ messages in thread
From: Randy Dunlap @ 2026-05-20 0:39 UTC (permalink / raw)
To: Julian Braha, Andrew Jones, linux-kbuild, linux-kernel
Cc: nathan, nsc, andriy.shevchenko
On 5/19/26 4:59 PM, Julian Braha wrote:
> On 5/19/26 22:58, Andrew Jones wrote:
>> The checker also warns about uppercase N/Y/M used as tristate literal
>> values following the same logic as checkpatch.
>
> Hm, I tested this out by manually adding a:
> ```
> config KCONFIG_SYM_TEST
> default Y
> ```
>
> to the end of the root Kconfig file, but the script didn't actually
> catch it. The hint about casing only appeared with the 'N'.
>
> Could this be a bug, or is the Y symbol actually defined somewhere...?
It's defined in a test file:
./scripts/kconfig/tests/choice_randomize/Kconfig:19:config Y
Could the script be seeing/using that?
--
~Randy
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] kconfig: add kconfig-sym-check static checker
2026-05-20 0:39 ` Randy Dunlap
@ 2026-05-20 13:19 ` Andrew Jones
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Jones @ 2026-05-20 13:19 UTC (permalink / raw)
To: Randy Dunlap
Cc: Julian Braha, linux-kbuild, linux-kernel, nathan, nsc,
andriy.shevchenko
On Tue, May 19, 2026 at 05:39:40PM -0700, Randy Dunlap wrote:
>
>
> On 5/19/26 4:59 PM, Julian Braha wrote:
> > On 5/19/26 22:58, Andrew Jones wrote:
> >> The checker also warns about uppercase N/Y/M used as tristate literal
> >> values following the same logic as checkpatch.
> >
> > Hm, I tested this out by manually adding a:
> > ```
> > config KCONFIG_SYM_TEST
> > default Y
> > ```
> >
> > to the end of the root Kconfig file, but the script didn't actually
> > catch it. The hint about casing only appeared with the 'N'.
> >
> > Could this be a bug, or is the Y symbol actually defined somewhere...?
>
> It's defined in a test file:
> ./scripts/kconfig/tests/choice_randomize/Kconfig:19:config Y
>
> Could the script be seeing/using that?
Yup, looks like it. But I still need a v3, because I see I broke things with a
v2 change I made trying to satisfy sashiko. The script now outputs a bunch of
junk along with the dangling symbols...
drew
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-05-20 13:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-19 21:58 [PATCH v2] kconfig: add kconfig-sym-check static checker Andrew Jones
2026-05-19 23:59 ` Julian Braha
2026-05-20 0:39 ` Randy Dunlap
2026-05-20 13:19 ` Andrew Jones
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox