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 
20*1f5207b7SJohn Levon static int my_id;
21*1f5207b7SJohn Levon static struct symbol *func_sym;
22*1f5207b7SJohn Levon 
23*1f5207b7SJohn Levon STATE(argument);
24*1f5207b7SJohn Levon STATE(ok);
25*1f5207b7SJohn Levon 
set_ok(struct sm_state * sm,struct expression * mod_expr)26*1f5207b7SJohn Levon static void set_ok(struct sm_state *sm, struct expression *mod_expr)
27*1f5207b7SJohn Levon {
28*1f5207b7SJohn Levon 	if (sm->state != &ok)
29*1f5207b7SJohn Levon 		set_state(my_id, sm->name, sm->sym, &ok);
30*1f5207b7SJohn Levon }
31*1f5207b7SJohn Levon 
match_function_def(struct symbol * sym)32*1f5207b7SJohn Levon static void match_function_def(struct symbol *sym)
33*1f5207b7SJohn Levon {
34*1f5207b7SJohn Levon 	struct symbol *arg;
35*1f5207b7SJohn Levon 
36*1f5207b7SJohn Levon 	func_sym = sym;
37*1f5207b7SJohn Levon 	FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
38*1f5207b7SJohn Levon 		if (!arg->ident) {
39*1f5207b7SJohn Levon 			continue;
40*1f5207b7SJohn Levon 		}
41*1f5207b7SJohn Levon 		set_state(my_id, arg->ident->name, arg, &argument);
42*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
43*1f5207b7SJohn Levon }
44*1f5207b7SJohn Levon 
get_arg_num(struct expression * expr)45*1f5207b7SJohn Levon static int get_arg_num(struct expression *expr)
46*1f5207b7SJohn Levon {
47*1f5207b7SJohn Levon 	struct smatch_state *state;
48*1f5207b7SJohn Levon 	struct symbol *arg;
49*1f5207b7SJohn Levon 	struct symbol *this_arg;
50*1f5207b7SJohn Levon 	int i;
51*1f5207b7SJohn Levon 
52*1f5207b7SJohn Levon 	expr = strip_expr(expr);
53*1f5207b7SJohn Levon 	if (expr->type != EXPR_SYMBOL)
54*1f5207b7SJohn Levon 		return -1;
55*1f5207b7SJohn Levon 	this_arg = expr->symbol;
56*1f5207b7SJohn Levon 
57*1f5207b7SJohn Levon 	state = get_state_expr(my_id, expr);
58*1f5207b7SJohn Levon 	if (!state || state != &argument)
59*1f5207b7SJohn Levon 		return -1;
60*1f5207b7SJohn Levon 
61*1f5207b7SJohn Levon 	i = 0;
62*1f5207b7SJohn Levon 	FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
63*1f5207b7SJohn Levon 		if (arg == this_arg)
64*1f5207b7SJohn Levon 			return i;
65*1f5207b7SJohn Levon 		i++;
66*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
67*1f5207b7SJohn Levon 
68*1f5207b7SJohn Levon 	return -1;
69*1f5207b7SJohn Levon }
70*1f5207b7SJohn Levon 
match_is_err(const char * fn,struct expression * expr,void * unused)71*1f5207b7SJohn Levon static void match_is_err(const char *fn, struct expression *expr, void *unused)
72*1f5207b7SJohn Levon {
73*1f5207b7SJohn Levon 	struct expression *arg;
74*1f5207b7SJohn Levon 	int arg_num;
75*1f5207b7SJohn Levon 
76*1f5207b7SJohn Levon 	arg = get_argument_from_call_expr(expr->args, 0);
77*1f5207b7SJohn Levon 	arg_num = get_arg_num(arg);
78*1f5207b7SJohn Levon 	if (arg_num < 0)
79*1f5207b7SJohn Levon 		return;
80*1f5207b7SJohn Levon 	sm_msg("info: expects ERR_PTR %d", arg_num);
81*1f5207b7SJohn Levon }
82*1f5207b7SJohn Levon 
check_expects_err_ptr(int id)83*1f5207b7SJohn Levon void check_expects_err_ptr(int id)
84*1f5207b7SJohn Levon {
85*1f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
86*1f5207b7SJohn Levon 		return;
87*1f5207b7SJohn Levon 	if (!option_info)
88*1f5207b7SJohn Levon 		return;
89*1f5207b7SJohn Levon 
90*1f5207b7SJohn Levon 	my_id = id;
91*1f5207b7SJohn Levon 	add_hook(&match_function_def, FUNC_DEF_HOOK);
92*1f5207b7SJohn Levon 	add_modification_hook(my_id, &set_ok);
93*1f5207b7SJohn Levon 	add_function_hook("IS_ERR", &match_is_err, NULL);
94*1f5207b7SJohn Levon }
95