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 var_sym_list
*ret
= NULL
;
45 struct var_sym_list
*more
;
49 expr
= strip_expr(expr
);
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
);
66 var
= expr_to_var_sym(expr
, &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
);
85 int cmp_var_sym(const struct var_sym
*a
, const struct var_sym
*b
)
96 ret
= strcmp(a
->var
, b
->var
);
102 if (!b
->sym
&& a
->sym
)
104 if (!a
->sym
&& b
->sym
)
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
))
120 new = alloc_var_sym(var
, sym
);
122 FOR_EACH_PTR(*list
, tmp
) {
123 if (cmp_var_sym(tmp
, new) < 0)
125 else if (cmp_var_sym(tmp
, new) == 0) {
128 INSERT_CURRENT(new, tmp
);
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
)
140 var
= expr_to_var_sym(expr
, &sym
);
143 add_var_sym(list
, var
, sym
);
148 static void free_var_sym(struct var_sym
*vs
)
150 free_string(vs
->var
);
154 void del_var_sym(struct var_sym_list
**list
, const char *var
, struct symbol
*sym
)
158 FOR_EACH_PTR(*list
, tmp
) {
159 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0) {
160 DELETE_CURRENT_PTR(tmp
);
164 } END_FOR_EACH_PTR(tmp
);
167 int in_var_sym_list(struct var_sym_list
*list
, const char *var
, struct symbol
*sym
)
171 FOR_EACH_PTR(list
, tmp
) {
172 if (tmp
->sym
== sym
&& strcmp(tmp
->var
, var
) == 0)
174 } END_FOR_EACH_PTR(tmp
);
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
);
190 void merge_var_sym_list(struct var_sym_list
**dest
, struct var_sym_list
*src
)
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
);
208 int var_sym_lists_equiv(struct var_sym_list
*one
, struct var_sym_list
*two
)
210 struct var_sym
*one_tmp
, *two_tmp
;
215 if (ptr_list_size((struct ptr_list
*)one
) != ptr_list_size((struct ptr_list
*)two
))
218 PREPARE_PTR_LIST(one
, one_tmp
);
219 PREPARE_PTR_LIST(two
, two_tmp
);
221 if (!one_tmp
&& !two_tmp
)
223 if (one_tmp
->sym
!= two_tmp
->sym
)
225 if (strcmp(one_tmp
->var
, two_tmp
->var
) != 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
);
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
)
245 FOR_EACH_PTR(*list
, tmp
) {
247 } END_FOR_EACH_PTR(tmp
);
248 free_var_sym_list(list
);