2 * sparse/smatch_slist.c
4 * Copyright (C) 2008,2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_slist.h"
17 ALLOCATOR(sm_state
, "smatch state");
18 ALLOCATOR(named_slist
, "named slist");
20 void __print_slist(struct state_list
*slist
)
22 struct sm_state
*state
;
24 printf("dumping slist at %d\n", get_lineno());
25 FOR_EACH_PTR(slist
, state
) {
26 printf("%d '%s'=%s\n", state
->owner
, state
->name
,
27 show_state(state
->state
));
28 } END_FOR_EACH_PTR(state
);
32 void add_history(struct sm_state
*state
)
34 struct state_history
*tmp
;
38 tmp
= malloc(sizeof(*tmp
));
39 tmp
->loc
= get_lineno();
40 add_ptr_list(&state
->line_history
, tmp
);
44 /* NULL states go at the end to simplify merge_slist */
45 int cmp_tracker(const struct sm_state
*a
, const struct sm_state
*b
)
56 if (a
->owner
> b
->owner
)
58 if (a
->owner
< b
->owner
)
61 ret
= strcmp(a
->name
, b
->name
);
65 if (!b
->sym
&& a
->sym
)
67 if (!a
->sym
&& b
->sym
)
77 static int cmp_sm_states(const struct sm_state
*a
, const struct sm_state
*b
)
81 ret
= cmp_tracker(a
, b
);
85 /* todo: add hook for smatch_extra.c */
86 if (a
->state
> b
->state
)
88 if (a
->state
< b
->state
)
93 void add_sm_state_slist(struct state_list
**slist
, struct sm_state
*new)
97 FOR_EACH_PTR(*slist
, tmp
) {
98 if (cmp_sm_states(tmp
, new) < 0)
100 else if (cmp_sm_states(tmp
, new) == 0) {
103 INSERT_CURRENT(new, tmp
);
106 } END_FOR_EACH_PTR(tmp
);
107 add_ptr_list(slist
, new);
110 static void add_possible(struct sm_state
*sm
, struct sm_state
*new)
112 struct sm_state
*tmp
;
116 struct smatch_state
*s
;
118 s
= merge_states(sm
->name
, sm
->owner
, sm
->sym
, sm
->state
, NULL
);
119 tmp
= alloc_state(sm
->name
, sm
->owner
, sm
->sym
, s
);
120 add_sm_state_slist(&sm
->possible
, tmp
);
123 FOR_EACH_PTR(new->possible
, tmp
) {
124 add_sm_state_slist(&sm
->possible
, tmp
);
125 } END_FOR_EACH_PTR(tmp
);
128 struct sm_state
*alloc_state(const char *name
, int owner
,
129 struct symbol
*sym
, struct smatch_state
*state
)
131 struct sm_state
*sm_state
= __alloc_sm_state(0);
133 sm_state
->name
= (char *)name
;
134 sm_state
->owner
= owner
;
136 sm_state
->state
= state
;
137 sm_state
->line_history
= NULL
;
138 add_history(sm_state
);
139 sm_state
->pools
= NULL
;
140 sm_state
->possible
= NULL
;
141 add_ptr_list(&sm_state
->possible
, sm_state
);
145 struct sm_state
*clone_state(struct sm_state
*s
)
147 struct sm_state
*tmp
;
149 tmp
= alloc_state(s
->name
, s
->owner
, s
->sym
, s
->state
);
150 tmp
->pools
= clone_stack(s
->pools
);
151 tmp
->possible
= s
->possible
;
155 int slist_has_state(struct state_list
*slist
, struct smatch_state
*state
)
157 struct sm_state
*tmp
;
159 FOR_EACH_PTR(slist
, tmp
) {
160 if (tmp
->state
== state
)
162 } END_FOR_EACH_PTR(tmp
);
167 static void check_order(struct state_list
*slist
)
169 struct sm_state
*state
;
170 struct sm_state
*last
= NULL
;
173 FOR_EACH_PTR(slist
, state
) {
174 if (last
&& cmp_tracker(state
, last
) <= 0) {
175 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
176 "%s vs %s\n", last
->owner
, state
->owner
,
177 last
->sym
, state
->sym
, last
->name
, state
->name
);
181 } END_FOR_EACH_PTR(state
);
188 struct state_list
*clone_slist(struct state_list
*from_slist
)
190 struct sm_state
*state
;
191 struct sm_state
*tmp
;
192 struct state_list
*to_slist
= NULL
;
194 FOR_EACH_PTR(from_slist
, state
) {
195 tmp
= clone_state(state
);
196 add_ptr_list(&to_slist
, tmp
);
197 } END_FOR_EACH_PTR(state
);
199 check_order(to_slist
);
204 struct state_list_stack
*clone_stack(struct state_list_stack
*from_stack
)
206 struct state_list
*slist
;
207 struct state_list_stack
*to_stack
= NULL
;
209 FOR_EACH_PTR(from_stack
, slist
) {
210 push_slist(&to_stack
, slist
);
211 } END_FOR_EACH_PTR(slist
);
215 // FIXME... shouldn't we free some of these state pointers?
216 struct smatch_state
*merge_states(const char *name
, int owner
,
218 struct smatch_state
*state1
,
219 struct smatch_state
*state2
)
221 struct smatch_state
*ret
;
223 if (state1
== state2
)
225 else if (__has_merge_function(owner
))
226 ret
= __client_merge_function(owner
, name
, sym
, state1
, state2
);
227 else if (!state1
|| !state2
)
235 * add_pool() adds a slist to ->pools. If the slist has already been
236 * added earlier then it doesn't get added a second time.
238 static void add_pool(struct sm_state
*to
, struct state_list
*new)
240 struct state_list
*tmp
;
242 FOR_EACH_PTR(to
->pools
, tmp
) {
245 else if (tmp
== new) {
248 INSERT_CURRENT(new, tmp
);
251 } END_FOR_EACH_PTR(tmp
);
252 add_ptr_list(&to
->pools
, new);
255 static void copy_pools(struct sm_state
*to
, struct sm_state
*sm
)
257 struct state_list
*tmp
;
262 FOR_EACH_PTR(sm
->pools
, tmp
) {
264 } END_FOR_EACH_PTR(tmp
);
267 struct sm_state
*merge_sm_states(struct sm_state
*one
, struct sm_state
*two
)
269 struct smatch_state
*s
;
270 struct sm_state
*result
;
272 s
= merge_states(one
->name
, one
->owner
, one
->sym
, one
->state
,
273 (two
?two
->state
:NULL
));
274 result
= alloc_state(one
->name
, one
->owner
, one
->sym
, s
);
275 add_possible(result
, one
);
276 add_possible(result
, two
);
277 copy_pools(result
, one
);
278 copy_pools(result
, two
);
281 struct sm_state
*tmp
;
284 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
285 get_lineno(), one
->name
, one
->owner
,
286 show_state(one
->state
), show_state(two
?two
->state
:NULL
),
289 FOR_EACH_PTR(result
->possible
, tmp
) {
293 printf("%s", show_state(tmp
->state
));
294 } END_FOR_EACH_PTR(tmp
);
301 struct sm_state
*get_sm_state_slist(struct state_list
*slist
, const char *name
,
302 int owner
, struct symbol
*sym
)
304 struct sm_state
*state
;
309 FOR_EACH_PTR(slist
, state
) {
310 if (state
->owner
== owner
&& state
->sym
== sym
311 && !strcmp(state
->name
, name
))
313 } END_FOR_EACH_PTR(state
);
317 struct smatch_state
*get_state_slist(struct state_list
*slist
,
318 const char *name
, int owner
,
321 struct sm_state
*state
;
323 state
= get_sm_state_slist(slist
, name
, owner
, sym
);
329 static void overwrite_sm_state(struct state_list
**slist
,
330 struct sm_state
*state
)
332 struct sm_state
*tmp
;
333 struct sm_state
*new = clone_state(state
); //fixme. why?
335 FOR_EACH_PTR(*slist
, tmp
) {
336 if (cmp_tracker(tmp
, new) < 0)
338 else if (cmp_tracker(tmp
, new) == 0) {
339 tmp
->state
= new->state
;
340 tmp
->pools
= new->pools
;
341 tmp
->possible
= new->possible
;
342 __free_sm_state(new);
345 INSERT_CURRENT(new, tmp
);
348 } END_FOR_EACH_PTR(tmp
);
349 add_ptr_list(slist
, new);
352 void set_state_slist(struct state_list
**slist
, const char *name
, int owner
,
353 struct symbol
*sym
, struct smatch_state
*state
)
355 struct sm_state
*tmp
;
356 struct sm_state
*new = alloc_state(name
, owner
, sym
, state
);
358 FOR_EACH_PTR(*slist
, tmp
) {
359 if (cmp_tracker(tmp
, new) < 0)
361 else if (cmp_tracker(tmp
, new) == 0) {
364 tmp
->possible
= NULL
;
365 add_ptr_list(&tmp
->possible
, tmp
);
366 __free_sm_state(new);
369 INSERT_CURRENT(new, tmp
);
372 } END_FOR_EACH_PTR(tmp
);
373 add_ptr_list(slist
, new);
376 void delete_state_slist(struct state_list
**slist
, const char *name
, int owner
,
379 struct sm_state
*state
;
381 FOR_EACH_PTR(*slist
, state
) {
382 if (state
->owner
== owner
&& state
->sym
== sym
383 && !strcmp(state
->name
, name
)){
384 delete_ptr_list_entry((struct ptr_list
**)slist
,
386 __free_sm_state(state
);
389 } END_FOR_EACH_PTR(state
);
393 void push_slist(struct state_list_stack
**list_stack
, struct state_list
*slist
)
395 add_ptr_list(list_stack
, slist
);
398 struct state_list
*pop_slist(struct state_list_stack
**list_stack
)
400 struct state_list
*slist
;
402 slist
= last_ptr_list((struct ptr_list
*)*list_stack
);
403 delete_ptr_list_last((struct ptr_list
**)list_stack
);
407 void del_slist(struct state_list
**slist
)
409 __free_ptr_list((struct ptr_list
**)slist
);
412 void del_slist_stack(struct state_list_stack
**slist_stack
)
414 struct state_list
*slist
;
416 FOR_EACH_PTR(*slist_stack
, slist
) {
417 __free_ptr_list((struct ptr_list
**)&slist
);
418 } END_FOR_EACH_PTR(slist
);
419 __free_ptr_list((struct ptr_list
**)slist_stack
);
423 * set_state_stack() sets the state for the top slist on the stack.
425 void set_state_stack(struct state_list_stack
**stack
, const char *name
,
426 int owner
, struct symbol
*sym
, struct smatch_state
*state
)
428 struct state_list
*slist
;
430 slist
= pop_slist(stack
);
431 set_state_slist(&slist
, name
, owner
, sym
, state
);
432 push_slist(stack
, slist
);
436 * get_state_stack() gets the state for the top slist on the stack.
438 struct smatch_state
*get_state_stack(struct state_list_stack
*stack
,
439 const char *name
, int owner
,
442 struct state_list
*slist
;
443 struct smatch_state
*ret
;
445 slist
= pop_slist(&stack
);
446 ret
= get_state_slist(slist
, name
, owner
, sym
);
447 push_slist(&stack
, slist
);
452 * merge_slist() is called whenever paths merge, such as after
453 * an if statement. It takes the two slists and creates one.
455 void merge_slist(struct state_list
**to
, struct state_list
*slist
)
457 struct sm_state
*to_state
, *state
, *tmp
;
458 struct state_list
*results
= NULL
;
459 struct state_list
*implied_to
= NULL
;
460 struct state_list
*implied_from
= NULL
;
467 /* merging a null and nonnull path gives you only the nonnull path */
472 *to
= clone_slist(slist
);
476 implied_to
= clone_slist(*to
);
477 implied_from
= clone_slist(slist
);
479 PREPARE_PTR_LIST(*to
, to_state
);
480 PREPARE_PTR_LIST(slist
, state
);
482 if (!to_state
&& !state
)
484 if (cmp_tracker(to_state
, state
) < 0) {
485 tmp
= merge_sm_states(to_state
, NULL
);
486 add_pool(tmp
, implied_to
);
487 add_ptr_list(&results
, tmp
);
488 NEXT_PTR_LIST(to_state
);
489 } else if (cmp_tracker(to_state
, state
) == 0) {
490 tmp
= merge_sm_states(to_state
, state
);
491 add_pool(tmp
, implied_to
);
492 add_pool(tmp
, implied_from
);
493 add_ptr_list(&results
, tmp
);
494 NEXT_PTR_LIST(to_state
);
495 NEXT_PTR_LIST(state
);
497 tmp
= merge_sm_states(state
, NULL
);
498 add_pool(tmp
, implied_from
);
499 add_ptr_list(&results
, tmp
);
500 NEXT_PTR_LIST(state
);
503 FINISH_PTR_LIST(state
);
504 FINISH_PTR_LIST(to_state
);
509 push_slist(&implied_pools
, implied_from
);
510 push_slist(&implied_pools
, implied_to
);
514 * is_currently_in_pool() is used because we remove states from pools.
515 * When set_state() is called then we set ->pools to NULL, but on
516 * other paths the state is still a member of those pools.
527 static int is_currently_in_pool(struct sm_state
*sm
, struct state_list
*pool
,
528 struct state_list
*cur_slist
)
530 struct sm_state
*cur_state
;
531 struct state_list
*tmp
;
533 cur_state
= get_sm_state_slist(cur_slist
, sm
->name
, sm
->owner
, sm
->sym
);
537 FOR_EACH_PTR(cur_state
->pools
, tmp
) {
540 } END_FOR_EACH_PTR(tmp
);
544 struct state_list
*clone_states_in_pool(struct state_list
*pool
,
545 struct state_list
*cur_slist
)
547 struct sm_state
*state
;
548 struct sm_state
*tmp
;
549 struct state_list
*to_slist
= NULL
;
551 FOR_EACH_PTR(pool
, state
) {
552 if (state
->state
== &merged
)
554 if (is_currently_in_pool(state
, pool
, cur_slist
)) {
555 tmp
= clone_state(state
);
556 add_ptr_list(&to_slist
, tmp
);
558 } END_FOR_EACH_PTR(state
);
560 check_order(to_slist
);
566 * filter() is used to find what states are the same across
567 * a series of slists.
568 * It takes a **slist and a *filter.
569 * It removes everything from **slist that isn't in *filter.
570 * The reason you would want to do this is if you want to
571 * know what other states are true if one state is true. (smatch_implied).
573 void filter(struct state_list
**slist
, struct state_list
*filter
,
574 struct state_list
*cur_slist
)
576 struct sm_state
*s_one
, *s_two
;
577 struct state_list
*results
= NULL
;
584 PREPARE_PTR_LIST(*slist
, s_one
);
585 PREPARE_PTR_LIST(filter
, s_two
);
587 if (!s_one
|| !s_two
)
589 if (cmp_tracker(s_one
, s_two
) < 0) {
590 NEXT_PTR_LIST(s_one
);
591 } else if (cmp_tracker(s_one
, s_two
) == 0) {
592 /* todo. pointer comparison works fine for most things
593 except smatch_extra. we may need a hook here. */
594 if (s_one
->state
== s_two
->state
&&
595 is_currently_in_pool(s_two
, filter
, cur_slist
)
596 && s_one
->state
!= &merged
) {
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 struct smatch_state
*s
;
631 s
= merge_states(tmp
->name
, tmp
->owner
, tmp
->sym
,
632 tmp
->state
, tmp_state
);
633 smatch_msg("mutually exclusive 'and' conditions states "
634 "'%s': %s + %s => %s",
635 tmp
->name
, show_state(tmp_state
),
636 show_state(tmp
->state
), show_state(s
));
640 set_state_stack(slist_stack
, tmp
->name
, tmp
->owner
, tmp
->sym
,
642 } END_FOR_EACH_PTR(tmp
);
643 del_slist(&tmp_slist
);
647 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
648 * It pops the two slists from the top of the stack and merges them
649 * together in a way that preserves the things they have in common
650 * but creates a merged state for most of the rest.
651 * You could have code that had: if (foo || foo) { foo->baz;
652 * It's this function which ensures smatch does the right thing.
654 void or_slist_stack(struct state_list_stack
**slist_stack
)
656 struct state_list
*one
;
657 struct state_list
*two
;
658 struct state_list
*res
= NULL
;
659 struct sm_state
*tmp
;
661 struct sm_state
*new_sm
;
663 one
= pop_slist(slist_stack
);
664 two
= pop_slist(slist_stack
);
666 FOR_EACH_PTR(one
, tmp
) {
667 sm
= get_sm_state_slist(two
, tmp
->name
, tmp
->owner
, tmp
->sym
);
668 new_sm
= merge_sm_states(tmp
, sm
);
669 add_ptr_list(&res
, new_sm
);
670 } END_FOR_EACH_PTR(tmp
);
672 FOR_EACH_PTR(two
, tmp
) {
673 sm
= get_sm_state_slist(one
, tmp
->name
, tmp
->owner
, tmp
->sym
);
674 new_sm
= merge_sm_states(tmp
, sm
);
675 add_ptr_list(&res
, new_sm
);
676 } END_FOR_EACH_PTR(tmp
);
678 push_slist(slist_stack
, res
);
685 * get_slist_from_named_stack() is only used for gotos.
687 struct state_list
**get_slist_from_named_stack(struct named_stack
*stack
,
690 struct named_slist
*tmp
;
692 FOR_EACH_PTR(stack
, tmp
) {
693 if (!strcmp(tmp
->name
, name
))
695 } END_FOR_EACH_PTR(tmp
);
699 void overwrite_slist(struct state_list
*from
, struct state_list
**to
)
701 struct sm_state
*tmp
;
703 FOR_EACH_PTR(from
, tmp
) {
704 overwrite_sm_state(to
, tmp
);
705 } END_FOR_EACH_PTR(tmp
);