11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2015 Oracle.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon  */
171f5207b7SJohn Levon 
181f5207b7SJohn Levon #include "smatch.h"
191f5207b7SJohn Levon #include "smatch_slist.h"
201f5207b7SJohn Levon 
211f5207b7SJohn Levon static int my_id;
221f5207b7SJohn Levon 
231f5207b7SJohn Levon static struct stree *used_stree;
241f5207b7SJohn Levon static struct stree_stack *saved_stack;
251f5207b7SJohn Levon 
261f5207b7SJohn Levon STATE(used);
271f5207b7SJohn Levon 
get_state_hook(int owner,const char * name,struct symbol * sym)281f5207b7SJohn Levon static void get_state_hook(int owner, const char *name, struct symbol *sym)
291f5207b7SJohn Levon {
301f5207b7SJohn Levon 	int arg;
311f5207b7SJohn Levon 
321f5207b7SJohn Levon 	if (!option_info)
331f5207b7SJohn Levon 		return;
34*c85f09ccSJohn Levon 
35*c85f09ccSJohn Levon 	if (__in_fake_assign || __in_fake_parameter_assign || __in_function_def || __in_unmatched_hook)
361f5207b7SJohn Levon 		return;
371f5207b7SJohn Levon 
381f5207b7SJohn Levon 	arg = get_param_num_from_sym(sym);
39*c85f09ccSJohn Levon 	if (arg < 0)
40*c85f09ccSJohn Levon 		return;
41*c85f09ccSJohn Levon 	if (param_was_set_var_sym(name, sym))
42*c85f09ccSJohn Levon 		return;
43*c85f09ccSJohn Levon 	set_state_stree(&used_stree, my_id, name, sym, &used);
441f5207b7SJohn Levon }
451f5207b7SJohn Levon 
set_param_used(struct expression * call,struct expression * arg,char * key,char * unused)461f5207b7SJohn Levon static void set_param_used(struct expression *call, struct expression *arg, char *key, char *unused)
471f5207b7SJohn Levon {
481f5207b7SJohn Levon 	struct symbol *sym;
491f5207b7SJohn Levon 	char *name;
501f5207b7SJohn Levon 	int arg_nr;
511f5207b7SJohn Levon 
52*c85f09ccSJohn Levon 	if (!option_info)
53*c85f09ccSJohn Levon 		return;
54*c85f09ccSJohn Levon 
551f5207b7SJohn Levon 	name = get_variable_from_key(arg, key, &sym);
561f5207b7SJohn Levon 	if (!name || !sym)
571f5207b7SJohn Levon 		goto free;
581f5207b7SJohn Levon 
591f5207b7SJohn Levon 	arg_nr = get_param_num_from_sym(sym);
60*c85f09ccSJohn Levon 	if (arg_nr < 0)
61*c85f09ccSJohn Levon 		goto free;
62*c85f09ccSJohn Levon 	if (param_was_set_var_sym(name, sym))
63*c85f09ccSJohn Levon 		goto free;
64*c85f09ccSJohn Levon 	set_state_stree(&used_stree, my_id, name, sym, &used);
651f5207b7SJohn Levon free:
661f5207b7SJohn Levon 	free_string(name);
671f5207b7SJohn Levon }
681f5207b7SJohn Levon 
process_states(void)691f5207b7SJohn Levon static void process_states(void)
701f5207b7SJohn Levon {
711f5207b7SJohn Levon 	struct sm_state *tmp;
721f5207b7SJohn Levon 	int arg;
731f5207b7SJohn Levon 	const char *name;
741f5207b7SJohn Levon 
751f5207b7SJohn Levon 	FOR_EACH_SM(used_stree, tmp) {
761f5207b7SJohn Levon 		arg = get_param_num_from_sym(tmp->sym);
771f5207b7SJohn Levon 		if (arg < 0)
781f5207b7SJohn Levon 			continue;
791f5207b7SJohn Levon 		name = get_param_name(tmp);
801f5207b7SJohn Levon 		if (!name)
811f5207b7SJohn Levon 			continue;
82efe51d0cSJohn Levon 		if (is_recursive_member(name))
83efe51d0cSJohn Levon 			continue;
84efe51d0cSJohn Levon 
85efe51d0cSJohn Levon 		if (is_ignored_kernel_data(name))
86efe51d0cSJohn Levon 			continue;
871f5207b7SJohn Levon 
881f5207b7SJohn Levon 		sql_insert_return_implies(PARAM_USED, arg, name, "");
891f5207b7SJohn Levon 	} END_FOR_EACH_SM(tmp);
901f5207b7SJohn Levon 
911f5207b7SJohn Levon 	free_stree(&used_stree);
921f5207b7SJohn Levon }
931f5207b7SJohn Levon 
match_function_def(struct symbol * sym)941f5207b7SJohn Levon static void match_function_def(struct symbol *sym)
951f5207b7SJohn Levon {
961f5207b7SJohn Levon 	free_stree(&used_stree);
971f5207b7SJohn Levon }
981f5207b7SJohn Levon 
match_save_states(struct expression * expr)991f5207b7SJohn Levon static void match_save_states(struct expression *expr)
1001f5207b7SJohn Levon {
1011f5207b7SJohn Levon 	push_stree(&saved_stack, used_stree);
1021f5207b7SJohn Levon 	used_stree = NULL;
1031f5207b7SJohn Levon }
1041f5207b7SJohn Levon 
match_restore_states(struct expression * expr)1051f5207b7SJohn Levon static void match_restore_states(struct expression *expr)
1061f5207b7SJohn Levon {
1071f5207b7SJohn Levon 	free_stree(&used_stree);
1081f5207b7SJohn Levon 	used_stree = pop_stree(&saved_stack);
1091f5207b7SJohn Levon }
1101f5207b7SJohn Levon 
register_param_used(int id)1111f5207b7SJohn Levon void register_param_used(int id)
1121f5207b7SJohn Levon {
1131f5207b7SJohn Levon 	my_id = id;
1141f5207b7SJohn Levon 
1151f5207b7SJohn Levon 	add_hook(&match_function_def, FUNC_DEF_HOOK);
1161f5207b7SJohn Levon 
1171f5207b7SJohn Levon 	add_get_state_hook(&get_state_hook);
1181f5207b7SJohn Levon 
1191f5207b7SJohn Levon 	add_hook(&match_save_states, INLINE_FN_START);
1201f5207b7SJohn Levon 	add_hook(&match_restore_states, INLINE_FN_END);
1211f5207b7SJohn Levon 
1221f5207b7SJohn Levon 	select_return_implies_hook(PARAM_USED, &set_param_used);
1231f5207b7SJohn Levon 	all_return_states_hook(&process_states);
1241f5207b7SJohn Levon }
125