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;
487 if (!__path_is_null()) {
494 switch (stmt
->type
) {
495 case STMT_COMPOUND
: /* after a switch before a case stmt */
500 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
501 print_unreached_initializers(stmt
->declaration
);
503 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
504 if (is_last_stmt(stmt
))
514 if (!option_spammy
&& empty_statement(stmt
))
516 sm_msg("info: ignoring unreachable code.");
520 static void split_asm_constraints(struct expression_list
*expr_list
)
522 struct expression
*expr
;
525 FOR_EACH_PTR(expr_list
, expr
) {
527 case 0: /* identifier */
528 case 1: /* constraint */
531 case 2: /* expression */
536 } END_FOR_EACH_PTR(expr
);
539 static int is_case_val(struct statement
*stmt
, sval_t sval
)
543 if (stmt
->type
!= STMT_CASE
)
545 if (!stmt
->case_expression
) {
549 if (!get_value(stmt
->case_expression
, &case_sval
))
551 if (case_sval
.value
== sval
.value
)
556 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
558 struct statement
*tmp
;
560 __split_expr(stmt
->switch_expression
);
562 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
563 __save_switch_states(top_expression(switch_expr_stack
));
568 stmt
= stmt
->switch_statement
;
571 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
573 __push_scope_hooks();
574 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
575 __smatch_lineno
= tmp
->pos
.line
;
576 if (is_case_val(tmp
, sval
)) {
577 __merge_switches(top_expression(switch_expr_stack
),
578 stmt
->case_expression
);
579 __pass_case_to_client(top_expression(switch_expr_stack
),
580 stmt
->case_expression
);
582 if (__path_is_null())
585 if (__path_is_null()) {
589 } END_FOR_EACH_PTR(tmp
);
591 __call_scope_hooks();
592 if (!__pop_default())
593 __merge_switches(top_expression(switch_expr_stack
),
595 __discard_switches();
597 pop_expression(&switch_expr_stack
);
600 void __split_stmt(struct statement
*stmt
)
607 if (out_of_memory() || __bail_on_rest_of_function
) {
608 static char *printed
= NULL
;
610 if (printed
!= cur_func
)
611 sm_msg("Function too hairy. Giving up.");
612 final_pass
= 0; /* turn off sm_msg() from here */
617 add_ptr_list(&big_statement_stack
, stmt
);
618 free_expression_stack(&big_expression_stack
);
619 set_position(stmt
->pos
);
620 print_unreached(stmt
);
621 __pass_to_client(stmt
, STMT_HOOK
);
623 switch (stmt
->type
) {
624 case STMT_DECLARATION
:
625 split_declaration(stmt
->declaration
);
628 __split_expr(stmt
->ret_value
);
629 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
632 case STMT_EXPRESSION
:
633 __split_expr(stmt
->expression
);
635 case STMT_COMPOUND
: {
636 struct statement
*tmp
;
639 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
640 __push_scope_hooks();
641 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
643 } END_FOR_EACH_PTR(tmp
);
644 __call_scope_hooks();
648 if (known_condition_true(stmt
->if_conditional
)) {
649 __split_stmt(stmt
->if_true
);
652 if (known_condition_false(stmt
->if_conditional
)) {
653 __split_stmt(stmt
->if_false
);
656 if (option_known_conditions
&&
657 implied_condition_true(stmt
->if_conditional
)) {
658 sm_info("this condition is true.");
659 __split_stmt(stmt
->if_true
);
662 if (option_known_conditions
&&
663 implied_condition_false(stmt
->if_conditional
)) {
664 sm_info("this condition is false.");
665 __split_stmt(stmt
->if_false
);
668 __split_whole_condition(stmt
->if_conditional
);
669 __split_stmt(stmt
->if_true
);
670 if (empty_statement(stmt
->if_true
) &&
671 last_stmt_on_same_line() &&
672 !get_macro_name(stmt
->if_true
->pos
))
673 sm_msg("warn: if();");
674 __push_true_states();
675 __use_false_states();
676 __split_stmt(stmt
->if_false
);
677 __merge_true_states();
680 if (stmt
->iterator_pre_condition
)
681 handle_pre_loop(stmt
);
682 else if (stmt
->iterator_post_condition
)
683 handle_post_loop(stmt
);
685 // these are for(;;) type loops.
686 handle_pre_loop(stmt
);
690 if (get_value(stmt
->switch_expression
, &sval
)) {
691 split_known_switch(stmt
, sval
);
694 __split_expr(stmt
->switch_expression
);
695 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
696 __save_switch_states(top_expression(switch_expr_stack
));
700 __split_stmt(stmt
->switch_statement
);
701 if (!__pop_default())
702 __merge_switches(top_expression(switch_expr_stack
),
704 __discard_switches();
706 pop_expression(&switch_expr_stack
);
709 __merge_switches(top_expression(switch_expr_stack
),
710 stmt
->case_expression
);
711 __pass_case_to_client(top_expression(switch_expr_stack
),
712 stmt
->case_expression
);
713 if (!stmt
->case_expression
)
715 __split_expr(stmt
->case_expression
);
716 __split_expr(stmt
->case_to
);
717 __split_stmt(stmt
->case_statement
);
720 if (stmt
->label_identifier
&&
721 stmt
->label_identifier
->type
== SYM_LABEL
&&
722 stmt
->label_identifier
->ident
) {
723 loop_count
= 1000000;
724 __merge_gotos(stmt
->label_identifier
->ident
->name
);
726 __split_stmt(stmt
->label_statement
);
729 __split_expr(stmt
->goto_expression
);
730 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
731 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
733 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
735 __process_continues();
737 } else if (stmt
->goto_label
&&
738 stmt
->goto_label
->type
== SYM_LABEL
&&
739 stmt
->goto_label
->ident
) {
740 __save_gotos(stmt
->goto_label
->ident
->name
);
747 __pass_to_client(stmt
, ASM_HOOK
);
748 __split_expr(stmt
->asm_string
);
749 split_asm_constraints(stmt
->asm_outputs
);
750 split_asm_constraints(stmt
->asm_inputs
);
751 split_asm_constraints(stmt
->asm_clobbers
);
756 __split_expr(stmt
->range_expression
);
757 __split_expr(stmt
->range_low
);
758 __split_expr(stmt
->range_high
);
762 __process_post_op_stack();
765 static void split_expr_list(struct expression_list
*expr_list
)
767 struct expression
*expr
;
769 FOR_EACH_PTR(expr_list
, expr
) {
771 __process_post_op_stack();
772 } END_FOR_EACH_PTR(expr
);
775 static void split_sym(struct symbol
*sym
)
779 if (!(sym
->namespace & NS_SYMBOL
))
782 __split_stmt(sym
->stmt
);
783 __split_expr(sym
->array_size
);
784 split_symlist(sym
->arguments
);
785 split_symlist(sym
->symbol_list
);
786 __split_stmt(sym
->inline_stmt
);
787 split_symlist(sym
->inline_symbol_list
);
790 static void split_symlist(struct symbol_list
*sym_list
)
794 FOR_EACH_PTR(sym_list
, sym
) {
796 } END_FOR_EACH_PTR(sym
);
799 typedef void (fake_cb
)(struct expression
*expr
);
801 static int member_to_number(struct expression
*expr
, struct ident
*member
)
803 struct symbol
*type
, *tmp
;
811 type
= get_type(expr
);
812 if (!type
|| type
->type
!= SYM_STRUCT
)
816 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
820 if (strcmp(name
, tmp
->ident
->name
) == 0)
822 } END_FOR_EACH_PTR(tmp
);
826 static struct ident
*number_to_member(struct expression
*expr
, int num
)
828 struct symbol
*type
, *member
;
831 type
= get_type(expr
);
832 if (!type
|| type
->type
!= SYM_STRUCT
)
835 FOR_EACH_PTR(type
->symbol_list
, member
) {
837 return member
->ident
;
839 } END_FOR_EACH_PTR(member
);
843 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
845 struct expression
*deref
, *assign
, *tmp
;
847 struct ident
*member
;
850 type
= get_type(symbol
);
851 if (!type
|| type
->type
!= SYM_STRUCT
)
854 FOR_EACH_PTR(members
, tmp
) {
855 if (tmp
->type
== EXPR_IDENTIFIER
) {
856 member
= tmp
->expr_ident
;
857 member_idx
= member_to_number(symbol
, member
);
858 tmp
= tmp
->ident_expression
;
860 member
= number_to_member(symbol
, member_idx
);
863 deref
= member_expression(symbol
, '.', member
);
864 if (tmp
->type
== EXPR_INITIALIZER
) {
865 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
867 assign
= assign_expression(deref
, tmp
);
870 } END_FOR_EACH_PTR(tmp
);
873 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
875 fake_member_assigns_helper(symbol_expression(sym
),
876 sym
->initializer
->expr_list
, fake_cb
);
879 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
881 struct expression
*array
, *offset
, *binop
, *assign
, *tmp
;
884 array
= symbol_expression(sym
);
886 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
887 if (tmp
->type
== EXPR_INDEX
) {
888 if (tmp
->idx_from
!= tmp
->idx_to
)
891 if (!tmp
->idx_expression
)
893 tmp
= tmp
->idx_expression
;
895 offset
= value_expr(idx
);
896 binop
= array_element_expression(array
, offset
);
897 if (tmp
->type
== EXPR_INITIALIZER
) {
898 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
900 assign
= assign_expression(binop
, tmp
);
905 } END_FOR_EACH_PTR(tmp
);
908 static void fake_assign_expr(struct symbol
*sym
)
910 struct expression
*assign
, *symbol
;
912 symbol
= symbol_expression(sym
);
913 assign
= assign_expression(symbol
, sym
->initializer
);
914 __split_expr(assign
);
917 static void call_split_expr(struct expression
*expr
)
922 static void do_initializer_stuff(struct symbol
*sym
)
924 if (!sym
->initializer
)
927 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
928 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
929 fake_element_assigns(sym
, call_split_expr
);
931 fake_member_assigns(sym
, call_split_expr
);
933 fake_assign_expr(sym
);
937 static void split_declaration(struct symbol_list
*sym_list
)
941 FOR_EACH_PTR(sym_list
, sym
) {
942 __pass_to_client(sym
, DECLARATION_HOOK
);
943 do_initializer_stuff(sym
);
945 } END_FOR_EACH_PTR(sym
);
948 static void call_global_assign_hooks(struct expression
*assign
)
950 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
953 static void fake_global_assign(struct symbol
*sym
)
955 struct expression
*assign
, *symbol
;
957 if (!sym
->initializer
)
959 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
960 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
961 fake_element_assigns(sym
, call_global_assign_hooks
);
963 fake_member_assigns(sym
, call_global_assign_hooks
);
965 symbol
= symbol_expression(sym
);
966 assign
= assign_expression(symbol
, sym
->initializer
);
967 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
971 static void split_function(struct symbol
*sym
)
973 struct symbol
*base_type
= get_base_type(sym
);
977 cur_func
= sym
->ident
->name
;
978 __smatch_lineno
= sym
->pos
.line
;
981 sm_debug("new function: %s\n", cur_func
);
983 if (option_two_passes
) {
987 __pass_to_client(sym
, FUNC_DEF_HOOK
);
988 __pass_to_client(sym
, AFTER_DEF_HOOK
);
989 __split_stmt(base_type
->stmt
);
990 __split_stmt(base_type
->inline_stmt
);
996 __pass_to_client(sym
, FUNC_DEF_HOOK
);
997 __pass_to_client(sym
, AFTER_DEF_HOOK
);
998 __split_stmt(base_type
->stmt
);
999 __split_stmt(base_type
->inline_stmt
);
1000 __pass_to_client(sym
, END_FUNC_HOOK
);
1003 free_data_info_allocs();
1004 free_expression_stack(&switch_expr_stack
);
1005 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1006 __bail_on_rest_of_function
= 0;
1009 static void parse_inline(struct expression
*call
)
1011 struct symbol
*base_type
;
1012 int loop_num_bak
= loop_num
;
1013 int final_pass_bak
= final_pass
;
1014 char *cur_func_bak
= cur_func
;
1015 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1016 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1017 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1018 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1020 __pass_to_client(call
, INLINE_FN_START
);
1021 final_pass
= 0; /* don't print anything */
1024 base_type
= get_base_type(call
->fn
->symbol
);
1025 cur_func_sym
= call
->fn
->symbol
;
1026 if (call
->fn
->symbol
->ident
)
1027 cur_func
= call
->fn
->symbol
->ident
->name
;
1030 set_position(call
->fn
->symbol
->pos
);
1033 nullify_all_states();
1034 big_statement_stack
= NULL
;
1035 big_expression_stack
= NULL
;
1036 switch_expr_stack
= NULL
;
1038 sm_debug("inline function: %s\n", cur_func
);
1041 __pass_to_client(call
->fn
->symbol
, FUNC_DEF_HOOK
);
1042 __pass_to_client(call
->fn
->symbol
, AFTER_DEF_HOOK
);
1043 __split_stmt(base_type
->stmt
);
1044 __split_stmt(base_type
->inline_stmt
);
1045 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1047 free_expression_stack(&switch_expr_stack
);
1048 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1051 loop_num
= loop_num_bak
;
1052 final_pass
= final_pass_bak
;
1053 cur_func_sym
= cur_func_sym_bak
;
1054 cur_func
= cur_func_bak
;
1055 big_statement_stack
= big_statement_stack_bak
;
1056 big_expression_stack
= big_expression_stack_bak
;
1057 switch_expr_stack
= switch_expr_stack_bak
;
1059 restore_all_states();
1060 set_position(call
->pos
);
1062 __pass_to_client(call
, INLINE_FN_END
);
1065 static struct symbol_list
*inlines_called
;
1066 static void add_inline_function(struct symbol
*sym
)
1068 static struct symbol_list
*already_added
;
1071 FOR_EACH_PTR(already_added
, tmp
) {
1074 } END_FOR_EACH_PTR(tmp
);
1076 add_ptr_list(&already_added
, sym
);
1077 add_ptr_list(&inlines_called
, sym
);
1080 static void process_inlines()
1084 FOR_EACH_PTR(inlines_called
, tmp
) {
1085 split_function(tmp
);
1086 } END_FOR_EACH_PTR(tmp
);
1087 free_ptr_list(&inlines_called
);
1090 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
)
1094 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1098 } END_FOR_EACH_PTR_REVERSE(sym
);
1103 static void split_inlines(struct symbol_list
*sym_list
)
1105 struct symbol
*sym
, *base
;
1106 struct symbol_list
*scope_list
;
1109 sym
= get_last_scoped_symbol(sym_list
);
1112 scope_list
= sym
->scope
->symbols
;
1113 stream
= sym
->pos
.stream
;
1115 /* find the last static symbol in the file */
1116 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1117 if (sym
->pos
.stream
!= stream
)
1119 if (sym
->type
!= SYM_NODE
)
1121 base
= get_base_type(sym
);
1124 if (base
->type
!= SYM_FN
)
1126 if (!base
->inline_stmt
)
1128 add_inline_function(sym
);
1129 } END_FOR_EACH_PTR_REVERSE(sym
);
1134 static void split_functions(struct symbol_list
*sym_list
)
1138 FOR_EACH_PTR(sym_list
, sym
) {
1139 set_position(sym
->pos
);
1140 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1141 split_function(sym
);
1144 __pass_to_client(sym
, BASE_HOOK
);
1145 fake_global_assign(sym
);
1147 } END_FOR_EACH_PTR(sym
);
1148 split_inlines(sym_list
);
1149 __pass_to_client(sym_list
, END_FILE_HOOK
);
1152 void smatch(int argc
, char **argv
)
1155 struct string_list
*filelist
= NULL
;
1156 struct symbol_list
*sym_list
;
1159 printf("Usage: smatch [--debug] <filename.c>\n");
1162 sparse_initialize(argc
, argv
, &filelist
);
1163 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1164 if (option_file_output
) {
1167 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1168 sm_outfd
= fopen(buf
, "w");
1170 printf("Error: Cannot open %s\n", base_file
);
1174 sym_list
= sparse_keep_tokens(base_file
);
1175 split_functions(sym_list
);
1176 } END_FOR_EACH_PTR_NOTAG(base_file
);