states: fix get_state_expr() uses of fake_parent
[smatch.git] / smatch_param_used.c
blob3ee442033574387b2da586a57691a16b7acbf7be
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 int arg;
32 if (!option_info)
33 return;
35 if (__ignore_param_used ||
36 __in_fake_assign ||
37 __in_fake_parameter_assign ||
38 __in_function_def ||
39 __in_unmatched_hook)
40 return;
42 arg = get_param_num_from_sym(sym);
43 if (arg < 0)
44 return;
45 if (param_was_set_var_sym(name, sym))
46 return;
47 set_state_stree(&used_stree, my_id, name, sym, &used);
50 static void set_param_used(struct expression *call, struct expression *arg, char *key, char *unused)
52 struct symbol *sym;
53 char *name;
54 int arg_nr;
56 if (!option_info)
57 return;
59 name = get_variable_from_key(arg, key, &sym);
60 if (!name || !sym)
61 goto free;
63 arg_nr = get_param_num_from_sym(sym);
64 if (arg_nr < 0)
65 goto free;
66 if (param_was_set_var_sym(name, sym))
67 goto free;
68 set_state_stree(&used_stree, my_id, name, sym, &used);
69 free:
70 free_string(name);
73 static void process_states(void)
75 struct sm_state *tmp;
76 int arg;
77 const char *name;
79 FOR_EACH_SM(used_stree, tmp) {
80 arg = get_param_num_from_sym(tmp->sym);
81 if (arg < 0)
82 continue;
83 name = get_param_name(tmp);
84 if (!name)
85 continue;
86 if (strcmp(name, "$") == 0 ||
87 strcmp(name, "*$") == 0)
88 continue;
89 if (is_recursive_member(name))
90 continue;
92 if (is_ignored_kernel_data(name))
93 continue;
95 sql_insert_return_implies(PARAM_USED, arg, name, "");
96 } END_FOR_EACH_SM(tmp);
98 free_stree(&used_stree);
101 static void match_function_def(struct symbol *sym)
103 free_stree(&used_stree);
106 void register_param_used(int id)
108 my_id = id;
110 add_hook(&match_function_def, AFTER_FUNC_HOOK);
111 add_function_data((unsigned long *)&used_stree);
113 add_get_state_hook(&get_state_hook);
115 select_return_implies_hook(PARAM_USED, &set_param_used);
116 all_return_states_hook(&process_states);