summaryrefslogtreecommitdiff
path: root/scripts/kconfig/tests
diff options
context:
space:
mode:
authorKees Cook <kees@kernel.org>2026-09-18 17:59:25 -0700
committerNathan Chancellor <nathan@kernel.org>2026-09-24 11:37:05 -0400
commit651fb291c4c86b7be96720b129ef4619fbcef53c (patch)
tree08c731ff40d734d57dbef71692e0c9161ecc9a2d /scripts/kconfig/tests
parent9f01cd3a324390b235c67ae4ca3d35085369ebdd (diff)
downloadlinux-next-651fb291c4c86b7be96720b129ef4619fbcef53c.tar.gz
linux-next-651fb291c4c86b7be96720b129ef4619fbcef53c.zip
kconfig: Add "def_string", "def_int" and "def_hex"
Kconfig has offered "def_bool" and "def_tristate" as a shorthand for a type definition plus a default since before the git era, but has never offered the equivalent for the other three types. Conor Dooley ran into this gap[1] when fixing a symbol that had been given the wrong type: Unfortunately, there is no such thing as "def_string", but in this case we can use "default" to propagate the value of ... Nothing in the grammar requires the restriction. The rule that consumes a default is already type agnostic. Add the three missing types. No changes are needed to existing diagnostics. E.g. declaring a symbol "bool" and then assigning it with "def_string" still reports warning: ignoring type redefinition of 'CONFLICT' from 'bool' to 'string' Added tests for the types. Build tested ARCH=x86_64 with GCC 16.2.0. Tests pass with "make testconfig". Link: https://lore.kernel.org/all/20230111104848.2088516-1-conor.dooley@microchip.com/ [1] Assisted-by: LLM Signed-off-by: Kees Cook <kees@kernel.org> Tested-by: Julian Braha <julianbraha@gmail.com> Reviewed-by: Julian Braha <julianbraha@gmail.com> Link: https://patch.msgid.link/20260919005929.4077729-1-kees@kernel.org Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Diffstat (limited to 'scripts/kconfig/tests')
-rw-r--r--scripts/kconfig/tests/def_type/Kconfig28
-rw-r--r--scripts/kconfig/tests/def_type/__init__.py18
-rw-r--r--scripts/kconfig/tests/def_type/expected_guard_n9
-rw-r--r--scripts/kconfig/tests/def_type/expected_guard_y11
-rw-r--r--scripts/kconfig/tests/def_type/guard_n.config1
-rw-r--r--scripts/kconfig/tests/def_type/guard_y.config1
6 files changed, 68 insertions, 0 deletions
diff --git a/scripts/kconfig/tests/def_type/Kconfig b/scripts/kconfig/tests/def_type/Kconfig
new file mode 100644
index 000000000000..fbee37a63179
--- /dev/null
+++ b/scripts/kconfig/tests/def_type/Kconfig
@@ -0,0 +1,28 @@
+# SPDX-License-Identifier: GPL-2.0
+# The def_<type> shorthands: a type definition plus a default value.
+
+config MODULES
+ bool "Enable loadable module support"
+ modules
+ default y
+
+config GUARD
+ bool "Guard symbol"
+
+config DEF_BOOL
+ def_bool GUARD
+
+config DEF_TRISTATE
+ def_tristate m if GUARD
+
+config DEF_STRING
+ def_string "guarded" if GUARD
+ default "fallback"
+
+config DEF_INT
+ def_int 64 if GUARD
+ default 32
+
+config DEF_HEX
+ def_hex 0xdead if GUARD
+ default 0x0
diff --git a/scripts/kconfig/tests/def_type/__init__.py b/scripts/kconfig/tests/def_type/__init__.py
new file mode 100644
index 000000000000..1ebaf5da07c8
--- /dev/null
+++ b/scripts/kconfig/tests/def_type/__init__.py
@@ -0,0 +1,18 @@
+# SPDX-License-Identifier: GPL-2.0
+"""
+Set a symbol's type and its default value in one line.
+
+"def_bool", "def_tristate", "def_string", "def_int" and "def_hex" are
+shorthand for a type definition plus a "default" property. Check that
+each one sets the type, and that an "if" on the shorthand does not
+disturb the usual default cascade: the shorthand is only the first arm
+of the list, so a later "default" still applies when its condition is
+not met.
+"""
+
+def test(conf):
+ assert conf.olddefconfig(dot_config='guard_y.config') == 0
+ assert conf.config_matches('expected_guard_y')
+
+ assert conf.olddefconfig(dot_config='guard_n.config') == 0
+ assert conf.config_matches('expected_guard_n')
diff --git a/scripts/kconfig/tests/def_type/expected_guard_n b/scripts/kconfig/tests/def_type/expected_guard_n
new file mode 100644
index 000000000000..14719720a8b9
--- /dev/null
+++ b/scripts/kconfig/tests/def_type/expected_guard_n
@@ -0,0 +1,9 @@
+#
+# Automatically generated file; DO NOT EDIT.
+# Main menu
+#
+CONFIG_MODULES=y
+# CONFIG_GUARD is not set
+CONFIG_DEF_STRING="fallback"
+CONFIG_DEF_INT=32
+CONFIG_DEF_HEX=0x0
diff --git a/scripts/kconfig/tests/def_type/expected_guard_y b/scripts/kconfig/tests/def_type/expected_guard_y
new file mode 100644
index 000000000000..b2844072d0b8
--- /dev/null
+++ b/scripts/kconfig/tests/def_type/expected_guard_y
@@ -0,0 +1,11 @@
+#
+# Automatically generated file; DO NOT EDIT.
+# Main menu
+#
+CONFIG_MODULES=y
+CONFIG_GUARD=y
+CONFIG_DEF_BOOL=y
+CONFIG_DEF_TRISTATE=m
+CONFIG_DEF_STRING="guarded"
+CONFIG_DEF_INT=64
+CONFIG_DEF_HEX=0xdead
diff --git a/scripts/kconfig/tests/def_type/guard_n.config b/scripts/kconfig/tests/def_type/guard_n.config
new file mode 100644
index 000000000000..ed9ad6c1a2d4
--- /dev/null
+++ b/scripts/kconfig/tests/def_type/guard_n.config
@@ -0,0 +1 @@
+# CONFIG_GUARD is not set
diff --git a/scripts/kconfig/tests/def_type/guard_y.config b/scripts/kconfig/tests/def_type/guard_y.config
new file mode 100644
index 000000000000..afe35b084542
--- /dev/null
+++ b/scripts/kconfig/tests/def_type/guard_y.config
@@ -0,0 +1 @@
+CONFIG_GUARD=y