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
;
442 struct symbol
*right_sym
;
448 long long min
= whole_range
.min
;
449 long long max
= whole_range
.max
;
452 if (__is_condition_assign(expr
))
454 left
= strip_expr(expr
->left
);
455 name
= get_variable_from_expr(left
, &sym
);
458 right
= strip_expr(expr
->right
);
459 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
460 right
= strip_expr(right
->left
);
462 right_name
= get_variable_from_expr(right
, &right_sym
);
463 if (expr
->op
== '=' && right_name
&& right_sym
) {
464 right_sm
= get_sm_state_expr(SMATCH_EXTRA
, right
);
466 right_sm
= set_state_expr(SMATCH_EXTRA
, right
, extra_undefined());
469 set_equiv(right_sm
, left
);
473 if (expr
->op
== '=') {
474 struct range_list
*rl
= NULL
;
476 if (get_implied_range_list(right
, &rl
)) {
477 set_extra_mod(name
, sym
, alloc_extra_state_range_list(rl
));
479 struct symbol
*type
= get_type(right
);
481 if (type
&& type_unsigned(type
))
483 set_extra_mod(name
, sym
, alloc_extra_state_range(min
, max
));
488 known
= get_implied_value(right
, &value
);
489 if (expr
->op
== SPECIAL_ADD_ASSIGN
) {
490 if (get_implied_min(left
, &tmp
)) {
496 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
499 if (expr
->op
== SPECIAL_SUB_ASSIGN
) {
500 if (get_implied_max(left
, &tmp
)) {
506 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
509 set_extra_mod(name
, sym
, alloc_extra_state_range(min
, max
));
511 free_string(right_name
);
515 static void reset_struct_members(const char *name
, struct symbol
*sym
, struct expression
*expr
, void *unused
)
517 struct state_list
*slist
;
518 struct sm_state
*tmp
;
522 slist
= get_all_states(SMATCH_EXTRA
);
523 FOR_EACH_PTR(slist
, tmp
) {
526 if (strncmp(name
, tmp
->name
, len
))
528 if (tmp
->name
[len
] != '-' && tmp
->name
[len
] != '.')
530 set_extra_mod(tmp
->name
, tmp
->sym
, extra_undefined());
531 } END_FOR_EACH_PTR(tmp
);
534 static void unop_expr(struct expression
*expr
)
538 long long min
= whole_range
.min
;
539 long long max
= whole_range
.max
;
543 case SPECIAL_INCREMENT
:
544 case SPECIAL_DECREMENT
:
550 name
= get_variable_from_expr(expr
->unop
, &sym
);
553 if (expr
->op
== SPECIAL_INCREMENT
) {
554 if (get_implied_min(expr
->unop
, &val
))
556 if (!inside_loop() && get_implied_max(expr
->unop
, &val
))
559 if (expr
->op
== SPECIAL_DECREMENT
) {
560 if (get_implied_max(expr
->unop
, &val
))
562 if (!inside_loop() && get_implied_min(expr
->unop
, &val
))
565 set_extra_mod(name
, sym
, alloc_extra_state_range(min
, max
));
570 static void delete_state_tracker(struct tracker
*t
)
572 remove_from_equiv(t
->name
, t
->sym
);
573 delete_state(t
->owner
, t
->name
, t
->sym
);
576 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
580 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
581 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
584 static void match_declarations(struct symbol
*sym
)
589 name
= sym
->ident
->name
;
590 if (!sym
->initializer
) {
591 set_state(SMATCH_EXTRA
, name
, sym
, extra_undefined());
592 scoped_state_extra(name
, sym
);
597 static void match_function_def(struct symbol
*sym
)
601 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
605 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined());
606 } END_FOR_EACH_PTR(arg
);
609 static int last_stmt_val(struct statement
*stmt
, long long *val
)
611 struct expression
*expr
;
616 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
617 if (stmt
->type
!= STMT_EXPRESSION
)
619 expr
= stmt
->expression
;
620 return get_value(expr
, val
);
623 static void match_comparison(struct expression
*expr
)
628 struct smatch_state
*true_state
;
629 struct smatch_state
*false_state
;
630 struct smatch_state
*orig
;
632 int comparison
= expr
->op
;
633 struct expression
*varies
;
636 varies
= strip_expr(expr
->right
);
637 if (!get_implied_value(expr
->left
, &fixed
)) {
638 if (!get_implied_value(expr
->right
, &fixed
))
640 varies
= strip_expr(expr
->left
);
644 if (varies
->op
== SPECIAL_INCREMENT
|| varies
->op
== SPECIAL_DECREMENT
)
645 varies
= varies
->unop
;
646 if (varies
->type
== EXPR_CALL
) {
647 function_comparison(comparison
, varies
, fixed
, left
);
651 name
= get_variable_from_expr(varies
, &sym
);
655 orig
= get_state(my_id
, name
, sym
);
657 orig
= extra_undefined();
659 switch (comparison
) {
661 case SPECIAL_UNSIGNED_LT
:
663 true_state
= filter_range(orig
, fixed
, whole_range
.max
);
664 false_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
666 true_state
= filter_range(orig
, whole_range
.min
, fixed
);
667 false_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
670 case SPECIAL_UNSIGNED_LTE
:
673 true_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
674 false_state
= filter_range(orig
, whole_range
.min
, fixed
);
676 true_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
677 false_state
= filter_range(orig
, fixed
, whole_range
.max
);
681 if (possibly_true(SPECIAL_EQUAL
, get_dinfo(orig
), fixed
, fixed
))
682 true_state
= alloc_extra_state(fixed
);
684 true_state
= alloc_extra_state_empty();
685 false_state
= filter_range(orig
, fixed
, fixed
);
687 case SPECIAL_UNSIGNED_GTE
:
690 true_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
691 false_state
= filter_range(orig
, fixed
, whole_range
.max
);
693 true_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
694 false_state
= filter_range(orig
, whole_range
.min
, fixed
);
698 case SPECIAL_UNSIGNED_GT
:
700 true_state
= filter_range(orig
, whole_range
.min
, fixed
);
701 false_state
= filter_range(orig
, fixed
+ 1, whole_range
.max
);
703 true_state
= filter_range(orig
, fixed
, whole_range
.max
);
704 false_state
= filter_range(orig
, whole_range
.min
, fixed
- 1);
707 case SPECIAL_NOTEQUAL
:
708 true_state
= filter_range(orig
, fixed
, fixed
);
709 if (possibly_true(SPECIAL_EQUAL
, get_dinfo(orig
), fixed
, fixed
))
710 false_state
= alloc_extra_state(fixed
);
712 false_state
= alloc_extra_state_empty();
715 sm_msg("unhandled comparison %d\n", comparison
);
718 set_extra_true_false(name
, sym
, true_state
, false_state
);
723 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
724 void __extra_match_condition(struct expression
*expr
)
728 struct smatch_state
*pre_state
;
729 struct smatch_state
*true_state
;
730 struct smatch_state
*false_state
;
732 expr
= strip_expr(expr
);
733 switch (expr
->type
) {
735 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
740 name
= get_variable_from_expr(expr
, &sym
);
743 pre_state
= get_state(my_id
, name
, sym
);
744 true_state
= add_filter(pre_state
, 0);
745 if (possibly_true(SPECIAL_EQUAL
, get_dinfo(pre_state
), 0, 0))
746 false_state
= alloc_extra_state(0);
748 false_state
= alloc_extra_state_empty();
749 set_extra_true_false(name
, sym
, true_state
, false_state
);
753 match_comparison(expr
);
755 case EXPR_ASSIGNMENT
:
756 __extra_match_condition(expr
->left
);
761 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
762 int implied_not_equal(struct expression
*expr
, long long val
)
766 struct smatch_state
*state
;
770 if (get_value(expr
, &expr_val
)) {
771 return expr_val
!= val
;
773 name
= get_variable_from_expr(expr
, &sym
);
776 state
= get_state(my_id
, name
, sym
);
777 if (!state
|| !state
->data
)
779 ret
= !possibly_false(SPECIAL_NOTEQUAL
, get_dinfo(state
), val
, 1);
785 int known_condition_true(struct expression
*expr
)
788 struct statement
*stmt
;
793 if (get_value(expr
, &tmp
) && tmp
)
796 expr
= strip_expr(expr
);
797 switch (expr
->type
) {
799 if (expr
->op
== '!') {
800 if (known_condition_false(expr
->unop
))
804 stmt
= get_expression_statement(expr
);
805 if (last_stmt_val(stmt
, &tmp
) && tmp
== 1)
814 int known_condition_false(struct expression
*expr
)
817 struct statement
*stmt
;
825 switch (expr
->type
) {
827 if (expr
->op
== '!') {
828 if (known_condition_true(expr
->unop
))
832 stmt
= get_expression_statement(expr
);
833 if (last_stmt_val(stmt
, &tmp
) && tmp
== 0)
837 if (sym_name_is("__builtin_constant_p", expr
->fn
))
846 struct range_list
*get_range_list(struct expression
*expr
)
850 struct range_list
*ret
= NULL
;
851 struct smatch_state
*state
;
853 state
= get_state_expr(SMATCH_EXTRA
, expr
);
855 return clone_range_list(get_dinfo(state
)->value_ranges
);
856 if (!get_absolute_min(expr
, &min
))
858 if (!get_absolute_max(expr
, &max
))
860 add_range(&ret
, min
, max
);
864 static int do_comparison(struct expression
*expr
)
866 struct range_list
*left_ranges
;
867 struct range_list
*right_ranges
;
868 int poss_true
, poss_false
;
870 left_ranges
= get_range_list(expr
->left
);
871 right_ranges
= get_range_list(expr
->right
);
873 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
874 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
876 free_range_list(&left_ranges
);
877 free_range_list(&right_ranges
);
879 if (!poss_true
&& !poss_false
)
881 if (poss_true
&& !poss_false
)
883 if (!poss_true
&& poss_false
)
888 int implied_condition_true(struct expression
*expr
)
890 struct statement
*stmt
;
897 if (get_implied_value(expr
, &tmp
) && tmp
)
900 if (expr
->type
== EXPR_POSTOP
)
901 return implied_condition_true(expr
->unop
);
903 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
904 return implied_not_equal(expr
->unop
, 1);
905 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
906 return implied_not_equal(expr
->unop
, -1);
908 expr
= strip_expr(expr
);
909 switch (expr
->type
) {
911 if (do_comparison(expr
) == 1)
915 if (expr
->op
== '!') {
916 if (implied_condition_false(expr
->unop
))
920 stmt
= get_expression_statement(expr
);
921 if (last_stmt_val(stmt
, &val
) && val
== 1)
925 if (implied_not_equal(expr
, 0) == 1)
932 int implied_condition_false(struct expression
*expr
)
934 struct statement
*stmt
;
935 struct expression
*tmp
;
944 switch (expr
->type
) {
946 if (do_comparison(expr
) == 2)
949 if (expr
->op
== '!') {
950 if (implied_condition_true(expr
->unop
))
954 stmt
= get_expression_statement(expr
);
955 if (last_stmt_val(stmt
, &val
) && val
== 0)
957 tmp
= strip_expr(expr
);
959 return implied_condition_false(tmp
);
962 if (get_implied_value(expr
, &val
) && val
== 0)
969 int get_implied_range_list(struct expression
*expr
, struct range_list
**rl
)
972 struct smatch_state
*state
;
974 expr
= strip_expr(expr
);
976 state
= get_state_expr(my_id
, expr
);
978 *rl
= clone_range_list(get_dinfo(state
)->value_ranges
);
982 if (get_implied_value(expr
, &val
)) {
984 add_range(rl
, val
, val
);
988 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '%') {
989 if (!get_implied_value(expr
->right
, &val
))
992 add_range(rl
, 0, val
- 1);
999 int is_whole_range(struct smatch_state
*state
)
1001 struct data_info
*dinfo
;
1002 struct data_range
*drange
;
1006 dinfo
= get_dinfo(state
);
1007 if (!dinfo
|| !dinfo
->value_ranges
)
1009 drange
= first_ptr_list((struct ptr_list
*)dinfo
->value_ranges
);
1010 if (drange
->min
== whole_range
.min
&& drange
->max
== whole_range
.max
)
1015 static void struct_member_callback(char *fn
, int param
, char *printed_name
, struct smatch_state
*state
)
1017 if (is_whole_range(state
))
1019 sm_msg("info: passes param_value '%s' %d '%s' %s", fn
, param
, printed_name
, state
->name
);
1022 static void match_call_info(struct expression
*expr
)
1024 struct range_list
*rl
= NULL
;
1025 struct expression
*arg
;
1029 name
= get_fnptr_name(expr
->fn
);
1033 FOR_EACH_PTR(expr
->args
, arg
) {
1034 if (get_implied_range_list(arg
, &rl
) && ! is_whole_range_rl(rl
)) {
1035 sm_msg("info: passes param_value '%s' %d '$$' %s",
1036 name
, i
, show_ranges(rl
));
1039 } END_FOR_EACH_PTR(arg
);
1044 static void get_value_ranges(char *value
, struct range_list
**rl
)
1046 long long val1
, val2
;
1055 if (!strncmp(start
, "min", 3)) {
1059 while (*c
&& *c
!= ',' && *c
!= '-')
1061 val1
= strtoll(start
, &c
, 10);
1066 add_range(rl
, val1
, val1
);
1070 add_range(rl
, val1
, val1
);
1075 c
++; /* skip the dash in eg. 4-5 */
1079 if (!strncmp(start
, "max", 3)) {
1084 while (*c
&& *c
!= ',' && *c
!= '-')
1086 val2
= strtoll(start
, &c
, 10);
1088 add_range(rl
, val1
, val2
);
1093 c
++; /* skip the comma in eg: 4-5,7 */
1098 void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1100 struct range_list
*rl
= NULL
;
1101 struct smatch_state
*state
;
1104 if (strncmp(key
, "$$", 2))
1107 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
1108 get_value_ranges(value
, &rl
);
1109 char *tmp
= show_ranges(rl
);
1110 if (strcmp(tmp
, value
))
1111 sm_msg("value = %s, ranges = %s", value
, tmp
);
1112 state
= alloc_extra_state_range_list(rl
);
1113 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1116 void register_smatch_extra(int id
)
1119 add_merge_hook(my_id
, &merge_func
);
1120 add_unmatched_state_hook(my_id
, &unmatched_state
);
1121 add_hook(&unop_expr
, OP_HOOK
);
1122 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1123 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1124 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1125 add_hook(&match_declarations
, DECLARATION_HOOK
);
1127 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1128 add_member_info_callback(my_id
, struct_member_callback
);
1130 add_definition_db_callback(set_param_value
, PARAM_VALUE
);
1133 void register_smatch_extra_late(int id
)
1135 set_default_modification_hook(SMATCH_EXTRA
, reset_struct_members
);