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"
23 struct expression
*__inline_fn
;
25 static int __smatch_lineno
= 0;
27 static char *base_file
;
28 static const char *filename
;
29 static char *pathname
;
30 static char *full_filename
;
31 static char *cur_func
;
32 static int loop_count
;
33 int __expr_stmt_count
;
34 int __in_function_def
;
35 static struct expression_list
*switch_expr_stack
= NULL
;
36 static struct expression_list
*post_op_stack
= NULL
;
38 struct expression_list
*big_expression_stack
;
39 struct statement_list
*big_statement_stack
;
40 int __in_pre_condition
= 0;
41 int __bail_on_rest_of_function
= 0;
42 char *get_function(void) { return cur_func
; }
43 int get_lineno(void) { return __smatch_lineno
; }
44 int inside_loop(void) { return !!loop_count
; }
45 struct expression
*get_switch_expr(void) { return top_expression(switch_expr_stack
); }
46 int in_expression_statement(void) { return !!__expr_stmt_count
; }
48 static void split_symlist(struct symbol_list
*sym_list
);
49 static void split_declaration(struct symbol_list
*sym_list
);
50 static void split_expr_list(struct expression_list
*expr_list
);
51 static void add_inline_function(struct symbol
*sym
);
52 static void parse_inline(struct expression
*expr
);
54 int option_assume_loops
= 0;
55 int option_known_conditions
= 0;
56 int option_two_passes
= 0;
57 struct symbol
*cur_func_sym
= NULL
;
59 int outside_of_function(void)
61 return cur_func_sym
== NULL
;
64 const char *get_filename(void)
73 const char *get_base_file(void)
78 static void set_position(struct position pos
)
81 static int prev_stream
= -1;
83 if (pos
.stream
== 0 && pos
.line
== 0)
86 __smatch_lineno
= pos
.line
;
88 if (pos
.stream
== prev_stream
)
91 filename
= stream_name(pos
.stream
);
94 pathname
= getcwd(NULL
, 0);
96 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
97 full_filename
= malloc(len
);
98 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
100 full_filename
= alloc_string(filename
);
105 static int is_inline_func(struct expression
*expr
)
107 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
109 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
114 static int is_noreturn_func(struct expression
*expr
)
116 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
118 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
123 int inlinable(struct expression
*expr
)
127 if (__inline_fn
) /* don't nest */
130 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
132 if (is_no_inline_function(expr
->symbol
->ident
->name
))
134 sym
= get_base_type(expr
->symbol
);
135 if (sym
->stmt
&& sym
->stmt
->type
== STMT_COMPOUND
) {
136 if (ptr_list_size((struct ptr_list
*)sym
->stmt
->stmts
) <= 10)
140 if (sym
->inline_stmt
&& sym
->inline_stmt
->type
== STMT_COMPOUND
) {
141 if (ptr_list_size((struct ptr_list
*)sym
->inline_stmt
->stmts
) <= 10)
148 void __process_post_op_stack(void)
150 struct expression
*expr
;
152 FOR_EACH_PTR(post_op_stack
, expr
) {
153 __pass_to_client(expr
, OP_HOOK
);
154 } END_FOR_EACH_PTR(expr
);
156 __free_ptr_list((struct ptr_list
**)&post_op_stack
);
159 void __split_expr(struct expression
*expr
)
164 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
166 if (__in_fake_assign
&& expr
->type
!= EXPR_ASSIGNMENT
)
168 if (__in_fake_assign
>= 4) /* don't allow too much nesting */
171 push_expression(&big_expression_stack
, expr
);
172 set_position(expr
->pos
);
173 __pass_to_client(expr
, EXPR_HOOK
);
175 switch (expr
->type
) {
178 __pass_to_client(expr
, DEREF_HOOK
);
179 __split_expr(expr
->unop
);
180 __pass_to_client(expr
, OP_HOOK
);
183 __split_expr(expr
->unop
);
184 push_expression(&post_op_stack
, expr
);
188 __split_stmt(expr
->statement
);
193 __pass_to_client(expr
, LOGIC_HOOK
);
194 __handle_logic(expr
);
197 __pass_to_client(expr
, BINOP_HOOK
);
199 __split_expr(expr
->left
);
200 __process_post_op_stack();
201 __split_expr(expr
->right
);
203 case EXPR_ASSIGNMENT
: {
204 struct expression
*tmp
;
209 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
212 if (__handle_condition_assigns(expr
))
214 /* foo = (x < 5 ? foo : 5); */
215 if (__handle_select_assigns(expr
))
217 /* foo = ({frob(); frob(); frob(); 1;}) */
218 if (__handle_expr_statement_assigns(expr
))
221 __split_expr(expr
->right
);
222 if (outside_of_function())
223 __pass_to_client(expr
, GLOBAL_ASSIGNMENT_HOOK
);
225 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
227 __fake_struct_member_assignments(expr
);
229 tmp
= strip_expr(expr
->right
);
230 if (tmp
->type
== EXPR_CALL
)
231 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
232 if (get_macro_name(tmp
->pos
) &&
233 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
234 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
235 __split_expr(expr
->left
);
239 __pass_to_client(expr
, DEREF_HOOK
);
240 __split_expr(expr
->deref
);
243 __split_expr(expr
->base
);
246 case EXPR_FORCE_CAST
:
247 __pass_to_client(expr
, CAST_HOOK
);
248 __split_expr(expr
->cast_expression
);
251 if (expr
->cast_expression
)
252 __pass_to_client(strip_parens(expr
->cast_expression
),
257 evaluate_expression(expr
);
259 case EXPR_CONDITIONAL
:
261 if (known_condition_true(expr
->conditional
)) {
262 __split_expr(expr
->cond_true
);
265 if (known_condition_false(expr
->conditional
)) {
266 __split_expr(expr
->cond_false
);
269 __pass_to_client(expr
, SELECT_HOOK
);
270 __split_whole_condition(expr
->conditional
);
271 __split_expr(expr
->cond_true
);
272 __push_true_states();
273 __use_false_states();
274 __split_expr(expr
->cond_false
);
275 __merge_true_states();
278 if (sym_name_is("__builtin_constant_p", expr
->fn
))
280 split_expr_list(expr
->args
);
281 __split_expr(expr
->fn
);
282 if (is_inline_func(expr
->fn
))
283 add_inline_function(expr
->fn
->symbol
);
284 if (inlinable(expr
->fn
))
286 __process_post_op_stack();
287 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
289 if (inlinable(expr
->fn
)) {
292 __pass_to_client(expr
, CALL_HOOK_AFTER_INLINE
);
293 if (is_noreturn_func(expr
->fn
))
296 case EXPR_INITIALIZER
:
297 split_expr_list(expr
->expr_list
);
299 case EXPR_IDENTIFIER
:
300 __split_expr(expr
->ident_expression
);
303 __split_expr(expr
->idx_expression
);
306 __split_expr(expr
->init_expr
);
309 __pass_to_client(expr
, SYM_HOOK
);
312 __pass_to_client(expr
, STRING_HOOK
);
317 pop_expression(&big_expression_stack
);
320 static int is_forever_loop(struct statement
*stmt
)
322 struct expression
*expr
;
324 expr
= strip_expr(stmt
->iterator_pre_condition
);
326 expr
= stmt
->iterator_post_condition
;
328 /* this is a for(;;) loop... */
332 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
339 static char *get_loop_name(int num
)
343 snprintf(buf
, 255, "-loop%d", num
);
345 return alloc_sname(buf
);
349 * Pre Loops are while and for loops.
351 static void handle_pre_loop(struct statement
*stmt
)
353 int once_through
; /* we go through the loop at least once */
354 struct sm_state
*extra_sm
= NULL
;
357 struct state_list
*slist
= NULL
;
358 struct sm_state
*sm
= NULL
;
360 loop_name
= get_loop_name(loop_num
);
363 __split_stmt(stmt
->iterator_pre_statement
);
365 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
371 __merge_gotos(loop_name
);
373 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
374 __in_pre_condition
++;
375 __pass_to_client(stmt
, PRELOOP_HOOK
);
376 __split_whole_condition(stmt
->iterator_pre_condition
);
377 __in_pre_condition
--;
378 FOR_EACH_PTR(slist
, sm
) {
379 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
380 } END_FOR_EACH_PTR(sm
);
383 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
385 if (option_assume_loops
)
388 __split_stmt(stmt
->iterator_statement
);
389 __warn_on_silly_pre_loops();
390 if (is_forever_loop(stmt
)) {
391 struct state_list
*slist
;
393 __save_gotos(loop_name
);
395 __push_fake_cur_slist();
396 __split_stmt(stmt
->iterator_post_statement
);
397 slist
= __pop_fake_cur_slist();
399 __discard_continues();
400 __discard_false_states();
403 if (!__path_is_null())
404 __merge_slist_into_cur(slist
);
408 unchanged
= __iterator_unchanged(extra_sm
);
409 __split_stmt(stmt
->iterator_post_statement
);
410 __save_gotos(loop_name
);
411 __split_whole_condition(stmt
->iterator_pre_condition
);
413 __merge_false_states();
415 __discard_false_states();
417 __merge_false_states();
419 if (extra_sm
&& unchanged
)
420 __extra_pre_loop_hook_after(extra_sm
,
421 stmt
->iterator_post_statement
,
422 stmt
->iterator_pre_condition
);
429 * Post loops are do {} while();
431 static void handle_post_loop(struct statement
*stmt
)
435 loop_name
= get_loop_name(loop_num
);
441 __merge_gotos(loop_name
);
442 __split_stmt(stmt
->iterator_statement
);
444 if (!is_zero(stmt
->iterator_post_condition
))
445 __save_gotos(loop_name
);
447 if (is_forever_loop(stmt
)) {
450 __split_whole_condition(stmt
->iterator_post_condition
);
451 __use_false_states();
457 static int empty_statement(struct statement
*stmt
)
461 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
466 static int last_stmt_on_same_line()
468 struct statement
*stmt
;
471 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
474 if (stmt
->pos
.line
== get_lineno())
477 } END_FOR_EACH_PTR_REVERSE(stmt
);
481 static struct statement
*last_stmt
;
482 static int is_last_stmt(struct statement
*stmt
)
484 if (stmt
== last_stmt
)
489 static void print_unreached_initializers(struct symbol_list
*sym_list
)
493 FOR_EACH_PTR(sym_list
, sym
) {
494 if (sym
->initializer
)
495 sm_msg("info: '%s' is not actually initialized (unreached code).",
496 (sym
->ident
? sym
->ident
->name
: "this variable"));
497 } END_FOR_EACH_PTR(sym
);
500 static void print_unreached(struct statement
*stmt
)
502 static int print
= 1;
507 if (!__path_is_null()) {
514 switch (stmt
->type
) {
515 case STMT_COMPOUND
: /* after a switch before a case stmt */
520 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
521 print_unreached_initializers(stmt
->declaration
);
523 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
524 if (is_last_stmt(stmt
))
528 /* people put extra breaks inside switch statements */
529 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
&&
530 strcmp(stmt
->goto_label
->ident
->name
, "break") == 0)
536 if (empty_statement(stmt
))
540 sm_msg("info: ignoring unreachable code.");
544 static void split_asm_constraints(struct expression_list
*expr_list
)
546 struct expression
*expr
;
549 FOR_EACH_PTR(expr_list
, expr
) {
551 case 0: /* identifier */
552 case 1: /* constraint */
555 case 2: /* expression */
560 } END_FOR_EACH_PTR(expr
);
563 static int is_case_val(struct statement
*stmt
, sval_t sval
)
567 if (stmt
->type
!= STMT_CASE
)
569 if (!stmt
->case_expression
) {
573 if (!get_value(stmt
->case_expression
, &case_sval
))
575 if (case_sval
.value
== sval
.value
)
580 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
582 struct statement
*tmp
;
584 __split_expr(stmt
->switch_expression
);
586 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
587 __save_switch_states(top_expression(switch_expr_stack
));
592 stmt
= stmt
->switch_statement
;
595 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
597 __push_scope_hooks();
598 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
599 __smatch_lineno
= tmp
->pos
.line
;
600 if (is_case_val(tmp
, sval
)) {
601 __merge_switches(top_expression(switch_expr_stack
),
602 stmt
->case_expression
);
603 __pass_case_to_client(top_expression(switch_expr_stack
),
604 stmt
->case_expression
);
606 if (__path_is_null())
609 if (__path_is_null()) {
613 } END_FOR_EACH_PTR(tmp
);
615 __call_scope_hooks();
616 if (!__pop_default())
617 __merge_switches(top_expression(switch_expr_stack
),
619 __discard_switches();
621 pop_expression(&switch_expr_stack
);
624 void __split_stmt(struct statement
*stmt
)
631 if (out_of_memory() || __bail_on_rest_of_function
) {
632 static char *printed
= NULL
;
634 if (printed
!= cur_func
)
635 sm_msg("Function too hairy. Giving up.");
636 final_pass
= 0; /* turn off sm_msg() from here */
641 add_ptr_list(&big_statement_stack
, stmt
);
642 free_expression_stack(&big_expression_stack
);
643 set_position(stmt
->pos
);
644 print_unreached(stmt
);
645 __pass_to_client(stmt
, STMT_HOOK
);
647 switch (stmt
->type
) {
648 case STMT_DECLARATION
:
649 split_declaration(stmt
->declaration
);
652 __split_expr(stmt
->ret_value
);
653 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
656 case STMT_EXPRESSION
:
657 __split_expr(stmt
->expression
);
659 case STMT_COMPOUND
: {
660 struct statement
*tmp
;
663 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
664 __push_scope_hooks();
665 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
667 } END_FOR_EACH_PTR(tmp
);
668 __call_scope_hooks();
672 if (known_condition_true(stmt
->if_conditional
)) {
673 __split_stmt(stmt
->if_true
);
676 if (known_condition_false(stmt
->if_conditional
)) {
677 __split_stmt(stmt
->if_false
);
680 if (option_known_conditions
&&
681 implied_condition_true(stmt
->if_conditional
)) {
682 sm_info("this condition is true.");
683 __split_stmt(stmt
->if_true
);
686 if (option_known_conditions
&&
687 implied_condition_false(stmt
->if_conditional
)) {
688 sm_info("this condition is false.");
689 __split_stmt(stmt
->if_false
);
692 __split_whole_condition(stmt
->if_conditional
);
693 __split_stmt(stmt
->if_true
);
694 if (empty_statement(stmt
->if_true
) &&
695 last_stmt_on_same_line() &&
696 !get_macro_name(stmt
->if_true
->pos
))
697 sm_msg("warn: if();");
698 __push_true_states();
699 __use_false_states();
700 __split_stmt(stmt
->if_false
);
701 __merge_true_states();
704 if (stmt
->iterator_pre_condition
)
705 handle_pre_loop(stmt
);
706 else if (stmt
->iterator_post_condition
)
707 handle_post_loop(stmt
);
709 // these are for(;;) type loops.
710 handle_pre_loop(stmt
);
714 if (get_value(stmt
->switch_expression
, &sval
)) {
715 split_known_switch(stmt
, sval
);
718 __split_expr(stmt
->switch_expression
);
719 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
720 __save_switch_states(top_expression(switch_expr_stack
));
724 __split_stmt(stmt
->switch_statement
);
725 if (!__pop_default())
726 __merge_switches(top_expression(switch_expr_stack
),
728 __discard_switches();
730 pop_expression(&switch_expr_stack
);
733 __merge_switches(top_expression(switch_expr_stack
),
734 stmt
->case_expression
);
735 __pass_case_to_client(top_expression(switch_expr_stack
),
736 stmt
->case_expression
);
737 if (!stmt
->case_expression
)
739 __split_expr(stmt
->case_expression
);
740 __split_expr(stmt
->case_to
);
741 __split_stmt(stmt
->case_statement
);
744 if (stmt
->label_identifier
&&
745 stmt
->label_identifier
->type
== SYM_LABEL
&&
746 stmt
->label_identifier
->ident
) {
747 loop_count
= 1000000;
748 __merge_gotos(stmt
->label_identifier
->ident
->name
);
750 __split_stmt(stmt
->label_statement
);
753 __split_expr(stmt
->goto_expression
);
754 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
755 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
757 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
759 __process_continues();
761 } else if (stmt
->goto_label
&&
762 stmt
->goto_label
->type
== SYM_LABEL
&&
763 stmt
->goto_label
->ident
) {
764 __save_gotos(stmt
->goto_label
->ident
->name
);
771 __pass_to_client(stmt
, ASM_HOOK
);
772 __split_expr(stmt
->asm_string
);
773 split_asm_constraints(stmt
->asm_outputs
);
774 split_asm_constraints(stmt
->asm_inputs
);
775 split_asm_constraints(stmt
->asm_clobbers
);
780 __split_expr(stmt
->range_expression
);
781 __split_expr(stmt
->range_low
);
782 __split_expr(stmt
->range_high
);
786 __process_post_op_stack();
789 static void split_expr_list(struct expression_list
*expr_list
)
791 struct expression
*expr
;
793 FOR_EACH_PTR(expr_list
, expr
) {
795 __process_post_op_stack();
796 } END_FOR_EACH_PTR(expr
);
799 static void split_sym(struct symbol
*sym
)
803 if (!(sym
->namespace & NS_SYMBOL
))
806 __split_stmt(sym
->stmt
);
807 __split_expr(sym
->array_size
);
808 split_symlist(sym
->arguments
);
809 split_symlist(sym
->symbol_list
);
810 __split_stmt(sym
->inline_stmt
);
811 split_symlist(sym
->inline_symbol_list
);
814 static void split_symlist(struct symbol_list
*sym_list
)
818 FOR_EACH_PTR(sym_list
, sym
) {
820 } END_FOR_EACH_PTR(sym
);
823 typedef void (fake_cb
)(struct expression
*expr
);
825 static int member_to_number(struct expression
*expr
, struct ident
*member
)
827 struct symbol
*type
, *tmp
;
835 type
= get_type(expr
);
836 if (!type
|| type
->type
!= SYM_STRUCT
)
840 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
844 if (strcmp(name
, tmp
->ident
->name
) == 0)
846 } END_FOR_EACH_PTR(tmp
);
850 static struct ident
*number_to_member(struct expression
*expr
, int num
)
852 struct symbol
*type
, *member
;
855 type
= get_type(expr
);
856 if (!type
|| type
->type
!= SYM_STRUCT
)
859 FOR_EACH_PTR(type
->symbol_list
, member
) {
861 return member
->ident
;
863 } END_FOR_EACH_PTR(member
);
867 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
);
874 static struct member_set
*alloc_member_set(struct symbol
*type
)
876 struct member_set
*member_set
;
877 struct symbol
*member
;
881 member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
882 member_set
= malloc(member_count
* sizeof(*member_set
));
884 FOR_EACH_PTR(type
->symbol_list
, member
) {
885 member_set
[member_idx
].ident
= member
->ident
;
886 member_set
[member_idx
].set
= 0;
888 } END_FOR_EACH_PTR(member
);
893 static void mark_member_as_set(struct symbol
*type
, struct member_set
*member_set
, struct ident
*ident
)
895 int member_count
= ptr_list_size((struct ptr_list
*)type
->symbol_list
);
898 for (i
= 0; i
< member_count
; i
++) {
899 if (member_set
[i
].ident
== ident
) {
900 member_set
[i
].set
= 1;
904 // crap. this is buggy.
905 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
908 static void set_unset_to_zero(struct expression
*symbol
, struct symbol
*type
, struct member_set
*member_set
)
910 struct expression
*deref
, *assign
;
911 struct symbol
*member
, *member_type
;
915 FOR_EACH_PTR(type
->symbol_list
, member
) {
916 if (!member
->ident
|| member_set
[member_idx
].set
) {
920 member_type
= get_real_base_type(member
);
921 if (!member_type
|| member_type
->type
== SYM_ARRAY
) {
925 /* TODO: this should be handled recursively and not ignored */
926 if (member_type
->type
== SYM_STRUCT
|| member_type
->type
== SYM_UNION
) {
930 deref
= member_expression(symbol
, '.', member
->ident
);
931 assign
= assign_expression(deref
, zero_expr());
932 __split_expr(assign
);
934 } END_FOR_EACH_PTR(member
);
938 static void fake_member_assigns_helper(struct expression
*symbol
, struct expression_list
*members
, fake_cb
*fake_cb
)
940 struct expression
*deref
, *assign
, *tmp
;
941 struct symbol
*struct_type
, *type
;
942 struct ident
*member
;
944 struct member_set
*member_set
;
946 struct_type
= get_type(symbol
);
948 (struct_type
->type
!= SYM_STRUCT
&& struct_type
->type
!= SYM_UNION
))
951 member_set
= alloc_member_set(struct_type
);
954 FOR_EACH_PTR(members
, tmp
) {
955 member
= number_to_member(symbol
, member_idx
);
956 while (tmp
->type
== EXPR_IDENTIFIER
) {
957 member
= tmp
->expr_ident
;
958 member_idx
= member_to_number(symbol
, member
);
959 tmp
= tmp
->ident_expression
;
961 mark_member_as_set(struct_type
, member_set
, member
);
963 deref
= member_expression(symbol
, '.', member
);
964 if (tmp
->type
== EXPR_INITIALIZER
) {
965 type
= get_type(deref
);
966 if (type
&& type
->type
== SYM_ARRAY
)
967 fake_element_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
969 fake_member_assigns_helper(deref
, tmp
->expr_list
, fake_cb
);
971 assign
= assign_expression(deref
, tmp
);
974 } END_FOR_EACH_PTR(tmp
);
976 set_unset_to_zero(symbol
, struct_type
, member_set
);
979 static void fake_member_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
981 fake_member_assigns_helper(symbol_expression(sym
),
982 sym
->initializer
->expr_list
, fake_cb
);
985 static void fake_element_assigns_helper(struct expression
*array
, struct expression_list
*expr_list
, fake_cb
*fake_cb
)
987 struct expression
*offset
, *binop
, *assign
, *tmp
;
992 FOR_EACH_PTR(expr_list
, tmp
) {
993 if (tmp
->type
== EXPR_INDEX
) {
994 if (tmp
->idx_from
!= tmp
->idx_to
)
997 if (!tmp
->idx_expression
)
999 tmp
= tmp
->idx_expression
;
1001 offset
= value_expr(idx
);
1002 binop
= array_element_expression(array
, offset
);
1003 if (tmp
->type
== EXPR_INITIALIZER
) {
1004 type
= get_type(binop
);
1005 if (type
&& type
->type
== SYM_ARRAY
)
1006 fake_element_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1008 fake_member_assigns_helper(binop
, tmp
->expr_list
, fake_cb
);
1010 assign
= assign_expression(binop
, tmp
);
1015 } END_FOR_EACH_PTR(tmp
);
1018 static void fake_element_assigns(struct symbol
*sym
, fake_cb
*fake_cb
)
1020 fake_element_assigns_helper(symbol_expression(sym
), sym
->initializer
->expr_list
, fake_cb
);
1023 static void fake_assign_expr(struct symbol
*sym
)
1025 struct expression
*assign
, *symbol
;
1027 symbol
= symbol_expression(sym
);
1028 assign
= assign_expression(symbol
, sym
->initializer
);
1029 __split_expr(assign
);
1032 static void call_split_expr(struct expression
*expr
)
1037 static void do_initializer_stuff(struct symbol
*sym
)
1039 if (!sym
->initializer
)
1042 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
1043 if (get_real_base_type(sym
)->type
== SYM_ARRAY
)
1044 fake_element_assigns(sym
, call_split_expr
);
1046 fake_member_assigns(sym
, call_split_expr
);
1048 fake_assign_expr(sym
);
1052 static void split_declaration(struct symbol_list
*sym_list
)
1056 FOR_EACH_PTR(sym_list
, sym
) {
1057 __pass_to_client(sym
, DECLARATION_HOOK
);
1058 do_initializer_stuff(sym
);
1060 } END_FOR_EACH_PTR(sym
);
1063 static void call_global_assign_hooks(struct expression
*assign
)
1065 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1068 static void fake_global_assign(struct symbol
*sym
)
1070 struct expression
*assign
, *symbol
;
1072 if (get_real_base_type(sym
)->type
== SYM_ARRAY
) {
1073 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1074 fake_element_assigns(sym
, call_global_assign_hooks
);
1075 } else if (sym
->initializer
) {
1076 symbol
= symbol_expression(sym
);
1077 assign
= assign_expression(symbol
, sym
->initializer
);
1078 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1080 fake_element_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1082 } else if (get_real_base_type(sym
)->type
== SYM_STRUCT
) {
1083 if (sym
->initializer
&& sym
->initializer
->type
== EXPR_INITIALIZER
) {
1084 fake_member_assigns(sym
, call_global_assign_hooks
);
1085 } else if (sym
->initializer
) {
1086 symbol
= symbol_expression(sym
);
1087 assign
= assign_expression(symbol
, sym
->initializer
);
1088 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1090 fake_member_assigns_helper(symbol_expression(sym
), NULL
, call_global_assign_hooks
);
1093 symbol
= symbol_expression(sym
);
1094 if (sym
->initializer
)
1095 assign
= assign_expression(symbol
, sym
->initializer
);
1097 assign
= assign_expression(symbol
, zero_expr());
1098 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
1102 static void start_function_definition(struct symbol
*sym
)
1104 __in_function_def
= 1;
1105 __pass_to_client(sym
, FUNC_DEF_HOOK
);
1106 __in_function_def
= 0;
1107 __pass_to_client(sym
, AFTER_DEF_HOOK
);
1111 static void split_function(struct symbol
*sym
)
1113 struct symbol
*base_type
= get_base_type(sym
);
1117 cur_func
= sym
->ident
->name
;
1118 __smatch_lineno
= sym
->pos
.line
;
1121 sm_debug("new function: %s\n", cur_func
);
1123 if (option_two_passes
) {
1127 start_function_definition(sym
);
1128 __split_stmt(base_type
->stmt
);
1129 __split_stmt(base_type
->inline_stmt
);
1134 start_function_definition(sym
);
1135 __split_stmt(base_type
->stmt
);
1136 __split_stmt(base_type
->inline_stmt
);
1137 __pass_to_client(sym
, END_FUNC_HOOK
);
1138 __pass_to_client(sym
, AFTER_FUNC_HOOK
);
1139 cur_func_sym
= NULL
;
1142 free_data_info_allocs();
1143 free_expression_stack(&switch_expr_stack
);
1144 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1145 __bail_on_rest_of_function
= 0;
1148 static void parse_inline(struct expression
*call
)
1150 struct symbol
*base_type
;
1151 int loop_num_bak
= loop_num
;
1152 int final_pass_bak
= final_pass
;
1153 char *cur_func_bak
= cur_func
;
1154 struct statement_list
*big_statement_stack_bak
= big_statement_stack
;
1155 struct expression_list
*big_expression_stack_bak
= big_expression_stack
;
1156 struct expression_list
*switch_expr_stack_bak
= switch_expr_stack
;
1157 struct symbol
*cur_func_sym_bak
= cur_func_sym
;
1159 __pass_to_client(call
, INLINE_FN_START
);
1160 final_pass
= 0; /* don't print anything */
1163 base_type
= get_base_type(call
->fn
->symbol
);
1164 cur_func_sym
= call
->fn
->symbol
;
1165 if (call
->fn
->symbol
->ident
)
1166 cur_func
= call
->fn
->symbol
->ident
->name
;
1169 set_position(call
->fn
->symbol
->pos
);
1172 nullify_all_states();
1173 big_statement_stack
= NULL
;
1174 big_expression_stack
= NULL
;
1175 switch_expr_stack
= NULL
;
1177 sm_debug("inline function: %s\n", cur_func
);
1180 start_function_definition(call
->fn
->symbol
);
1181 __split_stmt(base_type
->stmt
);
1182 __split_stmt(base_type
->inline_stmt
);
1183 __pass_to_client(call
->fn
->symbol
, END_FUNC_HOOK
);
1184 __pass_to_client(call
->fn
->symbol
, AFTER_FUNC_HOOK
);
1186 free_expression_stack(&switch_expr_stack
);
1187 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
1190 loop_num
= loop_num_bak
;
1191 final_pass
= final_pass_bak
;
1192 cur_func_sym
= cur_func_sym_bak
;
1193 cur_func
= cur_func_bak
;
1194 big_statement_stack
= big_statement_stack_bak
;
1195 big_expression_stack
= big_expression_stack_bak
;
1196 switch_expr_stack
= switch_expr_stack_bak
;
1198 restore_all_states();
1199 set_position(call
->pos
);
1201 __pass_to_client(call
, INLINE_FN_END
);
1204 static struct symbol_list
*inlines_called
;
1205 static void add_inline_function(struct symbol
*sym
)
1207 static struct symbol_list
*already_added
;
1210 FOR_EACH_PTR(already_added
, tmp
) {
1213 } END_FOR_EACH_PTR(tmp
);
1215 add_ptr_list(&already_added
, sym
);
1216 add_ptr_list(&inlines_called
, sym
);
1219 static void process_inlines()
1223 FOR_EACH_PTR(inlines_called
, tmp
) {
1224 split_function(tmp
);
1225 } END_FOR_EACH_PTR(tmp
);
1226 free_ptr_list(&inlines_called
);
1229 static struct symbol
*get_last_scoped_symbol(struct symbol_list
*big_list
, int use_static
)
1233 FOR_EACH_PTR_REVERSE(big_list
, sym
) {
1236 if (use_static
&& sym
->ctype
.modifiers
& MOD_STATIC
)
1238 if (!use_static
&& !(sym
->ctype
.modifiers
& MOD_STATIC
))
1240 } END_FOR_EACH_PTR_REVERSE(sym
);
1245 static void split_inlines_in_scope(struct symbol
*sym
)
1247 struct symbol
*base
;
1248 struct symbol_list
*scope_list
;
1251 scope_list
= sym
->scope
->symbols
;
1252 stream
= sym
->pos
.stream
;
1254 /* find the last static symbol in the file */
1255 FOR_EACH_PTR_REVERSE(scope_list
, sym
) {
1256 if (sym
->pos
.stream
!= stream
)
1258 if (sym
->type
!= SYM_NODE
)
1260 base
= get_base_type(sym
);
1263 if (base
->type
!= SYM_FN
)
1265 if (!base
->inline_stmt
)
1267 add_inline_function(sym
);
1268 } END_FOR_EACH_PTR_REVERSE(sym
);
1273 static void split_inlines(struct symbol_list
*sym_list
)
1277 sym
= get_last_scoped_symbol(sym_list
, 0);
1279 split_inlines_in_scope(sym
);
1280 sym
= get_last_scoped_symbol(sym_list
, 1);
1282 split_inlines_in_scope(sym
);
1285 static void split_functions(struct symbol_list
*sym_list
)
1289 FOR_EACH_PTR(sym_list
, sym
) {
1290 set_position(sym
->pos
);
1291 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
1292 split_function(sym
);
1295 __pass_to_client(sym
, BASE_HOOK
);
1296 fake_global_assign(sym
);
1298 } END_FOR_EACH_PTR(sym
);
1299 split_inlines(sym_list
);
1300 __pass_to_client(sym_list
, END_FILE_HOOK
);
1303 void smatch(int argc
, char **argv
)
1306 struct string_list
*filelist
= NULL
;
1307 struct symbol_list
*sym_list
;
1310 printf("Usage: smatch [--debug] <filename.c>\n");
1313 sparse_initialize(argc
, argv
, &filelist
);
1314 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
1315 if (option_file_output
) {
1318 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
1319 sm_outfd
= fopen(buf
, "w");
1321 printf("Error: Cannot open %s\n", base_file
);
1325 sym_list
= sparse_keep_tokens(base_file
);
1326 split_functions(sym_list
);
1327 } END_FOR_EACH_PTR_NOTAG(base_file
);