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 static 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 static void set_extra_expr_true_false(struct expression
*expr
,
114 struct smatch_state
*true_state
,
115 struct smatch_state
*false_state
)
120 expr
= strip_expr(expr
);
121 name
= get_variable_from_expr(expr
, &sym
);
124 set_extra_true_false(name
, sym
, true_state
, false_state
);
129 struct data_info
*get_dinfo(struct smatch_state
*state
)
133 return (struct data_info
*)state
->data
;
137 struct smatch_state
*filter_range(struct smatch_state
*orig
,
138 long long filter_min
, long long filter_max
)
140 struct smatch_state
*ret
;
141 struct data_info
*orig_info
;
142 struct data_info
*ret_info
;
145 orig
= extra_undefined();
146 orig_info
= get_dinfo(orig
);
147 ret
= alloc_extra_state_empty();
148 ret_info
= get_dinfo(ret
);
149 ret_info
->value_ranges
= remove_range(orig_info
->value_ranges
, filter_min
, filter_max
);
150 ret
->name
= show_ranges(ret_info
->value_ranges
);
154 struct smatch_state
*add_filter(struct smatch_state
*orig
, long long num
)
156 return filter_range(orig
, num
, num
);
159 static struct smatch_state
*merge_func(const char *name
, struct symbol
*sym
,
160 struct smatch_state
*s1
,
161 struct smatch_state
*s2
)
163 struct data_info
*info1
= get_dinfo(s1
);
164 struct data_info
*info2
= get_dinfo(s2
);
165 struct data_info
*ret_info
;
166 struct smatch_state
*tmp
;
167 struct range_list
*value_ranges
;
168 struct relation
*rel
;
169 struct relation
*new;
171 value_ranges
= range_list_union(info1
->value_ranges
, info2
->value_ranges
);
172 tmp
= alloc_extra_state_empty();
173 ret_info
= get_dinfo(tmp
);
174 ret_info
->value_ranges
= value_ranges
;
175 tmp
->name
= show_ranges(ret_info
->value_ranges
);
176 FOR_EACH_PTR(info1
->related
, rel
) {
177 new = get_common_relationship(info2
, rel
->op
, rel
->name
, rel
->sym
);
179 add_related(tmp
, new->op
, new->name
, new->sym
);
180 } END_FOR_EACH_PTR(rel
);
184 static struct sm_state
*handle_canonical_while_count_down(struct statement
*loop
)
186 struct expression
*iter_var
;
187 struct expression
*condition
;
191 condition
= strip_expr(loop
->iterator_pre_condition
);
194 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
196 if (condition
->op
!= SPECIAL_DECREMENT
)
199 iter_var
= condition
->unop
;
200 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
203 if (get_dinfo_min(get_dinfo(sm
->state
)) < 0)
205 start
= get_dinfo_max(get_dinfo(sm
->state
));
208 if (start
!= whole_range
.max
)
211 if (condition
->type
== EXPR_PREOP
)
212 set_extra_expr_mod(iter_var
, alloc_extra_state_range(1, start
));
213 if (condition
->type
== EXPR_POSTOP
)
214 set_extra_expr_mod(iter_var
, alloc_extra_state_range(0, start
));
215 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
218 static struct sm_state
*handle_canonical_for_inc(struct expression
*iter_expr
,
219 struct expression
*condition
)
221 struct expression
*iter_var
;
226 iter_var
= iter_expr
->unop
;
227 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
230 if (!get_single_value_from_dinfo(get_dinfo(sm
->state
), &start
))
232 if (!get_implied_value(condition
->right
, &end
))
233 end
= whole_range
.max
;
234 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
237 switch (condition
->op
) {
238 case SPECIAL_NOTEQUAL
:
240 if (end
!= whole_range
.max
)
250 set_extra_expr_mod(iter_var
, alloc_extra_state_range(start
, end
));
251 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
254 static struct sm_state
*handle_canonical_for_dec(struct expression
*iter_expr
,
255 struct expression
*condition
)
257 struct expression
*iter_var
;
262 iter_var
= iter_expr
->unop
;
263 sm
= get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
266 if (!get_single_value_from_dinfo(get_dinfo(sm
->state
), &start
))
268 if (!get_implied_value(condition
->right
, &end
))
269 end
= whole_range
.max
;
270 if (get_sm_state_expr(SMATCH_EXTRA
, condition
->left
) != sm
)
273 switch (condition
->op
) {
274 case SPECIAL_NOTEQUAL
:
276 if (end
!= whole_range
.min
)
286 set_extra_expr_mod(iter_var
, alloc_extra_state_range(end
, start
));
287 return get_sm_state_expr(SMATCH_EXTRA
, iter_var
);
290 static struct sm_state
*handle_canonical_for_loops(struct statement
*loop
)
292 struct expression
*iter_expr
;
293 struct expression
*condition
;
295 if (!loop
->iterator_post_statement
)
297 if (loop
->iterator_post_statement
->type
!= STMT_EXPRESSION
)
299 iter_expr
= loop
->iterator_post_statement
->expression
;
300 if (!loop
->iterator_pre_condition
)
302 if (loop
->iterator_pre_condition
->type
!= EXPR_COMPARE
)
304 condition
= loop
->iterator_pre_condition
;
306 if (iter_expr
->op
== SPECIAL_INCREMENT
)
307 return handle_canonical_for_inc(iter_expr
, condition
);
308 if (iter_expr
->op
== SPECIAL_DECREMENT
)
309 return handle_canonical_for_dec(iter_expr
, condition
);
313 struct sm_state
*__extra_handle_canonical_loops(struct statement
*loop
, struct state_list
**slist
)
315 struct sm_state
*ret
;
317 __push_fake_cur_slist();
318 if (!loop
->iterator_post_statement
)
319 ret
= handle_canonical_while_count_down(loop
);
321 ret
= handle_canonical_for_loops(loop
);
322 *slist
= __pop_fake_cur_slist();
326 int __iterator_unchanged(struct sm_state
*sm
)
330 if (get_sm_state(my_id
, sm
->name
, sm
->sym
) == sm
)
335 static void while_count_down_after(struct sm_state
*sm
, struct expression
*condition
)
337 long long after_value
;
339 /* paranoid checking. prolly not needed */
340 condition
= strip_expr(condition
);
343 if (condition
->type
!= EXPR_PREOP
&& condition
->type
!= EXPR_POSTOP
)
345 if (condition
->op
!= SPECIAL_DECREMENT
)
347 after_value
= get_dinfo_min(get_dinfo(sm
->state
));
349 set_extra_mod(sm
->name
, sm
->sym
, alloc_extra_state(after_value
));
352 void __extra_pre_loop_hook_after(struct sm_state
*sm
,
353 struct statement
*iterator
,
354 struct expression
*condition
)
356 struct expression
*iter_expr
;
361 struct smatch_state
*state
;
362 struct data_info
*dinfo
;
366 while_count_down_after(sm
, condition
);
370 iter_expr
= iterator
->expression
;
372 if (condition
->type
!= EXPR_COMPARE
)
374 if (!get_value(condition
->left
, &value
)) {
375 if (!get_value(condition
->right
, &value
))
380 name
= get_variable_from_expr(condition
->left
, &sym
);
382 name
= get_variable_from_expr(condition
->right
, &sym
);
385 if (sym
!= sm
->sym
|| strcmp(name
, sm
->name
))
387 state
= get_state(my_id
, name
, sym
);
388 dinfo
= get_dinfo(state
);
389 min
= get_dinfo_min(dinfo
);
390 max
= get_dinfo_max(dinfo
);
391 if (iter_expr
->op
== SPECIAL_INCREMENT
&&
392 min
!= whole_range
.min
&&
393 max
== whole_range
.max
) {
394 set_extra_mod(name
, sym
, alloc_extra_state(min
));
395 } else if (min
== whole_range
.min
&& max
!= whole_range
.max
) {
396 set_extra_mod(name
, sym
, alloc_extra_state(max
));
403 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
405 return extra_undefined();
408 static void match_function_call(struct expression
*expr
)
410 struct expression
*tmp
;
412 FOR_EACH_PTR(expr
->args
, tmp
) {
413 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '&') {
414 remove_from_equiv_expr(tmp
->unop
);
415 set_state_expr(SMATCH_EXTRA
, tmp
->unop
, extra_undefined());
417 } END_FOR_EACH_PTR(tmp
);
420 static void set_equiv(struct sm_state
*right_sm
, struct expression
*left
)
422 struct smatch_state
*state
;
423 struct data_info
*dinfo
;
424 struct relation
*rel
;
428 name
= get_variable_from_expr(left
, &sym
);
432 remove_from_equiv(name
, sym
);
434 state
= clone_extra_state(right_sm
->state
);
435 dinfo
= get_dinfo(state
);
437 add_equiv(state
, right_sm
->name
, right_sm
->sym
);
438 add_equiv(state
, name
, sym
);
440 FOR_EACH_PTR(dinfo
->related
, rel
) {
441 struct sm_state
*new_sm
;
443 new_sm
= clone_sm(right_sm
);
444 new_sm
->name
= rel
->name
;
445 new_sm
->sym
= rel
->sym
;
446 new_sm
->state
= state
;
448 } END_FOR_EACH_PTR(rel
);
453 static void match_assign(struct expression
*expr
)
455 struct expression
*left
;
456 struct expression
*right
;
457 struct sm_state
*right_sm
;
458 struct symbol
*right_sym
;
464 long long min
= whole_range
.min
;
465 long long max
= whole_range
.max
;
468 if (__is_condition_assign(expr
))
470 left
= strip_expr(expr
->left
);
471 name
= get_variable_from_expr(left
, &sym
);
474 right
= strip_parens(expr
->right
);
475 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
476 right
= strip_parens(right
->left
);
478 if (expr
->op
== '=' && right
->type
== EXPR_CALL
)
479 return; /* these are handled in match_call_assign() */
481 right_name
= get_variable_from_expr(right
, &right_sym
);
482 if (expr
->op
== '=' && right_name
&& right_sym
) {
483 right_sm
= get_sm_state_expr(SMATCH_EXTRA
, right
);
485 right_sm
= set_state_expr(SMATCH_EXTRA
, right
, extra_undefined());
488 set_equiv(right_sm
, left
);
492 known
= get_implied_value(right
, &value
);
495 struct range_list
*rl
= NULL
;
497 if (get_implied_range_list(right
, &rl
)) {
498 set_extra_mod(name
, sym
, alloc_extra_state_range_list(rl
));
502 if (expr_unsigned(right
))
506 case SPECIAL_ADD_ASSIGN
:
507 if (get_implied_min(left
, &tmp
)) {
513 if (!inside_loop() && known
&& get_implied_max(left
, &tmp
))
516 case SPECIAL_SUB_ASSIGN
:
517 if (get_implied_max(left
, &tmp
)) {
523 if (!inside_loop() && known
&& get_implied_min(left
, &tmp
))
527 set_extra_mod(name
, sym
, alloc_extra_state_range(min
, max
));
529 free_string(right_name
);
533 static void reset_struct_members(const char *name
, struct symbol
*sym
, struct expression
*expr
, void *unused
)
535 struct state_list
*slist
;
536 struct sm_state
*tmp
;
540 slist
= get_all_states(SMATCH_EXTRA
);
541 FOR_EACH_PTR(slist
, tmp
) {
544 if (strncmp(name
, tmp
->name
, len
))
546 if (tmp
->name
[len
] != '-' && tmp
->name
[len
] != '.')
548 set_extra_mod(tmp
->name
, tmp
->sym
, extra_undefined());
549 } END_FOR_EACH_PTR(tmp
);
552 static struct smatch_state
*increment_state(struct smatch_state
*state
)
557 min
= get_dinfo_min(get_dinfo(state
));
558 max
= get_dinfo_max(get_dinfo(state
));
561 max
= whole_range
.max
;
563 if (min
!= whole_range
.min
)
565 if (max
!= whole_range
.max
)
567 return alloc_extra_state_range(min
, max
);
570 static struct smatch_state
*decrement_state(struct smatch_state
*state
)
575 min
= get_dinfo_min(get_dinfo(state
));
576 max
= get_dinfo_max(get_dinfo(state
));
579 min
= whole_range
.min
;
581 if (min
!= whole_range
.min
)
583 if (max
!= whole_range
.max
)
585 return alloc_extra_state_range(min
, max
);
588 static void unop_expr(struct expression
*expr
)
590 struct smatch_state
*state
;
592 if (expr
->smatch_flags
& Handled
)
596 case SPECIAL_INCREMENT
:
597 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
598 state
= increment_state(state
);
599 set_extra_expr_mod(expr
->unop
, state
);
601 case SPECIAL_DECREMENT
:
602 state
= get_state_expr(SMATCH_EXTRA
, expr
->unop
);
603 state
= decrement_state(state
);
604 set_extra_expr_mod(expr
->unop
, state
);
611 static void delete_state_tracker(struct tracker
*t
)
613 remove_from_equiv(t
->name
, t
->sym
);
614 delete_state(t
->owner
, t
->name
, t
->sym
);
617 static void scoped_state_extra(const char *name
, struct symbol
*sym
)
621 t
= alloc_tracker(SMATCH_EXTRA
, name
, sym
);
622 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
625 static void match_declarations(struct symbol
*sym
)
630 name
= sym
->ident
->name
;
631 if (!sym
->initializer
) {
632 set_state(SMATCH_EXTRA
, name
, sym
, extra_undefined());
633 scoped_state_extra(name
, sym
);
638 static void match_function_def(struct symbol
*sym
)
642 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
645 set_state(my_id
, arg
->ident
->name
, arg
, extra_undefined());
646 } END_FOR_EACH_PTR(arg
);
649 static int last_stmt_val(struct statement
*stmt
, long long *val
)
651 struct expression
*expr
;
656 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
657 if (stmt
->type
!= STMT_EXPRESSION
)
659 expr
= stmt
->expression
;
660 return get_value(expr
, val
);
663 static int match_func_comparison(struct expression
*expr
)
665 struct expression
*left
= strip_expr(expr
->left
);
666 struct expression
*right
= strip_expr(expr
->right
);
669 if (left
->type
== EXPR_CALL
) {
670 if (!get_implied_value(right
, &val
))
672 function_comparison(expr
->op
, left
, val
, 1);
676 if (right
->type
== EXPR_CALL
) {
677 if (!get_implied_value(left
, &val
))
679 function_comparison(expr
->op
, right
, val
, 0);
686 static void match_comparison(struct expression
*expr
)
688 struct expression
*left
= strip_expr(expr
->left
);
689 struct expression
*right
= strip_expr(expr
->right
);
690 struct range_list
*left_orig
;
691 struct range_list
*left_true
;
692 struct range_list
*left_false
;
693 struct range_list
*right_orig
;
694 struct range_list
*right_true
;
695 struct range_list
*right_false
;
696 struct smatch_state
*left_true_state
;
697 struct smatch_state
*left_false_state
;
698 struct smatch_state
*right_true_state
;
699 struct smatch_state
*right_false_state
;
701 int right_postop
= 0;
703 if (match_func_comparison(expr
))
706 if (left
->op
== SPECIAL_INCREMENT
|| left
->op
== SPECIAL_DECREMENT
) {
707 if (left
->type
== EXPR_POSTOP
) {
708 left
->smatch_flags
|= Handled
;
709 left_postop
= left
->op
;
711 left
= strip_expr(left
->unop
);
714 if (right
->op
== SPECIAL_INCREMENT
|| right
->op
== SPECIAL_DECREMENT
) {
715 if (right
->type
== EXPR_POSTOP
) {
716 right
->smatch_flags
|= Handled
;
717 right_postop
= right
->op
;
719 right
= strip_expr(right
->unop
);
722 if (!get_implied_range_list(left
, &left_orig
))
723 left_orig
= alloc_range_list(whole_range
.min
, whole_range
.max
);
724 if (!get_implied_range_list(right
, &right_orig
))
725 right_orig
= alloc_range_list(whole_range
.min
, whole_range
.max
);
727 left_true
= clone_range_list(left_orig
);
728 left_false
= clone_range_list(left_orig
);
729 right_true
= clone_range_list(right_orig
);
730 right_false
= clone_range_list(right_orig
);
734 case SPECIAL_UNSIGNED_LT
:
735 if (rl_max(right_orig
) != whole_range
.max
)
736 left_true
= remove_range(left_orig
, rl_max(right_orig
), whole_range
.max
);
737 if (rl_min(right_orig
) != whole_range
.min
)
738 left_false
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
) - 1);
740 if (rl_min(left_orig
) != whole_range
.min
)
741 right_true
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
));
742 if (rl_max(left_orig
) != whole_range
.max
)
743 right_false
= remove_range(right_orig
, rl_max(left_orig
) + 1, whole_range
.max
);
745 case SPECIAL_UNSIGNED_LTE
:
747 if (rl_max(right_orig
) != whole_range
.max
)
748 left_true
= remove_range(left_orig
, rl_max(right_orig
) + 1, whole_range
.max
);
749 if (rl_min(right_orig
) != whole_range
.min
)
750 left_false
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
));
752 if (rl_min(left_orig
) != whole_range
.min
)
753 right_true
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
) - 1);
754 if (rl_max(left_orig
) != whole_range
.max
)
755 right_false
= remove_range(right_orig
, rl_max(left_orig
), whole_range
.max
);
758 if (rl_max(right_orig
) != whole_range
.max
)
759 left_true
= remove_range(left_true
, rl_max(right_orig
) + 1, whole_range
.max
);
760 if (rl_min(right_orig
) != whole_range
.min
)
761 left_true
= remove_range(left_true
, whole_range
.min
, rl_min(right_orig
) - 1);
762 if (rl_min(right_orig
) == rl_max(right_orig
))
763 left_false
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
765 if (rl_max(left_orig
) != whole_range
.max
)
766 right_true
= remove_range(right_true
, rl_max(left_orig
) + 1, whole_range
.max
);
767 if (rl_min(left_orig
) != whole_range
.min
)
768 right_true
= remove_range(right_true
, whole_range
.min
, rl_min(left_orig
) - 1);
769 if (rl_min(left_orig
) == rl_max(left_orig
))
770 right_false
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
772 case SPECIAL_UNSIGNED_GTE
:
774 if (rl_min(right_orig
) != whole_range
.min
)
775 left_true
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
) - 1);
776 if (rl_max(right_orig
) != whole_range
.max
)
777 left_false
= remove_range(left_orig
, rl_max(right_orig
), whole_range
.max
);
779 if (rl_max(left_orig
) != whole_range
.max
)
780 right_true
= remove_range(right_orig
, rl_max(left_orig
) + 1, whole_range
.max
);
781 if (rl_min(left_orig
) != whole_range
.min
)
782 right_false
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
));
785 case SPECIAL_UNSIGNED_GT
:
786 if (rl_min(right_orig
) != whole_range
.min
)
787 left_true
= remove_range(left_orig
, whole_range
.min
, rl_min(right_orig
));
788 if (rl_max(right_orig
) != whole_range
.max
)
789 left_false
= remove_range(left_orig
, rl_max(right_orig
) + 1, whole_range
.max
);
791 if (rl_max(left_orig
) != whole_range
.max
)
792 right_true
= remove_range(right_orig
, rl_max(left_orig
), whole_range
.max
);
793 if (rl_min(left_orig
) != whole_range
.min
)
794 right_false
= remove_range(right_orig
, whole_range
.min
, rl_min(left_orig
) - 1);
796 case SPECIAL_NOTEQUAL
:
797 if (rl_max(right_orig
) != whole_range
.max
)
798 left_false
= remove_range(left_false
, rl_max(right_orig
) + 1, whole_range
.max
);
799 if (rl_min(right_orig
) != whole_range
.min
)
800 left_false
= remove_range(left_false
, whole_range
.min
, rl_min(right_orig
) - 1);
801 if (rl_min(right_orig
) == rl_max(right_orig
))
802 left_true
= remove_range(left_orig
, rl_min(right_orig
), rl_min(right_orig
));
804 if (rl_max(left_orig
) != whole_range
.max
)
805 right_false
= remove_range(right_false
, rl_max(left_orig
) + 1, whole_range
.max
);
806 if (rl_min(left_orig
) != whole_range
.min
)
807 right_false
= remove_range(right_false
, whole_range
.min
, rl_min(left_orig
) - 1);
808 if (rl_min(left_orig
) == rl_max(left_orig
))
809 right_true
= remove_range(right_orig
, rl_min(left_orig
), rl_min(left_orig
));
815 left_true_state
= alloc_extra_state_range_list(left_true
);
816 left_false_state
= alloc_extra_state_range_list(left_false
);
817 right_true_state
= alloc_extra_state_range_list(right_true
);
818 right_false_state
= alloc_extra_state_range_list(right_false
);
820 if (left_postop
== SPECIAL_INCREMENT
) {
821 left_true_state
= increment_state(left_true_state
);
822 left_false_state
= increment_state(left_false_state
);
824 if (left_postop
== SPECIAL_DECREMENT
) {
825 left_true_state
= decrement_state(left_true_state
);
826 left_false_state
= decrement_state(left_false_state
);
828 if (right_postop
== SPECIAL_INCREMENT
) {
829 right_true_state
= increment_state(right_true_state
);
830 right_false_state
= increment_state(right_false_state
);
832 if (right_postop
== SPECIAL_DECREMENT
) {
833 right_true_state
= decrement_state(right_true_state
);
834 right_false_state
= decrement_state(right_false_state
);
837 set_extra_expr_true_false(left
, left_true_state
, left_false_state
);
838 set_extra_expr_true_false(right
, right_true_state
, right_false_state
);
841 /* this is actually hooked from smatch_implied.c... it's hacky, yes */
842 void __extra_match_condition(struct expression
*expr
)
846 struct smatch_state
*pre_state
;
847 struct smatch_state
*true_state
;
848 struct smatch_state
*false_state
;
850 expr
= strip_expr(expr
);
851 switch (expr
->type
) {
853 function_comparison(SPECIAL_NOTEQUAL
, expr
, 0, 1);
858 name
= get_variable_from_expr(expr
, &sym
);
861 pre_state
= get_state(my_id
, name
, sym
);
862 true_state
= add_filter(pre_state
, 0);
863 if (possibly_true(SPECIAL_EQUAL
, get_dinfo(pre_state
), 0, 0))
864 false_state
= alloc_extra_state(0);
866 false_state
= alloc_extra_state_empty();
867 set_extra_true_false(name
, sym
, true_state
, false_state
);
871 match_comparison(expr
);
873 case EXPR_ASSIGNMENT
:
874 __extra_match_condition(expr
->left
);
879 /* returns 1 if it is not possible for expr to be value, otherwise returns 0 */
880 int implied_not_equal(struct expression
*expr
, long long val
)
884 struct smatch_state
*state
;
888 if (get_value(expr
, &expr_val
))
889 return expr_val
!= val
;
891 name
= get_variable_from_expr(expr
, &sym
);
894 state
= get_state(my_id
, name
, sym
);
895 if (!state
|| !state
->data
)
897 ret
= !possibly_false(SPECIAL_NOTEQUAL
, get_dinfo(state
), val
, 1);
903 int known_condition_true(struct expression
*expr
)
906 struct statement
*stmt
;
911 if (get_value(expr
, &tmp
) && tmp
)
914 expr
= strip_expr(expr
);
915 switch (expr
->type
) {
917 if (expr
->op
== '!') {
918 if (known_condition_false(expr
->unop
))
922 stmt
= get_expression_statement(expr
);
923 if (last_stmt_val(stmt
, &tmp
) && tmp
== 1)
932 int known_condition_false(struct expression
*expr
)
935 struct statement
*stmt
;
943 switch (expr
->type
) {
945 if (expr
->op
== '!') {
946 if (known_condition_true(expr
->unop
))
950 stmt
= get_expression_statement(expr
);
951 if (last_stmt_val(stmt
, &tmp
) && tmp
== 0)
955 if (sym_name_is("__builtin_constant_p", expr
->fn
))
964 struct range_list
*get_range_list(struct expression
*expr
)
968 struct range_list
*ret
= NULL
;
969 struct smatch_state
*state
;
971 state
= get_state_expr(SMATCH_EXTRA
, expr
);
973 return clone_range_list(get_dinfo(state
)->value_ranges
);
974 if (!get_absolute_min(expr
, &min
))
976 if (!get_absolute_max(expr
, &max
))
978 add_range(&ret
, min
, max
);
982 static int do_comparison(struct expression
*expr
)
984 struct range_list
*left_ranges
;
985 struct range_list
*right_ranges
;
986 int poss_true
, poss_false
;
988 left_ranges
= get_range_list(expr
->left
);
989 right_ranges
= get_range_list(expr
->right
);
991 poss_true
= possibly_true_range_lists(left_ranges
, expr
->op
, right_ranges
);
992 poss_false
= possibly_false_range_lists(left_ranges
, expr
->op
, right_ranges
);
994 free_range_list(&left_ranges
);
995 free_range_list(&right_ranges
);
997 if (!poss_true
&& !poss_false
)
999 if (poss_true
&& !poss_false
)
1001 if (!poss_true
&& poss_false
)
1006 int implied_condition_true(struct expression
*expr
)
1008 struct statement
*stmt
;
1015 if (get_implied_value(expr
, &tmp
) && tmp
)
1018 if (expr
->type
== EXPR_POSTOP
)
1019 return implied_condition_true(expr
->unop
);
1021 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_DECREMENT
)
1022 return implied_not_equal(expr
->unop
, 1);
1023 if (expr
->type
== EXPR_PREOP
&& expr
->op
== SPECIAL_INCREMENT
)
1024 return implied_not_equal(expr
->unop
, -1);
1026 expr
= strip_expr(expr
);
1027 switch (expr
->type
) {
1029 if (do_comparison(expr
) == 1)
1033 if (expr
->op
== '!') {
1034 if (implied_condition_false(expr
->unop
))
1038 stmt
= get_expression_statement(expr
);
1039 if (last_stmt_val(stmt
, &val
) && val
== 1)
1043 if (implied_not_equal(expr
, 0) == 1)
1050 int implied_condition_false(struct expression
*expr
)
1052 struct statement
*stmt
;
1053 struct expression
*tmp
;
1062 switch (expr
->type
) {
1064 if (do_comparison(expr
) == 2)
1067 if (expr
->op
== '!') {
1068 if (implied_condition_true(expr
->unop
))
1072 stmt
= get_expression_statement(expr
);
1073 if (last_stmt_val(stmt
, &val
) && val
== 0)
1075 tmp
= strip_expr(expr
);
1077 return implied_condition_false(tmp
);
1080 if (get_implied_value(expr
, &val
) && val
== 0)
1087 int get_implied_range_list(struct expression
*expr
, struct range_list
**rl
)
1090 struct smatch_state
*state
;
1094 expr
= strip_parens(expr
);
1096 state
= get_state_expr(my_id
, expr
);
1098 *rl
= clone_range_list(get_dinfo(state
)->value_ranges
);
1102 if (get_implied_value(expr
, &val
)) {
1104 add_range(rl
, val
, val
);
1108 if (expr
->type
!= EXPR_BINOP
)
1111 if (expr
->type
== EXPR_CALL
) {
1112 *rl
= db_return_vals(expr
);
1116 if (expr
->op
== '%') {
1117 if (!get_implied_value(expr
->right
, &val
))
1120 add_range(rl
, 0, val
- 1);
1124 if (!get_implied_min(expr
, &min
))
1126 if (!get_implied_max(expr
, &max
))
1129 *rl
= alloc_range_list(min
, max
);
1133 int is_whole_range(struct smatch_state
*state
)
1135 struct data_info
*dinfo
;
1136 struct data_range
*drange
;
1140 dinfo
= get_dinfo(state
);
1141 if (!dinfo
|| !dinfo
->value_ranges
)
1143 drange
= first_ptr_list((struct ptr_list
*)dinfo
->value_ranges
);
1144 if (drange
->min
== whole_range
.min
&& drange
->max
== whole_range
.max
)
1149 static void struct_member_callback(char *fn
, int param
, char *printed_name
, struct smatch_state
*state
)
1151 if (is_whole_range(state
))
1153 sm_msg("info: passes param_value '%s' %d '%s' %s", fn
, param
, printed_name
, state
->name
);
1156 static void match_call_info(struct expression
*expr
)
1158 struct range_list
*rl
= NULL
;
1159 struct expression
*arg
;
1163 name
= get_fnptr_name(expr
->fn
);
1167 FOR_EACH_PTR(expr
->args
, arg
) {
1168 if (get_implied_range_list(arg
, &rl
) && !is_whole_range_rl(rl
)) {
1169 sm_msg("info: passes param_value '%s' %d '$$' %s",
1170 name
, i
, show_ranges(rl
));
1173 } END_FOR_EACH_PTR(arg
);
1178 void set_param_value(const char *name
, struct symbol
*sym
, char *key
, char *value
)
1180 struct range_list
*rl
= NULL
;
1181 struct smatch_state
*state
;
1184 if (strncmp(key
, "$$", 2))
1187 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
1188 get_value_ranges(value
, &rl
);
1189 char *tmp
= show_ranges(rl
);
1190 if (strcmp(tmp
, value
))
1191 sm_msg("value = %s, ranges = %s", value
, tmp
);
1192 state
= alloc_extra_state_range_list(rl
);
1193 set_state(SMATCH_EXTRA
, fullname
, sym
, state
);
1196 static void match_call_assign(struct expression
*expr
)
1198 struct expression
*right
;
1199 struct range_list
*rl
;
1201 if (expr
->op
!= '=')
1204 right
= strip_parens(expr
->right
);
1205 while (right
->type
== EXPR_ASSIGNMENT
&& right
->op
== '=')
1206 right
= strip_parens(right
->left
);
1208 rl
= db_return_vals(right
);
1210 set_extra_expr_mod(expr
->left
, alloc_extra_state_range_list(rl
));
1212 set_extra_expr_mod(expr
->left
, extra_undefined());
1215 void register_smatch_extra(int id
)
1218 add_merge_hook(my_id
, &merge_func
);
1219 add_unmatched_state_hook(my_id
, &unmatched_state
);
1220 add_hook(&unop_expr
, OP_HOOK
);
1221 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
1222 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
1223 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
1224 add_hook(&match_call_assign
, CALL_ASSIGNMENT_HOOK
);
1225 add_hook(&match_declarations
, DECLARATION_HOOK
);
1227 add_hook(&match_call_info
, FUNCTION_CALL_HOOK
);
1228 add_member_info_callback(my_id
, struct_member_callback
);
1230 add_definition_db_callback(set_param_value
, PARAM_VALUE
);
1233 void register_smatch_extra_late(int id
)
1235 set_default_modification_hook(SMATCH_EXTRA
, reset_struct_members
);