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
;
116 if (slist_has_state(sm
->possible
, &undefined
))
118 tmp
= alloc_state(sm
->name
, sm
->owner
, sm
->sym
, &undefined
);
119 add_sm_state_slist(&sm
->possible
, tmp
);
122 FOR_EACH_PTR(new->possible
, tmp
) {
123 add_sm_state_slist(&sm
->possible
, tmp
);
124 } END_FOR_EACH_PTR(tmp
);
127 struct sm_state
*alloc_state(const char *name
, int owner
,
128 struct symbol
*sym
, struct smatch_state
*state
)
130 struct sm_state
*sm_state
= __alloc_sm_state(0);
132 sm_state
->name
= (char *)name
;
133 sm_state
->owner
= owner
;
135 sm_state
->state
= state
;
136 sm_state
->line_history
= NULL
;
137 add_history(sm_state
);
138 sm_state
->pools
= NULL
;
139 sm_state
->possible
= NULL
;
140 add_ptr_list(&sm_state
->possible
, sm_state
);
144 struct sm_state
*clone_state(struct sm_state
*s
)
146 struct sm_state
*tmp
;
148 tmp
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
149 tmp
->pools
= clone_stack(s
->pools
);
150 tmp
->possible
= s
->possible
;
154 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
156 struct sm_state
*tmp
;
158 FOR_EACH_PTR(slist
, tmp
) {
159 if (tmp
->state
== state
)
161 } END_FOR_EACH_PTR(tmp
);
166 static void check_order(struct state_list
*slist
)
168 struct sm_state
*state
;
169 struct sm_state
*last
= NULL
;
172 FOR_EACH_PTR(slist
, state
) {
173 if (last
&& cmp_tracker(state
, last
) <= 0) {
174 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
175 "%s vs %s\n", last
->owner
, state
->owner
,
176 last
->sym
, state
->sym
, last
->name
, state
->name
);
180 } END_FOR_EACH_PTR(state
);
187 struct state_list
*clone_slist(struct state_list
*from_slist
)
189 struct sm_state
*state
;
190 struct sm_state
*tmp
;
191 struct state_list
*to_slist
= NULL
;
193 FOR_EACH_PTR(from_slist
, state
) {
194 tmp
= clone_state(state
);
195 add_ptr_list(&to_slist
, tmp
);
196 } END_FOR_EACH_PTR(state
);
198 check_order(to_slist
);
203 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
205 struct state_list
*slist
;
206 struct state_list_stack
*to_stack
= NULL
;
208 FOR_EACH_PTR(from_stack
, slist
) {
209 push_slist(&to_stack
, slist
);
210 } END_FOR_EACH_PTR(slist
);
214 // FIXME... shouldn't we free some of these state pointers?
215 struct smatch_state
*merge_states(const char *name
, int owner
,
217 struct smatch_state
*state1
,
218 struct smatch_state
*state2
)
220 struct smatch_state
*ret
;
222 if (state1
== state2
)
224 else if (__has_merge_function(owner
))
225 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
229 SM_DEBUG("%d merge name='%s' owner=%d: %s + %s => %s\n",
230 get_lineno(), name
, owner
, show_state(state1
),
231 show_state(state2
), show_state(ret
));
236 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
238 struct smatch_state
*s
;
239 struct sm_state
*result
;
241 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
242 (two
?two
->state
:NULL
));
243 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
244 add_possible(result
, one
);
245 add_possible(result
, two
);
249 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
250 int owner
, struct symbol
*sym
)
252 struct sm_state
*state
;
257 FOR_EACH_PTR(slist
, state
) {
258 if (state
->owner
== owner
&& state
->sym
== sym
259 && !strcmp(state
->name
, name
))
261 } END_FOR_EACH_PTR(state
);
265 struct smatch_state
*get_state_slist(struct state_list
*slist
,
266 const char *name
, int owner
,
269 struct sm_state
*state
;
271 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
277 static void overwrite_sm_state(struct state_list
**slist
,
278 struct sm_state
*state
)
280 struct sm_state
*tmp
;
281 struct sm_state
*new = clone_state(state
); //fixme. why?
283 FOR_EACH_PTR(*slist
, tmp
) {
284 if (cmp_tracker(tmp
, new) < 0)
286 else if (cmp_tracker(tmp
, new) == 0) {
287 tmp
->state
= new->state
;
288 tmp
->pools
= new->pools
;
289 tmp
->possible
= new->possible
;
290 __free_sm_state(new);
293 INSERT_CURRENT(new, tmp
);
296 } END_FOR_EACH_PTR(tmp
);
297 add_ptr_list(slist
, new);
300 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
301 struct symbol
*sym
, struct smatch_state
*state
)
303 struct sm_state
*tmp
;
304 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
306 FOR_EACH_PTR(*slist
, tmp
) {
307 if (cmp_tracker(tmp
, new) < 0)
309 else if (cmp_tracker(tmp
, new) == 0) {
312 tmp
->possible
= NULL
;
313 add_ptr_list(&tmp
->possible
, tmp
);
314 __free_sm_state(new);
317 INSERT_CURRENT(new, tmp
);
320 } END_FOR_EACH_PTR(tmp
);
321 add_ptr_list(slist
, new);
324 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
327 struct sm_state
*state
;
329 FOR_EACH_PTR(*slist
, state
) {
330 if (state
->owner
== owner
&& state
->sym
== sym
331 && !strcmp(state
->name
, name
)){
332 delete_ptr_list_entry((struct ptr_list
**)slist
,
334 __free_sm_state(state
);
337 } END_FOR_EACH_PTR(state
);
341 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
343 add_ptr_list(list_stack
, slist
);
346 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
348 struct state_list
*slist
;
350 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
351 delete_ptr_list_last((struct ptr_list
**)list_stack
);
355 void del_slist(struct state_list
**slist
)
357 __free_ptr_list((struct ptr_list
**)slist
);
360 void del_slist_stack(struct state_list_stack
**slist_stack
)
362 struct state_list
*slist
;
364 FOR_EACH_PTR(*slist_stack
, slist
) {
365 __free_ptr_list((struct ptr_list
**)&slist
);
366 } END_FOR_EACH_PTR(slist
);
367 __free_ptr_list((struct ptr_list
**)slist_stack
);
371 * set_state_stack() sets the state for the top slist on the stack.
373 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
374 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
376 struct state_list
*slist
;
378 slist
= pop_slist(stack
);
379 set_state_slist(&slist
, name
, owner
, sym
, state
);
380 push_slist(stack
, slist
);
384 * get_state_stack() gets the state for the top slist on the stack.
386 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
387 const char *name
, int owner
,
390 struct state_list
*slist
;
391 struct smatch_state
*ret
;
393 slist
= pop_slist(&stack
);
394 ret
= get_state_slist(slist
, name
, owner
, sym
);
395 push_slist(&stack
, slist
);
400 * add_pool() adds a slist to ->pools. If the slist has already been
401 * added earlier then it doesn't get added a second time.
403 static void add_pool(struct sm_state
*to
, struct state_list
*new)
405 struct state_list
*tmp
;
407 FOR_EACH_PTR(to
->pools
, tmp
) {
410 else if (tmp
== new) {
413 INSERT_CURRENT(new, tmp
);
416 } END_FOR_EACH_PTR(tmp
);
417 add_ptr_list(&to
->pools
, new);
420 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
422 struct state_list
*tmp
;
424 FOR_EACH_PTR(sm
->pools
, tmp
) {
426 } END_FOR_EACH_PTR(tmp
);
430 * merge_slist() is called whenever paths merge, such as after
431 * an if statement. It takes the two slists and creates one.
433 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
435 struct sm_state
*to_state
, *state
, *tmp
;
436 struct state_list
*results
= NULL
;
437 struct smatch_state
*s
;
438 struct state_list
*implied_to
= NULL
;
439 struct state_list
*implied_from
= NULL
;
446 /* merging a null and nonnull path gives you only the nonnull path */
451 *to
= clone_slist(slist
);
455 PREPARE_PTR_LIST(*to
, to_state
);
456 PREPARE_PTR_LIST(slist
, state
);
458 if (!to_state
&& !state
)
460 if (cmp_tracker(to_state
, state
) < 0) {
461 tmp
= merge_sm_states(to_state
, NULL
);
463 copy_pools(tmp
, to_state
);
465 add_ptr_list(&implied_to
, to_state
);
466 add_pool(tmp
, implied_to
);
468 add_ptr_list(&results
, tmp
);
469 NEXT_PTR_LIST(to_state
);
470 } else if (cmp_tracker(to_state
, state
) == 0) {
471 if (to_state
->state
== state
->state
) {
473 tmp
= alloc_state(to_state
->name
,
476 copy_pools(tmp
, to_state
);
477 copy_pools(tmp
, state
);
479 tmp
= merge_sm_states(to_state
, state
);
481 copy_pools(tmp
, to_state
);
482 copy_pools(tmp
, state
);
484 add_ptr_list(&implied_to
, to_state
);
485 add_pool(tmp
, implied_to
);
486 add_ptr_list(&implied_from
, state
);
487 add_pool(tmp
, implied_from
);
489 add_ptr_list(&results
, tmp
);
490 NEXT_PTR_LIST(to_state
);
491 NEXT_PTR_LIST(state
);
493 tmp
= merge_sm_states(state
, NULL
);
495 copy_pools(tmp
, state
);
497 add_ptr_list(&implied_from
, state
);
498 add_pool(tmp
, implied_from
);
500 add_ptr_list(&results
, tmp
);
501 NEXT_PTR_LIST(state
);
504 FINISH_PTR_LIST(state
);
505 FINISH_PTR_LIST(to_state
);
511 push_slist(&implied_pools
, implied_from
);
513 push_slist(&implied_pools
, implied_to
);
517 * is_currently_in_pool() is used because we remove states from pools.
518 * When set_state() is called then we set ->pools to NULL, but on
519 * other paths the state is still a member of those pools.
530 static int is_currently_in_pool(struct sm_state
*sm
, struct state_list
*pool
,
531 struct state_list
*cur_slist
)
533 struct sm_state
*cur_state
;
534 struct state_list
*tmp
;
536 cur_state
= get_sm_state_slist(cur_slist
, sm
->name
, sm
->owner
, sm
->sym
);
540 FOR_EACH_PTR(cur_state
->pools
, tmp
) {
543 } END_FOR_EACH_PTR(tmp
);
547 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
548 struct state_list
*cur_slist
)
550 struct sm_state
*state
;
551 struct sm_state
*tmp
;
552 struct state_list
*to_slist
= NULL
;
554 FOR_EACH_PTR(pool
, state
) {
555 if (is_currently_in_pool(state
, pool
, cur_slist
)) {
556 tmp
= clone_state(state
);
557 add_ptr_list(&to_slist
, tmp
);
559 } END_FOR_EACH_PTR(state
);
561 check_order(to_slist
);
567 * filter() is used to find what states are the same across
568 * a series of slists.
569 * It takes a **slist and a *filter.
570 * It removes everything from **slist that isn't in *filter.
571 * The reason you would want to do this is if you want to
572 * know what other states are true if one state is true. (smatch_implied).
574 void filter(struct state_list
**slist
, struct state_list
*filter
,
575 struct state_list
*cur_slist
)
577 struct sm_state
*s_one
, *s_two
;
578 struct state_list
*results
= NULL
;
585 PREPARE_PTR_LIST(*slist
, s_one
);
586 PREPARE_PTR_LIST(filter
, s_two
);
588 if (!s_one
|| !s_two
)
590 if (cmp_tracker(s_one
, s_two
) < 0) {
591 NEXT_PTR_LIST(s_one
);
592 } else if (cmp_tracker(s_one
, s_two
) == 0) {
593 /* todo. pointer comparison works fine for most things
594 except smatch_extra. we may need a hook here. */
595 if (s_one
->state
== s_two
->state
&&
596 is_currently_in_pool(s_two
, filter
, cur_slist
)) {
597 add_ptr_list(&results
, s_one
);
599 NEXT_PTR_LIST(s_one
);
600 NEXT_PTR_LIST(s_two
);
602 NEXT_PTR_LIST(s_two
);
605 FINISH_PTR_LIST(s_two
);
606 FINISH_PTR_LIST(s_one
);
613 * and_slist_stack() is basically the same as popping the top two slists,
614 * overwriting the one with the other and pushing it back on the stack.
615 * The difference is that it checks to see that a mutually exclusive
616 * state isn't included in both stacks. If smatch sees something like
617 * "if (a && !a)" it prints a warning.
619 void and_slist_stack(struct state_list_stack
**slist_stack
)
621 struct sm_state
*tmp
;
622 struct smatch_state
*tmp_state
;
623 struct state_list
*tmp_slist
= pop_slist(slist_stack
);
625 FOR_EACH_PTR(tmp_slist
, tmp
) {
626 tmp_state
= get_state_stack(*slist_stack
, tmp
->name
,
627 tmp
->owner
, tmp
->sym
);
628 if (tmp_state
&& tmp_state
!= tmp
->state
) {
629 smatch_msg("mutually exclusive 'and' conditions states "
631 tmp
->name
, show_state(tmp_state
),
632 show_state(tmp
->state
));
633 tmp
->state
= merge_states(tmp
->name
, tmp
->owner
,
634 tmp
->sym
, tmp
->state
,
637 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
639 } END_FOR_EACH_PTR(tmp
);
640 del_slist(&tmp_slist
);
644 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
645 * It pops the two slists from the top of the stack and merges them
646 * together in a way that preserves the things they have in common
647 * but creates a merged state for most of the rest.
648 * You could have code that had: if (foo || foo) { foo->baz;
649 * It's this function which ensures smatch does the right thing.
651 void or_slist_stack(struct state_list_stack
**slist_stack
)
653 struct state_list
*one
;
654 struct state_list
*two
;
655 struct state_list
*res
= NULL
;
656 struct sm_state
*tmp
;
658 struct sm_state
*new_sm
;
660 one
= pop_slist(slist_stack
);
661 two
= pop_slist(slist_stack
);
663 FOR_EACH_PTR(one
, tmp
) {
664 sm
= get_sm_state_slist(two
, tmp
->name
, tmp
->owner
, tmp
->sym
);
665 new_sm
= merge_sm_states(tmp
, sm
);
666 add_ptr_list(&res
, new_sm
);
667 } END_FOR_EACH_PTR(tmp
);
669 FOR_EACH_PTR(two
, tmp
) {
670 sm
= get_sm_state_slist(one
, tmp
->name
, tmp
->owner
, tmp
->sym
);
671 new_sm
= merge_sm_states(tmp
, sm
);
672 add_ptr_list(&res
, new_sm
);
673 } END_FOR_EACH_PTR(tmp
);
675 push_slist(slist_stack
, res
);
682 * get_slist_from_named_stack() is only used for gotos.
684 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
687 struct named_slist
*tmp
;
689 FOR_EACH_PTR(stack
, tmp
) {
690 if (!strcmp(tmp
->name
, name
))
692 } END_FOR_EACH_PTR(tmp
);
696 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
698 struct sm_state
*tmp
;
700 FOR_EACH_PTR(from
, tmp
) {
701 overwrite_sm_state(to
, tmp
);
702 } END_FOR_EACH_PTR(tmp
);