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 (!orig_state
|| !get_dinfo(orig_state
)->related
) {
74 set_state_expr(SMATCH_EXTRA
, expr
, state
);
78 FOR_EACH_PTR(get_dinfo(orig_state
)->related
, rel
) {
79 sm_msg("setting %s to %s", rel
->name
, state
->name
);
80 set_state(SMATCH_EXTRA
, rel
->name
, rel
->sym
, state
);
81 add_equiv(state
, rel
->name
, rel
->sym
);
82 } END_FOR_EACH_PTR(rel
);
85 void set_extra_true_false(const char *name
, struct symbol
*sym
,
86 struct smatch_state
*true_state
,
87 struct smatch_state
*false_state
)
90 struct smatch_state
*orig_state
;
92 if (in_warn_on_macro())
95 orig_state
= get_state(SMATCH_EXTRA
, name
, sym
);
97 if (!orig_state
|| !get_dinfo(orig_state
)->related
) {
98 set_true_false_states(SMATCH_EXTRA
, name
, sym
, true_state
, false_state
);
102 // FIXME!!!! equiv => related
103 FOR_EACH_PTR(get_dinfo(orig_state
)->related
, rel
) {
104 set_true_false_states(SMATCH_EXTRA
, rel
->name
, rel
->sym
,
105 true_state
, false_state
);
107 add_equiv(true_state
, rel
->name
, rel
->sym
);
109 add_equiv(false_state
, rel
->name
, rel
->sym
);
110 } END_FOR_EACH_PTR(rel
);
113 struct data_info
*get_dinfo(struct smatch_state
*state
)
117 return (struct data_info
*)state
->data
;
121 struct smatch_state
*filter_range(struct smatch_state
*orig
,
122 long long filter_min
, long long filter_max
)
124 struct smatch_state
*ret
;
125 struct data_info
*orig_info
;
126 struct data_info
*ret_info
;
129 orig
= extra_undefined();
130 orig_info
= get_dinfo(orig
);
131 ret
= alloc_extra_state_empty();
132 ret_info
= get_dinfo(ret
);
133 ret_info
->value_ranges
= remove_range(orig_info
->value_ranges
, filter_min
, filter_max
);
134 ret
->name
= show_ranges(ret_info
->value_ranges
);
138 struct smatch_state
*add_filter(struct smatch_state
*orig
, long long num
)
140 return filter_range(orig
, num
, num
);
143 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
144 struct smatch_state
*s1
,
145 struct smatch_state
*s2
)
147 struct data_info
*info1
= get_dinfo(s1
);
148 struct data_info
*info2
= get_dinfo(s2
);
149 struct data_info
*ret_info
;
150 struct smatch_state
*tmp
;
151 struct range_list
*value_ranges
;
152 struct relation
*rel
;
153 struct relation
*new;
155 value_ranges
= range_list_union(info1
->value_ranges
, info2
->value_ranges
);
156 tmp
= alloc_extra_state_empty();
157 ret_info
= get_dinfo(tmp
);
158 ret_info
->value_ranges
= value_ranges
;
159 tmp
->name
= show_ranges(ret_info
->value_ranges
);
160 FOR_EACH_PTR(info1
->related
, rel
) {
161 new = get_common_relationship(info2
, rel
->op
, rel
->name
, rel
->sym
);
163 add_related(tmp
, new->op
, new->name
, new->sym
);
164 } END_FOR_EACH_PTR(rel
);
168 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
170 struct expression
*iter_var
;
171 struct expression
*condition
;
175 condition
= strip_expr(loop
->iterator_pre_condition
);
178 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
180 if (condition
->op
!= SPECIAL_DECREMENT
)
183 iter_var
= condition
->unop
;
184 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
187 if (get_dinfo_min(get_dinfo(sm
->state
)) < 0)
189 start
= get_dinfo_max(get_dinfo(sm
->state
));
192 if (start
!= whole_range
.max
)
195 if (condition
->type
== EXPR_PREOP
)
196 set_extra_expr_mod(iter_var
, alloc_extra_state_range(1, start
));
197 if (condition
->type
== EXPR_POSTOP
)
198 set_extra_expr_mod(iter_var
, alloc_extra_state_range(0, start
));
199 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
202 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
203 struct expression
*condition
)
205 struct expression
*iter_var
;
210 iter_var
= iter_expr
->unop
;
211 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
214 if (!get_single_value_from_dinfo(get_dinfo(sm
->state
), &start
))
216 if (!get_implied_value(condition
->right
, &end
))
217 end
= whole_range
.max
;
218 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
221 switch (condition
->op
) {
222 case SPECIAL_NOTEQUAL
:
224 if (end
!= whole_range
.max
)
234 set_extra_expr_mod(iter_var
, alloc_extra_state_range(start
, end
));
235 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
238 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
239 struct expression
*condition
)
241 struct expression
*iter_var
;
246 iter_var
= iter_expr
->unop
;
247 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
250 if (!get_single_value_from_dinfo(get_dinfo(sm
->state
), &start
))
252 if (!get_implied_value(condition
->right
, &end
))
253 end
= whole_range
.max
;
254 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
257 switch (condition
->op
) {
258 case SPECIAL_NOTEQUAL
:
260 if (end
!= whole_range
.min
)
270 set_extra_expr_mod(iter_var
, alloc_extra_state_range(end
, start
));
271 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
274 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
276 struct expression
*iter_expr
;
277 struct expression
*condition
;
279 if (!loop
->iterator_post_statement
)
281 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
283 iter_expr
= loop
->iterator_post_statement
->expression
;
284 if (!loop
->iterator_pre_condition
)
286 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
288 condition
= loop
->iterator_pre_condition
;
290 if (iter_expr
->op
== SPECIAL_INCREMENT
)
291 return handle_canonical_for_inc(iter_expr
, condition
);
292 if (iter_expr
->op
== SPECIAL_DECREMENT
)
293 return handle_canonical_for_dec(iter_expr
, condition
);
297 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct state_list
**slist
)
299 struct sm_state
*ret
;
301 __push_fake_cur_slist();;
302 if (!loop
->iterator_post_statement
)
303 ret
= handle_canonical_while_count_down(loop
);
305 ret
= handle_canonical_for_loops(loop
);
306 *slist
= __pop_fake_cur_slist();
310 int __iterator_unchanged(struct sm_state
*sm
)
314 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
319 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
321 long long after_value
;
323 /* paranoid checking. prolly not needed */
324 condition
= strip_expr(condition
);
327 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
329 if (condition
->op
!= SPECIAL_DECREMENT
)
331 after_value
= get_dinfo_min(get_dinfo(sm
->state
));
333 set_extra_mod(sm
->name
, sm
->sym
, alloc_extra_state(after_value
));
336 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
337 struct statement
*iterator
,
338 struct expression
*condition
)
340 struct expression
*iter_expr
;
345 struct smatch_state
*state
;
346 struct data_info
*dinfo
;
350 while_count_down_after(sm
, condition
);
354 iter_expr
= iterator
->expression
;
356 if (condition
->type
!= EXPR_COMPARE
)
358 if (!get_value(condition
->left
, &value
)) {
359 if (!get_value(condition
->right
, &value
))
364 name
= get_variable_from_expr(condition
->left
, &sym
);
366 name
= get_variable_from_expr(condition
->right
, &sym
);
369 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
371 state
= get_state(my_id
, name
, sym
);
372 dinfo
= get_dinfo(state
);
373 min
= get_dinfo_min(dinfo
);
374 max
= get_dinfo_max(dinfo
);
375 if (iter_expr
->op
== SPECIAL_INCREMENT
&&
376 min
!= whole_range
.min
&&
377 max
== whole_range
.max
) {
378 set_extra_mod(name
, sym
, alloc_extra_state(min
));
379 } else if (min
== whole_range
.min
&& max
!= whole_range
.max
) {
380 set_extra_mod(name
, sym
, alloc_extra_state(max
));
387 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
389 return extra_undefined();
392 static void match_function_call(struct expression
*expr
)
394 struct expression
*tmp
;
396 FOR_EACH_PTR(expr
->args
, tmp
) {
397 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&') {
398 remove_from_equiv_expr(tmp
->unop
);
399 set_state_expr(SMATCH_EXTRA
, tmp
->unop
, extra_undefined());
401 } END_FOR_EACH_PTR(tmp
);
404 static void set_equiv(struct sm_state
*right_sm
, struct expression
*left
)
406 struct smatch_state
*state
;
407 struct data_info
*dinfo
;
408 struct relation
*rel
;
412 name
= get_variable_from_expr(left
, &sym
);
416 remove_from_equiv(name
, sym
);
418 state
= clone_extra_state(right_sm
->state
);
419 dinfo
= get_dinfo(state
);
421 add_equiv(state
, right_sm
->name
, right_sm
->sym
);
422 add_equiv(state
, name
, sym
);
424 FOR_EACH_PTR(dinfo
->related
, rel
) {
425 struct sm_state
*new_sm
;
427 new_sm
= clone_sm(right_sm
);
428 new_sm
->name
= rel
->name
;
429 new_sm
->sym
= rel
->sym
;
430 new_sm
->state
= state
;
432 } END_FOR_EACH_PTR(rel
);
437 static void match_assign(struct expression
*expr
)
439 struct expression
*left
;
440 struct expression
*right
;
441 struct sm_state
*right_sm
;
446 long long min
= whole_range
.min
;
447 long long max
= whole_range
.max
;
450 if (__is_condition_assign(expr
))
452 left
= strip_expr(expr
->left
);
453 name
= get_variable_from_expr(left
, &sym
);
456 right
= strip_expr(expr
->right
);
457 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
458 right
= strip_expr(right
->left
);
460 right_sm
= get_sm_state_expr(SMATCH_EXTRA
, right
);
461 if (expr
->op
== '=' && right_sm
) {
462 set_equiv(right_sm
, left
);
466 if (expr
->op
== '=') {
467 struct range_list
*rl
= NULL
;
469 if (get_implied_range_list(right
, &rl
)) {
470 set_extra_mod(name
, sym
, alloc_extra_state_range_list(rl
));
472 struct symbol
*type
= get_type(right
);
474 if (type
&& type_unsigned(type
))
476 set_extra_mod(name
, sym
, alloc_extra_state_range(min
, max
));
481 known
= get_implied_value(right
, &value
);
482 if (expr
->op
== SPECIAL_ADD_ASSIGN
) {
483 if (get_implied_min(left
, &tmp
)) {
489 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
492 if (expr
->op
== SPECIAL_SUB_ASSIGN
) {
493 if (get_implied_max(left
, &tmp
)) {
499 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
502 set_extra_mod(name
, sym
, alloc_extra_state_range(min
, max
));
507 static void unop_expr(struct expression
*expr
)
511 long long min
= whole_range
.min
;
512 long long max
= whole_range
.max
;
522 name
= get_variable_from_expr(expr
->unop
, &sym
);
525 if (expr
->op
== SPECIAL_INCREMENT
) {
526 if (get_implied_min(expr
->unop
, &val
))
528 if (!inside_loop() && get_implied_max(expr
->unop
, &val
))
531 if (expr
->op
== SPECIAL_DECREMENT
) {
532 if (get_implied_max(expr
->unop
, &val
))
534 if (!inside_loop() && get_implied_min(expr
->unop
, &val
))
537 set_extra_mod(name
, sym
, alloc_extra_state_range(min
, max
));
542 static void delete_state_tracker(struct tracker
*t
)
544 remove_from_equiv(t
->name
, t
->sym
);
545 delete_state(t
->owner
, t
->name
, t
->sym
);
548 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
552 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
553 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
556 static void match_declarations(struct symbol
*sym
)
561 name
= sym
->ident
->name
;
562 if (!sym
->initializer
) {
563 set_state(SMATCH_EXTRA
, name
, sym
, extra_undefined());
564 scoped_state_extra(name
, sym
);
569 static void match_function_def(struct symbol
*sym
)
573 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
577 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined());
578 } END_FOR_EACH_PTR(arg
);
581 static int last_stmt_val(struct statement
*stmt
, long long *val
)
583 struct expression
*expr
;
588 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
589 if (stmt
->type
!= STMT_EXPRESSION
)
591 expr
= stmt
->expression
;
592 return get_value(expr
, val
);
595 static void match_comparison(struct expression
*expr
)
600 struct smatch_state
*true_state
;
601 struct smatch_state
*false_state
;
602 struct smatch_state
*orig
;
604 int comparison
= expr
->op
;
605 struct expression
*varies
;
608 varies
= strip_expr(expr
->right
);
609 if (!get_implied_value(expr
->left
, &fixed
)) {
610 if (!get_implied_value(expr
->right
, &fixed
))
612 varies
= strip_expr(expr
->left
);
616 if (varies
->op
== SPECIAL_INCREMENT
|| varies
->op
== SPECIAL_DECREMENT
)
617 varies
= varies
->unop
;
618 if (varies
->type
== EXPR_CALL
) {
619 function_comparison(comparison
, varies
, fixed
, left
);
623 name
= get_variable_from_expr(varies
, &sym
);
627 orig
= get_state(my_id
, name
, sym
);
629 orig
= extra_undefined();
631 switch (comparison
) {
633 case SPECIAL_UNSIGNED_LT
:
635 true_state
= filter_range(orig
, fixed
, whole_range
.max
);
636 false_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
638 true_state
= filter_range(orig
, whole_range
.min
, fixed
);
639 false_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
642 case SPECIAL_UNSIGNED_LTE
:
645 true_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
646 false_state
= filter_range(orig
, whole_range
.min
, fixed
);
648 true_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
649 false_state
= filter_range(orig
, fixed
, whole_range
.max
);
653 if (possibly_true(SPECIAL_EQUAL
, get_dinfo(orig
), fixed
, fixed
))
654 true_state
= alloc_extra_state(fixed
);
656 true_state
= alloc_extra_state_empty();
657 false_state
= filter_range(orig
, fixed
, fixed
);
659 case SPECIAL_UNSIGNED_GTE
:
662 true_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
663 false_state
= filter_range(orig
, fixed
, whole_range
.max
);
665 true_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
666 false_state
= filter_range(orig
, whole_range
.min
, fixed
);
670 case SPECIAL_UNSIGNED_GT
:
672 true_state
= filter_range(orig
, whole_range
.min
, fixed
);
673 false_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
675 true_state
= filter_range(orig
, fixed
, whole_range
.max
);
676 false_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
679 case SPECIAL_NOTEQUAL
:
680 true_state
= filter_range(orig
, fixed
, fixed
);
681 if (possibly_true(SPECIAL_EQUAL
, get_dinfo(orig
), fixed
, fixed
))
682 false_state
= alloc_extra_state(fixed
);
684 false_state
= alloc_extra_state_empty();
687 sm_msg("unhandled comparison %d\n", comparison
);
690 set_extra_true_false(name
, sym
, true_state
, false_state
);
695 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
696 void __extra_match_condition(struct expression
*expr
)
700 struct smatch_state
*pre_state
;
701 struct smatch_state
*true_state
;
702 struct smatch_state
*false_state
;
704 expr
= strip_expr(expr
);
705 switch (expr
->type
) {
707 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
712 name
= get_variable_from_expr(expr
, &sym
);
715 pre_state
= get_state(my_id
, name
, sym
);
716 true_state
= add_filter(pre_state
, 0);
717 if (possibly_true(SPECIAL_EQUAL
, get_dinfo(pre_state
), 0, 0))
718 false_state
= alloc_extra_state(0);
720 false_state
= alloc_extra_state_empty();
721 set_extra_true_false(name
, sym
, true_state
, false_state
);
725 match_comparison(expr
);
727 case EXPR_ASSIGNMENT
:
728 __extra_match_condition(expr
->left
);
733 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
734 int implied_not_equal(struct expression
*expr
, long long val
)
738 struct smatch_state
*state
;
742 if (get_value(expr
, &expr_val
)) {
743 return expr_val
!= val
;
745 name
= get_variable_from_expr(expr
, &sym
);
748 state
= get_state(my_id
, name
, sym
);
749 if (!state
|| !state
->data
)
751 ret
= !possibly_false(SPECIAL_NOTEQUAL
, get_dinfo(state
), val
, 1);
757 int known_condition_true(struct expression
*expr
)
760 struct statement
*stmt
;
765 if (get_value(expr
, &tmp
) && tmp
)
768 expr
= strip_expr(expr
);
769 switch (expr
->type
) {
771 if (expr
->op
== '!') {
772 if (known_condition_false(expr
->unop
))
776 stmt
= get_expression_statement(expr
);
777 if (last_stmt_val(stmt
, &tmp
) && tmp
== 1)
786 int known_condition_false(struct expression
*expr
)
789 struct statement
*stmt
;
797 switch (expr
->type
) {
799 if (expr
->op
== '!') {
800 if (known_condition_true(expr
->unop
))
804 stmt
= get_expression_statement(expr
);
805 if (last_stmt_val(stmt
, &tmp
) && tmp
== 0)
809 if (sym_name_is("__builtin_constant_p", expr
->fn
))
818 struct range_list
*get_range_list(struct expression
*expr
)
822 struct range_list
*ret
= NULL
;
823 struct smatch_state
*state
;
825 state
= get_state_expr(SMATCH_EXTRA
, expr
);
827 return clone_range_list(get_dinfo(state
)->value_ranges
);
828 if (!get_absolute_min(expr
, &min
))
830 if (!get_absolute_max(expr
, &max
))
832 add_range(&ret
, min
, max
);
836 static int do_comparison(struct expression
*expr
)
838 struct range_list
*left_ranges
;
839 struct range_list
*right_ranges
;
840 int poss_true
, poss_false
;
842 left_ranges
= get_range_list(expr
->left
);
843 right_ranges
= get_range_list(expr
->right
);
845 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
846 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
848 free_range_list(&left_ranges
);
849 free_range_list(&right_ranges
);
851 if (!poss_true
&& !poss_false
)
853 if (poss_true
&& !poss_false
)
855 if (!poss_true
&& poss_false
)
860 int implied_condition_true(struct expression
*expr
)
862 struct statement
*stmt
;
869 if (get_implied_value(expr
, &tmp
) && tmp
)
872 if (expr
->type
== EXPR_POSTOP
)
873 return implied_condition_true(expr
->unop
);
875 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
876 return implied_not_equal(expr
->unop
, 1);
877 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
878 return implied_not_equal(expr
->unop
, -1);
880 expr
= strip_expr(expr
);
881 switch (expr
->type
) {
883 if (do_comparison(expr
) == 1)
887 if (expr
->op
== '!') {
888 if (implied_condition_false(expr
->unop
))
892 stmt
= get_expression_statement(expr
);
893 if (last_stmt_val(stmt
, &val
) && val
== 1)
897 if (implied_not_equal(expr
, 0) == 1)
904 int implied_condition_false(struct expression
*expr
)
906 struct statement
*stmt
;
907 struct expression
*tmp
;
916 switch (expr
->type
) {
918 if (do_comparison(expr
) == 2)
921 if (expr
->op
== '!') {
922 if (implied_condition_true(expr
->unop
))
926 stmt
= get_expression_statement(expr
);
927 if (last_stmt_val(stmt
, &val
) && val
== 0)
929 tmp
= strip_expr(expr
);
931 return implied_condition_false(tmp
);
934 if (get_implied_value(expr
, &val
) && val
== 0)
941 int get_implied_range_list(struct expression
*expr
, struct range_list
**rl
)
944 struct smatch_state
*state
;
946 expr
= strip_expr(expr
);
948 state
= get_state_expr(my_id
, expr
);
950 *rl
= clone_range_list(get_dinfo(state
)->value_ranges
);
954 if (get_implied_value(expr
, &val
)) {
956 add_range(rl
, val
, val
);
960 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
961 if (!get_implied_value(expr
->right
, &val
))
964 add_range(rl
, 0, val
- 1);
971 int is_whole_range(struct smatch_state
*state
)
973 struct data_info
*dinfo
;
974 struct data_range
*drange
;
978 dinfo
= get_dinfo(state
);
981 drange
= first_ptr_list((struct ptr_list
*)dinfo
->value_ranges
);
982 if (drange
->min
== whole_range
.min
&& drange
->max
== whole_range
.max
)
987 static void match_call_info(struct expression
*expr
)
989 struct expression
*arg
;
993 if (expr
->fn
->type
!= EXPR_SYMBOL
)
996 name
= get_variable_from_expr(expr
->fn
, NULL
);
997 FOR_EACH_PTR(expr
->args
, arg
) {
998 struct range_list
*rl
= NULL
;
1001 if (!get_implied_range_list(arg
, &rl
))
1003 if (is_whole_range_rl(rl
))
1006 msg
= show_ranges(rl
);
1007 sm_msg("info: passes param_value '%s' %d %s", name
, i
, msg
);
1010 } END_FOR_EACH_PTR(arg
);
1014 static void get_value_ranges(char *value
, struct range_list
**rl
)
1016 long long val1
, val2
;
1023 if (!strncmp(start
, "min", 3)) {
1027 while (*c
&& *c
!= ',' && *c
!= '-')
1029 val1
= strtoll(start
, &c
, 10);
1032 add_range(rl
, val1
, val1
);
1036 add_range(rl
, val1
, val1
);
1043 if (!strncmp(start
, "max", 3)) {
1047 while (*c
&& *c
!= ',' && *c
!= '-')
1049 val2
= strtoll(start
, &c
, 10);
1051 add_range(rl
, val1
, val2
);
1060 void set_param_value(const char *name
, struct symbol
*sym
, char *value
)
1062 struct range_list
*rl
= NULL
;
1063 struct smatch_state
*state
;
1065 get_value_ranges(value
, &rl
);
1066 state
= alloc_extra_state_range_list(rl
);
1067 set_state(SMATCH_EXTRA
, name
, sym
, state
);
1070 void register_smatch_extra(int id
)
1073 add_merge_hook(my_id
, &merge_func
);
1074 add_unmatched_state_hook(my_id
, &unmatched_state
);
1075 add_hook(&unop_expr
, OP_HOOK
);
1076 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1077 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1078 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1079 add_hook(&match_declarations
, DECLARATION_HOOK
);
1081 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1082 add_definition_db_callback(set_param_value
, PARAM_VALUE
);