kbuild: Warn on selecting symbols with unmet direct dependencies
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / scripts / kconfig / menu.c
blob11799894f3bda59425415545b9533db72f70b819
1 /*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
6 #include <stdlib.h>
7 #include <string.h>
9 #define LKC_DIRECT_LINK
10 #include "lkc.h"
12 static const char nohelp_text[] = N_(
13 "There is no help available for this kernel option.\n");
15 struct menu rootmenu;
16 static struct menu **last_entry_ptr;
18 struct file *file_list;
19 struct file *current_file;
21 void menu_warn(struct menu *menu, const char *fmt, ...)
23 va_list ap;
24 va_start(ap, fmt);
25 fprintf(stderr, "%s:%d:warning: ", menu->file->name, menu->lineno);
26 vfprintf(stderr, fmt, ap);
27 fprintf(stderr, "\n");
28 va_end(ap);
31 static void prop_warn(struct property *prop, const char *fmt, ...)
33 va_list ap;
34 va_start(ap, fmt);
35 fprintf(stderr, "%s:%d:warning: ", prop->file->name, prop->lineno);
36 vfprintf(stderr, fmt, ap);
37 fprintf(stderr, "\n");
38 va_end(ap);
41 void _menu_init(void)
43 current_entry = current_menu = &rootmenu;
44 last_entry_ptr = &rootmenu.list;
47 void menu_add_entry(struct symbol *sym)
49 struct menu *menu;
51 menu = malloc(sizeof(*menu));
52 memset(menu, 0, sizeof(*menu));
53 menu->sym = sym;
54 menu->parent = current_menu;
55 menu->file = current_file;
56 menu->lineno = zconf_lineno();
58 *last_entry_ptr = menu;
59 last_entry_ptr = &menu->next;
60 current_entry = menu;
63 void menu_end_entry(void)
67 struct menu *menu_add_menu(void)
69 menu_end_entry();
70 last_entry_ptr = &current_entry->list;
71 return current_menu = current_entry;
74 void menu_end_menu(void)
76 last_entry_ptr = &current_menu->next;
77 current_menu = current_menu->parent;
80 static struct expr *menu_check_dep(struct expr *e)
82 if (!e)
83 return e;
85 switch (e->type) {
86 case E_NOT:
87 e->left.expr = menu_check_dep(e->left.expr);
88 break;
89 case E_OR:
90 case E_AND:
91 e->left.expr = menu_check_dep(e->left.expr);
92 e->right.expr = menu_check_dep(e->right.expr);
93 break;
94 case E_SYMBOL:
95 /* change 'm' into 'm' && MODULES */
96 if (e->left.sym == &symbol_mod)
97 return expr_alloc_and(e, expr_alloc_symbol(modules_sym));
98 break;
99 default:
100 break;
102 return e;
105 void menu_add_dep(struct expr *dep)
107 current_entry->dep = expr_alloc_and(current_entry->dep, menu_check_dep(dep));
108 current_entry->dir_dep = current_entry->dep;
111 void menu_set_type(int type)
113 struct symbol *sym = current_entry->sym;
115 if (sym->type == type)
116 return;
117 if (sym->type == S_UNKNOWN) {
118 sym->type = type;
119 return;
121 menu_warn(current_entry, "type of '%s' redefined from '%s' to '%s'",
122 sym->name ? sym->name : "<choice>",
123 sym_type_name(sym->type), sym_type_name(type));
126 struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
128 struct property *prop = prop_alloc(type, current_entry->sym);
130 prop->menu = current_entry;
131 prop->expr = expr;
132 prop->visible.expr = menu_check_dep(dep);
134 if (prompt) {
135 if (isspace(*prompt)) {
136 prop_warn(prop, "leading whitespace ignored");
137 while (isspace(*prompt))
138 prompt++;
140 if (current_entry->prompt)
141 prop_warn(prop, "prompt redefined");
142 current_entry->prompt = prop;
144 prop->text = prompt;
146 return prop;
149 struct property *menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
151 return menu_add_prop(type, prompt, NULL, dep);
154 void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
156 menu_add_prop(type, NULL, expr, dep);
159 void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
161 menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
164 void menu_add_option(int token, char *arg)
166 struct property *prop;
168 switch (token) {
169 case T_OPT_MODULES:
170 prop = prop_alloc(P_DEFAULT, modules_sym);
171 prop->expr = expr_alloc_symbol(current_entry->sym);
172 break;
173 case T_OPT_DEFCONFIG_LIST:
174 if (!sym_defconfig_list)
175 sym_defconfig_list = current_entry->sym;
176 else if (sym_defconfig_list != current_entry->sym)
177 zconf_error("trying to redefine defconfig symbol");
178 break;
179 case T_OPT_ENV:
180 prop_add_env(arg);
181 break;
185 static int menu_range_valid_sym(struct symbol *sym, struct symbol *sym2)
187 return sym2->type == S_INT || sym2->type == S_HEX ||
188 (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
191 static void sym_check_prop(struct symbol *sym)
193 struct property *prop;
194 struct symbol *sym2;
195 for (prop = sym->prop; prop; prop = prop->next) {
196 switch (prop->type) {
197 case P_DEFAULT:
198 if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
199 prop->expr->type != E_SYMBOL)
200 prop_warn(prop,
201 "default for config symbol '%s'"
202 " must be a single symbol", sym->name);
203 break;
204 case P_SELECT:
205 sym2 = prop_get_symbol(prop);
206 if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
207 prop_warn(prop,
208 "config symbol '%s' uses select, but is "
209 "not boolean or tristate", sym->name);
210 else if (sym2->type != S_UNKNOWN &&
211 sym2->type != S_BOOLEAN &&
212 sym2->type != S_TRISTATE)
213 prop_warn(prop,
214 "'%s' has wrong type. 'select' only "
215 "accept arguments of boolean and "
216 "tristate type", sym2->name);
217 break;
218 case P_RANGE:
219 if (sym->type != S_INT && sym->type != S_HEX)
220 prop_warn(prop, "range is only allowed "
221 "for int or hex symbols");
222 if (!menu_range_valid_sym(sym, prop->expr->left.sym) ||
223 !menu_range_valid_sym(sym, prop->expr->right.sym))
224 prop_warn(prop, "range is invalid");
225 break;
226 default:
232 void menu_finalize(struct menu *parent)
234 struct menu *menu, *last_menu;
235 struct symbol *sym;
236 struct property *prop;
237 struct expr *parentdep, *basedep, *dep, *dep2, **ep;
239 sym = parent->sym;
240 if (parent->list) {
241 if (sym && sym_is_choice(sym)) {
242 if (sym->type == S_UNKNOWN) {
243 /* find the first choice value to find out choice type */
244 current_entry = parent;
245 for (menu = parent->list; menu; menu = menu->next) {
246 if (menu->sym && menu->sym->type != S_UNKNOWN) {
247 menu_set_type(menu->sym->type);
248 break;
252 /* set the type of the remaining choice values */
253 for (menu = parent->list; menu; menu = menu->next) {
254 current_entry = menu;
255 if (menu->sym && menu->sym->type == S_UNKNOWN)
256 menu_set_type(sym->type);
258 parentdep = expr_alloc_symbol(sym);
259 } else if (parent->prompt)
260 parentdep = parent->prompt->visible.expr;
261 else
262 parentdep = parent->dep;
264 for (menu = parent->list; menu; menu = menu->next) {
265 basedep = expr_transform(menu->dep);
266 basedep = expr_alloc_and(expr_copy(parentdep), basedep);
267 basedep = expr_eliminate_dups(basedep);
268 menu->dep = basedep;
269 if (menu->sym)
270 prop = menu->sym->prop;
271 else
272 prop = menu->prompt;
273 for (; prop; prop = prop->next) {
274 if (prop->menu != menu)
275 continue;
276 dep = expr_transform(prop->visible.expr);
277 dep = expr_alloc_and(expr_copy(basedep), dep);
278 dep = expr_eliminate_dups(dep);
279 if (menu->sym && menu->sym->type != S_TRISTATE)
280 dep = expr_trans_bool(dep);
281 prop->visible.expr = dep;
282 if (prop->type == P_SELECT) {
283 struct symbol *es = prop_get_symbol(prop);
284 es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
285 expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
289 for (menu = parent->list; menu; menu = menu->next)
290 menu_finalize(menu);
291 } else if (sym) {
292 /* ignore inherited dependencies for dir_dep */
293 sym->dir_dep.expr = expr_transform(expr_copy(parent->dir_dep));
294 sym->dir_dep.expr = expr_eliminate_dups(sym->dir_dep.expr);
296 basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
297 basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
298 basedep = expr_eliminate_dups(expr_transform(basedep));
299 last_menu = NULL;
300 for (menu = parent->next; menu; menu = menu->next) {
301 dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
302 if (!expr_contains_symbol(dep, sym))
303 break;
304 if (expr_depends_symbol(dep, sym))
305 goto next;
306 dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
307 dep = expr_eliminate_dups(expr_transform(dep));
308 dep2 = expr_copy(basedep);
309 expr_eliminate_eq(&dep, &dep2);
310 expr_free(dep);
311 if (!expr_is_yes(dep2)) {
312 expr_free(dep2);
313 break;
315 expr_free(dep2);
316 next:
317 menu_finalize(menu);
318 menu->parent = parent;
319 last_menu = menu;
321 if (last_menu) {
322 parent->list = parent->next;
323 parent->next = last_menu->next;
324 last_menu->next = NULL;
327 for (menu = parent->list; menu; menu = menu->next) {
328 if (sym && sym_is_choice(sym) &&
329 menu->sym && !sym_is_choice_value(menu->sym)) {
330 current_entry = menu;
331 menu->sym->flags |= SYMBOL_CHOICEVAL;
332 if (!menu->prompt)
333 menu_warn(menu, "choice value must have a prompt");
334 for (prop = menu->sym->prop; prop; prop = prop->next) {
335 if (prop->type == P_DEFAULT)
336 prop_warn(prop, "defaults for choice "
337 "values not supported");
338 if (prop->menu == menu)
339 continue;
340 if (prop->type == P_PROMPT &&
341 prop->menu->parent->sym != sym)
342 prop_warn(prop, "choice value used outside its choice group");
344 /* Non-tristate choice values of tristate choices must
345 * depend on the choice being set to Y. The choice
346 * values' dependencies were propagated to their
347 * properties above, so the change here must be re-
348 * propagated.
350 if (sym->type == S_TRISTATE && menu->sym->type != S_TRISTATE) {
351 basedep = expr_alloc_comp(E_EQUAL, sym, &symbol_yes);
352 menu->dep = expr_alloc_and(basedep, menu->dep);
353 for (prop = menu->sym->prop; prop; prop = prop->next) {
354 if (prop->menu != menu)
355 continue;
356 prop->visible.expr = expr_alloc_and(expr_copy(basedep),
357 prop->visible.expr);
360 menu_add_symbol(P_CHOICE, sym, NULL);
361 prop = sym_get_choice_prop(sym);
362 for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
364 *ep = expr_alloc_one(E_LIST, NULL);
365 (*ep)->right.sym = menu->sym;
367 if (menu->list && (!menu->prompt || !menu->prompt->text)) {
368 for (last_menu = menu->list; ; last_menu = last_menu->next) {
369 last_menu->parent = parent;
370 if (!last_menu->next)
371 break;
373 last_menu->next = menu->next;
374 menu->next = menu->list;
375 menu->list = NULL;
379 if (sym && !(sym->flags & SYMBOL_WARNED)) {
380 if (sym->type == S_UNKNOWN)
381 menu_warn(parent, "config symbol defined without type");
383 if (sym_is_choice(sym) && !parent->prompt)
384 menu_warn(parent, "choice must have a prompt");
386 /* Check properties connected to this symbol */
387 sym_check_prop(sym);
388 sym->flags |= SYMBOL_WARNED;
391 if (sym && !sym_is_optional(sym) && parent->prompt) {
392 sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
393 expr_alloc_and(parent->prompt->visible.expr,
394 expr_alloc_symbol(&symbol_mod)));
398 bool menu_has_prompt(struct menu *menu)
400 if (!menu->prompt)
401 return false;
402 return true;
405 bool menu_is_visible(struct menu *menu)
407 struct menu *child;
408 struct symbol *sym;
409 tristate visible;
411 if (!menu->prompt)
412 return false;
414 sym = menu->sym;
415 if (sym) {
416 sym_calc_value(sym);
417 visible = menu->prompt->visible.tri;
418 } else
419 visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
421 if (visible != no)
422 return true;
424 if (!sym || sym_get_tristate_value(menu->sym) == no)
425 return false;
427 for (child = menu->list; child; child = child->next) {
428 if (menu_is_visible(child)) {
429 if (sym)
430 sym->flags |= SYMBOL_DEF_USER;
431 return true;
435 return false;
438 const char *menu_get_prompt(struct menu *menu)
440 if (menu->prompt)
441 return menu->prompt->text;
442 else if (menu->sym)
443 return menu->sym->name;
444 return NULL;
447 struct menu *menu_get_root_menu(struct menu *menu)
449 return &rootmenu;
452 struct menu *menu_get_parent_menu(struct menu *menu)
454 enum prop_type type;
456 for (; menu != &rootmenu; menu = menu->parent) {
457 type = menu->prompt ? menu->prompt->type : 0;
458 if (type == P_MENU)
459 break;
461 return menu;
464 bool menu_has_help(struct menu *menu)
466 return menu->help != NULL;
469 const char *menu_get_help(struct menu *menu)
471 if (menu->help)
472 return menu->help;
473 else
474 return "";
477 static void get_prompt_str(struct gstr *r, struct property *prop)
479 int i, j;
480 struct menu *submenu[8], *menu;
482 str_printf(r, _("Prompt: %s\n"), _(prop->text));
483 str_printf(r, _(" Defined at %s:%d\n"), prop->menu->file->name,
484 prop->menu->lineno);
485 if (!expr_is_yes(prop->visible.expr)) {
486 str_append(r, _(" Depends on: "));
487 expr_gstr_print(prop->visible.expr, r);
488 str_append(r, "\n");
490 menu = prop->menu->parent;
491 for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent)
492 submenu[i++] = menu;
493 if (i > 0) {
494 str_printf(r, _(" Location:\n"));
495 for (j = 4; --i >= 0; j += 2) {
496 menu = submenu[i];
497 str_printf(r, "%*c-> %s", j, ' ', _(menu_get_prompt(menu)));
498 if (menu->sym) {
499 str_printf(r, " (%s [=%s])", menu->sym->name ?
500 menu->sym->name : _("<choice>"),
501 sym_get_string_value(menu->sym));
503 str_append(r, "\n");
508 void get_symbol_str(struct gstr *r, struct symbol *sym)
510 bool hit;
511 struct property *prop;
513 if (sym && sym->name) {
514 str_printf(r, "Symbol: %s [=%s]\n", sym->name,
515 sym_get_string_value(sym));
516 str_printf(r, "Type : %s\n", sym_type_name(sym->type));
517 if (sym->type == S_INT || sym->type == S_HEX) {
518 prop = sym_get_range_prop(sym);
519 if (prop) {
520 str_printf(r, "Range : ");
521 expr_gstr_print(prop->expr, r);
522 str_append(r, "\n");
526 for_all_prompts(sym, prop)
527 get_prompt_str(r, prop);
528 hit = false;
529 for_all_properties(sym, prop, P_SELECT) {
530 if (!hit) {
531 str_append(r, " Selects: ");
532 hit = true;
533 } else
534 str_printf(r, " && ");
535 expr_gstr_print(prop->expr, r);
537 if (hit)
538 str_append(r, "\n");
539 if (sym->rev_dep.expr) {
540 str_append(r, _(" Selected by: "));
541 expr_gstr_print(sym->rev_dep.expr, r);
542 str_append(r, "\n");
544 str_append(r, "\n\n");
547 struct gstr get_relations_str(struct symbol **sym_arr)
549 struct symbol *sym;
550 struct gstr res = str_new();
551 int i;
553 for (i = 0; sym_arr && (sym = sym_arr[i]); i++)
554 get_symbol_str(&res, sym);
555 if (!i)
556 str_append(&res, _("No matches found.\n"));
557 return res;
561 void menu_get_ext_help(struct menu *menu, struct gstr *help)
563 struct symbol *sym = menu->sym;
565 if (menu_has_help(menu)) {
566 if (sym->name) {
567 str_printf(help, "CONFIG_%s:\n\n", sym->name);
568 str_append(help, _(menu_get_help(menu)));
569 str_append(help, "\n");
571 } else {
572 str_append(help, nohelp_text);
574 if (sym)
575 get_symbol_str(help, sym);