project: fix parameter implications
[smatch.git] / check_expects_err_ptr.c
blob5dd3eb7536d6c32b7b03f5094843acba494df6a4
1 /*
2 * smatch/check_expects_err_ptr.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
12 static int my_id;
13 static struct symbol *func_sym;
15 STATE(argument);
16 STATE(ok);
18 static void set_ok(const char *name, struct symbol *sym, struct expression *expr, void *unused)
20 set_state(my_id, name, sym, &ok);
23 static void match_function_def(struct symbol *sym)
25 struct symbol *arg;
27 func_sym = sym;
28 FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
29 if (!arg->ident) {
30 continue;
32 set_state(my_id, arg->ident->name, arg, &argument);
33 } END_FOR_EACH_PTR(arg);
36 static int get_arg_num(struct expression *expr)
38 struct smatch_state *state;
39 struct symbol *arg;
40 struct symbol *this_arg;
41 int i;
43 expr = strip_expr(expr);
44 if (expr->type != EXPR_SYMBOL)
45 return -1;
46 this_arg = expr->symbol;
48 state = get_state_expr(my_id, expr);
49 if (!state || state != &argument)
50 return -1;
52 i = 0;
53 FOR_EACH_PTR(func_sym->ctype.base_type->arguments, arg) {
54 if (arg == this_arg)
55 return i;
56 i++;
57 } END_FOR_EACH_PTR(arg);
59 return -1;
62 static void match_is_err(const char *fn, struct expression *expr, void *unused)
64 struct expression *arg;
65 int arg_num;
67 arg = get_argument_from_call_expr(expr->args, 0);
68 arg_num = get_arg_num(arg);
69 if (arg_num < 0)
70 return;
71 sm_msg("info: expects ERR_PTR %d", arg_num);
74 void check_expects_err_ptr(int id)
76 if (option_project != PROJ_KERNEL)
77 return;
78 if (!option_info)
79 return;
81 my_id = id;
82 add_hook(&match_function_def, FUNC_DEF_HOOK);
83 set_default_modification_hook(my_id, &set_ok);
84 add_function_hook("IS_ERR", &match_is_err, NULL);