2 * sparse/smatch_extra.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * smatch_extra.c is supposed to track the value of every variable.
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
28 struct string_list
*__ignored_macros
= NULL
;
29 static int in_warn_on_macro()
31 struct statement
*stmt
;
35 stmt
= get_current_statement();
36 macro
= get_macro_name(stmt
->pos
);
40 FOR_EACH_PTR(__ignored_macros
, tmp
) {
41 if (!strcmp(tmp
, macro
))
43 } END_FOR_EACH_PTR(tmp
);
47 struct sm_state
*set_extra_mod(const char *name
, struct symbol
*sym
, struct smatch_state
*state
)
49 if (in_warn_on_macro())
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())
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
)
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
);
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
)
91 struct smatch_state
*orig_state
;
93 if (in_warn_on_macro())
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
);
104 set_related(&true_state
, estate_related(orig_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
)
121 expr
= strip_expr(expr
);
122 name
= get_variable_from_expr(expr
, &sym
);
125 set_extra_true_false(name
, sym
, true_state
, false_state
);
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
;
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
;
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
))
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
);
181 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
183 struct expression
*iter_var
;
184 struct expression
*condition
;
188 condition
= strip_expr(loop
->iterator_pre_condition
);
191 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
193 if (condition
->op
!= SPECIAL_DECREMENT
)
196 iter_var
= condition
->unop
;
197 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
200 if (estate_min(sm
->state
) < 0)
202 start
= estate_max(sm
->state
);
205 if (start
!= whole_range
.max
)
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
;
223 iter_var
= iter_expr
->unop
;
224 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
227 if (!estate_get_single_value(sm
->state
, &start
))
229 if (!get_implied_value(condition
->right
, &end
))
230 end
= whole_range
.max
;
231 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
234 switch (condition
->op
) {
235 case SPECIAL_NOTEQUAL
:
237 if (end
!= whole_range
.max
)
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
;
259 iter_var
= iter_expr
->unop
;
260 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
263 if (!estate_get_single_value(sm
->state
, &start
))
265 if (!get_implied_value(condition
->right
, &end
))
266 end
= whole_range
.max
;
267 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
270 switch (condition
->op
) {
271 case SPECIAL_NOTEQUAL
:
273 if (end
!= whole_range
.min
)
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
)
294 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
296 iter_expr
= loop
->iterator_post_statement
->expression
;
297 if (!loop
->iterator_pre_condition
)
299 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
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
);
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
);
318 ret
= handle_canonical_for_loops(loop
);
319 *slist
= __pop_fake_cur_slist();
323 int __iterator_unchanged(struct sm_state
*sm
)
327 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
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
);
340 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
342 if (condition
->op
!= SPECIAL_DECREMENT
)
344 after_value
= estate_min(sm
->state
);
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
;
358 struct smatch_state
*state
;
362 while_count_down_after(sm
, condition
);
366 iter_expr
= iterator
->expression
;
368 if (condition
->type
!= EXPR_COMPARE
)
370 if (!get_value(condition
->left
, &value
)) {
371 if (!get_value(condition
->right
, &value
))
376 name
= get_variable_from_expr(condition
->left
, &sym
);
378 name
= get_variable_from_expr(condition
->right
, &sym
);
381 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
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
));
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
;
427 long long min
= whole_range
.min
;
428 long long max
= whole_range
.max
;
431 if (__is_condition_assign(expr
))
433 left
= strip_expr(expr
->left
);
434 name
= get_variable_from_expr(left
, &sym
);
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
);
450 known
= get_implied_value(right
, &value
);
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
));
460 if (expr_unsigned(right
))
464 case SPECIAL_ADD_ASSIGN
:
465 if (get_implied_min(left
, &tmp
)) {
471 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
474 case SPECIAL_SUB_ASSIGN
:
475 if (get_implied_max(left
, &tmp
)) {
481 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
485 set_extra_mod(name
, sym
, alloc_estate_range(min
, max
));
487 free_string(right_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
);
502 max
= whole_range
.max
;
504 if (min
!= whole_range
.min
)
506 if (max
!= whole_range
.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
);
517 min
= whole_range
.min
;
519 if (min
!= whole_range
.min
)
521 if (max
!= whole_range
.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
)
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
);
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
);
549 static void asm_expr(struct statement
*stmt
)
552 struct expression
*expr
;
555 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
557 case 0: /* identifier */
558 case 1: /* constraint */
561 case 2: /* expression */
563 set_extra_expr_mod(expr
, extra_undefined());
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
)
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
)
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
)
605 check_dereference(expr
->unop
);
608 static void match_pointer_as_array(struct expression
*expr
)
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
)
624 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
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
);
637 if (left
->type
== EXPR_CALL
) {
638 if (!get_implied_value(right
, &val
))
640 function_comparison(expr
->op
, left
, val
, 1);
644 if (right
->type
== EXPR_CALL
) {
645 if (!get_implied_value(left
, &val
))
647 function_comparison(expr
->op
, right
, val
, 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
;
669 int right_postop
= 0;
671 if (match_func_comparison(expr
))
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
);
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
);
713 case SPECIAL_UNSIGNED_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
);
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
));
740 case SPECIAL_UNSIGNED_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
));
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);
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
));
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
)
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
) {
821 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
826 name
= get_variable_from_expr(expr
, &sym
);
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);
834 false_state
= alloc_estate_empty();
835 set_extra_true_false(name
, sym
, true_state
, false_state
);
839 match_comparison(expr
);
841 case EXPR_ASSIGNMENT
:
842 __extra_match_condition(expr
->left
);
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
)
856 struct smatch_state
*state
;
862 expr
= strip_parens(expr
);
866 state
= get_state_expr(my_id
, expr
);
868 *rl
= clone_range_list(estate_ranges(state
));
872 if (expr
->type
== EXPR_CALL
) {
873 if (get_implied_return(expr
, rl
))
875 *rl
= db_return_vals(expr
);
879 if (get_implied_value(expr
, &val
)) {
880 add_range(rl
, val
, val
);
884 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
885 if (!get_implied_value(expr
->right
, &val
))
887 add_range(rl
, 0, val
- 1);
891 if (!get_implied_min(expr
, &min
))
893 if (!get_implied_max(expr
, &max
))
896 *rl
= alloc_range_list(min
, max
);
899 if (is_whole_range_rl(*rl
))
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
))
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
;
923 name
= get_fnptr_name(expr
->fn
);
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");
934 } END_FOR_EACH_PTR(arg
);
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
;
945 if (strncmp(key
, "$$", 2))
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
)
959 /* if we have a db set up this gets set in smatch_function_hooks.c */
961 set_extra_expr_mod(expr
->left
, extra_undefined());
964 void register_smatch_extra(int 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
);
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
);