comparison: fix how addresses are handled
[smatch.git] / smatch_var_sym.c
blob54a42867ba5c1ecbb67f4078b2d2635b857e62ff
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 expression *unop;
45 struct var_sym_list *ret = NULL;
46 char *var;
47 struct symbol *sym;
49 expr = strip_expr(expr);
50 if (!expr)
51 return NULL;
53 if ((expr->type == EXPR_PREOP && expr->op == '*')) {
54 unop = strip_expr(expr->unop);
56 if (unop->type == EXPR_SYMBOL)
57 goto one_var;
58 return expr_to_vsl(unop);
61 if (expr->type == EXPR_BINOP ||
62 expr->type == EXPR_LOGICAL ||
63 expr->type == EXPR_COMPARE) {
64 struct var_sym_list *left, *right;
66 left = expr_to_vsl(expr->left);
67 right = expr_to_vsl(expr->right);
68 ret = combine_var_sym_lists(left, right);
69 free_var_syms_and_list(&left);
70 free_var_syms_and_list(&right);
71 return ret;
74 if (expr->type == EXPR_DEREF)
75 return expr_to_vsl(expr->deref);
77 one_var:
78 var = expr_to_var_sym(expr, &sym);
79 if (!var || !sym) {
80 free_string(var);
81 return NULL;
83 add_var_sym(&ret, var, sym);
84 return ret;
87 int cmp_var_sym(const struct var_sym *a, const struct var_sym *b)
89 int ret;
91 if (a == b)
92 return 0;
93 if (!b)
94 return -1;
95 if (!a)
96 return 1;
98 ret = strcmp(a->var, b->var);
99 if (ret < 0)
100 return -1;
101 if (ret > 0)
102 return 1;
104 if (!b->sym && a->sym)
105 return -1;
106 if (!a->sym && b->sym)
107 return 1;
108 if (a->sym < b->sym)
109 return -1;
110 if (a->sym > b->sym)
111 return 1;
113 return 0;
116 void add_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym)
118 struct var_sym *tmp, *new;
120 if (in_var_sym_list(*list, var, sym))
121 return;
122 new = alloc_var_sym(var, sym);
124 FOR_EACH_PTR(*list, tmp) {
125 if (cmp_var_sym(tmp, new) < 0)
126 continue;
127 else if (cmp_var_sym(tmp, new) == 0) {
128 return;
129 } else {
130 INSERT_CURRENT(new, tmp);
131 return;
133 } END_FOR_EACH_PTR(tmp);
134 add_ptr_list(list, new);
137 void add_var_sym_expr(struct var_sym_list **list, struct expression *expr)
139 char *var;
140 struct symbol *sym;
142 var = expr_to_var_sym(expr, &sym);
143 if (!var || !sym)
144 goto free;
145 add_var_sym(list, var, sym);
146 free:
147 free_string(var);
150 static void free_var_sym(struct var_sym *vs)
152 free_string(vs->var);
153 __free_var_sym(vs);
156 void del_var_sym(struct var_sym_list **list, const char *var, struct symbol *sym)
158 struct var_sym *tmp;
160 FOR_EACH_PTR(*list, tmp) {
161 if (tmp->sym == sym && strcmp(tmp->var, var) == 0) {
162 DELETE_CURRENT_PTR(tmp);
163 free_var_sym(tmp);
164 return;
166 } END_FOR_EACH_PTR(tmp);
169 int in_var_sym_list(struct var_sym_list *list, const char *var, struct symbol *sym)
171 struct var_sym *tmp;
173 FOR_EACH_PTR(list, tmp) {
174 if (tmp->sym == sym && strcmp(tmp->var, var) == 0)
175 return 1;
176 } END_FOR_EACH_PTR(tmp);
177 return 0;
180 struct var_sym_list *clone_var_sym_list(struct var_sym_list *from_vsl)
182 struct var_sym *tmp, *clone_vs;
183 struct var_sym_list *to_vsl = NULL;
185 FOR_EACH_PTR(from_vsl, tmp) {
186 clone_vs = alloc_var_sym(tmp->var, tmp->sym);
187 add_ptr_list(&to_vsl, clone_vs);
188 } END_FOR_EACH_PTR(tmp);
189 return to_vsl;
192 void merge_var_sym_list(struct var_sym_list **dest, struct var_sym_list *src)
194 struct var_sym *tmp;
196 FOR_EACH_PTR(src, tmp) {
197 add_var_sym(dest, tmp->var, tmp->sym);
198 } END_FOR_EACH_PTR(tmp);
201 struct var_sym_list *combine_var_sym_lists(struct var_sym_list *one, struct var_sym_list *two)
203 struct var_sym_list *to_vsl;
205 to_vsl = clone_var_sym_list(one);
206 merge_var_sym_list(&to_vsl, two);
207 return to_vsl;
210 int var_sym_lists_equiv(struct var_sym_list *one, struct var_sym_list *two)
212 struct var_sym *one_tmp, *two_tmp;
214 if (one == two)
215 return 1;
217 if (ptr_list_size((struct ptr_list *)one) != ptr_list_size((struct ptr_list *)two))
218 return 0;
220 PREPARE_PTR_LIST(one, one_tmp);
221 PREPARE_PTR_LIST(two, two_tmp);
222 for (;;) {
223 if (!one_tmp && !two_tmp)
224 return 1;
225 if (one_tmp->sym != two_tmp->sym)
226 return 0;
227 if (strcmp(one_tmp->var, two_tmp->var) != 0)
228 return 0;
229 NEXT_PTR_LIST(one_tmp);
230 NEXT_PTR_LIST(two_tmp);
232 FINISH_PTR_LIST(two_tmp);
233 FINISH_PTR_LIST(one_tmp);
235 return 1;
238 void free_var_sym_list(struct var_sym_list **list)
240 __free_ptr_list((struct ptr_list **)list);
243 void free_var_syms_and_list(struct var_sym_list **list)
245 struct var_sym *tmp;
247 FOR_EACH_PTR(*list, tmp) {
248 free_var_sym(tmp);
249 } END_FOR_EACH_PTR(tmp);
250 free_var_sym_list(list);