Call the merge_hook() instead of setting the state to &undefined.
[smatch.git] / smatch_slist.c
blob423179927f7bb4671117549607d8d4e5c7154069
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 smatch_state *s;
440 struct state_list *implied_to = NULL;
441 struct state_list *implied_from = NULL;
443 #ifdef CHECKORDER
444 check_order(*to);
445 check_order(slist);
446 #endif
448 /* merging a null and nonnull path gives you only the nonnull path */
449 if (!slist) {
450 return;
452 if (!*to) {
453 *to = clone_slist(slist);
454 return;
457 PREPARE_PTR_LIST(*to, to_state);
458 PREPARE_PTR_LIST(slist, state);
459 for (;;) {
460 if (!to_state && !state)
461 break;
462 if (cmp_tracker(to_state, state) < 0) {
463 tmp = merge_sm_states(to_state, NULL);
465 copy_pools(tmp, to_state);
467 add_ptr_list(&implied_to, to_state);
468 add_pool(tmp, implied_to);
470 add_ptr_list(&results, tmp);
471 NEXT_PTR_LIST(to_state);
472 } else if (cmp_tracker(to_state, state) == 0) {
473 if (to_state->state == state->state) {
474 s = to_state->state;
475 tmp = alloc_state(to_state->name,
476 to_state->owner,
477 to_state->sym, s);
478 copy_pools(tmp, to_state);
479 copy_pools(tmp, state);
480 } else {
481 tmp = merge_sm_states(to_state, state);
483 copy_pools(tmp, to_state);
484 copy_pools(tmp, state);
486 add_ptr_list(&implied_to, to_state);
487 add_pool(tmp, implied_to);
488 add_ptr_list(&implied_from, state);
489 add_pool(tmp, implied_from);
491 add_ptr_list(&results, tmp);
492 NEXT_PTR_LIST(to_state);
493 NEXT_PTR_LIST(state);
494 } else {
495 tmp = merge_sm_states(state, NULL);
497 copy_pools(tmp, state);
499 add_ptr_list(&implied_from, state);
500 add_pool(tmp, implied_from);
502 add_ptr_list(&results, tmp);
503 NEXT_PTR_LIST(state);
506 FINISH_PTR_LIST(state);
507 FINISH_PTR_LIST(to_state);
509 del_slist(to);
510 *to = results;
512 if (implied_from)
513 push_slist(&implied_pools, implied_from);
514 if (implied_to)
515 push_slist(&implied_pools, implied_to);
519 * is_currently_in_pool() is used because we remove states from pools.
520 * When set_state() is called then we set ->pools to NULL, but on
521 * other paths the state is still a member of those pools.
522 * Confusing huh?
523 * if (foo) {
524 * bar = 1;
525 * a = malloc();
527 * if (!a)
528 * return;
529 * if (bar)
530 * a->b = x;
532 static int is_currently_in_pool(struct sm_state *sm, struct state_list *pool,
533 struct state_list *cur_slist)
535 struct sm_state *cur_state;
536 struct state_list *tmp;
538 cur_state = get_sm_state_slist(cur_slist, sm->name, sm->owner, sm->sym);
539 if (!cur_state)
540 return 0;
542 FOR_EACH_PTR(cur_state->pools, tmp) {
543 if (tmp == pool)
544 return 1;
545 } END_FOR_EACH_PTR(tmp);
546 return 0;
549 struct state_list *clone_states_in_pool(struct state_list *pool,
550 struct state_list *cur_slist)
552 struct sm_state *state;
553 struct sm_state *tmp;
554 struct state_list *to_slist = NULL;
556 FOR_EACH_PTR(pool, state) {
557 if (is_currently_in_pool(state, pool, cur_slist)) {
558 tmp = clone_state(state);
559 add_ptr_list(&to_slist, tmp);
561 } END_FOR_EACH_PTR(state);
562 #ifdef CHECKORDER
563 check_order(to_slist);
564 #endif
565 return to_slist;
569 * filter() is used to find what states are the same across
570 * a series of slists.
571 * It takes a **slist and a *filter.
572 * It removes everything from **slist that isn't in *filter.
573 * The reason you would want to do this is if you want to
574 * know what other states are true if one state is true. (smatch_implied).
576 void filter(struct state_list **slist, struct state_list *filter,
577 struct state_list *cur_slist)
579 struct sm_state *s_one, *s_two;
580 struct state_list *results = NULL;
582 #ifdef CHECKORDER
583 check_order(*slist);
584 check_order(filter);
585 #endif
587 PREPARE_PTR_LIST(*slist, s_one);
588 PREPARE_PTR_LIST(filter, s_two);
589 for (;;) {
590 if (!s_one || !s_two)
591 break;
592 if (cmp_tracker(s_one, s_two) < 0) {
593 NEXT_PTR_LIST(s_one);
594 } else if (cmp_tracker(s_one, s_two) == 0) {
595 /* todo. pointer comparison works fine for most things
596 except smatch_extra. we may need a hook here. */
597 if (s_one->state == s_two->state &&
598 is_currently_in_pool(s_two, filter, cur_slist)) {
599 add_ptr_list(&results, s_one);
601 NEXT_PTR_LIST(s_one);
602 NEXT_PTR_LIST(s_two);
603 } else {
604 NEXT_PTR_LIST(s_two);
607 FINISH_PTR_LIST(s_two);
608 FINISH_PTR_LIST(s_one);
610 del_slist(slist);
611 *slist = results;
615 * and_slist_stack() is basically the same as popping the top two slists,
616 * overwriting the one with the other and pushing it back on the stack.
617 * The difference is that it checks to see that a mutually exclusive
618 * state isn't included in both stacks. If smatch sees something like
619 * "if (a && !a)" it prints a warning.
621 void and_slist_stack(struct state_list_stack **slist_stack)
623 struct sm_state *tmp;
624 struct smatch_state *tmp_state;
625 struct state_list *tmp_slist = pop_slist(slist_stack);
627 FOR_EACH_PTR(tmp_slist, tmp) {
628 tmp_state = get_state_stack(*slist_stack, tmp->name,
629 tmp->owner, tmp->sym);
630 if (tmp_state && tmp_state != tmp->state) {
631 smatch_msg("mutually exclusive 'and' conditions states "
632 "'%s': %s & %s.\n",
633 tmp->name, show_state(tmp_state),
634 show_state(tmp->state));
635 tmp->state = merge_states(tmp->name, tmp->owner,
636 tmp->sym, tmp->state,
637 tmp_state);
639 set_state_stack(slist_stack, tmp->name, tmp->owner, tmp->sym,
640 tmp->state);
641 } END_FOR_EACH_PTR(tmp);
642 del_slist(&tmp_slist);
646 * or_slist_stack() is for if we have: if (foo || bar) { foo->baz;
647 * It pops the two slists from the top of the stack and merges them
648 * together in a way that preserves the things they have in common
649 * but creates a merged state for most of the rest.
650 * You could have code that had: if (foo || foo) { foo->baz;
651 * It's this function which ensures smatch does the right thing.
653 void or_slist_stack(struct state_list_stack **slist_stack)
655 struct state_list *one;
656 struct state_list *two;
657 struct state_list *res = NULL;
658 struct sm_state *tmp;
659 struct sm_state *sm;
660 struct sm_state *new_sm;
662 one = pop_slist(slist_stack);
663 two = pop_slist(slist_stack);
665 FOR_EACH_PTR(one, tmp) {
666 sm = get_sm_state_slist(two, 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 FOR_EACH_PTR(two, tmp) {
672 sm = get_sm_state_slist(one, tmp->name, tmp->owner, tmp->sym);
673 new_sm = merge_sm_states(tmp, sm);
674 add_ptr_list(&res, new_sm);
675 } END_FOR_EACH_PTR(tmp);
677 push_slist(slist_stack, res);
679 del_slist(&one);
680 del_slist(&two);
684 * get_slist_from_named_stack() is only used for gotos.
686 struct state_list **get_slist_from_named_stack(struct named_stack *stack,
687 const char *name)
689 struct named_slist *tmp;
691 FOR_EACH_PTR(stack, tmp) {
692 if (!strcmp(tmp->name, name))
693 return &tmp->slist;
694 } END_FOR_EACH_PTR(tmp);
695 return NULL;
698 void overwrite_slist(struct state_list *from, struct state_list **to)
700 struct sm_state *tmp;
702 FOR_EACH_PTR(from, tmp) {
703 overwrite_sm_state(to, tmp);
704 } END_FOR_EACH_PTR(tmp);