xref: /illumos-gate/usr/src/tools/smatch/src/smatch_param_used.c (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2015 Oracle.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon #include "smatch_slist.h"
20*1f5207b7SJohn Levon 
21*1f5207b7SJohn Levon static int my_id;
22*1f5207b7SJohn Levon 
23*1f5207b7SJohn Levon static struct stree *used_stree;
24*1f5207b7SJohn Levon static struct stree_stack *saved_stack;
25*1f5207b7SJohn Levon 
26*1f5207b7SJohn Levon STATE(used);
27*1f5207b7SJohn Levon 
28*1f5207b7SJohn Levon static void get_state_hook(int owner, const char *name, struct symbol *sym)
29*1f5207b7SJohn Levon {
30*1f5207b7SJohn Levon 	int arg;
31*1f5207b7SJohn Levon 
32*1f5207b7SJohn Levon 	if (!option_info)
33*1f5207b7SJohn Levon 		return;
34*1f5207b7SJohn Levon 	if (__in_fake_assign)
35*1f5207b7SJohn Levon 		return;
36*1f5207b7SJohn Levon 
37*1f5207b7SJohn Levon 	arg = get_param_num_from_sym(sym);
38*1f5207b7SJohn Levon 	if (arg >= 0)
39*1f5207b7SJohn Levon 		set_state_stree(&used_stree, my_id, name, sym, &used);
40*1f5207b7SJohn Levon }
41*1f5207b7SJohn Levon 
42*1f5207b7SJohn Levon static void set_param_used(struct expression *call, struct expression *arg, char *key, char *unused)
43*1f5207b7SJohn Levon {
44*1f5207b7SJohn Levon 	struct symbol *sym;
45*1f5207b7SJohn Levon 	char *name;
46*1f5207b7SJohn Levon 	int arg_nr;
47*1f5207b7SJohn Levon 
48*1f5207b7SJohn Levon 	name = get_variable_from_key(arg, key, &sym);
49*1f5207b7SJohn Levon 	if (!name || !sym)
50*1f5207b7SJohn Levon 		goto free;
51*1f5207b7SJohn Levon 
52*1f5207b7SJohn Levon 	arg_nr = get_param_num_from_sym(sym);
53*1f5207b7SJohn Levon 	if (arg_nr >= 0)
54*1f5207b7SJohn Levon 		set_state(my_id, name, sym, &used);
55*1f5207b7SJohn Levon free:
56*1f5207b7SJohn Levon 	free_string(name);
57*1f5207b7SJohn Levon }
58*1f5207b7SJohn Levon 
59*1f5207b7SJohn Levon static void process_states(void)
60*1f5207b7SJohn Levon {
61*1f5207b7SJohn Levon 	struct sm_state *tmp;
62*1f5207b7SJohn Levon 	int arg;
63*1f5207b7SJohn Levon 	const char *name;
64*1f5207b7SJohn Levon 
65*1f5207b7SJohn Levon 	FOR_EACH_SM(used_stree, tmp) {
66*1f5207b7SJohn Levon 		arg = get_param_num_from_sym(tmp->sym);
67*1f5207b7SJohn Levon 		if (arg < 0)
68*1f5207b7SJohn Levon 			continue;
69*1f5207b7SJohn Levon 		name = get_param_name(tmp);
70*1f5207b7SJohn Levon 		if (!name)
71*1f5207b7SJohn Levon 			continue;
72*1f5207b7SJohn Levon 
73*1f5207b7SJohn Levon 		sql_insert_return_implies(PARAM_USED, arg, name, "");
74*1f5207b7SJohn Levon 	} END_FOR_EACH_SM(tmp);
75*1f5207b7SJohn Levon 
76*1f5207b7SJohn Levon 	free_stree(&used_stree);
77*1f5207b7SJohn Levon }
78*1f5207b7SJohn Levon 
79*1f5207b7SJohn Levon static void match_function_def(struct symbol *sym)
80*1f5207b7SJohn Levon {
81*1f5207b7SJohn Levon 	free_stree(&used_stree);
82*1f5207b7SJohn Levon }
83*1f5207b7SJohn Levon 
84*1f5207b7SJohn Levon static void match_save_states(struct expression *expr)
85*1f5207b7SJohn Levon {
86*1f5207b7SJohn Levon 	push_stree(&saved_stack, used_stree);
87*1f5207b7SJohn Levon 	used_stree = NULL;
88*1f5207b7SJohn Levon }
89*1f5207b7SJohn Levon 
90*1f5207b7SJohn Levon static void match_restore_states(struct expression *expr)
91*1f5207b7SJohn Levon {
92*1f5207b7SJohn Levon 	free_stree(&used_stree);
93*1f5207b7SJohn Levon 	used_stree = pop_stree(&saved_stack);
94*1f5207b7SJohn Levon }
95*1f5207b7SJohn Levon 
96*1f5207b7SJohn Levon void register_param_used(int id)
97*1f5207b7SJohn Levon {
98*1f5207b7SJohn Levon 	my_id = id;
99*1f5207b7SJohn Levon 
100*1f5207b7SJohn Levon 	add_hook(&match_function_def, FUNC_DEF_HOOK);
101*1f5207b7SJohn Levon 
102*1f5207b7SJohn Levon 	add_get_state_hook(&get_state_hook);
103*1f5207b7SJohn Levon 
104*1f5207b7SJohn Levon 	add_hook(&match_save_states, INLINE_FN_START);
105*1f5207b7SJohn Levon 	add_hook(&match_restore_states, INLINE_FN_END);
106*1f5207b7SJohn Levon 
107*1f5207b7SJohn Levon 	select_return_implies_hook(PARAM_USED, &set_param_used);
108*1f5207b7SJohn Levon 	all_return_states_hook(&process_states);
109*1f5207b7SJohn Levon }
110