type: fix get_member_from_string()
[smatch.git] / smatch_var_sym.c
blob7cfa06ac943e974b5227708ee65bf69db0028026
1 /*
2 * Copyright (C) 2013 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
20 ALLOCATOR(var_sym, "var_sym structs");
22 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym)
24 struct var_sym *tmp;
26 tmp = __alloc_var_sym(0);
27 tmp->var = alloc_string(var);
28 tmp->sym = sym;
29 return tmp;
32 struct var_sym_list *expr_to_vsl(struct expression *expr)
34 struct expression *unop;
35 struct var_sym_list *ret = NULL;
36 char *var;
37 struct symbol *sym;
39 expr = strip_expr(expr);
40 if (!expr)
41 return NULL;
43 if ((expr->type == EXPR_PREOP && expr->op == '*')) {
44 unop = strip_expr(expr->unop);
46 if (unop->type == EXPR_SYMBOL)
47 goto one_var;
48 return expr_to_vsl(unop);
51 if (expr->type == EXPR_BINOP ||
52 expr->type == EXPR_LOGICAL ||
53 expr->type == EXPR_COMPARE) {
54 struct var_sym_list *left, *right;
56 left = expr_to_vsl(expr->left);
57 right = expr_to_vsl(expr->right);
58 ret = combine_var_sym_lists(left, right);
59 free_var_syms_and_list(&left);
60 free_var_syms_and_list(&right);
61 return ret;
64 if (expr->type == EXPR_DEREF)
65 return expr_to_vsl(expr->deref);
67 one_var:
68 var = expr_to_var_sym(expr, &sym);
69 if (!var || !sym) {
70 free_string(var);
71 return NULL;
73 add_var_sym(&ret, var, sym);
74 return ret;
77 int cmp_var_sym(const struct var_sym *a, const struct var_sym *b)
79 int ret;
81 if (a == b)
82 return 0;
83 if (!b)
84 return -1;
85 if (!a)
86 return 1;
88 ret = strcmp(a->var, b->var);
89 if (ret < 0)
90 return -1;
91 if (ret > 0)
92 return 1;
94 if (!b->sym && a->sym)
95 return -1;
96 if (!a->sym && b->sym)
97 return 1;
98 if (a->sym < b->sym)
99 return -1;
100 if (a->sym > b->sym)
101 return 1;
103 return 0;
106 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym)
108 struct var_sym *tmp, *new;
110 if (in_var_sym_list(*list, var, sym))
111 return;
112 new = alloc_var_sym(var, sym);
114 FOR_EACH_PTR(*list, tmp) {
115 if (cmp_var_sym(tmp, new) < 0)
116 continue;
117 else if (cmp_var_sym(tmp, new) == 0) {
118 return;
119 } else {
120 INSERT_CURRENT(new, tmp);
121 return;
123 } END_FOR_EACH_PTR(tmp);
124 add_ptr_list(list, new);
127 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr)
129 char *var;
130 struct symbol *sym;
132 var = expr_to_var_sym(expr, &sym);
133 if (!var || !sym)
134 goto free;
135 add_var_sym(list, var, sym);
136 free:
137 free_string(var);
140 static void free_var_sym(struct var_sym *vs)
142 free_string(vs->var);
143 __free_var_sym(vs);
146 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym)
148 struct var_sym *tmp;
150 FOR_EACH_PTR(*list, tmp) {
151 if (tmp->sym == sym && strcmp(tmp->var, var) == 0) {
152 DELETE_CURRENT_PTR(tmp);
153 free_var_sym(tmp);
154 return;
156 } END_FOR_EACH_PTR(tmp);
159 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym)
161 struct var_sym *tmp;
163 FOR_EACH_PTR(list, tmp) {
164 if (tmp->sym == sym && strcmp(tmp->var, var) == 0)
165 return 1;
166 } END_FOR_EACH_PTR(tmp);
167 return 0;
170 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl)
172 struct var_sym *tmp, *clone_vs;
173 struct var_sym_list *to_vsl = NULL;
175 FOR_EACH_PTR(from_vsl, tmp) {
176 clone_vs = alloc_var_sym(tmp->var, tmp->sym);
177 add_ptr_list(&to_vsl, clone_vs);
178 } END_FOR_EACH_PTR(tmp);
179 return to_vsl;
182 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src)
184 struct var_sym *tmp;
186 FOR_EACH_PTR(src, tmp) {
187 add_var_sym(dest, tmp->var, tmp->sym);
188 } END_FOR_EACH_PTR(tmp);
191 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two)
193 struct var_sym_list *to_vsl;
195 to_vsl = clone_var_sym_list(one);
196 merge_var_sym_list(&to_vsl, two);
197 return to_vsl;
200 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two)
202 struct var_sym *one_tmp, *two_tmp;
204 if (one == two)
205 return 1;
207 if (ptr_list_size((struct ptr_list *)one) != ptr_list_size((struct ptr_list *)two))
208 return 0;
210 PREPARE_PTR_LIST(one, one_tmp);
211 PREPARE_PTR_LIST(two, two_tmp);
212 for (;;) {
213 if (!one_tmp && !two_tmp)
214 return 1;
215 if (one_tmp->sym != two_tmp->sym)
216 return 0;
217 if (strcmp(one_tmp->var, two_tmp->var) != 0)
218 return 0;
219 NEXT_PTR_LIST(one_tmp);
220 NEXT_PTR_LIST(two_tmp);
222 FINISH_PTR_LIST(two_tmp);
223 FINISH_PTR_LIST(one_tmp);
225 return 1;
228 void free_var_sym_list(struct var_sym_list **list)
230 __free_ptr_list((struct ptr_list **)list);
233 void free_var_syms_and_list(struct var_sym_list **list)
235 struct var_sym *tmp;
237 FOR_EACH_PTR(*list, tmp) {
238 free_var_sym(tmp);
239 } END_FOR_EACH_PTR(tmp);
240 free_var_sym_list(list);