kconfig: Warn if choice default is not in choice
[carl9170fw.git] / config / menu.c
index f3bffa309333061ed5d852e7c00132107017add5..ce88de89f146e3ce1c6d980130ff93bae1b992ce 100644 (file)
@@ -119,12 +119,13 @@ void menu_set_type(int type)
                sym->type = type;
                return;
        }
-       menu_warn(current_entry, "type of '%s' redefined from '%s' to '%s'",
-           sym->name ? sym->name : "<choice>",
-           sym_type_name(sym->type), sym_type_name(type));
+       menu_warn(current_entry,
+               "ignoring type redefinition of '%s' from '%s' to '%s'",
+               sym->name ? sym->name : "<choice>",
+               sym_type_name(sym->type), sym_type_name(type));
 }
 
-struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
+static struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
 {
        struct property *prop = prop_alloc(type, current_entry->sym);
 
@@ -146,11 +147,24 @@ struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *e
                        struct menu *menu = current_entry;
 
                        while ((menu = menu->parent) != NULL) {
+                               struct expr *dup_expr;
+
                                if (!menu->visibility)
                                        continue;
+                               /*
+                                * Do not add a reference to the
+                                * menu's visibility expression but
+                                * use a copy of it.  Otherwise the
+                                * expression reduction functions
+                                * will modify expressions that have
+                                * multiple references which can
+                                * cause unwanted side effects.
+                                */
+                               dup_expr = expr_copy(menu->visibility);
+
                                prop->visible.expr
                                        = expr_alloc_and(prop->visible.expr,
-                                                        menu->visibility);
+                                                        dup_expr);
                        }
                }
 
@@ -184,12 +198,15 @@ void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
 
 void menu_add_option(int token, char *arg)
 {
-       struct property *prop;
-
        switch (token) {
        case T_OPT_MODULES:
-               prop = prop_alloc(P_DEFAULT, modules_sym);
-               prop->expr = expr_alloc_symbol(current_entry->sym);
+               if (modules_sym)
+                       zconf_error("symbol '%s' redefines option 'modules'"
+                                   " already defined by symbol '%s'",
+                                   current_entry->sym->name,
+                                   modules_sym->name
+                                   );
+               modules_sym = current_entry->sym;
                break;
        case T_OPT_DEFCONFIG_LIST:
                if (!sym_defconfig_list)
@@ -200,6 +217,9 @@ void menu_add_option(int token, char *arg)
        case T_OPT_ENV:
                prop_add_env(arg);
                break;
+       case T_OPT_ALLNOCONFIG_Y:
+               current_entry->sym->flags |= SYMBOL_ALLNOCONFIG_Y;
+               break;
        }
 }
 
@@ -213,6 +233,8 @@ static void sym_check_prop(struct symbol *sym)
 {
        struct property *prop;
        struct symbol *sym2;
+       char *use;
+
        for (prop = sym->prop; prop; prop = prop->next) {
                switch (prop->type) {
                case P_DEFAULT:
@@ -230,25 +252,37 @@ 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:
+                       use = prop->type == P_SELECT ? "select" : "imply";
                        sym2 = prop_get_symbol(prop);
                        if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
                                prop_warn(prop,
-                                   "config symbol '%s' uses select, but is "
-                                   "not boolean or tristate", sym->name);
+                                   "config symbol '%s' uses %s, but is "
+                                   "not boolean or tristate", sym->name, use);
                        else if (sym2->type != S_UNKNOWN &&
-                                sym2->type != S_BOOLEAN &&
-                                sym2->type != S_TRISTATE)
+                                sym2->type != S_BOOLEAN &&
+                                sym2->type != S_TRISTATE)
                                prop_warn(prop,
-                                   "'%s' has wrong type. 'select' only "
+                                   "'%s' has wrong type. '%s' only "
                                    "accept arguments of boolean and "
-                                   "tristate type", sym2->name);
+                                   "tristate type", sym2->name, use);
                        break;
                case P_RANGE:
                        if (sym->type != S_INT && sym->type != S_HEX)
                                prop_warn(prop, "range is only allowed "
-                                               "for int or hex symbols");
+                                               "for int or hex symbols");
                        if (!menu_validate_number(sym, prop->expr->left.sym) ||
                            !menu_validate_number(sym, prop->expr->right.sym))
                                prop_warn(prop, "range is invalid");
@@ -313,6 +347,10 @@ void menu_finalize(struct menu *parent)
                                        struct symbol *es = prop_get_symbol(prop);
                                        es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
                                                        expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
+                               } else if (prop->type == P_IMPLY) {
+                                       struct symbol *es = prop_get_symbol(prop);
+                                       es->implied.expr = expr_alloc_or(es->implied.expr,
+                                                       expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
                                }
                        }
                }
@@ -430,6 +468,22 @@ bool menu_has_prompt(struct menu *menu)
        return true;
 }
 
+/*
+ * Determine if a menu is empty.
+ * A menu is considered empty if it contains no or only
+ * invisible entries.
+ */
+bool menu_is_empty(struct menu *menu)
+{
+       struct menu *child;
+
+       for (child = menu->list; child; child = child->next) {
+               if (menu_is_visible(child))
+                       return(false);
+       }
+       return(true);
+}
+
 bool menu_is_visible(struct menu *menu)
 {
        struct menu *child;
@@ -441,7 +495,7 @@ bool menu_is_visible(struct menu *menu)
 
        if (menu->visibility) {
                if (expr_calc_value(menu->visibility) == no)
-                       return no;
+                       return false;
        }
 
        sym = menu->sym;
@@ -512,16 +566,9 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
 {
        int i, j;
        struct menu *submenu[8], *menu, *location = NULL;
-       struct jump_key *jump;
+       struct jump_key *jump = NULL;
 
        str_printf(r, _("Prompt: %s\n"), _(prop->text));
-       str_printf(r, _("  Defined at %s:%d\n"), prop->menu->file->name,
-               prop->menu->lineno);
-       if (!expr_is_yes(prop->visible.expr)) {
-               str_append(r, _("  Depends on: "));
-               expr_gstr_print(prop->visible.expr, r);
-               str_append(r, "\n");
-       }
        menu = prop->menu->parent;
        for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) {
                bool accessible = menu_is_visible(menu);
@@ -557,8 +604,8 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
                str_printf(r, _("  Location:\n"));
                for (j = 4; --i >= 0; j += 2) {
                        menu = submenu[i];
-                       if (head && location && menu == location)
-                               jump->offset = r->len - 1;
+                       if (jump && menu == location)
+                               jump->offset = strlen(r->s);
                        str_printf(r, "%*c-> %s", j, ' ',
                                   _(menu_get_prompt(menu)));
                        if (menu->sym) {
@@ -571,13 +618,42 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
        }
 }
 
+/*
+ * get property of type P_SYMBOL
+ */
+static struct property *get_symbol_prop(struct symbol *sym)
+{
+       struct property *prop = NULL;
+
+       for_all_properties(sym, prop, P_SYMBOL)
+               break;
+       return prop;
+}
+
+static void get_symbol_props_str(struct gstr *r, struct symbol *sym,
+                                enum prop_type tok, const char *prefix)
+{
+       bool hit = false;
+       struct property *prop;
+
+       for_all_properties(sym, prop, tok) {
+               if (!hit) {
+                       str_append(r, prefix);
+                       hit = true;
+               } else
+                       str_printf(r, " && ");
+               expr_gstr_print(prop->expr, r);
+       }
+       if (hit)
+               str_append(r, "\n");
+}
+
 /*
  * head is optional and may be NULL
  */
-void get_symbol_str(struct gstr *r, struct symbol *sym,
+static void get_symbol_str(struct gstr *r, struct symbol *sym,
                    struct list_head *head)
 {
-       bool hit;
        struct property *prop;
 
        if (sym && sym->name) {
@@ -595,22 +671,32 @@ void get_symbol_str(struct gstr *r, struct symbol *sym,
        }
        for_all_prompts(sym, prop)
                get_prompt_str(r, prop, head);
-       hit = false;
-       for_all_properties(sym, prop, P_SELECT) {
-               if (!hit) {
-                       str_append(r, "  Selects: ");
-                       hit = true;
-               } else
-                       str_printf(r, " && ");
-               expr_gstr_print(prop->expr, r);
+
+       prop = get_symbol_prop(sym);
+       if (prop) {
+               str_printf(r, _("  Defined at %s:%d\n"), prop->menu->file->name,
+                       prop->menu->lineno);
+               if (!expr_is_yes(prop->visible.expr)) {
+                       str_append(r, _("  Depends on: "));
+                       expr_gstr_print(prop->visible.expr, r);
+                       str_append(r, "\n");
+               }
        }
-       if (hit)
-               str_append(r, "\n");
+
+       get_symbol_props_str(r, sym, P_SELECT, _("  Selects: "));
        if (sym->rev_dep.expr) {
                str_append(r, _("  Selected by: "));
                expr_gstr_print(sym->rev_dep.expr, r);
                str_append(r, "\n");
        }
+
+       get_symbol_props_str(r, sym, P_IMPLY, _("  Implies: "));
+       if (sym->implied.expr) {
+               str_append(r, _("  Implied by: "));
+               expr_gstr_print(sym->implied.expr, r);
+               str_append(r, "\n");
+       }
+
        str_append(r, "\n\n");
 }