kconfig: add kconfig-sym-check static checker
PATCH v4 1/1 kconfig: add kconfig-sym-check static checker
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 exclusion file lists one symbol per line; blank lines and lines starting with '#' are ignored.
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.
---
Note, v1 was based on next-20260508 and this checker found 14 dangling symbols on that base. v4 is based on next-20260526 and there are now 15. EZX_PCAP has recently emerged as a new dangling symbol after commit b12d1da45f12 ("mfd: ezx-pcap: Remove unused driver") was merged.
v4:
- improved stripping of $(macro) expansions [Sashiko]
- improved failure when run without srctree parameter
- Added Julian's t-b
v3:
- Filter out scripts/kconfig/tests Kconfig files since they may be wrong on purpose and indeed there is a 'config Y' in there which would mask improper use of 'Y'. [Julian and Randy]
- Fixed breakage introduced in v2 when attempting to be too
clever...
- More changes from another sashiko review which required the Perl to get even uglier. So ugly that I enlisted Claude to help generate it.
- Added a sentence to the commit message to describe the excludes file format.
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 | 23 +++-- scripts/kconfig/kconfig-sym-check.pl | 132 +++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 9 deletions(-) create mode 100755 scripts/kconfig/kconfig-sym-check.pl
Makefile +14 -9
@@ -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 \
@@ -1821,14 +1822,15 @@ help: echo ' (default: $(INSTALL_HDR_PATH))'; \ echo '' @echo 'Static analysers:' - @echo ' checkstack - Generate a list of stack hogs and consider all functions' - @echo ' with a stack size larger than MINSTACKSIZE (default: 100)' - @echo ' versioncheck - Sanity check on version.h usage' - @echo ' includecheck - Check for duplicate included header files' - @echo ' headerdep - Detect inclusion cycles in headers' - @echo ' coccicheck - Check with Coccinelle' - @echo ' clang-analyzer - Check with clang static analyzer' - @echo ' clang-tidy - Check with clang-tidy' + @echo ' checkstack - Generate a list of stack hogs and consider all functions' + @echo ' with a stack size larger than MINSTACKSIZE (default: 100)' + @echo ' versioncheck - Sanity check on version.h usage' + @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 '' @echo 'Tools:' @echo ' nsdeps - Generate missing symbol namespace dependencies'
@@ -2272,7 +2274,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) \
@@ -2287,6 +2289,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
scripts/kconfig/kconfig-sym-check.pl new file +132 -0
132 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 123 lines · Andrew Jones
Add 'make kconfig-sym-check', a static checker that finds
Kconfigsymbols 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 exclusion file lists one symbol per line; blank lines and lines starting with '#' are ignored.
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.
Assisted-by Claude:claude-sonnet-4-6Signed-off-by Andrew Jones <[email protected]>Acked-by Andy Shevchenko <[email protected]>Acked-by Randy Dunlap <[email protected]>Tested-by Randy Dunlap <[email protected]>Tested-by Julian Braha <[email protected]>---
Note, v1 was based on next-20260508 and this checker found 14 dangling symbols on that base. v4 is based on next-20260526 and there are now 15.
EZX_PCAPhas recently emerged as a new dangling symbol after commitb12d1da45f12("mfd: ezx-pcap: Remove unused driver") was merged.v4:
- improved stripping of $(macro) expansions [Sashiko]
- improved failure when run without srctree parameter
- Added Julian's t-bv3:
- Filter outscripts/kconfig/testsKconfigfiles since they may be wrong on purpose and indeed there is a 'config Y' in there which would mask improper use of 'Y'. [Julian and Randy]
- Fixed breakage introduced in v2 when attempting to be tooclever...- More changes from another sashiko review which required the Perl to get even uglier. So ugly that I enlisted Claude to help generate it.
- Added a sentence to the commit message to describe the excludes file format.v2:
- Added Andy's and Randy's tags
- Accept srctree as first argument so theMakefilecan 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) expansionsMakefile | 23 +++-- scripts/kconfig/kconfig-sym-check.pl | 132 +++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 9 deletions(-) create mode 100755 scripts/kconfig/kconfig-sym-check.plMakefile +14
-9@@ -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 \@@ -1821,14 +1822,15 @@ help: echo ' (default: $(INSTALL_HDR_PATH))'; \ echo '' @echo 'Static analysers:' - @echo ' checkstack - Generate a list of stack hogs and consider all functions' - @echo ' with a stack size larger than MINSTACKSIZE (default: 100)' - @echo ' versioncheck - Sanity check on version.h usage' - @echo ' includecheck - Check for duplicate included header files' - @echo ' headerdep - Detect inclusion cycles in headers' - @echo ' coccicheck - Check with Coccinelle' - @echo ' clang-analyzer - Check with clang static analyzer' - @echo ' clang-tidy - Check with clang-tidy' + @echo ' checkstack - Generate a list of stack hogs and consider all functions' + @echo ' with a stack size larger than MINSTACKSIZE (default: 100)' + @echo ' versioncheck - Sanity check on version.h usage' + @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 '' @echo 'Tools:' @echo ' nsdeps - Generate missing symbol namespace dependencies'@@ -2272,7 +2274,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) \@@ -2287,6 +2289,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 hostscripts/kconfig/kconfig-sym-check.pl new file +0
-0
scripts/kconfig/kconfig-sym-check.pl line 8
Quoted 8 lines
scripts/kconfig/kconfig-sym-check.pl new file +8
-0@@ -0,0 +1,132 @@ +#!/usr/bin/env perl +# SPDX-License-Identifier: GPL-2.0 + +use warnings; +use strict; + +my $srctree = shift @ARGV; +unless (defined $srctree) {
unless (defined $srctree && -d $srctree) {
would also catch common attempts like 'kconfig-sym-check.pl --help' (at least in most cases).
Quoted 70 lines
scripts/kconfig/kconfig-sym-check.pl new file +70
-0+ $srctree = `git rev-parse --show-toplevel 2>/dev/null`; + chomp $srctree; + my $msg = "Usage: $0 <srctree> [excludes file]\n"; + $msg .= "Please provide <srctree>."; + $msg .= " Is it '$srctree'?" if $srctree; + $msg .= "\n"; + die $msg; +} +my $kconfig_sym_check_excludes = defined $ARGV[0] ? $ARGV[0] : undef; + +sub indent_depth { + my ($ws) = @_; + my $col = 0; + for my $c (split //, $ws) { + $col = $c eq "\t" ? int($col / 8) * 8 + 8 : $col + 1; + } + return $col; +} + +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; +} + +@files = grep { !m{/scripts/kconfig/tests/} } @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; + + while (/\\\s*$/) { + s/\\\s*$/ /; + my $cont = <F> // last; + chomp $cont; + $_ .= $cont; + } + + next if /^\s*$/; + next if /^\s*#/; + + /^(\s*)/; + $level = indent_depth($1); + + if ($help && $level < $help_level) { + $help = 0; + } + + next if ($help); + + if (/^\s*(help|\-\-\-help\-\-\-)$/) { + $help = 1; + my $next; + while (defined($next = <F>)) { + last unless $next =~ /^\s*(?:#.*)?$/; + } + last unless defined $next; + $next =~ /^(\s*)/;
scripts/kconfig/kconfig-sym-check.pl line 88
Quoted 10 lines
scripts/kconfig/kconfig-sym-check.pl new file +10
-0+ if (indent_depth($1) >= $level) { + $help_level = indent_depth($1); + } else { + $help = 0; + } + $_ = $next; + redo; + } + + if (/^\s*(config|menuconfig)\s+([a-zA-Z0-9_]+)\s*(#.*)?$/) {
Sashiko claims that named choices are not catched here (but I could not find any). And all current findings of kconfig-sym-check look valid to me, thus I don't see a strong need to adjust here for now.
Quoted 29 lines
scripts/kconfig/kconfig-sym-check.pl new file +29
-0+ $configs{$2}++; + next; + } + + if (/^\s*(default|def_bool|def_tristate|select|depends\s+on|imply|visible\s+if|range|if|bool|tristate|int|hex|string|prompt)\s+(.+)\s*$/) { + my $s = $2; + $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][0-9a-fA-F]+$/); + } @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*(#.*)?$/);
scripts/kconfig/kconfig-sym-check.pl line 127
Quoted 10 lines
scripts/kconfig/kconfig-sym-check.pl new file +10
-0+ } +} + +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";
Just bike-shedding: I'd liked if kconfig-sym-check would output the origin of its findings (gcc error style would help common tooling to jump there, too). But finding stray ever-undefined symbols is a good thing, and finding these is no magic.
As my perl is in a quite bad state, I cannot review in-depth. Would you be available for maintaining the script yourself (= entry in MAINTAINERS)?
Nathan Chancellor
maintainerQuoted 1 line · Andrew Jones
kconfig: add kconfig-sym-check static checker
I took this in its current form since perfect is the enemy of good but I think some of Nicolas's comments are certainly relevant, so please feel free to set fixups on top of kbuild-next as you see fit.
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git kbuild-next
Thanks!
[1/1] kconfig: add kconfig-sym-check static checker
https://git.kernel.org/kbuild/c/f58316a441b46
Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped or reverted. Patches applied to an "unstable" branch are accepted pending wider testing in -next and any post-commit review; they will generally be moved to the main branch in a week if no issues are found.
Best regards,
Signature · 2 lines
Cheers, Nathan
Quoted 13 lines · Nathan Chancellor
Quoted 1 line · Andrew Jones
kconfig: add kconfig-sym-check static checker
I took this in its current form since perfect is the enemy of good but I think some of Nicolas's comments are certainly relevant, so please feel free to set fixups on top of kbuild-next as you see fit.
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git kbuild-next
Thanks!
Quoted 8 lines
[1/1] kconfig: add kconfig-sym-check static checker
https://git.kernel.org/kbuild/c/f58316a441b46Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped or reverted. Patches applied to an "unstable" branch are accepted pending wider testing in -next and any post-commit review; they will generally be moved to the main branch in a week if no issues are found.
Thanks, Nathan.
I've been meaning to get back to this in order to address Nicolas's comments, but I got buried in other things recently and have vacation next week. I won't drop those improvement suggestions from my TODO, though, so I'll hopefully be able to address them in the near future. And I'm happy to keep it maintained going forward as well.
Thanks,
drew
Nathan Chancellor
maintainerHi Drew,
Quoted 5 lines · Andrew Jones
I've been meaning to get back to this in order to address Nicolas's comments, but I got buried in other things recently and have vacation next week. I won't drop those improvement suggestions from my TODO, though, so I'll hopefully be able to address them in the near future. And I'm happy to keep it maintained going forward as well.
No worries at all, I see quality of life improvements as "things to work on when things are slow" and things are slower during the summer anyways, so please do not feel like you have to rush to get these done. Just wanted to make sure they were on your radar.
Signature · 2 lines
Cheers, Nathan