1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon #include "smatch_extra.h"
20*1f5207b7SJohn Levon #include "smatch_slist.h"
21*1f5207b7SJohn Levon 
22*1f5207b7SJohn Levon static int my_id;
23*1f5207b7SJohn Levon 
24*1f5207b7SJohn Levon #define MAX_ERRNO 4095
25*1f5207b7SJohn Levon 
26*1f5207b7SJohn Levon STATE(added);
27*1f5207b7SJohn Levon STATE(not_added);
28*1f5207b7SJohn Levon 
match_added(const char * fn,struct expression * call_expr,struct expression * assign_expr,void * unused)29*1f5207b7SJohn Levon static void match_added(const char *fn, struct expression *call_expr,
30*1f5207b7SJohn Levon 			struct expression *assign_expr, void *unused)
31*1f5207b7SJohn Levon {
32*1f5207b7SJohn Levon 	struct expression *arg_expr;
33*1f5207b7SJohn Levon 
34*1f5207b7SJohn Levon 	arg_expr = get_argument_from_call_expr(call_expr->args, 0);
35*1f5207b7SJohn Levon 	set_state_expr(my_id, arg_expr, &added);
36*1f5207b7SJohn Levon }
37*1f5207b7SJohn Levon 
match_not_added(const char * fn,struct expression * call_expr,struct expression * assign_expr,void * unused)38*1f5207b7SJohn Levon static void match_not_added(const char *fn, struct expression *call_expr,
39*1f5207b7SJohn Levon 			struct expression *assign_expr, void *unused)
40*1f5207b7SJohn Levon {
41*1f5207b7SJohn Levon 	struct expression *arg_expr;
42*1f5207b7SJohn Levon 
43*1f5207b7SJohn Levon 	arg_expr = get_argument_from_call_expr(call_expr->args, 0);
44*1f5207b7SJohn Levon 	set_state_expr(my_id, arg_expr, &not_added);
45*1f5207b7SJohn Levon }
46*1f5207b7SJohn Levon 
match_platform_device_del(const char * fn,struct expression * expr,void * unused)47*1f5207b7SJohn Levon static void match_platform_device_del(const char *fn, struct expression *expr, void *unused)
48*1f5207b7SJohn Levon {
49*1f5207b7SJohn Levon 	struct expression *arg_expr;
50*1f5207b7SJohn Levon 	struct sm_state *sm;
51*1f5207b7SJohn Levon 
52*1f5207b7SJohn Levon 	arg_expr = get_argument_from_call_expr(expr->args, 0);
53*1f5207b7SJohn Levon 	sm = get_sm_state_expr(my_id, arg_expr);
54*1f5207b7SJohn Levon 	if (!sm)
55*1f5207b7SJohn Levon 		return;
56*1f5207b7SJohn Levon 	if (!slist_has_state(sm->possible, &not_added))
57*1f5207b7SJohn Levon 		return;
58*1f5207b7SJohn Levon 	sm_warning("perhaps platform_device_put() was intended here?");
59*1f5207b7SJohn Levon }
60*1f5207b7SJohn Levon 
check_platform_device_put(int id)61*1f5207b7SJohn Levon void check_platform_device_put(int id)
62*1f5207b7SJohn Levon {
63*1f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
64*1f5207b7SJohn Levon 		return;
65*1f5207b7SJohn Levon 	my_id = id;
66*1f5207b7SJohn Levon 
67*1f5207b7SJohn Levon 	return_implies_state("platform_device_add", 0, 0, &match_added, NULL);
68*1f5207b7SJohn Levon 	return_implies_state("platform_device_add", -MAX_ERRNO, -1, &match_not_added, NULL);
69*1f5207b7SJohn Levon 	add_function_hook("platform_device_del", &match_platform_device_del, NULL);
70*1f5207b7SJohn Levon }
71