aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/kconfig/symbol.c
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/kconfig/symbol.c')
-rw-r--r--scripts/kconfig/symbol.c343
1 files changed, 310 insertions, 33 deletions
diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c
index 2e7a048e0cfc..c0efe102d655 100644
--- a/scripts/kconfig/symbol.c
+++ b/scripts/kconfig/symbol.c
@@ -205,6 +205,16 @@ static void sym_calc_visibility(struct symbol *sym)
205 } 205 }
206 if (sym_is_choice_value(sym)) 206 if (sym_is_choice_value(sym))
207 return; 207 return;
208 /* defaulting to "yes" if no explicit "depends on" are given */
209 tri = yes;
210 if (sym->dir_dep.expr)
211 tri = expr_calc_value(sym->dir_dep.expr);
212 if (tri == mod)
213 tri = yes;
214 if (sym->dir_dep.tri != tri) {
215 sym->dir_dep.tri = tri;
216 sym_set_changed(sym);
217 }
208 tri = no; 218 tri = no;
209 if (sym->rev_dep.expr) 219 if (sym->rev_dep.expr)
210 tri = expr_calc_value(sym->rev_dep.expr); 220 tri = expr_calc_value(sym->rev_dep.expr);
@@ -216,44 +226,63 @@ static void sym_calc_visibility(struct symbol *sym)
216 } 226 }
217} 227}
218 228
219static struct symbol *sym_calc_choice(struct symbol *sym) 229/*
230 * Find the default symbol for a choice.
231 * First try the default values for the choice symbol
232 * Next locate the first visible choice value
233 * Return NULL if none was found
234 */
235struct symbol *sym_choice_default(struct symbol *sym)
220{ 236{
221 struct symbol *def_sym; 237 struct symbol *def_sym;
222 struct property *prop; 238 struct property *prop;
223 struct expr *e; 239 struct expr *e;
224 240
225 /* is the user choice visible? */
226 def_sym = sym->def[S_DEF_USER].val;
227 if (def_sym) {
228 sym_calc_visibility(def_sym);
229 if (def_sym->visible != no)
230 return def_sym;
231 }
232
233 /* any of the defaults visible? */ 241 /* any of the defaults visible? */
234 for_all_defaults(sym, prop) { 242 for_all_defaults(sym, prop) {
235 prop->visible.tri = expr_calc_value(prop->visible.expr); 243 prop->visible.tri = expr_calc_value(prop->visible.expr);
236 if (prop->visible.tri == no) 244 if (prop->visible.tri == no)
237 continue; 245 continue;
238 def_sym = prop_get_symbol(prop); 246 def_sym = prop_get_symbol(prop);
239 sym_calc_visibility(def_sym);
240 if (def_sym->visible != no) 247 if (def_sym->visible != no)
241 return def_sym; 248 return def_sym;
242 } 249 }
243 250
244 /* just get the first visible value */ 251 /* just get the first visible value */
245 prop = sym_get_choice_prop(sym); 252 prop = sym_get_choice_prop(sym);
246 expr_list_for_each_sym(prop->expr, e, def_sym) { 253 expr_list_for_each_sym(prop->expr, e, def_sym)
247 sym_calc_visibility(def_sym);
248 if (def_sym->visible != no) 254 if (def_sym->visible != no)
249 return def_sym; 255 return def_sym;
250 }
251 256
252 /* no choice? reset tristate value */ 257 /* failed to locate any defaults */
253 sym->curr.tri = no;
254 return NULL; 258 return NULL;
255} 259}
256 260
261static struct symbol *sym_calc_choice(struct symbol *sym)
262{
263 struct symbol *def_sym;
264 struct property *prop;
265 struct expr *e;
266
267 /* first calculate all choice values' visibilities */
268 prop = sym_get_choice_prop(sym);
269 expr_list_for_each_sym(prop->expr, e, def_sym)
270 sym_calc_visibility(def_sym);
271
272 /* is the user choice visible? */
273 def_sym = sym->def[S_DEF_USER].val;
274 if (def_sym && def_sym->visible != no)
275 return def_sym;
276
277 def_sym = sym_choice_default(sym);
278
279 if (def_sym == NULL)
280 /* no choice? reset tristate value */
281 sym->curr.tri = no;
282
283 return def_sym;
284}
285
257void sym_calc_value(struct symbol *sym) 286void sym_calc_value(struct symbol *sym)
258{ 287{
259 struct symbol_value newval, oldval; 288 struct symbol_value newval, oldval;
@@ -321,6 +350,14 @@ void sym_calc_value(struct symbol *sym)
321 } 350 }
322 } 351 }
323 calc_newval: 352 calc_newval:
353 if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
354 fprintf(stderr, "warning: (");
355 expr_fprint(sym->rev_dep.expr, stderr);
356 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
357 sym->name);
358 expr_fprint(sym->dir_dep.expr, stderr);
359 fprintf(stderr, ")\n");
360 }
324 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri); 361 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
325 } 362 }
326 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN) 363 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
@@ -365,12 +402,13 @@ void sym_calc_value(struct symbol *sym)
365 402
366 if (sym_is_choice(sym)) { 403 if (sym_is_choice(sym)) {
367 struct symbol *choice_sym; 404 struct symbol *choice_sym;
368 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
369 405
370 prop = sym_get_choice_prop(sym); 406 prop = sym_get_choice_prop(sym);
371 expr_list_for_each_sym(prop->expr, e, choice_sym) { 407 expr_list_for_each_sym(prop->expr, e, choice_sym) {
372 choice_sym->flags |= flags; 408 if ((sym->flags & SYMBOL_WRITE) &&
373 if (flags & SYMBOL_CHANGED) 409 choice_sym->visible != no)
410 choice_sym->flags |= SYMBOL_WRITE;
411 if (sym->flags & SYMBOL_CHANGED)
374 sym_set_changed(choice_sym); 412 sym_set_changed(choice_sym);
375 } 413 }
376 } 414 }
@@ -623,6 +661,80 @@ bool sym_set_string_value(struct symbol *sym, const char *newval)
623 return true; 661 return true;
624} 662}
625 663
664/*
665 * Find the default value associated to a symbol.
666 * For tristate symbol handle the modules=n case
667 * in which case "m" becomes "y".
668 * If the symbol does not have any default then fallback
669 * to the fixed default values.
670 */
671const char *sym_get_string_default(struct symbol *sym)
672{
673 struct property *prop;
674 struct symbol *ds;
675 const char *str;
676 tristate val;
677
678 sym_calc_visibility(sym);
679 sym_calc_value(modules_sym);
680 val = symbol_no.curr.tri;
681 str = symbol_empty.curr.val;
682
683 /* If symbol has a default value look it up */
684 prop = sym_get_default_prop(sym);
685 if (prop != NULL) {
686 switch (sym->type) {
687 case S_BOOLEAN:
688 case S_TRISTATE:
689 /* The visibility imay limit the value from yes => mod */
690 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
691 break;
692 default:
693 /*
694 * The following fails to handle the situation
695 * where a default value is further limited by
696 * the valid range.
697 */
698 ds = prop_get_symbol(prop);
699 if (ds != NULL) {
700 sym_calc_value(ds);
701 str = (const char *)ds->curr.val;
702 }
703 }
704 }
705
706 /* Handle select statements */
707 val = EXPR_OR(val, sym->rev_dep.tri);
708
709 /* transpose mod to yes if modules are not enabled */
710 if (val == mod)
711 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
712 val = yes;
713
714 /* transpose mod to yes if type is bool */
715 if (sym->type == S_BOOLEAN && val == mod)
716 val = yes;
717
718 switch (sym->type) {
719 case S_BOOLEAN:
720 case S_TRISTATE:
721 switch (val) {
722 case no: return "n";
723 case mod: return "m";
724 case yes: return "y";
725 }
726 case S_INT:
727 case S_HEX:
728 return str;
729 case S_STRING:
730 return str;
731 case S_OTHER:
732 case S_UNKNOWN:
733 break;
734 }
735 return "";
736}
737
626const char *sym_get_string_value(struct symbol *sym) 738const char *sym_get_string_value(struct symbol *sym)
627{ 739{
628 tristate val; 740 tristate val;
@@ -728,6 +840,55 @@ struct symbol *sym_find(const char *name)
728 return symbol; 840 return symbol;
729} 841}
730 842
843/*
844 * Expand symbol's names embedded in the string given in argument. Symbols'
845 * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
846 * the empty string.
847 */
848const char *sym_expand_string_value(const char *in)
849{
850 const char *src;
851 char *res;
852 size_t reslen;
853
854 reslen = strlen(in) + 1;
855 res = malloc(reslen);
856 res[0] = '\0';
857
858 while ((src = strchr(in, '$'))) {
859 char *p, name[SYMBOL_MAXLENGTH];
860 const char *symval = "";
861 struct symbol *sym;
862 size_t newlen;
863
864 strncat(res, in, src - in);
865 src++;
866
867 p = name;
868 while (isalnum(*src) || *src == '_')
869 *p++ = *src++;
870 *p = '\0';
871
872 sym = sym_find(name);
873 if (sym != NULL) {
874 sym_calc_value(sym);
875 symval = sym_get_string_value(sym);
876 }
877
878 newlen = strlen(res) + strlen(symval) + strlen(src);
879 if (newlen > reslen) {
880 reslen = newlen;
881 res = realloc(res, reslen);
882 }
883
884 strcat(res, symval);
885 in = src;
886 }
887 strcat(res, in);
888
889 return res;
890}
891
731struct symbol **sym_re_search(const char *pattern) 892struct symbol **sym_re_search(const char *pattern)
732{ 893{
733 struct symbol *sym, **sym_arr = NULL; 894 struct symbol *sym, **sym_arr = NULL;
@@ -765,6 +926,112 @@ struct symbol **sym_re_search(const char *pattern)
765 return sym_arr; 926 return sym_arr;
766} 927}
767 928
929/*
930 * When we check for recursive dependencies we use a stack to save
931 * current state so we can print out relevant info to user.
932 * The entries are located on the call stack so no need to free memory.
933 * Note inser() remove() must always match to properly clear the stack.
934 */
935static struct dep_stack {
936 struct dep_stack *prev, *next;
937 struct symbol *sym;
938 struct property *prop;
939 struct expr *expr;
940} *check_top;
941
942static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
943{
944 memset(stack, 0, sizeof(*stack));
945 if (check_top)
946 check_top->next = stack;
947 stack->prev = check_top;
948 stack->sym = sym;
949 check_top = stack;
950}
951
952static void dep_stack_remove(void)
953{
954 check_top = check_top->prev;
955 if (check_top)
956 check_top->next = NULL;
957}
958
959/*
960 * Called when we have detected a recursive dependency.
961 * check_top point to the top of the stact so we use
962 * the ->prev pointer to locate the bottom of the stack.
963 */
964static void sym_check_print_recursive(struct symbol *last_sym)
965{
966 struct dep_stack *stack;
967 struct symbol *sym, *next_sym;
968 struct menu *menu = NULL;
969 struct property *prop;
970 struct dep_stack cv_stack;
971
972 if (sym_is_choice_value(last_sym)) {
973 dep_stack_insert(&cv_stack, last_sym);
974 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
975 }
976
977 for (stack = check_top; stack != NULL; stack = stack->prev)
978 if (stack->sym == last_sym)
979 break;
980 if (!stack) {
981 fprintf(stderr, "unexpected recursive dependency error\n");
982 return;
983 }
984
985 for (; stack; stack = stack->next) {
986 sym = stack->sym;
987 next_sym = stack->next ? stack->next->sym : last_sym;
988 prop = stack->prop;
989 if (prop == NULL)
990 prop = stack->sym->prop;
991
992 /* for choice values find the menu entry (used below) */
993 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
994 for (prop = sym->prop; prop; prop = prop->next) {
995 menu = prop->menu;
996 if (prop->menu)
997 break;
998 }
999 }
1000 if (stack->sym == last_sym)
1001 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1002 prop->file->name, prop->lineno);
1003 if (stack->expr) {
1004 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1005 prop->file->name, prop->lineno,
1006 sym->name ? sym->name : "<choice>",
1007 prop_get_type_name(prop->type),
1008 next_sym->name ? next_sym->name : "<choice>");
1009 } else if (stack->prop) {
1010 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1011 prop->file->name, prop->lineno,
1012 sym->name ? sym->name : "<choice>",
1013 next_sym->name ? next_sym->name : "<choice>");
1014 } else if (sym_is_choice(sym)) {
1015 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1016 menu->file->name, menu->lineno,
1017 sym->name ? sym->name : "<choice>",
1018 next_sym->name ? next_sym->name : "<choice>");
1019 } else if (sym_is_choice_value(sym)) {
1020 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1021 menu->file->name, menu->lineno,
1022 sym->name ? sym->name : "<choice>",
1023 next_sym->name ? next_sym->name : "<choice>");
1024 } else {
1025 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1026 prop->file->name, prop->lineno,
1027 sym->name ? sym->name : "<choice>",
1028 next_sym->name ? next_sym->name : "<choice>");
1029 }
1030 }
1031
1032 if (check_top == &cv_stack)
1033 dep_stack_remove();
1034}
768 1035
769static struct symbol *sym_check_expr_deps(struct expr *e) 1036static struct symbol *sym_check_expr_deps(struct expr *e)
770{ 1037{
@@ -801,24 +1068,33 @@ static struct symbol *sym_check_sym_deps(struct symbol *sym)
801{ 1068{
802 struct symbol *sym2; 1069 struct symbol *sym2;
803 struct property *prop; 1070 struct property *prop;
1071 struct dep_stack stack;
1072
1073 dep_stack_insert(&stack, sym);
804 1074
805 sym2 = sym_check_expr_deps(sym->rev_dep.expr); 1075 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
806 if (sym2) 1076 if (sym2)
807 return sym2; 1077 goto out;
808 1078
809 for (prop = sym->prop; prop; prop = prop->next) { 1079 for (prop = sym->prop; prop; prop = prop->next) {
810 if (prop->type == P_CHOICE || prop->type == P_SELECT) 1080 if (prop->type == P_CHOICE || prop->type == P_SELECT)
811 continue; 1081 continue;
1082 stack.prop = prop;
812 sym2 = sym_check_expr_deps(prop->visible.expr); 1083 sym2 = sym_check_expr_deps(prop->visible.expr);
813 if (sym2) 1084 if (sym2)
814 break; 1085 break;
815 if (prop->type != P_DEFAULT || sym_is_choice(sym)) 1086 if (prop->type != P_DEFAULT || sym_is_choice(sym))
816 continue; 1087 continue;
1088 stack.expr = prop->expr;
817 sym2 = sym_check_expr_deps(prop->expr); 1089 sym2 = sym_check_expr_deps(prop->expr);
818 if (sym2) 1090 if (sym2)
819 break; 1091 break;
1092 stack.expr = NULL;
820 } 1093 }
821 1094
1095out:
1096 dep_stack_remove();
1097
822 return sym2; 1098 return sym2;
823} 1099}
824 1100
@@ -827,6 +1103,9 @@ static struct symbol *sym_check_choice_deps(struct symbol *choice)
827 struct symbol *sym, *sym2; 1103 struct symbol *sym, *sym2;
828 struct property *prop; 1104 struct property *prop;
829 struct expr *e; 1105 struct expr *e;
1106 struct dep_stack stack;
1107
1108 dep_stack_insert(&stack, choice);
830 1109
831 prop = sym_get_choice_prop(choice); 1110 prop = sym_get_choice_prop(choice);
832 expr_list_for_each_sym(prop->expr, e, sym) 1111 expr_list_for_each_sym(prop->expr, e, sym)
@@ -840,10 +1119,8 @@ static struct symbol *sym_check_choice_deps(struct symbol *choice)
840 1119
841 expr_list_for_each_sym(prop->expr, e, sym) { 1120 expr_list_for_each_sym(prop->expr, e, sym) {
842 sym2 = sym_check_sym_deps(sym); 1121 sym2 = sym_check_sym_deps(sym);
843 if (sym2) { 1122 if (sym2)
844 fprintf(stderr, " -> %s", sym->name);
845 break; 1123 break;
846 }
847 } 1124 }
848out: 1125out:
849 expr_list_for_each_sym(prop->expr, e, sym) 1126 expr_list_for_each_sym(prop->expr, e, sym)
@@ -853,6 +1130,8 @@ out:
853 prop_get_symbol(sym_get_choice_prop(sym2)) == choice) 1130 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
854 sym2 = choice; 1131 sym2 = choice;
855 1132
1133 dep_stack_remove();
1134
856 return sym2; 1135 return sym2;
857} 1136}
858 1137
@@ -862,18 +1141,20 @@ struct symbol *sym_check_deps(struct symbol *sym)
862 struct property *prop; 1141 struct property *prop;
863 1142
864 if (sym->flags & SYMBOL_CHECK) { 1143 if (sym->flags & SYMBOL_CHECK) {
865 fprintf(stderr, "%s:%d:error: found recursive dependency: %s", 1144 sym_check_print_recursive(sym);
866 sym->prop->file->name, sym->prop->lineno,
867 sym->name ? sym->name : "<choice>");
868 return sym; 1145 return sym;
869 } 1146 }
870 if (sym->flags & SYMBOL_CHECKED) 1147 if (sym->flags & SYMBOL_CHECKED)
871 return NULL; 1148 return NULL;
872 1149
873 if (sym_is_choice_value(sym)) { 1150 if (sym_is_choice_value(sym)) {
1151 struct dep_stack stack;
1152
874 /* for choice groups start the check with main choice symbol */ 1153 /* for choice groups start the check with main choice symbol */
1154 dep_stack_insert(&stack, sym);
875 prop = sym_get_choice_prop(sym); 1155 prop = sym_get_choice_prop(sym);
876 sym2 = sym_check_deps(prop_get_symbol(prop)); 1156 sym2 = sym_check_deps(prop_get_symbol(prop));
1157 dep_stack_remove();
877 } else if (sym_is_choice(sym)) { 1158 } else if (sym_is_choice(sym)) {
878 sym2 = sym_check_choice_deps(sym); 1159 sym2 = sym_check_choice_deps(sym);
879 } else { 1160 } else {
@@ -882,14 +1163,8 @@ struct symbol *sym_check_deps(struct symbol *sym)
882 sym->flags &= ~SYMBOL_CHECK; 1163 sym->flags &= ~SYMBOL_CHECK;
883 } 1164 }
884 1165
885 if (sym2) { 1166 if (sym2 && sym2 == sym)
886 fprintf(stderr, " -> %s", sym->name ? sym->name : "<choice>"); 1167 sym2 = NULL;
887 if (sym2 == sym) {
888 fprintf(stderr, "\n");
889 zconfnerrs++;
890 sym2 = NULL;
891 }
892 }
893 1168
894 return sym2; 1169 return sym2;
895} 1170}
@@ -943,6 +1218,8 @@ const char *prop_get_type_name(enum prop_type type)
943 return "select"; 1218 return "select";
944 case P_RANGE: 1219 case P_RANGE:
945 return "range"; 1220 return "range";
1221 case P_SYMBOL:
1222 return "symbol";
946 case P_UNKNOWN: 1223 case P_UNKNOWN:
947 break; 1224 break;
948 } 1225 }