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 int outside_of_function(void)
59 return cur_func_sym
== NULL
;
62 const char *get_filename(void)
71 const char *get_base_file(void)
76 static void set_position(struct position pos
)
79 static int prev_stream
= -1;
81 if (pos
.stream
== 0 && pos
.line
== 0)
84 __smatch_lineno
= pos
.line
;
86 if (pos
.stream
== prev_stream
)
89 filename
= stream_name(pos
.stream
);
92 pathname
= getcwd(NULL
, 0);
94 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
95 full_filename
= malloc(len
);
96 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
98 full_filename
= alloc_string(filename
);
103 static int is_inline_func(struct expression
*expr
)
105 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
107 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
112 static int is_noreturn_func(struct expression
*expr
)
114 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
116 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
121 int inlinable(struct expression
*expr
)
125 if (__inline_fn
) /* don't nest */
128 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
130 sym
= get_base_type(expr
->symbol
);
131 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
132 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
136 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
137 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
144 void __process_post_op_stack(void)
146 struct expression
*expr
;
148 FOR_EACH_PTR(post_op_stack
, expr
) {
149 __pass_to_client(expr
, OP_HOOK
);
150 } END_FOR_EACH_PTR(expr
);
152 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
155 void __split_expr(struct expression
*expr
)
160 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
162 push_expression(&big_expression_stack
, expr
);
163 set_position(expr
->pos
);
164 __pass_to_client(expr
, EXPR_HOOK
);
166 switch (expr
->type
) {
169 __pass_to_client(expr
, DEREF_HOOK
);
170 __split_expr(expr
->unop
);
171 __pass_to_client(expr
, OP_HOOK
);
174 __split_expr(expr
->unop
);
175 push_expression(&post_op_stack
, expr
);
179 __split_stmt(expr
->statement
);
184 __pass_to_client(expr
, LOGIC_HOOK
);
185 __handle_logic(expr
);
188 __pass_to_client(expr
, BINOP_HOOK
);
190 __split_expr(expr
->left
);
191 __process_post_op_stack();
192 __split_expr(expr
->right
);
194 case EXPR_ASSIGNMENT
: {
195 struct expression
*tmp
;
200 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
203 if (__handle_condition_assigns(expr
))
205 /* foo = (x < 5 ? foo : 5); */
206 if (__handle_select_assigns(expr
))
208 /* foo = ({frob(); frob(); frob(); 1;}) */
209 if (__handle_expr_statement_assigns(expr
))
212 __split_expr(expr
->right
);
213 if (outside_of_function())
214 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
216 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
217 tmp
= strip_expr(expr
->right
);
218 if (tmp
->type
== EXPR_CALL
)
219 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
220 if (get_macro_name(tmp
->pos
) &&
221 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
222 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
223 __split_expr(expr
->left
);
227 __pass_to_client(expr
, DEREF_HOOK
);
228 __split_expr(expr
->deref
);
231 __split_expr(expr
->base
);
234 case EXPR_FORCE_CAST
:
235 __pass_to_client(expr
, CAST_HOOK
);
236 __split_expr(expr
->cast_expression
);
239 if (expr
->cast_expression
)
240 __pass_to_client(strip_parens(expr
->cast_expression
),
245 evaluate_expression(expr
);
247 case EXPR_CONDITIONAL
:
249 if (known_condition_true(expr
->conditional
)) {
250 __split_expr(expr
->cond_true
);
253 if (known_condition_false(expr
->conditional
)) {
254 __split_expr(expr
->cond_false
);
257 __pass_to_client(expr
, SELECT_HOOK
);
258 __split_whole_condition(expr
->conditional
);
259 __split_expr(expr
->cond_true
);
260 __push_true_states();
261 __use_false_states();
262 __split_expr(expr
->cond_false
);
263 __merge_true_states();
266 if (sym_name_is("__builtin_constant_p", expr
->fn
))
268 split_expr_list(expr
->args
);
269 __split_expr(expr
->fn
);
270 if (is_inline_func(expr
->fn
))
271 add_inline_function(expr
->fn
->symbol
);
272 if (inlinable(expr
->fn
))
274 __process_post_op_stack();
275 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
277 if (inlinable(expr
->fn
)) {
280 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
281 if (is_noreturn_func(expr
->fn
))
284 case EXPR_INITIALIZER
:
285 split_expr_list(expr
->expr_list
);
287 case EXPR_IDENTIFIER
:
288 __split_expr(expr
->ident_expression
);
291 __split_expr(expr
->idx_expression
);
294 __split_expr(expr
->init_expr
);
297 __pass_to_client(expr
, SYM_HOOK
);
300 __pass_to_client(expr
, STRING_HOOK
);
305 pop_expression(&big_expression_stack
);
308 static int is_forever_loop(struct statement
*stmt
)
310 struct expression
*expr
;
312 expr
= strip_expr(stmt
->iterator_pre_condition
);
314 expr
= stmt
->iterator_post_condition
;
316 /* this is a for(;;) loop... */
320 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
327 static char *get_loop_name(int num
)
331 snprintf(buf
, 255, "-loop%d", num
);
333 return alloc_sname(buf
);
337 * Pre Loops are while and for loops.
339 static void handle_pre_loop(struct statement
*stmt
)
341 int once_through
; /* we go through the loop at least once */
342 struct sm_state
*extra_sm
= NULL
;
345 struct state_list
*slist
= NULL
;
346 struct sm_state
*sm
= NULL
;
348 loop_name
= get_loop_name(loop_num
);
351 __split_stmt(stmt
->iterator_pre_statement
);
353 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
359 __merge_gotos(loop_name
);
361 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
362 __in_pre_condition
++;
363 __pass_to_client(stmt
, PRELOOP_HOOK
);
364 __split_whole_condition(stmt
->iterator_pre_condition
);
365 __in_pre_condition
--;
366 FOR_EACH_PTR(slist
, sm
) {
367 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
368 } END_FOR_EACH_PTR(sm
);
371 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
373 if (option_assume_loops
)
376 __split_stmt(stmt
->iterator_statement
);
377 __warn_on_silly_pre_loops();
378 if (is_forever_loop(stmt
)) {
379 struct state_list
*slist
;
381 __save_gotos(loop_name
);
383 __push_fake_cur_slist();
384 __split_stmt(stmt
->iterator_post_statement
);
385 slist
= __pop_fake_cur_slist();
387 __discard_continues();
388 __discard_false_states();
391 if (!__path_is_null())
392 __merge_slist_into_cur(slist
);
396 unchanged
= __iterator_unchanged(extra_sm
);
397 __split_stmt(stmt
->iterator_post_statement
);
398 __save_gotos(loop_name
);
399 __split_whole_condition(stmt
->iterator_pre_condition
);
401 __merge_false_states();
403 __discard_false_states();
405 __merge_false_states();
407 if (extra_sm
&& unchanged
)
408 __extra_pre_loop_hook_after(extra_sm
,
409 stmt
->iterator_post_statement
,
410 stmt
->iterator_pre_condition
);
417 * Post loops are do {} while();
419 static void handle_post_loop(struct statement
*stmt
)
423 loop_name
= get_loop_name(loop_num
);
429 __merge_gotos(loop_name
);
430 __split_stmt(stmt
->iterator_statement
);
432 if (!is_zero(stmt
->iterator_post_condition
))
433 __save_gotos(loop_name
);
435 if (is_forever_loop(stmt
)) {
438 __split_whole_condition(stmt
->iterator_post_condition
);
439 __use_false_states();
445 static int empty_statement(struct statement
*stmt
)
449 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
454 static int last_stmt_on_same_line()
456 struct statement
*stmt
;
459 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
462 if (stmt
->pos
.line
== get_lineno())
465 } END_FOR_EACH_PTR_REVERSE(stmt
);
469 static struct statement
*last_stmt
;
470 static int is_last_stmt(struct statement
*stmt
)
472 if (stmt
== last_stmt
)
477 static void print_unreached_initializers(struct symbol_list
*sym_list
)
481 FOR_EACH_PTR(sym_list
, sym
) {
482 if (sym
->initializer
)
483 sm_msg("info: '%s' is not actually initialized (unreached code).",
484 (sym
->ident
? sym
->ident
->name
: "this variable"));
485 } END_FOR_EACH_PTR(sym
);
488 static void print_unreached(struct statement
*stmt
)
490 static int print
= 1;
495 if (!__path_is_null()) {
502 switch (stmt
->type
) {
503 case STMT_COMPOUND
: /* after a switch before a case stmt */
508 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
509 print_unreached_initializers(stmt
->declaration
);
511 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
512 if (is_last_stmt(stmt
))
522 if (!option_spammy
&& empty_statement(stmt
))
524 sm_msg("info: ignoring unreachable code.");
528 static void split_asm_constraints(struct expression_list
*expr_list
)
530 struct expression
*expr
;
533 FOR_EACH_PTR(expr_list
, expr
) {
535 case 0: /* identifier */
536 case 1: /* constraint */
539 case 2: /* expression */
544 } END_FOR_EACH_PTR(expr
);
547 static int is_case_val(struct statement
*stmt
, sval_t sval
)
551 if (stmt
->type
!= STMT_CASE
)
553 if (!stmt
->case_expression
) {
557 if (!get_value(stmt
->case_expression
, &case_sval
))
559 if (case_sval
.value
== sval
.value
)
564 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
566 struct statement
*tmp
;
568 __split_expr(stmt
->switch_expression
);
570 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
571 __save_switch_states(top_expression(switch_expr_stack
));
576 stmt
= stmt
->switch_statement
;
579 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
581 __push_scope_hooks();
582 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
583 __smatch_lineno
= tmp
->pos
.line
;
584 if (is_case_val(tmp
, sval
)) {
585 __merge_switches(top_expression(switch_expr_stack
),
586 stmt
->case_expression
);
587 __pass_case_to_client(top_expression(switch_expr_stack
),
588 stmt
->case_expression
);
590 if (__path_is_null())
593 if (__path_is_null()) {
597 } END_FOR_EACH_PTR(tmp
);
599 __call_scope_hooks();
600 if (!__pop_default())
601 __merge_switches(top_expression(switch_expr_stack
),
603 __discard_switches();
605 pop_expression(&switch_expr_stack
);
608 void __split_stmt(struct statement
*stmt
)
615 if (out_of_memory() || __bail_on_rest_of_function
) {
616 static char *printed
= NULL
;
618 if (printed
!= cur_func
)
619 sm_msg("Function too hairy. Giving up.");
620 final_pass
= 0; /* turn off sm_msg() from here */
625 add_ptr_list(&big_statement_stack
, stmt
);
626 free_expression_stack(&big_expression_stack
);
627 set_position(stmt
->pos
);
628 print_unreached(stmt
);
629 __pass_to_client(stmt
, STMT_HOOK
);
631 switch (stmt
->type
) {
632 case STMT_DECLARATION
:
633 split_declaration(stmt
->declaration
);
636 __split_expr(stmt
->ret_value
);
637 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
640 case STMT_EXPRESSION
:
641 __split_expr(stmt
->expression
);
643 case STMT_COMPOUND
: {
644 struct statement
*tmp
;
647 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
648 __push_scope_hooks();
649 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
651 } END_FOR_EACH_PTR(tmp
);
652 __call_scope_hooks();
656 if (known_condition_true(stmt
->if_conditional
)) {
657 __split_stmt(stmt
->if_true
);
660 if (known_condition_false(stmt
->if_conditional
)) {
661 __split_stmt(stmt
->if_false
);
664 if (option_known_conditions
&&
665 implied_condition_true(stmt
->if_conditional
)) {
666 sm_info("this condition is true.");
667 __split_stmt(stmt
->if_true
);
670 if (option_known_conditions
&&
671 implied_condition_false(stmt
->if_conditional
)) {
672 sm_info("this condition is false.");
673 __split_stmt(stmt
->if_false
);
676 __split_whole_condition(stmt
->if_conditional
);
677 __split_stmt(stmt
->if_true
);
678 if (empty_statement(stmt
->if_true
) &&
679 last_stmt_on_same_line() &&
680 !get_macro_name(stmt
->if_true
->pos
))
681 sm_msg("warn: if();");
682 __push_true_states();
683 __use_false_states();
684 __split_stmt(stmt
->if_false
);
685 __merge_true_states();
688 if (stmt
->iterator_pre_condition
)
689 handle_pre_loop(stmt
);
690 else if (stmt
->iterator_post_condition
)
691 handle_post_loop(stmt
);
693 // these are for(;;) type loops.
694 handle_pre_loop(stmt
);
698 if (get_value(stmt
->switch_expression
, &sval
)) {
699 split_known_switch(stmt
, sval
);
702 __split_expr(stmt
->switch_expression
);
703 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
704 __save_switch_states(top_expression(switch_expr_stack
));
708 __split_stmt(stmt
->switch_statement
);
709 if (!__pop_default())
710 __merge_switches(top_expression(switch_expr_stack
),
712 __discard_switches();
714 pop_expression(&switch_expr_stack
);
717 __merge_switches(top_expression(switch_expr_stack
),
718 stmt
->case_expression
);
719 __pass_case_to_client(top_expression(switch_expr_stack
),
720 stmt
->case_expression
);
721 if (!stmt
->case_expression
)
723 __split_expr(stmt
->case_expression
);
724 __split_expr(stmt
->case_to
);
725 __split_stmt(stmt
->case_statement
);
728 if (stmt
->label_identifier
&&
729 stmt
->label_identifier
->type
== SYM_LABEL
&&
730 stmt
->label_identifier
->ident
) {
731 loop_count
= 1000000;
732 __merge_gotos(stmt
->label_identifier
->ident
->name
);
734 __split_stmt(stmt
->label_statement
);
737 __split_expr(stmt
->goto_expression
);
738 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
739 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
741 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
743 __process_continues();
745 } else if (stmt
->goto_label
&&
746 stmt
->goto_label
->type
== SYM_LABEL
&&
747 stmt
->goto_label
->ident
) {
748 __save_gotos(stmt
->goto_label
->ident
->name
);
755 __pass_to_client(stmt
, ASM_HOOK
);
756 __split_expr(stmt
->asm_string
);
757 split_asm_constraints(stmt
->asm_outputs
);
758 split_asm_constraints(stmt
->asm_inputs
);
759 split_asm_constraints(stmt
->asm_clobbers
);
764 __split_expr(stmt
->range_expression
);
765 __split_expr(stmt
->range_low
);
766 __split_expr(stmt
->range_high
);
770 __process_post_op_stack();
773 static void split_expr_list(struct expression_list
*expr_list
)
775 struct expression
*expr
;
777 FOR_EACH_PTR(expr_list
, expr
) {
779 __process_post_op_stack();
780 } END_FOR_EACH_PTR(expr
);
783 static void split_sym(struct symbol
*sym
)
787 if (!(sym
->namespace & NS_SYMBOL
))
790 __split_stmt(sym
->stmt
);
791 __split_expr(sym
->array_size
);
792 split_symlist(sym
->arguments
);
793 split_symlist(sym
->symbol_list
);
794 __split_stmt(sym
->inline_stmt
);
795 split_symlist(sym
->inline_symbol_list
);
798 static void split_symlist(struct symbol_list
*sym_list
)
802 FOR_EACH_PTR(sym_list
, sym
) {
804 } END_FOR_EACH_PTR(sym
);
807 typedef void (fake_cb
)(struct expression
*expr
);
809 static int member_to_number(struct expression
*expr
, struct ident
*member
)
811 struct symbol
*type
, *tmp
;
819 type
= get_type(expr
);
820 if (!type
|| type
->type
!= SYM_STRUCT
)
824 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
828 if (strcmp(name
, tmp
->ident
->name
) == 0)
830 } END_FOR_EACH_PTR(tmp
);
834 static struct ident
*number_to_member(struct expression
*expr
, int num
)
836 struct symbol
*type
, *member
;
839 type
= get_type(expr
);
840 if (!type
|| type
->type
!= SYM_STRUCT
)
843 FOR_EACH_PTR(type
->symbol_list
, member
) {
845 return member
->ident
;
847 } END_FOR_EACH_PTR(member
);
851 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
853 static void set_members_to_zero(struct expression
*symbol
)
855 struct expression
*deref
, *assign
;
857 struct symbol
*member
, *member_type
;
859 type
= get_type(symbol
);
863 FOR_EACH_PTR(type
->symbol_list
, member
) {
866 member_type
= get_real_base_type(member
);
867 if (!member_type
|| member_type
->type
== SYM_ARRAY
)
869 deref
= member_expression(symbol
, '.', member
->ident
);
870 assign
= assign_expression(deref
, zero_expr());
871 __split_expr(assign
);
872 } END_FOR_EACH_PTR(member
);
876 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
878 struct expression
*deref
, *assign
, *tmp
;
880 struct ident
*member
;
883 type
= get_type(symbol
);
884 if (!type
|| (type
->type
!= SYM_STRUCT
&& type
->type
!= SYM_UNION
))
887 set_members_to_zero(symbol
);
889 FOR_EACH_PTR(members
, tmp
) {
890 member
= number_to_member(symbol
, member_idx
);
891 while (tmp
->type
== EXPR_IDENTIFIER
) {
892 member
= tmp
->expr_ident
;
893 member_idx
= member_to_number(symbol
, member
);
894 tmp
= tmp
->ident_expression
;
897 deref
= member_expression(symbol
, '.', member
);
898 if (tmp
->type
== EXPR_INITIALIZER
) {
899 type
= get_type(deref
);
900 if (type
&& type
->type
== SYM_ARRAY
)
901 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
903 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
905 assign
= assign_expression(deref
, tmp
);
908 } END_FOR_EACH_PTR(tmp
);
911 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
913 fake_member_assigns_helper(symbol_expression(sym
),
914 sym
->initializer
->expr_list
, fake_cb
);
917 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
919 struct expression
*offset
, *binop
, *assign
, *tmp
;
924 FOR_EACH_PTR(expr_list
, tmp
) {
925 if (tmp
->type
== EXPR_INDEX
) {
926 if (tmp
->idx_from
!= tmp
->idx_to
)
929 if (!tmp
->idx_expression
)
931 tmp
= tmp
->idx_expression
;
933 offset
= value_expr(idx
);
934 binop
= array_element_expression(array
, offset
);
935 if (tmp
->type
== EXPR_INITIALIZER
) {
936 type
= get_type(binop
);
937 if (type
&& type
->type
== SYM_ARRAY
)
938 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
940 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
942 assign
= assign_expression(binop
, tmp
);
947 } END_FOR_EACH_PTR(tmp
);
950 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
952 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
955 static void fake_assign_expr(struct symbol
*sym
)
957 struct expression
*assign
, *symbol
;
959 symbol
= symbol_expression(sym
);
960 assign
= assign_expression(symbol
, sym
->initializer
);
961 __split_expr(assign
);
964 static void call_split_expr(struct expression
*expr
)
969 static void do_initializer_stuff(struct symbol
*sym
)
971 if (!sym
->initializer
)
974 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
975 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
976 fake_element_assigns(sym
, call_split_expr
);
978 fake_member_assigns(sym
, call_split_expr
);
980 fake_assign_expr(sym
);
984 static void split_declaration(struct symbol_list
*sym_list
)
988 FOR_EACH_PTR(sym_list
, sym
) {
989 __pass_to_client(sym
, DECLARATION_HOOK
);
990 do_initializer_stuff(sym
);
992 } END_FOR_EACH_PTR(sym
);
995 static void call_global_assign_hooks(struct expression
*assign
)
997 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1000 static void fake_global_assign(struct symbol
*sym
)
1002 struct expression
*assign
, *symbol
;
1004 if (!sym
->initializer
)
1006 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1007 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1008 fake_element_assigns(sym
, call_global_assign_hooks
);
1010 fake_member_assigns(sym
, call_global_assign_hooks
);
1012 symbol
= symbol_expression(sym
);
1013 assign
= assign_expression(symbol
, sym
->initializer
);
1014 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1018 static void split_function(struct symbol
*sym
)
1020 struct symbol
*base_type
= get_base_type(sym
);
1024 cur_func
= sym
->ident
->name
;
1025 __smatch_lineno
= sym
->pos
.line
;
1028 sm_debug("new function: %s\n", cur_func
);
1030 if (option_two_passes
) {
1034 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1035 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1036 __split_stmt(base_type
->stmt
);
1037 __split_stmt(base_type
->inline_stmt
);
1043 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1044 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1045 __split_stmt(base_type
->stmt
);
1046 __split_stmt(base_type
->inline_stmt
);
1047 __pass_to_client(sym
, END_FUNC_HOOK
);
1048 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1049 cur_func_sym
= NULL
;
1052 free_data_info_allocs();
1053 free_expression_stack(&switch_expr_stack
);
1054 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1055 __bail_on_rest_of_function
= 0;
1058 static void parse_inline(struct expression
*call
)
1060 struct symbol
*base_type
;
1061 int loop_num_bak
= loop_num
;
1062 int final_pass_bak
= final_pass
;
1063 char *cur_func_bak
= cur_func
;
1064 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1065 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1066 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1067 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1069 __pass_to_client(call
, INLINE_FN_START
);
1070 final_pass
= 0; /* don't print anything */
1073 base_type
= get_base_type(call
->fn
->symbol
);
1074 cur_func_sym
= call
->fn
->symbol
;
1075 if (call
->fn
->symbol
->ident
)
1076 cur_func
= call
->fn
->symbol
->ident
->name
;
1079 set_position(call
->fn
->symbol
->pos
);
1082 nullify_all_states();
1083 big_statement_stack
= NULL
;
1084 big_expression_stack
= NULL
;
1085 switch_expr_stack
= NULL
;
1087 sm_debug("inline function: %s\n", cur_func
);
1090 __pass_to_client(call
->fn
->symbol
, FUNC_DEF_HOOK
);
1091 __pass_to_client(call
->fn
->symbol
, AFTER_DEF_HOOK
);
1092 __split_stmt(base_type
->stmt
);
1093 __split_stmt(base_type
->inline_stmt
);
1094 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1095 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1097 free_expression_stack(&switch_expr_stack
);
1098 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1101 loop_num
= loop_num_bak
;
1102 final_pass
= final_pass_bak
;
1103 cur_func_sym
= cur_func_sym_bak
;
1104 cur_func
= cur_func_bak
;
1105 big_statement_stack
= big_statement_stack_bak
;
1106 big_expression_stack
= big_expression_stack_bak
;
1107 switch_expr_stack
= switch_expr_stack_bak
;
1109 restore_all_states();
1110 set_position(call
->pos
);
1112 __pass_to_client(call
, INLINE_FN_END
);
1115 static struct symbol_list
*inlines_called
;
1116 static void add_inline_function(struct symbol
*sym
)
1118 static struct symbol_list
*already_added
;
1121 FOR_EACH_PTR(already_added
, tmp
) {
1124 } END_FOR_EACH_PTR(tmp
);
1126 add_ptr_list(&already_added
, sym
);
1127 add_ptr_list(&inlines_called
, sym
);
1130 static void process_inlines()
1134 FOR_EACH_PTR(inlines_called
, tmp
) {
1135 split_function(tmp
);
1136 } END_FOR_EACH_PTR(tmp
);
1137 free_ptr_list(&inlines_called
);
1140 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1144 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1147 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1149 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1151 } END_FOR_EACH_PTR_REVERSE(sym
);
1156 static void split_inlines_in_scope(struct symbol
*sym
)
1158 struct symbol
*base
;
1159 struct symbol_list
*scope_list
;
1162 scope_list
= sym
->scope
->symbols
;
1163 stream
= sym
->pos
.stream
;
1165 /* find the last static symbol in the file */
1166 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1167 if (sym
->pos
.stream
!= stream
)
1169 if (sym
->type
!= SYM_NODE
)
1171 base
= get_base_type(sym
);
1174 if (base
->type
!= SYM_FN
)
1176 if (!base
->inline_stmt
)
1178 add_inline_function(sym
);
1179 } END_FOR_EACH_PTR_REVERSE(sym
);
1184 static void split_inlines(struct symbol_list
*sym_list
)
1188 sym
= get_last_scoped_symbol(sym_list
, 0);
1190 split_inlines_in_scope(sym
);
1191 sym
= get_last_scoped_symbol(sym_list
, 1);
1193 split_inlines_in_scope(sym
);
1196 static void split_functions(struct symbol_list
*sym_list
)
1200 FOR_EACH_PTR(sym_list
, sym
) {
1201 set_position(sym
->pos
);
1202 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1203 split_function(sym
);
1206 __pass_to_client(sym
, BASE_HOOK
);
1207 fake_global_assign(sym
);
1209 } END_FOR_EACH_PTR(sym
);
1210 split_inlines(sym_list
);
1211 __pass_to_client(sym_list
, END_FILE_HOOK
);
1214 void smatch(int argc
, char **argv
)
1217 struct string_list
*filelist
= NULL
;
1218 struct symbol_list
*sym_list
;
1221 printf("Usage: smatch [--debug] <filename.c>\n");
1224 sparse_initialize(argc
, argv
, &filelist
);
1225 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1226 if (option_file_output
) {
1229 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1230 sm_outfd
= fopen(buf
, "w");
1232 printf("Error: Cannot open %s\n", base_file
);
1236 sym_list
= sparse_keep_tokens(base_file
);
1237 split_functions(sym_list
);
1238 } END_FOR_EACH_PTR_NOTAG(base_file
);