db: move return_states to raw SQL
[smatch.git] / smatch_param_limit.c
blob0a0b78ef13af5d8f65259cc36307efec09e3bf9a
1 /*
2 * sparse/smatch_param_limit.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is for functions like this:
13 * int foo(int a)
14 * {
15 * if (a >= 0 && a < 10) {
16 * a = 42;
17 * return 1;
18 * }
19 * return 0;
20 * }
22 * If we pass in 5, it returns 1.
24 * It's a bit complicated because we can't just consider the final value, we
25 * have to always consider the passed in value.
29 #include "scope.h"
30 #include "smatch.h"
31 #include "smatch_extra.h"
32 #include "smatch_slist.h"
34 static int my_id;
36 STATE(original);
38 static struct state_list *start_states;
39 static void save_start_states(struct statement *stmt)
41 start_states = get_all_states(SMATCH_EXTRA);
44 static void match_end_func(void)
46 free_slist(&start_states);
49 static struct smatch_state *unmatched_state(struct sm_state *sm)
51 return &original;
54 static struct smatch_state *filter_my_sm(struct sm_state *sm)
56 struct range_list *ret = NULL;
57 struct sm_state *tmp;
58 struct smatch_state *estate;
60 FOR_EACH_PTR(sm->possible, tmp) {
61 if (tmp->state == &merged)
62 continue;
63 if (tmp->state == &original) {
64 estate = get_state_slist(tmp->pool, SMATCH_EXTRA, tmp->name, tmp->sym);
65 if (!estate) {
66 sm_msg("debug: no value found in pool %p", tmp->pool);
67 continue;
69 } else {
70 estate = tmp->state;
72 ret = rl_union(ret, estate_rl(estate));
73 } END_FOR_EACH_PTR(tmp);
75 return alloc_estate_rl(ret);
78 struct smatch_state *get_orig_estate(const char *name, struct symbol *sym)
80 struct sm_state *sm;
81 struct smatch_state *state;
83 sm = get_sm_state(my_id, name, sym);
84 if (sm)
85 return filter_my_sm(sm);
87 state = get_state(SMATCH_EXTRA, name, sym);
88 if (state)
89 return state;
90 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym)));
93 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
95 struct state_list *extra_slist;
96 struct sm_state *tmp;
97 struct sm_state *my_sm;
98 struct smatch_state *state;
99 int param;
101 extra_slist = get_all_states_slist(SMATCH_EXTRA, slist);
103 FOR_EACH_PTR(extra_slist, tmp) {
104 if (!tmp->sym->ident || strcmp(tmp->name, tmp->sym->ident->name) != 0)
105 continue;
107 param = get_param_num_from_sym(tmp->sym);
108 if (param < 0)
109 continue;
111 my_sm = get_sm_state_slist(slist, my_id, tmp->name, tmp->sym);
112 if (!my_sm) {
113 struct smatch_state *old;
115 old = get_state_slist(start_states, SMATCH_EXTRA, tmp->name, tmp->sym);
116 if (old && estates_equiv(old, tmp->state))
117 continue;
118 sql_insert_return_states(return_id, return_ranges,
119 LIMITED_VALUE, param, "$$",
120 tmp->state->name);
121 continue;
124 state = filter_my_sm(my_sm);
125 if (!state)
126 continue;
127 /* This represents an impossible state. I screwd up. Bail. */
128 if (!estate_rl(state))
129 continue;
130 sql_insert_return_states(return_id, return_ranges,
131 LIMITED_VALUE, param, "$$",
132 state->name);
133 } END_FOR_EACH_PTR(tmp);
135 free_slist(&extra_slist);
138 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
140 struct smatch_state *orig_vals;
141 int param;
143 param = get_param_num_from_sym(sym);
144 if (param < 0)
145 return;
147 /* we are only saving params for now */
148 if (!sym->ident || strcmp(name, sym->ident->name) != 0)
149 return;
151 orig_vals = get_orig_estate(name, sym);
152 set_state(my_id, name, sym, orig_vals);
155 void register_param_limit(int id)
157 if (!option_info)
158 return;
160 my_id = id;
162 add_hook(&save_start_states, AFTER_DEF_HOOK);
163 add_extra_mod_hook(&extra_mod_hook);
164 add_unmatched_state_hook(my_id, &unmatched_state);
165 add_returned_state_callback(&print_return_value_param);
166 add_hook(&match_end_func, END_FUNC_HOOK);