flow: don't print duplicate "unreachable code" warnings
[smatch.git] / smatch_slist.c
blob248edcc2d6f67ab6dc82bc9522f089c567002364
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, 1, 4, "state names", sname);
22 static int sm_state_counter;
24 char *show_sm(struct sm_state *sm)
26 static char buf[256];
27 struct sm_state *tmp;
28 int pos;
29 int i;
31 pos = snprintf(buf, sizeof(buf), "[%s] '%s' = %s (",
32 check_name(sm->owner), sm->name, show_state(sm->state));
33 if (pos > sizeof(buf))
34 goto truncate;
36 i = 0;
37 FOR_EACH_PTR(sm->possible, tmp) {
38 if (i++)
39 pos += snprintf(buf + pos, sizeof(buf) - pos, ", ");
40 if (pos > sizeof(buf))
41 goto truncate;
42 pos += snprintf(buf + pos, sizeof(buf) - pos, "%s",
43 show_state(tmp->state));
44 if (pos > sizeof(buf))
45 goto truncate;
46 } END_FOR_EACH_PTR(tmp);
47 snprintf(buf + pos, sizeof(buf) - pos, ")");
49 return buf;
51 truncate:
52 for (i = 0; i < 3; i++)
53 buf[sizeof(buf) - 2 - i] = '.';
54 return buf;
57 void __print_slist(struct state_list *slist)
59 struct sm_state *sm;
61 printf("dumping slist at %d\n", get_lineno());
62 FOR_EACH_PTR(slist, sm) {
63 printf("%s\n", show_sm(sm));
64 } END_FOR_EACH_PTR(sm);
65 printf("---\n");
68 /* NULL states go at the end to simplify merge_slist */
69 int cmp_tracker(const struct sm_state *a, const struct sm_state *b)
71 int ret;
73 if (a == b)
74 return 0;
75 if (!b)
76 return -1;
77 if (!a)
78 return 1;
80 if (a->owner > b->owner)
81 return -1;
82 if (a->owner < b->owner)
83 return 1;
85 ret = strcmp(a->name, b->name);
86 if (ret)
87 return ret;
89 if (!b->sym && a->sym)
90 return -1;
91 if (!a->sym && b->sym)
92 return 1;
93 if (a->sym > b->sym)
94 return -1;
95 if (a->sym < b->sym)
96 return 1;
98 return 0;
101 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b)
103 int ret;
105 ret = cmp_tracker(a, b);
106 if (ret)
107 return ret;
109 /* todo: add hook for smatch_extra.c */
110 if (a->state > b->state)
111 return -1;
112 if (a->state < b->state)
113 return 1;
114 return 0;
117 static struct sm_state *alloc_sm_state(int owner, const char *name,
118 struct symbol *sym, struct smatch_state *state)
120 struct sm_state *sm_state = __alloc_sm_state(0);
122 sm_state_counter++;
124 sm_state->name = alloc_sname(name);
125 sm_state->owner = owner;
126 sm_state->sym = sym;
127 sm_state->state = state;
128 sm_state->line = get_lineno();
129 sm_state->merged = 0;
130 sm_state->implied = 0;
131 sm_state->pool = NULL;
132 sm_state->left = NULL;
133 sm_state->right = NULL;
134 sm_state->nr_children = 1;
135 sm_state->possible = NULL;
136 add_ptr_list(&sm_state->possible, sm_state);
137 return sm_state;
140 static struct sm_state *alloc_state_no_name(int owner, const char *name,
141 struct symbol *sym,
142 struct smatch_state *state)
144 struct sm_state *tmp;
146 tmp = alloc_sm_state(owner, NULL, sym, state);
147 tmp->name = name;
148 return tmp;
151 void add_sm_state_slist(struct state_list **slist, struct sm_state *new)
153 struct sm_state *tmp;
155 FOR_EACH_PTR(*slist, tmp) {
156 if (cmp_sm_states(tmp, new) < 0)
157 continue;
158 else if (cmp_sm_states(tmp, new) == 0) {
159 return;
160 } else {
161 INSERT_CURRENT(new, tmp);
162 return;
164 } END_FOR_EACH_PTR(tmp);
165 add_ptr_list(slist, new);
168 static void copy_possibles(struct sm_state *to, struct sm_state *from)
170 struct sm_state *tmp;
172 FOR_EACH_PTR(from->possible, tmp) {
173 add_sm_state_slist(&to->possible, tmp);
174 } END_FOR_EACH_PTR(tmp);
177 char *alloc_sname(const char *str)
179 char *tmp;
181 if (!str)
182 return NULL;
183 tmp = __alloc_sname(strlen(str) + 1);
184 strcpy(tmp, str);
185 return tmp;
188 int out_of_memory()
191 * I decided to use 50M here based on trial and error.
192 * It works out OK for the kernel and so it should work
193 * for most other projects as well.
195 if (sm_state_counter * sizeof(struct sm_state) >= 50000000)
196 return 1;
197 return 0;
200 static void free_sm_state(struct sm_state *sm)
202 free_slist(&sm->possible);
204 * fixme. Free the actual state.
205 * Right now we leave it until the end of the function
206 * because we don't want to double free it.
207 * Use the freelist to not double free things
211 static void free_all_sm_states(struct allocation_blob *blob)
213 unsigned int size = sizeof(struct sm_state);
214 unsigned int offset = 0;
216 while (offset < blob->offset) {
217 free_sm_state((struct sm_state *)(blob->data + offset));
218 offset += size;
222 /* At the end of every function we free all the sm_states */
223 void free_every_single_sm_state(void)
225 struct allocator_struct *desc = &sm_state_allocator;
226 struct allocation_blob *blob = desc->blobs;
228 desc->blobs = NULL;
229 desc->allocations = 0;
230 desc->total_bytes = 0;
231 desc->useful_bytes = 0;
232 desc->freelist = NULL;
233 while (blob) {
234 struct allocation_blob *next = blob->next;
235 free_all_sm_states(blob);
236 blob_free(blob, desc->chunking);
237 blob = next;
239 clear_sname_alloc();
241 sm_state_counter = 0;
244 struct sm_state *clone_sm(struct sm_state *s)
246 struct sm_state *ret;
248 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
249 ret->merged = s->merged;
250 ret->implied = s->implied;
251 ret->line = s->line;
252 /* clone_sm() doesn't copy the pools. Each state needs to have
253 only one pool. */
254 ret->possible = clone_slist(s->possible);
255 ret->left = s->left;
256 ret->right = s->right;
257 ret->nr_children = s->nr_children;
258 return ret;
261 int is_merged(struct sm_state *sm)
263 return sm->merged;
266 int is_implied(struct sm_state *sm)
268 return sm->implied;
271 int slist_has_state(struct state_list *slist, struct smatch_state *state)
273 struct sm_state *tmp;
275 FOR_EACH_PTR(slist, tmp) {
276 if (tmp->state == state)
277 return 1;
278 } END_FOR_EACH_PTR(tmp);
279 return 0;
282 static void check_order(struct state_list *slist)
284 #ifdef CHECKORDER
285 struct sm_state *sm;
286 struct sm_state *last = NULL;
287 int printed = 0;
289 FOR_EACH_PTR(slist, sm) {
290 if (last && cmp_tracker(sm, last) <= 0) {
291 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
292 "%s vs %s\n", last->owner, sm->owner,
293 last->sym, sm->sym, last->name, sm->name);
294 printed = 1;
296 last = sm;
297 } END_FOR_EACH_PTR(sm);
299 if (printed)
300 printf("======\n");
301 #endif
304 struct state_list *clone_slist(struct state_list *from_slist)
306 struct sm_state *sm;
307 struct state_list *to_slist = NULL;
309 FOR_EACH_PTR(from_slist, sm) {
310 add_ptr_list(&to_slist, sm);
311 } END_FOR_EACH_PTR(sm);
312 return to_slist;
315 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
317 struct state_list *slist;
318 struct state_list_stack *to_stack = NULL;
320 FOR_EACH_PTR(from_stack, slist) {
321 push_slist(&to_stack, slist);
322 } END_FOR_EACH_PTR(slist);
323 return to_stack;
326 struct smatch_state *merge_states(int owner, const char *name,
327 struct symbol *sym,
328 struct smatch_state *state1,
329 struct smatch_state *state2)
331 struct smatch_state *ret;
333 if (state1 == state2)
334 ret = state1;
335 else if (__has_merge_function(owner))
336 ret = __client_merge_function(owner, state1, state2);
337 else if (!state1 || !state2)
338 ret = &undefined;
339 else
340 ret = &merged;
341 return ret;
344 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
346 struct smatch_state *s;
347 struct sm_state *result;
349 if (one == two)
350 return one;
351 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
352 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
353 result->merged = 1;
354 result->left = one;
355 result->right = two;
356 result->nr_children = one->nr_children + two->nr_children;
357 copy_possibles(result, one);
358 copy_possibles(result, two);
360 if (option_debug) {
361 struct sm_state *tmp;
362 int i = 0;
364 printf("%d merge [%s] '%s' %s(L %d) + %s(L %d) => %s (",
365 get_lineno(), check_name(one->owner), one->name,
366 show_state(one->state), one->line,
367 show_state(two->state), two->line,
368 show_state(s));
370 FOR_EACH_PTR(result->possible, tmp) {
371 if (i++)
372 printf(", ");
373 printf("%s", show_state(tmp->state));
374 } END_FOR_EACH_PTR(tmp);
375 printf(")\n");
378 return result;
381 struct sm_state *get_sm_state_slist(struct state_list *slist, int owner, const char *name,
382 struct symbol *sym)
384 struct sm_state *sm;
386 if (!name)
387 return NULL;
389 FOR_EACH_PTR(slist, sm) {
390 if (sm->owner == owner && sm->sym == sym && !strcmp(sm->name, name))
391 return sm;
392 } END_FOR_EACH_PTR(sm);
393 return NULL;
396 struct smatch_state *get_state_slist(struct state_list *slist,
397 int owner, const char *name,
398 struct symbol *sym)
400 struct sm_state *sm;
402 sm = get_sm_state_slist(slist, owner, name, sym);
403 if (sm)
404 return sm->state;
405 return NULL;
408 void overwrite_sm_state(struct state_list **slist, struct sm_state *new)
410 struct sm_state *tmp;
412 FOR_EACH_PTR(*slist, tmp) {
413 if (cmp_tracker(tmp, new) < 0)
414 continue;
415 else if (cmp_tracker(tmp, new) == 0) {
416 REPLACE_CURRENT_PTR(tmp, new);
417 return;
418 } else {
419 INSERT_CURRENT(new, tmp);
420 return;
422 } END_FOR_EACH_PTR(tmp);
423 add_ptr_list(slist, new);
426 void overwrite_sm_state_stack(struct state_list_stack **stack,
427 struct sm_state *sm)
429 struct state_list *slist;
431 slist = pop_slist(stack);
432 overwrite_sm_state(&slist, sm);
433 push_slist(stack, slist);
436 struct sm_state *set_state_slist(struct state_list **slist, int owner, const char *name,
437 struct symbol *sym, struct smatch_state *state)
439 struct sm_state *tmp;
440 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
442 FOR_EACH_PTR(*slist, tmp) {
443 if (cmp_tracker(tmp, new) < 0)
444 continue;
445 else if (cmp_tracker(tmp, new) == 0) {
446 REPLACE_CURRENT_PTR(tmp, new);
447 return new;
448 } else {
449 INSERT_CURRENT(new, tmp);
450 return new;
452 } END_FOR_EACH_PTR(tmp);
453 add_ptr_list(slist, new);
454 return new;
457 void delete_state_slist(struct state_list **slist, int owner, const char *name,
458 struct symbol *sym)
460 struct sm_state *sm;
462 FOR_EACH_PTR(*slist, sm) {
463 if (sm->owner == owner && sm->sym == sym && !strcmp(sm->name, name)) {
464 DELETE_CURRENT_PTR(sm);
465 return;
467 } END_FOR_EACH_PTR(sm);
470 void delete_state_stack(struct state_list_stack **stack, int owner, const char *name,
471 struct symbol *sym)
473 struct state_list *slist;
475 slist = pop_slist(stack);
476 delete_state_slist(&slist, owner, name, sym);
477 push_slist(stack, slist);
480 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
482 add_ptr_list(list_stack, slist);
485 struct state_list *pop_slist(struct state_list_stack **list_stack)
487 struct state_list *slist;
489 slist = last_ptr_list((struct ptr_list *)*list_stack);
490 delete_ptr_list_last((struct ptr_list **)list_stack);
491 return slist;
494 void free_slist(struct state_list **slist)
496 __free_ptr_list((struct ptr_list **)slist);
499 void free_stack(struct state_list_stack **stack)
501 __free_ptr_list((struct ptr_list **)stack);
504 void free_stack_and_slists(struct state_list_stack **slist_stack)
506 struct state_list *slist;
508 FOR_EACH_PTR(*slist_stack, slist) {
509 free_slist(&slist);
510 } END_FOR_EACH_PTR(slist);
511 free_stack(slist_stack);
515 * set_state_stack() sets the state for the top slist on the stack.
517 struct sm_state *set_state_stack(struct state_list_stack **stack, int owner, const char *name,
518 struct symbol *sym, struct smatch_state *state)
520 struct state_list *slist;
521 struct sm_state *sm;
523 slist = pop_slist(stack);
524 sm = set_state_slist(&slist, owner, name, sym, state);
525 push_slist(stack, slist);
527 return sm;
531 * get_sm_state_stack() gets the state for the top slist on the stack.
533 struct sm_state *get_sm_state_stack(struct state_list_stack *stack,
534 int owner, const char *name,
535 struct symbol *sym)
537 struct state_list *slist;
538 struct sm_state *ret;
540 slist = pop_slist(&stack);
541 ret = get_sm_state_slist(slist, owner, name, sym);
542 push_slist(&stack, slist);
543 return ret;
546 struct smatch_state *get_state_stack(struct state_list_stack *stack,
547 int owner, const char *name,
548 struct symbol *sym)
550 struct sm_state *sm;
552 sm = get_sm_state_stack(stack, owner, name, sym);
553 if (sm)
554 return sm->state;
555 return NULL;
558 static void match_states(struct state_list **one, struct state_list **two)
560 struct sm_state *one_sm;
561 struct sm_state *two_sm;
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_sm);
568 PREPARE_PTR_LIST(*two, two_sm);
569 for (;;) {
570 if (!one_sm && !two_sm)
571 break;
572 if (cmp_tracker(one_sm, two_sm) < 0) {
573 tmp_state = __client_unmatched_state_function(one_sm);
574 tmp = alloc_state_no_name(one_sm->owner, one_sm->name,
575 one_sm->sym, tmp_state);
576 add_ptr_list(&add_to_two, tmp);
577 NEXT_PTR_LIST(one_sm);
578 } else if (cmp_tracker(one_sm, two_sm) == 0) {
579 NEXT_PTR_LIST(one_sm);
580 NEXT_PTR_LIST(two_sm);
581 } else {
582 tmp_state = __client_unmatched_state_function(two_sm);
583 tmp = alloc_state_no_name(two_sm->owner, two_sm->name,
584 two_sm->sym, tmp_state);
585 add_ptr_list(&add_to_one, tmp);
586 NEXT_PTR_LIST(two_sm);
589 FINISH_PTR_LIST(two_sm);
590 FINISH_PTR_LIST(one_sm);
592 overwrite_slist(add_to_one, one);
593 overwrite_slist(add_to_two, two);
596 static void clone_pool_havers(struct state_list *slist)
598 struct sm_state *sm;
599 struct sm_state *new;
601 FOR_EACH_PTR(slist, sm) {
602 if (sm->pool) {
603 new = clone_sm(sm);
604 REPLACE_CURRENT_PTR(sm, new);
606 } END_FOR_EACH_PTR(sm);
609 int __slist_id;
611 * Sets the first state to the slist_id.
613 static void set_slist_id(struct state_list *slist)
615 struct smatch_state *state;
616 struct sm_state *tmp, *new;
618 state = alloc_state_num(++__slist_id);
619 new = alloc_sm_state(-1, "unnull_path", NULL, state);
621 FOR_EACH_PTR(slist, tmp) {
622 if (tmp->owner != (unsigned short)-1)
623 return;
624 REPLACE_CURRENT_PTR(tmp, new);
625 return;
626 } END_FOR_EACH_PTR(tmp);
629 int get_slist_id(struct state_list *slist)
631 struct sm_state *tmp;
633 FOR_EACH_PTR(slist, tmp) {
634 if (tmp->owner != (unsigned short)-1)
635 return 0;
636 return PTR_INT(tmp->state->data);
637 } END_FOR_EACH_PTR(tmp);
638 return 0;
642 * merge_slist() is called whenever paths merge, such as after
643 * an if statement. It takes the two slists and creates one.
645 void merge_slist(struct state_list **to, struct state_list *slist)
647 struct sm_state *one_sm, *two_sm, *tmp;
648 struct state_list *results = NULL;
649 struct state_list *implied_one = NULL;
650 struct state_list *implied_two = NULL;
652 if (out_of_memory())
653 return;
655 check_order(*to);
656 check_order(slist);
658 /* merging a null and nonnull path gives you only the nonnull path */
659 if (!slist)
660 return;
662 if (!*to) {
663 *to = clone_slist(slist);
664 return;
667 implied_one = clone_slist(*to);
668 implied_two = clone_slist(slist);
670 match_states(&implied_one, &implied_two);
672 clone_pool_havers(implied_one);
673 clone_pool_havers(implied_two);
675 set_slist_id(implied_one);
676 set_slist_id(implied_two);
678 PREPARE_PTR_LIST(implied_one, one_sm);
679 PREPARE_PTR_LIST(implied_two, two_sm);
680 for (;;) {
681 if (!one_sm && !two_sm)
682 break;
683 if (cmp_tracker(one_sm, two_sm) < 0) {
684 sm_msg("error: Internal smatch error.");
685 NEXT_PTR_LIST(one_sm);
686 } else if (cmp_tracker(one_sm, two_sm) == 0) {
687 if (one_sm != two_sm) {
688 one_sm->pool = implied_one;
689 two_sm->pool = implied_two;
692 tmp = merge_sm_states(one_sm, two_sm);
693 add_ptr_list(&results, tmp);
694 NEXT_PTR_LIST(one_sm);
695 NEXT_PTR_LIST(two_sm);
696 } else {
697 sm_msg("error: Internal smatch error.");
698 NEXT_PTR_LIST(two_sm);
701 FINISH_PTR_LIST(two_sm);
702 FINISH_PTR_LIST(one_sm);
704 free_slist(to);
705 *to = results;
709 * filter_slist() removes any sm states "slist" holds in common with "filter"
711 void filter_slist(struct state_list **slist, struct state_list *filter)
713 struct sm_state *one_sm, *two_sm;
714 struct state_list *results = NULL;
716 PREPARE_PTR_LIST(*slist, one_sm);
717 PREPARE_PTR_LIST(filter, two_sm);
718 for (;;) {
719 if (!one_sm && !two_sm)
720 break;
721 if (cmp_tracker(one_sm, two_sm) < 0) {
722 add_ptr_list(&results, one_sm);
723 NEXT_PTR_LIST(one_sm);
724 } else if (cmp_tracker(one_sm, two_sm) == 0) {
725 if (one_sm != two_sm)
726 add_ptr_list(&results, one_sm);
727 NEXT_PTR_LIST(one_sm);
728 NEXT_PTR_LIST(two_sm);
729 } else {
730 NEXT_PTR_LIST(two_sm);
733 FINISH_PTR_LIST(two_sm);
734 FINISH_PTR_LIST(one_sm);
736 free_slist(slist);
737 *slist = results;
741 * and_slist_stack() pops the top two slists, overwriting the one with
742 * the other and pushing it back on the stack.
744 void and_slist_stack(struct state_list_stack **slist_stack)
746 struct sm_state *tmp;
747 struct state_list *right_slist = pop_slist(slist_stack);
749 FOR_EACH_PTR(right_slist, tmp) {
750 overwrite_sm_state_stack(slist_stack, tmp);
751 } END_FOR_EACH_PTR(tmp);
752 free_slist(&right_slist);
756 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
757 * It pops the two slists from the top of the stack and merges them
758 * together in a way that preserves the things they have in common
759 * but creates a merged state for most of the rest.
760 * You could have code that had: if (foo || foo) { foo->baz;
761 * It's this function which ensures smatch does the right thing.
763 void or_slist_stack(struct state_list_stack **pre_conds,
764 struct state_list *cur_slist,
765 struct state_list_stack **slist_stack)
767 struct state_list *new;
768 struct state_list *old;
769 struct state_list *pre_slist;
770 struct state_list *res;
771 struct state_list *tmp_slist;
773 new = pop_slist(slist_stack);
774 old = pop_slist(slist_stack);
776 pre_slist = pop_slist(pre_conds);
777 push_slist(pre_conds, clone_slist(pre_slist));
779 res = clone_slist(pre_slist);
780 overwrite_slist(old, &res);
782 tmp_slist = clone_slist(cur_slist);
783 overwrite_slist(new, &tmp_slist);
785 merge_slist(&res, tmp_slist);
786 filter_slist(&res, pre_slist);
788 push_slist(slist_stack, res);
789 free_slist(&tmp_slist);
790 free_slist(&pre_slist);
791 free_slist(&new);
792 free_slist(&old);
796 * get_slist_from_named_stack() is only used for gotos.
798 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
799 const char *name)
801 struct named_slist *tmp;
803 FOR_EACH_PTR(stack, tmp) {
804 if (!strcmp(tmp->name, name))
805 return &tmp->slist;
806 } END_FOR_EACH_PTR(tmp);
807 return NULL;
810 void overwrite_slist(struct state_list *from, struct state_list **to)
812 struct sm_state *tmp;
814 FOR_EACH_PTR(from, tmp) {
815 overwrite_sm_state(to, tmp);
816 } END_FOR_EACH_PTR(tmp);