capped: fix handling of assignments
[smatch.git] / smatch_param_used.c
blob6c05689a0a7f21c390a76748d640d6d564ce4a0e
1 /*
2 * Copyright (C) 2015 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
19 #include "smatch_slist.h"
21 static int my_id;
22 int __ignore_param_used;
24 static struct stree *used_stree;
26 STATE(used);
28 static void get_state_hook(int owner, const char *name, struct symbol *sym)
30 static const char *prev;
31 int arg;
33 if (!option_info)
34 return;
36 if (__ignore_param_used ||
37 __in_fake_assign ||
38 __in_fake_parameter_assign ||
39 __in_function_def ||
40 __in_unmatched_hook ||
41 __in_fake_struct_assign)
42 return;
44 if (!name || name[0] == '&')
45 return;
47 if (name == prev)
48 return;
49 prev = name;
51 arg = get_param_num_from_sym(sym);
52 if (arg < 0)
53 return;
54 if (param_was_set_var_sym(name, sym))
55 return;
56 if (parent_was_PARAM_CLEAR(name, sym))
57 return;
59 set_state_stree(&used_stree, my_id, name, sym, &used);
62 static void set_param_used(struct expression *call, struct expression *arg, char *key, char *unused)
64 struct symbol *sym;
65 char *name;
66 int arg_nr;
68 if (!option_info)
69 return;
71 if (key[0] == '&')
72 return;
74 name = get_variable_from_key(arg, key, &sym);
75 if (!name || !sym)
76 goto free;
78 arg_nr = get_param_num_from_sym(sym);
79 if (arg_nr < 0)
80 goto free;
81 if (param_was_set_var_sym(name, sym))
82 goto free;
83 set_state_stree(&used_stree, my_id, name, sym, &used);
84 free:
85 free_string(name);
88 static void process_states(void)
90 struct sm_state *tmp;
91 int arg;
92 const char *name;
94 FOR_EACH_SM(used_stree, tmp) {
95 arg = get_param_num_from_sym(tmp->sym);
96 if (arg < 0)
97 continue;
98 name = get_param_name(tmp);
99 if (!name)
100 continue;
101 if (strcmp(name, "$") == 0 ||
102 strcmp(name, "*$") == 0)
103 continue;
104 if (is_recursive_member(name))
105 continue;
107 if (is_ignored_kernel_data(name))
108 continue;
110 sql_insert_return_implies(PARAM_USED, arg, name, "");
111 } END_FOR_EACH_SM(tmp);
113 free_stree(&used_stree);
116 static void match_function_def(struct symbol *sym)
118 free_stree(&used_stree);
121 void register_param_used(int id)
123 my_id = id;
125 add_hook(&match_function_def, AFTER_FUNC_HOOK);
126 add_function_data((unsigned long *)&used_stree);
128 add_get_state_hook(&get_state_hook);
130 select_return_implies_hook(PARAM_USED, &set_param_used);
131 all_return_states_hook(&process_states);