struct_assignment: ignore certain types of assignments
[smatch.git] / check_balanced.c
blob913d5948c0f9ec97b4c0e1b2bb76b024e6ad2788
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 stree *stree;
122 struct sm_state *tmp;
124 if (__inline_fn)
125 return;
127 stree = __get_cur_stree();
128 FOR_EACH_MY_SM(my_id, stree, tmp) {
129 if (tmp->state == &merged)
130 check_possible(tmp);
131 } END_FOR_EACH_SM(tmp);
134 static void clear_lists(void)
136 free_trackers_and_list(&starts_left);
137 free_trackers_and_list(&starts_right);
140 static void match_func_end(struct symbol *sym)
142 if (__inline_fn)
143 return;
144 if (is_reachable())
145 match_return(NULL);
148 static void match_after_func(struct symbol *sym)
150 clear_lists();
153 static void get_left_funcs(const char *name, struct token **token)
155 const char *func;
157 while (token_type(*token) == TOKEN_IDENT) {
158 func = show_ident((*token)->ident);
159 add_function_hook(func, &match_left, (char *)name);
160 *token = (*token)->next;
162 if (token_type(*token) == TOKEN_SPECIAL)
163 *token = (*token)->next;
166 static void get_right_funcs(const char *name, struct token **token)
168 const char *func;
170 while (token_type(*token) == TOKEN_IDENT) {
171 func = show_ident((*token)->ident);
172 add_function_hook(func, &match_right, (char *)name);
173 *token = (*token)->next;
175 if (token_type(*token) == TOKEN_SPECIAL)
176 *token = (*token)->next;
179 static void register_funcs_from_file(void)
181 struct token *token;
182 char *name;
184 token = get_tokens_file("kernel.balanced_funcs");
185 if (!token)
186 return;
187 if (token_type(token) != TOKEN_STREAMBEGIN)
188 return;
189 token = token->next;
190 while (token_type(token) != TOKEN_STREAMEND) {
191 if (token_type(token) != TOKEN_IDENT)
192 return;
193 name = alloc_string(show_ident(token->ident));
194 token = token->next;
195 get_left_funcs(name, &token);
196 get_right_funcs(name, &token);
198 clear_token_alloc();
201 void check_balanced(int id)
203 my_id = id;
204 add_unmatched_state_hook(my_id, &unmatched_state);
205 add_hook(&match_return, RETURN_HOOK);
206 add_hook(&match_func_end, END_FUNC_HOOK);
207 add_hook(&match_after_func, AFTER_FUNC_HOOK);
208 register_funcs_from_file();