2 * Copyright (C) 2014 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 * Keep a record of all the things we have tested for so that we know when we
20 * test for it again. For example, if we have code like this:
30 * That's the end goal at least. But actually implementing the flow of that
31 * requires quite a bit of work because if "foo" changes the condition needs to
32 * be retested and smatch_implications.c needs to be updated.
34 * For now, I just record the conditions and use it to see if we test for NULL
40 #include "smatch_slist.h"
45 static struct smatch_state
*alloc_link_state(struct string_list
*links
)
47 struct smatch_state
*state
;
52 state
= __alloc_smatch_state(0);
55 FOR_EACH_PTR(links
, tmp
) {
57 snprintf(buf
, sizeof(buf
), "%s", tmp
);
59 append(buf
, ", ", sizeof(buf
));
60 append(buf
, tmp
, sizeof(buf
));
62 } END_FOR_EACH_PTR(tmp
);
64 state
->name
= alloc_sname(buf
);
69 static struct smatch_state
*merge_links(struct smatch_state
*s1
, struct smatch_state
*s2
)
71 struct smatch_state
*ret
;
72 struct string_list
*links
;
74 links
= combine_string_lists(s1
->data
, s2
->data
);
75 ret
= alloc_link_state(links
);
79 static void save_link_var_sym(const char *var
, struct symbol
*sym
, const char *link
)
81 struct smatch_state
*old_state
, *new_state
;
82 struct string_list
*links
;
85 old_state
= get_state(link_id
, var
, sym
);
87 links
= clone_str_list(old_state
->data
);
91 new = alloc_sname(link
);
92 insert_string(&links
, new);
94 new_state
= alloc_link_state(links
);
95 set_state(link_id
, var
, sym
, new_state
);
98 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
100 struct string_list
*links
;
103 links
= sm
->state
->data
;
105 FOR_EACH_PTR(links
, tmp
) {
106 set_state(my_id
, tmp
, NULL
, &undefined
);
107 } END_FOR_EACH_PTR(tmp
);
108 set_state(link_id
, sm
->name
, sm
->sym
, &undefined
);
111 static struct smatch_state
*alloc_state(struct expression
*expr
, int is_true
)
113 struct smatch_state
*state
;
115 state
= __alloc_smatch_state(0);
117 state
->name
= alloc_sname("true");
119 state
->name
= alloc_sname("false");
124 static void store_all_links(struct expression
*expr
, const char *condition
)
129 expr
= strip_expr(expr
);
131 switch (expr
->type
) {
134 store_all_links(expr
->left
, condition
);
135 store_all_links(expr
->right
, condition
);
141 var
= expr_to_var_sym(expr
, &sym
);
144 save_link_var_sym(var
, sym
, condition
);
149 static int condition_too_complicated(struct expression
*expr
)
151 if (get_complication_score(expr
) > 2)
157 static void match_condition(struct expression
*expr
)
159 struct smatch_state
*true_state
, *false_state
;
163 if (get_implied_value(expr
, &val
))
166 if (condition_too_complicated(expr
))
169 name
= expr_to_str(expr
);
172 true_state
= alloc_state(expr
, TRUE
);
173 false_state
= alloc_state(expr
, FALSE
);
174 set_true_false_states(my_id
, name
, NULL
, true_state
, false_state
);
175 store_all_links(expr
, alloc_sname(name
));
179 struct smatch_state
*get_stored_condition(struct expression
*expr
)
181 struct smatch_state
*state
;
184 name
= expr_to_str(expr
);
188 state
= get_state(my_id
, name
, NULL
);
193 void register_stored_conditions(int id
)
197 add_hook(&match_condition
, CONDITION_HOOK
);
200 void register_stored_conditions_links(int id
)
203 add_merge_hook(link_id
, &merge_links
);
204 add_modification_hook(link_id
, &match_modify
);