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
);
858 static struct member_set
*alloc_member_set(struct symbol
*type
)
860 struct member_set
*member_set
;
861 struct symbol
*member
;
865 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
866 member_set
= malloc(member_count
* sizeof(*member_set
));
868 FOR_EACH_PTR(type
->symbol_list
, member
) {
869 member_set
[member_idx
].ident
= member
->ident
;
870 member_set
[member_idx
].set
= 0;
872 } END_FOR_EACH_PTR(member
);
877 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
879 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
882 for (i
= 0; i
< member_count
; i
++) {
883 if (member_set
[i
].ident
== ident
) {
884 member_set
[i
].set
= 1;
888 // crap. this is buggy.
889 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
892 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
894 struct expression
*deref
, *assign
;
895 struct symbol
*member
, *member_type
;
899 FOR_EACH_PTR(type
->symbol_list
, member
) {
900 if (!member
->ident
|| member_set
[member_idx
].set
) {
904 member_type
= get_real_base_type(member
);
905 if (!member_type
|| member_type
->type
== SYM_ARRAY
)
907 deref
= member_expression(symbol
, '.', member
->ident
);
908 assign
= assign_expression(deref
, zero_expr());
909 __split_expr(assign
);
911 } END_FOR_EACH_PTR(member
);
915 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
917 struct expression
*deref
, *assign
, *tmp
;
918 struct symbol
*struct_type
, *type
;
919 struct ident
*member
;
921 struct member_set
*member_set
;
923 struct_type
= get_type(symbol
);
925 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
928 member_set
= alloc_member_set(struct_type
);
931 FOR_EACH_PTR(members
, tmp
) {
932 member
= number_to_member(symbol
, member_idx
);
933 while (tmp
->type
== EXPR_IDENTIFIER
) {
934 member
= tmp
->expr_ident
;
935 member_idx
= member_to_number(symbol
, member
);
936 tmp
= tmp
->ident_expression
;
938 mark_member_as_set(struct_type
, member_set
, member
);
940 deref
= member_expression(symbol
, '.', member
);
941 if (tmp
->type
== EXPR_INITIALIZER
) {
942 type
= get_type(deref
);
943 if (type
&& type
->type
== SYM_ARRAY
)
944 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
946 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
948 assign
= assign_expression(deref
, tmp
);
951 } END_FOR_EACH_PTR(tmp
);
953 set_unset_to_zero(symbol
, struct_type
, member_set
);
956 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
958 fake_member_assigns_helper(symbol_expression(sym
),
959 sym
->initializer
->expr_list
, fake_cb
);
962 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
964 struct expression
*offset
, *binop
, *assign
, *tmp
;
969 FOR_EACH_PTR(expr_list
, tmp
) {
970 if (tmp
->type
== EXPR_INDEX
) {
971 if (tmp
->idx_from
!= tmp
->idx_to
)
974 if (!tmp
->idx_expression
)
976 tmp
= tmp
->idx_expression
;
978 offset
= value_expr(idx
);
979 binop
= array_element_expression(array
, offset
);
980 if (tmp
->type
== EXPR_INITIALIZER
) {
981 type
= get_type(binop
);
982 if (type
&& type
->type
== SYM_ARRAY
)
983 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
985 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
987 assign
= assign_expression(binop
, tmp
);
992 } END_FOR_EACH_PTR(tmp
);
995 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
997 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1000 static void fake_assign_expr(struct symbol
*sym
)
1002 struct expression
*assign
, *symbol
;
1004 symbol
= symbol_expression(sym
);
1005 assign
= assign_expression(symbol
, sym
->initializer
);
1006 __split_expr(assign
);
1009 static void call_split_expr(struct expression
*expr
)
1014 static void do_initializer_stuff(struct symbol
*sym
)
1016 if (!sym
->initializer
)
1019 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1020 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1021 fake_element_assigns(sym
, call_split_expr
);
1023 fake_member_assigns(sym
, call_split_expr
);
1025 fake_assign_expr(sym
);
1029 static void split_declaration(struct symbol_list
*sym_list
)
1033 FOR_EACH_PTR(sym_list
, sym
) {
1034 __pass_to_client(sym
, DECLARATION_HOOK
);
1035 do_initializer_stuff(sym
);
1037 } END_FOR_EACH_PTR(sym
);
1040 static void call_global_assign_hooks(struct expression
*assign
)
1042 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1045 static void fake_global_assign(struct symbol
*sym
)
1047 struct expression
*assign
, *symbol
;
1049 if (!sym
->initializer
)
1051 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1052 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1053 fake_element_assigns(sym
, call_global_assign_hooks
);
1055 fake_member_assigns(sym
, call_global_assign_hooks
);
1057 symbol
= symbol_expression(sym
);
1058 assign
= assign_expression(symbol
, sym
->initializer
);
1059 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1063 static void split_function(struct symbol
*sym
)
1065 struct symbol
*base_type
= get_base_type(sym
);
1069 cur_func
= sym
->ident
->name
;
1070 __smatch_lineno
= sym
->pos
.line
;
1073 sm_debug("new function: %s\n", cur_func
);
1075 if (option_two_passes
) {
1079 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1080 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1081 __split_stmt(base_type
->stmt
);
1082 __split_stmt(base_type
->inline_stmt
);
1088 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1089 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1090 __split_stmt(base_type
->stmt
);
1091 __split_stmt(base_type
->inline_stmt
);
1092 __pass_to_client(sym
, END_FUNC_HOOK
);
1093 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1094 cur_func_sym
= NULL
;
1097 free_data_info_allocs();
1098 free_expression_stack(&switch_expr_stack
);
1099 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1100 __bail_on_rest_of_function
= 0;
1103 static void parse_inline(struct expression
*call
)
1105 struct symbol
*base_type
;
1106 int loop_num_bak
= loop_num
;
1107 int final_pass_bak
= final_pass
;
1108 char *cur_func_bak
= cur_func
;
1109 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1110 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1111 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1112 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1114 __pass_to_client(call
, INLINE_FN_START
);
1115 final_pass
= 0; /* don't print anything */
1118 base_type
= get_base_type(call
->fn
->symbol
);
1119 cur_func_sym
= call
->fn
->symbol
;
1120 if (call
->fn
->symbol
->ident
)
1121 cur_func
= call
->fn
->symbol
->ident
->name
;
1124 set_position(call
->fn
->symbol
->pos
);
1127 nullify_all_states();
1128 big_statement_stack
= NULL
;
1129 big_expression_stack
= NULL
;
1130 switch_expr_stack
= NULL
;
1132 sm_debug("inline function: %s\n", cur_func
);
1135 __pass_to_client(call
->fn
->symbol
, FUNC_DEF_HOOK
);
1136 __pass_to_client(call
->fn
->symbol
, AFTER_DEF_HOOK
);
1137 __split_stmt(base_type
->stmt
);
1138 __split_stmt(base_type
->inline_stmt
);
1139 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1140 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1142 free_expression_stack(&switch_expr_stack
);
1143 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1146 loop_num
= loop_num_bak
;
1147 final_pass
= final_pass_bak
;
1148 cur_func_sym
= cur_func_sym_bak
;
1149 cur_func
= cur_func_bak
;
1150 big_statement_stack
= big_statement_stack_bak
;
1151 big_expression_stack
= big_expression_stack_bak
;
1152 switch_expr_stack
= switch_expr_stack_bak
;
1154 restore_all_states();
1155 set_position(call
->pos
);
1157 __pass_to_client(call
, INLINE_FN_END
);
1160 static struct symbol_list
*inlines_called
;
1161 static void add_inline_function(struct symbol
*sym
)
1163 static struct symbol_list
*already_added
;
1166 FOR_EACH_PTR(already_added
, tmp
) {
1169 } END_FOR_EACH_PTR(tmp
);
1171 add_ptr_list(&already_added
, sym
);
1172 add_ptr_list(&inlines_called
, sym
);
1175 static void process_inlines()
1179 FOR_EACH_PTR(inlines_called
, tmp
) {
1180 split_function(tmp
);
1181 } END_FOR_EACH_PTR(tmp
);
1182 free_ptr_list(&inlines_called
);
1185 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1189 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1192 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1194 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1196 } END_FOR_EACH_PTR_REVERSE(sym
);
1201 static void split_inlines_in_scope(struct symbol
*sym
)
1203 struct symbol
*base
;
1204 struct symbol_list
*scope_list
;
1207 scope_list
= sym
->scope
->symbols
;
1208 stream
= sym
->pos
.stream
;
1210 /* find the last static symbol in the file */
1211 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1212 if (sym
->pos
.stream
!= stream
)
1214 if (sym
->type
!= SYM_NODE
)
1216 base
= get_base_type(sym
);
1219 if (base
->type
!= SYM_FN
)
1221 if (!base
->inline_stmt
)
1223 add_inline_function(sym
);
1224 } END_FOR_EACH_PTR_REVERSE(sym
);
1229 static void split_inlines(struct symbol_list
*sym_list
)
1233 sym
= get_last_scoped_symbol(sym_list
, 0);
1235 split_inlines_in_scope(sym
);
1236 sym
= get_last_scoped_symbol(sym_list
, 1);
1238 split_inlines_in_scope(sym
);
1241 static void split_functions(struct symbol_list
*sym_list
)
1245 FOR_EACH_PTR(sym_list
, sym
) {
1246 set_position(sym
->pos
);
1247 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1248 split_function(sym
);
1251 __pass_to_client(sym
, BASE_HOOK
);
1252 fake_global_assign(sym
);
1254 } END_FOR_EACH_PTR(sym
);
1255 split_inlines(sym_list
);
1256 __pass_to_client(sym_list
, END_FILE_HOOK
);
1259 void smatch(int argc
, char **argv
)
1262 struct string_list
*filelist
= NULL
;
1263 struct symbol_list
*sym_list
;
1266 printf("Usage: smatch [--debug] <filename.c>\n");
1269 sparse_initialize(argc
, argv
, &filelist
);
1270 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1271 if (option_file_output
) {
1274 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1275 sm_outfd
= fopen(buf
, "w");
1277 printf("Error: Cannot open %s\n", base_file
);
1281 sym_list
= sparse_keep_tokens(base_file
);
1282 split_functions(sym_list
);
1283 } END_FOR_EACH_PTR_NOTAG(base_file
);