type: fix get_binop_type() for bit shift operations
[smatch.git] / check_dereferences_param.c
blob71ccf7d074a8beebfc0f2658602ca028b723b9a8
1 /*
2 * sparse/check_dereferences_param.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is an --info recipe. The goal is to print a message for every parameter
12 * which we can not avoid dereferencing. This is maybe a bit restrictive but it
13 * avoids some false positives.
16 #include "smatch.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
20 static int my_id;
22 STATE(derefed);
23 STATE(ignore);
24 STATE(param);
26 static int is_arg(struct expression *expr)
28 struct symbol *arg;
30 if (expr->type != EXPR_SYMBOL)
31 return 0;
33 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
34 if (arg == expr->symbol)
35 return 1;
36 } END_FOR_EACH_PTR(arg);
37 return 0;
40 static void set_ignore(struct sm_state *sm)
42 if (sm->state == &derefed)
43 return;
44 set_state(my_id, sm->name, sm->sym, &ignore);
47 static void match_function_def(struct symbol *sym)
49 struct symbol *arg;
50 int i;
52 i = -1;
53 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
54 i++;
55 if (!arg->ident)
56 continue;
57 set_state(my_id, arg->ident->name, arg, &param);
58 } END_FOR_EACH_PTR(arg);
61 static void check_deref(struct expression *expr)
63 struct expression *tmp;
64 struct sm_state *sm;
66 tmp = get_assigned_expr(expr);
67 if (tmp)
68 expr = tmp;
69 expr = strip_expr(expr);
71 if (!is_arg(expr))
72 return;
73 if (implied_not_equal(expr, 0))
74 return;
76 sm = get_sm_state_expr(my_id, expr);
77 if (sm && slist_has_state(sm->possible, &ignore))
78 return;
79 set_state_expr(my_id, expr, &derefed);
82 static void match_dereference(struct expression *expr)
84 if (expr->type != EXPR_PREOP)
85 return;
86 if (getting_address())
87 return;
88 check_deref(expr->unop);
91 static void set_param_dereferenced(struct expression *arg, char *unused)
93 check_deref(arg);
96 static void process_states(struct state_list *slist)
98 struct symbol *arg;
99 int i;
101 i = -1;
102 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
103 i++;
104 if (!arg->ident)
105 continue;
106 if (get_state_slist(slist, my_id, arg->ident->name, arg) == &derefed)
107 sm_msg("info: dereferences_param %d %s", i, global_static());
108 } END_FOR_EACH_PTR(arg);
111 void check_dereferences_param(int id)
113 if (!option_info)
114 return;
116 my_id = id;
118 add_hook(&match_function_def, FUNC_DEF_HOOK);
120 add_hook(&match_dereference, DEREF_HOOK);
121 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
122 add_modification_hook(my_id, &set_ignore);
124 all_return_states_hook(&process_states);