2 * sparse/smatch_constraints.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * smatch_constraints.c is for tracking how variables are related
17 * This is stored in a field in the smatch_extra dinfo.
19 * Normally the way that variables become related is through a
20 * condition and you say: add_constraint_expr(left, '<', right);
21 * The other way it can happen is if you have an assignment:
22 * set_equiv(left, right);
24 * One two variables "a" and "b" are related if then if we find
25 * that "a" is greater than 0 we need to update "b".
27 * When a variable gets modified all the old relationships are
28 * deleted. remove_contraints(expr);
30 * Also we need an is_true_constraint(left, '<', right) and
31 * is_false_constraint (left, '<', right). This is used by
37 #include "smatch_slist.h"
38 #include "smatch_extra.h"
40 ALLOCATOR(relation
, "related variables");
43 * set_equiv() is only used for assignments where we set one variable
44 * equal to the other. a = b;. It's not used for if conditions where
47 void set_equiv(struct expression
*left
, struct expression
*right
)
49 struct sm_state
*right_sm
;
50 struct smatch_state
*state
;
53 struct symbol
*left_sym
;
55 left_name
= get_variable_from_expr(left
, &left_sym
);
56 if (!left_name
|| !left_sym
)
59 right_sm
= get_sm_state_expr(SMATCH_EXTRA
, right
);
61 right_sm
= set_state_expr(SMATCH_EXTRA
, right
, extra_undefined());
65 remove_from_equiv(left_name
, left_sym
);
67 state
= clone_estate(right_sm
->state
);
68 if (!estate_related(state
))
69 add_equiv(state
, right_sm
->name
, right_sm
->sym
);
70 add_equiv(state
, left_name
, left_sym
);
72 FOR_EACH_PTR(estate_related(state
), rel
) {
73 struct sm_state
*new_sm
;
75 new_sm
= clone_sm(right_sm
);
76 new_sm
->name
= rel
->name
;
77 new_sm
->sym
= rel
->sym
;
78 new_sm
->state
= state
;
80 } END_FOR_EACH_PTR(rel
);
82 free_string(left_name
);
85 static struct relation
*alloc_relation(int op
, const char *name
, struct symbol
*sym
)
89 tmp
= __alloc_relation(0);
91 tmp
->name
= alloc_string(name
);
96 struct related_list
*clone_related_list(struct related_list
*related
)
99 struct related_list
*to_list
= NULL
;
101 FOR_EACH_PTR(related
, rel
) {
102 add_ptr_list(&to_list
, rel
);
103 } END_FOR_EACH_PTR(rel
);
108 struct relation
*get_common_relationship(struct smatch_state
*state
, int op
,
109 const char *name
, struct symbol
*sym
)
111 struct relation
*tmp
;
114 // Find the common x < y and x <= y
115 FOR_EACH_PTR(estate_related(state
), tmp
) {
116 if (tmp
->op
< op
|| tmp
->sym
< sym
|| strcmp(tmp
->name
, name
) < 0)
118 if (tmp
->op
== op
&& tmp
->sym
== sym
&& !strcmp(tmp
->name
, name
))
121 } END_FOR_EACH_PTR(tmp
);
125 static void debug_addition(struct smatch_state
*state
, int op
, const char *name
)
127 struct relation
*tmp
;
129 if (!option_debug_related
)
134 FOR_EACH_PTR(estate_related(state
), tmp
) {
135 sm_printf("%s %s ", show_special(tmp
->op
), tmp
->name
);
136 } END_FOR_EACH_PTR(tmp
);
137 sm_printf(") <-- %s %s\n", show_special(op
), name
);
140 void add_related(struct smatch_state
*state
, int op
, const char *name
, struct symbol
*sym
)
142 struct data_info
*dinfo
;
143 struct relation
*tmp
;
144 struct relation
*new;
146 debug_addition(state
, op
, name
);
148 dinfo
= get_dinfo(state
);
149 FOR_EACH_PTR(dinfo
->related
, tmp
) {
150 if (tmp
->op
< op
|| tmp
->sym
< sym
|| strcmp(tmp
->name
, name
) < 0)
152 if (tmp
->op
== op
&& tmp
->sym
== sym
&& !strcmp(tmp
->name
, name
))
154 new = alloc_relation(op
, name
, sym
);
155 INSERT_CURRENT(new, tmp
);
157 } END_FOR_EACH_PTR(tmp
);
158 new = alloc_relation(op
, name
, sym
);
159 add_ptr_list(&dinfo
->related
, new);
162 void del_related(struct smatch_state
*state
, int op
, const char *name
, struct symbol
*sym
)
164 struct relation
*tmp
;
166 FOR_EACH_PTR(estate_related(state
), tmp
) {
167 if (tmp
->sym
< sym
|| strcmp(tmp
->name
, name
) < 0)
169 if (tmp
->sym
== sym
&& !strcmp(tmp
->name
, name
)) {
170 DELETE_CURRENT_PTR(tmp
);
174 } END_FOR_EACH_PTR(tmp
);
177 void add_equiv(struct smatch_state
*state
, const char *name
, struct symbol
*sym
)
179 add_related(state
, SPECIAL_EQUAL
, name
, sym
);
182 static void del_equiv(struct smatch_state
*state
, const char *name
, struct symbol
*sym
)
184 del_related(state
, SPECIAL_EQUAL
, name
, sym
);
187 void remove_from_equiv(const char *name
, struct symbol
*sym
)
189 struct sm_state
*orig_sm
;
190 struct relation
*rel
;
191 struct smatch_state
*state
;
192 struct related_list
*to_update
;
194 // FIXME equiv => related
195 orig_sm
= get_sm_state(SMATCH_EXTRA
, name
, sym
);
196 if (!orig_sm
|| !get_dinfo(orig_sm
->state
)->related
)
199 state
= clone_estate(orig_sm
->state
);
200 del_equiv(state
, name
, sym
);
201 to_update
= get_dinfo(state
)->related
;
202 if (ptr_list_size((struct ptr_list
*)get_dinfo(state
)->related
) == 1)
203 get_dinfo(state
)->related
= NULL
;
205 FOR_EACH_PTR(to_update
, rel
) {
206 struct sm_state
*new_sm
;
208 new_sm
= clone_sm(orig_sm
);
209 new_sm
->name
= rel
->name
;
210 new_sm
->sym
= rel
->sym
;
211 new_sm
->state
= state
;
213 } END_FOR_EACH_PTR(rel
);
216 void remove_from_equiv_expr(struct expression
*expr
)
221 name
= get_variable_from_expr(expr
, &sym
);
224 remove_from_equiv(name
, sym
);
229 void add_constrain_expr(struct expression
*left
, int op
, struct expression
*right
)
234 void set_equiv_state_expr(int id
, struct expression
*expr
, struct smatch_state
*state
)
236 struct relation
*rel
;
237 struct smatch_state
*estate
;
239 estate
= get_state_expr(SMATCH_EXTRA
, expr
);
244 FOR_EACH_PTR(get_dinfo(estate
)->related
, rel
) {
245 if (rel
->op
!= SPECIAL_EQUAL
)
247 set_state(id
, rel
->name
, rel
->sym
, state
);
248 } END_FOR_EACH_PTR(rel
);