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
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
);
32 struct var_sym
*alloc_var_sym(const char *var
, struct symbol
*sym
)
36 tmp
= __alloc_var_sym(0);
37 tmp
->var
= alloc_string(var
);
42 struct var_sym_list
*expr_to_vsl(struct expression
*expr
)
44 struct expression
*unop
;
45 struct var_sym_list
*ret
= NULL
;
49 expr
= strip_expr(expr
);
53 if ((expr
->type
== EXPR_PREOP
&& expr
->op
== '*')) {
54 unop
= strip_expr(expr
->unop
);
56 if (unop
->type
== EXPR_SYMBOL
)
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
);
74 if (expr
->type
== EXPR_DEREF
)
75 return expr_to_vsl(expr
->deref
);
78 var
= expr_to_var_sym(expr
, &sym
);
83 add_var_sym(&ret
, var
, sym
);
87 int cmp_var_sym(const struct var_sym
*a
, const struct var_sym
*b
)
98 ret
= strcmp(a
->var
, b
->var
);
104 if (!b
->sym
&& a
->sym
)
106 if (!a
->sym
&& b
->sym
)
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
))
122 new = alloc_var_sym(var
, sym
);
124 FOR_EACH_PTR(*list
, tmp
) {
125 if (cmp_var_sym(tmp
, new) < 0)
127 else if (cmp_var_sym(tmp
, new) == 0) {
130 INSERT_CURRENT(new, tmp
);
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
)
142 var
= expr_to_var_sym(expr
, &sym
);
145 add_var_sym(list
, var
, sym
);
150 static void free_var_sym(struct var_sym
*vs
)
152 free_string(vs
->var
);
156 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
)
160 FOR_EACH_PTR(*list
, tmp
) {
161 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0) {
162 DELETE_CURRENT_PTR(tmp
);
166 } END_FOR_EACH_PTR(tmp
);
169 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
)
173 FOR_EACH_PTR(list
, tmp
) {
174 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0)
176 } END_FOR_EACH_PTR(tmp
);
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
);
192 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
)
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
);
210 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
)
212 struct var_sym
*one_tmp
, *two_tmp
;
217 if (ptr_list_size((struct ptr_list
*)one
) != ptr_list_size((struct ptr_list
*)two
))
220 PREPARE_PTR_LIST(one
, one_tmp
);
221 PREPARE_PTR_LIST(two
, two_tmp
);
223 if (!one_tmp
&& !two_tmp
)
225 if (one_tmp
->sym
!= two_tmp
->sym
)
227 if (strcmp(one_tmp
->var
, two_tmp
->var
) != 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
);
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
)
247 FOR_EACH_PTR(*list
, tmp
) {
249 } END_FOR_EACH_PTR(tmp
);
250 free_var_sym_list(list
);