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