user_data: cleanup: remove special handling of macro assigns
[smatch.git] / smatch_comparison.c
blobf869b774f2f08c170b31b1d3ec1ecd3bb049f3a3
1 /*
2 * smatch/smatch_comparison.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
11 * The point here is to store the relationships between two variables.
12 * Ie: y > x.
13 * To do that we create a state with the two variables in alphabetical order:
14 * ->name = "x vs y" and the state would be "<". On the false path the state
15 * would be ">=".
17 * Part of the trick of it is that if x or y is modified then we need to reset
18 * the state. We need to keep a list of all the states which depend on x and
19 * all the states which depend on y. The link_id code handles this.
23 #include "smatch.h"
24 #include "smatch_extra.h"
25 #include "smatch_slist.h"
27 static int compare_id;
28 static int link_id;
30 struct compare_data {
31 const char *var1;
32 struct var_sym_list *vsl1;
33 int comparison;
34 const char *var2;
35 struct var_sym_list *vsl2;
37 ALLOCATOR(compare_data, "compare data");
39 int chunk_vsl_eq(const char *a, struct var_sym_list *a_vsl, const char *b, struct var_sym_list *b_vsl)
41 if (strcmp(a, b) == 0)
42 return 1;
43 return 0;
46 static struct symbol *vsl_to_sym(struct var_sym_list *vsl)
48 struct var_sym *vs;
50 if (!vsl)
51 return NULL;
52 if (ptr_list_size((struct ptr_list *)vsl) != 1)
53 return NULL;
54 vs = first_ptr_list((struct ptr_list *)vsl);
55 return vs->sym;
58 static struct smatch_state *alloc_compare_state(
59 const char *var1, struct var_sym_list *vsl1,
60 int comparison,
61 const char *var2, struct var_sym_list *vsl2)
63 struct smatch_state *state;
64 struct compare_data *data;
66 state = __alloc_smatch_state(0);
67 state->name = alloc_sname(show_special(comparison));
68 data = __alloc_compare_data(0);
69 data->var1 = alloc_sname(var1);
70 data->vsl1 = clone_var_sym_list(vsl1);
71 data->comparison = comparison;
72 data->var2 = alloc_sname(var2);
73 data->vsl2 = clone_var_sym_list(vsl2);
74 state->data = data;
75 return state;
78 static int state_to_comparison(struct smatch_state *state)
80 if (!state || !state->data)
81 return 0;
82 return ((struct compare_data *)state->data)->comparison;
86 * flip_op() reverses the op left and right. So "x >= y" becomes "y <= x".
88 static int flip_op(int op)
90 switch (op) {
91 case 0:
92 return 0;
93 case '<':
94 return '>';
95 case SPECIAL_UNSIGNED_LT:
96 return SPECIAL_UNSIGNED_GT;
97 case SPECIAL_LTE:
98 return SPECIAL_GTE;
99 case SPECIAL_UNSIGNED_LTE:
100 return SPECIAL_UNSIGNED_GTE;
101 case SPECIAL_EQUAL:
102 return SPECIAL_EQUAL;
103 case SPECIAL_NOTEQUAL:
104 return SPECIAL_NOTEQUAL;
105 case SPECIAL_GTE:
106 return SPECIAL_LTE;
107 case SPECIAL_UNSIGNED_GTE:
108 return SPECIAL_UNSIGNED_LTE;
109 case '>':
110 return '<';
111 case SPECIAL_UNSIGNED_GT:
112 return SPECIAL_UNSIGNED_LT;
113 default:
114 sm_msg("internal smatch bug. unhandled comparison %d", op);
115 return op;
119 static int falsify_op(int op)
121 switch (op) {
122 case 0:
123 return 0;
124 case '<':
125 return SPECIAL_GTE;
126 case SPECIAL_UNSIGNED_LT:
127 return SPECIAL_UNSIGNED_GTE;
128 case SPECIAL_LTE:
129 return '>';
130 case SPECIAL_UNSIGNED_LTE:
131 return SPECIAL_UNSIGNED_GT;
132 case SPECIAL_EQUAL:
133 return SPECIAL_NOTEQUAL;
134 case SPECIAL_NOTEQUAL:
135 return SPECIAL_EQUAL;
136 case SPECIAL_GTE:
137 return '<';
138 case SPECIAL_UNSIGNED_GTE:
139 return SPECIAL_UNSIGNED_LT;
140 case '>':
141 return SPECIAL_LTE;
142 case SPECIAL_UNSIGNED_GT:
143 return SPECIAL_UNSIGNED_LTE;
144 default:
145 sm_msg("internal smatch bug. unhandled comparison %d", op);
146 return op;
150 static int rl_comparison(struct range_list *left_rl, struct range_list *right_rl)
152 sval_t left_min, left_max, right_min, right_max;
154 if (!left_rl || !right_rl)
155 return 0;
157 left_min = rl_min(left_rl);
158 left_max = rl_max(left_rl);
159 right_min = rl_min(right_rl);
160 right_max = rl_max(right_rl);
162 if (left_min.value == left_max.value &&
163 right_min.value == right_max.value &&
164 left_min.value == right_min.value)
165 return SPECIAL_EQUAL;
167 if (sval_cmp(left_max, right_min) < 0)
168 return '<';
169 if (sval_cmp(left_max, right_min) == 0)
170 return SPECIAL_LTE;
171 if (sval_cmp(left_min, right_max) > 0)
172 return '>';
173 if (sval_cmp(left_min, right_max) == 0)
174 return SPECIAL_GTE;
176 return 0;
179 static struct range_list *get_orig_rl(struct var_sym_list *vsl)
181 struct symbol *sym;
182 struct smatch_state *state;
184 if (!vsl)
185 return NULL;
186 sym = vsl_to_sym(vsl);
187 if (!sym || !sym->ident)
188 return NULL;
189 state = get_orig_estate(sym->ident->name, sym);
190 return estate_rl(state);
193 static struct smatch_state *unmatched_comparison(struct sm_state *sm)
195 struct compare_data *data = sm->state->data;
196 struct range_list *left_rl, *right_rl;
197 int op;
199 if (!data)
200 return &undefined;
202 if (strstr(data->var1, " orig"))
203 left_rl = get_orig_rl(data->vsl1);
204 else if (!get_implied_rl_var_sym(data->var1, vsl_to_sym(data->vsl1), &left_rl))
205 return &undefined;
206 if (strstr(data->var2, " orig"))
207 right_rl = get_orig_rl(data->vsl2);
208 else if (!get_implied_rl_var_sym(data->var2, vsl_to_sym(data->vsl2), &right_rl))
209 return &undefined;
212 op = rl_comparison(left_rl, right_rl);
213 if (op)
214 return alloc_compare_state(data->var1, data->vsl1, op, data->var2, data->vsl2);
216 return &undefined;
219 /* remove_unsigned_from_comparison() is obviously a hack. */
220 static int remove_unsigned_from_comparison(int op)
222 switch (op) {
223 case SPECIAL_UNSIGNED_LT:
224 return '<';
225 case SPECIAL_UNSIGNED_LTE:
226 return SPECIAL_LTE;
227 case SPECIAL_UNSIGNED_GTE:
228 return SPECIAL_GTE;
229 case SPECIAL_UNSIGNED_GT:
230 return '>';
231 default:
232 return op;
237 * This is for when you merge states "a < b" and "a == b", the result is that
238 * we can say for sure, "a <= b" after the merge.
240 static int merge_comparisons(int one, int two)
242 int LT, EQ, GT;
244 one = remove_unsigned_from_comparison(one);
245 two = remove_unsigned_from_comparison(two);
247 LT = EQ = GT = 0;
249 switch (one) {
250 case '<':
251 LT = 1;
252 break;
253 case SPECIAL_LTE:
254 LT = 1;
255 EQ = 1;
256 break;
257 case SPECIAL_EQUAL:
258 EQ = 1;
259 break;
260 case SPECIAL_GTE:
261 GT = 1;
262 EQ = 1;
263 break;
264 case '>':
265 GT = 1;
268 switch (two) {
269 case '<':
270 LT = 1;
271 break;
272 case SPECIAL_LTE:
273 LT = 1;
274 EQ = 1;
275 break;
276 case SPECIAL_EQUAL:
277 EQ = 1;
278 break;
279 case SPECIAL_GTE:
280 GT = 1;
281 EQ = 1;
282 break;
283 case '>':
284 GT = 1;
287 if (LT && EQ && GT)
288 return 0;
289 if (LT && EQ)
290 return SPECIAL_LTE;
291 if (LT && GT)
292 return SPECIAL_NOTEQUAL;
293 if (LT)
294 return '<';
295 if (EQ && GT)
296 return SPECIAL_GTE;
297 if (GT)
298 return '>';
299 return 0;
303 * This is for if you have "a < b" and "b <= c". Or in other words,
304 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
305 * The return comparison would be '<'.
307 * This function is different from merge_comparisons(), for example:
308 * merge_comparison('<', '==') returns '<='
309 * get_combined_comparison('<', '==') returns '<'
311 static int combine_comparisons(int left_compare, int right_compare)
313 int LT, EQ, GT;
315 left_compare = remove_unsigned_from_comparison(left_compare);
316 right_compare = remove_unsigned_from_comparison(right_compare);
318 LT = EQ = GT = 0;
320 switch (left_compare) {
321 case '<':
322 LT++;
323 break;
324 case SPECIAL_LTE:
325 LT++;
326 EQ++;
327 break;
328 case SPECIAL_EQUAL:
329 return right_compare;
330 case SPECIAL_GTE:
331 GT++;
332 EQ++;
333 break;
334 case '>':
335 GT++;
338 switch (right_compare) {
339 case '<':
340 LT++;
341 break;
342 case SPECIAL_LTE:
343 LT++;
344 EQ++;
345 break;
346 case SPECIAL_EQUAL:
347 return left_compare;
348 case SPECIAL_GTE:
349 GT++;
350 EQ++;
351 break;
352 case '>':
353 GT++;
356 if (LT == 2) {
357 if (EQ == 2)
358 return SPECIAL_LTE;
359 return '<';
362 if (GT == 2) {
363 if (EQ == 2)
364 return SPECIAL_GTE;
365 return '>';
367 return 0;
370 static struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
372 struct compare_data *data = s1->data;
373 int op;
375 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
376 if (op)
377 return alloc_compare_state(data->var1, data->vsl1, op, data->var2, data->vsl2);
378 return &undefined;
381 struct smatch_state *alloc_link_state(struct string_list *links)
383 struct smatch_state *state;
384 static char buf[256];
385 char *tmp;
386 int i;
388 state = __alloc_smatch_state(0);
390 i = 0;
391 FOR_EACH_PTR(links, tmp) {
392 if (!i++) {
393 snprintf(buf, sizeof(buf), "%s", tmp);
394 } else {
395 append(buf, ", ", sizeof(buf));
396 append(buf, tmp, sizeof(buf));
398 } END_FOR_EACH_PTR(tmp);
400 state->name = alloc_sname(buf);
401 state->data = links;
402 return state;
405 static void save_start_states(struct statement *stmt)
407 struct symbol *param;
408 char orig[64];
409 char state_name[128];
410 struct smatch_state *state;
411 struct string_list *links;
412 char *link;
414 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
415 struct var_sym_list *vsl1 = NULL;
416 struct var_sym_list *vsl2 = NULL;
418 if (!param->ident)
419 continue;
420 snprintf(orig, sizeof(orig), "%s orig", param->ident->name);
421 snprintf(state_name, sizeof(state_name), "%s vs %s", param->ident->name, orig);
422 add_var_sym(&vsl1, param->ident->name, param);
423 add_var_sym(&vsl2, orig, param);
424 state = alloc_compare_state(param->ident->name, vsl1, SPECIAL_EQUAL, alloc_sname(orig), vsl2);
425 set_state(compare_id, state_name, NULL, state);
427 link = alloc_sname(state_name);
428 links = NULL;
429 insert_string(&links, link);
430 state = alloc_link_state(links);
431 set_state(link_id, param->ident->name, param, state);
432 } END_FOR_EACH_PTR(param);
435 static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
437 struct smatch_state *ret;
438 struct string_list *links;
440 links = combine_string_lists(s1->data, s2->data);
441 ret = alloc_link_state(links);
442 return ret;
445 static void save_link_var_sym(const char *var, struct symbol *sym, const char *link)
447 struct smatch_state *old_state, *new_state;
448 struct string_list *links;
449 char *new;
451 old_state = get_state(link_id, var, sym);
452 if (old_state)
453 links = clone_str_list(old_state->data);
454 else
455 links = NULL;
457 new = alloc_sname(link);
458 insert_string(&links, new);
460 new_state = alloc_link_state(links);
461 set_state(link_id, var, sym, new_state);
464 static void match_inc(struct sm_state *sm)
466 struct string_list *links;
467 struct smatch_state *state;
468 char *tmp;
470 links = sm->state->data;
472 FOR_EACH_PTR(links, tmp) {
473 state = get_state(compare_id, tmp, NULL);
475 switch (state_to_comparison(state)) {
476 case SPECIAL_EQUAL:
477 case SPECIAL_GTE:
478 case SPECIAL_UNSIGNED_GTE:
479 case '>':
480 case SPECIAL_UNSIGNED_GT: {
481 struct compare_data *data = state->data;
482 struct smatch_state *new;
484 new = alloc_compare_state(data->var1, data->vsl1, '>', data->var2, data->vsl2);
485 set_state(compare_id, tmp, NULL, new);
486 break;
488 default:
489 set_state(compare_id, tmp, NULL, &undefined);
491 } END_FOR_EACH_PTR(tmp);
494 static void match_dec(struct sm_state *sm)
496 struct string_list *links;
497 struct smatch_state *state;
498 char *tmp;
500 links = sm->state->data;
502 FOR_EACH_PTR(links, tmp) {
503 state = get_state(compare_id, tmp, NULL);
505 switch (state_to_comparison(state)) {
506 case SPECIAL_EQUAL:
507 case SPECIAL_LTE:
508 case SPECIAL_UNSIGNED_LTE:
509 case '<':
510 case SPECIAL_UNSIGNED_LT: {
511 struct compare_data *data = state->data;
512 struct smatch_state *new;
514 new = alloc_compare_state(data->var1, data->vsl1, '<', data->var2, data->vsl2);
515 set_state(compare_id, tmp, NULL, new);
516 break;
518 default:
519 set_state(compare_id, tmp, NULL, &undefined);
521 } END_FOR_EACH_PTR(tmp);
524 static int match_inc_dec(struct sm_state *sm, struct expression *mod_expr)
526 if (!mod_expr)
527 return 0;
528 if (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP)
529 return 0;
531 if (mod_expr->op == SPECIAL_INCREMENT) {
532 match_inc(sm);
533 return 1;
535 if (mod_expr->op == SPECIAL_DECREMENT) {
536 match_dec(sm);
537 return 1;
539 return 0;
542 static void match_modify(struct sm_state *sm, struct expression *mod_expr)
544 struct string_list *links;
545 char *tmp;
547 if (match_inc_dec(sm, mod_expr))
548 return;
550 links = sm->state->data;
552 FOR_EACH_PTR(links, tmp) {
553 set_state(compare_id, tmp, NULL, &undefined);
554 } END_FOR_EACH_PTR(tmp);
555 set_state(link_id, sm->name, sm->sym, &undefined);
558 static char *chunk_to_var_sym(struct expression *expr, struct symbol **sym)
560 char *name, *left_name, *right_name;
561 struct symbol *tmp;
562 char buf[128];
564 expr = strip_expr(expr);
565 if (!expr)
566 return NULL;
567 if (sym)
568 *sym = NULL;
570 name = expr_to_var_sym(expr, &tmp);
571 if (name && tmp) {
572 if (sym)
573 *sym = tmp;
574 return name;
576 if (name)
577 free_string(name);
579 if (expr->type != EXPR_BINOP)
580 return NULL;
581 if (expr->op != '-' && expr->op != '+')
582 return NULL;
584 left_name = expr_to_var(expr->left);
585 if (!left_name)
586 return NULL;
587 right_name = expr_to_var(expr->right);
588 if (!right_name) {
589 free_string(left_name);
590 return NULL;
592 snprintf(buf, sizeof(buf), "%s %s %s", left_name, show_special(expr->op), right_name);
593 free_string(left_name);
594 free_string(right_name);
595 return alloc_string(buf);
598 static char *chunk_to_var(struct expression *expr)
600 return chunk_to_var_sym(expr, NULL);
603 static void save_link(struct expression *expr, char *link)
605 char *var;
606 struct symbol *sym;
608 expr = strip_expr(expr);
609 if (expr->type == EXPR_BINOP) {
610 char *chunk;
612 chunk = chunk_to_var(expr);
613 if (!chunk)
614 return;
616 save_link(expr->left, link);
617 save_link(expr->right, link);
618 save_link_var_sym(chunk, NULL, link);
619 return;
622 var = expr_to_var_sym(expr, &sym);
623 if (!var || !sym)
624 goto done;
626 save_link_var_sym(var, sym, link);
627 done:
628 free_string(var);
631 static void update_tf_links(struct state_list *pre_slist,
632 const char *left_var, struct var_sym_list *left_vsl,
633 int left_comparison,
634 const char *mid_var, struct var_sym_list *mid_vsl,
635 struct string_list *links)
637 struct smatch_state *state;
638 struct smatch_state *true_state, *false_state;
639 struct compare_data *data;
640 const char *right_var;
641 struct var_sym_list *right_vsl;
642 int right_comparison;
643 int true_comparison;
644 int false_comparison;
645 char *tmp;
646 char state_name[256];
647 struct var_sym *vs;
649 FOR_EACH_PTR(links, tmp) {
650 state = get_state_slist(pre_slist, compare_id, tmp, NULL);
651 if (!state || !state->data)
652 continue;
653 data = state->data;
654 right_comparison = data->comparison;
655 right_var = data->var2;
656 right_vsl = data->vsl2;
657 if (chunk_vsl_eq(mid_var, mid_vsl, right_var, right_vsl)) {
658 right_var = data->var1;
659 right_vsl = data->vsl1;
660 right_comparison = flip_op(right_comparison);
662 true_comparison = combine_comparisons(left_comparison, right_comparison);
663 false_comparison = combine_comparisons(falsify_op(left_comparison), right_comparison);
665 if (strcmp(left_var, right_var) > 0) {
666 const char *tmp_var = left_var;
667 struct var_sym_list *tmp_vsl = left_vsl;
669 left_var = right_var;
670 left_vsl = right_vsl;
671 right_var = tmp_var;
672 right_vsl = tmp_vsl;
673 true_comparison = flip_op(true_comparison);
674 false_comparison = flip_op(false_comparison);
677 if (!true_comparison && !false_comparison)
678 continue;
680 if (true_comparison)
681 true_state = alloc_compare_state(left_var, left_vsl, true_comparison, right_var, right_vsl);
682 else
683 true_state = NULL;
684 if (false_comparison)
685 false_state = alloc_compare_state(left_var, left_vsl, false_comparison, right_var, right_vsl);
686 else
687 false_state = NULL;
689 snprintf(state_name, sizeof(state_name), "%s vs %s", left_var, right_var);
690 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
691 FOR_EACH_PTR(left_vsl, vs) {
692 save_link_var_sym(vs->var, vs->sym, state_name);
693 } END_FOR_EACH_PTR(vs);
694 FOR_EACH_PTR(right_vsl, vs) {
695 save_link_var_sym(vs->var, vs->sym, state_name);
696 } END_FOR_EACH_PTR(vs);
697 if (!vsl_to_sym(left_vsl))
698 save_link_var_sym(left_var, NULL, state_name);
699 if (!vsl_to_sym(right_vsl))
700 save_link_var_sym(right_var, NULL, state_name);
701 } END_FOR_EACH_PTR(tmp);
704 static void update_tf_data(struct state_list *pre_slist,
705 const char *left_name, struct symbol *left_sym,
706 const char *right_name, struct symbol *right_sym,
707 struct compare_data *tdata)
709 struct smatch_state *state;
711 state = get_state_slist(pre_slist, link_id, tdata->var2, vsl_to_sym(tdata->vsl2));
712 if (state)
713 update_tf_links(pre_slist, tdata->var1, tdata->vsl1, tdata->comparison, tdata->var2, tdata->vsl2, state->data);
715 state = get_state_slist(pre_slist, link_id, tdata->var1, vsl_to_sym(tdata->vsl1));
716 if (state)
717 update_tf_links(pre_slist, tdata->var2, tdata->vsl2, flip_op(tdata->comparison), tdata->var1, tdata->vsl1, state->data);
720 static void match_compare(struct expression *expr)
722 char *left = NULL;
723 char *right = NULL;
724 struct symbol *left_sym, *right_sym;
725 struct var_sym_list *left_vsl, *right_vsl;
726 int op, false_op;
727 struct smatch_state *true_state, *false_state;
728 char state_name[256];
729 struct state_list *pre_slist;
731 if (expr->type != EXPR_COMPARE)
732 return;
733 left = chunk_to_var_sym(expr->left, &left_sym);
734 if (!left)
735 goto free;
736 left_vsl = expr_to_vsl(expr->left);
737 right = chunk_to_var_sym(expr->right, &right_sym);
738 if (!right)
739 goto free;
740 right_vsl = expr_to_vsl(expr->right);
742 if (strcmp(left, right) > 0) {
743 struct symbol *tmp_sym = left_sym;
744 char *tmp_name = left;
745 struct var_sym_list *tmp_vsl = left_vsl;
747 left = right;
748 left_sym = right_sym;
749 left_vsl = right_vsl;
750 right = tmp_name;
751 right_sym = tmp_sym;
752 right_vsl = tmp_vsl;
753 op = flip_op(expr->op);
754 } else {
755 op = expr->op;
757 false_op = falsify_op(op);
758 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
759 true_state = alloc_compare_state(left, left_vsl, op, right, right_vsl);
760 false_state = alloc_compare_state(left, left_vsl, false_op, right, right_vsl);
762 pre_slist = clone_slist(__get_cur_slist());
763 update_tf_data(pre_slist, left, left_sym, right, right_sym, true_state->data);
764 free_slist(&pre_slist);
766 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
767 save_link(expr->left, state_name);
768 save_link(expr->right, state_name);
769 free:
770 free_string(left);
771 free_string(right);
774 static void add_comparison_var_sym(const char *left_name,
775 struct var_sym_list *left_vsl,
776 int comparison,
777 const char *right_name, struct var_sym_list *right_vsl)
779 struct smatch_state *state;
780 struct var_sym *vs;
781 char state_name[256];
783 if (strcmp(left_name, right_name) > 0) {
784 const char *tmp_name = left_name;
785 struct var_sym_list *tmp_vsl = left_vsl;
787 left_name = right_name;
788 left_vsl = right_vsl;
789 right_name = tmp_name;
790 right_vsl = tmp_vsl;
791 comparison = flip_op(comparison);
793 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
794 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
796 set_state(compare_id, state_name, NULL, state);
798 FOR_EACH_PTR(left_vsl, vs) {
799 save_link_var_sym(vs->var, vs->sym, state_name);
800 } END_FOR_EACH_PTR(vs);
801 FOR_EACH_PTR(right_vsl, vs) {
802 save_link_var_sym(vs->var, vs->sym, state_name);
803 } END_FOR_EACH_PTR(vs);
806 static void add_comparison(struct expression *left, int comparison, struct expression *right)
808 char *left_name = NULL;
809 char *right_name = NULL;
810 struct symbol *left_sym, *right_sym;
811 struct var_sym_list *left_vsl, *right_vsl;
812 struct smatch_state *state;
813 char state_name[256];
815 left_name = chunk_to_var_sym(left, &left_sym);
816 if (!left_name)
817 goto free;
818 left_vsl = expr_to_vsl(left);
819 right_name = chunk_to_var_sym(right, &right_sym);
820 if (!right_name)
821 goto free;
822 right_vsl = expr_to_vsl(right);
824 if (strcmp(left_name, right_name) > 0) {
825 struct symbol *tmp_sym = left_sym;
826 char *tmp_name = left_name;
827 struct var_sym_list *tmp_vsl = left_vsl;
829 left_name = right_name;
830 left_sym = right_sym;
831 left_vsl = right_vsl;
832 right_name = tmp_name;
833 right_sym = tmp_sym;
834 right_vsl = tmp_vsl;
835 comparison = flip_op(comparison);
837 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
838 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
840 set_state(compare_id, state_name, NULL, state);
841 save_link(left, state_name);
842 save_link(right, state_name);
844 free:
845 free_string(left_name);
846 free_string(right_name);
849 static void match_assign_add(struct expression *expr)
851 struct expression *right;
852 struct expression *r_left, *r_right;
853 sval_t left_tmp, right_tmp;
855 right = strip_expr(expr->right);
856 r_left = strip_expr(right->left);
857 r_right = strip_expr(right->right);
859 get_absolute_min(r_left, &left_tmp);
860 get_absolute_min(r_right, &right_tmp);
862 if (left_tmp.value > 0)
863 add_comparison(expr->left, '>', r_right);
864 else if (left_tmp.value == 0)
865 add_comparison(expr->left, SPECIAL_GTE, r_right);
867 if (right_tmp.value > 0)
868 add_comparison(expr->left, '>', r_left);
869 else if (right_tmp.value == 0)
870 add_comparison(expr->left, SPECIAL_GTE, r_left);
873 static void match_assign_sub(struct expression *expr)
875 struct expression *right;
876 struct expression *r_left, *r_right;
877 int comparison;
878 sval_t min;
880 right = strip_expr(expr->right);
881 r_left = strip_expr(right->left);
882 r_right = strip_expr(right->right);
884 if (get_absolute_min(r_right, &min) && sval_is_negative(min))
885 return;
887 comparison = get_comparison(r_left, r_right);
889 switch (comparison) {
890 case '>':
891 case SPECIAL_GTE:
892 if (implied_not_equal(r_right, 0))
893 add_comparison(expr->left, '>', r_left);
894 else
895 add_comparison(expr->left, SPECIAL_GTE, r_left);
896 return;
900 static void match_assign_divide(struct expression *expr)
902 struct expression *right;
903 struct expression *r_left, *r_right;
904 sval_t min;
906 right = strip_expr(expr->right);
907 r_left = strip_expr(right->left);
908 r_right = strip_expr(right->right);
909 if (!get_implied_min(r_right, &min) || min.value <= 1)
910 return;
912 add_comparison(expr->left, '<', r_left);
915 static void match_binop_assign(struct expression *expr)
917 struct expression *right;
919 right = strip_expr(expr->right);
920 if (right->op == '+')
921 match_assign_add(expr);
922 if (right->op == '-')
923 match_assign_sub(expr);
924 if (right->op == '/')
925 match_assign_divide(expr);
928 static void copy_comparisons(struct expression *left, struct expression *right)
930 struct string_list *links;
931 struct smatch_state *state;
932 struct compare_data *data;
933 struct symbol *left_sym, *right_sym;
934 char *left_var = NULL;
935 char *right_var = NULL;
936 struct var_sym_list *left_vsl;
937 const char *var;
938 struct var_sym_list *vsl;
939 int comparison;
940 char *tmp;
942 left_var = chunk_to_var_sym(left, &left_sym);
943 if (!left_var)
944 goto done;
945 left_vsl = expr_to_vsl(left);
946 right_var = chunk_to_var_sym(right, &right_sym);
947 if (!right_var)
948 goto done;
950 state = get_state(link_id, right_var, right_sym);
951 if (!state)
952 return;
953 links = state->data;
955 FOR_EACH_PTR(links, tmp) {
956 state = get_state(compare_id, tmp, NULL);
957 if (!state || !state->data)
958 continue;
959 data = state->data;
960 comparison = data->comparison;
961 var = data->var2;
962 vsl = data->vsl2;
963 if (chunk_vsl_eq(var, vsl, right_var, NULL)) {
964 var = data->var1;
965 vsl = data->vsl1;
966 comparison = flip_op(comparison);
968 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
969 } END_FOR_EACH_PTR(tmp);
971 done:
972 free_string(right_var);
975 static void match_assign(struct expression *expr)
977 struct expression *right;
979 if (expr->op != '=')
980 return;
982 copy_comparisons(expr->left, expr->right);
983 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
985 right = strip_expr(expr->right);
986 if (right->type == EXPR_BINOP)
987 match_binop_assign(expr);
990 static int get_comparison_strings(char *one, char *two)
992 char buf[256];
993 struct smatch_state *state;
994 int invert = 0;
995 int ret = 0;
997 if (strcmp(one, two) > 0) {
998 char *tmp = one;
1000 one = two;
1001 two = tmp;
1002 invert = 1;
1005 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1006 state = get_state(compare_id, buf, NULL);
1007 if (state)
1008 ret = state_to_comparison(state);
1010 if (invert)
1011 ret = flip_op(ret);
1013 return ret;
1016 int get_comparison(struct expression *a, struct expression *b)
1018 char *one = NULL;
1019 char *two = NULL;
1020 int ret = 0;
1022 one = chunk_to_var(a);
1023 if (!one)
1024 goto free;
1025 two = chunk_to_var(b);
1026 if (!two)
1027 goto free;
1029 ret = get_comparison_strings(one, two);
1030 free:
1031 free_string(one);
1032 free_string(two);
1033 return ret;
1036 static void update_links_from_call(struct expression *left,
1037 int left_compare,
1038 struct expression *right)
1040 struct string_list *links;
1041 struct smatch_state *state;
1042 struct compare_data *data;
1043 struct symbol *left_sym, *right_sym;
1044 char *left_var = NULL;
1045 char *right_var = NULL;
1046 struct var_sym_list *left_vsl;
1047 const char *var;
1048 struct var_sym_list *vsl;
1049 int comparison;
1050 char *tmp;
1052 left_var = chunk_to_var_sym(left, &left_sym);
1053 if (!left_var)
1054 goto done;
1055 left_vsl = expr_to_vsl(left);
1056 right_var = chunk_to_var_sym(right, &right_sym);
1057 if (!right_var)
1058 goto done;
1060 state = get_state(link_id, right_var, right_sym);
1061 if (!state)
1062 return;
1063 links = state->data;
1065 FOR_EACH_PTR(links, tmp) {
1066 state = get_state(compare_id, tmp, NULL);
1067 if (!state || !state->data)
1068 continue;
1069 data = state->data;
1070 comparison = data->comparison;
1071 var = data->var2;
1072 vsl = data->vsl2;
1073 if (chunk_vsl_eq(var, vsl, right_var, NULL)) {
1074 var = data->var1;
1075 vsl = data->vsl1;
1076 comparison = flip_op(comparison);
1078 comparison = combine_comparisons(left_compare, comparison);
1079 if (!comparison)
1080 continue;
1081 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1082 } END_FOR_EACH_PTR(tmp);
1084 done:
1085 free_string(right_var);
1088 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
1090 struct expression *arg;
1091 int comparison;
1092 const char *c = range;
1094 if (!str_to_comparison_arg(c, call, &comparison, &arg))
1095 return;
1096 update_links_from_call(expr, comparison, arg);
1097 add_comparison(expr, comparison, arg);
1100 static char *range_comparison_to_param_helper(struct expression *expr, char starts_with)
1102 struct symbol *param;
1103 char *var = NULL;
1104 char buf[256];
1105 char *ret_str = NULL;
1106 int compare;
1107 int i;
1109 var = chunk_to_var(expr);
1110 if (!var)
1111 goto free;
1113 i = -1;
1114 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1115 i++;
1116 if (!param->ident)
1117 continue;
1118 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1119 compare = get_comparison_strings(var, buf);
1120 if (!compare)
1121 continue;
1122 if (show_special(compare)[0] != starts_with)
1123 continue;
1124 snprintf(buf, sizeof(buf), "[%sp%d]", show_special(compare), i);
1125 ret_str = alloc_sname(buf);
1126 break;
1127 } END_FOR_EACH_PTR(param);
1129 free:
1130 free_string(var);
1131 return ret_str;
1134 char *expr_equal_to_param(struct expression *expr)
1136 return range_comparison_to_param_helper(expr, '=');
1139 char *expr_lte_to_param(struct expression *expr)
1141 return range_comparison_to_param_helper(expr, '<');
1144 static void free_data(struct symbol *sym)
1146 if (__inline_fn)
1147 return;
1148 clear_compare_data_alloc();
1151 void register_comparison(int id)
1153 compare_id = id;
1154 add_hook(&match_compare, CONDITION_HOOK);
1155 add_hook(&match_assign, ASSIGNMENT_HOOK);
1156 add_hook(&save_start_states, AFTER_DEF_HOOK);
1157 add_unmatched_state_hook(compare_id, unmatched_comparison);
1158 add_merge_hook(compare_id, &merge_compare_states);
1159 add_hook(&free_data, AFTER_FUNC_HOOK);
1162 void register_comparison_links(int id)
1164 link_id = id;
1165 add_merge_hook(link_id, &merge_links);
1166 add_modification_hook(link_id, &match_modify);