constraints: fix set_equiv()
[smatch.git] / smatch_extra.c
blobcc45ead3b0f38299784cd719984b348a29415c6f
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 (!estate_related(orig_state)) {
74 set_state_expr(SMATCH_EXTRA, expr, state);
75 return;
78 FOR_EACH_PTR(estate_related(orig_state), rel) {
79 if (option_debug_related)
80 sm_msg("updating related %s to %s", rel->name, state->name);
81 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
82 add_equiv(state, rel->name, rel->sym);
83 } END_FOR_EACH_PTR(rel);
86 static void set_extra_true_false(const char *name, struct symbol *sym,
87 struct smatch_state *true_state,
88 struct smatch_state *false_state)
90 struct relation *rel;
91 struct smatch_state *orig_state;
93 if (in_warn_on_macro())
94 return;
96 orig_state = get_state(SMATCH_EXTRA, name, sym);
98 if (!estate_related(orig_state)) {
99 set_true_false_states(SMATCH_EXTRA, name, sym, true_state, false_state);
100 return;
103 // FIXME!!!! equiv => related
104 FOR_EACH_PTR(estate_related(orig_state), rel) {
105 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
106 true_state, false_state);
107 if (true_state)
108 add_equiv(true_state, rel->name, rel->sym);
109 if (false_state)
110 add_equiv(false_state, rel->name, rel->sym);
111 } END_FOR_EACH_PTR(rel);
114 static void set_extra_expr_true_false(struct expression *expr,
115 struct smatch_state *true_state,
116 struct smatch_state *false_state)
118 char *name;
119 struct symbol *sym;
121 expr = strip_expr(expr);
122 name = get_variable_from_expr(expr, &sym);
123 if (!name || !sym)
124 goto free;
125 set_extra_true_false(name, sym, true_state, false_state);
126 free:
127 free_string(name);
130 struct smatch_state *filter_range(struct smatch_state *orig,
131 long long filter_min, long long filter_max)
133 struct range_list *rl;
135 if (!orig)
136 orig = extra_undefined();
138 rl = remove_range(estate_ranges(orig), filter_min, filter_max);
139 return alloc_estate_range_list(rl);
142 struct smatch_state *add_filter(struct smatch_state *orig, long long num)
144 return filter_range(orig, num, num);
147 static struct smatch_state *merge_func(const char *name, struct symbol *sym,
148 struct smatch_state *s1,
149 struct smatch_state *s2)
151 struct smatch_state *tmp;
152 struct range_list *value_ranges;
154 value_ranges = range_list_union(estate_ranges(s1), estate_ranges(s2));
155 tmp = alloc_estate_range_list(value_ranges);
156 get_dinfo(tmp)->related = get_shared_relations(estate_related(s1),
157 estate_related(s2));
158 return tmp;
161 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
163 struct expression *iter_var;
164 struct expression *condition;
165 struct sm_state *sm;
166 long long start;
168 condition = strip_expr(loop->iterator_pre_condition);
169 if (!condition)
170 return NULL;
171 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
172 return NULL;
173 if (condition->op != SPECIAL_DECREMENT)
174 return NULL;
176 iter_var = condition->unop;
177 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
178 if (!sm)
179 return NULL;
180 if (estate_min(sm->state) < 0)
181 return NULL;
182 start = estate_max(sm->state);
183 if (start <= 0)
184 return NULL;
185 if (start != whole_range.max)
186 start--;
188 if (condition->type == EXPR_PREOP)
189 set_extra_expr_mod(iter_var, alloc_estate_range(1, start));
190 if (condition->type == EXPR_POSTOP)
191 set_extra_expr_mod(iter_var, alloc_estate_range(0, start));
192 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
195 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
196 struct expression *condition)
198 struct expression *iter_var;
199 struct sm_state *sm;
200 long long start;
201 long long end;
203 iter_var = iter_expr->unop;
204 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
205 if (!sm)
206 return NULL;
207 if (!estate_get_single_value(sm->state, &start))
208 return NULL;
209 if (!get_implied_value(condition->right, &end))
210 end = whole_range.max;
211 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
212 return NULL;
214 switch (condition->op) {
215 case SPECIAL_NOTEQUAL:
216 case '<':
217 if (end != whole_range.max)
218 end--;
219 break;
220 case SPECIAL_LTE:
221 break;
222 default:
223 return NULL;
225 if (end < start)
226 return NULL;
227 set_extra_expr_mod(iter_var, alloc_estate_range(start, end));
228 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
231 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
232 struct expression *condition)
234 struct expression *iter_var;
235 struct sm_state *sm;
236 long long start;
237 long long end;
239 iter_var = iter_expr->unop;
240 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
241 if (!sm)
242 return NULL;
243 if (!estate_get_single_value(sm->state, &start))
244 return NULL;
245 if (!get_implied_value(condition->right, &end))
246 end = whole_range.max;
247 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
248 return NULL;
250 switch (condition->op) {
251 case SPECIAL_NOTEQUAL:
252 case '>':
253 if (end != whole_range.min)
254 end++;
255 break;
256 case SPECIAL_GTE:
257 break;
258 default:
259 return NULL;
261 if (end > start)
262 return NULL;
263 set_extra_expr_mod(iter_var, alloc_estate_range(end, start));
264 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
267 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
269 struct expression *iter_expr;
270 struct expression *condition;
272 if (!loop->iterator_post_statement)
273 return NULL;
274 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
275 return NULL;
276 iter_expr = loop->iterator_post_statement->expression;
277 if (!loop->iterator_pre_condition)
278 return NULL;
279 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
280 return NULL;
281 condition = loop->iterator_pre_condition;
283 if (iter_expr->op == SPECIAL_INCREMENT)
284 return handle_canonical_for_inc(iter_expr, condition);
285 if (iter_expr->op == SPECIAL_DECREMENT)
286 return handle_canonical_for_dec(iter_expr, condition);
287 return NULL;
290 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
292 struct sm_state *ret;
294 __push_fake_cur_slist();
295 if (!loop->iterator_post_statement)
296 ret = handle_canonical_while_count_down(loop);
297 else
298 ret = handle_canonical_for_loops(loop);
299 *slist = __pop_fake_cur_slist();
300 return ret;
303 int __iterator_unchanged(struct sm_state *sm)
305 if (!sm)
306 return 0;
307 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
308 return 1;
309 return 0;
312 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
314 long long after_value;
316 /* paranoid checking. prolly not needed */
317 condition = strip_expr(condition);
318 if (!condition)
319 return;
320 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
321 return;
322 if (condition->op != SPECIAL_DECREMENT)
323 return;
324 after_value = estate_min(sm->state);
325 after_value--;
326 set_extra_mod(sm->name, sm->sym, alloc_estate(after_value));
329 void __extra_pre_loop_hook_after(struct sm_state *sm,
330 struct statement *iterator,
331 struct expression *condition)
333 struct expression *iter_expr;
334 char *name;
335 struct symbol *sym;
336 long long value;
337 int left = 0;
338 struct smatch_state *state;
339 long long min, max;
341 if (!iterator) {
342 while_count_down_after(sm, condition);
343 return;
346 iter_expr = iterator->expression;
348 if (condition->type != EXPR_COMPARE)
349 return;
350 if (!get_value(condition->left, &value)) {
351 if (!get_value(condition->right, &value))
352 return;
353 left = 1;
355 if (left)
356 name = get_variable_from_expr(condition->left, &sym);
357 else
358 name = get_variable_from_expr(condition->right, &sym);
359 if (!name || !sym)
360 goto free;
361 if (sym != sm->sym || strcmp(name, sm->name))
362 goto free;
363 state = get_state(my_id, name, sym);
364 min = estate_min(state);
365 max = estate_max(state);
366 if (iter_expr->op == SPECIAL_INCREMENT &&
367 min != whole_range.min &&
368 max == whole_range.max) {
369 set_extra_mod(name, sym, alloc_estate(min));
370 } else if (min == whole_range.min && max != whole_range.max) {
371 set_extra_mod(name, sym, alloc_estate(max));
373 free:
374 free_string(name);
375 return;
378 static struct smatch_state *unmatched_state(struct sm_state *sm)
380 return extra_undefined();
383 static void match_function_call(struct expression *expr)
385 struct expression *arg;
386 struct expression *tmp;
388 FOR_EACH_PTR(expr->args, arg) {
389 tmp = strip_expr(arg);
390 if (tmp->type == EXPR_PREOP && tmp->op == '&') {
391 remove_from_equiv_expr(tmp->unop);
392 set_state_expr(SMATCH_EXTRA, tmp->unop, extra_undefined());
394 } END_FOR_EACH_PTR(arg);
397 static void match_assign(struct expression *expr)
399 struct expression *left;
400 struct expression *right;
401 struct symbol *right_sym;
402 char *right_name;
403 struct symbol *sym;
404 char *name;
405 long long value;
406 int known;
407 long long min = whole_range.min;
408 long long max = whole_range.max;
409 long long tmp;
411 if (__is_condition_assign(expr))
412 return;
413 left = strip_expr(expr->left);
414 name = get_variable_from_expr(left, &sym);
415 if (!name)
416 return;
417 right = strip_parens(expr->right);
418 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
419 right = strip_parens(right->left);
421 if (expr->op == '=' && right->type == EXPR_CALL)
422 return; /* these are handled in match_call_assign() */
424 right_name = get_variable_from_expr(right, &right_sym);
425 if (expr->op == '=' && right_name && right_sym) {
426 set_equiv(left, right);
427 goto free;
430 known = get_implied_value(right, &value);
431 switch (expr->op) {
432 case '=': {
433 struct range_list *rl = NULL;
435 if (get_implied_range_list(right, &rl)) {
436 set_extra_mod(name, sym, alloc_estate_range_list(rl));
437 goto free;
440 if (expr_unsigned(right))
441 min = 0;
442 break;
444 case SPECIAL_ADD_ASSIGN:
445 if (get_implied_min(left, &tmp)) {
446 if (known)
447 min = tmp + value;
448 else
449 min = tmp;
451 if (!inside_loop() && known && get_implied_max(left, &tmp))
452 max = tmp + value;
453 break;
454 case SPECIAL_SUB_ASSIGN:
455 if (get_implied_max(left, &tmp)) {
456 if (known)
457 max = tmp - value;
458 else
459 max = tmp;
461 if (!inside_loop() && known && get_implied_min(left, &tmp))
462 min = tmp - value;
463 break;
465 set_extra_mod(name, sym, alloc_estate_range(min, max));
466 free:
467 free_string(right_name);
468 free_string(name);
471 static void reset_struct_members(struct sm_state *sm)
473 set_extra_mod(sm->name, sm->sym, extra_undefined());
476 static struct smatch_state *increment_state(struct smatch_state *state)
478 long long min = estate_min(state);
479 long long max = estate_max(state);
481 if (inside_loop())
482 max = whole_range.max;
484 if (min != whole_range.min)
485 min++;
486 if (max != whole_range.max)
487 max++;
488 return alloc_estate_range(min, max);
491 static struct smatch_state *decrement_state(struct smatch_state *state)
493 long long min = estate_min(state);
494 long long max = estate_max(state);
496 if (inside_loop())
497 min = whole_range.min;
499 if (min != whole_range.min)
500 min--;
501 if (max != whole_range.max)
502 max--;
503 return alloc_estate_range(min, max);
506 static void unop_expr(struct expression *expr)
508 struct smatch_state *state;
510 if (expr->smatch_flags & Handled)
511 return;
513 switch (expr->op) {
514 case SPECIAL_INCREMENT:
515 state = get_state_expr(SMATCH_EXTRA, expr->unop);
516 state = increment_state(state);
517 set_extra_expr_mod(expr->unop, state);
518 break;
519 case SPECIAL_DECREMENT:
520 state = get_state_expr(SMATCH_EXTRA, expr->unop);
521 state = decrement_state(state);
522 set_extra_expr_mod(expr->unop, state);
523 break;
524 default:
525 return;
529 static void delete_state_tracker(struct tracker *t)
531 remove_from_equiv(t->name, t->sym);
532 delete_state(t->owner, t->name, t->sym);
535 static void scoped_state_extra(const char *name, struct symbol *sym)
537 struct tracker *t;
539 t = alloc_tracker(SMATCH_EXTRA, name, sym);
540 add_scope_hook((scope_hook *)&delete_state_tracker, t);
543 static void match_declarations(struct symbol *sym)
545 const char *name;
547 if (sym->ident) {
548 name = sym->ident->name;
549 if (!sym->initializer) {
550 set_state(SMATCH_EXTRA, name, sym, extra_undefined());
551 scoped_state_extra(name, sym);
556 static void check_dereference(struct expression *expr)
558 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min, valid_ptr_max));
561 static void match_dereferences(struct expression *expr)
563 if (expr->type != EXPR_PREOP)
564 return;
565 check_dereference(expr->unop);
568 static void match_pointer_as_array(struct expression *expr)
570 if (!is_array(expr))
571 return;
572 check_dereference(expr->unop->left);
575 static void set_param_dereferenced(struct expression *arg, char *unused)
577 check_dereference(arg);
580 static void match_function_def(struct symbol *sym)
582 struct symbol *arg;
584 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
585 if (!arg->ident)
586 continue;
587 set_state(my_id, arg->ident->name, arg, extra_undefined());
588 } END_FOR_EACH_PTR(arg);
591 static int match_func_comparison(struct expression *expr)
593 struct expression *left = strip_expr(expr->left);
594 struct expression *right = strip_expr(expr->right);
595 long long val;
597 if (left->type == EXPR_CALL) {
598 if (!get_implied_value(right, &val))
599 return 1;
600 function_comparison(expr->op, left, val, 1);
601 return 1;
604 if (right->type == EXPR_CALL) {
605 if (!get_implied_value(left, &val))
606 return 1;
607 function_comparison(expr->op, right, val, 0);
608 return 1;
611 return 0;
614 static void match_comparison(struct expression *expr)
616 struct expression *left = strip_expr(expr->left);
617 struct expression *right = strip_expr(expr->right);
618 struct range_list *left_orig;
619 struct range_list *left_true;
620 struct range_list *left_false;
621 struct range_list *right_orig;
622 struct range_list *right_true;
623 struct range_list *right_false;
624 struct smatch_state *left_true_state;
625 struct smatch_state *left_false_state;
626 struct smatch_state *right_true_state;
627 struct smatch_state *right_false_state;
628 int left_postop = 0;
629 int right_postop = 0;
631 if (match_func_comparison(expr))
632 return;
634 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
635 if (left->type == EXPR_POSTOP) {
636 left->smatch_flags |= Handled;
637 left_postop = left->op;
639 left = strip_expr(left->unop);
642 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
643 if (right->type == EXPR_POSTOP) {
644 right->smatch_flags |= Handled;
645 right_postop = right->op;
647 right = strip_expr(right->unop);
650 if (!get_implied_range_list(left, &left_orig))
651 left_orig = alloc_range_list(whole_range.min, whole_range.max);
652 if (!get_implied_range_list(right, &right_orig))
653 right_orig = alloc_range_list(whole_range.min, whole_range.max);
655 left_true = clone_range_list(left_orig);
656 left_false = clone_range_list(left_orig);
657 right_true = clone_range_list(right_orig);
658 right_false = clone_range_list(right_orig);
660 switch (expr->op) {
661 case '<':
662 case SPECIAL_UNSIGNED_LT:
663 if (rl_max(right_orig) != whole_range.max)
664 left_true = remove_range(left_orig, rl_max(right_orig), whole_range.max);
665 if (rl_min(right_orig) != whole_range.min)
666 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
668 if (rl_min(left_orig) != whole_range.min)
669 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig));
670 if (rl_max(left_orig) != whole_range.max)
671 right_false = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
672 break;
673 case SPECIAL_UNSIGNED_LTE:
674 case SPECIAL_LTE:
675 if (rl_max(right_orig) != whole_range.max)
676 left_true = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
677 if (rl_min(right_orig) != whole_range.min)
678 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig));
680 if (rl_min(left_orig) != whole_range.min)
681 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
682 if (rl_max(left_orig) != whole_range.max)
683 right_false = remove_range(right_orig, rl_max(left_orig), whole_range.max);
684 break;
685 case SPECIAL_EQUAL:
686 if (rl_max(right_orig) != whole_range.max)
687 left_true = remove_range(left_true, rl_max(right_orig) + 1, whole_range.max);
688 if (rl_min(right_orig) != whole_range.min)
689 left_true = remove_range(left_true, whole_range.min, rl_min(right_orig) - 1);
690 if (rl_min(right_orig) == rl_max(right_orig))
691 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
693 if (rl_max(left_orig) != whole_range.max)
694 right_true = remove_range(right_true, rl_max(left_orig) + 1, whole_range.max);
695 if (rl_min(left_orig) != whole_range.min)
696 right_true = remove_range(right_true, whole_range.min, rl_min(left_orig) - 1);
697 if (rl_min(left_orig) == rl_max(left_orig))
698 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
699 break;
700 case SPECIAL_UNSIGNED_GTE:
701 case SPECIAL_GTE:
702 if (rl_min(right_orig) != whole_range.min)
703 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
704 if (rl_max(right_orig) != whole_range.max)
705 left_false = remove_range(left_orig, rl_max(right_orig), whole_range.max);
707 if (rl_max(left_orig) != whole_range.max)
708 right_true = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
709 if (rl_min(left_orig) != whole_range.min)
710 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig));
711 break;
712 case '>':
713 case SPECIAL_UNSIGNED_GT:
714 if (rl_min(right_orig) != whole_range.min)
715 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig));
716 if (rl_max(right_orig) != whole_range.max)
717 left_false = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
719 if (rl_max(left_orig) != whole_range.max)
720 right_true = remove_range(right_orig, rl_max(left_orig), whole_range.max);
721 if (rl_min(left_orig) != whole_range.min)
722 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
723 break;
724 case SPECIAL_NOTEQUAL:
725 if (rl_max(right_orig) != whole_range.max)
726 left_false = remove_range(left_false, rl_max(right_orig) + 1, whole_range.max);
727 if (rl_min(right_orig) != whole_range.min)
728 left_false = remove_range(left_false, whole_range.min, rl_min(right_orig) - 1);
729 if (rl_min(right_orig) == rl_max(right_orig))
730 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
732 if (rl_max(left_orig) != whole_range.max)
733 right_false = remove_range(right_false, rl_max(left_orig) + 1, whole_range.max);
734 if (rl_min(left_orig) != whole_range.min)
735 right_false = remove_range(right_false, whole_range.min, rl_min(left_orig) - 1);
736 if (rl_min(left_orig) == rl_max(left_orig))
737 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
738 break;
739 default:
740 return;
743 left_true_state = alloc_estate_range_list(left_true);
744 left_false_state = alloc_estate_range_list(left_false);
745 right_true_state = alloc_estate_range_list(right_true);
746 right_false_state = alloc_estate_range_list(right_false);
748 if (left_postop == SPECIAL_INCREMENT) {
749 left_true_state = increment_state(left_true_state);
750 left_false_state = increment_state(left_false_state);
752 if (left_postop == SPECIAL_DECREMENT) {
753 left_true_state = decrement_state(left_true_state);
754 left_false_state = decrement_state(left_false_state);
756 if (right_postop == SPECIAL_INCREMENT) {
757 right_true_state = increment_state(right_true_state);
758 right_false_state = increment_state(right_false_state);
760 if (right_postop == SPECIAL_DECREMENT) {
761 right_true_state = decrement_state(right_true_state);
762 right_false_state = decrement_state(right_false_state);
765 set_extra_expr_true_false(left, left_true_state, left_false_state);
766 set_extra_expr_true_false(right, right_true_state, right_false_state);
769 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
770 void __extra_match_condition(struct expression *expr)
772 struct symbol *sym;
773 char *name;
774 struct smatch_state *pre_state;
775 struct smatch_state *true_state;
776 struct smatch_state *false_state;
778 expr = strip_expr(expr);
779 switch (expr->type) {
780 case EXPR_CALL:
781 function_comparison(SPECIAL_NOTEQUAL, expr, 0, 1);
782 return;
783 case EXPR_PREOP:
784 case EXPR_SYMBOL:
785 case EXPR_DEREF:
786 name = get_variable_from_expr(expr, &sym);
787 if (!name)
788 return;
789 pre_state = get_state(my_id, name, sym);
790 true_state = add_filter(pre_state, 0);
791 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
792 false_state = alloc_estate(0);
793 else
794 false_state = alloc_estate_empty();
795 set_extra_true_false(name, sym, true_state, false_state);
796 free_string(name);
797 return;
798 case EXPR_COMPARE:
799 match_comparison(expr);
800 return;
801 case EXPR_ASSIGNMENT:
802 __extra_match_condition(expr->left);
803 return;
807 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
808 int implied_not_equal(struct expression *expr, long long val)
810 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
813 int get_implied_range_list(struct expression *expr, struct range_list **rl)
815 long long val;
816 struct smatch_state *state;
817 long long min;
818 long long max;
820 *rl = NULL;
822 expr = strip_parens(expr);
823 if (!expr)
824 return 0;
826 state = get_state_expr(my_id, expr);
827 if (state) {
828 *rl = clone_range_list(estate_ranges(state));
829 return 1;
832 if (expr->type == EXPR_CALL) {
833 *rl = db_return_vals(expr);
834 return !!*rl;
837 if (get_implied_value(expr, &val)) {
838 add_range(rl, val, val);
839 return 1;
842 if (expr->type == EXPR_BINOP && expr->op == '%') {
843 if (!get_implied_value(expr->right, &val))
844 return 0;
845 add_range(rl, 0, val - 1);
846 return 1;
849 if (!get_implied_min(expr, &min))
850 return 0;
851 if (!get_implied_max(expr, &max))
852 return 0;
854 *rl = alloc_range_list(min, max);
855 return 1;
858 int is_whole_range(struct smatch_state *state)
860 return is_whole_range_rl(estate_ranges(state));
863 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
865 if (is_whole_range(state))
866 return;
867 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
870 static void match_call_info(struct expression *expr)
872 struct range_list *rl = NULL;
873 struct expression *arg;
874 char *name;
875 int i = 0;
877 name = get_fnptr_name(expr->fn);
878 if (!name)
879 return;
881 FOR_EACH_PTR(expr->args, arg) {
882 if (get_implied_range_list(arg, &rl) && !is_whole_range_rl(rl)) {
883 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
884 name, i, show_ranges(rl),
885 is_static(expr->fn) ? "static" : "global");
887 i++;
888 } END_FOR_EACH_PTR(arg);
890 free_string(name);
893 void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
895 struct range_list *rl = NULL;
896 struct smatch_state *state;
897 char fullname[256];
899 if (strncmp(key, "$$", 2))
900 return;
902 snprintf(fullname, 256, "%s%s", name, key + 2);
903 get_value_ranges(value, &rl);
904 char *tmp = show_ranges(rl);
905 if (strcmp(tmp, value))
906 sm_msg("value = %s, ranges = %s", value, tmp);
907 state = alloc_estate_range_list(rl);
908 set_state(SMATCH_EXTRA, fullname, sym, state);
911 static void match_call_assign(struct expression *expr)
913 struct expression *right;
914 struct range_list *rl;
916 if (expr->op != '=')
917 return;
919 right = strip_parens(expr->right);
920 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
921 right = strip_parens(right->left);
923 rl = db_return_vals(right);
924 if (rl)
925 set_extra_expr_mod(expr->left, alloc_estate_range_list(rl));
926 else
927 set_extra_expr_mod(expr->left, extra_undefined());
930 void register_smatch_extra(int id)
932 my_id = id;
933 add_merge_hook(my_id, &merge_func);
934 add_unmatched_state_hook(my_id, &unmatched_state);
935 add_hook(&unop_expr, OP_HOOK);
936 add_hook(&match_function_def, FUNC_DEF_HOOK);
937 add_hook(&match_declarations, DECLARATION_HOOK);
938 if (option_info) {
939 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
940 add_member_info_callback(my_id, struct_member_callback);
942 add_definition_db_callback(set_param_value, PARAM_VALUE);
945 void register_smatch_extra_late(int id)
947 add_hook(&match_dereferences, DEREF_HOOK);
948 add_hook(&match_pointer_as_array, OP_HOOK);
949 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
950 add_indirect_modification_hook(SMATCH_EXTRA, reset_struct_members);
951 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
952 add_hook(&match_assign, ASSIGNMENT_HOOK);
953 add_hook(&match_call_assign, CALL_ASSIGNMENT_HOOK);