validation: add a test for ranged case statements
[smatch.git] / smatch_param_set.c
blobdc4c34068cc8b5563fe8fac9332590064d0a4bb5
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 relies on the fact that these states are stored so that
58 * foo->bar is before foo->bar->baz.
60 static int parent_set(struct string_list *list, const char *name)
62 char *tmp;
63 int len;
64 int ret;
66 FOR_EACH_PTR(list, tmp) {
67 len = strlen(tmp);
68 ret = strncmp(tmp, name, len);
69 if (ret < 0)
70 continue;
71 if (ret > 0)
72 return 0;
73 if (name[len] == '-')
74 return 1;
75 } END_FOR_EACH_PTR(tmp);
77 return 0;
80 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr)
82 struct stree *stree;
83 struct sm_state *sm;
84 struct smatch_state *extra;
85 int param;
86 struct range_list *rl;
87 const char *param_name;
88 struct string_list *set_list = NULL;
89 char *math_str;
90 char buf[256];
91 sval_t sval;
93 stree = __get_cur_stree();
95 FOR_EACH_MY_SM(my_id, stree, sm) {
96 if (!estate_rl(sm->state))
97 continue;
98 extra = get_state(SMATCH_EXTRA, sm->name, sm->sym);
99 if (!estate_rl(extra))
100 continue;
101 rl = rl_intersection(estate_rl(sm->state), estate_rl(extra));
102 if (!rl)
103 continue;
105 param = get_param_num_from_sym(sm->sym);
106 if (param < 0)
107 continue;
108 param_name = get_param_name(sm);
109 if (!param_name)
110 continue;
111 if (strcmp(param_name, "$") == 0)
112 continue;
114 if (rl_to_sval(rl, &sval)) {
115 insert_string(&set_list, (char *)sm->name);
116 sql_insert_return_states(return_id, return_ranges,
117 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
118 param, param_name, show_rl(rl));
119 continue;
122 math_str = get_value_in_terms_of_parameter_math_var_sym(sm->name, sm->sym);
123 if (math_str) {
124 snprintf(buf, sizeof(buf), "%s[%s]", show_rl(rl), math_str);
125 insert_string(&set_list, (char *)sm->name);
126 sql_insert_return_states(return_id, return_ranges,
127 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
128 param, param_name, buf);
129 continue;
132 /* no useful information here. */
133 if (is_whole_rl(rl) && parent_set(set_list, sm->name))
134 continue;
135 insert_string(&set_list, (char *)sm->name);
137 sql_insert_return_states(return_id, return_ranges,
138 param_has_filter_data(sm) ? PARAM_ADD : PARAM_SET,
139 param, param_name, show_rl(rl));
141 } END_FOR_EACH_SM(sm);
143 free_ptr_list((struct ptr_list **)&set_list);
146 int param_was_set(struct expression *expr)
148 if (get_state_expr(my_id, expr))
149 return 1;
150 return 0;
153 void register_param_set(int id)
155 my_id = id;
157 add_extra_mod_hook(&extra_mod_hook);
158 add_unmatched_state_hook(my_id, &unmatched_state);
159 add_merge_hook(my_id, &merge_estates);
160 add_split_return_callback(&print_return_value_param);