2 * sparse/smatch_states.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
15 struct state_list
*cur_slist
; /* current states */
17 static struct state_list_stack
*true_stack
; /* states after a t/f branch */
18 static struct state_list_stack
*false_stack
;
19 static struct state_list_stack
*pre_cond_stack
; /* states before a t/f branch */
21 static struct state_list_stack
*cond_true_stack
; /* states affected by a branch */
22 static struct state_list_stack
*cond_false_stack
;
24 static struct state_list_stack
*break_stack
;
25 static struct state_list_stack
*switch_stack
;
26 static struct state_list_stack
*default_stack
;
27 static struct state_list_stack
*continue_stack
;
28 static struct state_list_stack
*false_only_stack
;
30 struct slist_stack
*goto_stack
;
34 void __print_cur_slist()
36 struct smatch_state
*state
;
38 printf("dumping slist at %d\n", get_lineno());
39 FOR_EACH_PTR(cur_slist
, state
) {
40 printf("%s=%d\n", state
->name
, state
->state
);
41 } END_FOR_EACH_PTR(state
);
46 void set_state(const char *name
, int owner
, struct symbol
*sym
, int state
)
48 struct smatch_state
*tmp
;
53 FOR_EACH_PTR(cur_slist
, tmp
) {
54 if (tmp
->owner
== owner
&& tmp
->sym
== sym
55 && !strcmp(tmp
->name
, name
)){
56 SM_DEBUG("%d state change name='%s' owner=%d: %d => %d\n"
57 , get_lineno(), name
, owner
, tmp
->state
, state
);
62 } END_FOR_EACH_PTR(tmp
);
63 SM_DEBUG("%d new state. name='%s' owner=%d: %d\n", get_lineno(), name
,
65 tmp
= alloc_state(name
, owner
, sym
, state
);
66 add_state_slist(&cur_slist
, tmp
);
70 int get_state(const char *name
, int owner
, struct symbol
*sym
)
72 return get_state_slist(cur_slist
, name
, owner
, sym
);
75 void delete_state(const char *name
, int owner
, struct symbol
*sym
)
77 delete_state_slist(&cur_slist
, name
, owner
, sym
);
80 struct state_list
*get_current_states(int owner
)
82 struct state_list
*slist
;
83 struct smatch_state
*tmp
;
85 FOR_EACH_PTR(cur_slist
, tmp
) {
86 if (tmp
->owner
== owner
) {
87 add_ptr_list(&slist
, tmp
);
89 } END_FOR_EACH_PTR(tmp
);
94 void set_true_false_states(const char *name
, int owner
, struct symbol
*sym
,
95 int true_state
, int false_state
)
98 /* fixme. save history */
100 SM_DEBUG("%d set_true_false %s. Was %d. Now T:%d F:%d\n",
101 get_lineno(), name
, get_state(name
, owner
, sym
), true_state
,
104 if (!cond_false_stack
|| !cond_true_stack
) {
105 printf("Error: missing true/false stacks\n");
109 set_state_slist(&cur_slist
, name
, owner
, sym
, true_state
);
110 set_state_stack(&cond_true_stack
, name
, owner
, sym
, true_state
);
111 set_state_stack(&cond_false_stack
, name
, owner
, sym
, false_state
);
117 del_slist(&cur_slist
);
120 void clear_all_states()
122 struct named_slist
*named_slist
;
125 del_slist_stack(&true_stack
);
126 del_slist_stack(&false_stack
);
127 del_slist_stack(&pre_cond_stack
);
128 del_slist_stack(&cond_true_stack
);
129 del_slist_stack(&cond_false_stack
);
130 del_slist_stack(&break_stack
);
131 del_slist_stack(&switch_stack
);
132 del_slist_stack(&continue_stack
);
134 FOR_EACH_PTR(goto_stack
, named_slist
) {
135 del_slist(&named_slist
->slist
);
136 } END_FOR_EACH_PTR(named_slist
);
137 __free_ptr_list((struct ptr_list
**)&goto_stack
);
141 void __push_cond_stacks()
143 push_slist(&cond_true_stack
, NULL
);
144 push_slist(&cond_false_stack
, NULL
);
147 static void __use_cond_stack(struct state_list_stack
**stack
)
149 struct state_list
*slist
;
150 struct smatch_state
*tmp
;
154 slist
= pop_slist(&pre_cond_stack
);
156 push_slist(&pre_cond_stack
, slist
);
158 slist
= pop_slist(stack
);
160 FOR_EACH_PTR(slist
, tmp
) {
161 set_state(tmp
->name
, tmp
->owner
, tmp
->sym
, tmp
->state
);
162 } END_FOR_EACH_PTR(tmp
);
165 push_slist(stack
, slist
);
170 void __use_cond_true_states()
172 __use_cond_stack(&cond_true_stack
);
175 void __use_cond_false_states()
177 __use_cond_stack(&cond_false_stack
);
180 void __negate_cond_stacks()
182 struct state_list
*tmp
;
184 tmp
= pop_slist(&cond_false_stack
);
185 push_slist(&cond_false_stack
, pop_slist(&cond_true_stack
));
186 push_slist(&cond_true_stack
, tmp
);
190 void __and_cond_states()
192 struct state_list
*tmp_slist
;
194 tmp_slist
= pop_slist(&cond_true_stack
);
195 and_slist_stack(&cond_true_stack
, tmp_slist
);
196 or_slist_stack(&cond_false_stack
);
199 void __or_cond_states()
201 struct state_list
*tmp_slist
;
203 or_slist_stack(&cond_true_stack
);
204 tmp_slist
= pop_slist(&cond_false_stack
);
205 and_slist_stack(&cond_false_stack
, tmp_slist
);
209 void __save_pre_cond_states()
211 push_slist(&pre_cond_stack
, clone_slist(cur_slist
));
215 static int false_only_prepped
= 0;
216 void __prep_false_only_stack()
218 false_only_prepped
= 1;
221 static void overwrite_slist(struct state_list
*from
, struct state_list
*to
)
223 struct smatch_state
*tmp
;
225 FOR_EACH_PTR(from
, tmp
) {
226 set_state_slist(&to
, tmp
->name
, tmp
->owner
, tmp
->sym
, tmp
->state
);
227 } END_FOR_EACH_PTR(tmp
);
230 void __use_false_only_stack()
232 struct state_list
*slist
;
234 slist
= pop_slist(&false_only_stack
);
235 overwrite_slist(slist
, cur_slist
);
237 false_only_prepped
= 0;
240 void __use_cond_states()
242 struct state_list
*tmp
, *tmp2
, *tmp3
;
244 if (false_only_prepped
) {
245 tmp
= pop_slist(&cond_false_stack
);
246 push_slist(&false_only_stack
, clone_slist(tmp
));
247 push_slist(&cond_false_stack
, tmp
);
250 /* Everyone calls __use_true_states so setting up
251 the true stack is enough here */
252 tmp
= pop_slist(&pre_cond_stack
);
253 tmp3
= clone_slist(tmp
);
254 tmp2
= pop_slist(&cond_true_stack
);
255 overwrite_slist(tmp2
, tmp3
);
256 push_slist(&true_stack
, tmp3
);
258 tmp2
= pop_slist(&cond_false_stack
);
259 overwrite_slist(tmp2
, tmp
);
260 push_slist(&false_stack
, tmp
);
263 void __use_true_states()
265 struct state_list
*slist
;
268 slist
= pop_slist(&true_stack
);
273 void __use_false_states()
275 struct state_list
*slist
;
277 push_slist(&true_stack
, clone_slist(cur_slist
));
279 slist
= pop_slist(&false_stack
);
284 void __pop_false_states()
286 struct state_list
*slist
;
288 slist
= pop_slist(&false_stack
);
292 void __merge_false_states()
294 struct state_list
*slist
;
296 slist
= pop_slist(&false_stack
);
301 void __merge_true_states()
303 struct state_list
*slist
;
305 slist
= pop_slist(&true_stack
);
310 void __pop_true_states()
312 struct state_list
*slist
;
314 slist
= pop_slist(&true_stack
);
318 void __push_continues()
320 push_slist(&continue_stack
, NULL
);
323 void __pop_continues()
325 struct state_list
*slist
;
327 slist
= pop_slist(&continue_stack
);
331 void __process_continues()
333 struct smatch_state
*state
;
335 FOR_EACH_PTR(cur_slist
, state
) {
336 merge_state_stack(&continue_stack
, state
->name
, state
->owner
,
337 state
->sym
, state
->state
);
338 } END_FOR_EACH_PTR(state
);
341 void __merge_continues()
343 struct state_list
*slist
;
345 slist
= pop_slist(&continue_stack
);
352 push_slist(&break_stack
, NULL
);
355 void __process_breaks()
357 struct smatch_state
*state
;
359 FOR_EACH_PTR(cur_slist
, state
) {
360 merge_state_stack(&break_stack
, state
->name
, state
->owner
,
361 state
->sym
, state
->state
);
362 } END_FOR_EACH_PTR(state
);
365 void __merge_breaks()
367 struct state_list
*slist
;
369 slist
= pop_slist(&break_stack
);
376 struct state_list
*slist
;
378 slist
= pop_slist(&break_stack
);
382 void __save_switch_states()
384 push_slist(&switch_stack
, clone_slist(cur_slist
));
387 void __merge_switches()
389 struct state_list
*slist
;
391 slist
= pop_slist(&switch_stack
);
393 push_slist(&switch_stack
, slist
);
396 void __pop_switches()
398 struct state_list
*slist
;
400 slist
= pop_slist(&switch_stack
);
404 void __push_default()
406 push_slist(&default_stack
, NULL
);
407 set_state_stack(&default_stack
, "has_default", 0, NULL
, 0);
412 set_state_stack(&default_stack
, "has_default", 0, NULL
, 1);
417 struct state_list
*slist
;
418 struct smatch_state
*state
;
421 slist
= pop_slist(&default_stack
);
422 FOR_EACH_PTR(slist
, state
) {
423 if (!strcmp(state
->name
, "has_default"))
425 } END_FOR_EACH_PTR(state
);
430 static struct named_slist
*alloc_named_slist(const char *name
,
431 struct state_list
*slist
)
433 struct named_slist
*named_slist
= __alloc_named_slist(0);
435 named_slist
->name
= (char *)name
;
436 named_slist
->slist
= slist
;
440 void __save_gotos(const char *name
)
442 struct state_list
*slist
;
444 slist
= get_slist_from_slist_stack(name
);
446 struct smatch_state
*state
;
447 FOR_EACH_PTR(cur_slist
, state
) {
448 merge_state_slist(&slist
, state
->name
, state
->owner
,
449 state
->sym
, state
->state
);
450 } END_FOR_EACH_PTR(state
);
453 struct state_list
*slist
;
454 struct named_slist
*named_slist
;
455 slist
= clone_slist(cur_slist
);
456 named_slist
= alloc_named_slist(name
, slist
);
457 add_ptr_list(&goto_stack
, named_slist
);
461 void __merge_gotos(const char *name
)
463 struct state_list
*slist
;
465 slist
= get_slist_from_slist_stack(name
);