avl: change some void pointers to struct sm_state pointers
[smatch.git] / check_balanced.c
blobdc52fe2d2e31c898d74b37c3d40ef308ea35b220
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This test checks that prempt is enabled at the end of every function.
22 #include "smatch.h"
23 #include "smatch_slist.h"
25 static int my_id;
27 STATE(left);
28 STATE(right);
29 STATE(start_state);
31 static struct tracker_list *starts_left;
32 static struct tracker_list *starts_right;
34 static struct smatch_state *get_start_state(struct sm_state *sm)
36 int is_left = 0;
37 int is_right = 0;
39 if (in_tracker_list(starts_left, my_id, sm->name, sm->sym))
40 is_left = 1;
41 if (in_tracker_list(starts_right, my_id, sm->name, sm->sym))
42 is_right = 1;
43 if (is_left && is_right)
44 return &undefined;
45 if (is_left)
46 return &left;
47 if (is_right)
48 return &right;
49 return &undefined;
52 static struct smatch_state *unmatched_state(struct sm_state *sm)
54 return &start_state;
57 static void match_left(const char *fn, struct expression *expr, void *data)
59 struct sm_state *sm;
60 char *name = (char *)data;
62 if (__inline_fn)
63 return;
65 sm = get_sm_state(my_id, name, NULL);
66 if (!sm)
67 add_tracker(&starts_right, my_id, name, NULL);
68 if (sm && slist_has_state(sm->possible, &left))
69 sm_msg("warn: double '%s'", fn);
70 set_state(my_id, (char *)data, NULL, &left);
73 static void match_right(const char *fn, struct expression *expr, void *data)
75 struct sm_state *sm;
76 char *name = (char *)data;
78 if (__inline_fn)
79 return;
81 sm = get_sm_state(my_id, name, NULL);
82 if (!sm)
83 add_tracker(&starts_left, my_id, name, NULL);
84 if (sm && slist_has_state(sm->possible, &right))
85 sm_msg("warn: double '%s'", fn);
86 set_state(my_id, (char *)data, NULL, &right);
89 static void check_possible(struct sm_state *sm)
91 struct sm_state *tmp;
92 int is_left = 0;
93 int is_right = 0;
94 int undef = 0;
96 FOR_EACH_PTR(sm->possible, tmp) {
97 if (tmp->state == &left)
98 is_left = 1;
99 if (tmp->state == &right)
100 is_right = 1;
101 if (tmp->state == &start_state) {
102 struct smatch_state *s;
104 s = get_start_state(tmp);
105 if (s == &left)
106 is_left = 1;
107 else if (s == &right)
108 is_right = 1;
109 else
110 undef = 1;
112 if (tmp->state == &undefined)
113 undef = 1; // i don't think this is possible any more.
114 } END_FOR_EACH_PTR(tmp);
115 if ((is_left && is_right) || undef)
116 sm_msg("warn: returning with unbalanced %s", sm->name);
119 static void match_return(struct expression *expr)
121 struct state_list *slist;
122 struct sm_state *tmp;
124 if (__inline_fn)
125 return;
127 slist = get_all_states(my_id);
128 FOR_EACH_PTR(slist, tmp) {
129 if (tmp->state == &merged)
130 check_possible(tmp);
131 } END_FOR_EACH_PTR(tmp);
132 free_slist(&slist);
135 static void clear_lists(void)
137 free_trackers_and_list(&starts_left);
138 free_trackers_and_list(&starts_right);
141 static void match_func_end(struct symbol *sym)
143 if (__inline_fn)
144 return;
145 if (is_reachable())
146 match_return(NULL);
147 clear_lists();
150 static void get_left_funcs(const char *name, struct token **token)
152 const char *func;
154 while (token_type(*token) == TOKEN_IDENT) {
155 func = show_ident((*token)->ident);
156 add_function_hook(func, &match_left, (char *)name);
157 *token = (*token)->next;
159 if (token_type(*token) == TOKEN_SPECIAL)
160 *token = (*token)->next;
163 static void get_right_funcs(const char *name, struct token **token)
165 const char *func;
167 while (token_type(*token) == TOKEN_IDENT) {
168 func = show_ident((*token)->ident);
169 add_function_hook(func, &match_right, (char *)name);
170 *token = (*token)->next;
172 if (token_type(*token) == TOKEN_SPECIAL)
173 *token = (*token)->next;
176 static void register_funcs_from_file(void)
178 struct token *token;
179 char *name;
181 token = get_tokens_file("kernel.balanced_funcs");
182 if (!token)
183 return;
184 if (token_type(token) != TOKEN_STREAMBEGIN)
185 return;
186 token = token->next;
187 while (token_type(token) != TOKEN_STREAMEND) {
188 if (token_type(token) != TOKEN_IDENT)
189 return;
190 name = alloc_string(show_ident(token->ident));
191 token = token->next;
192 get_left_funcs(name, &token);
193 get_right_funcs(name, &token);
195 clear_token_alloc();
198 void check_balanced(int id)
200 my_id = id;
201 add_unmatched_state_hook(my_id, &unmatched_state);
202 add_hook(&match_return, RETURN_HOOK);
203 add_hook(&match_func_end, END_FUNC_HOOK);
204 register_funcs_from_file();