Skip to contentSkip to conversation

kconfig: add kconfig-sym-check static checker

PATCH Superseded Andrew Jones 3 participants 4 messages May 19 – May 20

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

---

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
Makefile +6 -1
index fbc67fcb6cdb..b86ccc1e08cd 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 \
@@ -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
scripts/kconfig/kconfig-sym-check.pl new file +97 -0

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

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

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

Hm, I tested this out by manually adding a:

```
config KCONFIG_SYM_TEST
  default Y
```
Quoted 4 lines

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?

Signature · 1 line
~Randy
#3 lore raw
Quoted 16 lines · Randy Dunlap
Quoted 13 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.

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

Quoted 6 lines
Quoted 1 line

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