2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
31 struct expression
*__inline_fn
;
33 static int __smatch_lineno
= 0;
35 static char *base_file
;
36 static const char *filename
;
37 static char *pathname
;
38 static char *full_filename
;
39 static char *cur_func
;
40 static unsigned int loop_count
;
41 static int last_goto_statement_handled
;
42 int __expr_stmt_count
;
43 int __in_function_def
;
44 static struct expression_list
*switch_expr_stack
= NULL
;
45 static struct expression_list
*post_op_stack
= NULL
;
47 struct expression_list
*big_expression_stack
;
48 struct statement_list
*big_statement_stack
;
49 int __in_pre_condition
= 0;
50 int __bail_on_rest_of_function
= 0;
51 static struct timeval fn_start_time
;
52 char *get_function(void) { return cur_func
; }
53 int get_lineno(void) { return __smatch_lineno
; }
54 int inside_loop(void) { return !!loop_count
; }
55 int definitely_inside_loop(void) { return !!(loop_count
& ~0x80000000); }
56 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
57 int in_expression_statement(void) { return !!__expr_stmt_count
; }
59 static void split_symlist(struct symbol_list
*sym_list
);
60 static void split_declaration(struct symbol_list
*sym_list
);
61 static void split_expr_list(struct expression_list
*expr_list
);
62 static void add_inline_function(struct symbol
*sym
);
63 static void parse_inline(struct expression
*expr
);
65 int option_assume_loops
= 0;
66 int option_known_conditions
= 0;
67 int option_two_passes
= 0;
68 struct symbol
*cur_func_sym
= NULL
;
69 struct stree
*global_states
;
71 long long valid_ptr_min
= 4096;
72 long long valid_ptr_max
= 7777777777;
73 sval_t valid_ptr_min_sval
= {
77 sval_t valid_ptr_max_sval
= {
79 {.value
= LONG_MAX
- 100000},
82 static void set_valid_ptr_max(void)
84 if (type_bits(&ptr_ctype
) == 32)
85 valid_ptr_max
= 7777777777;
86 else if (type_bits(&ptr_ctype
) == 64)
87 valid_ptr_max
= 7777777777777777777LL;
89 valid_ptr_max_sval
.value
= valid_ptr_max
;
92 int outside_of_function(void)
94 return cur_func_sym
== NULL
;
97 const char *get_filename(void)
101 if (option_full_path
)
102 return full_filename
;
106 const char *get_base_file(void)
111 static void set_position(struct position pos
)
114 static int prev_stream
= -1;
116 if (pos
.stream
== 0 && pos
.line
== 0)
119 __smatch_lineno
= pos
.line
;
121 if (pos
.stream
== prev_stream
)
124 filename
= stream_name(pos
.stream
);
127 pathname
= getcwd(NULL
, 0);
129 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
130 full_filename
= malloc(len
);
131 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
133 full_filename
= alloc_string(filename
);
138 static int is_inline_func(struct expression
*expr
)
140 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
142 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
147 static int is_noreturn_func(struct expression
*expr
)
149 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
151 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
156 int inlinable(struct expression
*expr
)
160 if (__inline_fn
) /* don't nest */
163 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
165 if (is_no_inline_function(expr
->symbol
->ident
->name
))
167 sym
= get_base_type(expr
->symbol
);
168 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
169 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
173 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
174 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
181 void __process_post_op_stack(void)
183 struct expression
*expr
;
185 FOR_EACH_PTR(post_op_stack
, expr
) {
186 __pass_to_client(expr
, OP_HOOK
);
187 } END_FOR_EACH_PTR(expr
);
189 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
192 void __split_expr(struct expression
*expr
)
197 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
199 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
201 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
204 push_expression(&big_expression_stack
, expr
);
205 set_position(expr
->pos
);
206 __pass_to_client(expr
, EXPR_HOOK
);
208 switch (expr
->type
) {
211 __pass_to_client(expr
, DEREF_HOOK
);
212 __split_expr(expr
->unop
);
213 __pass_to_client(expr
, OP_HOOK
);
216 __split_expr(expr
->unop
);
217 push_expression(&post_op_stack
, expr
);
221 __split_stmt(expr
->statement
);
226 __pass_to_client(expr
, LOGIC_HOOK
);
227 __handle_logic(expr
);
230 __pass_to_client(expr
, BINOP_HOOK
);
232 __split_expr(expr
->left
);
233 __process_post_op_stack();
234 __split_expr(expr
->right
);
236 case EXPR_ASSIGNMENT
: {
237 struct expression
*tmp
;
242 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
245 if (__handle_condition_assigns(expr
))
247 /* foo = (x < 5 ? foo : 5); */
248 if (__handle_select_assigns(expr
))
250 /* foo = ({frob(); frob(); frob(); 1;}) */
251 if (__handle_expr_statement_assigns(expr
))
254 __split_expr(expr
->right
);
255 if (outside_of_function())
256 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
258 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
260 __fake_struct_member_assignments(expr
);
262 tmp
= strip_expr(expr
->right
);
263 if (tmp
->type
== EXPR_CALL
)
264 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
265 if (get_macro_name(tmp
->pos
) &&
266 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
267 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
268 __split_expr(expr
->left
);
272 __pass_to_client(expr
, DEREF_HOOK
);
273 __split_expr(expr
->deref
);
276 __split_expr(expr
->base
);
279 case EXPR_FORCE_CAST
:
280 __pass_to_client(expr
, CAST_HOOK
);
281 __split_expr(expr
->cast_expression
);
284 if (expr
->cast_expression
)
285 __pass_to_client(strip_parens(expr
->cast_expression
),
290 evaluate_expression(expr
);
292 case EXPR_CONDITIONAL
:
294 if (known_condition_true(expr
->conditional
)) {
295 __split_expr(expr
->cond_true
);
298 if (known_condition_false(expr
->conditional
)) {
299 __split_expr(expr
->cond_false
);
302 __pass_to_client(expr
, SELECT_HOOK
);
303 __split_whole_condition(expr
->conditional
);
304 __split_expr(expr
->cond_true
);
305 __push_true_states();
306 __use_false_states();
307 __split_expr(expr
->cond_false
);
308 __merge_true_states();
311 if (sym_name_is("__builtin_constant_p", expr
->fn
))
313 split_expr_list(expr
->args
);
314 __split_expr(expr
->fn
);
315 if (is_inline_func(expr
->fn
))
316 add_inline_function(expr
->fn
->symbol
);
317 if (inlinable(expr
->fn
))
319 __process_post_op_stack();
320 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
322 if (inlinable(expr
->fn
)) {
325 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
326 if (is_noreturn_func(expr
->fn
))
329 case EXPR_INITIALIZER
:
330 split_expr_list(expr
->expr_list
);
332 case EXPR_IDENTIFIER
:
333 __split_expr(expr
->ident_expression
);
336 __split_expr(expr
->idx_expression
);
339 __split_expr(expr
->init_expr
);
342 __pass_to_client(expr
, SYM_HOOK
);
345 __pass_to_client(expr
, STRING_HOOK
);
350 pop_expression(&big_expression_stack
);
353 static int is_forever_loop(struct statement
*stmt
)
355 struct expression
*expr
;
357 expr
= strip_expr(stmt
->iterator_pre_condition
);
359 expr
= stmt
->iterator_post_condition
;
361 /* this is a for(;;) loop... */
365 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
372 static char *get_loop_name(int num
)
376 snprintf(buf
, 255, "-loop%d", num
);
378 return alloc_sname(buf
);
382 * Pre Loops are while and for loops.
384 static void handle_pre_loop(struct statement
*stmt
)
386 int once_through
; /* we go through the loop at least once */
387 struct sm_state
*extra_sm
= NULL
;
390 struct stree
*stree
= NULL
;
391 struct sm_state
*sm
= NULL
;
393 loop_name
= get_loop_name(loop_num
);
396 __split_stmt(stmt
->iterator_pre_statement
);
398 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
404 __merge_gotos(loop_name
);
406 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
407 __in_pre_condition
++;
408 __pass_to_client(stmt
, PRELOOP_HOOK
);
409 __split_whole_condition(stmt
->iterator_pre_condition
);
410 __in_pre_condition
--;
411 FOR_EACH_SM(stree
, sm
) {
412 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
413 } END_FOR_EACH_SM(sm
);
416 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
418 if (option_assume_loops
)
421 __split_stmt(stmt
->iterator_statement
);
422 __warn_on_silly_pre_loops();
423 if (is_forever_loop(stmt
)) {
425 __save_gotos(loop_name
);
427 __push_fake_cur_stree();
428 __split_stmt(stmt
->iterator_post_statement
);
429 stree
= __pop_fake_cur_stree();
431 __discard_false_states();
434 if (!__path_is_null())
435 __merge_stree_into_cur(stree
);
439 unchanged
= __iterator_unchanged(extra_sm
);
440 __split_stmt(stmt
->iterator_post_statement
);
441 __save_gotos(loop_name
);
442 __in_pre_condition
++;
443 __split_whole_condition(stmt
->iterator_pre_condition
);
444 __in_pre_condition
--;
446 __merge_false_states();
448 __discard_false_states();
450 __merge_false_states();
452 if (extra_sm
&& unchanged
)
453 __extra_pre_loop_hook_after(extra_sm
,
454 stmt
->iterator_post_statement
,
455 stmt
->iterator_pre_condition
);
462 * Post loops are do {} while();
464 static void handle_post_loop(struct statement
*stmt
)
468 loop_name
= get_loop_name(loop_num
);
474 __merge_gotos(loop_name
);
475 __split_stmt(stmt
->iterator_statement
);
477 if (!is_zero(stmt
->iterator_post_condition
))
478 __save_gotos(loop_name
);
480 if (is_forever_loop(stmt
)) {
483 __split_whole_condition(stmt
->iterator_post_condition
);
484 __use_false_states();
490 static int empty_statement(struct statement
*stmt
)
494 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
499 static int last_stmt_on_same_line(void)
501 struct statement
*stmt
;
504 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
507 if (stmt
->pos
.line
== get_lineno())
510 } END_FOR_EACH_PTR_REVERSE(stmt
);
514 static void split_asm_constraints(struct expression_list
*expr_list
)
516 struct expression
*expr
;
519 FOR_EACH_PTR(expr_list
, expr
) {
521 case 0: /* identifier */
522 case 1: /* constraint */
525 case 2: /* expression */
530 } END_FOR_EACH_PTR(expr
);
533 static int is_case_val(struct statement
*stmt
, sval_t sval
)
537 if (stmt
->type
!= STMT_CASE
)
539 if (!stmt
->case_expression
) {
543 if (!get_value(stmt
->case_expression
, &case_sval
))
545 if (case_sval
.value
== sval
.value
)
550 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
552 struct statement
*tmp
;
554 __split_expr(stmt
->switch_expression
);
556 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
557 __save_switch_states(top_expression(switch_expr_stack
));
562 stmt
= stmt
->switch_statement
;
564 __push_scope_hooks();
565 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
566 __smatch_lineno
= tmp
->pos
.line
;
567 if (is_case_val(tmp
, sval
)) {
568 __merge_switches(top_expression(switch_expr_stack
),
569 stmt
->case_expression
);
570 __pass_case_to_client(top_expression(switch_expr_stack
),
571 stmt
->case_expression
);
573 if (__path_is_null())
576 if (__path_is_null()) {
580 } END_FOR_EACH_PTR(tmp
);
582 __call_scope_hooks();
583 if (!__pop_default())
584 __merge_switches(top_expression(switch_expr_stack
),
586 __discard_switches();
588 pop_expression(&switch_expr_stack
);
591 static int taking_too_long(void)
595 ms
= ms_since(&fn_start_time
);
596 if (ms
> 1000 * 60 * 5) /* five minutes */
601 static int is_last_stmt(struct statement
*cur_stmt
)
603 struct symbol
*fn
= get_base_type(cur_func_sym
);
604 struct statement
*stmt
;
610 stmt
= fn
->inline_stmt
;
611 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
613 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
614 if (stmt
&& stmt
->type
== STMT_LABEL
)
615 stmt
= stmt
->label_statement
;
616 if (stmt
== cur_stmt
)
621 static void handle_backward_goto(struct statement
*goto_stmt
)
623 const char *goto_name
, *label_name
;
624 struct statement
*func_stmt
;
625 struct symbol
*base_type
= get_base_type(cur_func_sym
);
626 struct statement
*tmp
;
631 if (last_goto_statement_handled
)
633 last_goto_statement_handled
= 1;
635 if (!goto_stmt
->goto_label
||
636 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
637 !goto_stmt
->goto_label
->ident
)
639 goto_name
= goto_stmt
->goto_label
->ident
->name
;
641 func_stmt
= base_type
->stmt
;
643 func_stmt
= base_type
->inline_stmt
;
646 if (func_stmt
->type
!= STMT_COMPOUND
)
649 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
651 if (tmp
->type
!= STMT_LABEL
)
653 if (!tmp
->label_identifier
||
654 tmp
->label_identifier
->type
!= SYM_LABEL
||
655 !tmp
->label_identifier
->ident
)
657 label_name
= tmp
->label_identifier
->ident
->name
;
658 if (strcmp(goto_name
, label_name
) != 0)
663 } END_FOR_EACH_PTR(tmp
);
666 static void fake_a_return(void)
668 struct symbol
*return_type
;
673 return_type
= get_real_base_type(cur_func_sym
);
674 return_type
= get_real_base_type(return_type
);
675 if (return_type
!= &void_ctype
) {
676 __pass_to_client(unknown_value_expression(NULL
), RETURN_HOOK
);
680 __pass_to_client(cur_func_sym
, END_FUNC_HOOK
);
681 __pass_to_client(cur_func_sym
, AFTER_FUNC_HOOK
);
684 void __split_stmt(struct statement
*stmt
)
691 if (__bail_on_rest_of_function
|| out_of_memory() || taking_too_long()) {
692 static char *printed
= NULL
;
694 __bail_on_rest_of_function
= 1;
695 if (printed
!= cur_func
)
696 sm_msg("Function too hairy. Giving up.");
698 final_pass
= 0; /* turn off sm_msg() from here */
703 add_ptr_list(&big_statement_stack
, stmt
);
704 free_expression_stack(&big_expression_stack
);
705 set_position(stmt
->pos
);
706 __pass_to_client(stmt
, STMT_HOOK
);
708 switch (stmt
->type
) {
709 case STMT_DECLARATION
:
710 split_declaration(stmt
->declaration
);
713 __split_expr(stmt
->ret_value
);
714 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
715 __process_post_op_stack();
718 case STMT_EXPRESSION
:
719 __split_expr(stmt
->expression
);
721 case STMT_COMPOUND
: {
722 struct statement
*tmp
;
724 __push_scope_hooks();
725 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
727 } END_FOR_EACH_PTR(tmp
);
728 __call_scope_hooks();
732 if (known_condition_true(stmt
->if_conditional
)) {
733 __split_stmt(stmt
->if_true
);
736 if (known_condition_false(stmt
->if_conditional
)) {
737 __split_stmt(stmt
->if_false
);
740 if (option_known_conditions
&&
741 implied_condition_true(stmt
->if_conditional
)) {
742 sm_info("this condition is true.");
743 __split_stmt(stmt
->if_true
);
746 if (option_known_conditions
&&
747 implied_condition_false(stmt
->if_conditional
)) {
748 sm_info("this condition is false.");
749 __split_stmt(stmt
->if_false
);
752 __split_whole_condition(stmt
->if_conditional
);
753 __split_stmt(stmt
->if_true
);
754 if (empty_statement(stmt
->if_true
) &&
755 last_stmt_on_same_line() &&
756 !get_macro_name(stmt
->if_true
->pos
))
757 sm_msg("warn: if();");
758 __push_true_states();
759 __use_false_states();
760 __split_stmt(stmt
->if_false
);
761 __merge_true_states();
764 if (stmt
->iterator_pre_condition
)
765 handle_pre_loop(stmt
);
766 else if (stmt
->iterator_post_condition
)
767 handle_post_loop(stmt
);
769 // these are for(;;) type loops.
770 handle_pre_loop(stmt
);
774 if (get_value(stmt
->switch_expression
, &sval
)) {
775 split_known_switch(stmt
, sval
);
778 __split_expr(stmt
->switch_expression
);
779 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
780 __save_switch_states(top_expression(switch_expr_stack
));
784 __split_stmt(stmt
->switch_statement
);
785 if (!__pop_default())
786 __merge_switches(top_expression(switch_expr_stack
),
788 __discard_switches();
790 pop_expression(&switch_expr_stack
);
793 __merge_switches(top_expression(switch_expr_stack
),
794 stmt
->case_expression
);
795 __pass_case_to_client(top_expression(switch_expr_stack
),
796 stmt
->case_expression
);
797 if (!stmt
->case_expression
)
799 __split_expr(stmt
->case_expression
);
800 __split_expr(stmt
->case_to
);
801 __split_stmt(stmt
->case_statement
);
804 if (stmt
->label_identifier
&&
805 stmt
->label_identifier
->type
== SYM_LABEL
&&
806 stmt
->label_identifier
->ident
) {
807 loop_count
|= 0x80000000;
808 __merge_gotos(stmt
->label_identifier
->ident
->name
);
810 __split_stmt(stmt
->label_statement
);
813 __split_expr(stmt
->goto_expression
);
814 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
815 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
817 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
819 __process_continues();
821 } else if (stmt
->goto_label
&&
822 stmt
->goto_label
->type
== SYM_LABEL
&&
823 stmt
->goto_label
->ident
) {
824 __save_gotos(stmt
->goto_label
->ident
->name
);
827 if (is_last_stmt(stmt
))
828 handle_backward_goto(stmt
);
833 __pass_to_client(stmt
, ASM_HOOK
);
834 __split_expr(stmt
->asm_string
);
835 split_asm_constraints(stmt
->asm_outputs
);
836 split_asm_constraints(stmt
->asm_inputs
);
837 split_asm_constraints(stmt
->asm_clobbers
);
842 __split_expr(stmt
->range_expression
);
843 __split_expr(stmt
->range_low
);
844 __split_expr(stmt
->range_high
);
847 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
849 __process_post_op_stack();
852 static void split_expr_list(struct expression_list
*expr_list
)
854 struct expression
*expr
;
856 FOR_EACH_PTR(expr_list
, expr
) {
858 __process_post_op_stack();
859 } END_FOR_EACH_PTR(expr
);
862 static void split_sym(struct symbol
*sym
)
866 if (!(sym
->namespace & NS_SYMBOL
))
869 __split_stmt(sym
->stmt
);
870 __split_expr(sym
->array_size
);
871 split_symlist(sym
->arguments
);
872 split_symlist(sym
->symbol_list
);
873 __split_stmt(sym
->inline_stmt
);
874 split_symlist(sym
->inline_symbol_list
);
877 static void split_symlist(struct symbol_list
*sym_list
)
881 FOR_EACH_PTR(sym_list
, sym
) {
883 } END_FOR_EACH_PTR(sym
);
886 typedef void (fake_cb
)(struct expression
*expr
);
888 static int member_to_number(struct expression
*expr
, struct ident
*member
)
890 struct symbol
*type
, *tmp
;
898 type
= get_type(expr
);
899 if (!type
|| type
->type
!= SYM_STRUCT
)
903 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
907 if (strcmp(name
, tmp
->ident
->name
) == 0)
909 } END_FOR_EACH_PTR(tmp
);
913 static struct ident
*number_to_member(struct expression
*expr
, int num
)
915 struct symbol
*type
, *member
;
918 type
= get_type(expr
);
919 if (!type
|| type
->type
!= SYM_STRUCT
)
922 FOR_EACH_PTR(type
->symbol_list
, member
) {
924 return member
->ident
;
926 } END_FOR_EACH_PTR(member
);
930 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
937 static struct member_set
*alloc_member_set(struct symbol
*type
)
939 struct member_set
*member_set
;
940 struct symbol
*member
;
944 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
945 member_set
= malloc(member_count
* sizeof(*member_set
));
947 FOR_EACH_PTR(type
->symbol_list
, member
) {
948 member_set
[member_idx
].ident
= member
->ident
;
949 member_set
[member_idx
].set
= 0;
951 } END_FOR_EACH_PTR(member
);
956 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
958 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
961 for (i
= 0; i
< member_count
; i
++) {
962 if (member_set
[i
].ident
== ident
) {
963 member_set
[i
].set
= 1;
967 // crap. this is buggy.
968 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
971 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
973 struct expression
*deref
, *assign
;
974 struct symbol
*member
, *member_type
;
978 FOR_EACH_PTR(type
->symbol_list
, member
) {
979 if (!member
->ident
|| member_set
[member_idx
].set
) {
983 member_type
= get_real_base_type(member
);
984 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
988 /* TODO: this should be handled recursively and not ignored */
989 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
993 deref
= member_expression(symbol
, '.', member
->ident
);
994 assign
= assign_expression(deref
, zero_expr());
995 __split_expr(assign
);
997 } END_FOR_EACH_PTR(member
);
1001 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
1003 struct expression
*deref
, *assign
, *tmp
;
1004 struct symbol
*struct_type
, *type
;
1005 struct ident
*member
;
1007 struct member_set
*member_set
;
1009 struct_type
= get_type(symbol
);
1011 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
1014 member_set
= alloc_member_set(struct_type
);
1017 FOR_EACH_PTR(members
, tmp
) {
1018 member
= number_to_member(symbol
, member_idx
);
1019 while (tmp
->type
== EXPR_IDENTIFIER
) {
1020 member
= tmp
->expr_ident
;
1021 member_idx
= member_to_number(symbol
, member
);
1022 tmp
= tmp
->ident_expression
;
1024 mark_member_as_set(struct_type
, member_set
, member
);
1026 deref
= member_expression(symbol
, '.', member
);
1027 if (tmp
->type
== EXPR_INITIALIZER
) {
1028 type
= get_type(deref
);
1029 if (type
&& type
->type
== SYM_ARRAY
)
1030 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1032 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
1034 assign
= assign_expression(deref
, tmp
);
1037 } END_FOR_EACH_PTR(tmp
);
1039 set_unset_to_zero(symbol
, struct_type
, member_set
);
1042 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1044 fake_member_assigns_helper(symbol_expression(sym
),
1045 sym
->initializer
->expr_list
, fake_cb
);
1048 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1050 struct expression
*offset
, *binop
, *assign
, *tmp
;
1051 struct symbol
*type
;
1055 FOR_EACH_PTR(expr_list
, tmp
) {
1056 if (tmp
->type
== EXPR_INDEX
) {
1057 if (tmp
->idx_from
!= tmp
->idx_to
)
1059 idx
= tmp
->idx_from
;
1060 if (!tmp
->idx_expression
)
1062 tmp
= tmp
->idx_expression
;
1064 offset
= value_expr(idx
);
1065 binop
= array_element_expression(array
, offset
);
1066 if (tmp
->type
== EXPR_INITIALIZER
) {
1067 type
= get_type(binop
);
1068 if (type
&& type
->type
== SYM_ARRAY
)
1069 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1071 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1073 assign
= assign_expression(binop
, tmp
);
1078 } END_FOR_EACH_PTR(tmp
);
1081 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1083 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1086 static void fake_assign_expr(struct symbol
*sym
)
1088 struct expression
*assign
, *symbol
;
1090 symbol
= symbol_expression(sym
);
1091 assign
= assign_expression(symbol
, sym
->initializer
);
1092 __split_expr(assign
);
1095 static void call_split_expr(struct expression
*expr
)
1100 static void do_initializer_stuff(struct symbol
*sym
)
1102 if (!sym
->initializer
)
1105 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1106 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1107 fake_element_assigns(sym
, call_split_expr
);
1109 fake_member_assigns(sym
, call_split_expr
);
1111 fake_assign_expr(sym
);
1115 static void split_declaration(struct symbol_list
*sym_list
)
1119 FOR_EACH_PTR(sym_list
, sym
) {
1120 __pass_to_client(sym
, DECLARATION_HOOK
);
1121 do_initializer_stuff(sym
);
1123 } END_FOR_EACH_PTR(sym
);
1126 static void call_global_assign_hooks(struct expression
*assign
)
1128 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1131 static void fake_global_assign(struct symbol
*sym
)
1133 struct expression
*assign
, *symbol
;
1135 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1136 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1137 fake_element_assigns(sym
, call_global_assign_hooks
);
1138 } else if (sym
->initializer
) {
1139 symbol
= symbol_expression(sym
);
1140 assign
= assign_expression(symbol
, sym
->initializer
);
1141 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1143 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1145 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1146 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1147 fake_member_assigns(sym
, call_global_assign_hooks
);
1148 } else if (sym
->initializer
) {
1149 symbol
= symbol_expression(sym
);
1150 assign
= assign_expression(symbol
, sym
->initializer
);
1151 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1153 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1156 symbol
= symbol_expression(sym
);
1157 if (sym
->initializer
)
1158 assign
= assign_expression(symbol
, sym
->initializer
);
1160 assign
= assign_expression(symbol
, zero_expr());
1161 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1165 static void start_function_definition(struct symbol
*sym
)
1167 __in_function_def
= 1;
1168 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1169 __in_function_def
= 0;
1170 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1174 static void split_function(struct symbol
*sym
)
1176 struct symbol
*base_type
= get_base_type(sym
);
1178 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1181 gettimeofday(&fn_start_time
, NULL
);
1184 cur_func
= sym
->ident
->name
;
1185 __smatch_lineno
= sym
->pos
.line
;
1187 last_goto_statement_handled
= 0;
1188 sm_debug("new function: %s\n", cur_func
);
1190 if (option_two_passes
) {
1194 start_function_definition(sym
);
1195 __split_stmt(base_type
->stmt
);
1196 __split_stmt(base_type
->inline_stmt
);
1202 start_function_definition(sym
);
1203 __split_stmt(base_type
->stmt
);
1204 __split_stmt(base_type
->inline_stmt
);
1205 __pass_to_client(sym
, END_FUNC_HOOK
);
1206 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1208 cur_func_sym
= NULL
;
1211 free_data_info_allocs();
1212 free_expression_stack(&switch_expr_stack
);
1213 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1214 __bail_on_rest_of_function
= 0;
1217 static void parse_inline(struct expression
*call
)
1219 struct symbol
*base_type
;
1220 int loop_num_bak
= loop_num
;
1221 int final_pass_bak
= final_pass
;
1222 char *cur_func_bak
= cur_func
;
1223 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1224 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1225 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1226 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1228 __pass_to_client(call
, INLINE_FN_START
);
1229 final_pass
= 0; /* don't print anything */
1232 base_type
= get_base_type(call
->fn
->symbol
);
1233 cur_func_sym
= call
->fn
->symbol
;
1234 if (call
->fn
->symbol
->ident
)
1235 cur_func
= call
->fn
->symbol
->ident
->name
;
1238 set_position(call
->fn
->symbol
->pos
);
1241 big_statement_stack
= NULL
;
1242 big_expression_stack
= NULL
;
1243 switch_expr_stack
= NULL
;
1245 sm_debug("inline function: %s\n", cur_func
);
1248 start_function_definition(call
->fn
->symbol
);
1249 __split_stmt(base_type
->stmt
);
1250 __split_stmt(base_type
->inline_stmt
);
1251 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1252 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1254 free_expression_stack(&switch_expr_stack
);
1255 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1259 loop_num
= loop_num_bak
;
1260 final_pass
= final_pass_bak
;
1261 cur_func_sym
= cur_func_sym_bak
;
1262 cur_func
= cur_func_bak
;
1263 big_statement_stack
= big_statement_stack_bak
;
1264 big_expression_stack
= big_expression_stack_bak
;
1265 switch_expr_stack
= switch_expr_stack_bak
;
1267 restore_all_states();
1268 set_position(call
->pos
);
1270 __pass_to_client(call
, INLINE_FN_END
);
1273 static struct symbol_list
*inlines_called
;
1274 static void add_inline_function(struct symbol
*sym
)
1276 static struct symbol_list
*already_added
;
1279 FOR_EACH_PTR(already_added
, tmp
) {
1282 } END_FOR_EACH_PTR(tmp
);
1284 add_ptr_list(&already_added
, sym
);
1285 add_ptr_list(&inlines_called
, sym
);
1288 static void process_inlines(void)
1292 FOR_EACH_PTR(inlines_called
, tmp
) {
1293 split_function(tmp
);
1294 } END_FOR_EACH_PTR(tmp
);
1295 free_ptr_list(&inlines_called
);
1298 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1302 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1305 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1307 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1309 } END_FOR_EACH_PTR_REVERSE(sym
);
1314 static void split_inlines_in_scope(struct symbol
*sym
)
1316 struct symbol
*base
;
1317 struct symbol_list
*scope_list
;
1320 scope_list
= sym
->scope
->symbols
;
1321 stream
= sym
->pos
.stream
;
1323 /* find the last static symbol in the file */
1324 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1325 if (sym
->pos
.stream
!= stream
)
1327 if (sym
->type
!= SYM_NODE
)
1329 base
= get_base_type(sym
);
1332 if (base
->type
!= SYM_FN
)
1334 if (!base
->inline_stmt
)
1336 add_inline_function(sym
);
1337 } END_FOR_EACH_PTR_REVERSE(sym
);
1342 static void split_inlines(struct symbol_list
*sym_list
)
1346 sym
= get_last_scoped_symbol(sym_list
, 0);
1348 split_inlines_in_scope(sym
);
1349 sym
= get_last_scoped_symbol(sym_list
, 1);
1351 split_inlines_in_scope(sym
);
1354 static struct stree
*clone_estates_perm(struct stree
*orig
)
1356 struct stree
*ret
= NULL
;
1357 struct sm_state
*tmp
;
1359 FOR_EACH_SM(orig
, tmp
) {
1360 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1361 } END_FOR_EACH_SM(tmp
);
1366 static void split_functions(struct symbol_list
*sym_list
)
1371 FOR_EACH_PTR(sym_list
, sym
) {
1372 set_position(sym
->pos
);
1373 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1374 __pass_to_client(sym
, BASE_HOOK
);
1375 fake_global_assign(sym
);
1377 } END_FOR_EACH_PTR(sym
);
1378 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1381 FOR_EACH_PTR(sym_list
, sym
) {
1382 set_position(sym
->pos
);
1383 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1384 split_function(sym
);
1387 } END_FOR_EACH_PTR(sym
);
1388 split_inlines(sym_list
);
1389 __pass_to_client(sym_list
, END_FILE_HOOK
);
1392 void smatch(int argc
, char **argv
)
1395 struct string_list
*filelist
= NULL
;
1396 struct symbol_list
*sym_list
;
1399 printf("Usage: smatch [--debug] <filename.c>\n");
1402 sparse_initialize(argc
, argv
, &filelist
);
1403 set_valid_ptr_max();
1404 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1405 if (option_file_output
) {
1408 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1409 sm_outfd
= fopen(buf
, "w");
1411 printf("Error: Cannot open %s\n", base_file
);
1415 sym_list
= sparse_keep_tokens(base_file
);
1416 split_functions(sym_list
);
1417 } END_FOR_EACH_PTR_NOTAG(base_file
);