don't print "list" variable.
[smatch.git] / smatch_slist.c
blob4540c3c6dbc80cb1b5379937ecd8ee7286b81542
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_slist.h"
15 #undef CHECKORDER
17 ALLOCATOR(smatch_state, "smatch state");
18 ALLOCATOR(sm_state, "sm state");
19 ALLOCATOR(named_slist, "named slist");
20 __DO_ALLOCATOR(char, 0, 1, "state names", sname);
22 void __print_slist(struct state_list *slist)
24 struct sm_state *state;
25 struct sm_state *poss;
26 int i;
28 printf("dumping slist at %d\n", get_lineno());
29 FOR_EACH_PTR(slist, state) {
30 printf("%d '%s'=%s (", state->owner, state->name,
31 show_state(state->state));
32 i = 0;
33 FOR_EACH_PTR(state->possible, poss) {
34 if (i++)
35 printf(", ");
36 printf("%s", show_state(poss->state));
37 } END_FOR_EACH_PTR(poss);
38 printf(")\n");
39 } END_FOR_EACH_PTR(state);
40 printf("---\n");
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)
47 int ret;
49 if (a == b)
50 return 0;
51 if (!b)
52 return -1;
53 if (!a)
54 return 1;
56 if (a->owner > b->owner)
57 return -1;
58 if (a->owner < b->owner)
59 return 1;
61 ret = strcmp(a->name, b->name);
62 if (ret)
63 return ret;
65 if (!b->sym && a->sym)
66 return -1;
67 if (!a->sym && b->sym)
68 return 1;
69 if (a->sym > b->sym)
70 return -1;
71 if (a->sym < b->sym)
72 return 1;
74 return 0;
77 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b)
79 int ret;
81 ret = cmp_tracker(a, b);
82 if (ret)
83 return ret;
85 /* todo: add hook for smatch_extra.c */
86 if (a->state > b->state)
87 return -1;
88 if (a->state < b->state)
89 return 1;
90 return 0;
93 static struct sm_state *alloc_state_no_name(const char *name, int owner,
94 struct symbol *sym,
95 struct smatch_state *state)
97 struct sm_state *tmp;
99 tmp = alloc_state(NULL, owner, sym, state);
100 tmp->name = name;
101 return tmp;
104 void add_sm_state_slist(struct state_list **slist, struct sm_state *new)
106 struct sm_state *tmp;
108 FOR_EACH_PTR(*slist, tmp) {
109 if (cmp_sm_states(tmp, new) < 0)
110 continue;
111 else if (cmp_sm_states(tmp, new) == 0) {
112 return;
113 } else {
114 INSERT_CURRENT(new, tmp);
115 return;
117 } END_FOR_EACH_PTR(tmp);
118 add_ptr_list(slist, new);
121 static void add_possible(struct sm_state *sm, struct sm_state *new)
123 struct sm_state *tmp;
124 struct sm_state *tmp2;
126 if (!new) {
127 struct smatch_state *s;
129 s = merge_states(sm->name, sm->owner, sm->sym, sm->state, NULL);
130 tmp = alloc_state_no_name(sm->name, sm->owner, sm->sym, s);
131 add_sm_state_slist(&sm->possible, tmp);
132 return;
135 FOR_EACH_PTR(new->possible, tmp) {
136 tmp2 = alloc_state_no_name(tmp->name, tmp->owner, tmp->sym,
137 tmp->state);
138 add_sm_state_slist(&sm->possible, tmp2);
139 } END_FOR_EACH_PTR(tmp);
142 char *alloc_sname(const char *str)
144 char *tmp;
146 if (!str)
147 return NULL;
148 tmp = __alloc_sname(strlen(str) + 1);
149 strcpy(tmp, str);
150 return tmp;
153 struct sm_state *alloc_state(const char *name, int owner,
154 struct symbol *sym, struct smatch_state *state)
156 struct sm_state *sm_state = __alloc_sm_state(0);
158 sm_state->name = alloc_sname(name);
159 sm_state->owner = owner;
160 sm_state->sym = sym;
161 sm_state->state = state;
162 sm_state->line = get_lineno();
163 sm_state->merged = 0;
164 sm_state->my_pool = NULL;
165 sm_state->pre_left = NULL;
166 sm_state->pre_right = NULL;
167 sm_state->possible = NULL;
168 add_ptr_list(&sm_state->possible, sm_state);
169 return sm_state;
172 static void free_sm_state(struct sm_state *sm)
174 free_slist(&sm->possible);
176 * fixme. Free the actual state.
177 * Right now we leave it until the end of the function
178 * because we don't want to double free it.
179 * Use the freelist to not double free things
183 static void free_all_sm_states(struct allocation_blob *blob)
185 unsigned int size = sizeof(struct sm_state);
186 unsigned int offset = 0;
188 while (offset < blob->offset) {
189 free_sm_state((struct sm_state *)(blob->data + offset));
190 offset += size;
194 /* At the end of every function we free all the sm_states */
195 void free_every_single_sm_state(void)
197 struct allocator_struct *desc = &sm_state_allocator;
198 struct allocation_blob *blob = desc->blobs;
200 desc->blobs = NULL;
201 desc->allocations = 0;
202 desc->total_bytes = 0;
203 desc->useful_bytes = 0;
204 desc->freelist = NULL;
205 while (blob) {
206 struct allocation_blob *next = blob->next;
207 free_all_sm_states(blob);
208 blob_free(blob, desc->chunking);
209 blob = next;
211 clear_sname_alloc();
214 struct sm_state *clone_state(struct sm_state *s)
216 struct sm_state *ret;
218 ret = alloc_state_no_name(s->name, s->owner, s->sym, s->state);
219 ret->line = s->line;
220 ret->merged = s->merged;
221 /* clone_state() doesn't copy the my_pools. Each state needs to have
222 only one my_pool. */
223 ret->possible = clone_slist(s->possible);
224 ret->pre_left = s->pre_left;
225 ret->pre_right = s->pre_right;
226 return ret;
229 int is_merged(struct sm_state *sm)
231 return sm->merged;
234 int slist_has_state(struct state_list *slist, struct smatch_state *state)
236 struct sm_state *tmp;
238 FOR_EACH_PTR(slist, tmp) {
239 if (tmp->state == state)
240 return 1;
241 } END_FOR_EACH_PTR(tmp);
242 return 0;
245 static void check_order(struct state_list *slist)
247 #ifdef CHECKORDER
248 struct sm_state *state;
249 struct sm_state *last = NULL;
250 int printed = 0;
252 FOR_EACH_PTR(slist, state) {
253 if (last && cmp_tracker(state, last) <= 0) {
254 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
255 "%s vs %s\n", last->owner, state->owner,
256 last->sym, state->sym, last->name, state->name);
257 printed = 1;
259 last = state;
260 } END_FOR_EACH_PTR(state);
262 if (printed)
263 printf("======\n");
264 #endif
267 struct state_list *clone_slist(struct state_list *from_slist)
269 struct sm_state *state;
270 struct state_list *to_slist = NULL;
272 FOR_EACH_PTR(from_slist, state) {
273 add_ptr_list(&to_slist, state);
274 } END_FOR_EACH_PTR(state);
275 check_order(to_slist);
276 return to_slist;
279 struct state_list *clone_slist_and_states(struct state_list *from_slist)
281 struct sm_state *state;
282 struct sm_state *tmp;
283 struct state_list *to_slist = NULL;
285 FOR_EACH_PTR(from_slist, state) {
286 tmp = clone_state(state);
287 add_ptr_list(&to_slist, tmp);
288 } END_FOR_EACH_PTR(state);
289 check_order(to_slist);
290 return to_slist;
293 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
295 struct state_list *slist;
296 struct state_list_stack *to_stack = NULL;
298 FOR_EACH_PTR(from_stack, slist) {
299 push_slist(&to_stack, slist);
300 } END_FOR_EACH_PTR(slist);
301 return to_stack;
304 struct smatch_state *merge_states(const char *name, int owner,
305 struct symbol *sym,
306 struct smatch_state *state1,
307 struct smatch_state *state2)
309 struct smatch_state *ret;
311 if (state1 == state2)
312 ret = state1;
313 else if (__has_merge_function(owner))
314 ret = __client_merge_function(owner, name, sym, state1, state2);
315 else if (!state1 || !state2)
316 ret = &undefined;
317 else
318 ret = &merged;
319 return ret;
323 * add_pool() adds a slist to ->pools. If the slist has already been
324 * added earlier then it doesn't get added a second time.
326 void add_pool(struct state_list_stack **pools, struct state_list *new)
328 struct state_list *tmp;
330 FOR_EACH_PTR(*pools, tmp) {
331 if (tmp < new)
332 continue;
333 else if (tmp == new) {
334 return;
335 } else {
336 INSERT_CURRENT(new, tmp);
337 return;
339 } END_FOR_EACH_PTR(tmp);
340 add_ptr_list(pools, new);
343 void merge_pools(struct state_list_stack **to, struct state_list_stack *from)
345 struct state_list *tmp;
347 FOR_EACH_PTR(from, tmp) {
348 add_pool(to, tmp);
349 } END_FOR_EACH_PTR(tmp);
352 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
354 struct smatch_state *s;
355 struct sm_state *result;
357 if (one == two)
358 return one;
359 s = merge_states(one->name, one->owner, one->sym, one->state,
360 (two?two->state:NULL));
361 result = alloc_state_no_name(one->name, one->owner, one->sym, s);
362 if (two && one->line == two->line)
363 result->line = one->line;
364 result->merged = 1;
365 result->pre_left = one;
366 result->pre_right = two;
367 add_possible(result, one);
368 add_possible(result, two);
370 if (debug_states) {
371 struct sm_state *tmp;
372 int i = 0;
374 printf("%d merge name='%s' owner=%d: %s + %s => %s (",
375 get_lineno(), one->name, one->owner,
376 show_state(one->state), show_state(two?two->state:NULL),
377 show_state(s));
379 FOR_EACH_PTR(result->possible, tmp) {
380 if (i++) {
381 printf(", ");
383 printf("%s", show_state(tmp->state));
384 } END_FOR_EACH_PTR(tmp);
385 printf(")\n");
388 return result;
391 struct sm_state *get_sm_state_slist(struct state_list *slist, const char *name,
392 int owner, struct symbol *sym)
394 struct sm_state *state;
396 if (!name)
397 return NULL;
399 FOR_EACH_PTR(slist, state) {
400 if (state->owner == owner && state->sym == sym
401 && !strcmp(state->name, name))
402 return state;
403 } END_FOR_EACH_PTR(state);
404 return NULL;
407 struct smatch_state *get_state_slist(struct state_list *slist,
408 const char *name, int owner,
409 struct symbol *sym)
411 struct sm_state *state;
413 state = get_sm_state_slist(slist, name, owner, sym);
414 if (state)
415 return state->state;
416 return NULL;
419 void overwrite_sm_state(struct state_list **slist, struct sm_state *new)
421 struct sm_state *tmp;
423 FOR_EACH_PTR(*slist, tmp) {
424 if (cmp_tracker(tmp, new) < 0)
425 continue;
426 else if (cmp_tracker(tmp, new) == 0) {
427 REPLACE_CURRENT_PTR(tmp, new);
428 return;
429 } else {
430 INSERT_CURRENT(new, tmp);
431 return;
433 } END_FOR_EACH_PTR(tmp);
434 add_ptr_list(slist, new);
437 void overwrite_sm_state_stack(struct state_list_stack **stack,
438 struct sm_state *state)
440 struct state_list *slist;
442 slist = pop_slist(stack);
443 overwrite_sm_state(&slist, state);
444 push_slist(stack, slist);
447 void set_state_slist(struct state_list **slist, const char *name, int owner,
448 struct symbol *sym, struct smatch_state *state)
450 struct sm_state *tmp;
451 struct sm_state *new = alloc_state(name, owner, sym, state);
453 FOR_EACH_PTR(*slist, tmp) {
454 if (cmp_tracker(tmp, new) < 0)
455 continue;
456 else if (cmp_tracker(tmp, new) == 0) {
457 REPLACE_CURRENT_PTR(tmp, new);
458 return;
459 } else {
460 INSERT_CURRENT(new, tmp);
461 return;
463 } END_FOR_EACH_PTR(tmp);
464 add_ptr_list(slist, new);
467 void delete_state_slist(struct state_list **slist, const char *name, int owner,
468 struct symbol *sym)
470 struct sm_state *state;
472 FOR_EACH_PTR(*slist, state) {
473 if (state->owner == owner && state->sym == sym
474 && !strcmp(state->name, name)){
475 DELETE_CURRENT_PTR(state);
476 return;
478 } END_FOR_EACH_PTR(state);
482 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
484 add_ptr_list(list_stack, slist);
487 struct state_list *pop_slist(struct state_list_stack **list_stack)
489 struct state_list *slist;
491 slist = last_ptr_list((struct ptr_list *)*list_stack);
492 delete_ptr_list_last((struct ptr_list **)list_stack);
493 return slist;
496 void free_slist(struct state_list **slist)
498 __free_ptr_list((struct ptr_list **)slist);
501 void free_stack(struct state_list_stack **stack)
503 __free_ptr_list((struct ptr_list **)stack);
506 void free_stack_and_slists(struct state_list_stack **slist_stack)
508 struct state_list *slist;
510 FOR_EACH_PTR(*slist_stack, slist) {
511 free_slist(&slist);
512 } END_FOR_EACH_PTR(slist);
513 free_stack(slist_stack);
517 * set_state_stack() sets the state for the top slist on the stack.
519 void set_state_stack(struct state_list_stack **stack, const char *name,
520 int owner, struct symbol *sym, struct smatch_state *state)
522 struct state_list *slist;
524 slist = pop_slist(stack);
525 set_state_slist(&slist, name, owner, sym, state);
526 push_slist(stack, slist);
530 * get_sm_state_stack() gets the state for the top slist on the stack.
532 struct sm_state *get_sm_state_stack(struct state_list_stack *stack,
533 const char *name, int owner,
534 struct symbol *sym)
536 struct state_list *slist;
537 struct sm_state *ret;
539 slist = pop_slist(&stack);
540 ret = get_sm_state_slist(slist, name, owner, sym);
541 push_slist(&stack, slist);
542 return ret;
546 struct smatch_state *get_state_stack(struct state_list_stack *stack,
547 const char *name, int owner,
548 struct symbol *sym)
550 struct sm_state *state;
552 state = get_sm_state_stack(stack, name, owner, sym);
553 if (state)
554 return state->state;
555 return NULL;
558 static void match_states(struct state_list **one, struct state_list **two)
560 struct sm_state *one_state;
561 struct sm_state *two_state;
562 struct sm_state *tmp;
563 struct smatch_state *tmp_state;
564 struct state_list *add_to_one = NULL;
565 struct state_list *add_to_two = NULL;
567 PREPARE_PTR_LIST(*one, one_state);
568 PREPARE_PTR_LIST(*two, two_state);
569 for (;;) {
570 if (!one_state && !two_state)
571 break;
572 if (cmp_tracker(one_state, two_state) < 0) {
573 tmp_state = __client_unmatched_state_function(one_state);
574 tmp = alloc_state_no_name(one_state->name,
575 one_state->owner,
576 one_state->sym, tmp_state);
577 add_ptr_list(&add_to_two, tmp);
578 NEXT_PTR_LIST(one_state);
579 } else if (cmp_tracker(one_state, two_state) == 0) {
580 NEXT_PTR_LIST(one_state);
581 NEXT_PTR_LIST(two_state);
582 } else {
583 tmp_state = __client_unmatched_state_function(two_state);
584 tmp = alloc_state_no_name(two_state->name,
585 two_state->owner,
586 two_state->sym, tmp_state);
587 add_ptr_list(&add_to_one, tmp);
588 NEXT_PTR_LIST(two_state);
591 FINISH_PTR_LIST(two_state);
592 FINISH_PTR_LIST(one_state);
594 overwrite_slist(add_to_one, one);
595 overwrite_slist(add_to_two, two);
598 static void clone_modified(struct state_list **one, struct state_list **two)
600 struct sm_state *one_state;
601 struct sm_state *two_state;
602 struct sm_state *clone;
603 struct state_list *add_to_one = NULL;
604 struct state_list *add_to_two = NULL;
606 PREPARE_PTR_LIST(*one, one_state);
607 PREPARE_PTR_LIST(*two, two_state);
608 for (;;) {
609 if (!one_state && !two_state)
610 break;
611 if (cmp_tracker(one_state, two_state) < 0) {
612 NEXT_PTR_LIST(one_state);
613 } else if (cmp_tracker(one_state, two_state) == 0) {
614 if (one_state != two_state) {
615 clone = clone_state(one_state);
616 add_ptr_list(&add_to_one, clone);
617 clone = clone_state(two_state);
618 add_ptr_list(&add_to_two, clone);
620 NEXT_PTR_LIST(one_state);
621 NEXT_PTR_LIST(two_state);
622 } else {
623 NEXT_PTR_LIST(two_state);
626 FINISH_PTR_LIST(two_state);
627 FINISH_PTR_LIST(one_state);
629 overwrite_slist(add_to_one, one);
630 overwrite_slist(add_to_two, two);
633 static void clone_pool_havers(struct state_list *slist)
635 struct sm_state *state;
636 struct sm_state *new;
638 FOR_EACH_PTR(slist, state) {
639 if (state->my_pool) {
640 new = clone_state(state);
641 REPLACE_CURRENT_PTR(state, new);
643 } END_FOR_EACH_PTR(state);
647 * merge_slist() is called whenever paths merge, such as after
648 * an if statement. It takes the two slists and creates one.
650 static void __merge_slist(struct state_list **to, struct state_list *slist, int clone)
652 struct sm_state *to_state, *state, *tmp;
653 struct state_list *results = NULL;
654 struct state_list *implied_to = NULL;
655 struct state_list *implied_from = NULL;
657 check_order(*to);
658 check_order(slist);
660 /* merging a null and nonnull path gives you only the nonnull path */
661 if (!slist) {
662 return;
664 if (!*to) {
665 *to = clone_slist(slist);
666 return;
669 implied_to = clone_slist(*to);
670 implied_from = clone_slist(slist);
672 match_states(&implied_to, &implied_from);
673 if (clone)
674 clone_modified(&implied_to, &implied_from);
675 clone_pool_havers(implied_to);
676 clone_pool_havers(implied_from);
678 PREPARE_PTR_LIST(implied_to, to_state);
679 PREPARE_PTR_LIST(implied_from, state);
680 for (;;) {
681 if (!to_state && !state)
682 break;
683 if (cmp_tracker(to_state, state) < 0) {
684 smatch_msg("error: Internal smatch error.");
685 NEXT_PTR_LIST(to_state);
686 } else if (cmp_tracker(to_state, state) == 0) {
687 if (to_state != state) {
688 if (to_state->my_pool)
689 smatch_msg("to_state '%s' has pools", to_state->name);
690 if (state->my_pool)
691 smatch_msg("state '%s' has pools", state->name);
692 to_state->my_pool = implied_to;
693 state->my_pool = implied_from;
696 tmp = merge_sm_states(to_state, state);
697 add_ptr_list(&results, tmp);
698 NEXT_PTR_LIST(to_state);
699 NEXT_PTR_LIST(state);
700 } else {
701 smatch_msg("error: Internal smatch error.");
702 NEXT_PTR_LIST(state);
705 FINISH_PTR_LIST(state);
706 FINISH_PTR_LIST(to_state);
708 free_slist(to);
709 *to = results;
712 void merge_slist(struct state_list **to, struct state_list *slist)
714 __merge_slist(to, slist, 0);
717 void merge_slist_clone(struct state_list **to, struct state_list *slist)
719 __merge_slist(to, slist, 1);
723 * and_slist_stack() is basically the same as popping the top two slists,
724 * overwriting the one with the other and pushing it back on the stack.
725 * The difference is that it checks to see that a mutually exclusive
726 * state isn't included in both stacks. If smatch sees something like
727 * "if (a && !a)" it prints a warning.
729 void and_slist_stack(struct state_list_stack **slist_stack)
731 struct sm_state *tmp;
732 struct state_list *right_slist = pop_slist(slist_stack);
734 FOR_EACH_PTR(right_slist, tmp) {
735 overwrite_sm_state_stack(slist_stack, tmp);
736 } END_FOR_EACH_PTR(tmp);
737 free_slist(&right_slist);
741 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
742 * It pops the two slists from the top of the stack and merges them
743 * together in a way that preserves the things they have in common
744 * but creates a merged state for most of the rest.
745 * You could have code that had: if (foo || foo) { foo->baz;
746 * It's this function which ensures smatch does the right thing.
748 void or_slist_stack(struct state_list_stack **pre_conds,
749 struct state_list *cur_slist,
750 struct state_list_stack **slist_stack)
752 struct state_list *new;
753 struct state_list *old;
754 struct state_list *res = NULL;
755 struct state_list *tmp_slist;
757 new = pop_slist(slist_stack);
758 old = pop_slist(slist_stack);
760 tmp_slist = pop_slist(pre_conds);
761 res = clone_slist(tmp_slist);
762 push_slist(pre_conds, tmp_slist);
763 overwrite_slist(old, &res);
765 tmp_slist = clone_slist(cur_slist);
766 overwrite_slist(new, &tmp_slist);
768 merge_slist(&res, tmp_slist);
770 push_slist(slist_stack, res);
771 free_slist(&tmp_slist);
772 free_slist(&new);
773 free_slist(&old);
777 * get_slist_from_named_stack() is only used for gotos.
779 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
780 const char *name)
782 struct named_slist *tmp;
784 FOR_EACH_PTR(stack, tmp) {
785 if (!strcmp(tmp->name, name))
786 return &tmp->slist;
787 } END_FOR_EACH_PTR(tmp);
788 return NULL;
791 void overwrite_slist(struct state_list *from, struct state_list **to)
793 struct sm_state *tmp;
795 FOR_EACH_PTR(from, tmp) {
796 overwrite_sm_state(to, tmp);
797 } END_FOR_EACH_PTR(tmp);