2 * Copyright (C) 2014 Oracle.
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
21 #include "smatch_slist.h"
28 struct stree
*to_check
;
30 static struct statement
*get_cur_stmt(void)
32 return last_ptr_list((struct ptr_list
*)big_statement_stack
);
35 static void set_modified(struct sm_state
*sm
, struct expression
*mod_expr
)
37 set_state(my_id
, sm
->name
, sm
->sym
, &modified
);
40 static struct expression
*strip_condition(struct expression
*expr
)
42 expr
= strip_expr(expr
);
44 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!')
45 return strip_condition(expr
->unop
);
47 if (expr
->type
== EXPR_COMPARE
&&
48 (expr
->op
== SPECIAL_EQUAL
||
49 expr
->op
== SPECIAL_NOTEQUAL
)) {
50 if (is_zero(expr
->left
))
51 return strip_condition(expr
->right
);
52 if (is_zero(expr
->right
))
53 return strip_condition(expr
->left
);
59 static int conditions_match(struct expression
*cond
, struct expression
*prev
)
61 prev
= strip_condition(prev
);
66 if (prev
->type
== EXPR_LOGICAL
) {
67 if (conditions_match(cond
, prev
->left
) ||
68 conditions_match(cond
, prev
->right
))
76 * People like to do "if (foo) { ... } else if (!foo) { ... }". Don't
77 * complain when they do that even though it is nonsense.
79 static int is_obvious_else(struct expression
*cond
)
81 struct statement
*parent
;
82 struct expression
*prev
;
86 parent
= get_cur_stmt()->parent
;
90 if (parent
->type
!= STMT_IF
)
93 if (!parent
->if_false
)
95 if (parent
->if_false
!= get_cur_stmt())
98 prev
= strip_condition(parent
->if_conditional
);
100 return conditions_match(cond
, prev
);
103 static int name_means_synchronize(const char *name
)
108 if (strcasestr(name
, "wait"))
110 if (strcasestr(name
, "down"))
112 if (strcasestr(name
, "lock") && !strcasestr(name
, "unlock"))
114 if (strcasestr(name
, "delay"))
116 if (strcasestr(name
, "schedule"))
118 if (strcmp(name
, "smp_rmb") == 0)
120 if (strcmp(name
, "mb") == 0)
122 if (strcmp(name
, "barrier") == 0)
127 static int previous_statement_was_synchronize(void)
129 struct statement
*stmt
;
131 struct position prev_pos
;
135 prev_pos
= __prev_stmt
->pos
;
138 prev_pos
= __cur_stmt
->pos
;
142 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
143 if (stmt
->pos
.line
< prev_pos
.line
)
146 ident
= get_macro_name(pos
);
147 if (name_means_synchronize(ident
))
149 ident
= pos_ident(pos
);
152 if (strcmp(ident
, "if") == 0) {
154 ident
= pos_ident(pos
);
158 if (name_means_synchronize(ident
))
160 } END_FOR_EACH_PTR_REVERSE(stmt
);
164 static void match_condition(struct expression
*expr
)
166 struct smatch_state
*state
;
173 if (get_value(expr
, &dummy
))
176 if (get_macro_name(expr
->pos
))
179 state
= get_stored_condition(expr
);
180 if (!state
|| !state
->data
)
182 if (get_macro_name(((struct expression
*)state
->data
)->pos
))
186 * we allow double checking for NULL because people do this all the time
187 * and trying to stop them is a losers' battle.
189 if (is_pointer(expr
) && implied_condition_true(expr
))
192 if (definitely_inside_loop()) {
198 name
= expr_to_var_sym(expr
, &sym
);
201 set_state_expr(my_id
, expr
, &checked
);
202 set_state_stree(&to_check
, my_id
, name
, sym
, &checked
);
207 if (is_obvious_else(state
->data
))
211 * It's common to test something, then take a lock and test if it is
214 if (previous_statement_was_synchronize())
217 name
= expr_to_str(expr
);
218 sm_msg("warn: we tested '%s' before and it was '%s'", name
, state
->name
);
222 int get_check_line(struct sm_state
*sm
)
224 struct sm_state
*tmp
;
226 FOR_EACH_PTR(sm
->possible
, tmp
) {
227 if (tmp
->state
== &checked
)
229 } END_FOR_EACH_PTR(tmp
);
234 static void after_loop(struct statement
*stmt
)
236 struct sm_state
*check
, *sm
;
238 if (!stmt
|| stmt
->type
!= STMT_ITERATOR
)
240 if (definitely_inside_loop())
245 FOR_EACH_SM(to_check
, check
) {
247 sm
= get_sm_state(my_id
, check
->name
, check
->sym
);
251 if (slist_has_state(sm
->possible
, &modified
))
254 sm_printf("%s:%d %s() ", get_filename(), get_check_line(sm
), get_function());
255 sm_printf("warn: we tested '%s' already\n", check
->name
);
256 } END_FOR_EACH_SM(check
);
258 free_stree(&to_check
);
261 static void match_func_end(struct symbol
*sym
)
266 sm_msg("debug: odd... found an function without an end.");
267 free_stree(&to_check
);
270 void check_double_checking(int id
)
277 add_hook(&match_condition
, CONDITION_HOOK
);
278 add_modification_hook(my_id
, &set_modified
);
279 add_hook(after_loop
, STMT_HOOK_AFTER
);
280 add_hook(&match_func_end
, AFTER_FUNC_HOOK
);