implied: cleanup debug output a little
[smatch.git] / check_dereferences_param.c
blob27a855e6677dd412b4bc3e64d902a81558af00dd
1 /*
2 * sparse/check_dereferences_param.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_extra.h"
12 #include "smatch_slist.h"
14 static int my_id;
16 STATE(derefed);
17 STATE(ignore);
19 static int is_arg(struct expression *expr)
21 struct symbol *arg;
23 if (expr->type != EXPR_SYMBOL)
24 return 0;
26 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
27 if (arg == expr->symbol)
28 return 1;
29 } END_FOR_EACH_PTR(arg);
30 return 0;
33 static void set_ignore(const char *name, struct symbol *sym, struct expression *expr, void *unused)
35 if (get_state(my_id, name, sym) == &derefed)
36 return;
37 set_state(my_id, name, sym, &ignore);
40 static void match_function_def(struct symbol *sym)
42 struct symbol *arg;
43 int i;
45 i = -1;
46 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
47 i++;
48 if (!arg->ident)
49 continue;
50 add_modification_hook(my_id, arg->ident->name, &set_ignore, NULL);
51 } END_FOR_EACH_PTR(arg);
54 static void check_deref(struct expression *expr)
56 struct sm_state *sm;
58 expr = strip_expr(expr);
60 if (!is_arg(expr))
61 return;
62 if (implied_not_equal(expr, 0))
63 return;
65 sm = get_sm_state_expr(my_id, expr);
66 if (sm && slist_has_state(sm->possible, &ignore))
67 return;
68 set_state_expr(my_id, expr, &derefed);
71 static void match_dereference(struct expression *expr)
73 if (expr->type != EXPR_PREOP)
74 return;
75 if (getting_address())
76 return;
77 check_deref(expr->unop);
80 static void set_param_dereferenced(struct expression *arg, char *unused)
82 check_deref(arg);
85 static void process_states(struct state_list *slist)
87 struct symbol *arg;
88 int i;
90 i = -1;
91 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
92 i++;
93 if (!arg->ident)
94 continue;
95 if (get_state_slist(slist, my_id, arg->ident->name, arg) == &derefed)
96 sm_msg("info: dereferences_param %d", i);
97 } END_FOR_EACH_PTR(arg);
100 void check_dereferences_param(int id)
102 if (!option_info)
103 return;
105 my_id = id;
107 add_hook(&match_function_def, FUNC_DEF_HOOK);
109 add_hook(&match_dereference, DEREF_HOOK);
110 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
112 all_return_states_hook(&process_states);