All the states are implied not just unique ones.
[smatch.git] / smatch_slist.c
blob2ff7bc074ec24dbb8de001e9faa806842b29413d
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 if (!state1 || !state2)
229 ret = &undefined;
230 else
231 ret = &merged;
232 return ret;
236 * add_pool() adds a slist to ->pools. If the slist has already been
237 * added earlier then it doesn't get added a second time.
239 static void add_pool(struct sm_state *to, struct state_list *new)
241 struct state_list *tmp;
243 FOR_EACH_PTR(to->pools, tmp) {
244 if (tmp < new)
245 continue;
246 else if (tmp == new) {
247 return;
248 } else {
249 INSERT_CURRENT(new, tmp);
250 return;
252 } END_FOR_EACH_PTR(tmp);
253 add_ptr_list(&to->pools, new);
256 static void copy_pools(struct sm_state *to, struct sm_state *sm)
258 struct state_list *tmp;
260 if (!sm)
261 return;
263 FOR_EACH_PTR(sm->pools, tmp) {
264 add_pool(to, tmp);
265 } END_FOR_EACH_PTR(tmp);
268 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
270 struct smatch_state *s;
271 struct sm_state *result;
273 s = merge_states(one->name, one->owner, one->sym, one->state,
274 (two?two->state:NULL));
275 result = alloc_state(one->name, one->owner, one->sym, s);
276 add_possible(result, one);
277 add_possible(result, two);
278 copy_pools(result, one);
279 copy_pools(result, two);
281 if (debug_states) {
282 struct sm_state *tmp;
283 int i = 0;
285 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
286 get_lineno(), one->name, one->owner,
287 show_state(one->state), show_state(two?two->state:NULL),
288 show_state(s));
290 FOR_EACH_PTR(result->possible, tmp) {
291 if (i++) {
292 printf(", ");
294 printf("%s", show_state(tmp->state));
295 } END_FOR_EACH_PTR(tmp);
296 printf(")\n");
299 return result;
302 struct sm_state *get_sm_state_slist(struct state_list *slist, const char *name,
303 int owner, struct symbol *sym)
305 struct sm_state *state;
307 if (!name)
308 return NULL;
310 FOR_EACH_PTR(slist, state) {
311 if (state->owner == owner && state->sym == sym
312 && !strcmp(state->name, name))
313 return state;
314 } END_FOR_EACH_PTR(state);
315 return NULL;
318 struct smatch_state *get_state_slist(struct state_list *slist,
319 const char *name, int owner,
320 struct symbol *sym)
322 struct sm_state *state;
324 state = get_sm_state_slist(slist, name, owner, sym);
325 if (state)
326 return state->state;
327 return NULL;
330 static void overwrite_sm_state(struct state_list **slist,
331 struct sm_state *state)
333 struct sm_state *tmp;
334 struct sm_state *new = clone_state(state); //fixme. why?
336 FOR_EACH_PTR(*slist, tmp) {
337 if (cmp_tracker(tmp, new) < 0)
338 continue;
339 else if (cmp_tracker(tmp, new) == 0) {
340 tmp->state = new->state;
341 tmp->pools = new->pools;
342 tmp->possible = new->possible;
343 __free_sm_state(new);
344 return;
345 } else {
346 INSERT_CURRENT(new, tmp);
347 return;
349 } END_FOR_EACH_PTR(tmp);
350 add_ptr_list(slist, new);
353 void set_state_slist(struct state_list **slist, const char *name, int owner,
354 struct symbol *sym, struct smatch_state *state)
356 struct sm_state *tmp;
357 struct sm_state *new = alloc_state(name, owner, sym, state);
359 FOR_EACH_PTR(*slist, tmp) {
360 if (cmp_tracker(tmp, new) < 0)
361 continue;
362 else if (cmp_tracker(tmp, new) == 0) {
363 tmp->state = state;
364 tmp->pools = NULL;
365 tmp->possible = NULL;
366 add_ptr_list(&tmp->possible, tmp);
367 __free_sm_state(new);
368 return;
369 } else {
370 INSERT_CURRENT(new, tmp);
371 return;
373 } END_FOR_EACH_PTR(tmp);
374 add_ptr_list(slist, new);
377 void delete_state_slist(struct state_list **slist, const char *name, int owner,
378 struct symbol *sym)
380 struct sm_state *state;
382 FOR_EACH_PTR(*slist, state) {
383 if (state->owner == owner && state->sym == sym
384 && !strcmp(state->name, name)){
385 delete_ptr_list_entry((struct ptr_list **)slist,
386 state, 1);
387 __free_sm_state(state);
388 return;
390 } END_FOR_EACH_PTR(state);
394 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
396 add_ptr_list(list_stack, slist);
399 struct state_list *pop_slist(struct state_list_stack **list_stack)
401 struct state_list *slist;
403 slist = last_ptr_list((struct ptr_list *)*list_stack);
404 delete_ptr_list_last((struct ptr_list **)list_stack);
405 return slist;
408 void del_slist(struct state_list **slist)
410 __free_ptr_list((struct ptr_list **)slist);
413 void del_slist_stack(struct state_list_stack **slist_stack)
415 struct state_list *slist;
417 FOR_EACH_PTR(*slist_stack, slist) {
418 __free_ptr_list((struct ptr_list **)&slist);
419 } END_FOR_EACH_PTR(slist);
420 __free_ptr_list((struct ptr_list **)slist_stack);
424 * set_state_stack() sets the state for the top slist on the stack.
426 void set_state_stack(struct state_list_stack **stack, const char *name,
427 int owner, struct symbol *sym, struct smatch_state *state)
429 struct state_list *slist;
431 slist = pop_slist(stack);
432 set_state_slist(&slist, name, owner, sym, state);
433 push_slist(stack, slist);
437 * get_state_stack() gets the state for the top slist on the stack.
439 struct smatch_state *get_state_stack(struct state_list_stack *stack,
440 const char *name, int owner,
441 struct symbol *sym)
443 struct state_list *slist;
444 struct smatch_state *ret;
446 slist = pop_slist(&stack);
447 ret = get_state_slist(slist, name, owner, sym);
448 push_slist(&stack, slist);
449 return ret;
453 * merge_slist() is called whenever paths merge, such as after
454 * an if statement. It takes the two slists and creates one.
456 void merge_slist(struct state_list **to, struct state_list *slist)
458 struct sm_state *to_state, *state, *tmp;
459 struct state_list *results = NULL;
460 struct state_list *implied_to = NULL;
461 struct state_list *implied_from = NULL;
463 #ifdef CHECKORDER
464 check_order(*to);
465 check_order(slist);
466 #endif
468 /* merging a null and nonnull path gives you only the nonnull path */
469 if (!slist) {
470 return;
472 if (!*to) {
473 *to = clone_slist(slist);
474 return;
477 implied_to = clone_slist(*to);
478 implied_from = clone_slist(slist);
480 PREPARE_PTR_LIST(*to, to_state);
481 PREPARE_PTR_LIST(slist, state);
482 for (;;) {
483 if (!to_state && !state)
484 break;
485 if (cmp_tracker(to_state, state) < 0) {
486 tmp = merge_sm_states(to_state, NULL);
487 add_pool(tmp, implied_to);
488 add_ptr_list(&results, tmp);
489 NEXT_PTR_LIST(to_state);
490 } else if (cmp_tracker(to_state, state) == 0) {
491 tmp = merge_sm_states(to_state, state);
492 add_pool(tmp, implied_to);
493 add_pool(tmp, implied_from);
494 add_ptr_list(&results, tmp);
495 NEXT_PTR_LIST(to_state);
496 NEXT_PTR_LIST(state);
497 } else {
498 tmp = merge_sm_states(state, NULL);
499 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);
507 del_slist(to);
508 *to = results;
510 push_slist(&implied_pools, implied_from);
511 push_slist(&implied_pools, implied_to);
515 * is_currently_in_pool() is used because we remove states from pools.
516 * When set_state() is called then we set ->pools to NULL, but on
517 * other paths the state is still a member of those pools.
518 * Confusing huh?
519 * if (foo) {
520 * bar = 1;
521 * a = malloc();
523 * if (!a)
524 * return;
525 * if (bar)
526 * a->b = x;
528 static int is_currently_in_pool(struct sm_state *sm, struct state_list *pool,
529 struct state_list *cur_slist)
531 struct sm_state *cur_state;
532 struct state_list *tmp;
534 cur_state = get_sm_state_slist(cur_slist, sm->name, sm->owner, sm->sym);
535 if (!cur_state)
536 return 0;
538 FOR_EACH_PTR(cur_state->pools, tmp) {
539 if (tmp == pool)
540 return 1;
541 } END_FOR_EACH_PTR(tmp);
542 return 0;
545 struct state_list *clone_states_in_pool(struct state_list *pool,
546 struct state_list *cur_slist)
548 struct sm_state *state;
549 struct sm_state *tmp;
550 struct state_list *to_slist = NULL;
552 FOR_EACH_PTR(pool, state) {
553 if (is_currently_in_pool(state, pool, cur_slist)) {
554 tmp = clone_state(state);
555 add_ptr_list(&to_slist, tmp);
557 } END_FOR_EACH_PTR(state);
558 #ifdef CHECKORDER
559 check_order(to_slist);
560 #endif
561 return to_slist;
565 * filter() is used to find what states are the same across
566 * a series of slists.
567 * It takes a **slist and a *filter.
568 * It removes everything from **slist that isn't in *filter.
569 * The reason you would want to do this is if you want to
570 * know what other states are true if one state is true. (smatch_implied).
572 void filter(struct state_list **slist, struct state_list *filter,
573 struct state_list *cur_slist)
575 struct sm_state *s_one, *s_two;
576 struct state_list *results = NULL;
578 #ifdef CHECKORDER
579 check_order(*slist);
580 check_order(filter);
581 #endif
583 PREPARE_PTR_LIST(*slist, s_one);
584 PREPARE_PTR_LIST(filter, s_two);
585 for (;;) {
586 if (!s_one || !s_two)
587 break;
588 if (cmp_tracker(s_one, s_two) < 0) {
589 NEXT_PTR_LIST(s_one);
590 } else if (cmp_tracker(s_one, s_two) == 0) {
591 /* todo. pointer comparison works fine for most things
592 except smatch_extra. we may need a hook here. */
593 if (s_one->state == s_two->state &&
594 is_currently_in_pool(s_two, filter, cur_slist)) {
595 add_ptr_list(&results, s_one);
597 NEXT_PTR_LIST(s_one);
598 NEXT_PTR_LIST(s_two);
599 } else {
600 NEXT_PTR_LIST(s_two);
603 FINISH_PTR_LIST(s_two);
604 FINISH_PTR_LIST(s_one);
606 del_slist(slist);
607 *slist = results;
611 * and_slist_stack() is basically the same as popping the top two slists,
612 * overwriting the one with the other and pushing it back on the stack.
613 * The difference is that it checks to see that a mutually exclusive
614 * state isn't included in both stacks. If smatch sees something like
615 * "if (a && !a)" it prints a warning.
617 void and_slist_stack(struct state_list_stack **slist_stack)
619 struct sm_state *tmp;
620 struct smatch_state *tmp_state;
621 struct state_list *tmp_slist = pop_slist(slist_stack);
623 FOR_EACH_PTR(tmp_slist, tmp) {
624 tmp_state = get_state_stack(*slist_stack, tmp->name,
625 tmp->owner, tmp->sym);
626 if (tmp_state && tmp_state != tmp->state) {
627 struct smatch_state *s;
629 s = merge_states(tmp->name, tmp->owner, tmp->sym,
630 tmp->state, tmp_state);
631 smatch_msg("mutually exclusive 'and' conditions states "
632 "'%s': %s + %s => %s",
633 tmp->name, show_state(tmp_state),
634 show_state(tmp->state), show_state(s));
635 tmp->state = s;
638 set_state_stack(slist_stack, tmp->name, tmp->owner, tmp->sym,
639 tmp->state);
640 } END_FOR_EACH_PTR(tmp);
641 del_slist(&tmp_slist);
645 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
646 * It pops the two slists from the top of the stack and merges them
647 * together in a way that preserves the things they have in common
648 * but creates a merged state for most of the rest.
649 * You could have code that had: if (foo || foo) { foo->baz;
650 * It's this function which ensures smatch does the right thing.
652 void or_slist_stack(struct state_list_stack **slist_stack)
654 struct state_list *one;
655 struct state_list *two;
656 struct state_list *res = NULL;
657 struct sm_state *tmp;
658 struct sm_state *sm;
659 struct sm_state *new_sm;
661 one = pop_slist(slist_stack);
662 two = pop_slist(slist_stack);
664 FOR_EACH_PTR(one, tmp) {
665 sm = get_sm_state_slist(two, tmp->name, tmp->owner, tmp->sym);
666 new_sm = merge_sm_states(tmp, sm);
667 add_ptr_list(&res, new_sm);
668 } END_FOR_EACH_PTR(tmp);
670 FOR_EACH_PTR(two, tmp) {
671 sm = get_sm_state_slist(one, tmp->name, tmp->owner, tmp->sym);
672 new_sm = merge_sm_states(tmp, sm);
673 add_ptr_list(&res, new_sm);
674 } END_FOR_EACH_PTR(tmp);
676 push_slist(slist_stack, res);
678 del_slist(&one);
679 del_slist(&two);
683 * get_slist_from_named_stack() is only used for gotos.
685 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
686 const char *name)
688 struct named_slist *tmp;
690 FOR_EACH_PTR(stack, tmp) {
691 if (!strcmp(tmp->name, name))
692 return &tmp->slist;
693 } END_FOR_EACH_PTR(tmp);
694 return NULL;
697 void overwrite_slist(struct state_list *from, struct state_list **to)
699 struct sm_state *tmp;
701 FOR_EACH_PTR(from, tmp) {
702 overwrite_sm_state(to, tmp);
703 } END_FOR_EACH_PTR(tmp);