extra: delete the get_range_list() function
[smatch.git] / smatch_slist.c
blob269832f48fc10c1c77aa353115e692ccd2935bef
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 static int sm_state_counter;
24 void __print_slist(struct state_list *slist)
26 struct sm_state *state;
27 struct sm_state *poss;
28 int i;
30 printf("dumping slist at %d\n", get_lineno());
31 FOR_EACH_PTR(slist, state) {
32 printf("[%s] '%s'=%s (", check_name(state->owner), state->name,
33 show_state(state->state));
34 i = 0;
35 FOR_EACH_PTR(state->possible, poss) {
36 if (i++)
37 printf(", ");
38 printf("%s", show_state(poss->state));
39 } END_FOR_EACH_PTR(poss);
40 printf(")\n");
41 } END_FOR_EACH_PTR(state);
42 printf("---\n");
45 /* NULL states go at the end to simplify merge_slist */
46 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 static struct sm_state *alloc_sm_state(int owner, const char *name,
95 struct symbol *sym, struct smatch_state *state)
97 struct sm_state *sm_state = __alloc_sm_state(0);
99 sm_state_counter++;
101 sm_state->name = alloc_sname(name);
102 sm_state->owner = owner;
103 sm_state->sym = sym;
104 sm_state->state = state;
105 sm_state->line = get_lineno();
106 sm_state->merged = 0;
107 sm_state->implied = 0;
108 sm_state->my_pool = NULL;
109 sm_state->left = NULL;
110 sm_state->right = NULL;
111 sm_state->nr_children = 1;
112 sm_state->possible = NULL;
113 add_ptr_list(&sm_state->possible, sm_state);
114 return sm_state;
117 static struct sm_state *alloc_state_no_name(int owner, const char *name,
118 struct symbol *sym,
119 struct smatch_state *state)
121 struct sm_state *tmp;
123 tmp = alloc_sm_state(owner, NULL, sym, state);
124 tmp->name = name;
125 return tmp;
128 void add_sm_state_slist(struct state_list **slist, struct sm_state *new)
130 struct sm_state *tmp;
132 FOR_EACH_PTR(*slist, tmp) {
133 if (cmp_sm_states(tmp, new) < 0)
134 continue;
135 else if (cmp_sm_states(tmp, new) == 0) {
136 return;
137 } else {
138 INSERT_CURRENT(new, tmp);
139 return;
141 } END_FOR_EACH_PTR(tmp);
142 add_ptr_list(slist, new);
145 static void add_possible(struct sm_state *sm, struct sm_state *new)
147 struct sm_state *tmp;
148 struct sm_state *tmp2;
150 FOR_EACH_PTR(new->possible, tmp) {
151 tmp2 = alloc_state_no_name(tmp->owner, tmp->name, tmp->sym,
152 tmp->state);
153 tmp2->line = tmp->line;
154 add_sm_state_slist(&sm->possible, tmp2);
155 } END_FOR_EACH_PTR(tmp);
158 char *alloc_sname(const char *str)
160 char *tmp;
162 if (!str)
163 return NULL;
164 tmp = __alloc_sname(strlen(str) + 1);
165 strcpy(tmp, str);
166 return tmp;
169 int out_of_memory()
172 * I decided to use 50M here based on trial and error.
173 * It works out OK for the kernel and so it should work
174 * for most other projects as well.
176 if (sm_state_counter * sizeof(struct sm_state) >= 50000000)
177 return 1;
178 return 0;
181 static void free_sm_state(struct sm_state *sm)
183 free_slist(&sm->possible);
185 * fixme. Free the actual state.
186 * Right now we leave it until the end of the function
187 * because we don't want to double free it.
188 * Use the freelist to not double free things
192 static void free_all_sm_states(struct allocation_blob *blob)
194 unsigned int size = sizeof(struct sm_state);
195 unsigned int offset = 0;
197 while (offset < blob->offset) {
198 free_sm_state((struct sm_state *)(blob->data + offset));
199 offset += size;
203 /* At the end of every function we free all the sm_states */
204 void free_every_single_sm_state(void)
206 struct allocator_struct *desc = &sm_state_allocator;
207 struct allocation_blob *blob = desc->blobs;
209 desc->blobs = NULL;
210 desc->allocations = 0;
211 desc->total_bytes = 0;
212 desc->useful_bytes = 0;
213 desc->freelist = NULL;
214 while (blob) {
215 struct allocation_blob *next = blob->next;
216 free_all_sm_states(blob);
217 blob_free(blob, desc->chunking);
218 blob = next;
220 clear_sname_alloc();
222 sm_state_counter = 0;
225 struct sm_state *clone_sm(struct sm_state *s)
227 struct sm_state *ret;
229 ret = alloc_state_no_name(s->owner, s->name, s->sym, s->state);
230 ret->merged = s->merged;
231 ret->implied = s->implied;
232 ret->line = s->line;
233 /* clone_sm() doesn't copy the my_pools. Each state needs to have
234 only one my_pool. */
235 ret->possible = clone_slist(s->possible);
236 ret->left = s->left;
237 ret->right = s->right;
238 ret->nr_children = s->nr_children;
239 return ret;
242 int is_merged(struct sm_state *sm)
244 return sm->merged;
247 int is_implied(struct sm_state *sm)
249 return sm->implied;
252 int slist_has_state(struct state_list *slist, struct smatch_state *state)
254 struct sm_state *tmp;
256 FOR_EACH_PTR(slist, tmp) {
257 if (tmp->state == state)
258 return 1;
259 } END_FOR_EACH_PTR(tmp);
260 return 0;
263 static void check_order(struct state_list *slist)
265 #ifdef CHECKORDER
266 struct sm_state *sm;
267 struct sm_state *last = NULL;
268 int printed = 0;
270 FOR_EACH_PTR(slist, sm) {
271 if (last && cmp_tracker(sm, last) <= 0) {
272 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
273 "%s vs %s\n", last->owner, sm->owner,
274 last->sym, sm->sym, last->name, sm->name);
275 printed = 1;
277 last = state;
278 } END_FOR_EACH_PTR(sm);
280 if (printed)
281 printf("======\n");
282 #endif
285 struct state_list *clone_slist(struct state_list *from_slist)
287 struct sm_state *sm;
288 struct state_list *to_slist = NULL;
290 FOR_EACH_PTR(from_slist, sm) {
291 add_ptr_list(&to_slist, sm);
292 } END_FOR_EACH_PTR(sm);
293 check_order(to_slist);
294 return to_slist;
297 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
299 struct state_list *slist;
300 struct state_list_stack *to_stack = NULL;
302 FOR_EACH_PTR(from_stack, slist) {
303 push_slist(&to_stack, slist);
304 } END_FOR_EACH_PTR(slist);
305 return to_stack;
308 struct smatch_state *merge_states(int owner, const char *name,
309 struct symbol *sym,
310 struct smatch_state *state1,
311 struct smatch_state *state2)
313 struct smatch_state *ret;
315 if (state1 == state2)
316 ret = state1;
317 else if (__has_merge_function(owner))
318 ret = __client_merge_function(owner, name, sym, state1, state2);
319 else if (!state1 || !state2)
320 ret = &undefined;
321 else
322 ret = &merged;
323 return ret;
327 * add_pool() adds a slist to ->pools. If the slist has already been
328 * added earlier then it doesn't get added a second time.
330 void add_pool(struct state_list_stack **pools, struct state_list *new)
332 struct state_list *tmp;
334 FOR_EACH_PTR(*pools, tmp) {
335 if (tmp < new)
336 continue;
337 else if (tmp == new) {
338 return;
339 } else {
340 INSERT_CURRENT(new, tmp);
341 return;
343 } END_FOR_EACH_PTR(tmp);
344 add_ptr_list(pools, new);
347 struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two)
349 struct smatch_state *s;
350 struct sm_state *result;
352 if (one == two)
353 return one;
354 s = merge_states(one->owner, one->name, one->sym, one->state, two->state);
355 result = alloc_state_no_name(one->owner, one->name, one->sym, s);
356 result->merged = 1;
357 result->left = one;
358 result->right = two;
359 result->nr_children = one->nr_children + two->nr_children;
360 add_possible(result, one);
361 add_possible(result, two);
363 if (option_debug) {
364 struct sm_state *tmp;
365 int i = 0;
367 printf("%d merge name='%s' [%s] %s(L %d) + %s(L %d) => %s (",
368 get_lineno(), one->name, check_name(one->owner),
369 show_state(one->state), one->line,
370 show_state(two->state), two->line,
371 show_state(s));
373 FOR_EACH_PTR(result->possible, tmp) {
374 if (i++)
375 printf(", ");
376 printf("%s", show_state(tmp->state));
377 } END_FOR_EACH_PTR(tmp);
378 printf(")\n");
381 return result;
384 struct sm_state *get_sm_state_slist(struct state_list *slist, int owner, const char *name,
385 struct symbol *sym)
387 struct sm_state *sm;
389 if (!name)
390 return NULL;
392 FOR_EACH_PTR(slist, sm) {
393 if (sm->owner == owner && sm->sym == sym && !strcmp(sm->name, name))
394 return sm;
395 } END_FOR_EACH_PTR(sm);
396 return NULL;
399 struct smatch_state *get_state_slist(struct state_list *slist,
400 int owner, const char *name,
401 struct symbol *sym)
403 struct sm_state *sm;
405 sm = get_sm_state_slist(slist, owner, name, sym);
406 if (sm)
407 return sm->state;
408 return NULL;
411 void overwrite_sm_state(struct state_list **slist, struct sm_state *new)
413 struct sm_state *tmp;
415 FOR_EACH_PTR(*slist, tmp) {
416 if (cmp_tracker(tmp, new) < 0)
417 continue;
418 else if (cmp_tracker(tmp, new) == 0) {
419 REPLACE_CURRENT_PTR(tmp, new);
420 return;
421 } else {
422 INSERT_CURRENT(new, tmp);
423 return;
425 } END_FOR_EACH_PTR(tmp);
426 add_ptr_list(slist, new);
429 void overwrite_sm_state_stack(struct state_list_stack **stack,
430 struct sm_state *sm)
432 struct state_list *slist;
434 slist = pop_slist(stack);
435 overwrite_sm_state(&slist, sm);
436 push_slist(stack, slist);
439 struct sm_state *set_state_slist(struct state_list **slist, int owner, const char *name,
440 struct symbol *sym, struct smatch_state *state)
442 struct sm_state *tmp;
443 struct sm_state *new = alloc_sm_state(owner, name, sym, state);
445 FOR_EACH_PTR(*slist, tmp) {
446 if (cmp_tracker(tmp, new) < 0)
447 continue;
448 else if (cmp_tracker(tmp, new) == 0) {
449 REPLACE_CURRENT_PTR(tmp, new);
450 return new;
451 } else {
452 INSERT_CURRENT(new, tmp);
453 return new;
455 } END_FOR_EACH_PTR(tmp);
456 add_ptr_list(slist, new);
457 return new;
460 void delete_state_slist(struct state_list **slist, int owner, const char *name,
461 struct symbol *sym)
463 struct sm_state *sm;
465 FOR_EACH_PTR(*slist, sm) {
466 if (sm->owner == owner && sm->sym == sym && !strcmp(sm->name, name)) {
467 DELETE_CURRENT_PTR(sm);
468 return;
470 } END_FOR_EACH_PTR(sm);
473 void delete_state_stack(struct state_list_stack **stack, int owner, const char *name,
474 struct symbol *sym)
476 struct state_list *slist;
478 slist = pop_slist(stack);
479 delete_state_slist(&slist, owner, name, sym);
480 push_slist(stack, slist);
483 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
485 add_ptr_list(list_stack, slist);
488 struct state_list *pop_slist(struct state_list_stack **list_stack)
490 struct state_list *slist;
492 slist = last_ptr_list((struct ptr_list *)*list_stack);
493 delete_ptr_list_last((struct ptr_list **)list_stack);
494 return slist;
497 void free_slist(struct state_list **slist)
499 __free_ptr_list((struct ptr_list **)slist);
502 void free_stack(struct state_list_stack **stack)
504 __free_ptr_list((struct ptr_list **)stack);
507 void free_stack_and_slists(struct state_list_stack **slist_stack)
509 struct state_list *slist;
511 FOR_EACH_PTR(*slist_stack, slist) {
512 free_slist(&slist);
513 } END_FOR_EACH_PTR(slist);
514 free_stack(slist_stack);
518 * set_state_stack() sets the state for the top slist on the stack.
520 struct sm_state *set_state_stack(struct state_list_stack **stack, int owner, const char *name,
521 struct symbol *sym, struct smatch_state *state)
523 struct state_list *slist;
524 struct sm_state *sm;
526 slist = pop_slist(stack);
527 sm = set_state_slist(&slist, owner, name, sym, state);
528 push_slist(stack, slist);
530 return sm;
534 * get_sm_state_stack() gets the state for the top slist on the stack.
536 struct sm_state *get_sm_state_stack(struct state_list_stack *stack,
537 int owner, const char *name,
538 struct symbol *sym)
540 struct state_list *slist;
541 struct sm_state *ret;
543 slist = pop_slist(&stack);
544 ret = get_sm_state_slist(slist, owner, name, sym);
545 push_slist(&stack, slist);
546 return ret;
549 struct smatch_state *get_state_stack(struct state_list_stack *stack,
550 int owner, const char *name,
551 struct symbol *sym)
553 struct sm_state *sm;
555 sm = get_sm_state_stack(stack, owner, name, sym);
556 if (sm)
557 return sm->state;
558 return NULL;
561 static void match_states(struct state_list **one, struct state_list **two)
563 struct sm_state *one_sm;
564 struct sm_state *two_sm;
565 struct sm_state *tmp;
566 struct smatch_state *tmp_state;
567 struct state_list *add_to_one = NULL;
568 struct state_list *add_to_two = NULL;
570 PREPARE_PTR_LIST(*one, one_sm);
571 PREPARE_PTR_LIST(*two, two_sm);
572 for (;;) {
573 if (!one_sm && !two_sm)
574 break;
575 if (cmp_tracker(one_sm, two_sm) < 0) {
576 tmp_state = __client_unmatched_state_function(one_sm);
577 tmp = alloc_state_no_name(one_sm->owner, one_sm->name,
578 one_sm->sym, tmp_state);
579 add_ptr_list(&add_to_two, tmp);
580 NEXT_PTR_LIST(one_sm);
581 } else if (cmp_tracker(one_sm, two_sm) == 0) {
582 NEXT_PTR_LIST(one_sm);
583 NEXT_PTR_LIST(two_sm);
584 } else {
585 tmp_state = __client_unmatched_state_function(two_sm);
586 tmp = alloc_state_no_name(two_sm->owner, two_sm->name,
587 two_sm->sym, tmp_state);
588 add_ptr_list(&add_to_one, tmp);
589 NEXT_PTR_LIST(two_sm);
592 FINISH_PTR_LIST(two_sm);
593 FINISH_PTR_LIST(one_sm);
595 overwrite_slist(add_to_one, one);
596 overwrite_slist(add_to_two, two);
599 static void clone_pool_havers(struct state_list *slist)
601 struct sm_state *sm;
602 struct sm_state *new;
604 FOR_EACH_PTR(slist, sm) {
605 if (sm->my_pool) {
606 new = clone_sm(sm);
607 REPLACE_CURRENT_PTR(sm, new);
609 } END_FOR_EACH_PTR(sm);
613 * merge_slist() is called whenever paths merge, such as after
614 * an if statement. It takes the two slists and creates one.
616 void merge_slist(struct state_list **to, struct state_list *slist)
618 struct sm_state *one_sm, *two_sm, *tmp;
619 struct state_list *results = NULL;
620 struct state_list *implied_one = NULL;
621 struct state_list *implied_two = NULL;
623 if (out_of_memory())
624 return;
626 check_order(*to);
627 check_order(slist);
629 /* merging a null and nonnull path gives you only the nonnull path */
630 if (!slist)
631 return;
633 if (!*to) {
634 *to = clone_slist(slist);
635 return;
638 implied_one = clone_slist(*to);
639 implied_two = clone_slist(slist);
641 match_states(&implied_one, &implied_two);
643 clone_pool_havers(implied_one);
644 clone_pool_havers(implied_two);
646 PREPARE_PTR_LIST(implied_one, one_sm);
647 PREPARE_PTR_LIST(implied_two, two_sm);
648 for (;;) {
649 if (!one_sm && !two_sm)
650 break;
651 if (cmp_tracker(one_sm, two_sm) < 0) {
652 sm_msg("error: Internal smatch error.");
653 NEXT_PTR_LIST(one_sm);
654 } else if (cmp_tracker(one_sm, two_sm) == 0) {
655 if (one_sm != two_sm) {
656 one_sm->my_pool = implied_one;
657 two_sm->my_pool = implied_two;
660 tmp = merge_sm_states(one_sm, two_sm);
661 add_ptr_list(&results, tmp);
662 NEXT_PTR_LIST(one_sm);
663 NEXT_PTR_LIST(two_sm);
664 } else {
665 sm_msg("error: Internal smatch error.");
666 NEXT_PTR_LIST(two_sm);
669 FINISH_PTR_LIST(two_sm);
670 FINISH_PTR_LIST(one_sm);
672 free_slist(to);
673 *to = results;
677 * filter_slist() removes any sm states "slist" holds in common with "filter"
679 void filter_slist(struct state_list **slist, struct state_list *filter)
681 struct sm_state *one_sm, *two_sm;
682 struct state_list *results = NULL;
684 PREPARE_PTR_LIST(*slist, one_sm);
685 PREPARE_PTR_LIST(filter, two_sm);
686 for (;;) {
687 if (!one_sm && !two_sm)
688 break;
689 if (cmp_tracker(one_sm, two_sm) < 0) {
690 add_ptr_list(&results, one_sm);
691 NEXT_PTR_LIST(one_sm);
692 } else if (cmp_tracker(one_sm, two_sm) == 0) {
693 if (one_sm != two_sm)
694 add_ptr_list(&results, one_sm);
695 NEXT_PTR_LIST(one_sm);
696 NEXT_PTR_LIST(two_sm);
697 } else {
698 NEXT_PTR_LIST(two_sm);
701 FINISH_PTR_LIST(two_sm);
702 FINISH_PTR_LIST(one_sm);
704 free_slist(slist);
705 *slist = results;
709 * and_slist_stack() pops the top two slists, overwriting the one with
710 * the other and pushing it back on the stack.
712 void and_slist_stack(struct state_list_stack **slist_stack)
714 struct sm_state *tmp;
715 struct state_list *right_slist = pop_slist(slist_stack);
717 FOR_EACH_PTR(right_slist, tmp) {
718 overwrite_sm_state_stack(slist_stack, tmp);
719 } END_FOR_EACH_PTR(tmp);
720 free_slist(&right_slist);
724 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
725 * It pops the two slists from the top of the stack and merges them
726 * together in a way that preserves the things they have in common
727 * but creates a merged state for most of the rest.
728 * You could have code that had: if (foo || foo) { foo->baz;
729 * It's this function which ensures smatch does the right thing.
731 void or_slist_stack(struct state_list_stack **pre_conds,
732 struct state_list *cur_slist,
733 struct state_list_stack **slist_stack)
735 struct state_list *new;
736 struct state_list *old;
737 struct state_list *pre_slist;
738 struct state_list *res;
739 struct state_list *tmp_slist;
741 new = pop_slist(slist_stack);
742 old = pop_slist(slist_stack);
744 pre_slist = pop_slist(pre_conds);
745 push_slist(pre_conds, clone_slist(pre_slist));
747 res = clone_slist(pre_slist);
748 overwrite_slist(old, &res);
750 tmp_slist = clone_slist(cur_slist);
751 overwrite_slist(new, &tmp_slist);
753 merge_slist(&res, tmp_slist);
754 filter_slist(&res, pre_slist);
756 push_slist(slist_stack, res);
757 free_slist(&tmp_slist);
758 free_slist(&pre_slist);
759 free_slist(&new);
760 free_slist(&old);
764 * get_slist_from_named_stack() is only used for gotos.
766 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
767 const char *name)
769 struct named_slist *tmp;
771 FOR_EACH_PTR(stack, tmp) {
772 if (!strcmp(tmp->name, name))
773 return &tmp->slist;
774 } END_FOR_EACH_PTR(tmp);
775 return NULL;
778 void overwrite_slist(struct state_list *from, struct state_list **to)
780 struct sm_state *tmp;
782 FOR_EACH_PTR(from, tmp) {
783 overwrite_sm_state(to, tmp);
784 } END_FOR_EACH_PTR(tmp);