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 int outside_of_function(void)
73 return cur_func_sym
== NULL
;
76 const char *get_filename(void)
85 const char *get_base_file(void)
90 static void set_position(struct position pos
)
93 static int prev_stream
= -1;
95 if (pos
.stream
== 0 && pos
.line
== 0)
98 __smatch_lineno
= pos
.line
;
100 if (pos
.stream
== prev_stream
)
103 filename
= stream_name(pos
.stream
);
106 pathname
= getcwd(NULL
, 0);
108 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
109 full_filename
= malloc(len
);
110 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
112 full_filename
= alloc_string(filename
);
117 static int is_inline_func(struct expression
*expr
)
119 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
121 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
126 static int is_noreturn_func(struct expression
*expr
)
128 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
130 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
135 int inlinable(struct expression
*expr
)
139 if (__inline_fn
) /* don't nest */
142 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
144 if (is_no_inline_function(expr
->symbol
->ident
->name
))
146 sym
= get_base_type(expr
->symbol
);
147 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
148 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
152 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
153 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
160 void __process_post_op_stack(void)
162 struct expression
*expr
;
164 FOR_EACH_PTR(post_op_stack
, expr
) {
165 __pass_to_client(expr
, OP_HOOK
);
166 } END_FOR_EACH_PTR(expr
);
168 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
171 void __split_expr(struct expression
*expr
)
176 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
178 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
180 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
183 push_expression(&big_expression_stack
, expr
);
184 set_position(expr
->pos
);
185 __pass_to_client(expr
, EXPR_HOOK
);
187 switch (expr
->type
) {
190 __pass_to_client(expr
, DEREF_HOOK
);
191 __split_expr(expr
->unop
);
192 __pass_to_client(expr
, OP_HOOK
);
195 __split_expr(expr
->unop
);
196 push_expression(&post_op_stack
, expr
);
200 __split_stmt(expr
->statement
);
205 __pass_to_client(expr
, LOGIC_HOOK
);
206 __handle_logic(expr
);
209 __pass_to_client(expr
, BINOP_HOOK
);
211 __split_expr(expr
->left
);
212 __process_post_op_stack();
213 __split_expr(expr
->right
);
215 case EXPR_ASSIGNMENT
: {
216 struct expression
*tmp
;
221 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
224 if (__handle_condition_assigns(expr
))
226 /* foo = (x < 5 ? foo : 5); */
227 if (__handle_select_assigns(expr
))
229 /* foo = ({frob(); frob(); frob(); 1;}) */
230 if (__handle_expr_statement_assigns(expr
))
233 __split_expr(expr
->right
);
234 if (outside_of_function())
235 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
237 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
239 __fake_struct_member_assignments(expr
);
241 tmp
= strip_expr(expr
->right
);
242 if (tmp
->type
== EXPR_CALL
)
243 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
244 if (get_macro_name(tmp
->pos
) &&
245 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
246 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
247 __split_expr(expr
->left
);
251 __pass_to_client(expr
, DEREF_HOOK
);
252 __split_expr(expr
->deref
);
255 __split_expr(expr
->base
);
258 case EXPR_FORCE_CAST
:
259 __pass_to_client(expr
, CAST_HOOK
);
260 __split_expr(expr
->cast_expression
);
263 if (expr
->cast_expression
)
264 __pass_to_client(strip_parens(expr
->cast_expression
),
269 evaluate_expression(expr
);
271 case EXPR_CONDITIONAL
:
273 if (known_condition_true(expr
->conditional
)) {
274 __split_expr(expr
->cond_true
);
277 if (known_condition_false(expr
->conditional
)) {
278 __split_expr(expr
->cond_false
);
281 __pass_to_client(expr
, SELECT_HOOK
);
282 __split_whole_condition(expr
->conditional
);
283 __split_expr(expr
->cond_true
);
284 __push_true_states();
285 __use_false_states();
286 __split_expr(expr
->cond_false
);
287 __merge_true_states();
290 if (sym_name_is("__builtin_constant_p", expr
->fn
))
292 split_expr_list(expr
->args
);
293 __split_expr(expr
->fn
);
294 if (is_inline_func(expr
->fn
))
295 add_inline_function(expr
->fn
->symbol
);
296 if (inlinable(expr
->fn
))
298 __process_post_op_stack();
299 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
301 if (inlinable(expr
->fn
)) {
304 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
305 if (is_noreturn_func(expr
->fn
))
308 case EXPR_INITIALIZER
:
309 split_expr_list(expr
->expr_list
);
311 case EXPR_IDENTIFIER
:
312 __split_expr(expr
->ident_expression
);
315 __split_expr(expr
->idx_expression
);
318 __split_expr(expr
->init_expr
);
321 __pass_to_client(expr
, SYM_HOOK
);
324 __pass_to_client(expr
, STRING_HOOK
);
329 pop_expression(&big_expression_stack
);
332 static int is_forever_loop(struct statement
*stmt
)
334 struct expression
*expr
;
336 expr
= strip_expr(stmt
->iterator_pre_condition
);
338 expr
= stmt
->iterator_post_condition
;
340 /* this is a for(;;) loop... */
344 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
351 static char *get_loop_name(int num
)
355 snprintf(buf
, 255, "-loop%d", num
);
357 return alloc_sname(buf
);
361 * Pre Loops are while and for loops.
363 static void handle_pre_loop(struct statement
*stmt
)
365 int once_through
; /* we go through the loop at least once */
366 struct sm_state
*extra_sm
= NULL
;
369 struct stree
*stree
= NULL
;
370 struct sm_state
*sm
= NULL
;
372 loop_name
= get_loop_name(loop_num
);
375 __split_stmt(stmt
->iterator_pre_statement
);
377 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
383 __merge_gotos(loop_name
);
385 extra_sm
= __extra_handle_canonical_loops(stmt
, &stree
);
386 __in_pre_condition
++;
387 __pass_to_client(stmt
, PRELOOP_HOOK
);
388 __split_whole_condition(stmt
->iterator_pre_condition
);
389 __in_pre_condition
--;
390 FOR_EACH_SM(stree
, sm
) {
391 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
392 } END_FOR_EACH_SM(sm
);
395 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
397 if (option_assume_loops
)
400 __split_stmt(stmt
->iterator_statement
);
401 __warn_on_silly_pre_loops();
402 if (is_forever_loop(stmt
)) {
404 __save_gotos(loop_name
);
406 __push_fake_cur_stree();
407 __split_stmt(stmt
->iterator_post_statement
);
408 stree
= __pop_fake_cur_stree();
410 __discard_false_states();
413 if (!__path_is_null())
414 __merge_stree_into_cur(stree
);
418 unchanged
= __iterator_unchanged(extra_sm
);
419 __split_stmt(stmt
->iterator_post_statement
);
420 __save_gotos(loop_name
);
421 __in_pre_condition
++;
422 __split_whole_condition(stmt
->iterator_pre_condition
);
423 __in_pre_condition
--;
425 __merge_false_states();
427 __discard_false_states();
429 __merge_false_states();
431 if (extra_sm
&& unchanged
)
432 __extra_pre_loop_hook_after(extra_sm
,
433 stmt
->iterator_post_statement
,
434 stmt
->iterator_pre_condition
);
441 * Post loops are do {} while();
443 static void handle_post_loop(struct statement
*stmt
)
447 loop_name
= get_loop_name(loop_num
);
453 __merge_gotos(loop_name
);
454 __split_stmt(stmt
->iterator_statement
);
456 if (!is_zero(stmt
->iterator_post_condition
))
457 __save_gotos(loop_name
);
459 if (is_forever_loop(stmt
)) {
462 __split_whole_condition(stmt
->iterator_post_condition
);
463 __use_false_states();
469 static int empty_statement(struct statement
*stmt
)
473 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
478 static int last_stmt_on_same_line(void)
480 struct statement
*stmt
;
483 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
486 if (stmt
->pos
.line
== get_lineno())
489 } END_FOR_EACH_PTR_REVERSE(stmt
);
493 static void split_asm_constraints(struct expression_list
*expr_list
)
495 struct expression
*expr
;
498 FOR_EACH_PTR(expr_list
, expr
) {
500 case 0: /* identifier */
501 case 1: /* constraint */
504 case 2: /* expression */
509 } END_FOR_EACH_PTR(expr
);
512 static int is_case_val(struct statement
*stmt
, sval_t sval
)
516 if (stmt
->type
!= STMT_CASE
)
518 if (!stmt
->case_expression
) {
522 if (!get_value(stmt
->case_expression
, &case_sval
))
524 if (case_sval
.value
== sval
.value
)
529 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
531 struct statement
*tmp
;
533 __split_expr(stmt
->switch_expression
);
535 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
536 __save_switch_states(top_expression(switch_expr_stack
));
541 stmt
= stmt
->switch_statement
;
543 __push_scope_hooks();
544 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
545 __smatch_lineno
= tmp
->pos
.line
;
546 if (is_case_val(tmp
, sval
)) {
547 __merge_switches(top_expression(switch_expr_stack
),
548 stmt
->case_expression
);
549 __pass_case_to_client(top_expression(switch_expr_stack
),
550 stmt
->case_expression
);
552 if (__path_is_null())
555 if (__path_is_null()) {
559 } END_FOR_EACH_PTR(tmp
);
561 __call_scope_hooks();
562 if (!__pop_default())
563 __merge_switches(top_expression(switch_expr_stack
),
565 __discard_switches();
567 pop_expression(&switch_expr_stack
);
570 static int taking_too_long(void)
574 ms
= ms_since(&fn_start_time
);
575 if (ms
> 1000 * 60 * 5) /* five minutes */
580 static int is_last_stmt(struct statement
*cur_stmt
)
582 struct symbol
*fn
= get_base_type(cur_func_sym
);
583 struct statement
*stmt
;
589 stmt
= fn
->inline_stmt
;
590 if (!stmt
|| stmt
->type
!= STMT_COMPOUND
)
592 stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
593 if (stmt
&& stmt
->type
== STMT_LABEL
)
594 stmt
= stmt
->label_statement
;
595 if (stmt
== cur_stmt
)
600 static void handle_backward_goto(struct statement
*goto_stmt
)
602 const char *goto_name
, *label_name
;
603 struct statement
*func_stmt
;
604 struct symbol
*base_type
= get_base_type(cur_func_sym
);
605 struct statement
*tmp
;
610 if (last_goto_statement_handled
)
612 last_goto_statement_handled
= 1;
614 if (!goto_stmt
->goto_label
||
615 goto_stmt
->goto_label
->type
!= SYM_LABEL
||
616 !goto_stmt
->goto_label
->ident
)
618 goto_name
= goto_stmt
->goto_label
->ident
->name
;
620 func_stmt
= base_type
->stmt
;
622 func_stmt
= base_type
->inline_stmt
;
625 if (func_stmt
->type
!= STMT_COMPOUND
)
628 FOR_EACH_PTR(func_stmt
->stmts
, tmp
) {
630 if (tmp
->type
!= STMT_LABEL
)
632 if (!tmp
->label_identifier
||
633 tmp
->label_identifier
->type
!= SYM_LABEL
||
634 !tmp
->label_identifier
->ident
)
636 label_name
= tmp
->label_identifier
->ident
->name
;
637 if (strcmp(goto_name
, label_name
) != 0)
642 } END_FOR_EACH_PTR(tmp
);
645 void __split_stmt(struct statement
*stmt
)
652 if (__bail_on_rest_of_function
|| out_of_memory() || taking_too_long()) {
653 static char *printed
= NULL
;
655 __bail_on_rest_of_function
= 1;
656 if (printed
!= cur_func
)
657 sm_msg("Function too hairy. Giving up.");
658 final_pass
= 0; /* turn off sm_msg() from here */
663 add_ptr_list(&big_statement_stack
, stmt
);
664 free_expression_stack(&big_expression_stack
);
665 set_position(stmt
->pos
);
666 __pass_to_client(stmt
, STMT_HOOK
);
668 switch (stmt
->type
) {
669 case STMT_DECLARATION
:
670 split_declaration(stmt
->declaration
);
673 __split_expr(stmt
->ret_value
);
674 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
675 __process_post_op_stack();
678 case STMT_EXPRESSION
:
679 __split_expr(stmt
->expression
);
681 case STMT_COMPOUND
: {
682 struct statement
*tmp
;
684 __push_scope_hooks();
685 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
687 } END_FOR_EACH_PTR(tmp
);
688 __call_scope_hooks();
692 if (known_condition_true(stmt
->if_conditional
)) {
693 __split_stmt(stmt
->if_true
);
696 if (known_condition_false(stmt
->if_conditional
)) {
697 __split_stmt(stmt
->if_false
);
700 if (option_known_conditions
&&
701 implied_condition_true(stmt
->if_conditional
)) {
702 sm_info("this condition is true.");
703 __split_stmt(stmt
->if_true
);
706 if (option_known_conditions
&&
707 implied_condition_false(stmt
->if_conditional
)) {
708 sm_info("this condition is false.");
709 __split_stmt(stmt
->if_false
);
712 __split_whole_condition(stmt
->if_conditional
);
713 __split_stmt(stmt
->if_true
);
714 if (empty_statement(stmt
->if_true
) &&
715 last_stmt_on_same_line() &&
716 !get_macro_name(stmt
->if_true
->pos
))
717 sm_msg("warn: if();");
718 __push_true_states();
719 __use_false_states();
720 __split_stmt(stmt
->if_false
);
721 __merge_true_states();
724 if (stmt
->iterator_pre_condition
)
725 handle_pre_loop(stmt
);
726 else if (stmt
->iterator_post_condition
)
727 handle_post_loop(stmt
);
729 // these are for(;;) type loops.
730 handle_pre_loop(stmt
);
734 if (get_value(stmt
->switch_expression
, &sval
)) {
735 split_known_switch(stmt
, sval
);
738 __split_expr(stmt
->switch_expression
);
739 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
740 __save_switch_states(top_expression(switch_expr_stack
));
744 __split_stmt(stmt
->switch_statement
);
745 if (!__pop_default())
746 __merge_switches(top_expression(switch_expr_stack
),
748 __discard_switches();
750 pop_expression(&switch_expr_stack
);
753 __merge_switches(top_expression(switch_expr_stack
),
754 stmt
->case_expression
);
755 __pass_case_to_client(top_expression(switch_expr_stack
),
756 stmt
->case_expression
);
757 if (!stmt
->case_expression
)
759 __split_expr(stmt
->case_expression
);
760 __split_expr(stmt
->case_to
);
761 __split_stmt(stmt
->case_statement
);
764 if (stmt
->label_identifier
&&
765 stmt
->label_identifier
->type
== SYM_LABEL
&&
766 stmt
->label_identifier
->ident
) {
767 loop_count
|= 0x80000000;
768 __merge_gotos(stmt
->label_identifier
->ident
->name
);
770 __split_stmt(stmt
->label_statement
);
773 __split_expr(stmt
->goto_expression
);
774 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
775 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
777 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
779 __process_continues();
781 } else if (stmt
->goto_label
&&
782 stmt
->goto_label
->type
== SYM_LABEL
&&
783 stmt
->goto_label
->ident
) {
784 __save_gotos(stmt
->goto_label
->ident
->name
);
787 if (is_last_stmt(stmt
))
788 handle_backward_goto(stmt
);
793 __pass_to_client(stmt
, ASM_HOOK
);
794 __split_expr(stmt
->asm_string
);
795 split_asm_constraints(stmt
->asm_outputs
);
796 split_asm_constraints(stmt
->asm_inputs
);
797 split_asm_constraints(stmt
->asm_clobbers
);
802 __split_expr(stmt
->range_expression
);
803 __split_expr(stmt
->range_low
);
804 __split_expr(stmt
->range_high
);
807 __pass_to_client(stmt
, STMT_HOOK_AFTER
);
809 __process_post_op_stack();
812 static void split_expr_list(struct expression_list
*expr_list
)
814 struct expression
*expr
;
816 FOR_EACH_PTR(expr_list
, expr
) {
818 __process_post_op_stack();
819 } END_FOR_EACH_PTR(expr
);
822 static void split_sym(struct symbol
*sym
)
826 if (!(sym
->namespace & NS_SYMBOL
))
829 __split_stmt(sym
->stmt
);
830 __split_expr(sym
->array_size
);
831 split_symlist(sym
->arguments
);
832 split_symlist(sym
->symbol_list
);
833 __split_stmt(sym
->inline_stmt
);
834 split_symlist(sym
->inline_symbol_list
);
837 static void split_symlist(struct symbol_list
*sym_list
)
841 FOR_EACH_PTR(sym_list
, sym
) {
843 } END_FOR_EACH_PTR(sym
);
846 typedef void (fake_cb
)(struct expression
*expr
);
848 static int member_to_number(struct expression
*expr
, struct ident
*member
)
850 struct symbol
*type
, *tmp
;
858 type
= get_type(expr
);
859 if (!type
|| type
->type
!= SYM_STRUCT
)
863 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
867 if (strcmp(name
, tmp
->ident
->name
) == 0)
869 } END_FOR_EACH_PTR(tmp
);
873 static struct ident
*number_to_member(struct expression
*expr
, int num
)
875 struct symbol
*type
, *member
;
878 type
= get_type(expr
);
879 if (!type
|| type
->type
!= SYM_STRUCT
)
882 FOR_EACH_PTR(type
->symbol_list
, member
) {
884 return member
->ident
;
886 } END_FOR_EACH_PTR(member
);
890 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
897 static struct member_set
*alloc_member_set(struct symbol
*type
)
899 struct member_set
*member_set
;
900 struct symbol
*member
;
904 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
905 member_set
= malloc(member_count
* sizeof(*member_set
));
907 FOR_EACH_PTR(type
->symbol_list
, member
) {
908 member_set
[member_idx
].ident
= member
->ident
;
909 member_set
[member_idx
].set
= 0;
911 } END_FOR_EACH_PTR(member
);
916 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
918 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
921 for (i
= 0; i
< member_count
; i
++) {
922 if (member_set
[i
].ident
== ident
) {
923 member_set
[i
].set
= 1;
927 // crap. this is buggy.
928 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
931 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
933 struct expression
*deref
, *assign
;
934 struct symbol
*member
, *member_type
;
938 FOR_EACH_PTR(type
->symbol_list
, member
) {
939 if (!member
->ident
|| member_set
[member_idx
].set
) {
943 member_type
= get_real_base_type(member
);
944 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
948 /* TODO: this should be handled recursively and not ignored */
949 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
953 deref
= member_expression(symbol
, '.', member
->ident
);
954 assign
= assign_expression(deref
, zero_expr());
955 __split_expr(assign
);
957 } END_FOR_EACH_PTR(member
);
961 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
963 struct expression
*deref
, *assign
, *tmp
;
964 struct symbol
*struct_type
, *type
;
965 struct ident
*member
;
967 struct member_set
*member_set
;
969 struct_type
= get_type(symbol
);
971 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
974 member_set
= alloc_member_set(struct_type
);
977 FOR_EACH_PTR(members
, tmp
) {
978 member
= number_to_member(symbol
, member_idx
);
979 while (tmp
->type
== EXPR_IDENTIFIER
) {
980 member
= tmp
->expr_ident
;
981 member_idx
= member_to_number(symbol
, member
);
982 tmp
= tmp
->ident_expression
;
984 mark_member_as_set(struct_type
, member_set
, member
);
986 deref
= member_expression(symbol
, '.', member
);
987 if (tmp
->type
== EXPR_INITIALIZER
) {
988 type
= get_type(deref
);
989 if (type
&& type
->type
== SYM_ARRAY
)
990 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
992 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
994 assign
= assign_expression(deref
, tmp
);
997 } END_FOR_EACH_PTR(tmp
);
999 set_unset_to_zero(symbol
, struct_type
, member_set
);
1002 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1004 fake_member_assigns_helper(symbol_expression(sym
),
1005 sym
->initializer
->expr_list
, fake_cb
);
1008 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
1010 struct expression
*offset
, *binop
, *assign
, *tmp
;
1011 struct symbol
*type
;
1015 FOR_EACH_PTR(expr_list
, tmp
) {
1016 if (tmp
->type
== EXPR_INDEX
) {
1017 if (tmp
->idx_from
!= tmp
->idx_to
)
1019 idx
= tmp
->idx_from
;
1020 if (!tmp
->idx_expression
)
1022 tmp
= tmp
->idx_expression
;
1024 offset
= value_expr(idx
);
1025 binop
= array_element_expression(array
, offset
);
1026 if (tmp
->type
== EXPR_INITIALIZER
) {
1027 type
= get_type(binop
);
1028 if (type
&& type
->type
== SYM_ARRAY
)
1029 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1031 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1033 assign
= assign_expression(binop
, tmp
);
1038 } END_FOR_EACH_PTR(tmp
);
1041 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1043 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1046 static void fake_assign_expr(struct symbol
*sym
)
1048 struct expression
*assign
, *symbol
;
1050 symbol
= symbol_expression(sym
);
1051 assign
= assign_expression(symbol
, sym
->initializer
);
1052 __split_expr(assign
);
1055 static void call_split_expr(struct expression
*expr
)
1060 static void do_initializer_stuff(struct symbol
*sym
)
1062 if (!sym
->initializer
)
1065 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1066 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1067 fake_element_assigns(sym
, call_split_expr
);
1069 fake_member_assigns(sym
, call_split_expr
);
1071 fake_assign_expr(sym
);
1075 static void split_declaration(struct symbol_list
*sym_list
)
1079 FOR_EACH_PTR(sym_list
, sym
) {
1080 __pass_to_client(sym
, DECLARATION_HOOK
);
1081 do_initializer_stuff(sym
);
1083 } END_FOR_EACH_PTR(sym
);
1086 static void call_global_assign_hooks(struct expression
*assign
)
1088 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1091 static void fake_global_assign(struct symbol
*sym
)
1093 struct expression
*assign
, *symbol
;
1095 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1096 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1097 fake_element_assigns(sym
, call_global_assign_hooks
);
1098 } else if (sym
->initializer
) {
1099 symbol
= symbol_expression(sym
);
1100 assign
= assign_expression(symbol
, sym
->initializer
);
1101 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1103 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1105 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1106 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1107 fake_member_assigns(sym
, call_global_assign_hooks
);
1108 } else if (sym
->initializer
) {
1109 symbol
= symbol_expression(sym
);
1110 assign
= assign_expression(symbol
, sym
->initializer
);
1111 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1113 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1116 symbol
= symbol_expression(sym
);
1117 if (sym
->initializer
)
1118 assign
= assign_expression(symbol
, sym
->initializer
);
1120 assign
= assign_expression(symbol
, zero_expr());
1121 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1125 static void start_function_definition(struct symbol
*sym
)
1127 __in_function_def
= 1;
1128 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1129 __in_function_def
= 0;
1130 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1134 static void split_function(struct symbol
*sym
)
1136 struct symbol
*base_type
= get_base_type(sym
);
1138 if (!base_type
->stmt
&& !base_type
->inline_stmt
)
1141 gettimeofday(&fn_start_time
, NULL
);
1144 cur_func
= sym
->ident
->name
;
1145 __smatch_lineno
= sym
->pos
.line
;
1147 last_goto_statement_handled
= 0;
1148 sm_debug("new function: %s\n", cur_func
);
1150 if (option_two_passes
) {
1154 start_function_definition(sym
);
1155 __split_stmt(base_type
->stmt
);
1156 __split_stmt(base_type
->inline_stmt
);
1162 start_function_definition(sym
);
1163 __split_stmt(base_type
->stmt
);
1164 __split_stmt(base_type
->inline_stmt
);
1165 __pass_to_client(sym
, END_FUNC_HOOK
);
1166 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1168 cur_func_sym
= NULL
;
1171 free_data_info_allocs();
1172 free_expression_stack(&switch_expr_stack
);
1173 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1174 __bail_on_rest_of_function
= 0;
1177 static void parse_inline(struct expression
*call
)
1179 struct symbol
*base_type
;
1180 int loop_num_bak
= loop_num
;
1181 int final_pass_bak
= final_pass
;
1182 char *cur_func_bak
= cur_func
;
1183 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1184 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1185 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1186 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1188 __pass_to_client(call
, INLINE_FN_START
);
1189 final_pass
= 0; /* don't print anything */
1192 base_type
= get_base_type(call
->fn
->symbol
);
1193 cur_func_sym
= call
->fn
->symbol
;
1194 if (call
->fn
->symbol
->ident
)
1195 cur_func
= call
->fn
->symbol
->ident
->name
;
1198 set_position(call
->fn
->symbol
->pos
);
1201 big_statement_stack
= NULL
;
1202 big_expression_stack
= NULL
;
1203 switch_expr_stack
= NULL
;
1205 sm_debug("inline function: %s\n", cur_func
);
1208 start_function_definition(call
->fn
->symbol
);
1209 __split_stmt(base_type
->stmt
);
1210 __split_stmt(base_type
->inline_stmt
);
1211 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1212 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1214 free_expression_stack(&switch_expr_stack
);
1215 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1219 loop_num
= loop_num_bak
;
1220 final_pass
= final_pass_bak
;
1221 cur_func_sym
= cur_func_sym_bak
;
1222 cur_func
= cur_func_bak
;
1223 big_statement_stack
= big_statement_stack_bak
;
1224 big_expression_stack
= big_expression_stack_bak
;
1225 switch_expr_stack
= switch_expr_stack_bak
;
1227 restore_all_states();
1228 set_position(call
->pos
);
1230 __pass_to_client(call
, INLINE_FN_END
);
1233 static struct symbol_list
*inlines_called
;
1234 static void add_inline_function(struct symbol
*sym
)
1236 static struct symbol_list
*already_added
;
1239 FOR_EACH_PTR(already_added
, tmp
) {
1242 } END_FOR_EACH_PTR(tmp
);
1244 add_ptr_list(&already_added
, sym
);
1245 add_ptr_list(&inlines_called
, sym
);
1248 static void process_inlines(void)
1252 FOR_EACH_PTR(inlines_called
, tmp
) {
1253 split_function(tmp
);
1254 } END_FOR_EACH_PTR(tmp
);
1255 free_ptr_list(&inlines_called
);
1258 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1262 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1265 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1267 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1269 } END_FOR_EACH_PTR_REVERSE(sym
);
1274 static void split_inlines_in_scope(struct symbol
*sym
)
1276 struct symbol
*base
;
1277 struct symbol_list
*scope_list
;
1280 scope_list
= sym
->scope
->symbols
;
1281 stream
= sym
->pos
.stream
;
1283 /* find the last static symbol in the file */
1284 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1285 if (sym
->pos
.stream
!= stream
)
1287 if (sym
->type
!= SYM_NODE
)
1289 base
= get_base_type(sym
);
1292 if (base
->type
!= SYM_FN
)
1294 if (!base
->inline_stmt
)
1296 add_inline_function(sym
);
1297 } END_FOR_EACH_PTR_REVERSE(sym
);
1302 static void split_inlines(struct symbol_list
*sym_list
)
1306 sym
= get_last_scoped_symbol(sym_list
, 0);
1308 split_inlines_in_scope(sym
);
1309 sym
= get_last_scoped_symbol(sym_list
, 1);
1311 split_inlines_in_scope(sym
);
1314 static struct stree
*clone_estates_perm(struct stree
*orig
)
1316 struct stree
*ret
= NULL
;
1317 struct sm_state
*tmp
;
1319 FOR_EACH_SM(orig
, tmp
) {
1320 set_state_stree_perm(&ret
, tmp
->owner
, tmp
->name
, tmp
->sym
, clone_estate_perm(tmp
->state
));
1321 } END_FOR_EACH_SM(tmp
);
1326 static void split_functions(struct symbol_list
*sym_list
)
1331 FOR_EACH_PTR(sym_list
, sym
) {
1332 set_position(sym
->pos
);
1333 if (sym
->type
!= SYM_NODE
|| get_base_type(sym
)->type
!= SYM_FN
) {
1334 __pass_to_client(sym
, BASE_HOOK
);
1335 fake_global_assign(sym
);
1337 } END_FOR_EACH_PTR(sym
);
1338 global_states
= clone_estates_perm(get_all_states_stree(SMATCH_EXTRA
));
1341 FOR_EACH_PTR(sym_list
, sym
) {
1342 set_position(sym
->pos
);
1343 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1344 split_function(sym
);
1347 } END_FOR_EACH_PTR(sym
);
1348 split_inlines(sym_list
);
1349 __pass_to_client(sym_list
, END_FILE_HOOK
);
1352 void smatch(int argc
, char **argv
)
1355 struct string_list
*filelist
= NULL
;
1356 struct symbol_list
*sym_list
;
1359 printf("Usage: smatch [--debug] <filename.c>\n");
1362 sparse_initialize(argc
, argv
, &filelist
);
1363 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1364 if (option_file_output
) {
1367 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1368 sm_outfd
= fopen(buf
, "w");
1370 printf("Error: Cannot open %s\n", base_file
);
1374 sym_list
= sparse_keep_tokens(base_file
);
1375 split_functions(sym_list
);
1376 } END_FOR_EACH_PTR_NOTAG(base_file
);