modification_hooks: fix fall out from recent changes
[smatch.git] / smatch_comparison.c
blobabf9ee36972c18fab68d8c8565d402ef1282e53c
1 /*
2 * Copyright (C) 2012 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 * The point here is to store the relationships between two variables.
20 * Ie: y > x.
21 * To do that we create a state with the two variables in alphabetical order:
22 * ->name = "x vs y" and the state would be "<". On the false path the state
23 * would be ">=".
25 * Part of the trick of it is that if x or y is modified then we need to reset
26 * the state. We need to keep a list of all the states which depend on x and
27 * all the states which depend on y. The link_id code handles this.
31 #include "smatch.h"
32 #include "smatch_extra.h"
33 #include "smatch_slist.h"
35 static int compare_id;
36 static int link_id;
37 static int inc_dec_id;
38 static int inc_dec_link_id;
40 /* for handling for loops */
41 STATE(start);
42 STATE(incremented);
44 ALLOCATOR(compare_data, "compare data");
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_comparison() reverses the op left and right. So "x >= y" becomes "y <= x".
88 int flip_comparison(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 int negate_comparison(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 if (one == two)
248 return one;
250 LT = EQ = GT = 0;
252 switch (one) {
253 case '<':
254 LT = 1;
255 break;
256 case SPECIAL_LTE:
257 LT = 1;
258 EQ = 1;
259 break;
260 case SPECIAL_EQUAL:
261 EQ = 1;
262 break;
263 case SPECIAL_GTE:
264 GT = 1;
265 EQ = 1;
266 break;
267 case '>':
268 GT = 1;
271 switch (two) {
272 case '<':
273 LT = 1;
274 break;
275 case SPECIAL_LTE:
276 LT = 1;
277 EQ = 1;
278 break;
279 case SPECIAL_EQUAL:
280 EQ = 1;
281 break;
282 case SPECIAL_GTE:
283 GT = 1;
284 EQ = 1;
285 break;
286 case '>':
287 GT = 1;
290 if (LT && EQ && GT)
291 return 0;
292 if (LT && EQ)
293 return SPECIAL_LTE;
294 if (LT && GT)
295 return SPECIAL_NOTEQUAL;
296 if (LT)
297 return '<';
298 if (EQ && GT)
299 return SPECIAL_GTE;
300 if (GT)
301 return '>';
302 return 0;
306 * This is for if you have "a < b" and "b <= c". Or in other words,
307 * "a < b <= c". You would call this like get_combined_comparison('<', '<=').
308 * The return comparison would be '<'.
310 * This function is different from merge_comparisons(), for example:
311 * merge_comparison('<', '==') returns '<='
312 * get_combined_comparison('<', '==') returns '<'
314 static int combine_comparisons(int left_compare, int right_compare)
316 int LT, EQ, GT;
318 left_compare = remove_unsigned_from_comparison(left_compare);
319 right_compare = remove_unsigned_from_comparison(right_compare);
321 LT = EQ = GT = 0;
323 switch (left_compare) {
324 case '<':
325 LT++;
326 break;
327 case SPECIAL_LTE:
328 LT++;
329 EQ++;
330 break;
331 case SPECIAL_EQUAL:
332 return right_compare;
333 case SPECIAL_GTE:
334 GT++;
335 EQ++;
336 break;
337 case '>':
338 GT++;
341 switch (right_compare) {
342 case '<':
343 LT++;
344 break;
345 case SPECIAL_LTE:
346 LT++;
347 EQ++;
348 break;
349 case SPECIAL_EQUAL:
350 return left_compare;
351 case SPECIAL_GTE:
352 GT++;
353 EQ++;
354 break;
355 case '>':
356 GT++;
359 if (LT == 2) {
360 if (EQ == 2)
361 return SPECIAL_LTE;
362 return '<';
365 if (GT == 2) {
366 if (EQ == 2)
367 return SPECIAL_GTE;
368 return '>';
370 return 0;
373 static int filter_comparison(int orig, int op)
375 if (orig == op)
376 return orig;
378 switch (orig) {
379 case 0:
380 return op;
381 case '<':
382 switch (op) {
383 case '<':
384 case SPECIAL_LTE:
385 case SPECIAL_NOTEQUAL:
386 return '<';
388 return 0;
389 case SPECIAL_LTE:
390 switch (op) {
391 case '<':
392 case SPECIAL_LTE:
393 case SPECIAL_EQUAL:
394 return op;
395 case SPECIAL_NOTEQUAL:
396 return '<';
398 return 0;
399 case SPECIAL_EQUAL:
400 switch (op) {
401 case SPECIAL_LTE:
402 case SPECIAL_EQUAL:
403 case SPECIAL_GTE:
404 case SPECIAL_UNSIGNED_LTE:
405 case SPECIAL_UNSIGNED_GTE:
406 return SPECIAL_EQUAL;
408 return 0;
409 case SPECIAL_NOTEQUAL:
410 switch (op) {
411 case '<':
412 case SPECIAL_LTE:
413 return '<';
414 case SPECIAL_UNSIGNED_LT:
415 case SPECIAL_UNSIGNED_LTE:
416 return SPECIAL_UNSIGNED_LT;
417 case SPECIAL_NOTEQUAL:
418 return op;
419 case '>':
420 case SPECIAL_GTE:
421 return '>';
422 case SPECIAL_UNSIGNED_GT:
423 case SPECIAL_UNSIGNED_GTE:
424 return SPECIAL_UNSIGNED_GT;
426 return 0;
427 case SPECIAL_GTE:
428 switch (op) {
429 case SPECIAL_LTE:
430 return SPECIAL_EQUAL;
431 case '>':
432 case SPECIAL_GTE:
433 case SPECIAL_EQUAL:
434 return op;
435 case SPECIAL_NOTEQUAL:
436 return '>';
438 return 0;
439 case '>':
440 switch (op) {
441 case '>':
442 case SPECIAL_GTE:
443 case SPECIAL_NOTEQUAL:
444 return '>';
446 return 0;
447 case SPECIAL_UNSIGNED_LT:
448 switch (op) {
449 case SPECIAL_UNSIGNED_LT:
450 case SPECIAL_UNSIGNED_LTE:
451 case SPECIAL_NOTEQUAL:
452 return SPECIAL_UNSIGNED_LT;
454 return 0;
455 case SPECIAL_UNSIGNED_LTE:
456 switch (op) {
457 case SPECIAL_UNSIGNED_LT:
458 case SPECIAL_UNSIGNED_LTE:
459 case SPECIAL_EQUAL:
460 return op;
461 case SPECIAL_NOTEQUAL:
462 return SPECIAL_UNSIGNED_LT;
463 case SPECIAL_UNSIGNED_GTE:
464 return SPECIAL_EQUAL;
466 return 0;
467 case SPECIAL_UNSIGNED_GTE:
468 switch (op) {
469 case SPECIAL_UNSIGNED_LTE:
470 return SPECIAL_EQUAL;
471 case SPECIAL_NOTEQUAL:
472 return SPECIAL_UNSIGNED_GT;
473 case SPECIAL_EQUAL:
474 case SPECIAL_UNSIGNED_GTE:
475 case SPECIAL_UNSIGNED_GT:
476 return op;
478 return 0;
479 case SPECIAL_UNSIGNED_GT:
480 switch (op) {
481 case SPECIAL_UNSIGNED_GT:
482 case SPECIAL_UNSIGNED_GTE:
483 case SPECIAL_NOTEQUAL:
484 return SPECIAL_UNSIGNED_GT;
486 return 0;
488 return 0;
491 static struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
493 struct compare_data *data = s1->data;
494 int op;
496 op = merge_comparisons(state_to_comparison(s1), state_to_comparison(s2));
497 if (op)
498 return alloc_compare_state(data->var1, data->vsl1, op, data->var2, data->vsl2);
499 return &undefined;
502 static struct smatch_state *alloc_link_state(struct string_list *links)
504 struct smatch_state *state;
505 static char buf[256];
506 char *tmp;
507 int i;
509 state = __alloc_smatch_state(0);
511 i = 0;
512 FOR_EACH_PTR(links, tmp) {
513 if (!i++) {
514 snprintf(buf, sizeof(buf), "%s", tmp);
515 } else {
516 append(buf, ", ", sizeof(buf));
517 append(buf, tmp, sizeof(buf));
519 } END_FOR_EACH_PTR(tmp);
521 state->name = alloc_sname(buf);
522 state->data = links;
523 return state;
526 static void save_start_states(struct statement *stmt)
528 struct symbol *param;
529 char orig[64];
530 char state_name[128];
531 struct smatch_state *state;
532 struct string_list *links;
533 char *link;
535 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
536 struct var_sym_list *vsl1 = NULL;
537 struct var_sym_list *vsl2 = NULL;
539 if (!param->ident)
540 continue;
541 snprintf(orig, sizeof(orig), "%s orig", param->ident->name);
542 snprintf(state_name, sizeof(state_name), "%s vs %s", param->ident->name, orig);
543 add_var_sym(&vsl1, param->ident->name, param);
544 add_var_sym(&vsl2, orig, param);
545 state = alloc_compare_state(param->ident->name, vsl1, SPECIAL_EQUAL, alloc_sname(orig), vsl2);
546 set_state(compare_id, state_name, NULL, state);
548 link = alloc_sname(state_name);
549 links = NULL;
550 insert_string(&links, link);
551 state = alloc_link_state(links);
552 set_state(link_id, param->ident->name, param, state);
553 } END_FOR_EACH_PTR(param);
556 static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
558 struct smatch_state *ret;
559 struct string_list *links;
561 links = combine_string_lists(s1->data, s2->data);
562 ret = alloc_link_state(links);
563 return ret;
566 static void save_link_var_sym(const char *var, struct symbol *sym, const char *link)
568 struct smatch_state *old_state, *new_state;
569 struct string_list *links;
570 char *new;
572 old_state = get_state(link_id, var, sym);
573 if (old_state)
574 links = clone_str_list(old_state->data);
575 else
576 links = NULL;
578 new = alloc_sname(link);
579 insert_string(&links, new);
581 new_state = alloc_link_state(links);
582 set_state(link_id, var, sym, new_state);
585 static void match_inc(struct sm_state *sm)
587 struct string_list *links;
588 struct smatch_state *state;
589 char *tmp;
591 links = sm->state->data;
593 FOR_EACH_PTR(links, tmp) {
594 state = get_state(compare_id, tmp, NULL);
596 switch (state_to_comparison(state)) {
597 case SPECIAL_EQUAL:
598 case SPECIAL_GTE:
599 case SPECIAL_UNSIGNED_GTE:
600 case '>':
601 case SPECIAL_UNSIGNED_GT: {
602 struct compare_data *data = state->data;
603 struct smatch_state *new;
605 new = alloc_compare_state(data->var1, data->vsl1, '>', data->var2, data->vsl2);
606 set_state(compare_id, tmp, NULL, new);
607 break;
609 default:
610 set_state(compare_id, tmp, NULL, &undefined);
612 } END_FOR_EACH_PTR(tmp);
615 static void match_dec(struct sm_state *sm)
617 struct string_list *links;
618 struct smatch_state *state;
619 char *tmp;
621 links = sm->state->data;
623 FOR_EACH_PTR(links, tmp) {
624 state = get_state(compare_id, tmp, NULL);
626 switch (state_to_comparison(state)) {
627 case SPECIAL_EQUAL:
628 case SPECIAL_LTE:
629 case SPECIAL_UNSIGNED_LTE:
630 case '<':
631 case SPECIAL_UNSIGNED_LT: {
632 struct compare_data *data = state->data;
633 struct smatch_state *new;
635 new = alloc_compare_state(data->var1, data->vsl1, '<', data->var2, data->vsl2);
636 set_state(compare_id, tmp, NULL, new);
637 break;
639 default:
640 set_state(compare_id, tmp, NULL, &undefined);
642 } END_FOR_EACH_PTR(tmp);
645 static int match_inc_dec(struct sm_state *sm, struct expression *mod_expr)
647 if (!mod_expr)
648 return 0;
649 if (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP)
650 return 0;
652 if (mod_expr->op == SPECIAL_INCREMENT) {
653 match_inc(sm);
654 return 1;
656 if (mod_expr->op == SPECIAL_DECREMENT) {
657 match_dec(sm);
658 return 1;
660 return 0;
663 static void match_modify(struct sm_state *sm, struct expression *mod_expr)
665 struct string_list *links;
666 char *tmp;
669 * if (foo > bar) then ++foo is also > bar.
671 if (match_inc_dec(sm, mod_expr))
672 return;
674 links = sm->state->data;
676 FOR_EACH_PTR(links, tmp) {
677 set_state(compare_id, tmp, NULL, &undefined);
678 } END_FOR_EACH_PTR(tmp);
679 set_state(link_id, sm->name, sm->sym, &undefined);
682 static char *chunk_to_var_sym(struct expression *expr, struct symbol **sym)
684 char *name, *left_name, *right_name;
685 struct symbol *tmp;
686 char buf[128];
688 expr = strip_expr(expr);
689 if (!expr)
690 return NULL;
691 if (sym)
692 *sym = NULL;
694 if (expr->type == EXPR_PREOP &&
695 (expr->op == SPECIAL_INCREMENT ||
696 expr->op == SPECIAL_DECREMENT))
697 expr = strip_expr(expr->unop);
699 name = expr_to_var_sym(expr, &tmp);
700 if (name && tmp) {
701 if (sym)
702 *sym = tmp;
703 return name;
705 if (name)
706 free_string(name);
708 if (expr->type != EXPR_BINOP)
709 return NULL;
710 if (expr->op != '-' && expr->op != '+')
711 return NULL;
713 left_name = expr_to_var(expr->left);
714 if (!left_name)
715 return NULL;
716 right_name = expr_to_var(expr->right);
717 if (!right_name) {
718 free_string(left_name);
719 return NULL;
721 snprintf(buf, sizeof(buf), "%s %s %s", left_name, show_special(expr->op), right_name);
722 free_string(left_name);
723 free_string(right_name);
724 return alloc_string(buf);
727 static char *chunk_to_var(struct expression *expr)
729 return chunk_to_var_sym(expr, NULL);
732 static void save_link(struct expression *expr, char *link)
734 char *var;
735 struct symbol *sym;
737 expr = strip_expr(expr);
738 if (expr->type == EXPR_BINOP) {
739 char *chunk;
741 chunk = chunk_to_var(expr);
742 if (!chunk)
743 return;
745 save_link(expr->left, link);
746 save_link(expr->right, link);
747 save_link_var_sym(chunk, NULL, link);
748 return;
751 var = expr_to_var_sym(expr, &sym);
752 if (!var || !sym)
753 goto done;
755 save_link_var_sym(var, sym, link);
756 done:
757 free_string(var);
760 static int get_orig_comparison(struct stree *pre_stree, const char *left, const char *right)
762 struct smatch_state *state;
763 struct compare_data *data;
764 int flip = 0;
765 char state_name[256];
767 if (strcmp(left, right) > 0) {
768 const char *tmp = right;
770 flip = 1;
771 right = left;
772 left = tmp;
775 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
776 state = get_state_stree(pre_stree, compare_id, state_name, NULL);
777 if (!state || !state->data)
778 return 0;
779 data = state->data;
780 if (flip)
781 return flip_comparison(data->comparison);
782 return data->comparison;
787 * The idea here is that we take a comparison "a < b" and then we look at all
788 * the things which "b" is compared against "b < c" and we say that that implies
789 * a relationship "a < c".
791 * The names here about because the comparisons are organized like this
792 * "a < b < c".
795 static void update_tf_links(struct stree *pre_stree,
796 const char *left_var, struct var_sym_list *left_vsl,
797 int left_comparison, int left_false_comparison,
798 const char *mid_var, struct var_sym_list *mid_vsl,
799 struct string_list *links)
801 struct smatch_state *state;
802 struct smatch_state *true_state, *false_state;
803 struct compare_data *data;
804 const char *right_var;
805 struct var_sym_list *right_vsl;
806 int orig_comparison;
807 int right_comparison;
808 int true_comparison;
809 int false_comparison;
810 char *tmp;
811 char state_name[256];
812 struct var_sym *vs;
814 FOR_EACH_PTR(links, tmp) {
815 state = get_state_stree(pre_stree, compare_id, tmp, NULL);
816 if (!state || !state->data)
817 continue;
818 data = state->data;
819 right_comparison = data->comparison;
820 right_var = data->var2;
821 right_vsl = data->vsl2;
822 if (strcmp(mid_var, right_var) == 0) {
823 right_var = data->var1;
824 right_vsl = data->vsl1;
825 right_comparison = flip_comparison(right_comparison);
827 if (strcmp(left_var, right_var) == 0)
828 continue;
830 orig_comparison = get_orig_comparison(pre_stree, left_var, right_var);
832 true_comparison = combine_comparisons(left_comparison, right_comparison);
833 false_comparison = combine_comparisons(left_false_comparison, right_comparison);
835 true_comparison = filter_comparison(orig_comparison, true_comparison);
836 false_comparison = filter_comparison(orig_comparison, false_comparison);
838 if (strcmp(left_var, right_var) > 0) {
839 const char *tmp_var = left_var;
840 struct var_sym_list *tmp_vsl = left_vsl;
842 left_var = right_var;
843 left_vsl = right_vsl;
844 right_var = tmp_var;
845 right_vsl = tmp_vsl;
846 true_comparison = flip_comparison(true_comparison);
847 false_comparison = flip_comparison(false_comparison);
850 if (!true_comparison && !false_comparison)
851 continue;
853 if (true_comparison)
854 true_state = alloc_compare_state(left_var, left_vsl, true_comparison, right_var, right_vsl);
855 else
856 true_state = NULL;
857 if (false_comparison)
858 false_state = alloc_compare_state(left_var, left_vsl, false_comparison, right_var, right_vsl);
859 else
860 false_state = NULL;
862 snprintf(state_name, sizeof(state_name), "%s vs %s", left_var, right_var);
863 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
864 FOR_EACH_PTR(left_vsl, vs) {
865 save_link_var_sym(vs->var, vs->sym, state_name);
866 } END_FOR_EACH_PTR(vs);
867 FOR_EACH_PTR(right_vsl, vs) {
868 save_link_var_sym(vs->var, vs->sym, state_name);
869 } END_FOR_EACH_PTR(vs);
870 if (!vsl_to_sym(left_vsl))
871 save_link_var_sym(left_var, NULL, state_name);
872 if (!vsl_to_sym(right_vsl))
873 save_link_var_sym(right_var, NULL, state_name);
874 } END_FOR_EACH_PTR(tmp);
877 static void update_tf_data(struct stree *pre_stree,
878 const char *left_name, struct var_sym_list *left_vsl,
879 const char *right_name, struct var_sym_list *right_vsl,
880 int true_comparison, int false_comparison)
882 struct smatch_state *state;
884 state = get_state_stree(pre_stree, link_id, right_name, vsl_to_sym(right_vsl));
885 if (state)
886 update_tf_links(pre_stree, left_name, left_vsl, true_comparison, false_comparison, right_name, right_vsl, state->data);
888 state = get_state_stree(pre_stree, link_id, left_name, vsl_to_sym(left_vsl));
889 if (state)
890 update_tf_links(pre_stree, right_name, right_vsl, flip_comparison(true_comparison), flip_comparison(false_comparison), left_name, left_vsl, state->data);
893 static void iter_modify(struct sm_state *sm, struct expression *mod_expr)
895 if (sm->state != &start ||
896 !mod_expr ||
897 (mod_expr->type != EXPR_PREOP && mod_expr->type != EXPR_POSTOP) ||
898 mod_expr->op != SPECIAL_INCREMENT)
899 set_state(inc_dec_id, sm->name, sm->sym, &undefined);
900 else
901 set_state(inc_dec_id, sm->name, sm->sym, &incremented);
904 static void handle_for_loops(struct expression *expr, char *state_name, struct smatch_state *false_state)
906 sval_t sval;
907 char *iter_name, *cap_name;
908 struct symbol *iter_sym, *cap_sym;
909 struct compare_data *data;
911 if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
912 return;
914 if (!__cur_stmt || !__prev_stmt)
915 return;
916 if (__cur_stmt->type != STMT_ITERATOR)
917 return;
918 if (__cur_stmt->iterator_pre_condition != expr)
919 return;
921 /* literals are handled in smatch_extra.c */
922 if (get_value(expr->right, &sval))
923 return;
925 /* First time checking the condition */
926 if (__prev_stmt == __cur_stmt->iterator_pre_statement) {
927 if (!get_implied_value(expr->left, &sval) ||
928 sval.value != 0)
929 return;
931 iter_name = expr_to_var_sym(expr->left, &iter_sym);
932 cap_name = expr_to_var_sym(expr->right, &cap_sym);
933 if (!iter_name || !cap_name || !iter_sym || !cap_sym) {
934 free_string(iter_name);
935 free_string(cap_name);
936 return;
939 set_state(inc_dec_id, iter_name, iter_sym, &start);
940 store_link(inc_dec_link_id, cap_name, cap_sym, iter_name, iter_sym);
942 free_string(iter_name);
943 free_string(cap_name);
944 return;
947 /* Second time checking the condtion */
948 if (__prev_stmt != __cur_stmt->iterator_post_statement)
949 return;
951 if (get_state_expr(inc_dec_id, expr->left) != &incremented)
952 return;
954 data = false_state->data;
955 false_state = alloc_compare_state(data->var1, data->vsl1, SPECIAL_EQUAL, data->var2, data->vsl2);
957 set_true_false_states(compare_id, state_name, NULL, NULL, false_state);
960 static void handle_comparison(struct expression *expr_left, int op, struct expression *expr_right, char **_state_name, struct smatch_state **_false_state)
962 char *left = NULL;
963 char *right = NULL;
964 struct symbol *left_sym, *right_sym;
965 struct var_sym_list *left_vsl, *right_vsl;
966 int false_op;
967 int orig_comparison;
968 struct smatch_state *true_state, *false_state;
969 static char state_name[256];
970 struct stree *pre_stree;
971 sval_t sval;
973 false_op = negate_comparison(op);
975 if (op == SPECIAL_UNSIGNED_LT &&
976 get_implied_value(expr_left, &sval) &&
977 sval.value == 0)
978 false_op = SPECIAL_EQUAL;
980 if (op == SPECIAL_UNSIGNED_GT &&
981 get_implied_value(expr_right, &sval) &&
982 sval.value == 0)
983 false_op = SPECIAL_EQUAL;
985 left = chunk_to_var_sym(expr_left, &left_sym);
986 if (!left)
987 goto free;
988 left_vsl = expr_to_vsl(expr_left);
989 right = chunk_to_var_sym(expr_right, &right_sym);
990 if (!right)
991 goto free;
992 right_vsl = expr_to_vsl(expr_right);
994 if (strcmp(left, right) > 0) {
995 struct symbol *tmp_sym = left_sym;
996 char *tmp_name = left;
997 struct var_sym_list *tmp_vsl = left_vsl;
999 left = right;
1000 left_sym = right_sym;
1001 left_vsl = right_vsl;
1002 right = tmp_name;
1003 right_sym = tmp_sym;
1004 right_vsl = tmp_vsl;
1005 op = flip_comparison(op);
1006 false_op = flip_comparison(false_op);
1009 orig_comparison = get_comparison_strings(left, right);
1010 op = filter_comparison(orig_comparison, op);
1011 false_op = filter_comparison(orig_comparison, false_op);
1013 snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
1014 true_state = alloc_compare_state(left, left_vsl, op, right, right_vsl);
1015 false_state = alloc_compare_state(left, left_vsl, false_op, right, right_vsl);
1017 pre_stree = clone_stree(__get_cur_stree());
1018 update_tf_data(pre_stree, left, left_vsl, right, right_vsl, op, false_op);
1019 free_stree(&pre_stree);
1021 set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
1022 save_link(expr_left, state_name);
1023 save_link(expr_right, state_name);
1025 if (_false_state)
1026 *_false_state = false_state;
1027 if (_state_name)
1028 *_state_name = state_name;
1029 free:
1030 free_string(left);
1031 free_string(right);
1035 void __comparison_match_condition(struct expression *expr)
1037 struct expression *left, *right, *new_left, *new_right;
1038 struct smatch_state *false_state = NULL;
1039 char *state_name = NULL;
1041 if (expr->type != EXPR_COMPARE)
1042 return;
1044 handle_comparison(expr->left, expr->op, expr->right, &state_name, &false_state);
1045 if (false_state && state_name)
1046 handle_for_loops(expr, state_name, false_state);
1049 left = strip_parens(expr->left);
1050 right = strip_parens(expr->right);
1052 if (left->type == EXPR_BINOP && left->op == '+') {
1053 new_left = left->left;
1054 new_right = binop_expression(right, '-', left->right);
1055 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1057 new_left = left->right;
1058 new_right = binop_expression(right, '-', left->left);
1059 handle_comparison(new_left, expr->op, new_right, NULL, NULL);
1063 static void add_comparison_var_sym(const char *left_name,
1064 struct var_sym_list *left_vsl,
1065 int comparison,
1066 const char *right_name, struct var_sym_list *right_vsl)
1068 struct smatch_state *state;
1069 struct var_sym *vs;
1070 char state_name[256];
1072 if (strcmp(left_name, right_name) > 0) {
1073 const char *tmp_name = left_name;
1074 struct var_sym_list *tmp_vsl = left_vsl;
1076 left_name = right_name;
1077 left_vsl = right_vsl;
1078 right_name = tmp_name;
1079 right_vsl = tmp_vsl;
1080 comparison = flip_comparison(comparison);
1082 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1083 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1085 set_state(compare_id, state_name, NULL, state);
1087 FOR_EACH_PTR(left_vsl, vs) {
1088 save_link_var_sym(vs->var, vs->sym, state_name);
1089 } END_FOR_EACH_PTR(vs);
1090 FOR_EACH_PTR(right_vsl, vs) {
1091 save_link_var_sym(vs->var, vs->sym, state_name);
1092 } END_FOR_EACH_PTR(vs);
1095 static void add_comparison(struct expression *left, int comparison, struct expression *right)
1097 char *left_name = NULL;
1098 char *right_name = NULL;
1099 struct symbol *left_sym, *right_sym;
1100 struct var_sym_list *left_vsl, *right_vsl;
1101 struct smatch_state *state;
1102 char state_name[256];
1104 left_name = chunk_to_var_sym(left, &left_sym);
1105 if (!left_name)
1106 goto free;
1107 left_vsl = expr_to_vsl(left);
1108 right_name = chunk_to_var_sym(right, &right_sym);
1109 if (!right_name)
1110 goto free;
1111 right_vsl = expr_to_vsl(right);
1113 if (strcmp(left_name, right_name) > 0) {
1114 struct symbol *tmp_sym = left_sym;
1115 char *tmp_name = left_name;
1116 struct var_sym_list *tmp_vsl = left_vsl;
1118 left_name = right_name;
1119 left_sym = right_sym;
1120 left_vsl = right_vsl;
1121 right_name = tmp_name;
1122 right_sym = tmp_sym;
1123 right_vsl = tmp_vsl;
1124 comparison = flip_comparison(comparison);
1126 snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1127 state = alloc_compare_state(left_name, left_vsl, comparison, right_name, right_vsl);
1129 set_state(compare_id, state_name, NULL, state);
1130 save_link(left, state_name);
1131 save_link(right, state_name);
1133 free:
1134 free_string(left_name);
1135 free_string(right_name);
1138 static void match_assign_add(struct expression *expr)
1140 struct expression *right;
1141 struct expression *r_left, *r_right;
1142 sval_t left_tmp, right_tmp;
1144 right = strip_expr(expr->right);
1145 r_left = strip_expr(right->left);
1146 r_right = strip_expr(right->right);
1148 get_absolute_min(r_left, &left_tmp);
1149 get_absolute_min(r_right, &right_tmp);
1151 if (left_tmp.value > 0)
1152 add_comparison(expr->left, '>', r_right);
1153 else if (left_tmp.value == 0)
1154 add_comparison(expr->left, SPECIAL_GTE, r_right);
1156 if (right_tmp.value > 0)
1157 add_comparison(expr->left, '>', r_left);
1158 else if (right_tmp.value == 0)
1159 add_comparison(expr->left, SPECIAL_GTE, r_left);
1162 static void match_assign_sub(struct expression *expr)
1164 struct expression *right;
1165 struct expression *r_left, *r_right;
1166 int comparison;
1167 sval_t min;
1169 right = strip_expr(expr->right);
1170 r_left = strip_expr(right->left);
1171 r_right = strip_expr(right->right);
1173 if (get_absolute_min(r_right, &min) && sval_is_negative(min))
1174 return;
1176 comparison = get_comparison(r_left, r_right);
1178 switch (comparison) {
1179 case '>':
1180 case SPECIAL_GTE:
1181 if (implied_not_equal(r_right, 0))
1182 add_comparison(expr->left, '>', r_left);
1183 else
1184 add_comparison(expr->left, SPECIAL_GTE, r_left);
1185 return;
1189 static void match_assign_divide(struct expression *expr)
1191 struct expression *right;
1192 struct expression *r_left, *r_right;
1193 sval_t min;
1195 right = strip_expr(expr->right);
1196 r_left = strip_expr(right->left);
1197 r_right = strip_expr(right->right);
1198 if (!get_implied_min(r_right, &min) || min.value <= 1)
1199 return;
1201 add_comparison(expr->left, '<', r_left);
1204 static void match_binop_assign(struct expression *expr)
1206 struct expression *right;
1208 right = strip_expr(expr->right);
1209 if (right->op == '+')
1210 match_assign_add(expr);
1211 if (right->op == '-')
1212 match_assign_sub(expr);
1213 if (right->op == '/')
1214 match_assign_divide(expr);
1217 static void copy_comparisons(struct expression *left, struct expression *right)
1219 struct string_list *links;
1220 struct smatch_state *state;
1221 struct compare_data *data;
1222 struct symbol *left_sym, *right_sym;
1223 char *left_var = NULL;
1224 char *right_var = NULL;
1225 struct var_sym_list *left_vsl;
1226 const char *var;
1227 struct var_sym_list *vsl;
1228 int comparison;
1229 char *tmp;
1231 left_var = chunk_to_var_sym(left, &left_sym);
1232 if (!left_var)
1233 goto done;
1234 left_vsl = expr_to_vsl(left);
1235 right_var = chunk_to_var_sym(right, &right_sym);
1236 if (!right_var)
1237 goto done;
1239 state = get_state(link_id, right_var, right_sym);
1240 if (!state)
1241 return;
1242 links = state->data;
1244 FOR_EACH_PTR(links, tmp) {
1245 state = get_state(compare_id, tmp, NULL);
1246 if (!state || !state->data)
1247 continue;
1248 data = state->data;
1249 comparison = data->comparison;
1250 var = data->var2;
1251 vsl = data->vsl2;
1252 if (strcmp(var, right_var) == 0) {
1253 var = data->var1;
1254 vsl = data->vsl1;
1255 comparison = flip_comparison(comparison);
1257 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1258 } END_FOR_EACH_PTR(tmp);
1260 done:
1261 free_string(right_var);
1264 static void match_assign(struct expression *expr)
1266 struct expression *right;
1268 if (expr->op != '=')
1269 return;
1270 if (__in_fake_assign || outside_of_function())
1271 return;
1273 if (is_struct(expr->left))
1274 return;
1276 copy_comparisons(expr->left, expr->right);
1277 add_comparison(expr->left, SPECIAL_EQUAL, expr->right);
1279 right = strip_expr(expr->right);
1280 if (right->type == EXPR_BINOP)
1281 match_binop_assign(expr);
1284 int get_comparison_strings(const char *one, const char *two)
1286 char buf[256];
1287 struct smatch_state *state;
1288 int invert = 0;
1289 int ret = 0;
1291 if (strcmp(one, two) == 0)
1292 return SPECIAL_EQUAL;
1294 if (strcmp(one, two) > 0) {
1295 const char *tmp = one;
1297 one = two;
1298 two = tmp;
1299 invert = 1;
1302 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1303 state = get_state(compare_id, buf, NULL);
1304 if (state)
1305 ret = state_to_comparison(state);
1307 if (invert)
1308 ret = flip_comparison(ret);
1310 return ret;
1313 int get_comparison(struct expression *a, struct expression *b)
1315 char *one = NULL;
1316 char *two = NULL;
1317 int ret = 0;
1319 one = chunk_to_var(a);
1320 if (!one)
1321 goto free;
1322 two = chunk_to_var(b);
1323 if (!two)
1324 goto free;
1326 ret = get_comparison_strings(one, two);
1327 free:
1328 free_string(one);
1329 free_string(two);
1330 return ret;
1333 int possible_comparison(struct expression *a, int comparison, struct expression *b)
1335 char *one = NULL;
1336 char *two = NULL;
1337 int ret = 0;
1338 char buf[256];
1339 struct sm_state *sm;
1340 int saved;
1342 one = chunk_to_var(a);
1343 if (!one)
1344 goto free;
1345 two = chunk_to_var(b);
1346 if (!two)
1347 goto free;
1350 if (strcmp(one, two) == 0 && comparison == SPECIAL_EQUAL) {
1351 ret = 1;
1352 goto free;
1355 if (strcmp(one, two) > 0) {
1356 char *tmp = one;
1358 one = two;
1359 two = tmp;
1360 comparison = flip_comparison(comparison);
1363 snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1364 sm = get_sm_state(compare_id, buf, NULL);
1365 if (!sm)
1366 goto free;
1368 FOR_EACH_PTR(sm->possible, sm) {
1369 if (!sm->state->data)
1370 continue;
1371 saved = ((struct compare_data *)sm->state->data)->comparison;
1372 if (saved == comparison)
1373 ret = 1;
1374 if (comparison == SPECIAL_EQUAL &&
1375 (saved == SPECIAL_LTE ||
1376 saved == SPECIAL_GTE ||
1377 saved == SPECIAL_UNSIGNED_LTE ||
1378 saved == SPECIAL_UNSIGNED_GTE))
1379 ret = 1;
1380 if (ret == 1)
1381 goto free;
1382 } END_FOR_EACH_PTR(sm);
1384 return ret;
1385 free:
1386 free_string(one);
1387 free_string(two);
1388 return ret;
1391 struct state_list *get_all_comparisons(struct expression *expr)
1393 struct smatch_state *state;
1394 struct string_list *links;
1395 struct state_list *ret = NULL;
1396 struct sm_state *sm;
1397 char *tmp;
1399 state = get_state_expr(link_id, expr);
1400 if (!state)
1401 return NULL;
1402 links = state->data;
1404 FOR_EACH_PTR(links, tmp) {
1405 sm = get_sm_state(compare_id, tmp, NULL);
1406 if (!sm)
1407 continue;
1408 // FIXME have to compare name with vsl
1409 add_ptr_list(&ret, sm);
1410 } END_FOR_EACH_PTR(tmp);
1412 return ret;
1415 struct state_list *get_all_possible_equal_comparisons(struct expression *expr)
1417 struct smatch_state *state;
1418 struct string_list *links;
1419 struct state_list *ret = NULL;
1420 struct sm_state *sm;
1421 char *tmp;
1423 state = get_state_expr(link_id, expr);
1424 if (!state)
1425 return NULL;
1426 links = state->data;
1428 FOR_EACH_PTR(links, tmp) {
1429 sm = get_sm_state(compare_id, tmp, NULL);
1430 if (!sm)
1431 continue;
1432 if (!strchr(sm->state->name, '='))
1433 continue;
1434 if (strcmp(sm->state->name, "!=") == 0)
1435 continue;
1436 add_ptr_list(&ret, sm);
1437 } END_FOR_EACH_PTR(tmp);
1439 return ret;
1442 static void update_links_from_call(struct expression *left,
1443 int left_compare,
1444 struct expression *right)
1446 struct string_list *links;
1447 struct smatch_state *state;
1448 struct compare_data *data;
1449 struct symbol *left_sym, *right_sym;
1450 char *left_var = NULL;
1451 char *right_var = NULL;
1452 struct var_sym_list *left_vsl;
1453 const char *var;
1454 struct var_sym_list *vsl;
1455 int comparison;
1456 char *tmp;
1458 left_var = chunk_to_var_sym(left, &left_sym);
1459 if (!left_var)
1460 goto done;
1461 left_vsl = expr_to_vsl(left);
1462 right_var = chunk_to_var_sym(right, &right_sym);
1463 if (!right_var)
1464 goto done;
1466 state = get_state(link_id, right_var, right_sym);
1467 if (!state)
1468 return;
1469 links = state->data;
1471 FOR_EACH_PTR(links, tmp) {
1472 state = get_state(compare_id, tmp, NULL);
1473 if (!state || !state->data)
1474 continue;
1475 data = state->data;
1476 comparison = data->comparison;
1477 var = data->var2;
1478 vsl = data->vsl2;
1479 if (strcmp(var, right_var) == 0) {
1480 var = data->var1;
1481 vsl = data->vsl1;
1482 comparison = flip_comparison(comparison);
1484 comparison = combine_comparisons(left_compare, comparison);
1485 if (!comparison)
1486 continue;
1487 add_comparison_var_sym(left_var, left_vsl, comparison, var, vsl);
1488 } END_FOR_EACH_PTR(tmp);
1490 done:
1491 free_string(right_var);
1494 void __add_comparison_info(struct expression *expr, struct expression *call, const char *range)
1496 struct expression *arg;
1497 int comparison;
1498 const char *c = range;
1500 if (!str_to_comparison_arg(c, call, &comparison, &arg))
1501 return;
1502 update_links_from_call(expr, comparison, arg);
1503 add_comparison(expr, comparison, arg);
1506 static char *range_comparison_to_param_helper(struct expression *expr, char starts_with, int ignore)
1508 struct symbol *param;
1509 char *var = NULL;
1510 char buf[256];
1511 char *ret_str = NULL;
1512 int compare;
1513 int i;
1515 var = chunk_to_var(expr);
1516 if (!var)
1517 goto free;
1519 i = -1;
1520 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1521 i++;
1522 if (i == ignore)
1523 continue;
1524 if (!param->ident)
1525 continue;
1526 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1527 compare = get_comparison_strings(var, buf);
1528 if (!compare)
1529 continue;
1530 if (show_special(compare)[0] != starts_with)
1531 continue;
1532 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1533 ret_str = alloc_sname(buf);
1534 break;
1535 } END_FOR_EACH_PTR(param);
1537 free:
1538 free_string(var);
1539 return ret_str;
1542 char *name_sym_to_param_comparison(const char *name, struct symbol *sym)
1544 struct symbol *param;
1545 char buf[256];
1546 int compare;
1547 int i;
1549 i = -1;
1550 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1551 i++;
1552 if (!param->ident)
1553 continue;
1554 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1555 compare = get_comparison_strings(name, buf);
1556 if (!compare)
1557 continue;
1558 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1559 return alloc_sname(buf);
1560 } END_FOR_EACH_PTR(param);
1562 return NULL;
1565 char *expr_equal_to_param(struct expression *expr, int ignore)
1567 return range_comparison_to_param_helper(expr, '=', ignore);
1570 char *expr_lte_to_param(struct expression *expr, int ignore)
1572 return range_comparison_to_param_helper(expr, '<', ignore);
1575 char *expr_param_comparison(struct expression *expr, int ignore)
1577 struct symbol *param;
1578 char *var = NULL;
1579 char buf[256];
1580 char *ret_str = NULL;
1581 int compare;
1582 int i;
1584 var = chunk_to_var(expr);
1585 if (!var)
1586 goto free;
1588 i = -1;
1589 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, param) {
1590 i++;
1591 if (i == ignore)
1592 continue;
1593 if (!param->ident)
1594 continue;
1595 snprintf(buf, sizeof(buf), "%s orig", param->ident->name);
1596 compare = get_comparison_strings(var, buf);
1597 if (!compare)
1598 continue;
1599 snprintf(buf, sizeof(buf), "[%s$%d]", show_special(compare), i);
1600 ret_str = alloc_sname(buf);
1601 break;
1602 } END_FOR_EACH_PTR(param);
1604 free:
1605 free_string(var);
1606 return ret_str;
1609 static void match_call_info(struct expression *expr)
1611 struct expression *arg;
1612 struct smatch_state *state;
1613 struct sm_state *sm;
1614 struct compare_data *data;
1615 int comparison;
1616 struct string_list *links;
1617 char *arg_name;
1618 const char *right_name;
1619 char *link;
1620 char info_buf[256];
1621 int i;
1623 i = -1;
1624 FOR_EACH_PTR(expr->args, arg) {
1625 i++;
1627 state = get_state_expr(link_id, arg);
1628 if (!state)
1629 continue;
1631 links = state->data;
1632 FOR_EACH_PTR(links, link) {
1633 if (strstr(link, " orig"))
1634 continue;
1635 sm = get_sm_state(compare_id, link, NULL);
1636 if (!sm)
1637 continue;
1638 data = sm->state->data;
1639 if (!data || !data->comparison)
1640 continue;
1641 arg_name = expr_to_var(arg);
1642 if (!arg_name)
1643 continue;
1645 if (strcmp(data->var1, arg_name) == 0) {
1646 comparison = data->comparison;
1647 right_name = data->var2;
1648 } else if (strcmp(data->var2, arg_name) == 0) {
1649 comparison = flip_comparison(data->comparison);
1650 right_name = data->var1;
1651 } else {
1652 goto free;
1655 snprintf(info_buf, sizeof(info_buf), "%s %s", show_special(comparison), right_name);
1656 sql_insert_caller_info(expr, PARAM_COMPARE, i, "$", info_buf);
1658 free:
1659 free_string(arg_name);
1660 } END_FOR_EACH_PTR(link);
1661 } END_FOR_EACH_PTR(arg);
1664 static void free_data(struct symbol *sym)
1666 if (__inline_fn)
1667 return;
1668 clear_compare_data_alloc();
1671 void register_comparison(int id)
1673 compare_id = id;
1674 add_hook(&save_start_states, AFTER_DEF_HOOK);
1675 add_unmatched_state_hook(compare_id, unmatched_comparison);
1676 add_merge_hook(compare_id, &merge_compare_states);
1677 add_hook(&free_data, AFTER_FUNC_HOOK);
1678 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1681 void register_comparison_late(int id)
1683 add_hook(&match_assign, ASSIGNMENT_HOOK);
1686 void register_comparison_links(int id)
1688 link_id = id;
1689 add_merge_hook(link_id, &merge_links);
1690 add_modification_hook_late(link_id, &match_modify);
1693 void register_comparison_inc_dec(int id)
1695 inc_dec_id = id;
1696 add_modification_hook_late(inc_dec_id, &iter_modify);
1699 void register_comparison_inc_dec_links(int id)
1701 inc_dec_link_id = id;
1702 set_up_link_functions(inc_dec_id, inc_dec_link_id);
1705 static void filter_by_sm(struct sm_state *sm, int op,
1706 struct stree_stack **true_stack,
1707 struct stree_stack **false_stack)
1709 struct compare_data *data;
1710 int istrue = 0;
1711 int isfalse = 0;
1713 if (!sm)
1714 return;
1715 data = sm->state->data;
1716 if (!data) {
1717 if (sm->merged) {
1718 filter_by_sm(sm->left, op, true_stack, false_stack);
1719 filter_by_sm(sm->right, op, true_stack, false_stack);
1721 return;
1724 if (data->comparison &&
1725 data->comparison == filter_comparison(data->comparison, op))
1726 istrue = 1;
1728 if (data->comparison &&
1729 data->comparison == filter_comparison(data->comparison, negate_comparison(op)))
1730 isfalse = 1;
1732 if (istrue)
1733 add_pool(true_stack, sm->pool);
1734 if (isfalse)
1735 add_pool(false_stack, sm->pool);
1737 if (sm->merged) {
1738 filter_by_sm(sm->left, op, true_stack, false_stack);
1739 filter_by_sm(sm->right, op, true_stack, false_stack);
1743 struct sm_state *comparison_implication_hook(struct expression *expr,
1744 struct stree_stack **true_stack,
1745 struct stree_stack **false_stack)
1747 struct sm_state *sm;
1748 char *left, *right;
1749 int op;
1750 static char buf[256];
1752 if (expr->type != EXPR_COMPARE)
1753 return NULL;
1755 op = expr->op;
1757 left = expr_to_var(expr->left);
1758 right = expr_to_var(expr->right);
1759 if (!left || !right) {
1760 free_string(left);
1761 free_string(right);
1762 return NULL;
1765 if (strcmp(left, right) > 0) {
1766 char *tmp = left;
1768 left = right;
1769 right = tmp;
1770 op = flip_comparison(op);
1773 snprintf(buf, sizeof(buf), "%s vs %s", left, right);
1774 sm = get_sm_state(compare_id, buf, NULL);
1775 if (!sm)
1776 return NULL;
1777 if (!sm->merged)
1778 return NULL;
1780 filter_by_sm(sm, op, true_stack, false_stack);
1781 if (!*true_stack && !*false_stack)
1782 return NULL;
1784 return sm;