Skip to contentSkip to conversation

kconfig: add kconfig-sym-check static checker

PATCH Superseded Andrew Jones 3 participants 5 messages May 21 – May 22

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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.

---

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 | 123 +++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 9 deletions(-)
 create mode 100755 scripts/kconfig/kconfig-sym-check.pl
Makefile +14 -9
index fbc67fcb6cdb..92c8ed867d0d 100644
@@ -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 \
@@ -1805,14 +1806,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'
@@ -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
scripts/kconfig/kconfig-sym-check.pl new file +123 -0

123 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 2 lines · Andrew Jones

The checker also warns about uppercase N/Y/M used as tristate literal values following the same logic as checkpatch.

Thanks, this is better! But...

While it does catch the Y case now, it still doesn't catch 'Y' with quotes. Again, I had to manually inject this into Kconfig; I don't actually know if this is used anywhere.

- Julian Braha

#2 lore raw
Quoted 10 lines · Julian Braha
Quoted 2 lines · Andrew Jones

The checker also warns about uppercase N/Y/M used as tristate literal values following the same logic as checkpatch.

Thanks, this is better! But...

While it does catch the Y case now, it still doesn't catch 'Y' with quotes. Again, I had to manually inject this into Kconfig; I don't actually know if this is used anywhere.

Ah, I see kconfig interprets "default 'y'" equivalently to "default y". That's likely due to an implementation quirk of sym_lookup() and it's a bit frustrating and maybe even deserves a fix. At least 'Y' is still invalid, so we can look for "N", 'N', "M", 'M', "Y", 'Y' and complain when we see them, but that seems more like a job for the corresponding checkpatch patch[1]. This script is looking for symbols, and symbols should never be quoted. We only add the extra warning text in this script for dangling N, M, Y since there's a good chance those should have been n, m, y instead.

[1] https://lore.kernel.org/all/[email protected]/

Thanks,
drew

reviewer Tested-by
#3 lore raw
Quoted 2 lines · Andrew Jones

This script is looking for symbols, and symbols
should never be quoted.

Good point, out of scope.

It looks good to me otherwise, so:

#4 lore raw
Quoted 9 lines · Julian Braha
Quoted 2 lines · Andrew Jones

This script is looking for symbols, and symbols
should never be quoted.

Good point, out of scope.

It looks good to me otherwise, so:

Thanks! I'll also send a v4 of this patch to resolve the final sashiko comment since it looks worth doing.

drew