2 * smatch/smatch_comparison.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
11 * The point here is to store the relationships between two variables.
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
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.
24 #include "smatch_extra.h"
25 #include "smatch_slist.h"
27 static int compare_id
;
32 struct var_sym_list
*vsl1
;
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)
46 static struct symbol
*vsl_to_sym(struct var_sym_list
*vsl
)
52 if (ptr_list_size((struct ptr_list
*)vsl
) != 1)
54 vs
= first_ptr_list((struct ptr_list
*)vsl
);
58 static struct smatch_state
*alloc_compare_state(
59 const char *var1
, struct var_sym_list
*vsl1
,
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
);
78 static int state_to_comparison(struct smatch_state
*state
)
80 if (!state
|| !state
->data
)
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
)
95 case SPECIAL_UNSIGNED_LT
:
96 return SPECIAL_UNSIGNED_GT
;
99 case SPECIAL_UNSIGNED_LTE
:
100 return SPECIAL_UNSIGNED_GTE
;
102 return SPECIAL_EQUAL
;
103 case SPECIAL_NOTEQUAL
:
104 return SPECIAL_NOTEQUAL
;
107 case SPECIAL_UNSIGNED_GTE
:
108 return SPECIAL_UNSIGNED_LTE
;
111 case SPECIAL_UNSIGNED_GT
:
112 return SPECIAL_UNSIGNED_LT
;
114 sm_msg("internal smatch bug. unhandled comparison %d", op
);
119 static int falsify_op(int op
)
126 case SPECIAL_UNSIGNED_LT
:
127 return SPECIAL_UNSIGNED_GTE
;
130 case SPECIAL_UNSIGNED_LTE
:
131 return SPECIAL_UNSIGNED_GT
;
133 return SPECIAL_NOTEQUAL
;
134 case SPECIAL_NOTEQUAL
:
135 return SPECIAL_EQUAL
;
138 case SPECIAL_UNSIGNED_GTE
:
139 return SPECIAL_UNSIGNED_LT
;
142 case SPECIAL_UNSIGNED_GT
:
143 return SPECIAL_UNSIGNED_LTE
;
145 sm_msg("internal smatch bug. unhandled comparison %d", 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
)
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)
169 if (sval_cmp(left_max
, right_min
) == 0)
171 if (sval_cmp(left_min
, right_max
) > 0)
173 if (sval_cmp(left_min
, right_max
) == 0)
179 static struct range_list
*get_orig_rl(struct var_sym_list
*vsl
)
182 struct smatch_state
*state
;
186 sym
= vsl_to_sym(vsl
);
187 if (!sym
|| !sym
->ident
)
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
;
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
))
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
))
212 op
= rl_comparison(left_rl
, right_rl
);
214 return alloc_compare_state(data
->var1
, data
->vsl1
, op
, data
->var2
, data
->vsl2
);
219 /* remove_unsigned_from_comparison() is obviously a hack. */
220 static int remove_unsigned_from_comparison(int op
)
223 case SPECIAL_UNSIGNED_LT
:
225 case SPECIAL_UNSIGNED_LTE
:
227 case SPECIAL_UNSIGNED_GTE
:
229 case SPECIAL_UNSIGNED_GT
:
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
)
244 one
= remove_unsigned_from_comparison(one
);
245 two
= remove_unsigned_from_comparison(two
);
292 return SPECIAL_NOTEQUAL
;
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
)
315 left_compare
= remove_unsigned_from_comparison(left_compare
);
316 right_compare
= remove_unsigned_from_comparison(right_compare
);
320 switch (left_compare
) {
329 return right_compare
;
338 switch (right_compare
) {
370 static struct smatch_state
*merge_compare_states(struct smatch_state
*s1
, struct smatch_state
*s2
)
372 struct compare_data
*data
= s1
->data
;
375 op
= merge_comparisons(state_to_comparison(s1
), state_to_comparison(s2
));
377 return alloc_compare_state(data
->var1
, data
->vsl1
, op
, data
->var2
, data
->vsl2
);
381 struct smatch_state
*alloc_link_state(struct string_list
*links
)
383 struct smatch_state
*state
;
384 static char buf
[256];
388 state
= __alloc_smatch_state(0);
391 FOR_EACH_PTR(links
, tmp
) {
393 snprintf(buf
, sizeof(buf
), "%s", tmp
);
395 append(buf
, ", ", sizeof(buf
));
396 append(buf
, tmp
, sizeof(buf
));
398 } END_FOR_EACH_PTR(tmp
);
400 state
->name
= alloc_sname(buf
);
405 static void save_start_states(struct statement
*stmt
)
407 struct symbol
*param
;
409 char state_name
[128];
410 struct smatch_state
*state
;
411 struct string_list
*links
;
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
;
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
);
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
);
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
;
451 old_state
= get_state(link_id
, var
, sym
);
453 links
= clone_str_list(old_state
->data
);
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
;
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
)) {
478 case SPECIAL_UNSIGNED_GTE
:
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);
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
;
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
)) {
508 case SPECIAL_UNSIGNED_LTE
:
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);
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
)
528 if (mod_expr
->type
!= EXPR_PREOP
&& mod_expr
->type
!= EXPR_POSTOP
)
531 if (mod_expr
->op
== SPECIAL_INCREMENT
) {
535 if (mod_expr
->op
== SPECIAL_DECREMENT
) {
542 static void match_modify(struct sm_state
*sm
, struct expression
*mod_expr
)
544 struct string_list
*links
;
547 if (match_inc_dec(sm
, mod_expr
))
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
;
564 expr
= strip_expr(expr
);
570 name
= expr_to_var_sym(expr
, &tmp
);
579 if (expr
->type
!= EXPR_BINOP
)
581 if (expr
->op
!= '-' && expr
->op
!= '+')
584 left_name
= expr_to_var(expr
->left
);
587 right_name
= expr_to_var(expr
->right
);
589 free_string(left_name
);
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
)
608 expr
= strip_expr(expr
);
609 if (expr
->type
== EXPR_BINOP
) {
612 chunk
= chunk_to_var(expr
);
616 save_link(expr
->left
, link
);
617 save_link(expr
->right
, link
);
618 save_link_var_sym(chunk
, NULL
, link
);
622 var
= expr_to_var_sym(expr
, &sym
);
626 save_link_var_sym(var
, sym
, link
);
631 static void update_tf_links(struct state_list
*pre_slist
,
632 const char *left_var
, struct var_sym_list
*left_vsl
,
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
;
644 int false_comparison
;
646 char state_name
[256];
649 FOR_EACH_PTR(links
, tmp
) {
650 state
= get_state_slist(pre_slist
, compare_id
, tmp
, NULL
);
651 if (!state
|| !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
;
673 true_comparison
= flip_op(true_comparison
);
674 false_comparison
= flip_op(false_comparison
);
677 if (!true_comparison
&& !false_comparison
)
681 true_state
= alloc_compare_state(left_var
, left_vsl
, true_comparison
, right_var
, right_vsl
);
684 if (false_comparison
)
685 false_state
= alloc_compare_state(left_var
, left_vsl
, false_comparison
, right_var
, right_vsl
);
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
));
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
));
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
)
724 struct symbol
*left_sym
, *right_sym
;
725 struct var_sym_list
*left_vsl
, *right_vsl
;
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
)
733 left
= chunk_to_var_sym(expr
->left
, &left_sym
);
736 left_vsl
= expr_to_vsl(expr
->left
);
737 right
= chunk_to_var_sym(expr
->right
, &right_sym
);
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
;
748 left_sym
= right_sym
;
749 left_vsl
= right_vsl
;
753 op
= flip_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
);
774 static void add_comparison_var_sym(const char *left_name
,
775 struct var_sym_list
*left_vsl
,
777 const char *right_name
, struct var_sym_list
*right_vsl
)
779 struct smatch_state
*state
;
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
;
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
);
818 left_vsl
= expr_to_vsl(left
);
819 right_name
= chunk_to_var_sym(right
, &right_sym
);
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
;
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
);
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
;
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
))
887 comparison
= get_comparison(r_left
, r_right
);
889 switch (comparison
) {
892 if (implied_not_equal(r_right
, 0))
893 add_comparison(expr
->left
, '>', r_left
);
895 add_comparison(expr
->left
, SPECIAL_GTE
, r_left
);
900 static void match_assign_divide(struct expression
*expr
)
902 struct expression
*right
;
903 struct expression
*r_left
, *r_right
;
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)
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
;
938 struct var_sym_list
*vsl
;
942 left_var
= chunk_to_var_sym(left
, &left_sym
);
945 left_vsl
= expr_to_vsl(left
);
946 right_var
= chunk_to_var_sym(right
, &right_sym
);
950 state
= get_state(link_id
, right_var
, right_sym
);
955 FOR_EACH_PTR(links
, tmp
) {
956 state
= get_state(compare_id
, tmp
, NULL
);
957 if (!state
|| !state
->data
)
960 comparison
= data
->comparison
;
963 if (chunk_vsl_eq(var
, vsl
, right_var
, NULL
)) {
966 comparison
= flip_op(comparison
);
968 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
969 } END_FOR_EACH_PTR(tmp
);
972 free_string(right_var
);
975 static void match_assign(struct expression
*expr
)
977 struct expression
*right
;
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
)
993 struct smatch_state
*state
;
997 if (strcmp(one
, two
) > 0) {
1005 snprintf(buf
, sizeof(buf
), "%s vs %s", one
, two
);
1006 state
= get_state(compare_id
, buf
, NULL
);
1008 ret
= state_to_comparison(state
);
1016 int get_comparison(struct expression
*a
, struct expression
*b
)
1022 one
= chunk_to_var(a
);
1025 two
= chunk_to_var(b
);
1029 ret
= get_comparison_strings(one
, two
);
1036 static void update_links_from_call(struct expression
*left
,
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
;
1048 struct var_sym_list
*vsl
;
1052 left_var
= chunk_to_var_sym(left
, &left_sym
);
1055 left_vsl
= expr_to_vsl(left
);
1056 right_var
= chunk_to_var_sym(right
, &right_sym
);
1060 state
= get_state(link_id
, right_var
, right_sym
);
1063 links
= state
->data
;
1065 FOR_EACH_PTR(links
, tmp
) {
1066 state
= get_state(compare_id
, tmp
, NULL
);
1067 if (!state
|| !state
->data
)
1070 comparison
= data
->comparison
;
1073 if (chunk_vsl_eq(var
, vsl
, right_var
, NULL
)) {
1076 comparison
= flip_op(comparison
);
1078 comparison
= combine_comparisons(left_compare
, comparison
);
1081 add_comparison_var_sym(left_var
, left_vsl
, comparison
, var
, vsl
);
1082 } END_FOR_EACH_PTR(tmp
);
1085 free_string(right_var
);
1088 void __add_comparison_info(struct expression
*expr
, struct expression
*call
, const char *range
)
1090 struct expression
*arg
;
1092 const char *c
= range
;
1094 if (!str_to_comparison_arg(c
, call
, &comparison
, &arg
))
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
;
1105 char *ret_str
= NULL
;
1109 var
= chunk_to_var(expr
);
1114 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, param
) {
1118 snprintf(buf
, sizeof(buf
), "%s orig", param
->ident
->name
);
1119 compare
= get_comparison_strings(var
, buf
);
1122 if (show_special(compare
)[0] != starts_with
)
1124 snprintf(buf
, sizeof(buf
), "[%sp%d]", show_special(compare
), i
);
1125 ret_str
= alloc_sname(buf
);
1127 } END_FOR_EACH_PTR(param
);
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
)
1148 clear_compare_data_alloc();
1151 void register_comparison(int 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
)
1165 add_merge_hook(link_id
, &merge_links
);
1166 add_modification_hook(link_id
, &match_modify
);