db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / smatch_kernel_atomic_dec_test_path.c
blobd3c05f9e59bd02255a919e278823d50f1a1e2781
1 /*
2 * Copyright (C) 2016 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
18 #include "smatch.h"
19 #include "smatch_extra.h"
20 #include "smatch_slist.h"
22 static int my_id;
24 STATE(zero_path);
26 static void handle_test_functions(struct expression *expr)
28 struct expression *tmp;
29 struct statement *stmt;
30 int count = 0;
32 while (expr && expr->type == EXPR_ASSIGNMENT && expr->op == '=')
33 expr = strip_expr(expr->right);
35 if (!expr ||
36 expr->type != EXPR_CALL ||
37 expr->fn->type != EXPR_SYMBOL ||
38 !expr->fn->symbol_name)
39 return;
40 if (!strstr(expr->fn->symbol_name->name, "test"))
41 return;
43 while ((tmp = expr_get_parent_expr(expr))) {
44 expr = tmp;
45 if (count++ > 5)
46 break;
50 * FIXME: This doesn't work for assignments like:
52 * free = dec_and_test(foo);
53 * if (free)
54 * free_thing(x);
56 * Maybe we should just say everything is on an atomic_dec path at this
57 * point. That probably works fine. The other solution would be to
58 * store the variable and look up the state of it or something...
62 stmt = expr_get_parent_stmt(expr);
63 if (!stmt || stmt->type != STMT_IF) {
64 /* See above comment. I decided to go with the easy option */
65 set_state(my_id, "dec_path", NULL, &zero_path);
66 return;
69 set_true_false_states(my_id, "dec_path", NULL, &zero_path, NULL);
72 static void match_dec(struct expression *expr, const char *name, struct symbol *sym)
74 handle_test_functions(expr);
77 int on_atomic_dec_path(void)
79 return get_state(my_id, "dec_path", NULL) == &zero_path;
82 void register_kernel_atomic_dec_test_path(int id)
84 my_id = id;
86 if (option_project != PROJ_KERNEL)
87 return;
89 add_refcount_dec_hook(&match_dec);