Assignment always sets &isfreed to something else like &unfreed.
[smatch.git] / smatch_slist.c
bloba8e4c516b9d766eb886282b1d3498d700240c488
1 /*
2 * sparse/smatch_slist.c
4 * Copyright (C) 2008,2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "smatch.h"
13 #include "smatch.h"
14 #include "smatch_slist.h"
16 #undef CHECKORDER
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);
30 printf("---\n");
33 void add_history(struct sm_state *state)
35 struct state_history *tmp;
37 if (!state)
38 return;
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)
48 int ret;
50 if (!a && !b)
51 return 0;
52 if (!b)
53 return -1;
54 if (!a)
55 return 1;
57 if (a->owner > b->owner)
58 return -1;
59 if (a->owner < b->owner)
60 return 1;
62 ret = strcmp(a->name, b->name);
63 if (ret)
64 return ret;
66 if (!b->sym && a->sym)
67 return -1;
68 if (!a->sym && b->sym)
69 return 1;
70 if (a->sym > b->sym)
71 return -1;
72 if (a->sym < b->sym)
73 return 1;
75 return 0;
78 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b)
80 int ret;
82 ret = cmp_tracker(a, b);
83 if (ret)
84 return ret;
86 /* todo: add hook for smatch_extra.c */
87 if (a->state > b->state)
88 return -1;
89 if (a->state < b->state)
90 return 1;
91 return 0;
94 void add_sm_state_slist(struct state_list **slist, struct sm_state *new)
96 struct sm_state *tmp;
98 FOR_EACH_PTR(*slist, tmp) {
99 if (cmp_sm_states(tmp, new) < 0)
100 continue;
101 else if (cmp_sm_states(tmp, new) == 0) {
102 return;
103 } else {
104 INSERT_CURRENT(new, tmp);
105 return;
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 (!new) {
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);
122 return;
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;
136 sm_state->sym = sym;
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);
143 return 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;
153 return tmp;
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)
162 return 1;
163 } END_FOR_EACH_PTR(tmp);
164 return 0;
167 #ifdef CHECKORDER
168 static void check_order(struct state_list *slist)
170 struct sm_state *state;
171 struct sm_state *last = NULL;
172 int printed = 0;
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);
179 printed = 1;
181 last = state;
182 } END_FOR_EACH_PTR(state);
184 if (printed)
185 printf("======\n");
187 #endif
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);
199 #ifdef CHECKORDER
200 check_order(to_slist);
201 #endif
202 return 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);
213 return to_stack;
216 // FIXME... shouldn't we free some of these state pointers?
217 struct smatch_state *merge_states(const char *name, int owner,
218 struct symbol *sym,
219 struct smatch_state *state1,
220 struct smatch_state *state2)
222 struct smatch_state *ret;
224 if (state1 == state2)
225 ret = state1;
226 else if (__has_merge_function(owner))
227 ret = __client_merge_function(owner, name, sym, state1, state2);
228 else
229 ret = &merged;
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));
235 return 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);
248 return result;
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;
256 if (!name)
257 return NULL;
259 FOR_EACH_PTR(slist, state) {
260 if (state->owner == owner && state->sym == sym
261 && !strcmp(state->name, name))
262 return state;
263 } END_FOR_EACH_PTR(state);
264 return NULL;
267 struct smatch_state *get_state_slist(struct state_list *slist,
268 const char *name, int owner,
269 struct symbol *sym)
271 struct sm_state *state;
273 state = get_sm_state_slist(slist, name, owner, sym);
274 if (state)
275 return state->state;
276 return NULL;
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)
287 continue;
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);
293 return;
294 } else {
295 INSERT_CURRENT(new, tmp);
296 return;
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)
310 continue;
311 else if (cmp_tracker(tmp, new) == 0) {
312 tmp->state = state;
313 tmp->pools = NULL;
314 tmp->possible = NULL;
315 add_ptr_list(&tmp->possible, tmp);
316 __free_sm_state(new);
317 return;
318 } else {
319 INSERT_CURRENT(new, tmp);
320 return;
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,
327 struct symbol *sym)
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,
335 state, 1);
336 __free_sm_state(state);
337 return;
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);
354 return slist;
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,
390 struct symbol *sym)
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);
398 return ret;
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) {
410 if (tmp < new)
411 continue;
412 else if (tmp == new) {
413 return;
414 } else {
415 INSERT_CURRENT(new, tmp);
416 return;
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) {
427 add_pool(to, 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;
442 #ifdef CHECKORDER
443 check_order(*to);
444 check_order(slist);
445 #endif
447 /* merging a null and nonnull path gives you only the nonnull path */
448 if (!slist) {
449 return;
451 if (!*to) {
452 *to = clone_slist(slist);
453 return;
456 PREPARE_PTR_LIST(*to, to_state);
457 PREPARE_PTR_LIST(slist, state);
458 for (;;) {
459 if (!to_state && !state)
460 break;
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) {
473 tmp = to_state;
474 } else {
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);
488 } else {
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);
503 del_slist(to);
504 *to = results;
506 if (implied_from)
507 push_slist(&implied_pools, implied_from);
508 if (implied_to)
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.
516 * Confusing huh?
517 * if (foo) {
518 * bar = 1;
519 * a = malloc();
521 * if (!a)
522 * return;
523 * if (bar)
524 * a->b = x;
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);
533 if (!cur_state)
534 return 0;
536 FOR_EACH_PTR(cur_state->pools, tmp) {
537 if (tmp == pool)
538 return 1;
539 } END_FOR_EACH_PTR(tmp);
540 return 0;
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);
556 #ifdef CHECKORDER
557 check_order(to_slist);
558 #endif
559 return 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;
576 #ifdef CHECKORDER
577 check_order(*slist);
578 check_order(filter);
579 #endif
581 PREPARE_PTR_LIST(*slist, s_one);
582 PREPARE_PTR_LIST(filter, s_two);
583 for (;;) {
584 if (!s_one || !s_two)
585 break;
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);
597 } else {
598 NEXT_PTR_LIST(s_two);
601 FINISH_PTR_LIST(s_two);
602 FINISH_PTR_LIST(s_one);
604 del_slist(slist);
605 *slist = results;
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 "
626 "'%s': %s & %s.\n",
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,
631 tmp_state);
633 set_state_stack(slist_stack, tmp->name, tmp->owner, tmp->sym,
634 tmp->state);
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;
653 struct sm_state *sm;
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);
673 del_slist(&one);
674 del_slist(&two);
678 * get_slist_from_named_stack() is only used for gotos.
680 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
681 const char *name)
683 struct named_slist *tmp;
685 FOR_EACH_PTR(stack, tmp) {
686 if (!strcmp(tmp->name, name))
687 return &tmp->slist;
688 } END_FOR_EACH_PTR(tmp);
689 return NULL;
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);