type: fix get_binop_type() for bit shift operations
[smatch.git] / smatch_extra.c
blobaa5628dcc8ef596f245845b8e5d8a5ef31572997
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 = get_current_statement();
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 set_related(&state, estate_related(orig_state));
79 FOR_EACH_PTR(estate_related(orig_state), rel) {
80 if (option_debug_related)
81 sm_msg("updating related %s to %s", rel->name, state->name);
82 set_state(SMATCH_EXTRA, rel->name, rel->sym, state);
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 if (true_state)
104 set_related(&true_state, estate_related(orig_state));
105 if (false_state)
106 set_related(&false_state, estate_related(orig_state));
108 FOR_EACH_PTR(estate_related(orig_state), rel) {
109 set_true_false_states(SMATCH_EXTRA, rel->name, rel->sym,
110 true_state, false_state);
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_list(struct smatch_state *orig,
131 struct range_list *rl)
133 struct range_list *res;
134 struct data_range *tmp;
136 if (!orig)
137 orig = extra_undefined();
138 res = estate_ranges(orig);
140 FOR_EACH_PTR(rl, tmp) {
141 res = remove_range(res, tmp->min, tmp->max);
142 } END_FOR_EACH_PTR(tmp);
144 return alloc_estate_range_list(res);
147 struct smatch_state *filter_range(struct smatch_state *orig,
148 long long filter_min, long long filter_max)
150 struct range_list *rl;
152 if (!orig)
153 orig = extra_undefined();
155 rl = remove_range(estate_ranges(orig), filter_min, filter_max);
156 return alloc_estate_range_list(rl);
159 struct smatch_state *add_filter(struct smatch_state *orig, long long num)
161 return filter_range(orig, num, num);
164 static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
166 struct smatch_state *tmp;
167 struct range_list *value_ranges;
168 struct related_list *rlist;
170 if (estates_equiv(s1, s2))
171 return s1;
173 value_ranges = range_list_union(estate_ranges(s1), estate_ranges(s2));
174 tmp = alloc_estate_range_list(value_ranges);
175 rlist = get_shared_relations(estate_related(s1), estate_related(s2));
176 set_related(&tmp, rlist);
178 return tmp;
181 static struct sm_state *handle_canonical_while_count_down(struct statement *loop)
183 struct expression *iter_var;
184 struct expression *condition;
185 struct sm_state *sm;
186 long long start;
188 condition = strip_expr(loop->iterator_pre_condition);
189 if (!condition)
190 return NULL;
191 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
192 return NULL;
193 if (condition->op != SPECIAL_DECREMENT)
194 return NULL;
196 iter_var = condition->unop;
197 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
198 if (!sm)
199 return NULL;
200 if (estate_min(sm->state) < 0)
201 return NULL;
202 start = estate_max(sm->state);
203 if (start <= 0)
204 return NULL;
205 if (start != whole_range.max)
206 start--;
208 if (condition->type == EXPR_PREOP)
209 set_extra_expr_mod(iter_var, alloc_estate_range(1, start));
210 if (condition->type == EXPR_POSTOP)
211 set_extra_expr_mod(iter_var, alloc_estate_range(0, start));
212 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
215 static struct sm_state *handle_canonical_for_inc(struct expression *iter_expr,
216 struct expression *condition)
218 struct expression *iter_var;
219 struct sm_state *sm;
220 long long start;
221 long long end;
223 iter_var = iter_expr->unop;
224 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
225 if (!sm)
226 return NULL;
227 if (!estate_get_single_value(sm->state, &start))
228 return NULL;
229 if (!get_implied_value(condition->right, &end))
230 end = whole_range.max;
231 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
232 return NULL;
234 switch (condition->op) {
235 case SPECIAL_NOTEQUAL:
236 case '<':
237 if (end != whole_range.max)
238 end--;
239 break;
240 case SPECIAL_LTE:
241 break;
242 default:
243 return NULL;
245 if (end < start)
246 return NULL;
247 set_extra_expr_mod(iter_var, alloc_estate_range(start, end));
248 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
251 static struct sm_state *handle_canonical_for_dec(struct expression *iter_expr,
252 struct expression *condition)
254 struct expression *iter_var;
255 struct sm_state *sm;
256 long long start;
257 long long end;
259 iter_var = iter_expr->unop;
260 sm = get_sm_state_expr(SMATCH_EXTRA, iter_var);
261 if (!sm)
262 return NULL;
263 if (!estate_get_single_value(sm->state, &start))
264 return NULL;
265 if (!get_implied_value(condition->right, &end))
266 end = whole_range.max;
267 if (get_sm_state_expr(SMATCH_EXTRA, condition->left) != sm)
268 return NULL;
270 switch (condition->op) {
271 case SPECIAL_NOTEQUAL:
272 case '>':
273 if (end != whole_range.min)
274 end++;
275 break;
276 case SPECIAL_GTE:
277 break;
278 default:
279 return NULL;
281 if (end > start)
282 return NULL;
283 set_extra_expr_mod(iter_var, alloc_estate_range(end, start));
284 return get_sm_state_expr(SMATCH_EXTRA, iter_var);
287 static struct sm_state *handle_canonical_for_loops(struct statement *loop)
289 struct expression *iter_expr;
290 struct expression *condition;
292 if (!loop->iterator_post_statement)
293 return NULL;
294 if (loop->iterator_post_statement->type != STMT_EXPRESSION)
295 return NULL;
296 iter_expr = loop->iterator_post_statement->expression;
297 if (!loop->iterator_pre_condition)
298 return NULL;
299 if (loop->iterator_pre_condition->type != EXPR_COMPARE)
300 return NULL;
301 condition = loop->iterator_pre_condition;
303 if (iter_expr->op == SPECIAL_INCREMENT)
304 return handle_canonical_for_inc(iter_expr, condition);
305 if (iter_expr->op == SPECIAL_DECREMENT)
306 return handle_canonical_for_dec(iter_expr, condition);
307 return NULL;
310 struct sm_state *__extra_handle_canonical_loops(struct statement *loop, struct state_list **slist)
312 struct sm_state *ret;
314 __push_fake_cur_slist();
315 if (!loop->iterator_post_statement)
316 ret = handle_canonical_while_count_down(loop);
317 else
318 ret = handle_canonical_for_loops(loop);
319 *slist = __pop_fake_cur_slist();
320 return ret;
323 int __iterator_unchanged(struct sm_state *sm)
325 if (!sm)
326 return 0;
327 if (get_sm_state(my_id, sm->name, sm->sym) == sm)
328 return 1;
329 return 0;
332 static void while_count_down_after(struct sm_state *sm, struct expression *condition)
334 long long after_value;
336 /* paranoid checking. prolly not needed */
337 condition = strip_expr(condition);
338 if (!condition)
339 return;
340 if (condition->type != EXPR_PREOP && condition->type != EXPR_POSTOP)
341 return;
342 if (condition->op != SPECIAL_DECREMENT)
343 return;
344 after_value = estate_min(sm->state);
345 after_value--;
346 set_extra_mod(sm->name, sm->sym, alloc_estate(after_value));
349 void __extra_pre_loop_hook_after(struct sm_state *sm,
350 struct statement *iterator,
351 struct expression *condition)
353 struct expression *iter_expr;
354 char *name;
355 struct symbol *sym;
356 long long value;
357 int left = 0;
358 struct smatch_state *state;
359 long long min, max;
361 if (!iterator) {
362 while_count_down_after(sm, condition);
363 return;
366 iter_expr = iterator->expression;
368 if (condition->type != EXPR_COMPARE)
369 return;
370 if (!get_value(condition->left, &value)) {
371 if (!get_value(condition->right, &value))
372 return;
373 left = 1;
375 if (left)
376 name = get_variable_from_expr(condition->left, &sym);
377 else
378 name = get_variable_from_expr(condition->right, &sym);
379 if (!name || !sym)
380 goto free;
381 if (sym != sm->sym || strcmp(name, sm->name))
382 goto free;
383 state = get_state(my_id, name, sym);
384 min = estate_min(state);
385 max = estate_max(state);
386 if (iter_expr->op == SPECIAL_INCREMENT &&
387 min != whole_range.min &&
388 max == whole_range.max) {
389 set_extra_mod(name, sym, alloc_estate(min));
390 } else if (min == whole_range.min && max != whole_range.max) {
391 set_extra_mod(name, sym, alloc_estate(max));
393 free:
394 free_string(name);
395 return;
398 static struct smatch_state *unmatched_state(struct sm_state *sm)
400 return extra_undefined();
403 static void match_function_call(struct expression *expr)
405 struct expression *arg;
406 struct expression *tmp;
408 FOR_EACH_PTR(expr->args, arg) {
409 tmp = strip_expr(arg);
410 if (tmp->type == EXPR_PREOP && tmp->op == '&') {
411 remove_from_equiv_expr(tmp->unop);
412 set_state_expr(SMATCH_EXTRA, tmp->unop, extra_undefined());
414 } END_FOR_EACH_PTR(arg);
417 static void match_assign(struct expression *expr)
419 struct expression *left;
420 struct expression *right;
421 struct symbol *right_sym;
422 char *right_name;
423 struct symbol *sym;
424 char *name;
425 long long value;
426 int known;
427 long long min = whole_range.min;
428 long long max = whole_range.max;
429 long long tmp;
431 if (__is_condition_assign(expr))
432 return;
433 left = strip_expr(expr->left);
434 name = get_variable_from_expr(left, &sym);
435 if (!name)
436 return;
437 right = strip_parens(expr->right);
438 while (right->type == EXPR_ASSIGNMENT && right->op == '=')
439 right = strip_parens(right->left);
441 if (expr->op == '=' && right->type == EXPR_CALL)
442 return; /* these are handled in match_call_assign() */
444 right_name = get_variable_from_expr(right, &right_sym);
445 if (expr->op == '=' && right_name && right_sym) {
446 set_equiv(left, right);
447 goto free;
450 known = get_implied_value(right, &value);
451 switch (expr->op) {
452 case '=': {
453 struct range_list *rl = NULL;
455 if (get_implied_range_list(right, &rl)) {
456 set_extra_mod(name, sym, alloc_estate_range_list(rl));
457 goto free;
460 if (expr_unsigned(right))
461 min = 0;
462 break;
464 case SPECIAL_ADD_ASSIGN:
465 if (get_implied_min(left, &tmp)) {
466 if (known)
467 min = tmp + value;
468 else
469 min = tmp;
471 if (!inside_loop() && known && get_implied_max(left, &tmp))
472 max = tmp + value;
473 break;
474 case SPECIAL_SUB_ASSIGN:
475 if (get_implied_max(left, &tmp)) {
476 if (known)
477 max = tmp - value;
478 else
479 max = tmp;
481 if (!inside_loop() && known && get_implied_min(left, &tmp))
482 min = tmp - value;
483 break;
485 set_extra_mod(name, sym, alloc_estate_range(min, max));
486 free:
487 free_string(right_name);
488 free_string(name);
491 static void reset_struct_members(struct sm_state *sm)
493 set_extra_mod(sm->name, sm->sym, extra_undefined());
496 static struct smatch_state *increment_state(struct smatch_state *state)
498 long long min = estate_min(state);
499 long long max = estate_max(state);
501 if (inside_loop())
502 max = whole_range.max;
504 if (min != whole_range.min)
505 min++;
506 if (max != whole_range.max)
507 max++;
508 return alloc_estate_range(min, max);
511 static struct smatch_state *decrement_state(struct smatch_state *state)
513 long long min = estate_min(state);
514 long long max = estate_max(state);
516 if (inside_loop())
517 min = whole_range.min;
519 if (min != whole_range.min)
520 min--;
521 if (max != whole_range.max)
522 max--;
523 return alloc_estate_range(min, max);
526 static void unop_expr(struct expression *expr)
528 struct smatch_state *state;
530 if (expr->smatch_flags & Handled)
531 return;
533 switch (expr->op) {
534 case SPECIAL_INCREMENT:
535 state = get_state_expr(SMATCH_EXTRA, expr->unop);
536 state = increment_state(state);
537 set_extra_expr_mod(expr->unop, state);
538 break;
539 case SPECIAL_DECREMENT:
540 state = get_state_expr(SMATCH_EXTRA, expr->unop);
541 state = decrement_state(state);
542 set_extra_expr_mod(expr->unop, state);
543 break;
544 default:
545 return;
549 static void asm_expr(struct statement *stmt)
552 struct expression *expr;
553 int state = 0;
555 FOR_EACH_PTR(stmt->asm_outputs, expr) {
556 switch (state) {
557 case 0: /* identifier */
558 case 1: /* constraint */
559 state++;
560 continue;
561 case 2: /* expression */
562 state = 0;
563 set_extra_expr_mod(expr, extra_undefined());
564 continue;
566 } END_FOR_EACH_PTR(expr);
569 static void delete_state_tracker(struct tracker *t)
571 remove_from_equiv(t->name, t->sym);
572 delete_state(t->owner, t->name, t->sym);
575 static void scoped_state_extra(const char *name, struct symbol *sym)
577 struct tracker *t;
579 t = alloc_tracker(SMATCH_EXTRA, name, sym);
580 add_scope_hook((scope_hook *)&delete_state_tracker, t);
583 static void match_declarations(struct symbol *sym)
585 const char *name;
587 if (sym->ident) {
588 name = sym->ident->name;
589 if (!sym->initializer) {
590 set_state(SMATCH_EXTRA, name, sym, extra_undefined());
591 scoped_state_extra(name, sym);
596 static void check_dereference(struct expression *expr)
598 set_extra_expr_nomod(expr, alloc_estate_range(valid_ptr_min, valid_ptr_max));
601 static void match_dereferences(struct expression *expr)
603 if (expr->type != EXPR_PREOP)
604 return;
605 check_dereference(expr->unop);
608 static void match_pointer_as_array(struct expression *expr)
610 if (!is_array(expr))
611 return;
612 check_dereference(expr->unop->left);
615 static void set_param_dereferenced(struct expression *arg, char *unused)
617 check_dereference(arg);
620 static void match_function_def(struct symbol *sym)
622 struct symbol *arg;
624 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
625 if (!arg->ident)
626 continue;
627 set_state(my_id, arg->ident->name, arg, extra_undefined());
628 } END_FOR_EACH_PTR(arg);
631 static int match_func_comparison(struct expression *expr)
633 struct expression *left = strip_expr(expr->left);
634 struct expression *right = strip_expr(expr->right);
635 long long val;
637 if (left->type == EXPR_CALL) {
638 if (!get_implied_value(right, &val))
639 return 1;
640 function_comparison(expr->op, left, val, 1);
641 return 1;
644 if (right->type == EXPR_CALL) {
645 if (!get_implied_value(left, &val))
646 return 1;
647 function_comparison(expr->op, right, val, 0);
648 return 1;
651 return 0;
654 static void match_comparison(struct expression *expr)
656 struct expression *left = strip_expr(expr->left);
657 struct expression *right = strip_expr(expr->right);
658 struct range_list *left_orig;
659 struct range_list *left_true;
660 struct range_list *left_false;
661 struct range_list *right_orig;
662 struct range_list *right_true;
663 struct range_list *right_false;
664 struct smatch_state *left_true_state;
665 struct smatch_state *left_false_state;
666 struct smatch_state *right_true_state;
667 struct smatch_state *right_false_state;
668 int left_postop = 0;
669 int right_postop = 0;
671 if (match_func_comparison(expr))
672 return;
674 if (left->op == SPECIAL_INCREMENT || left->op == SPECIAL_DECREMENT) {
675 if (left->type == EXPR_POSTOP) {
676 left->smatch_flags |= Handled;
677 left_postop = left->op;
679 left = strip_expr(left->unop);
682 if (right->op == SPECIAL_INCREMENT || right->op == SPECIAL_DECREMENT) {
683 if (right->type == EXPR_POSTOP) {
684 right->smatch_flags |= Handled;
685 right_postop = right->op;
687 right = strip_expr(right->unop);
690 if (!get_implied_range_list(left, &left_orig))
691 left_orig = alloc_range_list(whole_range.min, whole_range.max);
692 if (!get_implied_range_list(right, &right_orig))
693 right_orig = alloc_range_list(whole_range.min, whole_range.max);
695 left_true = clone_range_list(left_orig);
696 left_false = clone_range_list(left_orig);
697 right_true = clone_range_list(right_orig);
698 right_false = clone_range_list(right_orig);
700 switch (expr->op) {
701 case '<':
702 case SPECIAL_UNSIGNED_LT:
703 if (rl_max(right_orig) != whole_range.max)
704 left_true = remove_range(left_orig, rl_max(right_orig), whole_range.max);
705 if (rl_min(right_orig) != whole_range.min)
706 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
708 if (rl_min(left_orig) != whole_range.min)
709 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig));
710 if (rl_max(left_orig) != whole_range.max)
711 right_false = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
712 break;
713 case SPECIAL_UNSIGNED_LTE:
714 case SPECIAL_LTE:
715 if (rl_max(right_orig) != whole_range.max)
716 left_true = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
717 if (rl_min(right_orig) != whole_range.min)
718 left_false = remove_range(left_orig, whole_range.min, rl_min(right_orig));
720 if (rl_min(left_orig) != whole_range.min)
721 right_true = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
722 if (rl_max(left_orig) != whole_range.max)
723 right_false = remove_range(right_orig, rl_max(left_orig), whole_range.max);
724 break;
725 case SPECIAL_EQUAL:
726 if (rl_max(right_orig) != whole_range.max)
727 left_true = remove_range(left_true, rl_max(right_orig) + 1, whole_range.max);
728 if (rl_min(right_orig) != whole_range.min)
729 left_true = remove_range(left_true, whole_range.min, rl_min(right_orig) - 1);
730 if (rl_min(right_orig) == rl_max(right_orig))
731 left_false = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
733 if (rl_max(left_orig) != whole_range.max)
734 right_true = remove_range(right_true, rl_max(left_orig) + 1, whole_range.max);
735 if (rl_min(left_orig) != whole_range.min)
736 right_true = remove_range(right_true, whole_range.min, rl_min(left_orig) - 1);
737 if (rl_min(left_orig) == rl_max(left_orig))
738 right_false = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
739 break;
740 case SPECIAL_UNSIGNED_GTE:
741 case SPECIAL_GTE:
742 if (rl_min(right_orig) != whole_range.min)
743 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig) - 1);
744 if (rl_max(right_orig) != whole_range.max)
745 left_false = remove_range(left_orig, rl_max(right_orig), whole_range.max);
747 if (rl_max(left_orig) != whole_range.max)
748 right_true = remove_range(right_orig, rl_max(left_orig) + 1, whole_range.max);
749 if (rl_min(left_orig) != whole_range.min)
750 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig));
751 break;
752 case '>':
753 case SPECIAL_UNSIGNED_GT:
754 if (rl_min(right_orig) != whole_range.min)
755 left_true = remove_range(left_orig, whole_range.min, rl_min(right_orig));
756 if (rl_max(right_orig) != whole_range.max)
757 left_false = remove_range(left_orig, rl_max(right_orig) + 1, whole_range.max);
759 if (rl_max(left_orig) != whole_range.max)
760 right_true = remove_range(right_orig, rl_max(left_orig), whole_range.max);
761 if (rl_min(left_orig) != whole_range.min)
762 right_false = remove_range(right_orig, whole_range.min, rl_min(left_orig) - 1);
763 break;
764 case SPECIAL_NOTEQUAL:
765 if (rl_max(right_orig) != whole_range.max)
766 left_false = remove_range(left_false, rl_max(right_orig) + 1, whole_range.max);
767 if (rl_min(right_orig) != whole_range.min)
768 left_false = remove_range(left_false, whole_range.min, rl_min(right_orig) - 1);
769 if (rl_min(right_orig) == rl_max(right_orig))
770 left_true = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
772 if (rl_max(left_orig) != whole_range.max)
773 right_false = remove_range(right_false, rl_max(left_orig) + 1, whole_range.max);
774 if (rl_min(left_orig) != whole_range.min)
775 right_false = remove_range(right_false, whole_range.min, rl_min(left_orig) - 1);
776 if (rl_min(left_orig) == rl_max(left_orig))
777 right_true = remove_range(right_orig, rl_min(left_orig), rl_min(left_orig));
778 break;
779 default:
780 return;
783 left_true_state = alloc_estate_range_list(left_true);
784 left_false_state = alloc_estate_range_list(left_false);
785 right_true_state = alloc_estate_range_list(right_true);
786 right_false_state = alloc_estate_range_list(right_false);
788 if (left_postop == SPECIAL_INCREMENT) {
789 left_true_state = increment_state(left_true_state);
790 left_false_state = increment_state(left_false_state);
792 if (left_postop == SPECIAL_DECREMENT) {
793 left_true_state = decrement_state(left_true_state);
794 left_false_state = decrement_state(left_false_state);
796 if (right_postop == SPECIAL_INCREMENT) {
797 right_true_state = increment_state(right_true_state);
798 right_false_state = increment_state(right_false_state);
800 if (right_postop == SPECIAL_DECREMENT) {
801 right_true_state = decrement_state(right_true_state);
802 right_false_state = decrement_state(right_false_state);
805 set_extra_expr_true_false(left, left_true_state, left_false_state);
806 set_extra_expr_true_false(right, right_true_state, right_false_state);
809 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
810 void __extra_match_condition(struct expression *expr)
812 struct symbol *sym;
813 char *name;
814 struct smatch_state *pre_state;
815 struct smatch_state *true_state;
816 struct smatch_state *false_state;
818 expr = strip_expr(expr);
819 switch (expr->type) {
820 case EXPR_CALL:
821 function_comparison(SPECIAL_NOTEQUAL, expr, 0, 1);
822 return;
823 case EXPR_PREOP:
824 case EXPR_SYMBOL:
825 case EXPR_DEREF:
826 name = get_variable_from_expr(expr, &sym);
827 if (!name)
828 return;
829 pre_state = get_state(my_id, name, sym);
830 true_state = add_filter(pre_state, 0);
831 if (possibly_true(expr, SPECIAL_EQUAL, zero_expr()))
832 false_state = alloc_estate(0);
833 else
834 false_state = alloc_estate_empty();
835 set_extra_true_false(name, sym, true_state, false_state);
836 free_string(name);
837 return;
838 case EXPR_COMPARE:
839 match_comparison(expr);
840 return;
841 case EXPR_ASSIGNMENT:
842 __extra_match_condition(expr->left);
843 return;
847 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
848 int implied_not_equal(struct expression *expr, long long val)
850 return !possibly_false(expr, SPECIAL_NOTEQUAL, value_expr(val));
853 int get_implied_range_list(struct expression *expr, struct range_list **rl)
855 long long val;
856 struct smatch_state *state;
857 long long min;
858 long long max;
860 *rl = NULL;
862 expr = strip_parens(expr);
863 if (!expr)
864 return 0;
866 state = get_state_expr(my_id, expr);
867 if (state) {
868 *rl = clone_range_list(estate_ranges(state));
869 goto out;
872 if (expr->type == EXPR_CALL) {
873 if (get_implied_return(expr, rl))
874 goto out;
875 *rl = db_return_vals(expr);
876 goto out;
879 if (get_implied_value(expr, &val)) {
880 add_range(rl, val, val);
881 goto out;
884 if (expr->type == EXPR_BINOP && expr->op == '%') {
885 if (!get_implied_value(expr->right, &val))
886 return 0;
887 add_range(rl, 0, val - 1);
888 goto out;
891 if (!get_implied_min(expr, &min))
892 return 0;
893 if (!get_implied_max(expr, &max))
894 return 0;
896 *rl = alloc_range_list(min, max);
898 out:
899 if (is_whole_range_rl(*rl))
900 return 0;
901 return 1;
904 int is_whole_range(struct smatch_state *state)
906 return is_whole_range_rl(estate_ranges(state));
909 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
911 if (is_whole_range(state))
912 return;
913 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn, param, printed_name, state->name, global_static);
916 static void match_call_info(struct expression *expr)
918 struct range_list *rl = NULL;
919 struct expression *arg;
920 char *name;
921 int i = 0;
923 name = get_fnptr_name(expr->fn);
924 if (!name)
925 return;
927 FOR_EACH_PTR(expr->args, arg) {
928 if (get_implied_range_list(arg, &rl) && !is_whole_range_rl(rl)) {
929 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
930 name, i, show_ranges(rl),
931 is_static(expr->fn) ? "static" : "global");
933 i++;
934 } END_FOR_EACH_PTR(arg);
936 free_string(name);
939 static void set_param_value(const char *name, struct symbol *sym, char *key, char *value)
941 struct range_list *rl = NULL;
942 struct smatch_state *state;
943 char fullname[256];
945 if (strncmp(key, "$$", 2))
946 return;
948 snprintf(fullname, 256, "%s%s", name, key + 2);
949 get_value_ranges(value, &rl);
950 state = alloc_estate_range_list(rl);
951 set_state(SMATCH_EXTRA, fullname, sym, state);
954 static void match_call_assign(struct expression *expr)
956 if (expr->op != '=')
957 return;
959 /* if we have a db set up this gets set in smatch_function_hooks.c */
960 if (option_no_db)
961 set_extra_expr_mod(expr->left, extra_undefined());
964 void register_smatch_extra(int id)
966 my_id = id;
968 alloc_estate_undefined();
969 add_merge_hook(my_id, &merge_func);
970 add_unmatched_state_hook(my_id, &unmatched_state);
971 add_hook(&unop_expr, OP_HOOK);
972 add_hook(&asm_expr, ASM_HOOK);
973 add_hook(&match_function_def, FUNC_DEF_HOOK);
974 add_hook(&match_declarations, DECLARATION_HOOK);
975 if (option_info) {
976 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
977 add_member_info_callback(my_id, struct_member_callback);
979 add_definition_db_callback(set_param_value, PARAM_VALUE);
982 void register_smatch_extra_late(int id)
984 add_hook(&match_dereferences, DEREF_HOOK);
985 add_hook(&match_pointer_as_array, OP_HOOK);
986 add_db_fn_call_callback(DEREFERENCE, &set_param_dereferenced);
987 add_indirect_modification_hook(SMATCH_EXTRA, reset_struct_members);
988 add_hook(&match_function_call, FUNCTION_CALL_HOOK);
989 add_hook(&match_assign, ASSIGNMENT_HOOK);
990 add_hook(&match_call_assign, CALL_ASSIGNMENT_HOOK);