4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h"
21 struct expression
*__inline_fn
;
23 static int __smatch_lineno
= 0;
25 static char *base_file
;
26 static const char *filename
;
27 static char *pathname
;
28 static char *full_filename
;
29 static char *cur_func
;
30 static int loop_count
;
31 int __expr_stmt_count
;
32 static struct expression_list
*switch_expr_stack
= NULL
;
33 static struct expression_list
*post_op_stack
= NULL
;
35 struct expression_list
*big_expression_stack
;
36 struct statement_list
*big_statement_stack
;
37 int __in_pre_condition
= 0;
38 int __bail_on_rest_of_function
= 0;
39 char *get_function(void) { return cur_func
; }
40 int get_lineno(void) { return __smatch_lineno
; }
41 int inside_loop(void) { return !!loop_count
; }
42 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
43 int in_expression_statement(void) { return !!__expr_stmt_count
; }
45 static void split_symlist(struct symbol_list
*sym_list
);
46 static void split_declaration(struct symbol_list
*sym_list
);
47 static void split_expr_list(struct expression_list
*expr_list
);
48 static void add_inline_function(struct symbol
*sym
);
49 static void parse_inline(struct expression
*expr
);
51 int option_assume_loops
= 0;
52 int option_known_conditions
= 0;
53 int option_two_passes
= 0;
54 struct symbol
*cur_func_sym
= NULL
;
56 const char *get_filename(void)
65 const char *get_base_file(void)
70 static void set_position(struct position pos
)
73 static int prev_stream
= -1;
75 __smatch_lineno
= pos
.line
;
77 if (pos
.stream
== prev_stream
)
80 filename
= stream_name(pos
.stream
);
83 pathname
= getcwd(NULL
, 0);
85 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
86 full_filename
= malloc(len
);
87 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
89 full_filename
= alloc_string(filename
);
94 static int is_inline_func(struct expression
*expr
)
96 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
98 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
103 static int is_noreturn_func(struct expression
*expr
)
105 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
107 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
112 int inlinable(struct expression
*expr
)
116 if (__inline_fn
) /* don't nest */
119 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
121 sym
= get_base_type(expr
->symbol
);
122 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
123 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
127 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
128 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
135 void __process_post_op_stack(void)
137 struct expression
*expr
;
139 FOR_EACH_PTR(post_op_stack
, expr
) {
140 __pass_to_client(expr
, OP_HOOK
);
141 } END_FOR_EACH_PTR(expr
);
143 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
146 void __split_expr(struct expression
*expr
)
151 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
153 push_expression(&big_expression_stack
, expr
);
154 set_position(expr
->pos
);
155 __pass_to_client(expr
, EXPR_HOOK
);
157 switch (expr
->type
) {
160 __pass_to_client(expr
, DEREF_HOOK
);
161 __split_expr(expr
->unop
);
162 __pass_to_client(expr
, OP_HOOK
);
165 __split_expr(expr
->unop
);
166 push_expression(&post_op_stack
, expr
);
170 __split_stmt(expr
->statement
);
175 __pass_to_client(expr
, LOGIC_HOOK
);
176 __handle_logic(expr
);
179 __pass_to_client(expr
, BINOP_HOOK
);
181 __split_expr(expr
->left
);
182 __process_post_op_stack();
183 __split_expr(expr
->right
);
185 case EXPR_ASSIGNMENT
: {
186 struct expression
*tmp
;
191 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
194 if (__handle_condition_assigns(expr
))
196 /* foo = (x < 5 ? foo : 5); */
197 if (__handle_select_assigns(expr
))
199 /* foo = ({frob(); frob(); frob(); 1;}) */
200 if (__handle_expr_statement_assigns(expr
))
203 __split_expr(expr
->right
);
204 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
205 tmp
= strip_expr(expr
->right
);
206 if (tmp
->type
== EXPR_CALL
)
207 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
208 if (get_macro_name(tmp
->pos
) &&
209 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
210 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
211 __split_expr(expr
->left
);
215 __pass_to_client(expr
, DEREF_HOOK
);
216 __split_expr(expr
->deref
);
219 __split_expr(expr
->base
);
222 case EXPR_FORCE_CAST
:
223 __pass_to_client(expr
, CAST_HOOK
);
224 __split_expr(expr
->cast_expression
);
227 if (expr
->cast_expression
)
228 __pass_to_client(strip_parens(expr
->cast_expression
),
233 evaluate_expression(expr
);
235 case EXPR_CONDITIONAL
:
237 if (known_condition_true(expr
->conditional
)) {
238 __split_expr(expr
->cond_true
);
241 if (known_condition_false(expr
->conditional
)) {
242 __split_expr(expr
->cond_false
);
245 __pass_to_client(expr
, SELECT_HOOK
);
246 __split_whole_condition(expr
->conditional
);
247 __split_expr(expr
->cond_true
);
248 __push_true_states();
249 __use_false_states();
250 __split_expr(expr
->cond_false
);
251 __merge_true_states();
254 if (sym_name_is("__builtin_constant_p", expr
->fn
))
256 split_expr_list(expr
->args
);
257 __split_expr(expr
->fn
);
258 if (is_inline_func(expr
->fn
))
259 add_inline_function(expr
->fn
->symbol
);
260 if (inlinable(expr
->fn
))
262 __process_post_op_stack();
263 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
265 if (inlinable(expr
->fn
)) {
268 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
269 if (is_noreturn_func(expr
->fn
))
272 case EXPR_INITIALIZER
:
273 split_expr_list(expr
->expr_list
);
275 case EXPR_IDENTIFIER
:
276 __split_expr(expr
->ident_expression
);
279 __split_expr(expr
->idx_expression
);
282 __split_expr(expr
->init_expr
);
285 __pass_to_client(expr
, SYM_HOOK
);
288 __pass_to_client(expr
, STRING_HOOK
);
293 pop_expression(&big_expression_stack
);
296 static int is_forever_loop(struct statement
*stmt
)
298 struct expression
*expr
;
300 expr
= strip_expr(stmt
->iterator_pre_condition
);
302 expr
= stmt
->iterator_post_condition
;
304 /* this is a for(;;) loop... */
308 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
315 static char *get_loop_name(int num
)
319 snprintf(buf
, 255, "-loop%d", num
);
321 return alloc_sname(buf
);
325 * Pre Loops are while and for loops.
327 static void handle_pre_loop(struct statement
*stmt
)
329 int once_through
; /* we go through the loop at least once */
330 struct sm_state
*extra_sm
= NULL
;
333 struct state_list
*slist
= NULL
;
334 struct sm_state
*sm
= NULL
;
336 loop_name
= get_loop_name(loop_num
);
339 __split_stmt(stmt
->iterator_pre_statement
);
341 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
347 __merge_gotos(loop_name
);
349 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
350 __in_pre_condition
++;
351 __pass_to_client(stmt
, PRELOOP_HOOK
);
352 __split_whole_condition(stmt
->iterator_pre_condition
);
353 __in_pre_condition
--;
354 FOR_EACH_PTR(slist
, sm
) {
355 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
356 } END_FOR_EACH_PTR(sm
);
359 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
361 if (option_assume_loops
)
364 __split_stmt(stmt
->iterator_statement
);
365 __warn_on_silly_pre_loops();
366 if (is_forever_loop(stmt
)) {
367 struct state_list
*slist
;
369 __save_gotos(loop_name
);
371 __push_fake_cur_slist();
372 __split_stmt(stmt
->iterator_post_statement
);
373 slist
= __pop_fake_cur_slist();
375 __discard_continues();
376 __discard_false_states();
379 if (!__path_is_null())
380 __merge_slist_into_cur(slist
);
384 unchanged
= __iterator_unchanged(extra_sm
);
385 __split_stmt(stmt
->iterator_post_statement
);
386 __save_gotos(loop_name
);
387 __split_whole_condition(stmt
->iterator_pre_condition
);
389 __merge_false_states();
391 __discard_false_states();
393 __merge_false_states();
395 if (extra_sm
&& unchanged
)
396 __extra_pre_loop_hook_after(extra_sm
,
397 stmt
->iterator_post_statement
,
398 stmt
->iterator_pre_condition
);
405 * Post loops are do {} while();
407 static void handle_post_loop(struct statement
*stmt
)
411 loop_name
= get_loop_name(loop_num
);
417 __merge_gotos(loop_name
);
418 __split_stmt(stmt
->iterator_statement
);
420 if (!is_zero(stmt
->iterator_post_condition
))
421 __save_gotos(loop_name
);
423 if (is_forever_loop(stmt
)) {
426 __split_whole_condition(stmt
->iterator_post_condition
);
427 __use_false_states();
433 static int empty_statement(struct statement
*stmt
)
437 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
442 static int last_stmt_on_same_line()
444 struct statement
*stmt
;
447 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
450 if (stmt
->pos
.line
== get_lineno())
453 } END_FOR_EACH_PTR_REVERSE(stmt
);
457 static struct statement
*last_stmt
;
458 static int is_last_stmt(struct statement
*stmt
)
460 if (stmt
== last_stmt
)
465 static void print_unreached_initializers(struct symbol_list
*sym_list
)
469 FOR_EACH_PTR(sym_list
, sym
) {
470 if (sym
->initializer
)
471 sm_msg("info: '%s' is not actually initialized (unreached code).",
472 (sym
->ident
? sym
->ident
->name
: "this variable"));
473 } END_FOR_EACH_PTR(sym
);
476 static void print_unreached(struct statement
*stmt
)
478 static int print
= 1;
480 if (!__path_is_null()) {
487 switch (stmt
->type
) {
488 case STMT_COMPOUND
: /* after a switch before a case stmt */
493 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
494 print_unreached_initializers(stmt
->declaration
);
496 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
497 if (is_last_stmt(stmt
))
507 if (!option_spammy
&& empty_statement(stmt
))
509 sm_msg("info: ignoring unreachable code.");
513 static void split_asm_constraints(struct expression_list
*expr_list
)
515 struct expression
*expr
;
518 FOR_EACH_PTR(expr_list
, expr
) {
520 case 0: /* identifier */
521 case 1: /* constraint */
524 case 2: /* expression */
529 } END_FOR_EACH_PTR(expr
);
532 static int is_case_val(struct statement
*stmt
, sval_t sval
)
536 if (stmt
->type
!= STMT_CASE
)
538 if (!stmt
->case_expression
) {
542 if (!get_value(stmt
->case_expression
, &case_sval
))
544 if (case_sval
.value
== sval
.value
)
549 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
551 struct statement
*tmp
;
553 __split_expr(stmt
->switch_expression
);
555 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
556 __save_switch_states(top_expression(switch_expr_stack
));
561 stmt
= stmt
->switch_statement
;
564 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
566 __push_scope_hooks();
567 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
568 __smatch_lineno
= tmp
->pos
.line
;
569 if (is_case_val(tmp
, sval
)) {
570 __merge_switches(top_expression(switch_expr_stack
),
571 stmt
->case_expression
);
572 __pass_case_to_client(top_expression(switch_expr_stack
),
573 stmt
->case_expression
);
575 if (__path_is_null())
578 if (__path_is_null()) {
582 } END_FOR_EACH_PTR(tmp
);
584 __call_scope_hooks();
585 if (!__pop_default())
586 __merge_switches(top_expression(switch_expr_stack
),
588 __discard_switches();
590 pop_expression(&switch_expr_stack
);
593 void __split_stmt(struct statement
*stmt
)
600 if (out_of_memory() || __bail_on_rest_of_function
) {
601 static char *printed
= NULL
;
603 if (printed
!= cur_func
)
604 sm_msg("Function too hairy. Giving up.");
605 final_pass
= 0; /* turn off sm_msg() from here */
610 add_ptr_list(&big_statement_stack
, stmt
);
611 free_expression_stack(&big_expression_stack
);
612 set_position(stmt
->pos
);
613 print_unreached(stmt
);
614 __pass_to_client(stmt
, STMT_HOOK
);
616 switch (stmt
->type
) {
617 case STMT_DECLARATION
:
618 split_declaration(stmt
->declaration
);
621 __split_expr(stmt
->ret_value
);
622 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
625 case STMT_EXPRESSION
:
626 __split_expr(stmt
->expression
);
628 case STMT_COMPOUND
: {
629 struct statement
*tmp
;
632 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
633 __push_scope_hooks();
634 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
636 } END_FOR_EACH_PTR(tmp
);
637 __call_scope_hooks();
641 if (known_condition_true(stmt
->if_conditional
)) {
642 __split_stmt(stmt
->if_true
);
645 if (known_condition_false(stmt
->if_conditional
)) {
646 __split_stmt(stmt
->if_false
);
649 if (option_known_conditions
&&
650 implied_condition_true(stmt
->if_conditional
)) {
651 sm_info("this condition is true.");
652 __split_stmt(stmt
->if_true
);
655 if (option_known_conditions
&&
656 implied_condition_false(stmt
->if_conditional
)) {
657 sm_info("this condition is false.");
658 __split_stmt(stmt
->if_false
);
661 __split_whole_condition(stmt
->if_conditional
);
662 __split_stmt(stmt
->if_true
);
663 if (empty_statement(stmt
->if_true
) &&
664 last_stmt_on_same_line() &&
665 !get_macro_name(stmt
->if_true
->pos
))
666 sm_msg("warn: if();");
667 __push_true_states();
668 __use_false_states();
669 __split_stmt(stmt
->if_false
);
670 __merge_true_states();
673 if (stmt
->iterator_pre_condition
)
674 handle_pre_loop(stmt
);
675 else if (stmt
->iterator_post_condition
)
676 handle_post_loop(stmt
);
678 // these are for(;;) type loops.
679 handle_pre_loop(stmt
);
683 if (get_value(stmt
->switch_expression
, &sval
)) {
684 split_known_switch(stmt
, sval
);
687 __split_expr(stmt
->switch_expression
);
688 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
689 __save_switch_states(top_expression(switch_expr_stack
));
693 __split_stmt(stmt
->switch_statement
);
694 if (!__pop_default())
695 __merge_switches(top_expression(switch_expr_stack
),
697 __discard_switches();
699 pop_expression(&switch_expr_stack
);
702 __merge_switches(top_expression(switch_expr_stack
),
703 stmt
->case_expression
);
704 __pass_case_to_client(top_expression(switch_expr_stack
),
705 stmt
->case_expression
);
706 if (!stmt
->case_expression
)
708 __split_expr(stmt
->case_expression
);
709 __split_expr(stmt
->case_to
);
710 __split_stmt(stmt
->case_statement
);
713 if (stmt
->label_identifier
&&
714 stmt
->label_identifier
->type
== SYM_LABEL
&&
715 stmt
->label_identifier
->ident
) {
716 loop_count
= 1000000;
717 __merge_gotos(stmt
->label_identifier
->ident
->name
);
719 __split_stmt(stmt
->label_statement
);
722 __split_expr(stmt
->goto_expression
);
723 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
724 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
726 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
728 __process_continues();
730 } else if (stmt
->goto_label
&&
731 stmt
->goto_label
->type
== SYM_LABEL
&&
732 stmt
->goto_label
->ident
) {
733 __save_gotos(stmt
->goto_label
->ident
->name
);
740 __pass_to_client(stmt
, ASM_HOOK
);
741 __split_expr(stmt
->asm_string
);
742 split_asm_constraints(stmt
->asm_outputs
);
743 split_asm_constraints(stmt
->asm_inputs
);
744 split_asm_constraints(stmt
->asm_clobbers
);
749 __split_expr(stmt
->range_expression
);
750 __split_expr(stmt
->range_low
);
751 __split_expr(stmt
->range_high
);
755 __process_post_op_stack();
758 static void split_expr_list(struct expression_list
*expr_list
)
760 struct expression
*expr
;
762 FOR_EACH_PTR(expr_list
, expr
) {
764 __process_post_op_stack();
765 } END_FOR_EACH_PTR(expr
);
768 static void split_sym(struct symbol
*sym
)
772 if (!(sym
->namespace & NS_SYMBOL
))
775 __split_stmt(sym
->stmt
);
776 __split_expr(sym
->array_size
);
777 split_symlist(sym
->arguments
);
778 split_symlist(sym
->symbol_list
);
779 __split_stmt(sym
->inline_stmt
);
780 split_symlist(sym
->inline_symbol_list
);
783 static void split_symlist(struct symbol_list
*sym_list
)
787 FOR_EACH_PTR(sym_list
, sym
) {
789 } END_FOR_EACH_PTR(sym
);
792 static void fake_member_assigns(struct symbol
*sym
)
794 struct expression
*symbol
, *deref
, *assign
, *tmp
;
796 symbol
= symbol_expression(sym
);
797 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
798 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
800 deref
= member_expression(symbol
, '.', tmp
->expr_ident
);
801 assign
= assign_expression(deref
, tmp
->ident_expression
);
802 __split_expr(assign
);
803 } END_FOR_EACH_PTR(tmp
);
806 static void fake_assign_expr(struct symbol
*sym
)
808 struct expression
*assign
, *symbol
;
810 symbol
= symbol_expression(sym
);
811 assign
= assign_expression(symbol
, sym
->initializer
);
812 __split_expr(assign
);
815 static void do_initializer_stuff(struct symbol
*sym
)
817 if (!sym
->initializer
)
819 if (sym
->initializer
->type
== EXPR_INITIALIZER
)
820 fake_member_assigns(sym
);
822 fake_assign_expr(sym
);
825 static void split_declaration(struct symbol_list
*sym_list
)
829 FOR_EACH_PTR(sym_list
, sym
) {
830 __pass_to_client(sym
, DECLARATION_HOOK
);
831 do_initializer_stuff(sym
);
833 } END_FOR_EACH_PTR(sym
);
836 static void fake_global_assign(struct symbol
*sym
)
838 struct expression
*assign
, *symbol
;
840 if (!sym
->initializer
)
842 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
843 struct expression
*deref
, *tmp
;
845 symbol
= symbol_expression(sym
);
846 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
847 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
849 deref
= member_expression(symbol
, '.', tmp
->expr_ident
);
850 assign
= assign_expression(deref
, tmp
->ident_expression
);
851 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
852 } END_FOR_EACH_PTR(tmp
);
854 symbol
= symbol_expression(sym
);
855 assign
= assign_expression(symbol
, sym
->initializer
);
856 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
860 static void split_function(struct symbol
*sym
)
862 struct symbol
*base_type
= get_base_type(sym
);
866 cur_func
= sym
->ident
->name
;
867 __smatch_lineno
= sym
->pos
.line
;
870 sm_debug("new function: %s\n", cur_func
);
872 if (option_two_passes
) {
876 __pass_to_client(sym
, FUNC_DEF_HOOK
);
877 __pass_to_client(sym
, AFTER_DEF_HOOK
);
878 __split_stmt(base_type
->stmt
);
879 __split_stmt(base_type
->inline_stmt
);
885 __pass_to_client(sym
, FUNC_DEF_HOOK
);
886 __pass_to_client(sym
, AFTER_DEF_HOOK
);
887 __split_stmt(base_type
->stmt
);
888 __split_stmt(base_type
->inline_stmt
);
889 __pass_to_client(sym
, END_FUNC_HOOK
);
892 free_data_info_allocs();
893 free_expression_stack(&switch_expr_stack
);
894 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
895 __bail_on_rest_of_function
= 0;
898 static void parse_inline(struct expression
*call
)
900 struct symbol
*base_type
;
901 int loop_num_bak
= loop_num
;
902 int final_pass_bak
= final_pass
;
903 char *cur_func_bak
= cur_func
;
904 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
905 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
906 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
907 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
909 __pass_to_client(call
, INLINE_FN_START
);
910 final_pass
= 0; /* don't print anything */
913 base_type
= get_base_type(call
->fn
->symbol
);
914 cur_func_sym
= call
->fn
->symbol
;
915 if (call
->fn
->symbol
->ident
)
916 cur_func
= call
->fn
->symbol
->ident
->name
;
919 set_position(call
->fn
->symbol
->pos
);
922 nullify_all_states();
923 big_statement_stack
= NULL
;
924 big_expression_stack
= NULL
;
925 switch_expr_stack
= NULL
;
927 sm_debug("inline function: %s\n", cur_func
);
930 __pass_to_client(call
->fn
->symbol
, FUNC_DEF_HOOK
);
931 __pass_to_client(call
->fn
->symbol
, AFTER_DEF_HOOK
);
932 __split_stmt(base_type
->stmt
);
933 __split_stmt(base_type
->inline_stmt
);
934 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
936 free_expression_stack(&switch_expr_stack
);
937 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
940 loop_num
= loop_num_bak
;
941 final_pass
= final_pass_bak
;
942 cur_func_sym
= cur_func_sym_bak
;
943 cur_func
= cur_func_bak
;
944 big_statement_stack
= big_statement_stack_bak
;
945 big_expression_stack
= big_expression_stack_bak
;
946 switch_expr_stack
= switch_expr_stack_bak
;
948 restore_all_states();
949 set_position(call
->pos
);
951 __pass_to_client(call
, INLINE_FN_END
);
954 static struct symbol_list
*inlines_called
;
955 static void add_inline_function(struct symbol
*sym
)
957 static struct symbol_list
*already_added
;
960 FOR_EACH_PTR(already_added
, tmp
) {
963 } END_FOR_EACH_PTR(tmp
);
965 add_ptr_list(&already_added
, sym
);
966 add_ptr_list(&inlines_called
, sym
);
969 static void process_inlines()
973 FOR_EACH_PTR(inlines_called
, tmp
) {
975 } END_FOR_EACH_PTR(tmp
);
976 free_ptr_list(&inlines_called
);
979 static void split_functions(struct symbol_list
*sym_list
)
983 FOR_EACH_PTR(sym_list
, sym
) {
984 set_position(sym
->pos
);
985 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
989 __pass_to_client(sym
, BASE_HOOK
);
990 fake_global_assign(sym
);
992 } END_FOR_EACH_PTR(sym
);
993 __pass_to_client_no_data(END_FILE_HOOK
);
996 void smatch(int argc
, char **argv
)
999 struct string_list
*filelist
= NULL
;
1000 struct symbol_list
*sym_list
;
1003 printf("Usage: smatch [--debug] <filename.c>\n");
1006 sparse_initialize(argc
, argv
, &filelist
);
1007 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1008 if (option_file_output
) {
1011 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1012 sm_outfd
= fopen(buf
, "w");
1014 printf("Error: Cannot open %s\n", base_file
);
1018 sym_list
= sparse_keep_tokens(base_file
);
1019 split_functions(sym_list
);
1020 } END_FOR_EACH_PTR_NOTAG(base_file
);