Add single quotes around the variable for consistency and grepability.
[smatch.git] / smatch_states.c
blobbbc94538481bfe596f9d78a60bcd4b8dcb97521d
1 /*
2 * sparse/smatch_states.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * You have a lists of states. kernel = locked, foo = NULL, ...
12 * When you hit an if {} else {} statement then you swap the list
13 * of states for a different list of states. The lists are stored
14 * on stacks.
16 * At the beginning of this file there are list of the stacks that
17 * we use. Each function in this file does something to one of
18 * of the stacks.
20 * So the smatch_flow.c understands code but it doesn't understand states.
21 * smatch_flow calls functions in this file. This file calls functions
22 * in smatch_slist.c which just has boring generic plumbing for handling
23 * state lists. But really it's this file where all the magic happens.
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include "smatch.h"
30 #include "smatch_slist.h"
32 struct smatch_state undefined = { .name = "undefined" };
33 struct smatch_state true_state = { .name = "true" };
34 struct smatch_state false_state = { .name = "false" };
36 static struct state_list *cur_slist; /* current states */
38 static struct state_list_stack *true_stack; /* states after a t/f branch */
39 static struct state_list_stack *false_stack;
40 static struct state_list_stack *pre_cond_stack; /* states before a t/f branch */
42 static struct state_list_stack *cond_true_stack; /* states affected by a branch */
43 static struct state_list_stack *cond_false_stack;
45 static struct state_list_stack *break_stack;
46 static struct state_list_stack *switch_stack;
47 static struct state_list_stack *default_stack;
48 static struct state_list_stack *continue_stack;
49 static struct state_list_stack *false_only_stack;
51 static struct slist_stack *goto_stack;
53 struct state_list_stack *implied_pools;
55 int debug_states;
57 void __print_slist(struct state_list *slist)
59 struct sm_state *state;
61 printf("dumping slist at %d\n", get_lineno());
62 FOR_EACH_PTR(slist, state) {
63 printf("'%s'=%s\n", state->name, show_state(state->state));
64 } END_FOR_EACH_PTR(state);
65 printf("---\n");
68 void __print_cur_slist()
70 __print_slist(cur_slist);
73 void set_state(const char *name, int owner, struct symbol *sym,
74 struct smatch_state *state)
76 if (!name)
77 return;
79 if (debug_states) {
80 struct smatch_state *s;
82 s = get_state(name, owner, sym);
83 if (!s)
84 printf("%d new state. name='%s' owner=%d: %s\n",
85 get_lineno(), name, owner, show_state(state));
86 else
87 printf("%d state change name='%s' owner=%d: %s => %s\n",
88 get_lineno(), name, owner, show_state(s),
89 show_state(state));
91 set_state_slist(&cur_slist, name, owner, sym, state);
93 if (cond_true_stack) {
94 set_state_stack(&cond_true_stack, name, owner, sym, state);
95 set_state_stack(&cond_false_stack, name, owner, sym, state);
99 struct smatch_state *get_state(const char *name, int owner, struct symbol *sym)
101 return get_state_slist(cur_slist, name, owner, sym);
104 struct sm_state *__get_sm_state(const char *name, int owner, struct symbol *sym)
106 return get_sm_state_slist(cur_slist, name, owner, sym);
109 void delete_state(const char *name, int owner, struct symbol *sym)
111 delete_state_slist(&cur_slist, name, owner, sym);
114 struct state_list *get_all_states(int owner)
116 struct state_list *slist = NULL;
117 struct sm_state *tmp;
119 FOR_EACH_PTR(cur_slist, tmp) {
120 if (tmp->owner == owner) {
121 add_ptr_list(&slist, tmp);
123 } END_FOR_EACH_PTR(tmp);
125 return slist;
128 void set_true_false_states(const char *name, int owner, struct symbol *sym,
129 struct smatch_state *true_state,
130 struct smatch_state *false_state)
132 /* fixme. save history */
134 if (debug_states) {
135 struct smatch_state *tmp;
137 tmp = get_state(name, owner, sym);
138 SM_DEBUG("%d set_true_false '%s'. Was %s. Now T:%s F:%s\n",
139 get_lineno(), name, show_state(tmp),
140 show_state(true_state), show_state(false_state));
143 if (!cond_false_stack || !cond_true_stack) {
144 printf("Error: missing true/false stacks\n");
145 return;
148 if (true_state) {
149 set_state_slist(&cur_slist, name, owner, sym, true_state);
150 set_state_stack(&cond_true_stack, name, owner, sym, true_state);
152 if (false_state)
153 set_state_stack(&cond_false_stack, name, owner, sym, false_state);
156 void nullify_path()
158 del_slist(&cur_slist);
162 * At the start of every function we mark the path
163 * as unnull. That there is always at least one state
164 * in the cur_slist until nullify_path is called. This
165 * is used in merge_slist() for the first null check.
168 void __unnullify_path()
170 set_state("unnull_path", 0, NULL, &true_state);
173 void clear_all_states()
175 struct named_slist *named_slist;
177 nullify_path();
178 del_slist_stack(&true_stack);
179 del_slist_stack(&false_stack);
180 del_slist_stack(&false_only_stack);
181 del_slist_stack(&pre_cond_stack);
182 del_slist_stack(&cond_true_stack);
183 del_slist_stack(&cond_false_stack);
184 del_slist_stack(&break_stack);
185 del_slist_stack(&switch_stack);
186 del_slist_stack(&continue_stack);
187 del_slist_stack(&implied_pools);
189 FOR_EACH_PTR(goto_stack, named_slist) {
190 del_slist(&named_slist->slist);
191 } END_FOR_EACH_PTR(named_slist);
192 __free_ptr_list((struct ptr_list **)&goto_stack);
196 void __push_cond_stacks()
198 push_slist(&cond_true_stack, NULL);
199 push_slist(&cond_false_stack, NULL);
203 * This combines the pre cond states with either the true or false states.
204 * For example:
205 * a = kmalloc() ; if (a !! foo(a)
206 * In the pre state a is possibly null. In the true state it is non null.
207 * In the false state it is null. Combine the pre and the false to get
208 * that when we call 'foo', 'a' is null.
211 static void __use_cond_stack(struct state_list_stack **stack)
213 struct state_list *slist;
215 del_slist(&cur_slist);
217 cur_slist = pop_slist(&pre_cond_stack);
218 push_slist(&pre_cond_stack, clone_slist(cur_slist));
220 slist = pop_slist(stack);
221 overwrite_slist(slist, &cur_slist);
222 push_slist(stack, slist);
226 void __use_cond_true_states()
228 __use_cond_stack(&cond_true_stack);
231 void __use_cond_false_states()
233 __use_cond_stack(&cond_false_stack);
236 void __negate_cond_stacks()
238 struct state_list *old_false, *old_true;
240 old_false = pop_slist(&cond_false_stack);
241 old_true = pop_slist(&cond_true_stack);
243 overwrite_slist(old_false, &cur_slist);
245 push_slist(&cond_false_stack, old_true);
246 push_slist(&cond_true_stack, old_false);
249 void __and_cond_states()
251 and_slist_stack(&cond_true_stack);
252 or_slist_stack(&cond_false_stack);
255 void __or_cond_states()
257 or_slist_stack(&cond_true_stack);
258 and_slist_stack(&cond_false_stack);
261 void __save_pre_cond_states()
263 push_slist(&pre_cond_stack, clone_slist(cur_slist));
266 void __pop_pre_cond_states()
268 struct state_list *tmp;
270 tmp = pop_slist(&pre_cond_stack);
271 del_slist(&tmp);
274 void __use_false_only_stack()
276 struct state_list *slist;
278 slist = pop_slist(&false_only_stack);
279 overwrite_slist(slist, &cur_slist);
280 del_slist(&slist);
283 void __pop_false_only_stack()
285 struct state_list *slist;
287 slist = pop_slist(&false_only_stack);
288 del_slist(&slist);
291 void __use_cond_states()
293 struct state_list *pre, *pre_clone, *true_states, *false_states;
295 pre = pop_slist(&pre_cond_stack);
296 pre_clone = clone_slist(pre);
298 true_states = pop_slist(&cond_true_stack);
299 overwrite_slist(true_states, &pre);
300 /* we use the true states right away */
301 del_slist(&cur_slist);
302 cur_slist = pre;
304 false_states = pop_slist(&cond_false_stack);
305 push_slist(&false_only_stack, clone_slist(false_states));
306 overwrite_slist(false_states, &pre_clone);
307 push_slist(&false_stack, pre_clone);
310 void __push_true_states()
312 push_slist(&true_stack, clone_slist(cur_slist));
315 void __use_false_states()
317 del_slist(&cur_slist);
318 cur_slist = pop_slist(&false_stack);
321 void __pop_false_states()
323 struct state_list *slist;
325 slist = pop_slist(&false_stack);
326 del_slist(&slist);
329 void __merge_false_states()
331 struct state_list *slist;
333 slist = pop_slist(&false_stack);
334 merge_slist(&cur_slist, slist);
335 del_slist(&slist);
338 void __merge_true_states()
340 struct state_list *slist;
342 slist = pop_slist(&true_stack);
343 merge_slist(&cur_slist, slist);
344 del_slist(&slist);
347 void __push_continues()
349 push_slist(&continue_stack, NULL);
352 void __pop_continues()
354 struct state_list *slist;
356 slist = pop_slist(&continue_stack);
357 del_slist(&slist);
360 void __process_continues()
362 struct state_list *slist;
364 slist = pop_slist(&continue_stack);
365 if (!slist) {
366 overwrite_slist(cur_slist, &slist);
367 } else {
368 merge_slist(&slist, cur_slist);
370 push_slist(&continue_stack, slist);
373 void __merge_continues()
375 struct state_list *slist;
377 slist = pop_slist(&continue_stack);
378 merge_slist(&cur_slist, slist);
379 del_slist(&slist);
382 void __push_breaks()
384 push_slist(&break_stack, NULL);
387 void __process_breaks()
389 struct state_list *slist;
391 slist = pop_slist(&break_stack);
392 if (!slist) {
393 overwrite_slist(cur_slist, &slist);
394 } else {
395 merge_slist(&slist, cur_slist);
397 push_slist(&break_stack, slist);
400 void __merge_breaks()
402 struct state_list *slist;
404 slist = pop_slist(&break_stack);
405 merge_slist(&cur_slist, slist);
406 del_slist(&slist);
409 void __use_breaks()
411 del_slist(&cur_slist);
412 cur_slist = pop_slist(&break_stack);
415 void __save_switch_states()
417 push_slist(&switch_stack, clone_slist(cur_slist));
420 void __merge_switches()
422 struct state_list *slist;
424 slist = pop_slist(&switch_stack);
425 merge_slist(&cur_slist, slist);
426 push_slist(&switch_stack, slist);
429 void __pop_switches()
431 struct state_list *slist;
433 slist = pop_slist(&switch_stack);
434 del_slist(&slist);
437 void __push_default()
439 push_slist(&default_stack, NULL);
442 void __set_default()
444 set_state_stack(&default_stack, "has_default", 0, NULL, &true_state);
447 int __pop_default()
449 struct state_list *slist;
451 slist = pop_slist(&default_stack);
452 if (slist) {
453 del_slist(&slist);
454 return 1;
456 return 0;
459 static struct named_slist *alloc_named_slist(const char *name,
460 struct state_list *slist)
462 struct named_slist *named_slist = __alloc_named_slist(0);
464 named_slist->name = (char *)name;
465 named_slist->slist = slist;
466 return named_slist;
469 void __save_gotos(const char *name)
471 struct state_list **slist;
473 slist = get_slist_from_slist_stack(goto_stack, name);
474 if (slist) {
475 merge_slist(slist, cur_slist);
476 return;
477 } else {
478 struct state_list *slist;
479 struct named_slist *named_slist;
481 slist = clone_slist(cur_slist);
482 named_slist = alloc_named_slist(name, slist);
483 add_ptr_list(&goto_stack, named_slist);
487 void __merge_gotos(const char *name)
489 struct state_list **slist;
491 slist = get_slist_from_slist_stack(goto_stack, name);
492 if (slist)
493 merge_slist(&cur_slist, *slist);