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_all_states_stree(my_id
);
128 FOR_EACH_SM(stree
, tmp
) {
129 if (tmp
->state
== &merged
)
131 } END_FOR_EACH_SM(tmp
);
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
)
150 static void get_left_funcs(const char *name
, struct token
**token
)
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
)
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)
181 token
= get_tokens_file("kernel.balanced_funcs");
184 if (token_type(token
) != TOKEN_STREAMBEGIN
)
187 while (token_type(token
) != TOKEN_STREAMEND
) {
188 if (token_type(token
) != TOKEN_IDENT
)
190 name
= alloc_string(show_ident(token
->ident
));
192 get_left_funcs(name
, &token
);
193 get_right_funcs(name
, &token
);
198 void check_balanced(int 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();