Don't delete the ->pools on merging.
[smatch.git] / smatch_slist.c
blob36cc13c81b1c6b5122c5cbe082cd443dd6bb6384
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 ALLOCATOR(sm_state, "smatch state");
17 ALLOCATOR(named_slist, "named slist");
19 #undef CHECKORDER
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);
44 static void add_possible(struct sm_state *sm, struct sm_state *new)
46 struct sm_state *tmp;
48 FOR_EACH_PTR(sm->possible, tmp) {
49 if (tmp->state < new->state) {
50 continue;
51 } else if (tmp->state == new->state) {
52 return;
53 } else {
54 INSERT_CURRENT(new, tmp);
55 return;
57 } END_FOR_EACH_PTR(tmp);
58 add_ptr_list(&sm->possible, new);
61 struct sm_state *alloc_state(const char *name, int owner,
62 struct symbol *sym, struct smatch_state *state)
64 struct sm_state *sm_state = __alloc_sm_state(0);
66 sm_state->name = (char *)name;
67 sm_state->owner = owner;
68 sm_state->sym = sym;
69 sm_state->state = state;
70 sm_state->line_history = NULL;
71 add_history(sm_state);
72 sm_state->pools = NULL;
73 sm_state->possible = NULL;
74 add_possible(sm_state, sm_state);
75 return sm_state;
78 struct sm_state *clone_state(struct sm_state *s)
80 struct sm_state *tmp;
82 tmp = alloc_state(s->name, s->owner, s->sym, s->state);
83 tmp->pools = clone_stack(s->pools);
84 return tmp;
87 /* NULL states go at the end to simplify merge_slist */
88 static int cmp_sm_states(const struct sm_state *a, const struct sm_state *b)
90 int ret;
92 if (!a && !b)
93 return 0;
94 if (!b)
95 return -1;
96 if (!a)
97 return 1;
99 if (a->owner > b->owner)
100 return -1;
101 if (a->owner < b->owner)
102 return 1;
104 ret = strcmp(a->name, b->name);
105 if (ret)
106 return ret;
108 if (!b->sym && a->sym)
109 return -1;
110 if (!a->sym && b->sym)
111 return 1;
112 if (a->sym > b->sym)
113 return -1;
114 if (a->sym < b->sym)
115 return 1;
117 return 0;
120 int slist_has_state(struct state_list *slist, struct smatch_state *state)
122 struct sm_state *tmp;
124 FOR_EACH_PTR(slist, tmp) {
125 if (tmp->state == state)
126 return 1;
127 } END_FOR_EACH_PTR(tmp);
128 return 0;
131 #ifdef CHECKORDER
132 static void check_order(struct state_list *slist)
134 struct sm_state *state;
135 struct sm_state *last = NULL;
136 int printed = 0;
138 FOR_EACH_PTR(slist, state) {
139 if (last && cmp_sm_states(state, last) <= 0) {
140 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
141 "%s vs %s\n", last->owner, state->owner,
142 last->sym, state->sym, last->name, state->name);
143 printed = 1;
145 last = state;
146 } END_FOR_EACH_PTR(state);
148 if (printed)
149 printf("======\n");
151 #endif
153 struct state_list *clone_slist(struct state_list *from_slist)
155 struct sm_state *state;
156 struct sm_state *tmp;
157 struct state_list *to_slist = NULL;
159 FOR_EACH_PTR(from_slist, state) {
160 tmp = clone_state(state);
161 add_ptr_list(&to_slist, tmp);
162 } END_FOR_EACH_PTR(state);
163 #ifdef CHECKORDER
164 check_order(to_slist);
165 #endif
166 return to_slist;
169 struct state_list_stack *clone_stack(struct state_list_stack *from_stack)
171 struct state_list *slist;
172 struct state_list_stack *to_stack = NULL;
174 FOR_EACH_PTR(from_stack, slist) {
175 push_slist(&to_stack, slist);
176 } END_FOR_EACH_PTR(slist);
177 return to_stack;
180 // FIXME... shouldn't we free some of these state pointers?
181 struct smatch_state *merge_states(const char *name, int owner,
182 struct symbol *sym,
183 struct smatch_state *state1,
184 struct smatch_state *state2)
186 struct smatch_state *ret;
188 if (state1 == state2)
189 ret = state1;
190 else if (__has_merge_function(owner))
191 ret = __client_merge_function(owner, name, sym, state1, state2);
192 else
193 ret = &merged;
195 SM_DEBUG("%d merge name='%s' owner=%d: %s + %s => %s\n",
196 get_lineno(), name, owner, show_state(state1),
197 show_state(state2), show_state(ret));
199 return ret;
202 struct smatch_state *get_state_slist(struct state_list *slist,
203 const char *name, int owner,
204 struct symbol *sym)
206 struct sm_state *state;
208 if (!name)
209 return NULL;
211 FOR_EACH_PTR(slist, state) {
212 if (state->owner == owner && state->sym == sym
213 && !strcmp(state->name, name))
214 return state->state;
215 } END_FOR_EACH_PTR(state);
216 return NULL;
219 struct sm_state *get_sm_state_slist(struct state_list *slist, const char *name,
220 int owner, struct symbol *sym)
222 struct sm_state *state;
224 if (!name)
225 return NULL;
227 FOR_EACH_PTR(slist, state) {
228 if (state->owner == owner && state->sym == sym
229 && !strcmp(state->name, name))
230 return state;
231 } END_FOR_EACH_PTR(state);
232 return NULL;
235 static void overwrite_sm_state(struct state_list **slist,
236 struct sm_state *state)
238 struct sm_state *tmp;
239 struct sm_state *new = clone_state(state);
241 FOR_EACH_PTR(*slist, tmp) {
242 if (cmp_sm_states(tmp, new) < 0)
243 continue;
244 else if (cmp_sm_states(tmp, new) == 0) {
245 tmp->state = new->state;
246 tmp->pools = new->pools;
247 tmp->possible = new->possible;
248 __free_sm_state(new);
249 return;
250 } else {
251 INSERT_CURRENT(new, tmp);
252 return;
254 } END_FOR_EACH_PTR(tmp);
255 add_ptr_list(slist, new);
258 void set_state_slist(struct state_list **slist, const char *name, int owner,
259 struct symbol *sym, struct smatch_state *state)
261 struct sm_state *tmp;
262 struct sm_state *new = alloc_state(name, owner, sym, state);
264 FOR_EACH_PTR(*slist, tmp) {
265 if (cmp_sm_states(tmp, new) < 0)
266 continue;
267 else if (cmp_sm_states(tmp, new) == 0) {
268 tmp->state = state;
269 tmp->pools = NULL;
270 tmp->possible = new->possible;
271 __free_sm_state(new);
272 return;
273 } else {
274 INSERT_CURRENT(new, tmp);
275 return;
277 } END_FOR_EACH_PTR(tmp);
278 add_ptr_list(slist, new);
281 void delete_state_slist(struct state_list **slist, const char *name, int owner,
282 struct symbol *sym)
284 struct sm_state *state;
286 FOR_EACH_PTR(*slist, state) {
287 if (state->owner == owner && state->sym == sym
288 && !strcmp(state->name, name)){
289 delete_ptr_list_entry((struct ptr_list **)slist,
290 state, 1);
291 __free_sm_state(state);
292 return;
294 } END_FOR_EACH_PTR(state);
298 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
300 add_ptr_list(list_stack, slist);
303 struct state_list *pop_slist(struct state_list_stack **list_stack)
305 struct state_list *slist;
307 slist = last_ptr_list((struct ptr_list *)*list_stack);
308 delete_ptr_list_last((struct ptr_list **)list_stack);
309 return slist;
312 void del_slist(struct state_list **slist)
314 __free_ptr_list((struct ptr_list **)slist);
317 void del_slist_stack(struct state_list_stack **slist_stack)
319 struct state_list *slist;
321 FOR_EACH_PTR(*slist_stack, slist) {
322 __free_ptr_list((struct ptr_list **)&slist);
323 } END_FOR_EACH_PTR(slist);
324 __free_ptr_list((struct ptr_list **)slist_stack);
328 * set_state_stack() sets the state for the top slist on the stack.
330 void set_state_stack(struct state_list_stack **stack, const char *name,
331 int owner, struct symbol *sym, struct smatch_state *state)
333 struct state_list *slist;
335 slist = pop_slist(stack);
336 set_state_slist(&slist, name, owner, sym, state);
337 push_slist(stack, slist);
341 * get_state_stack() gets the state for the top slist on the stack.
343 struct smatch_state *get_state_stack(struct state_list_stack *stack,
344 const char *name, int owner,
345 struct symbol *sym)
347 struct state_list *slist;
348 struct smatch_state *ret;
350 slist = pop_slist(&stack);
351 ret = get_state_slist(slist, name, owner, sym);
352 push_slist(&stack, slist);
353 return ret;
356 static void add_pool(struct sm_state *state, struct state_list **pool)
358 add_ptr_list(pool, state);
359 push_slist(&state->pools, *pool);
362 /* fixme. rename. heck cleanup this whole file */
363 static void add_single_pool(struct sm_state *to, struct state_list *new)
365 struct state_list *tmp;
367 FOR_EACH_PTR(to->pools, tmp) {
368 if (tmp < new)
369 continue;
370 else if (tmp == new) {
371 return;
372 } else {
373 INSERT_CURRENT(new, tmp);
374 return;
376 } END_FOR_EACH_PTR(tmp);
377 add_ptr_list(&to->pools, new);
380 static void copy_pools(struct sm_state *to, struct sm_state *sm)
382 struct state_list *tmp;
384 FOR_EACH_PTR(sm->pools, tmp) {
385 add_single_pool(to, tmp);
386 } END_FOR_EACH_PTR(tmp);
389 void merge_slist(struct state_list **to, struct state_list *slist)
391 struct sm_state *to_state, *state, *tmp;
392 struct state_list *results = NULL;
393 struct smatch_state *s;
394 struct state_list *implied_to = NULL;
395 struct state_list *implied_from = NULL;
397 #ifdef CHECKORDER
398 check_order(*to);
399 check_order(slist);
400 #endif
402 /* merging a null and nonnull path gives you only the nonnull path */
403 if (!slist) {
404 return;
406 if (!*to) {
407 *to = clone_slist(slist);
408 return;
411 PREPARE_PTR_LIST(*to, to_state);
412 PREPARE_PTR_LIST(slist, state);
413 for (;;) {
414 if (!to_state && !state)
415 break;
416 if (cmp_sm_states(to_state, state) < 0) {
417 s = merge_states(to_state->name, to_state->owner,
418 to_state->sym, to_state->state, NULL);
419 tmp = alloc_state(to_state->name, to_state->owner,
420 to_state->sym, s);
421 //add_possible(to_state, NULL);
422 add_pool(to_state, &implied_to);
423 copy_pools(tmp, to_state);
424 add_single_pool(tmp, implied_to);
425 add_ptr_list(&results, tmp);
426 NEXT_PTR_LIST(to_state);
427 } else if (cmp_sm_states(to_state, state) == 0) {
428 if (to_state->state == state->state) {
429 s = to_state->state;
430 tmp = alloc_state(to_state->name,
431 to_state->owner,
432 to_state->sym, s);
434 } else {
435 s = merge_states(to_state->name,
436 to_state->owner,
437 to_state->sym, to_state->state,
438 state->state);
440 tmp = alloc_state(to_state->name,
441 to_state->owner,
442 to_state->sym, s);
445 add_ptr_list(&implied_to, to_state);
446 add_ptr_list(&implied_from, state);
447 copy_pools(tmp, to_state);
448 copy_pools(tmp, state);
449 add_single_pool(tmp, implied_to);
450 add_single_pool(tmp, implied_from);
452 add_ptr_list(&results, tmp);
453 NEXT_PTR_LIST(to_state);
454 NEXT_PTR_LIST(state);
455 } else {
456 s = merge_states(state->name, state->owner,
457 state->sym, state->state, NULL);
458 tmp = alloc_state(state->name, state->owner,
459 state->sym, s);
460 //add_possible(state, NULL);
461 add_pool(state, &implied_from);
462 copy_pools(tmp, state);
463 add_single_pool(tmp, implied_from);
464 add_ptr_list(&results, tmp);
465 NEXT_PTR_LIST(state);
468 FINISH_PTR_LIST(state);
469 FINISH_PTR_LIST(to_state);
471 del_slist(to);
472 *to = results;
474 if (implied_from)
475 push_slist(&implied_pools, implied_from);
476 if (implied_to)
477 push_slist(&implied_pools, implied_to);
481 * filter() is used to find what states are the same across
482 * a series of slists.
483 * It takes a **slist and a *filter.
484 * It removes everything from **slist that isn't in *filter.
485 * The reason you would want to do this is if you want to
486 * know what other states are true if one state is true. (smatch_implied).
489 void filter(struct state_list **slist, struct state_list *filter)
491 struct sm_state *s_one, *s_two;
492 struct state_list **results;
494 #ifdef CHECKORDER
495 check_order(*slist);
496 check_order(filter);
497 #endif
499 results = malloc(sizeof(*results));
500 *results = NULL;
502 PREPARE_PTR_LIST(*slist, s_one);
503 PREPARE_PTR_LIST(filter, s_two);
504 for (;;) {
505 if (!s_one || !s_two)
506 break;
507 if (cmp_sm_states(s_one, s_two) < 0) {
508 SM_DEBUG("different missing in new %s\n", s_one->name);
509 NEXT_PTR_LIST(s_one);
510 } else if (cmp_sm_states(s_one, s_two) == 0) {
511 if (s_one->state == s_two->state) {
512 add_ptr_list(results, s_one);
513 SM_DEBUG("same %s\n", s_one->name);
514 } else
515 SM_DEBUG("different %s %s vs %s\n", s_one->name,
516 show_state(s_one->state),
517 show_state(s_two->state));
518 NEXT_PTR_LIST(s_one);
519 NEXT_PTR_LIST(s_two);
520 } else {
521 SM_DEBUG("different missing in old%s\n", s_two->name);
522 NEXT_PTR_LIST(s_two);
525 FINISH_PTR_LIST(s_two);
526 FINISH_PTR_LIST(s_one);
528 del_slist(slist);
529 *slist = *results;
533 * This function is basically the same as popping the top two slists,
534 * overwriting the one with the other and pushing it back on the stack.
535 * The difference is that it checks to see that a mutually exclusive
536 * state isn't included in both stacks.
539 void and_slist_stack(struct state_list_stack **slist_stack)
541 struct sm_state *tmp;
542 struct smatch_state *tmp_state;
543 struct state_list *tmp_slist = pop_slist(slist_stack);
545 FOR_EACH_PTR(tmp_slist, tmp) {
546 tmp_state = get_state_stack(*slist_stack, tmp->name,
547 tmp->owner, tmp->sym);
548 if (tmp_state && tmp_state != tmp->state) {
549 smatch_msg("mutually exclusive 'and' conditions states "
550 "'%s': %s & %s.\n",
551 tmp->name, show_state(tmp_state),
552 show_state(tmp->state));
553 tmp->state = merge_states(tmp->name, tmp->owner,
554 tmp->sym, tmp->state,
555 tmp_state);
557 set_state_stack(slist_stack, tmp->name, tmp->owner, tmp->sym,
558 tmp->state);
560 } END_FOR_EACH_PTR(tmp);
561 del_slist(&tmp_slist);
564 void or_slist_stack(struct state_list_stack **slist_stack)
566 struct state_list *one;
567 struct state_list *two;
568 struct state_list *res = NULL;
569 struct sm_state *tmp;
570 struct smatch_state *s;
572 one = pop_slist(slist_stack);
573 two = pop_slist(slist_stack);
575 FOR_EACH_PTR(one, tmp) {
576 s = get_state_slist(two, tmp->name, tmp->owner, tmp->sym);
577 s = merge_states(tmp->name, tmp->owner, tmp->sym,
578 tmp->state, s);
579 set_state_slist(&res, tmp->name, tmp->owner, tmp->sym, s);
580 } END_FOR_EACH_PTR(tmp);
583 FOR_EACH_PTR(two, tmp) {
584 s = get_state_slist(one, tmp->name, tmp->owner, tmp->sym);
585 s = merge_states(tmp->name, tmp->owner, tmp->sym,
586 tmp->state, s);
587 set_state_slist(&res, tmp->name, tmp->owner, tmp->sym, s);
588 } END_FOR_EACH_PTR(tmp);
590 push_slist(slist_stack, res);
592 del_slist(&one);
593 del_slist(&two);
596 struct state_list **get_slist_from_slist_stack(struct slist_stack *stack,
597 const char *name)
599 struct named_slist *tmp;
601 FOR_EACH_PTR(stack, tmp) {
602 if (!strcmp(tmp->name, name))
603 return &tmp->slist;
604 } END_FOR_EACH_PTR(tmp);
605 return NULL;
608 void overwrite_slist(struct state_list *from, struct state_list **to)
610 struct sm_state *tmp;
612 FOR_EACH_PTR(from, tmp) {
613 overwrite_sm_state(to, tmp);
614 } END_FOR_EACH_PTR(tmp);