Skip to contentSkip to conversation

kconfig: Warn if choice default is not in choice

PATCH In review Ulf Magnusson 2 participants 4 messages Oct 3, 2017 – Dec 7, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kconfig: Warn if choice default is not in choice

This will catch mistakes like in the following real-world example, where a "CONFIG_" prefix snuck in, making an undefined symbol the default:

	choice
		prompt "Compiler optimization level"
		default CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE

	config CC_OPTIMIZE_FOR_PERFORMANCE
		...

	config CC_OPTIMIZE_FOR_SIZE
		...

	endchoice

This now prints the following warning:

init/Kconfig:1036:warning: choice default symbol 'CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE' is not contained in the choice

Cases where the default symbol belongs to the wrong choice are also detected.

(The mistake is harmless here: Since the default symbol is not visible, the choice falls back on using the first visible symbol as the default, which is CC_OPTIMIZE_FOR_PERFORMANCE, as intended.)

Discovered while playing around with Kconfiglib
(https://github.com/ulfalizer/Kconfiglib).

---
 scripts/kconfig/menu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
scripts/kconfig/menu.c +10 -0
index e935793..ce88de8 100644
@@ -252,6 +252,16 @@ static void sym_check_prop(struct symbol *sym)
 					    "'%s': number is invalid",
 					    sym->name);
 			}
+			if (sym_is_choice(sym)) {
+				struct property *choice_prop =
+					sym_get_choice_prop(sym2);
+
+				if (!choice_prop ||
+				    prop_get_symbol(choice_prop) != sym)
+					prop_warn(prop,
+						  "choice default symbol '%s' is not contained in the choice",
+						  sym2->name);
+			}
 			break;
 		case P_SELECT:
 		case P_IMPLY:
Signature · 1 line
2.7.4
#1 lore raw
Quoted 56 lines · Ulf Magnusson

This will catch mistakes like in the following real-world example, where a "CONFIG_" prefix snuck in, making an undefined symbol the default:

        choice
                prompt "Compiler optimization level"
                default CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE

        config CC_OPTIMIZE_FOR_PERFORMANCE
                ...

        config CC_OPTIMIZE_FOR_SIZE
                ...

        endchoice

This now prints the following warning:

init/Kconfig:1036:warning: choice default symbol 'CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE' is not contained in the choice

Cases where the default symbol belongs to the wrong choice are also detected.

(The mistake is harmless here: Since the default symbol is not visible, the choice falls back on using the first visible symbol as the default, which is CC_OPTIMIZE_FOR_PERFORMANCE, as intended.)

Discovered while playing around with Kconfiglib
(https://github.com/ulfalizer/Kconfiglib).

---
 scripts/kconfig/menu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
scripts/kconfig/menu.c +10 -1
index e935793..ce88de8 100644
@@ -252,6 +252,16 @@ static void sym_check_prop(struct symbol *sym)
                                            "'%s': number is invalid",
                                            sym->name);
                        }
+                       if (sym_is_choice(sym)) {
+                               struct property *choice_prop =
+                                       sym_get_choice_prop(sym2);
+
+                               if (!choice_prop ||
+                                   prop_get_symbol(choice_prop) != sym)
+                                       prop_warn(prop,
+                                                 "choice default symbol '%s' is not contained in the choice",
+                                                 sym2->name);
+                       }
                        break;
                case P_SELECT:
                case P_IMPLY:
--
Quoted 2 lines

2.7.4

A patch to fix the compiler optimization level choice has been submitted as well:
https://lkml.org/lkml/2017/10/3/991

Cheers,
Ulf

#2 lore raw
Quoted 64 lines · Ulf Magnusson
Quoted 56 lines · Ulf Magnusson

This will catch mistakes like in the following real-world example, where a "CONFIG_" prefix snuck in, making an undefined symbol the default:

        choice
                prompt "Compiler optimization level"
                default CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE

        config CC_OPTIMIZE_FOR_PERFORMANCE
                ...

        config CC_OPTIMIZE_FOR_SIZE
                ...

        endchoice

This now prints the following warning:

init/Kconfig:1036:warning: choice default symbol 'CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE' is not contained in the choice

Cases where the default symbol belongs to the wrong choice are also detected.

(The mistake is harmless here: Since the default symbol is not visible, the choice falls back on using the first visible symbol as the default, which is CC_OPTIMIZE_FOR_PERFORMANCE, as intended.)

Discovered while playing around with Kconfiglib
(https://github.com/ulfalizer/Kconfiglib).

---
 scripts/kconfig/menu.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
scripts/kconfig/menu.c +10 -1
index e935793..ce88de8 100644
@@ -252,6 +252,16 @@ static void sym_check_prop(struct symbol *sym)
                                            "'%s': number is invalid",
                                            sym->name);
                        }
+                       if (sym_is_choice(sym)) {
+                               struct property *choice_prop =
+                                       sym_get_choice_prop(sym2);
+
+                               if (!choice_prop ||
+                                   prop_get_symbol(choice_prop) != sym)
+                                       prop_warn(prop,
+                                                 "choice default symbol '%s' is not contained in the choice",
+                                                 sym2->name);
+                       }
                        break;
                case P_SELECT:
                case P_IMPLY:
--
Quoted 2 lines

2.7.4

A patch to fix the compiler optimization level choice has been submitted as well:
https://lkml.org/lkml/2017/10/3/991

Quoted 2 lines

Cheers,
Ulf

Adding some CCs just because I forgot. No panic.

Cheers,
Ulf

#3 lore raw
In reply to Ulf Magnusson

2017-10-04 8:25 GMT+09:00 Ulf Magnusson <[email protected]>:

Quoted 26 lines

This will catch mistakes like in the following real-world example, where a "CONFIG_" prefix snuck in, making an undefined symbol the default:

        choice
                prompt "Compiler optimization level"
                default CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE

        config CC_OPTIMIZE_FOR_PERFORMANCE
                ...

        config CC_OPTIMIZE_FOR_SIZE
                ...

        endchoice

This now prints the following warning:

init/Kconfig:1036:warning: choice default symbol 'CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE' is not contained in the choice

Cases where the default symbol belongs to the wrong choice are also detected.

(The mistake is harmless here: Since the default symbol is not visible, the choice falls back on using the first visible symbol as the default, which is CC_OPTIMIZE_FOR_PERFORMANCE, as intended.)

Quoted 5 lines

Discovered while playing around with Kconfiglib
(https://github.com/ulfalizer/Kconfiglib).

---

Applied to linux-kbuild/kconfig.
Thanks!

Signature · 2 lines
Best Regards
Masahiro Yamada