type: handle strings better
[smatch.git] / smatch_param_limit.c
blob148a9b6b0fb117c13f0ac3d1ab397bf7f9f6e78c
1 /*
2 * sparse/smatch_param_limit.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "scope.h"
11 #include "smatch.h"
12 #include "smatch_extra.h"
13 #include "smatch_slist.h"
15 static int my_id;
17 STATE(original);
19 static struct state_list *start_states;
20 static void save_start_states(struct statement *stmt)
22 start_states = get_all_states(SMATCH_EXTRA);
25 static void match_end_func(void)
27 free_slist(&start_states);
30 static struct smatch_state *unmatched_state(struct sm_state *sm)
32 return &original;
35 static struct smatch_state *filter_my_sm(struct sm_state *sm)
37 struct range_list *ret = NULL;
38 struct sm_state *tmp;
39 struct smatch_state *estate;
41 FOR_EACH_PTR(sm->possible, tmp) {
42 if (tmp->state == &merged)
43 continue;
44 if (tmp->state == &original) {
45 estate = get_state_slist(tmp->pool, SMATCH_EXTRA, tmp->name, tmp->sym);
46 if (!estate) {
47 sm_msg("debug: no value found in pool %p", tmp->pool);
48 continue;
50 } else {
51 estate = tmp->state;
53 ret = rl_union(ret, estate_rl(estate));
54 } END_FOR_EACH_PTR(tmp);
56 return alloc_estate_rl(ret);
59 struct smatch_state *get_orig_estate(const char *name, struct symbol *sym)
61 struct sm_state *sm;
62 struct smatch_state *state;
64 sm = get_sm_state(my_id, name, sym);
65 if (sm)
66 return filter_my_sm(sm);
68 state = get_state(SMATCH_EXTRA, name, sym);
69 if (state)
70 return state;
71 return alloc_estate_rl(alloc_whole_rl(get_real_base_type(sym)));
74 static void print_return_value_param(int return_id, char *return_ranges, struct expression *expr, struct state_list *slist)
76 struct state_list *my_slist;
77 struct sm_state *tmp;
78 struct smatch_state *state;
79 int param;
81 my_slist = get_all_states_slist(my_id, slist);
83 FOR_EACH_PTR(my_slist, tmp) {
84 param = get_param_num_from_sym(tmp->sym);
85 if (param < 0)
86 continue;
87 state = filter_my_sm(tmp);
88 if (!state)
89 continue;
90 /* This represents an impossible state. I screwd up. Bail. */
91 if (!estate_rl(state))
92 continue;
93 sm_msg("info: return_param_limit %d %d '%s' '$$' '%s' %s",
94 return_id, param, return_ranges,
95 state->name, global_static());
96 } END_FOR_EACH_PTR(tmp);
98 free_slist(&my_slist);
103 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
105 struct smatch_state *orig_vals;
106 int param;
108 param = get_param_num_from_sym(sym);
109 if (param < 0)
110 return;
112 /* we are only saving params for now */
113 if (!sym->ident || strcmp(name, sym->ident->name) != 0)
114 return;
116 orig_vals = get_orig_estate(name, sym);
117 set_state(my_id, name, sym, orig_vals);
120 void register_param_limit(int id)
122 if (!option_info)
123 return;
125 my_id = id;
127 add_hook(&save_start_states, AFTER_DEF_HOOK);
128 add_extra_mod_hook(&extra_mod_hook);
129 add_unmatched_state_hook(my_id, &unmatched_state);
130 add_returned_state_callback(&print_return_value_param);
131 add_hook(&match_end_func, END_FUNC_HOOK);