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
;
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
),
161 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
163 struct expression
*iter_var
;
164 struct expression
*condition
;
168 condition
= strip_expr(loop
->iterator_pre_condition
);
171 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
173 if (condition
->op
!= SPECIAL_DECREMENT
)
176 iter_var
= condition
->unop
;
177 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
180 if (estate_min(sm
->state
) < 0)
182 start
= estate_max(sm
->state
);
185 if (start
!= whole_range
.max
)
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
;
203 iter_var
= iter_expr
->unop
;
204 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
207 if (!estate_get_single_value(sm
->state
, &start
))
209 if (!get_implied_value(condition
->right
, &end
))
210 end
= whole_range
.max
;
211 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
214 switch (condition
->op
) {
215 case SPECIAL_NOTEQUAL
:
217 if (end
!= whole_range
.max
)
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
;
239 iter_var
= iter_expr
->unop
;
240 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
243 if (!estate_get_single_value(sm
->state
, &start
))
245 if (!get_implied_value(condition
->right
, &end
))
246 end
= whole_range
.max
;
247 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
250 switch (condition
->op
) {
251 case SPECIAL_NOTEQUAL
:
253 if (end
!= whole_range
.min
)
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
)
274 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
276 iter_expr
= loop
->iterator_post_statement
->expression
;
277 if (!loop
->iterator_pre_condition
)
279 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
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
);
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
);
298 ret
= handle_canonical_for_loops(loop
);
299 *slist
= __pop_fake_cur_slist();
303 int __iterator_unchanged(struct sm_state
*sm
)
307 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
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
);
320 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
322 if (condition
->op
!= SPECIAL_DECREMENT
)
324 after_value
= estate_min(sm
->state
);
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
;
338 struct smatch_state
*state
;
342 while_count_down_after(sm
, condition
);
346 iter_expr
= iterator
->expression
;
348 if (condition
->type
!= EXPR_COMPARE
)
350 if (!get_value(condition
->left
, &value
)) {
351 if (!get_value(condition
->right
, &value
))
356 name
= get_variable_from_expr(condition
->left
, &sym
);
358 name
= get_variable_from_expr(condition
->right
, &sym
);
361 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
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
));
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
;
407 long long min
= whole_range
.min
;
408 long long max
= whole_range
.max
;
411 if (__is_condition_assign(expr
))
413 left
= strip_expr(expr
->left
);
414 name
= get_variable_from_expr(left
, &sym
);
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
);
430 known
= get_implied_value(right
, &value
);
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
));
440 if (expr_unsigned(right
))
444 case SPECIAL_ADD_ASSIGN
:
445 if (get_implied_min(left
, &tmp
)) {
451 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
454 case SPECIAL_SUB_ASSIGN
:
455 if (get_implied_max(left
, &tmp
)) {
461 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
465 set_extra_mod(name
, sym
, alloc_estate_range(min
, max
));
467 free_string(right_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
);
482 max
= whole_range
.max
;
484 if (min
!= whole_range
.min
)
486 if (max
!= whole_range
.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
);
497 min
= whole_range
.min
;
499 if (min
!= whole_range
.min
)
501 if (max
!= whole_range
.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
)
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
);
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
);
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
)
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
)
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
)
565 check_dereference(expr
->unop
);
568 static void match_pointer_as_array(struct expression
*expr
)
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
)
584 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
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
);
597 if (left
->type
== EXPR_CALL
) {
598 if (!get_implied_value(right
, &val
))
600 function_comparison(expr
->op
, left
, val
, 1);
604 if (right
->type
== EXPR_CALL
) {
605 if (!get_implied_value(left
, &val
))
607 function_comparison(expr
->op
, right
, val
, 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
;
629 int right_postop
= 0;
631 if (match_func_comparison(expr
))
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
);
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
);
673 case SPECIAL_UNSIGNED_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
);
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
));
700 case SPECIAL_UNSIGNED_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
));
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);
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
));
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
)
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
) {
781 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
786 name
= get_variable_from_expr(expr
, &sym
);
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);
794 false_state
= alloc_estate_empty();
795 set_extra_true_false(name
, sym
, true_state
, false_state
);
799 match_comparison(expr
);
801 case EXPR_ASSIGNMENT
:
802 __extra_match_condition(expr
->left
);
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
)
816 struct smatch_state
*state
;
822 expr
= strip_parens(expr
);
826 state
= get_state_expr(my_id
, expr
);
828 *rl
= clone_range_list(estate_ranges(state
));
832 if (expr
->type
== EXPR_CALL
) {
833 *rl
= db_return_vals(expr
);
837 if (get_implied_value(expr
, &val
)) {
838 add_range(rl
, val
, val
);
842 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
843 if (!get_implied_value(expr
->right
, &val
))
845 add_range(rl
, 0, val
- 1);
849 if (!get_implied_min(expr
, &min
))
851 if (!get_implied_max(expr
, &max
))
854 *rl
= alloc_range_list(min
, max
);
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
))
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
;
877 name
= get_fnptr_name(expr
->fn
);
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");
888 } END_FOR_EACH_PTR(arg
);
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
;
899 if (strncmp(key
, "$$", 2))
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
;
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
);
925 set_extra_expr_mod(expr
->left
, alloc_estate_range_list(rl
));
927 set_extra_expr_mod(expr
->left
, extra_undefined());
930 void register_smatch_extra(int 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
);
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
);