extra: modify match_comparison() so it can deal with range comparisons
[smatch.git] / smatch_extra.c
blob53f1e9e7370311e813f46071a1b98f8cdd0fe0a5
1 /*
2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * smatch_extra.c is supposed to track the value of every variable.
15 #include <stdlib.h>
16 #include <errno.h>
17 #ifndef __USE_ISOC99
18 #define __USE_ISOC99
19 #endif
20 #include <limits.h>
21 #include "parse.h"
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
26 static int my_id;
28 struct string_list *__ignored_macros = NULL;
29 static int in_warn_on_macro()
31 struct statement *stmt;
32 char *tmp;
33 char *macro;
35 stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
36 macro = get_macro_name(&stmt->pos);
37 if (!macro)
38 return 0;
40 FOR_EACH_PTR(__ignored_macros, tmp) {
41 if (!strcmp(tmp, macro))
42 return 1;
43 } END_FOR_EACH_PTR(tmp);
44 return 0;
47 struct sm_state *set_extra_mod(const char *name, struct symbol *sym, struct smatch_state *state)
49 if (in_warn_on_macro())
50 return NULL;
51 remove_from_equiv(name, sym);
52 return set_state(SMATCH_EXTRA, name, sym, state);
55 struct sm_state *set_extra_expr_mod(struct expression *expr, struct smatch_state *state)
57 if (in_warn_on_macro())
58 return NULL;
59 remove_from_equiv_expr(expr);
60 return set_state_expr(SMATCH_EXTRA, expr, state);
64 * This is for return_implies_state() hooks which modify a SMATCH_EXTRA state
66 void set_extra_expr_nomod(struct expression *expr, struct smatch_state *state)
68 struct relation *rel;
69 struct smatch_state *orig_state;
71 orig_state = get_state_expr(SMATCH_EXTRA, expr);
73 if (!orig_state || !get_dinfo(orig_state)->related) {
74 set_state_expr(SMATCH_EXTRA, expr, state);
75 return;
78 FOR_EACH_PTR(get_dinfo(orig_state)->related, rel) {
79 sm_msg("setting %s to %s", rel->name, state->name);
80 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
81 add_equiv(state, rel->name, rel->sym);
82 } END_FOR_EACH_PTR(rel);
85 static void set_extra_true_false(const char *name, struct symbol *sym,
86 struct smatch_state *true_state,
87 struct smatch_state *false_state)
89 struct relation *rel;
90 struct smatch_state *orig_state;
92 if (in_warn_on_macro())
93 return;
95 orig_state = get_state(SMATCH_EXTRA, name, sym);
97 if (!orig_state || !get_dinfo(orig_state)->related) {
98 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
99 return;
102 // FIXME!!!! equiv => related
103 FOR_EACH_PTR(get_dinfo(orig_state)->related, rel) {
104 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
105 true_state, false_state);
106 if (true_state)
107 add_equiv(true_state, rel->name, rel->sym);
108 if (false_state)
109 add_equiv(false_state, rel->name, rel->sym);
110 } END_FOR_EACH_PTR(rel);
113 static void set_extra_expr_true_false(struct expression *expr,
114 struct smatch_state *true_state,
115 struct smatch_state *false_state)
117 char *name;
118 struct symbol *sym;
120 expr = strip_expr(expr);
121 name = get_variable_from_expr(expr, &sym);
122 if (!name || !sym)
123 goto free;
124 set_extra_true_false(name, sym, true_state, false_state);
125 free:
126 free_string(name);
129 struct data_info *get_dinfo(struct smatch_state *state)
131 if (!state)
132 return NULL;
133 return (struct data_info *)state->data;
137 struct smatch_state *filter_range(struct smatch_state *orig,
138 long long filter_min, long long filter_max)
140 struct smatch_state *ret;
141 struct data_info *orig_info;
142 struct data_info *ret_info;
144 if (!orig)
145 orig = extra_undefined();
146 orig_info = get_dinfo(orig);
147 ret = alloc_extra_state_empty();
148 ret_info = get_dinfo(ret);
149 ret_info->value_ranges = remove_range(orig_info->value_ranges, filter_min, filter_max);
150 ret->name = show_ranges(ret_info->value_ranges);
151 return ret;
154 struct smatch_state *add_filter(struct smatch_state *orig, long long num)
156 return filter_range(orig, num, num);
159 static struct smatch_state *merge_func(const char *name, struct symbol *sym,
160 struct smatch_state *s1,
161 struct smatch_state *s2)
163 struct data_info *info1 = get_dinfo(s1);
164 struct data_info *info2 = get_dinfo(s2);
165 struct data_info *ret_info;
166 struct smatch_state *tmp;
167 struct range_list *value_ranges;
168 struct relation *rel;
169 struct relation *new;
171 value_ranges = range_list_union(info1->value_ranges, info2->value_ranges);
172 tmp = alloc_extra_state_empty();
173 ret_info = get_dinfo(tmp);
174 ret_info->value_ranges = value_ranges;
175 tmp->name = show_ranges(ret_info->value_ranges);
176 FOR_EACH_PTR(info1->related, rel) {
177 new = get_common_relationship(info2, rel->op, rel->name, rel->sym);
178 if (new)
179 add_related(tmp, new->op, new->name, new->sym);
180 } END_FOR_EACH_PTR(rel);
181 return tmp;
184 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
186 struct expression *iter_var;
187 struct expression *condition;
188 struct sm_state *sm;
189 long long start;
191 condition = strip_expr(loop->iterator_pre_condition);
192 if (!condition)
193 return NULL;
194 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
195 return NULL;
196 if (condition->op != SPECIAL_DECREMENT)
197 return NULL;
199 iter_var = condition->unop;
200 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
201 if (!sm)
202 return NULL;
203 if (get_dinfo_min(get_dinfo(sm->state)) < 0)
204 return NULL;
205 start = get_dinfo_max(get_dinfo(sm->state));
206 if (start <= 0)
207 return NULL;
208 if (start != whole_range.max)
209 start--;
211 if (condition->type == EXPR_PREOP)
212 set_extra_expr_mod(iter_var, alloc_extra_state_range(1, start));
213 if (condition->type == EXPR_POSTOP)
214 set_extra_expr_mod(iter_var, alloc_extra_state_range(0, start));
215 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
218 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
219 struct expression *condition)
221 struct expression *iter_var;
222 struct sm_state *sm;
223 long long start;
224 long long end;
226 iter_var = iter_expr->unop;
227 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
228 if (!sm)
229 return NULL;
230 if (!get_single_value_from_dinfo(get_dinfo(sm->state), &start))
231 return NULL;
232 if (!get_implied_value(condition->right, &end))
233 end = whole_range.max;
234 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
235 return NULL;
237 switch (condition->op) {
238 case SPECIAL_NOTEQUAL:
239 case '<':
240 if (end != whole_range.max)
241 end--;
242 break;
243 case SPECIAL_LTE:
244 break;
245 default:
246 return NULL;
248 if (end < start)
249 return NULL;
250 set_extra_expr_mod(iter_var, alloc_extra_state_range(start, end));
251 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
254 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
255 struct expression *condition)
257 struct expression *iter_var;
258 struct sm_state *sm;
259 long long start;
260 long long end;
262 iter_var = iter_expr->unop;
263 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
264 if (!sm)
265 return NULL;
266 if (!get_single_value_from_dinfo(get_dinfo(sm->state), &start))
267 return NULL;
268 if (!get_implied_value(condition->right, &end))
269 end = whole_range.max;
270 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
271 return NULL;
273 switch (condition->op) {
274 case SPECIAL_NOTEQUAL:
275 case '>':
276 if (end != whole_range.min)
277 end++;
278 break;
279 case SPECIAL_GTE:
280 break;
281 default:
282 return NULL;
284 if (end > start)
285 return NULL;
286 set_extra_expr_mod(iter_var, alloc_extra_state_range(end, start));
287 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
290 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
292 struct expression *iter_expr;
293 struct expression *condition;
295 if (!loop->iterator_post_statement)
296 return NULL;
297 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
298 return NULL;
299 iter_expr = loop->iterator_post_statement->expression;
300 if (!loop->iterator_pre_condition)
301 return NULL;
302 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
303 return NULL;
304 condition = loop->iterator_pre_condition;
306 if (iter_expr->op == SPECIAL_INCREMENT)
307 return handle_canonical_for_inc(iter_expr, condition);
308 if (iter_expr->op == SPECIAL_DECREMENT)
309 return handle_canonical_for_dec(iter_expr, condition);
310 return NULL;
313 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
315 struct sm_state *ret;
317 __push_fake_cur_slist();
318 if (!loop->iterator_post_statement)
319 ret = handle_canonical_while_count_down(loop);
320 else
321 ret = handle_canonical_for_loops(loop);
322 *slist = __pop_fake_cur_slist();
323 return ret;
326 int __iterator_unchanged(struct sm_state *sm)
328 if (!sm)
329 return 0;
330 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
331 return 1;
332 return 0;
335 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
337 long long after_value;
339 /* paranoid checking. prolly not needed */
340 condition = strip_expr(condition);
341 if (!condition)
342 return;
343 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
344 return;
345 if (condition->op != SPECIAL_DECREMENT)
346 return;
347 after_value = get_dinfo_min(get_dinfo(sm->state));
348 after_value--;
349 set_extra_mod(sm->name, sm->sym, alloc_extra_state(after_value));
352 void __extra_pre_loop_hook_after(struct sm_state *sm,
353 struct statement *iterator,
354 struct expression *condition)
356 struct expression *iter_expr;
357 char *name;
358 struct symbol *sym;
359 long long value;
360 int left = 0;
361 struct smatch_state *state;
362 struct data_info *dinfo;
363 long long min, max;
365 if (!iterator) {
366 while_count_down_after(sm, condition);
367 return;
370 iter_expr = iterator->expression;
372 if (condition->type != EXPR_COMPARE)
373 return;
374 if (!get_value(condition->left, &value)) {
375 if (!get_value(condition->right, &value))
376 return;
377 left = 1;
379 if (left)
380 name = get_variable_from_expr(condition->left, &sym);
381 else
382 name = get_variable_from_expr(condition->right, &sym);
383 if (!name || !sym)
384 goto free;
385 if (sym != sm->sym || strcmp(name, sm->name))
386 goto free;
387 state = get_state(my_id, name, sym);
388 dinfo = get_dinfo(state);
389 min = get_dinfo_min(dinfo);
390 max = get_dinfo_max(dinfo);
391 if (iter_expr->op == SPECIAL_INCREMENT &&
392 min != whole_range.min &&
393 max == whole_range.max) {
394 set_extra_mod(name, sym, alloc_extra_state(min));
395 } else if (min == whole_range.min && max != whole_range.max) {
396 set_extra_mod(name, sym, alloc_extra_state(max));
398 free:
399 free_string(name);
400 return;
403 static struct smatch_state *unmatched_state(struct sm_state *sm)
405 return extra_undefined();
408 static void match_function_call(struct expression *expr)
410 struct expression *tmp;
412 FOR_EACH_PTR(expr->args, tmp) {
413 if (tmp->type == EXPR_PREOP && tmp->op == '&') {
414 remove_from_equiv_expr(tmp->unop);
415 set_state_expr(SMATCH_EXTRA, tmp->unop, extra_undefined());
417 } END_FOR_EACH_PTR(tmp);
420 static void set_equiv(struct sm_state *right_sm, struct expression *left)
422 struct smatch_state *state;
423 struct data_info *dinfo;
424 struct relation *rel;
425 char *name;
426 struct symbol *sym;
428 name = get_variable_from_expr(left, &sym);
429 if (!name || !sym)
430 goto free;
432 remove_from_equiv(name, sym);
434 state = clone_extra_state(right_sm->state);
435 dinfo = get_dinfo(state);
436 if (!dinfo->related)
437 add_equiv(state, right_sm->name, right_sm->sym);
438 add_equiv(state, name, sym);
440 FOR_EACH_PTR(dinfo->related, rel) {
441 struct sm_state *new_sm;
443 new_sm = clone_sm(right_sm);
444 new_sm->name = rel->name;
445 new_sm->sym = rel->sym;
446 new_sm->state = state;
447 __set_sm(new_sm);
448 } END_FOR_EACH_PTR(rel);
449 free:
450 free_string(name);
453 static void match_assign(struct expression *expr)
455 struct expression *left;
456 struct expression *right;
457 struct sm_state *right_sm;
458 struct symbol *right_sym;
459 char *right_name;
460 struct symbol *sym;
461 char *name;
462 long long value;
463 int known;
464 long long min = whole_range.min;
465 long long max = whole_range.max;
466 long long tmp;
468 if (__is_condition_assign(expr))
469 return;
470 left = strip_expr(expr->left);
471 name = get_variable_from_expr(left, &sym);
472 if (!name)
473 return;
474 right = strip_parens(expr->right);
475 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
476 right = strip_parens(right->left);
478 if (expr->op == '=' && right->type == EXPR_CALL)
479 return; /* these are handled in match_call_assign() */
481 right_name = get_variable_from_expr(right, &right_sym);
482 if (expr->op == '=' && right_name && right_sym) {
483 right_sm = get_sm_state_expr(SMATCH_EXTRA, right);
484 if (!right_sm)
485 right_sm = set_state_expr(SMATCH_EXTRA, right, extra_undefined());
486 if (!right_sm)
487 goto free;
488 set_equiv(right_sm, left);
489 goto free;
492 known = get_implied_value(right, &value);
493 switch (expr->op) {
494 case '=': {
495 struct range_list *rl = NULL;
497 if (get_implied_range_list(right, &rl)) {
498 set_extra_mod(name, sym, alloc_extra_state_range_list(rl));
499 goto free;
502 if (expr_unsigned(right))
503 min = 0;
504 break;
506 case SPECIAL_ADD_ASSIGN:
507 if (get_implied_min(left, &tmp)) {
508 if (known)
509 min = tmp + value;
510 else
511 min = tmp;
513 if (!inside_loop() && known && get_implied_max(left, &tmp))
514 max = tmp + value;
515 break;
516 case SPECIAL_SUB_ASSIGN:
517 if (get_implied_max(left, &tmp)) {
518 if (known)
519 max = tmp - value;
520 else
521 max = tmp;
523 if (!inside_loop() && known && get_implied_min(left, &tmp))
524 min = tmp - value;
525 break;
527 set_extra_mod(name, sym, alloc_extra_state_range(min, max));
528 free:
529 free_string(right_name);
530 free_string(name);
533 static void reset_struct_members(const char *name, struct symbol *sym, struct expression *expr, void *unused)
535 struct state_list *slist;
536 struct sm_state *tmp;
537 int len;
539 len = strlen(name);
540 slist = get_all_states(SMATCH_EXTRA);
541 FOR_EACH_PTR(slist, tmp) {
542 if (sym != tmp->sym)
543 continue;
544 if (strncmp(name, tmp->name, len))
545 continue;
546 if (tmp->name[len] != '-' && tmp->name[len] != '.')
547 continue;
548 set_extra_mod(tmp->name, tmp->sym, extra_undefined());
549 } END_FOR_EACH_PTR(tmp);
552 static struct smatch_state *increment_state(struct smatch_state *state)
554 long long min;
555 long long max;
557 min = get_dinfo_min(get_dinfo(state));
558 max = get_dinfo_max(get_dinfo(state));
560 if (inside_loop())
561 max = whole_range.max;
563 if (min != whole_range.min)
564 min++;
565 if (max != whole_range.max)
566 max++;
567 return alloc_extra_state_range(min, max);
570 static struct smatch_state *decrement_state(struct smatch_state *state)
572 long long min;
573 long long max;
575 min = get_dinfo_min(get_dinfo(state));
576 max = get_dinfo_max(get_dinfo(state));
578 if (inside_loop())
579 min = whole_range.min;
581 if (min != whole_range.min)
582 min--;
583 if (max != whole_range.max)
584 max--;
585 return alloc_extra_state_range(min, max);
588 static void unop_expr(struct expression *expr)
590 struct smatch_state *state;
592 if (expr->smatch_flags & Handled)
593 return;
595 switch (expr->op) {
596 case SPECIAL_INCREMENT:
597 state = get_state_expr(SMATCH_EXTRA, expr->unop);
598 state = increment_state(state);
599 set_extra_expr_mod(expr->unop, state);
600 break;
601 case SPECIAL_DECREMENT:
602 state = get_state_expr(SMATCH_EXTRA, expr->unop);
603 state = decrement_state(state);
604 set_extra_expr_mod(expr->unop, state);
605 break;
606 default:
607 return;
611 static void delete_state_tracker(struct tracker *t)
613 remove_from_equiv(t->name, t->sym);
614 delete_state(t->owner, t->name, t->sym);
617 static void scoped_state_extra(const char *name, struct symbol *sym)
619 struct tracker *t;
621 t = alloc_tracker(SMATCH_EXTRA, name, sym);
622 add_scope_hook((scope_hook *)&delete_state_tracker, t);
625 static void match_declarations(struct symbol *sym)
627 const char *name;
629 if (sym->ident) {
630 name = sym->ident->name;
631 if (!sym->initializer) {
632 set_state(SMATCH_EXTRA, name, sym, extra_undefined());
633 scoped_state_extra(name, sym);
638 static void match_function_def(struct symbol *sym)
640 struct symbol *arg;
642 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
643 if (!arg->ident)
644 continue;
645 set_state(my_id, arg->ident->name, arg, extra_undefined());
646 } END_FOR_EACH_PTR(arg);
649 static int last_stmt_val(struct statement *stmt, long long *val)
651 struct expression *expr;
653 if (!stmt)
654 return 0;
656 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
657 if (stmt->type != STMT_EXPRESSION)
658 return 0;
659 expr = stmt->expression;
660 return get_value(expr, val);
663 static int match_func_comparison(struct expression *expr)
665 struct expression *left = strip_expr(expr->left);
666 struct expression *right = strip_expr(expr->right);
667 long long val;
669 if (left->type == EXPR_CALL) {
670 if (!get_implied_value(right, &val))
671 return 1;
672 function_comparison(expr->op, left, val, 1);
673 return 1;
676 if (right->type == EXPR_CALL) {
677 if (!get_implied_value(left, &val))
678 return 1;
679 function_comparison(expr->op, right, val, 0);
680 return 1;
683 return 0;
686 static void match_comparison(struct expression *expr)
688 struct expression *left = strip_expr(expr->left);
689 struct expression *right = strip_expr(expr->right);
690 struct range_list *left_orig;
691 struct range_list *left_true;
692 struct range_list *left_false;
693 struct range_list *right_orig;
694 struct range_list *right_true;
695 struct range_list *right_false;
696 struct smatch_state *left_true_state;
697 struct smatch_state *left_false_state;
698 struct smatch_state *right_true_state;
699 struct smatch_state *right_false_state;
700 int left_postop = 0;
701 int right_postop = 0;
703 if (match_func_comparison(expr))
704 return;
706 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
707 if (left->type == EXPR_POSTOP) {
708 left->smatch_flags |= Handled;
709 left_postop = left->op;
711 left = strip_expr(left->unop);
714 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
715 if (right->type == EXPR_POSTOP) {
716 right->smatch_flags |= Handled;
717 right_postop = right->op;
719 right = strip_expr(right->unop);
722 if (!get_implied_range_list(left, &left_orig))
723 left_orig = alloc_range_list(whole_range.min, whole_range.max);
724 if (!get_implied_range_list(right, &right_orig))
725 right_orig = alloc_range_list(whole_range.min, whole_range.max);
727 left_true = clone_range_list(left_orig);
728 left_false = clone_range_list(left_orig);
729 right_true = clone_range_list(right_orig);
730 right_false = clone_range_list(right_orig);
732 switch (expr->op) {
733 case '<':
734 case SPECIAL_UNSIGNED_LT:
735 if (rl_max(right_orig) != whole_range.max)
736 left_true = remove_range(left_orig, rl_max(right_orig), whole_range.max);
737 if (rl_min(right_orig) != whole_range.min)
738 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
740 if (rl_min(left_orig) != whole_range.min)
741 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig));
742 if (rl_max(left_orig) != whole_range.max)
743 right_false = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
744 break;
745 case SPECIAL_UNSIGNED_LTE:
746 case SPECIAL_LTE:
747 if (rl_max(right_orig) != whole_range.max)
748 left_true = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
749 if (rl_min(right_orig) != whole_range.min)
750 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig));
752 if (rl_min(left_orig) != whole_range.min)
753 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
754 if (rl_max(left_orig) != whole_range.max)
755 right_false = remove_range(right_orig, rl_max(left_orig), whole_range.max);
756 break;
757 case SPECIAL_EQUAL:
758 if (rl_max(right_orig) != whole_range.max)
759 left_true = remove_range(left_true, rl_max(right_orig) + 1, whole_range.max);
760 if (rl_min(right_orig) != whole_range.min)
761 left_true = remove_range(left_true, whole_range.min, rl_min(right_orig) - 1);
762 if (rl_min(right_orig) == rl_max(right_orig))
763 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
765 if (rl_max(left_orig) != whole_range.max)
766 right_true = remove_range(right_true, rl_max(left_orig) + 1, whole_range.max);
767 if (rl_min(left_orig) != whole_range.min)
768 right_true = remove_range(right_true, whole_range.min, rl_min(left_orig) - 1);
769 if (rl_min(left_orig) == rl_max(left_orig))
770 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
771 break;
772 case SPECIAL_UNSIGNED_GTE:
773 case SPECIAL_GTE:
774 if (rl_min(right_orig) != whole_range.min)
775 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
776 if (rl_max(right_orig) != whole_range.max)
777 left_false = remove_range(left_orig, rl_max(right_orig), whole_range.max);
779 if (rl_max(left_orig) != whole_range.max)
780 right_true = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
781 if (rl_min(left_orig) != whole_range.min)
782 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig));
783 break;
784 case '>':
785 case SPECIAL_UNSIGNED_GT:
786 if (rl_min(right_orig) != whole_range.min)
787 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig));
788 if (rl_max(right_orig) != whole_range.max)
789 left_false = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
791 if (rl_max(left_orig) != whole_range.max)
792 right_true = remove_range(right_orig, rl_max(left_orig), whole_range.max);
793 if (rl_min(left_orig) != whole_range.min)
794 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
795 break;
796 case SPECIAL_NOTEQUAL:
797 if (rl_max(right_orig) != whole_range.max)
798 left_false = remove_range(left_false, rl_max(right_orig) + 1, whole_range.max);
799 if (rl_min(right_orig) != whole_range.min)
800 left_false = remove_range(left_false, whole_range.min, rl_min(right_orig) - 1);
801 if (rl_min(right_orig) == rl_max(right_orig))
802 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
804 if (rl_max(left_orig) != whole_range.max)
805 right_false = remove_range(right_false, rl_max(left_orig) + 1, whole_range.max);
806 if (rl_min(left_orig) != whole_range.min)
807 right_false = remove_range(right_false, whole_range.min, rl_min(left_orig) - 1);
808 if (rl_min(left_orig) == rl_max(left_orig))
809 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
810 break;
811 default:
812 return;
815 left_true_state = alloc_extra_state_range_list(left_true);
816 left_false_state = alloc_extra_state_range_list(left_false);
817 right_true_state = alloc_extra_state_range_list(right_true);
818 right_false_state = alloc_extra_state_range_list(right_false);
820 if (left_postop == SPECIAL_INCREMENT) {
821 left_true_state = increment_state(left_true_state);
822 left_false_state = increment_state(left_false_state);
824 if (left_postop == SPECIAL_DECREMENT) {
825 left_true_state = decrement_state(left_true_state);
826 left_false_state = decrement_state(left_false_state);
828 if (right_postop == SPECIAL_INCREMENT) {
829 right_true_state = increment_state(right_true_state);
830 right_false_state = increment_state(right_false_state);
832 if (right_postop == SPECIAL_DECREMENT) {
833 right_true_state = decrement_state(right_true_state);
834 right_false_state = decrement_state(right_false_state);
837 set_extra_expr_true_false(left, left_true_state, left_false_state);
838 set_extra_expr_true_false(right, right_true_state, right_false_state);
841 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
842 void __extra_match_condition(struct expression *expr)
844 struct symbol *sym;
845 char *name;
846 struct smatch_state *pre_state;
847 struct smatch_state *true_state;
848 struct smatch_state *false_state;
850 expr = strip_expr(expr);
851 switch (expr->type) {
852 case EXPR_CALL:
853 function_comparison(SPECIAL_NOTEQUAL, expr, 0, 1);
854 return;
855 case EXPR_PREOP:
856 case EXPR_SYMBOL:
857 case EXPR_DEREF:
858 name = get_variable_from_expr(expr, &sym);
859 if (!name)
860 return;
861 pre_state = get_state(my_id, name, sym);
862 true_state = add_filter(pre_state, 0);
863 if (possibly_true(SPECIAL_EQUAL, get_dinfo(pre_state), 0, 0))
864 false_state = alloc_extra_state(0);
865 else
866 false_state = alloc_extra_state_empty();
867 set_extra_true_false(name, sym, true_state, false_state);
868 free_string(name);
869 return;
870 case EXPR_COMPARE:
871 match_comparison(expr);
872 return;
873 case EXPR_ASSIGNMENT:
874 __extra_match_condition(expr->left);
875 return;
879 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
880 int implied_not_equal(struct expression *expr, long long val)
882 char *name;
883 struct symbol *sym;
884 struct smatch_state *state;
885 long long expr_val;
886 int ret = 0;
888 if (get_value(expr, &expr_val))
889 return expr_val != val;
891 name = get_variable_from_expr(expr, &sym);
892 if (!name || !sym)
893 goto exit;
894 state = get_state(my_id, name, sym);
895 if (!state || !state->data)
896 goto exit;
897 ret = !possibly_false(SPECIAL_NOTEQUAL, get_dinfo(state), val, 1);
898 exit:
899 free_string(name);
900 return ret;
903 int known_condition_true(struct expression *expr)
905 long long tmp;
906 struct statement *stmt;
908 if (!expr)
909 return 0;
911 if (get_value(expr, &tmp) && tmp)
912 return 1;
914 expr = strip_expr(expr);
915 switch (expr->type) {
916 case EXPR_PREOP:
917 if (expr->op == '!') {
918 if (known_condition_false(expr->unop))
919 return 1;
920 break;
922 stmt = get_expression_statement(expr);
923 if (last_stmt_val(stmt, &tmp) && tmp == 1)
924 return 1;
925 break;
926 default:
927 break;
929 return 0;
932 int known_condition_false(struct expression *expr)
934 long long tmp;
935 struct statement *stmt;
937 if (!expr)
938 return 0;
940 if (is_zero(expr))
941 return 1;
943 switch (expr->type) {
944 case EXPR_PREOP:
945 if (expr->op == '!') {
946 if (known_condition_true(expr->unop))
947 return 1;
948 break;
950 stmt = get_expression_statement(expr);
951 if (last_stmt_val(stmt, &tmp) && tmp == 0)
952 return 1;
953 break;
954 case EXPR_CALL:
955 if (sym_name_is("__builtin_constant_p", expr->fn))
956 return 1;
957 break;
958 default:
959 break;
961 return 0;
964 struct range_list *get_range_list(struct expression *expr)
966 long long min;
967 long long max;
968 struct range_list *ret = NULL;
969 struct smatch_state *state;
971 state = get_state_expr(SMATCH_EXTRA, expr);
972 if (state)
973 return clone_range_list(get_dinfo(state)->value_ranges);
974 if (!get_absolute_min(expr, &min))
975 return NULL;
976 if (!get_absolute_max(expr, &max))
977 return NULL;
978 add_range(&ret, min, max);
979 return ret;
982 static int do_comparison(struct expression *expr)
984 struct range_list *left_ranges;
985 struct range_list *right_ranges;
986 int poss_true, poss_false;
988 left_ranges = get_range_list(expr->left);
989 right_ranges = get_range_list(expr->right);
991 poss_true = possibly_true_range_lists(left_ranges, expr->op, right_ranges);
992 poss_false = possibly_false_range_lists(left_ranges, expr->op, right_ranges);
994 free_range_list(&left_ranges);
995 free_range_list(&right_ranges);
997 if (!poss_true && !poss_false)
998 return 0;
999 if (poss_true && !poss_false)
1000 return 1;
1001 if (!poss_true && poss_false)
1002 return 2;
1003 return 3;
1006 int implied_condition_true(struct expression *expr)
1008 struct statement *stmt;
1009 long long tmp;
1010 long long val;
1012 if (!expr)
1013 return 0;
1015 if (get_implied_value(expr, &tmp) && tmp)
1016 return 1;
1018 if (expr->type == EXPR_POSTOP)
1019 return implied_condition_true(expr->unop);
1021 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_DECREMENT)
1022 return implied_not_equal(expr->unop, 1);
1023 if (expr->type == EXPR_PREOP && expr->op == SPECIAL_INCREMENT)
1024 return implied_not_equal(expr->unop, -1);
1026 expr = strip_expr(expr);
1027 switch (expr->type) {
1028 case EXPR_COMPARE:
1029 if (do_comparison(expr) == 1)
1030 return 1;
1031 break;
1032 case EXPR_PREOP:
1033 if (expr->op == '!') {
1034 if (implied_condition_false(expr->unop))
1035 return 1;
1036 break;
1038 stmt = get_expression_statement(expr);
1039 if (last_stmt_val(stmt, &val) && val == 1)
1040 return 1;
1041 break;
1042 default:
1043 if (implied_not_equal(expr, 0) == 1)
1044 return 1;
1045 break;
1047 return 0;
1050 int implied_condition_false(struct expression *expr)
1052 struct statement *stmt;
1053 struct expression *tmp;
1054 long long val;
1056 if (!expr)
1057 return 0;
1059 if (is_zero(expr))
1060 return 1;
1062 switch (expr->type) {
1063 case EXPR_COMPARE:
1064 if (do_comparison(expr) == 2)
1065 return 1;
1066 case EXPR_PREOP:
1067 if (expr->op == '!') {
1068 if (implied_condition_true(expr->unop))
1069 return 1;
1070 break;
1072 stmt = get_expression_statement(expr);
1073 if (last_stmt_val(stmt, &val) && val == 0)
1074 return 1;
1075 tmp = strip_expr(expr);
1076 if (tmp != expr)
1077 return implied_condition_false(tmp);
1078 break;
1079 default:
1080 if (get_implied_value(expr, &val) && val == 0)
1081 return 1;
1082 break;
1084 return 0;
1087 int get_implied_range_list(struct expression *expr, struct range_list **rl)
1089 long long val;
1090 struct smatch_state *state;
1091 long long min;
1092 long long max;
1094 expr = strip_parens(expr);
1096 state = get_state_expr(my_id, expr);
1097 if (state) {
1098 *rl = clone_range_list(get_dinfo(state)->value_ranges);
1099 return 1;
1102 if (get_implied_value(expr, &val)) {
1103 *rl = NULL;
1104 add_range(rl, val, val);
1105 return 1;
1108 if (expr->type != EXPR_BINOP)
1109 return 0;
1111 if (expr->type == EXPR_CALL) {
1112 *rl = db_return_vals(expr);
1113 return !!*rl;
1116 if (expr->op == '%') {
1117 if (!get_implied_value(expr->right, &val))
1118 return 0;
1119 *rl = NULL;
1120 add_range(rl, 0, val - 1);
1121 return 1;
1124 if (!get_implied_min(expr, &min))
1125 return 0;
1126 if (!get_implied_max(expr, &max))
1127 return 0;
1129 *rl = alloc_range_list(min, max);
1130 return 1;
1133 int is_whole_range(struct smatch_state *state)
1135 struct data_info *dinfo;
1136 struct data_range *drange;
1138 if (!state)
1139 return 1;
1140 dinfo = get_dinfo(state);
1141 if (!dinfo || !dinfo->value_ranges)
1142 return 1;
1143 drange = first_ptr_list((struct ptr_list *)dinfo->value_ranges);
1144 if (drange->min == whole_range.min && drange->max == whole_range.max)
1145 return 1;
1146 return 0;
1149 static void struct_member_callback(char *fn, int param, char *printed_name, struct smatch_state *state)
1151 if (is_whole_range(state))
1152 return;
1153 sm_msg("info: passes param_value '%s' %d '%s' %s", fn, param, printed_name, state->name);
1156 static void match_call_info(struct expression *expr)
1158 struct range_list *rl = NULL;
1159 struct expression *arg;
1160 char *name;
1161 int i = 0;
1163 name = get_fnptr_name(expr->fn);
1164 if (!name)
1165 return;
1167 FOR_EACH_PTR(expr->args, arg) {
1168 if (get_implied_range_list(arg, &rl) && !is_whole_range_rl(rl)) {
1169 sm_msg("info: passes param_value '%s' %d '$$' %s",
1170 name, i, show_ranges(rl));
1172 i++;
1173 } END_FOR_EACH_PTR(arg);
1175 free_string(name);
1178 void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
1180 struct range_list *rl = NULL;
1181 struct smatch_state *state;
1182 char fullname[256];
1184 if (strncmp(key, "$$", 2))
1185 return;
1187 snprintf(fullname, 256, "%s%s", name, key + 2);
1188 get_value_ranges(value, &rl);
1189 char *tmp = show_ranges(rl);
1190 if (strcmp(tmp, value))
1191 sm_msg("value = %s, ranges = %s", value, tmp);
1192 state = alloc_extra_state_range_list(rl);
1193 set_state(SMATCH_EXTRA, fullname, sym, state);
1196 static void match_call_assign(struct expression *expr)
1198 struct expression *right;
1199 struct range_list *rl;
1201 if (expr->op != '=')
1202 return;
1204 right = strip_parens(expr->right);
1205 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
1206 right = strip_parens(right->left);
1208 rl = db_return_vals(right);
1209 if (rl)
1210 set_extra_expr_mod(expr->left, alloc_extra_state_range_list(rl));
1211 else
1212 set_extra_expr_mod(expr->left, extra_undefined());
1215 void register_smatch_extra(int id)
1217 my_id = id;
1218 add_merge_hook(my_id, &merge_func);
1219 add_unmatched_state_hook(my_id, &unmatched_state);
1220 add_hook(&unop_expr, OP_HOOK);
1221 add_hook(&match_function_def, FUNC_DEF_HOOK);
1222 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
1223 add_hook(&match_assign, ASSIGNMENT_HOOK);
1224 add_hook(&match_call_assign, CALL_ASSIGNMENT_HOOK);
1225 add_hook(&match_declarations, DECLARATION_HOOK);
1226 if (option_info) {
1227 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
1228 add_member_info_callback(my_id, struct_member_callback);
1230 add_definition_db_callback(set_param_value, PARAM_VALUE);
1233 void register_smatch_extra_late(int id)
1235 set_default_modification_hook(SMATCH_EXTRA, reset_struct_members);