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
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
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 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
)
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
);
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
);
108 add_equiv(true_state
, rel
->name
, rel
->sym
);
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
)
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(struct smatch_state
*orig
,
131 long long filter_min
, long long filter_max
)
133 struct range_list
*rl
;
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
;
153 struct relation
*rel
;
154 struct relation
*new;
156 value_ranges
= range_list_union(estate_ranges(s1
), estate_ranges(s2
));
157 tmp
= alloc_estate_range_list(value_ranges
);
158 FOR_EACH_PTR(estate_related(s1
), rel
) {
159 new = get_common_relationship(s2
, rel
->op
, rel
->name
, rel
->sym
);
161 add_related(tmp
, new->op
, new->name
, new->sym
);
162 } END_FOR_EACH_PTR(rel
);
166 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
168 struct expression
*iter_var
;
169 struct expression
*condition
;
173 condition
= strip_expr(loop
->iterator_pre_condition
);
176 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
178 if (condition
->op
!= SPECIAL_DECREMENT
)
181 iter_var
= condition
->unop
;
182 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
185 if (estate_min(sm
->state
) < 0)
187 start
= estate_max(sm
->state
);
190 if (start
!= whole_range
.max
)
193 if (condition
->type
== EXPR_PREOP
)
194 set_extra_expr_mod(iter_var
, alloc_estate_range(1, start
));
195 if (condition
->type
== EXPR_POSTOP
)
196 set_extra_expr_mod(iter_var
, alloc_estate_range(0, start
));
197 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
200 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
201 struct expression
*condition
)
203 struct expression
*iter_var
;
208 iter_var
= iter_expr
->unop
;
209 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
212 if (!estate_get_single_value(sm
->state
, &start
))
214 if (!get_implied_value(condition
->right
, &end
))
215 end
= whole_range
.max
;
216 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
219 switch (condition
->op
) {
220 case SPECIAL_NOTEQUAL
:
222 if (end
!= whole_range
.max
)
232 set_extra_expr_mod(iter_var
, alloc_estate_range(start
, end
));
233 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
236 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
237 struct expression
*condition
)
239 struct expression
*iter_var
;
244 iter_var
= iter_expr
->unop
;
245 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
248 if (!estate_get_single_value(sm
->state
, &start
))
250 if (!get_implied_value(condition
->right
, &end
))
251 end
= whole_range
.max
;
252 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
255 switch (condition
->op
) {
256 case SPECIAL_NOTEQUAL
:
258 if (end
!= whole_range
.min
)
268 set_extra_expr_mod(iter_var
, alloc_estate_range(end
, start
));
269 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
272 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
274 struct expression
*iter_expr
;
275 struct expression
*condition
;
277 if (!loop
->iterator_post_statement
)
279 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
281 iter_expr
= loop
->iterator_post_statement
->expression
;
282 if (!loop
->iterator_pre_condition
)
284 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
286 condition
= loop
->iterator_pre_condition
;
288 if (iter_expr
->op
== SPECIAL_INCREMENT
)
289 return handle_canonical_for_inc(iter_expr
, condition
);
290 if (iter_expr
->op
== SPECIAL_DECREMENT
)
291 return handle_canonical_for_dec(iter_expr
, condition
);
295 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct state_list
**slist
)
297 struct sm_state
*ret
;
299 __push_fake_cur_slist();
300 if (!loop
->iterator_post_statement
)
301 ret
= handle_canonical_while_count_down(loop
);
303 ret
= handle_canonical_for_loops(loop
);
304 *slist
= __pop_fake_cur_slist();
308 int __iterator_unchanged(struct sm_state
*sm
)
312 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
317 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
319 long long after_value
;
321 /* paranoid checking. prolly not needed */
322 condition
= strip_expr(condition
);
325 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
327 if (condition
->op
!= SPECIAL_DECREMENT
)
329 after_value
= estate_min(sm
->state
);
331 set_extra_mod(sm
->name
, sm
->sym
, alloc_estate(after_value
));
334 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
335 struct statement
*iterator
,
336 struct expression
*condition
)
338 struct expression
*iter_expr
;
343 struct smatch_state
*state
;
347 while_count_down_after(sm
, condition
);
351 iter_expr
= iterator
->expression
;
353 if (condition
->type
!= EXPR_COMPARE
)
355 if (!get_value(condition
->left
, &value
)) {
356 if (!get_value(condition
->right
, &value
))
361 name
= get_variable_from_expr(condition
->left
, &sym
);
363 name
= get_variable_from_expr(condition
->right
, &sym
);
366 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
368 state
= get_state(my_id
, name
, sym
);
369 min
= estate_min(state
);
370 max
= estate_max(state
);
371 if (iter_expr
->op
== SPECIAL_INCREMENT
&&
372 min
!= whole_range
.min
&&
373 max
== whole_range
.max
) {
374 set_extra_mod(name
, sym
, alloc_estate(min
));
375 } else if (min
== whole_range
.min
&& max
!= whole_range
.max
) {
376 set_extra_mod(name
, sym
, alloc_estate(max
));
383 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
385 return extra_undefined();
388 static void match_function_call(struct expression
*expr
)
390 struct expression
*arg
;
391 struct expression
*tmp
;
393 FOR_EACH_PTR(expr
->args
, arg
) {
394 tmp
= strip_expr(arg
);
395 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&') {
396 remove_from_equiv_expr(tmp
->unop
);
397 set_state_expr(SMATCH_EXTRA
, tmp
->unop
, extra_undefined());
399 } END_FOR_EACH_PTR(arg
);
402 static void match_assign(struct expression
*expr
)
404 struct expression
*left
;
405 struct expression
*right
;
406 struct symbol
*right_sym
;
412 long long min
= whole_range
.min
;
413 long long max
= whole_range
.max
;
416 if (__is_condition_assign(expr
))
418 left
= strip_expr(expr
->left
);
419 name
= get_variable_from_expr(left
, &sym
);
422 right
= strip_parens(expr
->right
);
423 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
424 right
= strip_parens(right
->left
);
426 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
427 return; /* these are handled in match_call_assign() */
429 right_name
= get_variable_from_expr(right
, &right_sym
);
430 if (expr
->op
== '=' && right_name
&& right_sym
) {
431 set_equiv(left
, right
);
435 known
= get_implied_value(right
, &value
);
438 struct range_list
*rl
= NULL
;
440 if (get_implied_range_list(right
, &rl
)) {
441 set_extra_mod(name
, sym
, alloc_estate_range_list(rl
));
445 if (expr_unsigned(right
))
449 case SPECIAL_ADD_ASSIGN
:
450 if (get_implied_min(left
, &tmp
)) {
456 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
459 case SPECIAL_SUB_ASSIGN
:
460 if (get_implied_max(left
, &tmp
)) {
466 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
470 set_extra_mod(name
, sym
, alloc_estate_range(min
, max
));
472 free_string(right_name
);
476 static void reset_struct_members(struct sm_state
*sm
)
478 set_extra_mod(sm
->name
, sm
->sym
, extra_undefined());
481 static struct smatch_state
*increment_state(struct smatch_state
*state
)
483 long long min
= estate_min(state
);
484 long long max
= estate_max(state
);
487 max
= whole_range
.max
;
489 if (min
!= whole_range
.min
)
491 if (max
!= whole_range
.max
)
493 return alloc_estate_range(min
, max
);
496 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
498 long long min
= estate_min(state
);
499 long long max
= estate_max(state
);
502 min
= whole_range
.min
;
504 if (min
!= whole_range
.min
)
506 if (max
!= whole_range
.max
)
508 return alloc_estate_range(min
, max
);
511 static void unop_expr(struct expression
*expr
)
513 struct smatch_state
*state
;
515 if (expr
->smatch_flags
& Handled
)
519 case SPECIAL_INCREMENT
:
520 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
521 state
= increment_state(state
);
522 set_extra_expr_mod(expr
->unop
, state
);
524 case SPECIAL_DECREMENT
:
525 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
526 state
= decrement_state(state
);
527 set_extra_expr_mod(expr
->unop
, state
);
534 static void delete_state_tracker(struct tracker
*t
)
536 remove_from_equiv(t
->name
, t
->sym
);
537 delete_state(t
->owner
, t
->name
, t
->sym
);
540 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
544 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
545 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
548 static void match_declarations(struct symbol
*sym
)
553 name
= sym
->ident
->name
;
554 if (!sym
->initializer
) {
555 set_state(SMATCH_EXTRA
, name
, sym
, extra_undefined());
556 scoped_state_extra(name
, sym
);
561 static void check_dereference(struct expression
*expr
)
563 set_extra_expr_nomod(expr
, alloc_estate_range(valid_ptr_min
, valid_ptr_max
));
566 static void match_dereferences(struct expression
*expr
)
568 if (expr
->type
!= EXPR_PREOP
)
570 check_dereference(expr
->unop
);
573 static void match_pointer_as_array(struct expression
*expr
)
577 check_dereference(expr
->unop
->left
);
580 static void set_param_dereferenced(struct expression
*arg
, char *unused
)
582 check_dereference(arg
);
585 static void match_function_def(struct symbol
*sym
)
589 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
592 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined());
593 } END_FOR_EACH_PTR(arg
);
596 static int match_func_comparison(struct expression
*expr
)
598 struct expression
*left
= strip_expr(expr
->left
);
599 struct expression
*right
= strip_expr(expr
->right
);
602 if (left
->type
== EXPR_CALL
) {
603 if (!get_implied_value(right
, &val
))
605 function_comparison(expr
->op
, left
, val
, 1);
609 if (right
->type
== EXPR_CALL
) {
610 if (!get_implied_value(left
, &val
))
612 function_comparison(expr
->op
, right
, val
, 0);
619 static void match_comparison(struct expression
*expr
)
621 struct expression
*left
= strip_expr(expr
->left
);
622 struct expression
*right
= strip_expr(expr
->right
);
623 struct range_list
*left_orig
;
624 struct range_list
*left_true
;
625 struct range_list
*left_false
;
626 struct range_list
*right_orig
;
627 struct range_list
*right_true
;
628 struct range_list
*right_false
;
629 struct smatch_state
*left_true_state
;
630 struct smatch_state
*left_false_state
;
631 struct smatch_state
*right_true_state
;
632 struct smatch_state
*right_false_state
;
634 int right_postop
= 0;
636 if (match_func_comparison(expr
))
639 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
640 if (left
->type
== EXPR_POSTOP
) {
641 left
->smatch_flags
|= Handled
;
642 left_postop
= left
->op
;
644 left
= strip_expr(left
->unop
);
647 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
648 if (right
->type
== EXPR_POSTOP
) {
649 right
->smatch_flags
|= Handled
;
650 right_postop
= right
->op
;
652 right
= strip_expr(right
->unop
);
655 if (!get_implied_range_list(left
, &left_orig
))
656 left_orig
= alloc_range_list(whole_range
.min
, whole_range
.max
);
657 if (!get_implied_range_list(right
, &right_orig
))
658 right_orig
= alloc_range_list(whole_range
.min
, whole_range
.max
);
660 left_true
= clone_range_list(left_orig
);
661 left_false
= clone_range_list(left_orig
);
662 right_true
= clone_range_list(right_orig
);
663 right_false
= clone_range_list(right_orig
);
667 case SPECIAL_UNSIGNED_LT
:
668 if (rl_max(right_orig
) != whole_range
.max
)
669 left_true
= remove_range(left_orig
, rl_max(right_orig
), whole_range
.max
);
670 if (rl_min(right_orig
) != whole_range
.min
)
671 left_false
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
) - 1);
673 if (rl_min(left_orig
) != whole_range
.min
)
674 right_true
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
));
675 if (rl_max(left_orig
) != whole_range
.max
)
676 right_false
= remove_range(right_orig
, rl_max(left_orig
) + 1, whole_range
.max
);
678 case SPECIAL_UNSIGNED_LTE
:
680 if (rl_max(right_orig
) != whole_range
.max
)
681 left_true
= remove_range(left_orig
, rl_max(right_orig
) + 1, whole_range
.max
);
682 if (rl_min(right_orig
) != whole_range
.min
)
683 left_false
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
));
685 if (rl_min(left_orig
) != whole_range
.min
)
686 right_true
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
) - 1);
687 if (rl_max(left_orig
) != whole_range
.max
)
688 right_false
= remove_range(right_orig
, rl_max(left_orig
), whole_range
.max
);
691 if (rl_max(right_orig
) != whole_range
.max
)
692 left_true
= remove_range(left_true
, rl_max(right_orig
) + 1, whole_range
.max
);
693 if (rl_min(right_orig
) != whole_range
.min
)
694 left_true
= remove_range(left_true
, whole_range
.min
, rl_min(right_orig
) - 1);
695 if (rl_min(right_orig
) == rl_max(right_orig
))
696 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
698 if (rl_max(left_orig
) != whole_range
.max
)
699 right_true
= remove_range(right_true
, rl_max(left_orig
) + 1, whole_range
.max
);
700 if (rl_min(left_orig
) != whole_range
.min
)
701 right_true
= remove_range(right_true
, whole_range
.min
, rl_min(left_orig
) - 1);
702 if (rl_min(left_orig
) == rl_max(left_orig
))
703 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
705 case SPECIAL_UNSIGNED_GTE
:
707 if (rl_min(right_orig
) != whole_range
.min
)
708 left_true
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
) - 1);
709 if (rl_max(right_orig
) != whole_range
.max
)
710 left_false
= remove_range(left_orig
, rl_max(right_orig
), whole_range
.max
);
712 if (rl_max(left_orig
) != whole_range
.max
)
713 right_true
= remove_range(right_orig
, rl_max(left_orig
) + 1, whole_range
.max
);
714 if (rl_min(left_orig
) != whole_range
.min
)
715 right_false
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
));
718 case SPECIAL_UNSIGNED_GT
:
719 if (rl_min(right_orig
) != whole_range
.min
)
720 left_true
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
));
721 if (rl_max(right_orig
) != whole_range
.max
)
722 left_false
= remove_range(left_orig
, rl_max(right_orig
) + 1, whole_range
.max
);
724 if (rl_max(left_orig
) != whole_range
.max
)
725 right_true
= remove_range(right_orig
, rl_max(left_orig
), whole_range
.max
);
726 if (rl_min(left_orig
) != whole_range
.min
)
727 right_false
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
) - 1);
729 case SPECIAL_NOTEQUAL
:
730 if (rl_max(right_orig
) != whole_range
.max
)
731 left_false
= remove_range(left_false
, rl_max(right_orig
) + 1, whole_range
.max
);
732 if (rl_min(right_orig
) != whole_range
.min
)
733 left_false
= remove_range(left_false
, whole_range
.min
, rl_min(right_orig
) - 1);
734 if (rl_min(right_orig
) == rl_max(right_orig
))
735 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
737 if (rl_max(left_orig
) != whole_range
.max
)
738 right_false
= remove_range(right_false
, rl_max(left_orig
) + 1, whole_range
.max
);
739 if (rl_min(left_orig
) != whole_range
.min
)
740 right_false
= remove_range(right_false
, whole_range
.min
, rl_min(left_orig
) - 1);
741 if (rl_min(left_orig
) == rl_max(left_orig
))
742 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
748 left_true_state
= alloc_estate_range_list(left_true
);
749 left_false_state
= alloc_estate_range_list(left_false
);
750 right_true_state
= alloc_estate_range_list(right_true
);
751 right_false_state
= alloc_estate_range_list(right_false
);
753 if (left_postop
== SPECIAL_INCREMENT
) {
754 left_true_state
= increment_state(left_true_state
);
755 left_false_state
= increment_state(left_false_state
);
757 if (left_postop
== SPECIAL_DECREMENT
) {
758 left_true_state
= decrement_state(left_true_state
);
759 left_false_state
= decrement_state(left_false_state
);
761 if (right_postop
== SPECIAL_INCREMENT
) {
762 right_true_state
= increment_state(right_true_state
);
763 right_false_state
= increment_state(right_false_state
);
765 if (right_postop
== SPECIAL_DECREMENT
) {
766 right_true_state
= decrement_state(right_true_state
);
767 right_false_state
= decrement_state(right_false_state
);
770 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
771 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
774 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
775 void __extra_match_condition(struct expression
*expr
)
779 struct smatch_state
*pre_state
;
780 struct smatch_state
*true_state
;
781 struct smatch_state
*false_state
;
783 expr
= strip_expr(expr
);
784 switch (expr
->type
) {
786 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
791 name
= get_variable_from_expr(expr
, &sym
);
794 pre_state
= get_state(my_id
, name
, sym
);
795 true_state
= add_filter(pre_state
, 0);
796 if (possibly_true(expr
, SPECIAL_EQUAL
, zero_expr()))
797 false_state
= alloc_estate(0);
799 false_state
= alloc_estate_empty();
800 set_extra_true_false(name
, sym
, true_state
, false_state
);
804 match_comparison(expr
);
806 case EXPR_ASSIGNMENT
:
807 __extra_match_condition(expr
->left
);
812 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
813 int implied_not_equal(struct expression
*expr
, long long val
)
815 return !possibly_false(expr
, SPECIAL_NOTEQUAL
, value_expr(val
));
818 int get_implied_range_list(struct expression
*expr
, struct range_list
**rl
)
821 struct smatch_state
*state
;
827 expr
= strip_parens(expr
);
831 state
= get_state_expr(my_id
, expr
);
833 *rl
= clone_range_list(estate_ranges(state
));
837 if (expr
->type
== EXPR_CALL
) {
838 *rl
= db_return_vals(expr
);
842 if (get_implied_value(expr
, &val
)) {
843 add_range(rl
, val
, val
);
847 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
848 if (!get_implied_value(expr
->right
, &val
))
850 add_range(rl
, 0, val
- 1);
854 if (!get_implied_min(expr
, &min
))
856 if (!get_implied_max(expr
, &max
))
859 *rl
= alloc_range_list(min
, max
);
863 int is_whole_range(struct smatch_state
*state
)
865 return is_whole_range_rl(estate_ranges(state
));
868 static void struct_member_callback(char *fn
, char *global_static
, int param
, char *printed_name
, struct smatch_state
*state
)
870 if (is_whole_range(state
))
872 sm_msg("info: passes param_value '%s' %d '%s' %s %s", fn
, param
, printed_name
, state
->name
, global_static
);
875 static void match_call_info(struct expression
*expr
)
877 struct range_list
*rl
= NULL
;
878 struct expression
*arg
;
882 name
= get_fnptr_name(expr
->fn
);
886 FOR_EACH_PTR(expr
->args
, arg
) {
887 if (get_implied_range_list(arg
, &rl
) && !is_whole_range_rl(rl
)) {
888 sm_msg("info: passes param_value '%s' %d '$$' %s %s",
889 name
, i
, show_ranges(rl
),
890 is_static(expr
->fn
) ? "static" : "global");
893 } END_FOR_EACH_PTR(arg
);
898 void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
900 struct range_list
*rl
= NULL
;
901 struct smatch_state
*state
;
904 if (strncmp(key
, "$$", 2))
907 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
908 get_value_ranges(value
, &rl
);
909 char *tmp
= show_ranges(rl
);
910 if (strcmp(tmp
, value
))
911 sm_msg("value = %s, ranges = %s", value
, tmp
);
912 state
= alloc_estate_range_list(rl
);
913 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
916 static void match_call_assign(struct expression
*expr
)
918 struct expression
*right
;
919 struct range_list
*rl
;
924 right
= strip_parens(expr
->right
);
925 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
926 right
= strip_parens(right
->left
);
928 rl
= db_return_vals(right
);
930 set_extra_expr_mod(expr
->left
, alloc_estate_range_list(rl
));
932 set_extra_expr_mod(expr
->left
, extra_undefined());
935 void register_smatch_extra(int id
)
938 add_merge_hook(my_id
, &merge_func
);
939 add_unmatched_state_hook(my_id
, &unmatched_state
);
940 add_hook(&unop_expr
, OP_HOOK
);
941 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
942 add_hook(&match_declarations
, DECLARATION_HOOK
);
944 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
945 add_member_info_callback(my_id
, struct_member_callback
);
947 add_definition_db_callback(set_param_value
, PARAM_VALUE
);
950 void register_smatch_extra_late(int id
)
952 add_hook(&match_dereferences
, DEREF_HOOK
);
953 add_hook(&match_pointer_as_array
, OP_HOOK
);
954 add_db_fn_call_callback(DEREFERENCE
, &set_param_dereferenced
);
955 add_indirect_modification_hook(SMATCH_EXTRA
, reset_struct_members
);
956 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
957 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
958 add_hook(&match_call_assign
, CALL_ASSIGNMENT_HOOK
);