4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h"
21 static int __smatch_lineno
= 0;
23 static char *base_file
;
24 static const char *filename
;
25 static char *pathname
;
26 static char *full_filename
;
27 static char *cur_func
;
28 static int line_func_start
;
29 static int loop_count
;
30 int __expr_stmt_count
;
31 static struct expression_list
*switch_expr_stack
= NULL
;
33 struct expression_list
*big_expression_stack
;
34 struct statement_list
*big_statement_stack
;
35 int __in_pre_condition
= 0;
36 int __bail_on_rest_of_function
= 0;
37 char *get_function(void) { return cur_func
; }
38 int get_lineno(void) { return __smatch_lineno
; }
39 int inside_loop(void) { return !!loop_count
; }
40 int in_expression_statement(void) { return !!__expr_stmt_count
; }
42 static void split_symlist(struct symbol_list
*sym_list
);
43 static void split_declaration(struct symbol_list
*sym_list
);
44 static void split_expr_list(struct expression_list
*expr_list
);
45 static void add_inline_function(struct symbol
*sym
);
47 int option_assume_loops
= 0;
48 int option_known_conditions
= 0;
49 int option_two_passes
= 0;
50 struct symbol
*cur_func_sym
= NULL
;
52 const char *get_filename(void)
61 static void set_position(struct position pos
)
64 static int prev_stream
= -1;
66 __smatch_lineno
= pos
.line
;
68 if (pos
.stream
== prev_stream
)
71 filename
= stream_name(pos
.stream
);
74 pathname
= getcwd(NULL
, 0);
76 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
77 full_filename
= malloc(len
);
78 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
80 full_filename
= alloc_string(filename
);
85 static int is_inline_func(struct expression
*expr
)
87 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
89 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
94 static int is_noreturn_func(struct expression
*expr
)
96 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
98 if (expr
->symbol
->ctype
.modifiers
& MOD_NORETURN
)
103 void __split_expr(struct expression
*expr
)
108 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
110 push_expression(&big_expression_stack
, expr
);
111 set_position(expr
->pos
);
112 __pass_to_client(expr
, EXPR_HOOK
);
114 switch (expr
->type
) {
117 __pass_to_client(expr
, DEREF_HOOK
);
119 __pass_to_client(expr
, OP_HOOK
);
120 __split_expr(expr
->unop
);
124 __split_stmt(expr
->statement
);
129 __pass_to_client(expr
, LOGIC_HOOK
);
130 __handle_logic(expr
);
133 __pass_to_client(expr
, BINOP_HOOK
);
135 __split_expr(expr
->left
);
136 __split_expr(expr
->right
);
138 case EXPR_ASSIGNMENT
: {
139 struct expression
*tmp
;
144 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
147 if (__handle_condition_assigns(expr
))
149 /* foo = (x < 5 ? foo : 5); */
150 if (__handle_select_assigns(expr
))
152 /* foo = ({frob(); frob(); frob(); 1;}) */
153 if (__handle_expr_statement_assigns(expr
))
156 __split_expr(expr
->right
);
157 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
158 tmp
= strip_expr(expr
->right
);
159 if (tmp
->type
== EXPR_CALL
)
160 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
161 if (get_macro_name(tmp
->pos
) &&
162 get_macro_name(expr
->pos
) != get_macro_name(tmp
->pos
))
163 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
164 __split_expr(expr
->left
);
168 __pass_to_client(expr
, DEREF_HOOK
);
169 __split_expr(expr
->deref
);
172 __split_expr(expr
->base
);
175 case EXPR_FORCE_CAST
:
176 __pass_to_client(expr
, CAST_HOOK
);
177 __split_expr(expr
->cast_expression
);
180 /* there isn't anything to pass a client from inside a sizeof() */
184 evaluate_expression(expr
);
186 case EXPR_CONDITIONAL
:
188 if (known_condition_true(expr
->conditional
)) {
189 __split_expr(expr
->cond_true
);
192 if (known_condition_false(expr
->conditional
)) {
193 __split_expr(expr
->cond_false
);
196 __pass_to_client(expr
, SELECT_HOOK
);
197 __split_whole_condition(expr
->conditional
);
198 __split_expr(expr
->cond_true
);
199 __push_true_states();
200 __use_false_states();
201 __split_expr(expr
->cond_false
);
202 __merge_true_states();
205 if (sym_name_is("__builtin_constant_p", expr
->fn
))
207 split_expr_list(expr
->args
);
208 __split_expr(expr
->fn
);
209 if (is_inline_func(expr
->fn
))
210 add_inline_function(expr
->fn
->symbol
);
211 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
212 if (is_noreturn_func(expr
->fn
))
215 case EXPR_INITIALIZER
:
216 split_expr_list(expr
->expr_list
);
218 case EXPR_IDENTIFIER
:
219 __split_expr(expr
->ident_expression
);
222 __split_expr(expr
->idx_expression
);
225 __split_expr(expr
->init_expr
);
228 __pass_to_client(expr
, SYM_HOOK
);
231 __pass_to_client(expr
, STRING_HOOK
);
236 pop_expression(&big_expression_stack
);
239 static int is_forever_loop(struct statement
*stmt
)
241 struct expression
*expr
;
243 expr
= strip_expr(stmt
->iterator_pre_condition
);
245 expr
= stmt
->iterator_post_condition
;
247 /* this is a for(;;) loop... */
251 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
258 static char *get_loop_name(int num
)
262 snprintf(buf
, 255, "-loop%d", num
);
264 return alloc_sname(buf
);
268 * Pre Loops are while and for loops.
270 static void handle_pre_loop(struct statement
*stmt
)
272 int once_through
; /* we go through the loop at least once */
273 struct sm_state
*extra_sm
= NULL
;
276 struct state_list
*slist
= NULL
;
277 struct sm_state
*sm
= NULL
;
279 loop_name
= get_loop_name(loop_num
);
282 __split_stmt(stmt
->iterator_pre_statement
);
284 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
290 __merge_gotos(loop_name
);
292 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
293 __in_pre_condition
++;
294 __pass_to_client(stmt
, PRELOOP_HOOK
);
295 __split_whole_condition(stmt
->iterator_pre_condition
);
296 __in_pre_condition
--;
297 FOR_EACH_PTR(slist
, sm
) {
298 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
299 } END_FOR_EACH_PTR(sm
);
302 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
304 if (option_assume_loops
)
307 __split_stmt(stmt
->iterator_statement
);
308 __warn_on_silly_pre_loops();
309 if (is_forever_loop(stmt
)) {
310 struct state_list
*slist
;
312 __save_gotos(loop_name
);
314 __push_fake_cur_slist();
315 __split_stmt(stmt
->iterator_post_statement
);
316 slist
= __pop_fake_cur_slist();
318 __discard_continues();
319 __discard_false_states();
322 if (!__path_is_null())
323 __merge_slist_into_cur(slist
);
327 unchanged
= __iterator_unchanged(extra_sm
);
328 __split_stmt(stmt
->iterator_post_statement
);
329 __save_gotos(loop_name
);
330 __split_whole_condition(stmt
->iterator_pre_condition
);
332 __merge_false_states();
334 __discard_false_states();
336 __merge_false_states();
338 if (extra_sm
&& unchanged
)
339 __extra_pre_loop_hook_after(extra_sm
,
340 stmt
->iterator_post_statement
,
341 stmt
->iterator_pre_condition
);
348 * Post loops are do {} while();
350 static void handle_post_loop(struct statement
*stmt
)
354 loop_name
= get_loop_name(loop_num
);
360 __merge_gotos(loop_name
);
361 __split_stmt(stmt
->iterator_statement
);
363 if (!is_zero(stmt
->iterator_post_condition
))
364 __save_gotos(loop_name
);
366 if (is_forever_loop(stmt
)) {
369 __split_whole_condition(stmt
->iterator_post_condition
);
370 __use_false_states();
376 static int empty_statement(struct statement
*stmt
)
380 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
385 static int last_stmt_on_same_line()
387 struct statement
*stmt
;
390 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
393 if (stmt
->pos
.line
== get_lineno())
396 } END_FOR_EACH_PTR_REVERSE(stmt
);
400 static struct statement
*last_stmt
;
401 static int is_last_stmt(struct statement
*stmt
)
403 if (stmt
== last_stmt
)
408 static void print_unreached_initializers(struct symbol_list
*sym_list
)
412 FOR_EACH_PTR(sym_list
, sym
) {
413 if (sym
->initializer
)
414 sm_msg("info: '%s' is not actually initialized (unreached code).",
415 (sym
->ident
? sym
->ident
->name
: "this variable"));
416 } END_FOR_EACH_PTR(sym
);
419 static void print_unreached(struct statement
*stmt
)
421 static int print
= 1;
423 if (!__path_is_null()) {
430 switch (stmt
->type
) {
431 case STMT_COMPOUND
: /* after a switch before a case stmt */
436 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
437 print_unreached_initializers(stmt
->declaration
);
439 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
440 if (is_last_stmt(stmt
))
450 if (!option_spammy
&& empty_statement(stmt
))
452 sm_msg("info: ignoring unreachable code.");
456 static void split_asm_constraints(struct expression_list
*expr_list
)
458 struct expression
*expr
;
461 FOR_EACH_PTR(expr_list
, expr
) {
463 case 0: /* identifier */
464 case 1: /* constraint */
467 case 2: /* expression */
472 } END_FOR_EACH_PTR(expr
);
475 static int is_case_val(struct statement
*stmt
, sval_t sval
)
479 if (stmt
->type
!= STMT_CASE
)
481 if (!stmt
->case_expression
) {
485 if (!get_value(stmt
->case_expression
, &case_sval
))
487 if (case_sval
.value
== sval
.value
)
492 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
494 struct statement
*tmp
;
496 __split_expr(stmt
->switch_expression
);
498 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
499 __save_switch_states(top_expression(switch_expr_stack
));
504 stmt
= stmt
->switch_statement
;
507 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
509 __push_scope_hooks();
510 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
511 __smatch_lineno
= tmp
->pos
.line
;
512 if (is_case_val(tmp
, sval
)) {
513 __merge_switches(top_expression(switch_expr_stack
),
514 stmt
->case_expression
);
515 __pass_case_to_client(top_expression(switch_expr_stack
),
516 stmt
->case_expression
);
518 if (__path_is_null())
521 if (__path_is_null()) {
525 } END_FOR_EACH_PTR(tmp
);
527 __call_scope_hooks();
528 if (!__pop_default())
529 __merge_switches(top_expression(switch_expr_stack
),
531 __discard_switches();
533 pop_expression(&switch_expr_stack
);
536 void __split_stmt(struct statement
*stmt
)
543 if (out_of_memory() || __bail_on_rest_of_function
) {
544 static char *printed
= NULL
;
546 if (printed
!= cur_func
)
547 sm_msg("Function too hairy. Giving up.");
548 final_pass
= 0; /* turn off sm_msg() from here */
553 add_ptr_list(&big_statement_stack
, stmt
);
554 free_expression_stack(&big_expression_stack
);
555 set_position(stmt
->pos
);
556 print_unreached(stmt
);
557 __pass_to_client(stmt
, STMT_HOOK
);
559 switch (stmt
->type
) {
560 case STMT_DECLARATION
:
561 split_declaration(stmt
->declaration
);
564 __split_expr(stmt
->ret_value
);
565 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
568 case STMT_EXPRESSION
:
569 __split_expr(stmt
->expression
);
571 case STMT_COMPOUND
: {
572 struct statement
*tmp
;
575 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
576 __push_scope_hooks();
577 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
579 } END_FOR_EACH_PTR(tmp
);
580 __call_scope_hooks();
584 if (known_condition_true(stmt
->if_conditional
)) {
585 __split_stmt(stmt
->if_true
);
588 if (known_condition_false(stmt
->if_conditional
)) {
589 __split_stmt(stmt
->if_false
);
592 if (option_known_conditions
&&
593 implied_condition_true(stmt
->if_conditional
)) {
594 sm_info("this condition is true.");
595 __split_stmt(stmt
->if_true
);
598 if (option_known_conditions
&&
599 implied_condition_false(stmt
->if_conditional
)) {
600 sm_info("this condition is false.");
601 __split_stmt(stmt
->if_false
);
604 __split_whole_condition(stmt
->if_conditional
);
605 __split_stmt(stmt
->if_true
);
606 if (empty_statement(stmt
->if_true
) &&
607 last_stmt_on_same_line() &&
608 !get_macro_name(stmt
->if_true
->pos
))
609 sm_msg("warn: if();");
610 __push_true_states();
611 __use_false_states();
612 __split_stmt(stmt
->if_false
);
613 __merge_true_states();
616 if (stmt
->iterator_pre_condition
)
617 handle_pre_loop(stmt
);
618 else if (stmt
->iterator_post_condition
)
619 handle_post_loop(stmt
);
621 // these are for(;;) type loops.
622 handle_pre_loop(stmt
);
626 if (get_value(stmt
->switch_expression
, &sval
)) {
627 split_known_switch(stmt
, sval
);
630 __split_expr(stmt
->switch_expression
);
631 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
632 __save_switch_states(top_expression(switch_expr_stack
));
636 __split_stmt(stmt
->switch_statement
);
637 if (!__pop_default())
638 __merge_switches(top_expression(switch_expr_stack
),
640 __discard_switches();
642 pop_expression(&switch_expr_stack
);
645 __merge_switches(top_expression(switch_expr_stack
),
646 stmt
->case_expression
);
647 __pass_case_to_client(top_expression(switch_expr_stack
),
648 stmt
->case_expression
);
649 if (!stmt
->case_expression
)
651 __split_expr(stmt
->case_expression
);
652 __split_expr(stmt
->case_to
);
653 __split_stmt(stmt
->case_statement
);
656 if (stmt
->label_identifier
&&
657 stmt
->label_identifier
->type
== SYM_LABEL
&&
658 stmt
->label_identifier
->ident
) {
659 loop_count
= 1000000;
660 __merge_gotos(stmt
->label_identifier
->ident
->name
);
662 __split_stmt(stmt
->label_statement
);
665 __split_expr(stmt
->goto_expression
);
666 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
667 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
669 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
671 __process_continues();
673 } else if (stmt
->goto_label
&&
674 stmt
->goto_label
->type
== SYM_LABEL
&&
675 stmt
->goto_label
->ident
) {
676 __save_gotos(stmt
->goto_label
->ident
->name
);
683 __pass_to_client(stmt
, ASM_HOOK
);
684 __split_expr(stmt
->asm_string
);
685 split_asm_constraints(stmt
->asm_outputs
);
686 split_asm_constraints(stmt
->asm_inputs
);
687 split_asm_constraints(stmt
->asm_clobbers
);
692 __split_expr(stmt
->range_expression
);
693 __split_expr(stmt
->range_low
);
694 __split_expr(stmt
->range_high
);
699 static void split_expr_list(struct expression_list
*expr_list
)
701 struct expression
*expr
;
703 FOR_EACH_PTR(expr_list
, expr
) {
705 } END_FOR_EACH_PTR(expr
);
708 static void split_sym(struct symbol
*sym
)
712 if (!(sym
->namespace & NS_SYMBOL
))
715 __split_stmt(sym
->stmt
);
716 __split_expr(sym
->array_size
);
717 split_symlist(sym
->arguments
);
718 split_symlist(sym
->symbol_list
);
719 __split_stmt(sym
->inline_stmt
);
720 split_symlist(sym
->inline_symbol_list
);
723 static void split_symlist(struct symbol_list
*sym_list
)
727 FOR_EACH_PTR(sym_list
, sym
) {
729 } END_FOR_EACH_PTR(sym
);
732 static void fake_member_assigns(struct symbol
*sym
)
734 struct expression
*symbol
, *deref
, *assign
, *tmp
;
736 symbol
= symbol_expression(sym
);
737 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
738 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
740 deref
= deref_expression(symbol
, '.', tmp
->expr_ident
);
741 assign
= assign_expression(deref
, tmp
->ident_expression
);
742 __split_expr(assign
);
743 } END_FOR_EACH_PTR(tmp
);
746 static void fake_assign_expr(struct symbol
*sym
)
748 struct expression
*assign
, *symbol
;
750 symbol
= symbol_expression(sym
);
751 assign
= assign_expression(symbol
, sym
->initializer
);
752 __split_expr(assign
);
755 static void do_initializer_stuff(struct symbol
*sym
)
757 if (!sym
->initializer
)
759 if (sym
->initializer
->type
== EXPR_INITIALIZER
)
760 fake_member_assigns(sym
);
762 fake_assign_expr(sym
);
765 static void split_declaration(struct symbol_list
*sym_list
)
769 FOR_EACH_PTR(sym_list
, sym
) {
770 __pass_to_client(sym
, DECLARATION_HOOK
);
771 do_initializer_stuff(sym
);
773 } END_FOR_EACH_PTR(sym
);
776 static void fake_global_assign(struct symbol
*sym
)
778 struct expression
*assign
, *symbol
;
780 if (!sym
->initializer
)
782 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
783 struct expression
*deref
, *tmp
;
785 symbol
= symbol_expression(sym
);
786 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
787 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
789 deref
= deref_expression(symbol
, '.', tmp
->expr_ident
);
790 assign
= assign_expression(deref
, tmp
->ident_expression
);
791 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
792 } END_FOR_EACH_PTR(tmp
);
794 symbol
= symbol_expression(sym
);
795 assign
= assign_expression(symbol
, sym
->initializer
);
796 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
800 static void split_function(struct symbol
*sym
)
802 struct symbol
*base_type
= get_base_type(sym
);
806 line_func_start
= base_type
->stmt
->pos
.line
;
808 cur_func
= sym
->ident
->name
;
809 __smatch_lineno
= sym
->pos
.line
;
812 sm_debug("new function: %s\n", cur_func
);
814 if (option_two_passes
) {
818 __pass_to_client(sym
, FUNC_DEF_HOOK
);
819 __split_stmt(base_type
->stmt
);
820 __split_stmt(base_type
->inline_stmt
);
826 __pass_to_client(sym
, FUNC_DEF_HOOK
);
827 __split_stmt(base_type
->stmt
);
828 __split_stmt(base_type
->inline_stmt
);
829 __pass_to_client(sym
, END_FUNC_HOOK
);
833 free_data_info_allocs();
834 free_expression_stack(&switch_expr_stack
);
835 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
836 __bail_on_rest_of_function
= 0;
839 static struct symbol_list
*inlines_called
;
840 static void add_inline_function(struct symbol
*sym
)
842 static struct symbol_list
*already_added
;
845 FOR_EACH_PTR(already_added
, tmp
) {
848 } END_FOR_EACH_PTR(tmp
);
850 add_ptr_list(&already_added
, sym
);
851 add_ptr_list(&inlines_called
, sym
);
854 static void process_inlines()
858 FOR_EACH_PTR(inlines_called
, tmp
) {
860 } END_FOR_EACH_PTR(tmp
);
861 free_ptr_list(&inlines_called
);
864 static void split_functions(struct symbol_list
*sym_list
)
868 FOR_EACH_PTR(sym_list
, sym
) {
869 set_position(sym
->pos
);
870 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
874 __pass_to_client(sym
, BASE_HOOK
);
875 fake_global_assign(sym
);
877 } END_FOR_EACH_PTR(sym
);
878 __pass_to_client_no_data(END_FILE_HOOK
);
881 void smatch(int argc
, char **argv
)
884 struct string_list
*filelist
= NULL
;
885 struct symbol_list
*sym_list
;
888 printf("Usage: smatch [--debug] <filename.c>\n");
891 sparse_initialize(argc
, argv
, &filelist
);
892 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
893 if (option_file_output
) {
896 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
897 sm_outfd
= fopen(buf
, "w");
899 printf("Error: Cannot open %s\n", base_file
);
903 sym_list
= sparse_keep_tokens(base_file
);
904 split_functions(sym_list
);
905 } END_FOR_EACH_PTR_NOTAG(base_file
);