4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
16 #include "smatch_expression_stacks.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
22 struct expression
*__inline_fn
;
24 static int __smatch_lineno
= 0;
26 static char *base_file
;
27 static const char *filename
;
28 static char *pathname
;
29 static char *full_filename
;
30 static char *cur_func
;
31 static int loop_count
;
32 int __expr_stmt_count
;
33 static struct expression_list
*switch_expr_stack
= NULL
;
34 static struct expression_list
*post_op_stack
= NULL
;
36 struct expression_list
*big_expression_stack
;
37 struct statement_list
*big_statement_stack
;
38 int __in_pre_condition
= 0;
39 int __bail_on_rest_of_function
= 0;
40 char *get_function(void) { return cur_func
; }
41 int get_lineno(void) { return __smatch_lineno
; }
42 int inside_loop(void) { return !!loop_count
; }
43 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
44 int in_expression_statement(void) { return !!__expr_stmt_count
; }
46 static void split_symlist(struct symbol_list
*sym_list
);
47 static void split_declaration(struct symbol_list
*sym_list
);
48 static void split_expr_list(struct expression_list
*expr_list
);
49 static void add_inline_function(struct symbol
*sym
);
50 static void parse_inline(struct expression
*expr
);
52 int option_assume_loops
= 0;
53 int option_known_conditions
= 0;
54 int option_two_passes
= 0;
55 struct symbol
*cur_func_sym
= NULL
;
57 const char *get_filename(void)
66 const char *get_base_file(void)
71 static void set_position(struct position pos
)
74 static int prev_stream
= -1;
76 if (pos
.stream
== 0 && pos
.line
== 0)
79 __smatch_lineno
= pos
.line
;
81 if (pos
.stream
== prev_stream
)
84 filename
= stream_name(pos
.stream
);
87 pathname
= getcwd(NULL
, 0);
89 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
90 full_filename
= malloc(len
);
91 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
93 full_filename
= alloc_string(filename
);
98 static int is_inline_func(struct expression
*expr
)
100 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
102 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
107 static int is_noreturn_func(struct expression
*expr
)
109 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
111 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
116 int inlinable(struct expression
*expr
)
120 if (__inline_fn
) /* don't nest */
123 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
125 sym
= get_base_type(expr
->symbol
);
126 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
127 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
131 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
132 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
139 void __process_post_op_stack(void)
141 struct expression
*expr
;
143 FOR_EACH_PTR(post_op_stack
, expr
) {
144 __pass_to_client(expr
, OP_HOOK
);
145 } END_FOR_EACH_PTR(expr
);
147 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
150 void __split_expr(struct expression
*expr
)
155 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
157 push_expression(&big_expression_stack
, expr
);
158 set_position(expr
->pos
);
159 __pass_to_client(expr
, EXPR_HOOK
);
161 switch (expr
->type
) {
164 __pass_to_client(expr
, DEREF_HOOK
);
165 __split_expr(expr
->unop
);
166 __pass_to_client(expr
, OP_HOOK
);
169 __split_expr(expr
->unop
);
170 push_expression(&post_op_stack
, expr
);
174 __split_stmt(expr
->statement
);
179 __pass_to_client(expr
, LOGIC_HOOK
);
180 __handle_logic(expr
);
183 __pass_to_client(expr
, BINOP_HOOK
);
185 __split_expr(expr
->left
);
186 __process_post_op_stack();
187 __split_expr(expr
->right
);
189 case EXPR_ASSIGNMENT
: {
190 struct expression
*tmp
;
195 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
198 if (__handle_condition_assigns(expr
))
200 /* foo = (x < 5 ? foo : 5); */
201 if (__handle_select_assigns(expr
))
203 /* foo = ({frob(); frob(); frob(); 1;}) */
204 if (__handle_expr_statement_assigns(expr
))
207 __split_expr(expr
->right
);
208 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
209 tmp
= strip_expr(expr
->right
);
210 if (tmp
->type
== EXPR_CALL
)
211 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
212 if (get_macro_name(tmp
->pos
) &&
213 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
214 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
215 __split_expr(expr
->left
);
219 __pass_to_client(expr
, DEREF_HOOK
);
220 __split_expr(expr
->deref
);
223 __split_expr(expr
->base
);
226 case EXPR_FORCE_CAST
:
227 __pass_to_client(expr
, CAST_HOOK
);
228 __split_expr(expr
->cast_expression
);
231 if (expr
->cast_expression
)
232 __pass_to_client(strip_parens(expr
->cast_expression
),
237 evaluate_expression(expr
);
239 case EXPR_CONDITIONAL
:
241 if (known_condition_true(expr
->conditional
)) {
242 __split_expr(expr
->cond_true
);
245 if (known_condition_false(expr
->conditional
)) {
246 __split_expr(expr
->cond_false
);
249 __pass_to_client(expr
, SELECT_HOOK
);
250 __split_whole_condition(expr
->conditional
);
251 __split_expr(expr
->cond_true
);
252 __push_true_states();
253 __use_false_states();
254 __split_expr(expr
->cond_false
);
255 __merge_true_states();
258 if (sym_name_is("__builtin_constant_p", expr
->fn
))
260 split_expr_list(expr
->args
);
261 __split_expr(expr
->fn
);
262 if (is_inline_func(expr
->fn
))
263 add_inline_function(expr
->fn
->symbol
);
264 if (inlinable(expr
->fn
))
266 __process_post_op_stack();
267 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
269 if (inlinable(expr
->fn
)) {
272 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
273 if (is_noreturn_func(expr
->fn
))
276 case EXPR_INITIALIZER
:
277 split_expr_list(expr
->expr_list
);
279 case EXPR_IDENTIFIER
:
280 __split_expr(expr
->ident_expression
);
283 __split_expr(expr
->idx_expression
);
286 __split_expr(expr
->init_expr
);
289 __pass_to_client(expr
, SYM_HOOK
);
292 __pass_to_client(expr
, STRING_HOOK
);
297 pop_expression(&big_expression_stack
);
300 static int is_forever_loop(struct statement
*stmt
)
302 struct expression
*expr
;
304 expr
= strip_expr(stmt
->iterator_pre_condition
);
306 expr
= stmt
->iterator_post_condition
;
308 /* this is a for(;;) loop... */
312 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
319 static char *get_loop_name(int num
)
323 snprintf(buf
, 255, "-loop%d", num
);
325 return alloc_sname(buf
);
329 * Pre Loops are while and for loops.
331 static void handle_pre_loop(struct statement
*stmt
)
333 int once_through
; /* we go through the loop at least once */
334 struct sm_state
*extra_sm
= NULL
;
337 struct state_list
*slist
= NULL
;
338 struct sm_state
*sm
= NULL
;
340 loop_name
= get_loop_name(loop_num
);
343 __split_stmt(stmt
->iterator_pre_statement
);
345 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
351 __merge_gotos(loop_name
);
353 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
354 __in_pre_condition
++;
355 __pass_to_client(stmt
, PRELOOP_HOOK
);
356 __split_whole_condition(stmt
->iterator_pre_condition
);
357 __in_pre_condition
--;
358 FOR_EACH_PTR(slist
, sm
) {
359 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
360 } END_FOR_EACH_PTR(sm
);
363 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
365 if (option_assume_loops
)
368 __split_stmt(stmt
->iterator_statement
);
369 __warn_on_silly_pre_loops();
370 if (is_forever_loop(stmt
)) {
371 struct state_list
*slist
;
373 __save_gotos(loop_name
);
375 __push_fake_cur_slist();
376 __split_stmt(stmt
->iterator_post_statement
);
377 slist
= __pop_fake_cur_slist();
379 __discard_continues();
380 __discard_false_states();
383 if (!__path_is_null())
384 __merge_slist_into_cur(slist
);
388 unchanged
= __iterator_unchanged(extra_sm
);
389 __split_stmt(stmt
->iterator_post_statement
);
390 __save_gotos(loop_name
);
391 __split_whole_condition(stmt
->iterator_pre_condition
);
393 __merge_false_states();
395 __discard_false_states();
397 __merge_false_states();
399 if (extra_sm
&& unchanged
)
400 __extra_pre_loop_hook_after(extra_sm
,
401 stmt
->iterator_post_statement
,
402 stmt
->iterator_pre_condition
);
409 * Post loops are do {} while();
411 static void handle_post_loop(struct statement
*stmt
)
415 loop_name
= get_loop_name(loop_num
);
421 __merge_gotos(loop_name
);
422 __split_stmt(stmt
->iterator_statement
);
424 if (!is_zero(stmt
->iterator_post_condition
))
425 __save_gotos(loop_name
);
427 if (is_forever_loop(stmt
)) {
430 __split_whole_condition(stmt
->iterator_post_condition
);
431 __use_false_states();
437 static int empty_statement(struct statement
*stmt
)
441 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
446 static int last_stmt_on_same_line()
448 struct statement
*stmt
;
451 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
454 if (stmt
->pos
.line
== get_lineno())
457 } END_FOR_EACH_PTR_REVERSE(stmt
);
461 static struct statement
*last_stmt
;
462 static int is_last_stmt(struct statement
*stmt
)
464 if (stmt
== last_stmt
)
469 static void print_unreached_initializers(struct symbol_list
*sym_list
)
473 FOR_EACH_PTR(sym_list
, sym
) {
474 if (sym
->initializer
)
475 sm_msg("info: '%s' is not actually initialized (unreached code).",
476 (sym
->ident
? sym
->ident
->name
: "this variable"));
477 } END_FOR_EACH_PTR(sym
);
480 static void print_unreached(struct statement
*stmt
)
482 static int print
= 1;
484 if (!__path_is_null()) {
491 switch (stmt
->type
) {
492 case STMT_COMPOUND
: /* after a switch before a case stmt */
497 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
498 print_unreached_initializers(stmt
->declaration
);
500 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
501 if (is_last_stmt(stmt
))
511 if (!option_spammy
&& empty_statement(stmt
))
513 sm_msg("info: ignoring unreachable code.");
517 static void split_asm_constraints(struct expression_list
*expr_list
)
519 struct expression
*expr
;
522 FOR_EACH_PTR(expr_list
, expr
) {
524 case 0: /* identifier */
525 case 1: /* constraint */
528 case 2: /* expression */
533 } END_FOR_EACH_PTR(expr
);
536 static int is_case_val(struct statement
*stmt
, sval_t sval
)
540 if (stmt
->type
!= STMT_CASE
)
542 if (!stmt
->case_expression
) {
546 if (!get_value(stmt
->case_expression
, &case_sval
))
548 if (case_sval
.value
== sval
.value
)
553 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
555 struct statement
*tmp
;
557 __split_expr(stmt
->switch_expression
);
559 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
560 __save_switch_states(top_expression(switch_expr_stack
));
565 stmt
= stmt
->switch_statement
;
568 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
570 __push_scope_hooks();
571 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
572 __smatch_lineno
= tmp
->pos
.line
;
573 if (is_case_val(tmp
, sval
)) {
574 __merge_switches(top_expression(switch_expr_stack
),
575 stmt
->case_expression
);
576 __pass_case_to_client(top_expression(switch_expr_stack
),
577 stmt
->case_expression
);
579 if (__path_is_null())
582 if (__path_is_null()) {
586 } END_FOR_EACH_PTR(tmp
);
588 __call_scope_hooks();
589 if (!__pop_default())
590 __merge_switches(top_expression(switch_expr_stack
),
592 __discard_switches();
594 pop_expression(&switch_expr_stack
);
597 void __split_stmt(struct statement
*stmt
)
604 if (out_of_memory() || __bail_on_rest_of_function
) {
605 static char *printed
= NULL
;
607 if (printed
!= cur_func
)
608 sm_msg("Function too hairy. Giving up.");
609 final_pass
= 0; /* turn off sm_msg() from here */
614 add_ptr_list(&big_statement_stack
, stmt
);
615 free_expression_stack(&big_expression_stack
);
616 set_position(stmt
->pos
);
617 print_unreached(stmt
);
618 __pass_to_client(stmt
, STMT_HOOK
);
620 switch (stmt
->type
) {
621 case STMT_DECLARATION
:
622 split_declaration(stmt
->declaration
);
625 __split_expr(stmt
->ret_value
);
626 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
629 case STMT_EXPRESSION
:
630 __split_expr(stmt
->expression
);
632 case STMT_COMPOUND
: {
633 struct statement
*tmp
;
636 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
637 __push_scope_hooks();
638 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
640 } END_FOR_EACH_PTR(tmp
);
641 __call_scope_hooks();
645 if (known_condition_true(stmt
->if_conditional
)) {
646 __split_stmt(stmt
->if_true
);
649 if (known_condition_false(stmt
->if_conditional
)) {
650 __split_stmt(stmt
->if_false
);
653 if (option_known_conditions
&&
654 implied_condition_true(stmt
->if_conditional
)) {
655 sm_info("this condition is true.");
656 __split_stmt(stmt
->if_true
);
659 if (option_known_conditions
&&
660 implied_condition_false(stmt
->if_conditional
)) {
661 sm_info("this condition is false.");
662 __split_stmt(stmt
->if_false
);
665 __split_whole_condition(stmt
->if_conditional
);
666 __split_stmt(stmt
->if_true
);
667 if (empty_statement(stmt
->if_true
) &&
668 last_stmt_on_same_line() &&
669 !get_macro_name(stmt
->if_true
->pos
))
670 sm_msg("warn: if();");
671 __push_true_states();
672 __use_false_states();
673 __split_stmt(stmt
->if_false
);
674 __merge_true_states();
677 if (stmt
->iterator_pre_condition
)
678 handle_pre_loop(stmt
);
679 else if (stmt
->iterator_post_condition
)
680 handle_post_loop(stmt
);
682 // these are for(;;) type loops.
683 handle_pre_loop(stmt
);
687 if (get_value(stmt
->switch_expression
, &sval
)) {
688 split_known_switch(stmt
, sval
);
691 __split_expr(stmt
->switch_expression
);
692 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
693 __save_switch_states(top_expression(switch_expr_stack
));
697 __split_stmt(stmt
->switch_statement
);
698 if (!__pop_default())
699 __merge_switches(top_expression(switch_expr_stack
),
701 __discard_switches();
703 pop_expression(&switch_expr_stack
);
706 __merge_switches(top_expression(switch_expr_stack
),
707 stmt
->case_expression
);
708 __pass_case_to_client(top_expression(switch_expr_stack
),
709 stmt
->case_expression
);
710 if (!stmt
->case_expression
)
712 __split_expr(stmt
->case_expression
);
713 __split_expr(stmt
->case_to
);
714 __split_stmt(stmt
->case_statement
);
717 if (stmt
->label_identifier
&&
718 stmt
->label_identifier
->type
== SYM_LABEL
&&
719 stmt
->label_identifier
->ident
) {
720 loop_count
= 1000000;
721 __merge_gotos(stmt
->label_identifier
->ident
->name
);
723 __split_stmt(stmt
->label_statement
);
726 __split_expr(stmt
->goto_expression
);
727 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
728 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
730 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
732 __process_continues();
734 } else if (stmt
->goto_label
&&
735 stmt
->goto_label
->type
== SYM_LABEL
&&
736 stmt
->goto_label
->ident
) {
737 __save_gotos(stmt
->goto_label
->ident
->name
);
744 __pass_to_client(stmt
, ASM_HOOK
);
745 __split_expr(stmt
->asm_string
);
746 split_asm_constraints(stmt
->asm_outputs
);
747 split_asm_constraints(stmt
->asm_inputs
);
748 split_asm_constraints(stmt
->asm_clobbers
);
753 __split_expr(stmt
->range_expression
);
754 __split_expr(stmt
->range_low
);
755 __split_expr(stmt
->range_high
);
759 __process_post_op_stack();
762 static void split_expr_list(struct expression_list
*expr_list
)
764 struct expression
*expr
;
766 FOR_EACH_PTR(expr_list
, expr
) {
768 __process_post_op_stack();
769 } END_FOR_EACH_PTR(expr
);
772 static void split_sym(struct symbol
*sym
)
776 if (!(sym
->namespace & NS_SYMBOL
))
779 __split_stmt(sym
->stmt
);
780 __split_expr(sym
->array_size
);
781 split_symlist(sym
->arguments
);
782 split_symlist(sym
->symbol_list
);
783 __split_stmt(sym
->inline_stmt
);
784 split_symlist(sym
->inline_symbol_list
);
787 static void split_symlist(struct symbol_list
*sym_list
)
791 FOR_EACH_PTR(sym_list
, sym
) {
793 } END_FOR_EACH_PTR(sym
);
796 typedef void (fake_cb
)(struct expression
*expr
);
798 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
800 struct expression
*symbol
, *deref
, *assign
, *tmp
;
802 symbol
= symbol_expression(sym
);
803 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
804 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
806 deref
= member_expression(symbol
, '.', tmp
->expr_ident
);
807 assign
= assign_expression(deref
, tmp
->ident_expression
);
809 } END_FOR_EACH_PTR(tmp
);
812 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
814 struct expression
*array
, *offset
, *binop
, *assign
, *tmp
;
817 array
= symbol_expression(sym
);
819 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
820 if (tmp
->type
== EXPR_INDEX
) {
821 if (tmp
->idx_from
!= tmp
->idx_to
)
824 if (!tmp
->idx_expression
)
826 tmp
= tmp
->idx_expression
;
828 offset
= value_expr(idx
);
829 binop
= binop_expression(array
, '+', offset
);
830 assign
= assign_expression(binop
, tmp
);
834 } END_FOR_EACH_PTR(tmp
);
837 static void fake_assign_expr(struct symbol
*sym
)
839 struct expression
*assign
, *symbol
;
841 symbol
= symbol_expression(sym
);
842 assign
= assign_expression(symbol
, sym
->initializer
);
843 __split_expr(assign
);
846 static void call_split_expr(struct expression
*expr
)
851 static void do_initializer_stuff(struct symbol
*sym
)
853 if (!sym
->initializer
)
856 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
857 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
858 fake_element_assigns(sym
, call_split_expr
);
860 fake_member_assigns(sym
, call_split_expr
);
862 fake_assign_expr(sym
);
866 static void split_declaration(struct symbol_list
*sym_list
)
870 FOR_EACH_PTR(sym_list
, sym
) {
871 __pass_to_client(sym
, DECLARATION_HOOK
);
872 do_initializer_stuff(sym
);
874 } END_FOR_EACH_PTR(sym
);
877 static void call_global_assign_hooks(struct expression
*assign
)
879 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
882 static void fake_global_assign(struct symbol
*sym
)
884 struct expression
*assign
, *symbol
;
886 if (!sym
->initializer
)
888 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
889 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
890 fake_element_assigns(sym
, call_global_assign_hooks
);
892 fake_member_assigns(sym
, call_global_assign_hooks
);
894 symbol
= symbol_expression(sym
);
895 assign
= assign_expression(symbol
, sym
->initializer
);
896 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
900 static void split_function(struct symbol
*sym
)
902 struct symbol
*base_type
= get_base_type(sym
);
906 cur_func
= sym
->ident
->name
;
907 __smatch_lineno
= sym
->pos
.line
;
910 sm_debug("new function: %s\n", cur_func
);
912 if (option_two_passes
) {
916 __pass_to_client(sym
, FUNC_DEF_HOOK
);
917 __pass_to_client(sym
, AFTER_DEF_HOOK
);
918 __split_stmt(base_type
->stmt
);
919 __split_stmt(base_type
->inline_stmt
);
925 __pass_to_client(sym
, FUNC_DEF_HOOK
);
926 __pass_to_client(sym
, AFTER_DEF_HOOK
);
927 __split_stmt(base_type
->stmt
);
928 __split_stmt(base_type
->inline_stmt
);
929 __pass_to_client(sym
, END_FUNC_HOOK
);
932 free_data_info_allocs();
933 free_expression_stack(&switch_expr_stack
);
934 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
935 __bail_on_rest_of_function
= 0;
938 static void parse_inline(struct expression
*call
)
940 struct symbol
*base_type
;
941 int loop_num_bak
= loop_num
;
942 int final_pass_bak
= final_pass
;
943 char *cur_func_bak
= cur_func
;
944 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
945 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
946 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
947 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
949 __pass_to_client(call
, INLINE_FN_START
);
950 final_pass
= 0; /* don't print anything */
953 base_type
= get_base_type(call
->fn
->symbol
);
954 cur_func_sym
= call
->fn
->symbol
;
955 if (call
->fn
->symbol
->ident
)
956 cur_func
= call
->fn
->symbol
->ident
->name
;
959 set_position(call
->fn
->symbol
->pos
);
962 nullify_all_states();
963 big_statement_stack
= NULL
;
964 big_expression_stack
= NULL
;
965 switch_expr_stack
= NULL
;
967 sm_debug("inline function: %s\n", cur_func
);
970 __pass_to_client(call
->fn
->symbol
, FUNC_DEF_HOOK
);
971 __pass_to_client(call
->fn
->symbol
, AFTER_DEF_HOOK
);
972 __split_stmt(base_type
->stmt
);
973 __split_stmt(base_type
->inline_stmt
);
974 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
976 free_expression_stack(&switch_expr_stack
);
977 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
980 loop_num
= loop_num_bak
;
981 final_pass
= final_pass_bak
;
982 cur_func_sym
= cur_func_sym_bak
;
983 cur_func
= cur_func_bak
;
984 big_statement_stack
= big_statement_stack_bak
;
985 big_expression_stack
= big_expression_stack_bak
;
986 switch_expr_stack
= switch_expr_stack_bak
;
988 restore_all_states();
989 set_position(call
->pos
);
991 __pass_to_client(call
, INLINE_FN_END
);
994 static struct symbol_list
*inlines_called
;
995 static void add_inline_function(struct symbol
*sym
)
997 static struct symbol_list
*already_added
;
1000 FOR_EACH_PTR(already_added
, tmp
) {
1003 } END_FOR_EACH_PTR(tmp
);
1005 add_ptr_list(&already_added
, sym
);
1006 add_ptr_list(&inlines_called
, sym
);
1009 static void process_inlines()
1013 FOR_EACH_PTR(inlines_called
, tmp
) {
1014 split_function(tmp
);
1015 } END_FOR_EACH_PTR(tmp
);
1016 free_ptr_list(&inlines_called
);
1019 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
)
1023 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1027 } END_FOR_EACH_PTR_REVERSE(sym
);
1032 static void split_inlines(struct symbol_list
*sym_list
)
1034 struct symbol
*sym
, *base
;
1035 struct symbol_list
*scope_list
;
1038 sym
= get_last_scoped_symbol(sym_list
);
1041 scope_list
= sym
->scope
->symbols
;
1042 stream
= sym
->pos
.stream
;
1044 /* find the last static symbol in the file */
1045 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1046 if (sym
->pos
.stream
!= stream
)
1048 if (sym
->type
!= SYM_NODE
)
1050 base
= get_base_type(sym
);
1053 if (base
->type
!= SYM_FN
)
1055 if (!base
->inline_stmt
)
1057 add_inline_function(sym
);
1058 } END_FOR_EACH_PTR_REVERSE(sym
);
1063 static void split_functions(struct symbol_list
*sym_list
)
1067 FOR_EACH_PTR(sym_list
, sym
) {
1068 set_position(sym
->pos
);
1069 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1070 split_function(sym
);
1073 __pass_to_client(sym
, BASE_HOOK
);
1074 fake_global_assign(sym
);
1076 } END_FOR_EACH_PTR(sym
);
1077 split_inlines(sym_list
);
1078 __pass_to_client(sym_list
, END_FILE_HOOK
);
1081 void smatch(int argc
, char **argv
)
1084 struct string_list
*filelist
= NULL
;
1085 struct symbol_list
*sym_list
;
1088 printf("Usage: smatch [--debug] <filename.c>\n");
1091 sparse_initialize(argc
, argv
, &filelist
);
1092 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1093 if (option_file_output
) {
1096 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1097 sm_outfd
= fopen(buf
, "w");
1099 printf("Error: Cannot open %s\n", base_file
);
1103 sym_list
= sparse_keep_tokens(base_file
);
1104 split_functions(sym_list
);
1105 } END_FOR_EACH_PTR_NOTAG(base_file
);