2 * sparse/smatch_slist.c
4 * Copyright (C) 2008,2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 #include "smatch_slist.h"
18 ALLOCATOR(sm_state
, "smatch state");
19 ALLOCATOR(named_slist
, "named slist");
21 void __print_slist(struct state_list
*slist
)
23 struct sm_state
*state
;
25 printf("dumping slist at %d\n", get_lineno());
26 FOR_EACH_PTR(slist
, state
) {
27 printf("%d '%s'=%s\n", state
->owner
, state
->name
,
28 show_state(state
->state
));
29 } END_FOR_EACH_PTR(state
);
33 void add_history(struct sm_state
*state
)
35 struct state_history
*tmp
;
39 tmp
= malloc(sizeof(*tmp
));
40 tmp
->loc
= get_lineno();
41 add_ptr_list(&state
->line_history
, tmp
);
45 /* NULL states go at the end to simplify merge_slist */
46 static int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
57 if (a
->owner
> b
->owner
)
59 if (a
->owner
< b
->owner
)
62 ret
= strcmp(a
->name
, b
->name
);
66 if (!b
->sym
&& a
->sym
)
68 if (!a
->sym
&& b
->sym
)
78 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
82 ret
= cmp_tracker(a
, b
);
86 /* todo: add hook for smatch_extra.c */
87 if (a
->state
> b
->state
)
89 if (a
->state
< b
->state
)
94 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
98 FOR_EACH_PTR(*slist
, tmp
) {
99 if (cmp_sm_states(tmp
, new) < 0)
101 else if (cmp_sm_states(tmp
, new) == 0) {
104 INSERT_CURRENT(new, tmp
);
107 } END_FOR_EACH_PTR(tmp
);
108 add_ptr_list(slist
, new);
111 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
113 struct sm_state
*tmp
;
117 struct smatch_state
*s
;
119 s
= merge_states(sm
->name
, sm
->owner
, sm
->sym
, sm
->state
, NULL
);
120 tmp
= alloc_state(sm
->name
, sm
->owner
, sm
->sym
, s
);
121 add_sm_state_slist(&sm
->possible
, tmp
);
124 FOR_EACH_PTR(new->possible
, tmp
) {
125 add_sm_state_slist(&sm
->possible
, tmp
);
126 } END_FOR_EACH_PTR(tmp
);
129 struct sm_state
*alloc_state(const char *name
, int owner
,
130 struct symbol
*sym
, struct smatch_state
*state
)
132 struct sm_state
*sm_state
= __alloc_sm_state(0);
134 sm_state
->name
= (char *)name
;
135 sm_state
->owner
= owner
;
137 sm_state
->state
= state
;
138 sm_state
->line_history
= NULL
;
139 add_history(sm_state
);
140 sm_state
->pools
= NULL
;
141 sm_state
->possible
= NULL
;
142 add_ptr_list(&sm_state
->possible
, sm_state
);
146 struct sm_state
*clone_state(struct sm_state
*s
)
148 struct sm_state
*tmp
;
150 tmp
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
151 tmp
->pools
= clone_stack(s
->pools
);
152 tmp
->possible
= s
->possible
;
156 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
158 struct sm_state
*tmp
;
160 FOR_EACH_PTR(slist
, tmp
) {
161 if (tmp
->state
== state
)
163 } END_FOR_EACH_PTR(tmp
);
168 static void check_order(struct state_list
*slist
)
170 struct sm_state
*state
;
171 struct sm_state
*last
= NULL
;
174 FOR_EACH_PTR(slist
, state
) {
175 if (last
&& cmp_tracker(state
, last
) <= 0) {
176 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
177 "%s vs %s\n", last
->owner
, state
->owner
,
178 last
->sym
, state
->sym
, last
->name
, state
->name
);
182 } END_FOR_EACH_PTR(state
);
189 struct state_list
*clone_slist(struct state_list
*from_slist
)
191 struct sm_state
*state
;
192 struct sm_state
*tmp
;
193 struct state_list
*to_slist
= NULL
;
195 FOR_EACH_PTR(from_slist
, state
) {
196 tmp
= clone_state(state
);
197 add_ptr_list(&to_slist
, tmp
);
198 } END_FOR_EACH_PTR(state
);
200 check_order(to_slist
);
205 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
207 struct state_list
*slist
;
208 struct state_list_stack
*to_stack
= NULL
;
210 FOR_EACH_PTR(from_stack
, slist
) {
211 push_slist(&to_stack
, slist
);
212 } END_FOR_EACH_PTR(slist
);
216 // FIXME... shouldn't we free some of these state pointers?
217 struct smatch_state
*merge_states(const char *name
, int owner
,
219 struct smatch_state
*state1
,
220 struct smatch_state
*state2
)
222 struct smatch_state
*ret
;
224 if (state1
== state2
)
226 else if (__has_merge_function(owner
))
227 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
231 SM_DEBUG("%d merge name='%s' owner=%d: %s + %s => %s\n",
232 get_lineno(), name
, owner
, show_state(state1
),
233 show_state(state2
), show_state(ret
));
238 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
240 struct smatch_state
*s
;
241 struct sm_state
*result
;
243 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
244 (two
?two
->state
:NULL
));
245 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
246 add_possible(result
, one
);
247 add_possible(result
, two
);
251 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
252 int owner
, struct symbol
*sym
)
254 struct sm_state
*state
;
259 FOR_EACH_PTR(slist
, state
) {
260 if (state
->owner
== owner
&& state
->sym
== sym
261 && !strcmp(state
->name
, name
))
263 } END_FOR_EACH_PTR(state
);
267 struct smatch_state
*get_state_slist(struct state_list
*slist
,
268 const char *name
, int owner
,
271 struct sm_state
*state
;
273 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
279 static void overwrite_sm_state(struct state_list
**slist
,
280 struct sm_state
*state
)
282 struct sm_state
*tmp
;
283 struct sm_state
*new = clone_state(state
); //fixme. why?
285 FOR_EACH_PTR(*slist
, tmp
) {
286 if (cmp_tracker(tmp
, new) < 0)
288 else if (cmp_tracker(tmp
, new) == 0) {
289 tmp
->state
= new->state
;
290 tmp
->pools
= new->pools
;
291 tmp
->possible
= new->possible
;
292 __free_sm_state(new);
295 INSERT_CURRENT(new, tmp
);
298 } END_FOR_EACH_PTR(tmp
);
299 add_ptr_list(slist
, new);
302 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
303 struct symbol
*sym
, struct smatch_state
*state
)
305 struct sm_state
*tmp
;
306 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
308 FOR_EACH_PTR(*slist
, tmp
) {
309 if (cmp_tracker(tmp
, new) < 0)
311 else if (cmp_tracker(tmp
, new) == 0) {
314 tmp
->possible
= NULL
;
315 add_ptr_list(&tmp
->possible
, tmp
);
316 __free_sm_state(new);
319 INSERT_CURRENT(new, tmp
);
322 } END_FOR_EACH_PTR(tmp
);
323 add_ptr_list(slist
, new);
326 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
329 struct sm_state
*state
;
331 FOR_EACH_PTR(*slist
, state
) {
332 if (state
->owner
== owner
&& state
->sym
== sym
333 && !strcmp(state
->name
, name
)){
334 delete_ptr_list_entry((struct ptr_list
**)slist
,
336 __free_sm_state(state
);
339 } END_FOR_EACH_PTR(state
);
343 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
345 add_ptr_list(list_stack
, slist
);
348 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
350 struct state_list
*slist
;
352 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
353 delete_ptr_list_last((struct ptr_list
**)list_stack
);
357 void del_slist(struct state_list
**slist
)
359 __free_ptr_list((struct ptr_list
**)slist
);
362 void del_slist_stack(struct state_list_stack
**slist_stack
)
364 struct state_list
*slist
;
366 FOR_EACH_PTR(*slist_stack
, slist
) {
367 __free_ptr_list((struct ptr_list
**)&slist
);
368 } END_FOR_EACH_PTR(slist
);
369 __free_ptr_list((struct ptr_list
**)slist_stack
);
373 * set_state_stack() sets the state for the top slist on the stack.
375 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
376 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
378 struct state_list
*slist
;
380 slist
= pop_slist(stack
);
381 set_state_slist(&slist
, name
, owner
, sym
, state
);
382 push_slist(stack
, slist
);
386 * get_state_stack() gets the state for the top slist on the stack.
388 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
389 const char *name
, int owner
,
392 struct state_list
*slist
;
393 struct smatch_state
*ret
;
395 slist
= pop_slist(&stack
);
396 ret
= get_state_slist(slist
, name
, owner
, sym
);
397 push_slist(&stack
, slist
);
402 * add_pool() adds a slist to ->pools. If the slist has already been
403 * added earlier then it doesn't get added a second time.
405 static void add_pool(struct sm_state
*to
, struct state_list
*new)
407 struct state_list
*tmp
;
409 FOR_EACH_PTR(to
->pools
, tmp
) {
412 else if (tmp
== new) {
415 INSERT_CURRENT(new, tmp
);
418 } END_FOR_EACH_PTR(tmp
);
419 add_ptr_list(&to
->pools
, new);
422 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
424 struct state_list
*tmp
;
426 FOR_EACH_PTR(sm
->pools
, tmp
) {
428 } END_FOR_EACH_PTR(tmp
);
432 * merge_slist() is called whenever paths merge, such as after
433 * an if statement. It takes the two slists and creates one.
435 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
437 struct sm_state
*to_state
, *state
, *tmp
;
438 struct state_list
*results
= NULL
;
439 struct state_list
*implied_to
= NULL
;
440 struct state_list
*implied_from
= NULL
;
447 /* merging a null and nonnull path gives you only the nonnull path */
452 *to
= clone_slist(slist
);
456 PREPARE_PTR_LIST(*to
, to_state
);
457 PREPARE_PTR_LIST(slist
, state
);
459 if (!to_state
&& !state
)
461 if (cmp_tracker(to_state
, state
) < 0) {
462 tmp
= merge_sm_states(to_state
, NULL
);
464 copy_pools(tmp
, to_state
);
466 add_ptr_list(&implied_to
, to_state
);
467 add_pool(tmp
, implied_to
);
469 add_ptr_list(&results
, tmp
);
470 NEXT_PTR_LIST(to_state
);
471 } else if (cmp_tracker(to_state
, state
) == 0) {
472 if (to_state
->state
== state
->state
) {
475 tmp
= merge_sm_states(to_state
, state
);
477 copy_pools(tmp
, to_state
);
478 copy_pools(tmp
, state
);
480 add_ptr_list(&implied_to
, to_state
);
481 add_pool(tmp
, implied_to
);
482 add_ptr_list(&implied_from
, state
);
483 add_pool(tmp
, implied_from
);
485 add_ptr_list(&results
, tmp
);
486 NEXT_PTR_LIST(to_state
);
487 NEXT_PTR_LIST(state
);
489 tmp
= merge_sm_states(state
, NULL
);
491 copy_pools(tmp
, state
);
493 add_ptr_list(&implied_from
, state
);
494 add_pool(tmp
, implied_from
);
496 add_ptr_list(&results
, tmp
);
497 NEXT_PTR_LIST(state
);
500 FINISH_PTR_LIST(state
);
501 FINISH_PTR_LIST(to_state
);
507 push_slist(&implied_pools
, implied_from
);
509 push_slist(&implied_pools
, implied_to
);
513 * is_currently_in_pool() is used because we remove states from pools.
514 * When set_state() is called then we set ->pools to NULL, but on
515 * other paths the state is still a member of those pools.
526 static int is_currently_in_pool(struct sm_state
*sm
, struct state_list
*pool
,
527 struct state_list
*cur_slist
)
529 struct sm_state
*cur_state
;
530 struct state_list
*tmp
;
532 cur_state
= get_sm_state_slist(cur_slist
, sm
->name
, sm
->owner
, sm
->sym
);
536 FOR_EACH_PTR(cur_state
->pools
, tmp
) {
539 } END_FOR_EACH_PTR(tmp
);
543 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
544 struct state_list
*cur_slist
)
546 struct sm_state
*state
;
547 struct sm_state
*tmp
;
548 struct state_list
*to_slist
= NULL
;
550 FOR_EACH_PTR(pool
, state
) {
551 if (is_currently_in_pool(state
, pool
, cur_slist
)) {
552 tmp
= clone_state(state
);
553 add_ptr_list(&to_slist
, tmp
);
555 } END_FOR_EACH_PTR(state
);
557 check_order(to_slist
);
563 * filter() is used to find what states are the same across
564 * a series of slists.
565 * It takes a **slist and a *filter.
566 * It removes everything from **slist that isn't in *filter.
567 * The reason you would want to do this is if you want to
568 * know what other states are true if one state is true. (smatch_implied).
570 void filter(struct state_list
**slist
, struct state_list
*filter
,
571 struct state_list
*cur_slist
)
573 struct sm_state
*s_one
, *s_two
;
574 struct state_list
*results
= NULL
;
581 PREPARE_PTR_LIST(*slist
, s_one
);
582 PREPARE_PTR_LIST(filter
, s_two
);
584 if (!s_one
|| !s_two
)
586 if (cmp_tracker(s_one
, s_two
) < 0) {
587 NEXT_PTR_LIST(s_one
);
588 } else if (cmp_tracker(s_one
, s_two
) == 0) {
589 /* todo. pointer comparison works fine for most things
590 except smatch_extra. we may need a hook here. */
591 if (s_one
->state
== s_two
->state
&&
592 is_currently_in_pool(s_two
, filter
, cur_slist
)) {
593 add_ptr_list(&results
, s_one
);
595 NEXT_PTR_LIST(s_one
);
596 NEXT_PTR_LIST(s_two
);
598 NEXT_PTR_LIST(s_two
);
601 FINISH_PTR_LIST(s_two
);
602 FINISH_PTR_LIST(s_one
);
609 * and_slist_stack() is basically the same as popping the top two slists,
610 * overwriting the one with the other and pushing it back on the stack.
611 * The difference is that it checks to see that a mutually exclusive
612 * state isn't included in both stacks. If smatch sees something like
613 * "if (a && !a)" it prints a warning.
615 void and_slist_stack(struct state_list_stack
**slist_stack
)
617 struct sm_state
*tmp
;
618 struct smatch_state
*tmp_state
;
619 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
621 FOR_EACH_PTR(tmp_slist
, tmp
) {
622 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
623 tmp
->owner
, tmp
->sym
);
624 if (tmp_state
&& tmp_state
!= tmp
->state
) {
625 smatch_msg("mutually exclusive 'and' conditions states "
627 tmp
->name
, show_state(tmp_state
),
628 show_state(tmp
->state
));
629 tmp
->state
= merge_states(tmp
->name
, tmp
->owner
,
630 tmp
->sym
, tmp
->state
,
633 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
635 } END_FOR_EACH_PTR(tmp
);
636 del_slist(&tmp_slist
);
640 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
641 * It pops the two slists from the top of the stack and merges them
642 * together in a way that preserves the things they have in common
643 * but creates a merged state for most of the rest.
644 * You could have code that had: if (foo || foo) { foo->baz;
645 * It's this function which ensures smatch does the right thing.
647 void or_slist_stack(struct state_list_stack
**slist_stack
)
649 struct state_list
*one
;
650 struct state_list
*two
;
651 struct state_list
*res
= NULL
;
652 struct sm_state
*tmp
;
654 struct sm_state
*new_sm
;
656 one
= pop_slist(slist_stack
);
657 two
= pop_slist(slist_stack
);
659 FOR_EACH_PTR(one
, tmp
) {
660 sm
= get_sm_state_slist(two
, tmp
->name
, tmp
->owner
, tmp
->sym
);
661 new_sm
= merge_sm_states(tmp
, sm
);
662 add_ptr_list(&res
, new_sm
);
663 } END_FOR_EACH_PTR(tmp
);
665 FOR_EACH_PTR(two
, tmp
) {
666 sm
= get_sm_state_slist(one
, tmp
->name
, tmp
->owner
, tmp
->sym
);
667 new_sm
= merge_sm_states(tmp
, sm
);
668 add_ptr_list(&res
, new_sm
);
669 } END_FOR_EACH_PTR(tmp
);
671 push_slist(slist_stack
, res
);
678 * get_slist_from_named_stack() is only used for gotos.
680 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
683 struct named_slist
*tmp
;
685 FOR_EACH_PTR(stack
, tmp
) {
686 if (!strcmp(tmp
->name
, name
))
688 } END_FOR_EACH_PTR(tmp
);
692 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
694 struct sm_state
*tmp
;
696 FOR_EACH_PTR(from
, tmp
) {
697 overwrite_sm_state(to
, tmp
);
698 } END_FOR_EACH_PTR(tmp
);