Make goto_stack static.
[smatch.git] / smatch_slist.c
blobebe8de6716a4cf0cd2547bed4f33c447a7b44c74
1 /*
2 * sparse/smatch_slist.c
4 * Copyright (C) 2008 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 add_history(struct sm_state *state)
23 struct state_history *tmp;
25 if (!state)
26 return;
27 tmp = malloc(sizeof(*tmp));
28 tmp->loc = get_lineno();
29 add_ptr_list(&state->line_history, tmp);
32 struct sm_state *alloc_state(const char *name, int owner,
33 struct symbol *sym, struct smatch_state *state)
35 struct sm_state *sm_state = __alloc_sm_state(0);
37 sm_state->name = (char *)name;
38 sm_state->owner = owner;
39 sm_state->sym = sym;
40 sm_state->state = state;
41 sm_state->line_history = NULL;
42 sm_state->path_history = NULL;
43 add_history(sm_state);
44 return sm_state;
47 struct sm_state *clone_state(struct sm_state *s)
49 return alloc_state(s->name, s->owner, s->sym, s->state);
52 /* NULL states go at the end to simplify merge_slist */
53 static int cmp_sm_states(const void *_a, const void *_b)
55 const struct sm_state *a = _a;
56 const struct sm_state *b = _b;
57 int ret;
59 if (!a && !b)
60 return 0;
61 if (!b)
62 return -1;
63 if (!a)
64 return 1;
66 if (a->owner > b->owner)
67 return -1;
68 if (a->owner < b->owner)
69 return 1;
71 ret = strcmp(a->name, b->name);
72 if (ret)
73 return ret;
75 if (!b->sym && a->sym)
76 return -1;
77 if (!a->sym && b->sym)
78 return 1;
79 if (a->sym > b->sym)
80 return -1;
81 if (a->sym < b->sym)
82 return 1;
84 return 0;
87 #ifdef CHECKORDER
88 static void check_order(struct state_list *slist)
90 struct sm_state *state;
91 struct sm_state *last = NULL;
92 int printed = 0;
94 FOR_EACH_PTR(slist, state) {
95 if (last && cmp_sm_states(state, last) <= 0) {
96 printf("Error. Unsorted slist %d vs %d, %p vs %p, "
97 "%s vs %s\n", last->owner, state->owner,
98 last->sym, state->sym, last->name, state->name);
99 printed = 1;
101 last = state;
102 } END_FOR_EACH_PTR(state);
104 if (printed)
105 printf("======\n");
107 #endif
109 struct state_list *clone_slist(struct state_list *from_slist)
111 struct sm_state *state;
112 struct sm_state *tmp;
113 struct state_list *to_slist = NULL;
115 FOR_EACH_PTR(from_slist, state) {
116 tmp = clone_state(state);
117 add_ptr_list(&to_slist, tmp);
118 } END_FOR_EACH_PTR(state);
119 #ifdef CHECKORDER
120 check_order(to_slist);
121 #endif
122 return to_slist;
125 // FIXME... shouldn't we free some of these state pointers?
126 struct smatch_state *merge_states(const char *name, int owner,
127 struct symbol *sym,
128 struct smatch_state *state1,
129 struct smatch_state *state2)
131 struct smatch_state *ret;
133 if (state1 == state2)
134 ret = state1;
135 else if (__has_merge_function(owner))
136 ret = __client_merge_function(owner, name, sym, state1, state2);
137 else
138 ret = &undefined;
140 SM_DEBUG("%d merge name='%s' owner=%d: %s + %s => %s\n",
141 get_lineno(), name, owner, show_state(state1),
142 show_state(state2), show_state(ret));
144 return ret;
147 struct smatch_state *get_state_slist(struct state_list *slist, const char *name, int owner,
148 struct symbol *sym)
150 struct sm_state *state;
152 if (!name)
153 return NULL;
155 FOR_EACH_PTR(slist, state) {
156 if (state->owner == owner && state->sym == sym
157 && !strcmp(state->name, name))
158 return state->state;
159 } END_FOR_EACH_PTR(state);
160 return NULL;
163 void set_state_slist(struct state_list **slist, const char *name, int owner,
164 struct symbol *sym, struct smatch_state *state)
166 struct sm_state *tmp;
167 struct sm_state *new = alloc_state(name, owner, sym, state);
169 FOR_EACH_PTR(*slist, tmp) {
170 if (cmp_sm_states(tmp, new) < 0)
171 continue;
172 else if (cmp_sm_states(tmp, new) == 0) {
173 __free_sm_state(new);
174 tmp->state = state;
175 return;
176 } else {
177 INSERT_CURRENT(new, tmp);
178 return;
180 } END_FOR_EACH_PTR(tmp);
181 add_ptr_list(slist, new);
184 void delete_state_slist(struct state_list **slist, const char *name, int owner,
185 struct symbol *sym)
187 struct sm_state *state;
189 FOR_EACH_PTR(*slist, state) {
190 if (state->owner == owner && state->sym == sym
191 && !strcmp(state->name, name)){
192 delete_ptr_list_entry((struct ptr_list **)slist,
193 state, 1);
194 __free_sm_state(state);
195 return;
197 } END_FOR_EACH_PTR(state);
201 void push_slist(struct state_list_stack **list_stack, struct state_list *slist)
203 add_ptr_list(list_stack, slist);
206 struct state_list *pop_slist(struct state_list_stack **list_stack)
208 struct state_list *slist;
210 slist = last_ptr_list((struct ptr_list *)*list_stack);
211 delete_ptr_list_last((struct ptr_list **)list_stack);
212 return slist;
215 void del_slist(struct state_list **slist)
217 __free_ptr_list((struct ptr_list **)slist);
220 void del_slist_stack(struct state_list_stack **slist_stack)
222 struct state_list *slist;
224 FOR_EACH_PTR(*slist_stack, slist) {
225 __free_ptr_list((struct ptr_list **)&slist);
226 } END_FOR_EACH_PTR(slist);
227 __free_ptr_list((struct ptr_list **)slist_stack);
231 * set_state_stack() sets the state for the top slist on the stack.
233 void set_state_stack(struct state_list_stack **stack, const char *name,
234 int owner, struct symbol *sym, struct smatch_state *state)
236 struct state_list *slist;
238 slist = pop_slist(stack);
239 set_state_slist(&slist, name, owner, sym, state);
240 push_slist(stack, slist);
244 * get_state_stack() gets the state for the top slist on the stack.
246 struct smatch_state *get_state_stack(struct state_list_stack *stack,
247 const char *name, int owner,
248 struct symbol *sym)
250 struct state_list *slist;
251 struct smatch_state *ret;
253 slist = pop_slist(&stack);
254 ret = get_state_slist(slist, name, owner, sym);
255 push_slist(&stack, slist);
256 return ret;
259 void merge_slist(struct state_list **to, struct state_list *slist)
261 struct sm_state *to_state, *state, *tmp;
262 struct state_list **results;
263 struct smatch_state *s;
265 #ifdef CHECKORDER
266 check_order(*to);
267 check_order(slist);
268 #endif
270 /* merging a null and nonnull path gives you only the nonnull path */
271 if (!slist) {
272 return;
274 if (!*to) {
275 *to = clone_slist(slist);
276 return;
279 results = malloc(sizeof(*results));
280 *results = NULL;
282 PREPARE_PTR_LIST(*to, to_state);
283 PREPARE_PTR_LIST(slist, state);
284 for (;;) {
285 if (!to_state && !state)
286 break;
287 if (cmp_sm_states(to_state, state) < 0) {
288 s = merge_states(to_state->name, to_state->owner,
289 to_state->sym, to_state->state, NULL);
290 tmp = alloc_state(to_state->name, to_state->owner,
291 to_state->sym, s);
292 add_ptr_list(results, tmp);
293 NEXT_PTR_LIST(to_state);
294 } else if (cmp_sm_states(to_state, state) == 0) {
295 s = merge_states(to_state->name, to_state->owner,
296 to_state->sym, to_state->state,
297 state->state);
298 tmp = alloc_state(to_state->name, to_state->owner,
299 to_state->sym, s);
300 add_ptr_list(results, tmp);
301 NEXT_PTR_LIST(to_state);
302 NEXT_PTR_LIST(state);
303 } else {
304 s = merge_states(state->name, state->owner,
305 state->sym, state->state, NULL);
306 tmp = alloc_state(state->name, state->owner,
307 state->sym, s);
308 add_ptr_list(results, tmp);
309 NEXT_PTR_LIST(state);
312 FINISH_PTR_LIST(state);
313 FINISH_PTR_LIST(to_state);
315 del_slist(to);
316 *to = *results;
319 void filter(struct state_list **slist, struct state_list *filter)
321 struct sm_state *s_one, *s_two;
322 struct state_list **results;
324 #ifdef CHECKORDER
325 check_order(*slist);
326 check_order(filter);
327 #endif
329 results = malloc(sizeof(*results));
330 *results = NULL;
332 PREPARE_PTR_LIST(*slist, s_one);
333 PREPARE_PTR_LIST(filter, s_two);
334 for (;;) {
335 if (!s_one || !s_two)
336 break;
337 if (cmp_sm_states(s_one, s_two) < 0) {
338 SM_DEBUG("different missing in new %s\n", s_one->name);
339 NEXT_PTR_LIST(s_one);
340 } else if (cmp_sm_states(s_one, s_two) == 0) {
341 if (s_one->state == s_two->state) {
342 add_ptr_list(results, s_one);
343 SM_DEBUG("same %s\n", s_one->name);
344 } else
345 SM_DEBUG("different %s %s vs %s\n", s_one->name,
346 show_state(s_one->state),
347 show_state(s_two->state));
348 NEXT_PTR_LIST(s_one);
349 NEXT_PTR_LIST(s_two);
350 } else {
351 SM_DEBUG("different missing in old%s\n", s_two->name);
352 NEXT_PTR_LIST(s_two);
355 FINISH_PTR_LIST(s_two);
356 FINISH_PTR_LIST(s_one);
358 del_slist(slist);
359 *slist = *results;
363 * This function is basically the same as popping the top two slists,
364 * overwriting the one with the other and pushing it back on the stack.
365 * The difference is that it checks to see that a mutually exclusive
366 * state isn't included in both stacks.
369 void and_slist_stack(struct state_list_stack **slist_stack)
371 struct sm_state *tmp;
372 struct smatch_state *tmp_state;
373 struct state_list *tmp_slist = pop_slist(slist_stack);
375 FOR_EACH_PTR(tmp_slist, tmp) {
376 tmp_state = get_state_stack(*slist_stack, tmp->name,
377 tmp->owner, tmp->sym);
378 if (tmp_state && tmp_state != tmp->state) {
379 smatch_msg("mutually exclusive 'and' conditions states "
380 "'%s': %s & %s.\n",
381 tmp->name, show_state(tmp_state),
382 show_state(tmp->state));
383 tmp->state = merge_states(tmp->name, tmp->owner,
384 tmp->sym, tmp->state,
385 tmp_state);
387 set_state_stack(slist_stack, tmp->name, tmp->owner, tmp->sym,
388 tmp->state);
390 } END_FOR_EACH_PTR(tmp);
391 del_slist(&tmp_slist);
394 void or_slist_stack(struct state_list_stack **slist_stack)
396 struct state_list *one;
397 struct state_list *two;
398 struct state_list *res = NULL;
399 struct sm_state *tmp;
400 struct smatch_state *s;
402 one = pop_slist(slist_stack);
403 two = pop_slist(slist_stack);
405 FOR_EACH_PTR(one, tmp) {
406 s = get_state_slist(two, tmp->name, tmp->owner, tmp->sym);
407 s = merge_states(tmp->name, tmp->owner, tmp->sym,
408 tmp->state, s);
409 set_state_slist(&res, tmp->name, tmp->owner, tmp->sym, s);
410 } END_FOR_EACH_PTR(tmp);
413 FOR_EACH_PTR(two, tmp) {
414 s = get_state_slist(one, tmp->name, tmp->owner, tmp->sym);
415 s = merge_states(tmp->name, tmp->owner, tmp->sym,
416 tmp->state, s);
417 set_state_slist(&res, tmp->name, tmp->owner, tmp->sym, s);
418 } END_FOR_EACH_PTR(tmp);
420 push_slist(slist_stack, res);
422 del_slist(&one);
423 del_slist(&two);
426 struct state_list **get_slist_from_slist_stack(struct slist_stack *stack,
427 const char *name)
429 struct named_slist *tmp;
431 FOR_EACH_PTR(stack, tmp) {
432 if (!strcmp(tmp->name, name))
433 return &tmp->slist;
434 } END_FOR_EACH_PTR(tmp);
435 return NULL;
438 void overwrite_slist(struct state_list *from, struct state_list **to)
440 struct sm_state *tmp;
442 FOR_EACH_PTR(from, tmp) {
443 set_state_slist(to, tmp->name, tmp->owner, tmp->sym, tmp->state);
444 } END_FOR_EACH_PTR(tmp);