states: make debug output more consistent
[smatch.git] / smatch_param_set.c
blobb721f89f7a55f7984722159d154cae259adb5293
1 /*
2 * Copyright (C) 2012 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
19 * This is for functions like:
21 * int foo(int *x)
22 * {
23 * if (*x == 42) {
24 * *x = 0;
25 * return 1;
26 * }
27 * return 0;
28 * }
30 * If we return 1 that means the value of *x has been set to 0. If we return
31 * 0 then we have left *x alone.
35 #include "scope.h"
36 #include "smatch.h"
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 static int my_id;
42 static struct smatch_state *unmatched_state(struct sm_state *sm)
44 return alloc_estate_empty();
47 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
49 if (__in_fake_assign)
50 return;
51 if (get_param_num_from_sym(sym) < 0)
52 return;
53 set_state(my_id, name, sym, state);
57 * This function is is a dirty hack because extra_mod_hook is giving us a NULL
58 * sym instead of a vsl.
60 static void match_array_assignment(struct expression *expr)
62 struct expression *array, *offset;
63 char *name;
64 struct symbol *sym;
65 struct range_list *rl;
66 sval_t sval;
67 char buf[256];
69 if (__in_fake_assign)
70 return;
72 if (!is_array(expr->left))
73 return;
74 array = get_array_base(expr->left);
75 offset = get_array_offset(expr->left);
77 /* These are handled by extra_mod_hook() */
78 if (get_value(offset, &sval))
79 return;
80 name = expr_to_var_sym(array, &sym);
81 if (!name || !sym)
82 goto free;
83 if (get_param_num_from_sym(sym) < 0)
84 goto free;
85 get_absolute_rl(expr->right, &rl);
86 rl = cast_rl(get_type(expr->left), rl);
88 snprintf(buf, sizeof(buf), "*%s", name);
89 set_state(my_id, buf, sym, alloc_estate_rl(rl));
90 free:
91 free_string(name);
95 * This relies on the fact that these states are stored so that
96 * foo->bar is before foo->bar->baz.
98 static int parent_set(struct string_list *list, const char *name)
100 char *tmp;
101 int len;
102 int ret;
104 FOR_EACH_PTR(list, tmp) {
105 len = strlen(tmp);
106 ret = strncmp(tmp, name, len);
107 if (ret < 0)
108 continue;
109 if (ret > 0)
110 return 0;
111 if (name[len] == '-')
112 return 1;
113 } END_FOR_EACH_PTR(tmp);
115 return 0;
118 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
120 struct stree *stree;
121 struct sm_state *sm;
122 struct smatch_state *extra;
123 int param;
124 struct range_list *rl;
125 const char *param_name;
126 struct string_list *set_list = NULL;
127 char *math_str;
128 char buf[256];
129 sval_t sval;
131 stree = __get_cur_stree();
133 FOR_EACH_MY_SM(my_id, stree, sm) {
134 if (!estate_rl(sm->state))
135 continue;
136 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
137 if (extra) {
138 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
139 if (!rl)
140 continue;
141 } else {
142 rl = estate_rl(sm->state);
145 param = get_param_num_from_sym(sm->sym);
146 if (param < 0)
147 continue;
148 param_name = get_param_name(sm);
149 if (!param_name)
150 continue;
151 if (strcmp(param_name, "$") == 0)
152 continue;
154 if (rl_to_sval(rl, &sval)) {
155 insert_string(&set_list, (char *)sm->name);
156 sql_insert_return_states(return_id, return_ranges,
157 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
158 param, param_name, show_rl(rl));
159 continue;
162 math_str = get_value_in_terms_of_parameter_math_var_sym(sm->name, sm->sym);
163 if (math_str) {
164 snprintf(buf, sizeof(buf), "%s[%s]", show_rl(rl), math_str);
165 insert_string(&set_list, (char *)sm->name);
166 sql_insert_return_states(return_id, return_ranges,
167 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
168 param, param_name, buf);
169 continue;
172 /* no useful information here. */
173 if (is_whole_rl(rl) && parent_set(set_list, sm->name))
174 continue;
175 insert_string(&set_list, (char *)sm->name);
177 sql_insert_return_states(return_id, return_ranges,
178 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
179 param, param_name, show_rl(rl));
181 } END_FOR_EACH_SM(sm);
183 free_ptr_list((struct ptr_list **)&set_list);
186 int param_was_set(struct expression *expr)
188 if (get_state_expr(my_id, expr))
189 return 1;
190 return 0;
193 void register_param_set(int id)
195 my_id = id;
197 add_extra_mod_hook(&extra_mod_hook);
198 add_hook(match_array_assignment, ASSIGNMENT_HOOK);
199 add_unmatched_state_hook(my_id, &unmatched_state);
200 add_merge_hook(my_id, &merge_estates);
201 add_split_return_callback(&print_return_value_param);