Skip to contentSkip to conversation

Documentation: warn against using int, hex, string options as expressions in Kconfig

PATCH In review Julian Braha 4 participants 5 messages Jul 30 – Jul 31

Subsystems (3)
lore
Share & tools
lore

Julian Braha

author reviewer
#0 lore raw

PATCH 1/1 Documentation: warn against using int, hex, string options as expressions in Kconfig

I've been working on type checking Kconfig, and found some instances of dead code due to conditions that always evaluate to false.

For example:

config FOO
int

config BAR
bool
default 'y' if FOO

This 'if FOO' will always be false, regardless of FOO's value (even when FOO=1). The same goes for strings and hex.

See also this related patch:
https://lore.kernel.org/all/[email protected]/

Let's warn users against this usage.

---
 Documentation/kbuild/kconfig-language.rst | 11 +++++++++++
 1 file changed, 11 insertions(+)
Documentation/kbuild/kconfig-language.rst +11 -0
index ab2822d7ca1c..aee84b294a56 100644
@@ -320,6 +320,17 @@ An expression can have a value of 'n', 'm' or 'y' (or 0, 1, 2
 respectively for calculations). A menu entry becomes visible when its
 expression evaluates to 'm' or 'y'.
 
+Note that options of int, hex, or string type should not be used directly
+as expressions; these will always evaluate to 'n'. Instead, to check if
+one of these options is in use, consider using one of these heuristics::
+
+  <symbol> != 0
+  <symbol> != 0x0
+  <symbol> != ""
+
+because int, hex, and string options will take these values of '0', '0x0',
+and the empty string, respectively, when the dependencies are unmet.
+
 There are two types of symbols: constant and non-constant symbols.
 Non-constant symbols are the most common ones and are defined with the
 'config' statement. Non-constant symbols consist entirely of alphanumeric
Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines

I've been working on type checking Kconfig, and found some instances of dead code due to conditions that always evaluate to false.

For example:

config FOO
int

config BAR
bool
default 'y' if FOO

Drop this quoting of y for clarity?

Quoted 2 lines

This 'if FOO' will always be false, regardless of FOO's value (even when FOO=1). The same goes for strings and hex.

Hmmm, it might be kind of nice to either:

1. Warn about this in Kconfig directly (if possible)
2. Support this usage in Kconfig how one would intuitively expect from either C or Python where symbols with values of 0, 0x0, and '' would be treated as false and every other value set as true.

Quoted 4 lines

See also this related patch:
https://lore.kernel.org/all/[email protected]/

Let's warn users against this usage.

That said, until either of those potentially happen, this patch seems fine.

Signature · 2 lines
Cheers,
Nathan
author reviewer
#2 lore raw
Quoted 1 line · Nathan Chancellor

Drop this quoting of y for clarity?

Sure, I'll send a v2.

Quoted 8 lines
Quoted 2 lines

This 'if FOO' will always be false, regardless of FOO's value (even when FOO=1). The same goes for strings and hex.

Hmmm, it might be kind of nice to either:

1. Warn about this in Kconfig directly (if possible)
2. Support this usage in Kconfig how one would intuitively expect from either C or Python where symbols with values of 0, 0x0, and '' would be treated as false and every other value set as true.

Agreed, an interpreter change for this is on my roadmap. Though I was also considering to make this error out.

Would be good to get some more feedback from others on which approach is preferred (Jani, Arnd?)

Quoted 2 lines

That said, until either of those potentially happen, this patch seems fine.

Thanks for your review!

- Julian Braha

#3 lore raw
Quoted 10 lines · Julian Braha
Quoted 8 lines · Nathan Chancellor
Quoted 2 lines

This 'if FOO' will always be false, regardless of FOO's value (even when FOO=1). The same goes for strings and hex.

Hmmm, it might be kind of nice to either:

1. Warn about this in Kconfig directly (if possible)
2. Support this usage in Kconfig how one would intuitively expect from either C or Python where symbols with values of 0, 0x0, and '' would be treated as false and every other value set as true.

Quoted 5 lines

Agreed, an interpreter change for this is on my roadmap. Though I was also considering to make this error out.

Would be good to get some more feedback from others on which approach is preferred (Jani, Arnd?)

I'm fine with either of the two approaches, both seem better than to document the current behavior just to change that again later.

If it's going to cause a hard error, we have to be sure to fix all existing instances first, otherwise a warning would be better.

Arnd

reviewer
#4 lore raw
In reply to Julian Braha

Hi,
I haven't had time to really look at this yet, but...

Quoted 8 lines · Julian Braha

I've been working on type checking Kconfig, and found some instances of dead code due to conditions that always evaluate to false.

For example:

config FOO
int

Quoted 6 lines

config BAR
bool
default 'y' if FOO

This 'if FOO' will always be false, regardless of FOO's value (even when FOO=1). The same goes for strings and hex.

Does it help to say
default y if FOO > 0
or

	default y if FOO != 0

Oh, I think that's what you suggested below. :)
Sorry, I just read that part.

This is unfortunate IMO but yes, let's at least document it. Thanks.

Quoted 32 lines

See also this related patch:
https://lore.kernel.org/all/[email protected]/

Let's warn users against this usage.

---
 Documentation/kbuild/kconfig-language.rst | 11 +++++++++++
 1 file changed, 11 insertions(+)
Documentation/kbuild/kconfig-language.rst +11 -0
index ab2822d7ca1c..aee84b294a56 100644
@@ -320,6 +320,17 @@ An expression can have a value of 'n', 'm' or 'y' (or 0, 1, 2
 respectively for calculations). A menu entry becomes visible when its
 expression evaluates to 'm' or 'y'.
 
+Note that options of int, hex, or string type should not be used directly
+as expressions; these will always evaluate to 'n'. Instead, to check if
+one of these options is in use, consider using one of these heuristics::
+
+  <symbol> != 0
+  <symbol> != 0x0
+  <symbol> != ""
+
+because int, hex, and string options will take these values of '0', '0x0',
+and the empty string, respectively, when the dependencies are unmet.
+
 There are two types of symbols: constant and non-constant symbols.
 Non-constant symbols are the most common ones and are defined with the
 'config' statement. Non-constant symbols consist entirely of alphanumeric
Signature · 1 line
~Randy