1 /*
2  * Copyright (C) 2010 Dan Carpenter.
3  *
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.
8  *
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.
13  *
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
16  */
17 
18 #include "smatch.h"
19 #include "smatch_extra.h"
20 #include "smatch_slist.h"
21 
22 static int my_id;
23 
24 #define MAX_ERRNO 4095
25 
26 STATE(added);
27 STATE(not_added);
28 
match_added(const char * fn,struct expression * call_expr,struct expression * assign_expr,void * unused)29 static void match_added(const char *fn, struct expression *call_expr,
30 			struct expression *assign_expr, void *unused)
31 {
32 	struct expression *arg_expr;
33 
34 	arg_expr = get_argument_from_call_expr(call_expr->args, 0);
35 	set_state_expr(my_id, arg_expr, &added);
36 }
37 
match_not_added(const char * fn,struct expression * call_expr,struct expression * assign_expr,void * unused)38 static void match_not_added(const char *fn, struct expression *call_expr,
39 			struct expression *assign_expr, void *unused)
40 {
41 	struct expression *arg_expr;
42 
43 	arg_expr = get_argument_from_call_expr(call_expr->args, 0);
44 	set_state_expr(my_id, arg_expr, &not_added);
45 }
46 
match_platform_device_del(const char * fn,struct expression * expr,void * unused)47 static void match_platform_device_del(const char *fn, struct expression *expr, void *unused)
48 {
49 	struct expression *arg_expr;
50 	struct sm_state *sm;
51 
52 	arg_expr = get_argument_from_call_expr(expr->args, 0);
53 	sm = get_sm_state_expr(my_id, arg_expr);
54 	if (!sm)
55 		return;
56 	if (!slist_has_state(sm->possible, &not_added))
57 		return;
58 	sm_warning("perhaps platform_device_put() was intended here?");
59 }
60 
check_platform_device_put(int id)61 void check_platform_device_put(int id)
62 {
63 	if (option_project != PROJ_KERNEL)
64 		return;
65 	my_id = id;
66 
67 	return_implies_state("platform_device_add", 0, 0, &match_added, NULL);
68 	return_implies_state("platform_device_add", -MAX_ERRNO, -1, &match_not_added, NULL);
69 	add_function_hook("platform_device_del", &match_platform_device_del, NULL);
70 }
71