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.
23 #include "smatch_slist.h"
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
)
39 if (in_tracker_list(starts_left
, my_id
, sm
->name
, sm
->sym
))
41 if (in_tracker_list(starts_right
, my_id
, sm
->name
, sm
->sym
))
43 if (is_left
&& is_right
)
52 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
57 static void match_left(const char *fn
, struct expression
*expr
, void *data
)
60 char *name
= (char *)data
;
65 sm
= get_sm_state(my_id
, name
, NULL
);
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
)
76 char *name
= (char *)data
;
81 sm
= get_sm_state(my_id
, name
, NULL
);
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
)
96 FOR_EACH_PTR(sm
->possible
, tmp
) {
97 if (tmp
->state
== &left
)
99 if (tmp
->state
== &right
)
101 if (tmp
->state
== &start_state
) {
102 struct smatch_state
*s
;
104 s
= get_start_state(tmp
);
107 else if (s
== &right
)
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
)
122 struct sm_state
*tmp
;
127 stree
= __get_cur_stree();
128 FOR_EACH_MY_SM(my_id
, stree
, tmp
) {
129 if (tmp
->state
== &merged
)
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
)
148 static void match_after_func(struct symbol
*sym
)
153 static void get_left_funcs(const char *name
, struct token
**token
)
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
)
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)
184 token
= get_tokens_file("kernel.balanced_funcs");
187 if (token_type(token
) != TOKEN_STREAMBEGIN
)
190 while (token_type(token
) != TOKEN_STREAMEND
) {
191 if (token_type(token
) != TOKEN_IDENT
)
193 name
= alloc_string(show_ident(token
->ident
));
195 get_left_funcs(name
, &token
);
196 get_right_funcs(name
, &token
);
201 void check_balanced(int 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();