allocations: set the ->total_size, ->nr_elem and ->elem_size expressions
[smatch.git] / smatch_var_sym.c
bloba0517f9e67492e5ec7fd7242aec3253d6f644a89
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 smatch_state *alloc_var_sym_state(const char *var, struct symbol *sym)
24 struct smatch_state *state;
26 state = __alloc_smatch_state(0);
27 state->name = alloc_string(var);
28 state->data = alloc_var_sym(var, sym);
29 return state;
32 struct var_sym *alloc_var_sym(const char *var, struct symbol *sym)
34 struct var_sym *tmp;
36 tmp = __alloc_var_sym(0);
37 tmp->var = alloc_string(var);
38 tmp->sym = sym;
39 return tmp;
42 struct var_sym_list *expr_to_vsl(struct expression *expr)
44 struct var_sym_list *ret = NULL;
45 struct var_sym_list *more;
46 char *var;
47 struct symbol *sym;
49 expr = strip_expr(expr);
50 if (!expr)
51 return NULL;
53 if (expr->type == EXPR_BINOP ||
54 expr->type == EXPR_LOGICAL ||
55 expr->type == EXPR_COMPARE) {
56 struct var_sym_list *left, *right;
58 left = expr_to_vsl(expr->left);
59 right = expr_to_vsl(expr->right);
60 ret = combine_var_sym_lists(left, right);
61 free_var_syms_and_list(&left);
62 free_var_syms_and_list(&right);
63 return ret;
66 var = expr_to_var_sym(expr, &sym);
67 if (var && sym)
68 add_var_sym(&ret, var, sym);
71 // handle "*(foo[bar])" I guess */
72 if (expr->type == EXPR_PREOP && expr->op == '*') {
73 more = expr_to_vsl(expr->unop);
74 return combine_var_sym_lists(ret, more);
77 if (expr->type == EXPR_DEREF) {
78 more = expr_to_vsl(expr->deref);
79 return combine_var_sym_lists(ret, more);
82 return ret;
85 int cmp_var_sym(const struct var_sym *a, const struct var_sym *b)
87 int ret;
89 if (a == b)
90 return 0;
91 if (!b)
92 return -1;
93 if (!a)
94 return 1;
96 ret = strcmp(a->var, b->var);
97 if (ret < 0)
98 return -1;
99 if (ret > 0)
100 return 1;
102 if (!b->sym && a->sym)
103 return -1;
104 if (!a->sym && b->sym)
105 return 1;
106 if (a->sym < b->sym)
107 return -1;
108 if (a->sym > b->sym)
109 return 1;
111 return 0;
114 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym)
116 struct var_sym *tmp, *new;
118 if (in_var_sym_list(*list, var, sym))
119 return;
120 new = alloc_var_sym(var, sym);
122 FOR_EACH_PTR(*list, tmp) {
123 if (cmp_var_sym(tmp, new) < 0)
124 continue;
125 else if (cmp_var_sym(tmp, new) == 0) {
126 return;
127 } else {
128 INSERT_CURRENT(new, tmp);
129 return;
131 } END_FOR_EACH_PTR(tmp);
132 add_ptr_list(list, new);
135 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr)
137 char *var;
138 struct symbol *sym;
140 var = expr_to_var_sym(expr, &sym);
141 if (!var || !sym)
142 goto free;
143 add_var_sym(list, var, sym);
144 free:
145 free_string(var);
148 static void free_var_sym(struct var_sym *vs)
150 free_string(vs->var);
151 __free_var_sym(vs);
154 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym)
156 struct var_sym *tmp;
158 FOR_EACH_PTR(*list, tmp) {
159 if (tmp->sym == sym && strcmp(tmp->var, var) == 0) {
160 DELETE_CURRENT_PTR(tmp);
161 free_var_sym(tmp);
162 return;
164 } END_FOR_EACH_PTR(tmp);
167 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym)
169 struct var_sym *tmp;
171 FOR_EACH_PTR(list, tmp) {
172 if (tmp->sym == sym && strcmp(tmp->var, var) == 0)
173 return 1;
174 } END_FOR_EACH_PTR(tmp);
175 return 0;
178 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl)
180 struct var_sym *tmp, *clone_vs;
181 struct var_sym_list *to_vsl = NULL;
183 FOR_EACH_PTR(from_vsl, tmp) {
184 clone_vs = alloc_var_sym(tmp->var, tmp->sym);
185 add_ptr_list(&to_vsl, clone_vs);
186 } END_FOR_EACH_PTR(tmp);
187 return to_vsl;
190 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src)
192 struct var_sym *tmp;
194 FOR_EACH_PTR(src, tmp) {
195 add_var_sym(dest, tmp->var, tmp->sym);
196 } END_FOR_EACH_PTR(tmp);
199 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two)
201 struct var_sym_list *to_vsl;
203 to_vsl = clone_var_sym_list(one);
204 merge_var_sym_list(&to_vsl, two);
205 return to_vsl;
208 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two)
210 struct var_sym *one_tmp, *two_tmp;
212 if (one == two)
213 return 1;
215 if (ptr_list_size((struct ptr_list *)one) != ptr_list_size((struct ptr_list *)two))
216 return 0;
218 PREPARE_PTR_LIST(one, one_tmp);
219 PREPARE_PTR_LIST(two, two_tmp);
220 for (;;) {
221 if (!one_tmp && !two_tmp)
222 return 1;
223 if (one_tmp->sym != two_tmp->sym)
224 return 0;
225 if (strcmp(one_tmp->var, two_tmp->var) != 0)
226 return 0;
227 NEXT_PTR_LIST(one_tmp);
228 NEXT_PTR_LIST(two_tmp);
230 FINISH_PTR_LIST(two_tmp);
231 FINISH_PTR_LIST(one_tmp);
233 return 1;
236 void free_var_sym_list(struct var_sym_list **list)
238 __free_ptr_list((struct ptr_list **)list);
241 void free_var_syms_and_list(struct var_sym_list **list)
243 struct var_sym *tmp;
245 FOR_EACH_PTR(*list, tmp) {
246 free_var_sym(tmp);
247 } END_FOR_EACH_PTR(tmp);
248 free_var_sym_list(list);