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 if (expr
->cast_expression
)
181 __pass_to_client(strip_parens(expr
->cast_expression
),
186 evaluate_expression(expr
);
188 case EXPR_CONDITIONAL
:
190 if (known_condition_true(expr
->conditional
)) {
191 __split_expr(expr
->cond_true
);
194 if (known_condition_false(expr
->conditional
)) {
195 __split_expr(expr
->cond_false
);
198 __pass_to_client(expr
, SELECT_HOOK
);
199 __split_whole_condition(expr
->conditional
);
200 __split_expr(expr
->cond_true
);
201 __push_true_states();
202 __use_false_states();
203 __split_expr(expr
->cond_false
);
204 __merge_true_states();
207 if (sym_name_is("__builtin_constant_p", expr
->fn
))
209 split_expr_list(expr
->args
);
210 __split_expr(expr
->fn
);
211 if (is_inline_func(expr
->fn
))
212 add_inline_function(expr
->fn
->symbol
);
213 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
214 if (is_noreturn_func(expr
->fn
))
217 case EXPR_INITIALIZER
:
218 split_expr_list(expr
->expr_list
);
220 case EXPR_IDENTIFIER
:
221 __split_expr(expr
->ident_expression
);
224 __split_expr(expr
->idx_expression
);
227 __split_expr(expr
->init_expr
);
230 __pass_to_client(expr
, SYM_HOOK
);
233 __pass_to_client(expr
, STRING_HOOK
);
238 pop_expression(&big_expression_stack
);
241 static int is_forever_loop(struct statement
*stmt
)
243 struct expression
*expr
;
245 expr
= strip_expr(stmt
->iterator_pre_condition
);
247 expr
= stmt
->iterator_post_condition
;
249 /* this is a for(;;) loop... */
253 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
260 static char *get_loop_name(int num
)
264 snprintf(buf
, 255, "-loop%d", num
);
266 return alloc_sname(buf
);
270 * Pre Loops are while and for loops.
272 static void handle_pre_loop(struct statement
*stmt
)
274 int once_through
; /* we go through the loop at least once */
275 struct sm_state
*extra_sm
= NULL
;
278 struct state_list
*slist
= NULL
;
279 struct sm_state
*sm
= NULL
;
281 loop_name
= get_loop_name(loop_num
);
284 __split_stmt(stmt
->iterator_pre_statement
);
286 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
292 __merge_gotos(loop_name
);
294 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
295 __in_pre_condition
++;
296 __pass_to_client(stmt
, PRELOOP_HOOK
);
297 __split_whole_condition(stmt
->iterator_pre_condition
);
298 __in_pre_condition
--;
299 FOR_EACH_PTR(slist
, sm
) {
300 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
301 } END_FOR_EACH_PTR(sm
);
304 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
306 if (option_assume_loops
)
309 __split_stmt(stmt
->iterator_statement
);
310 __warn_on_silly_pre_loops();
311 if (is_forever_loop(stmt
)) {
312 struct state_list
*slist
;
314 __save_gotos(loop_name
);
316 __push_fake_cur_slist();
317 __split_stmt(stmt
->iterator_post_statement
);
318 slist
= __pop_fake_cur_slist();
320 __discard_continues();
321 __discard_false_states();
324 if (!__path_is_null())
325 __merge_slist_into_cur(slist
);
329 unchanged
= __iterator_unchanged(extra_sm
);
330 __split_stmt(stmt
->iterator_post_statement
);
331 __save_gotos(loop_name
);
332 __split_whole_condition(stmt
->iterator_pre_condition
);
334 __merge_false_states();
336 __discard_false_states();
338 __merge_false_states();
340 if (extra_sm
&& unchanged
)
341 __extra_pre_loop_hook_after(extra_sm
,
342 stmt
->iterator_post_statement
,
343 stmt
->iterator_pre_condition
);
350 * Post loops are do {} while();
352 static void handle_post_loop(struct statement
*stmt
)
356 loop_name
= get_loop_name(loop_num
);
362 __merge_gotos(loop_name
);
363 __split_stmt(stmt
->iterator_statement
);
365 if (!is_zero(stmt
->iterator_post_condition
))
366 __save_gotos(loop_name
);
368 if (is_forever_loop(stmt
)) {
371 __split_whole_condition(stmt
->iterator_post_condition
);
372 __use_false_states();
378 static int empty_statement(struct statement
*stmt
)
382 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
387 static int last_stmt_on_same_line()
389 struct statement
*stmt
;
392 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
395 if (stmt
->pos
.line
== get_lineno())
398 } END_FOR_EACH_PTR_REVERSE(stmt
);
402 static struct statement
*last_stmt
;
403 static int is_last_stmt(struct statement
*stmt
)
405 if (stmt
== last_stmt
)
410 static void print_unreached_initializers(struct symbol_list
*sym_list
)
414 FOR_EACH_PTR(sym_list
, sym
) {
415 if (sym
->initializer
)
416 sm_msg("info: '%s' is not actually initialized (unreached code).",
417 (sym
->ident
? sym
->ident
->name
: "this variable"));
418 } END_FOR_EACH_PTR(sym
);
421 static void print_unreached(struct statement
*stmt
)
423 static int print
= 1;
425 if (!__path_is_null()) {
432 switch (stmt
->type
) {
433 case STMT_COMPOUND
: /* after a switch before a case stmt */
438 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
439 print_unreached_initializers(stmt
->declaration
);
441 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
442 if (is_last_stmt(stmt
))
452 if (!option_spammy
&& empty_statement(stmt
))
454 sm_msg("info: ignoring unreachable code.");
458 static void split_asm_constraints(struct expression_list
*expr_list
)
460 struct expression
*expr
;
463 FOR_EACH_PTR(expr_list
, expr
) {
465 case 0: /* identifier */
466 case 1: /* constraint */
469 case 2: /* expression */
474 } END_FOR_EACH_PTR(expr
);
477 static int is_case_val(struct statement
*stmt
, sval_t sval
)
481 if (stmt
->type
!= STMT_CASE
)
483 if (!stmt
->case_expression
) {
487 if (!get_value(stmt
->case_expression
, &case_sval
))
489 if (case_sval
.value
== sval
.value
)
494 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
496 struct statement
*tmp
;
498 __split_expr(stmt
->switch_expression
);
500 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
501 __save_switch_states(top_expression(switch_expr_stack
));
506 stmt
= stmt
->switch_statement
;
509 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
511 __push_scope_hooks();
512 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
513 __smatch_lineno
= tmp
->pos
.line
;
514 if (is_case_val(tmp
, sval
)) {
515 __merge_switches(top_expression(switch_expr_stack
),
516 stmt
->case_expression
);
517 __pass_case_to_client(top_expression(switch_expr_stack
),
518 stmt
->case_expression
);
520 if (__path_is_null())
523 if (__path_is_null()) {
527 } END_FOR_EACH_PTR(tmp
);
529 __call_scope_hooks();
530 if (!__pop_default())
531 __merge_switches(top_expression(switch_expr_stack
),
533 __discard_switches();
535 pop_expression(&switch_expr_stack
);
538 void __split_stmt(struct statement
*stmt
)
545 if (out_of_memory() || __bail_on_rest_of_function
) {
546 static char *printed
= NULL
;
548 if (printed
!= cur_func
)
549 sm_msg("Function too hairy. Giving up.");
550 final_pass
= 0; /* turn off sm_msg() from here */
555 add_ptr_list(&big_statement_stack
, stmt
);
556 free_expression_stack(&big_expression_stack
);
557 set_position(stmt
->pos
);
558 print_unreached(stmt
);
559 __pass_to_client(stmt
, STMT_HOOK
);
561 switch (stmt
->type
) {
562 case STMT_DECLARATION
:
563 split_declaration(stmt
->declaration
);
566 __split_expr(stmt
->ret_value
);
567 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
570 case STMT_EXPRESSION
:
571 __split_expr(stmt
->expression
);
573 case STMT_COMPOUND
: {
574 struct statement
*tmp
;
577 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
578 __push_scope_hooks();
579 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
581 } END_FOR_EACH_PTR(tmp
);
582 __call_scope_hooks();
586 if (known_condition_true(stmt
->if_conditional
)) {
587 __split_stmt(stmt
->if_true
);
590 if (known_condition_false(stmt
->if_conditional
)) {
591 __split_stmt(stmt
->if_false
);
594 if (option_known_conditions
&&
595 implied_condition_true(stmt
->if_conditional
)) {
596 sm_info("this condition is true.");
597 __split_stmt(stmt
->if_true
);
600 if (option_known_conditions
&&
601 implied_condition_false(stmt
->if_conditional
)) {
602 sm_info("this condition is false.");
603 __split_stmt(stmt
->if_false
);
606 __split_whole_condition(stmt
->if_conditional
);
607 __split_stmt(stmt
->if_true
);
608 if (empty_statement(stmt
->if_true
) &&
609 last_stmt_on_same_line() &&
610 !get_macro_name(stmt
->if_true
->pos
))
611 sm_msg("warn: if();");
612 __push_true_states();
613 __use_false_states();
614 __split_stmt(stmt
->if_false
);
615 __merge_true_states();
618 if (stmt
->iterator_pre_condition
)
619 handle_pre_loop(stmt
);
620 else if (stmt
->iterator_post_condition
)
621 handle_post_loop(stmt
);
623 // these are for(;;) type loops.
624 handle_pre_loop(stmt
);
628 if (get_value(stmt
->switch_expression
, &sval
)) {
629 split_known_switch(stmt
, sval
);
632 __split_expr(stmt
->switch_expression
);
633 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
634 __save_switch_states(top_expression(switch_expr_stack
));
638 __split_stmt(stmt
->switch_statement
);
639 if (!__pop_default())
640 __merge_switches(top_expression(switch_expr_stack
),
642 __discard_switches();
644 pop_expression(&switch_expr_stack
);
647 __merge_switches(top_expression(switch_expr_stack
),
648 stmt
->case_expression
);
649 __pass_case_to_client(top_expression(switch_expr_stack
),
650 stmt
->case_expression
);
651 if (!stmt
->case_expression
)
653 __split_expr(stmt
->case_expression
);
654 __split_expr(stmt
->case_to
);
655 __split_stmt(stmt
->case_statement
);
658 if (stmt
->label_identifier
&&
659 stmt
->label_identifier
->type
== SYM_LABEL
&&
660 stmt
->label_identifier
->ident
) {
661 loop_count
= 1000000;
662 __merge_gotos(stmt
->label_identifier
->ident
->name
);
664 __split_stmt(stmt
->label_statement
);
667 __split_expr(stmt
->goto_expression
);
668 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
669 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
671 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
673 __process_continues();
675 } else if (stmt
->goto_label
&&
676 stmt
->goto_label
->type
== SYM_LABEL
&&
677 stmt
->goto_label
->ident
) {
678 __save_gotos(stmt
->goto_label
->ident
->name
);
685 __pass_to_client(stmt
, ASM_HOOK
);
686 __split_expr(stmt
->asm_string
);
687 split_asm_constraints(stmt
->asm_outputs
);
688 split_asm_constraints(stmt
->asm_inputs
);
689 split_asm_constraints(stmt
->asm_clobbers
);
694 __split_expr(stmt
->range_expression
);
695 __split_expr(stmt
->range_low
);
696 __split_expr(stmt
->range_high
);
701 static void split_expr_list(struct expression_list
*expr_list
)
703 struct expression
*expr
;
705 FOR_EACH_PTR(expr_list
, expr
) {
707 } END_FOR_EACH_PTR(expr
);
710 static void split_sym(struct symbol
*sym
)
714 if (!(sym
->namespace & NS_SYMBOL
))
717 __split_stmt(sym
->stmt
);
718 __split_expr(sym
->array_size
);
719 split_symlist(sym
->arguments
);
720 split_symlist(sym
->symbol_list
);
721 __split_stmt(sym
->inline_stmt
);
722 split_symlist(sym
->inline_symbol_list
);
725 static void split_symlist(struct symbol_list
*sym_list
)
729 FOR_EACH_PTR(sym_list
, sym
) {
731 } END_FOR_EACH_PTR(sym
);
734 static void fake_member_assigns(struct symbol
*sym
)
736 struct expression
*symbol
, *deref
, *assign
, *tmp
;
738 symbol
= symbol_expression(sym
);
739 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
740 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
742 deref
= deref_expression(symbol
, '.', tmp
->expr_ident
);
743 assign
= assign_expression(deref
, tmp
->ident_expression
);
744 __split_expr(assign
);
745 } END_FOR_EACH_PTR(tmp
);
748 static void fake_assign_expr(struct symbol
*sym
)
750 struct expression
*assign
, *symbol
;
752 symbol
= symbol_expression(sym
);
753 assign
= assign_expression(symbol
, sym
->initializer
);
754 __split_expr(assign
);
757 static void do_initializer_stuff(struct symbol
*sym
)
759 if (!sym
->initializer
)
761 if (sym
->initializer
->type
== EXPR_INITIALIZER
)
762 fake_member_assigns(sym
);
764 fake_assign_expr(sym
);
767 static void split_declaration(struct symbol_list
*sym_list
)
771 FOR_EACH_PTR(sym_list
, sym
) {
772 __pass_to_client(sym
, DECLARATION_HOOK
);
773 do_initializer_stuff(sym
);
775 } END_FOR_EACH_PTR(sym
);
778 static void fake_global_assign(struct symbol
*sym
)
780 struct expression
*assign
, *symbol
;
782 if (!sym
->initializer
)
784 if (sym
->initializer
->type
== EXPR_INITIALIZER
) {
785 struct expression
*deref
, *tmp
;
787 symbol
= symbol_expression(sym
);
788 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
789 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
791 deref
= deref_expression(symbol
, '.', tmp
->expr_ident
);
792 assign
= assign_expression(deref
, tmp
->ident_expression
);
793 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
794 } END_FOR_EACH_PTR(tmp
);
796 symbol
= symbol_expression(sym
);
797 assign
= assign_expression(symbol
, sym
->initializer
);
798 __pass_to_client(assign
, GLOBAL_ASSIGNMENT_HOOK
);
802 static void split_function(struct symbol
*sym
)
804 struct symbol
*base_type
= get_base_type(sym
);
808 line_func_start
= base_type
->stmt
->pos
.line
;
810 cur_func
= sym
->ident
->name
;
811 __smatch_lineno
= sym
->pos
.line
;
814 sm_debug("new function: %s\n", cur_func
);
816 if (option_two_passes
) {
820 __pass_to_client(sym
, FUNC_DEF_HOOK
);
821 __pass_to_client(sym
, AFTER_DEF_HOOK
);
822 __split_stmt(base_type
->stmt
);
823 __split_stmt(base_type
->inline_stmt
);
829 __pass_to_client(sym
, FUNC_DEF_HOOK
);
830 __pass_to_client(sym
, AFTER_DEF_HOOK
);
831 __split_stmt(base_type
->stmt
);
832 __split_stmt(base_type
->inline_stmt
);
833 __pass_to_client(sym
, END_FUNC_HOOK
);
837 free_data_info_allocs();
838 free_expression_stack(&switch_expr_stack
);
839 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
840 __bail_on_rest_of_function
= 0;
843 static struct symbol_list
*inlines_called
;
844 static void add_inline_function(struct symbol
*sym
)
846 static struct symbol_list
*already_added
;
849 FOR_EACH_PTR(already_added
, tmp
) {
852 } END_FOR_EACH_PTR(tmp
);
854 add_ptr_list(&already_added
, sym
);
855 add_ptr_list(&inlines_called
, sym
);
858 static void process_inlines()
862 FOR_EACH_PTR(inlines_called
, tmp
) {
864 } END_FOR_EACH_PTR(tmp
);
865 free_ptr_list(&inlines_called
);
868 static void split_functions(struct symbol_list
*sym_list
)
872 FOR_EACH_PTR(sym_list
, sym
) {
873 set_position(sym
->pos
);
874 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
878 __pass_to_client(sym
, BASE_HOOK
);
879 fake_global_assign(sym
);
881 } END_FOR_EACH_PTR(sym
);
882 __pass_to_client_no_data(END_FILE_HOOK
);
885 void smatch(int argc
, char **argv
)
888 struct string_list
*filelist
= NULL
;
889 struct symbol_list
*sym_list
;
892 printf("Usage: smatch [--debug] <filename.c>\n");
895 sparse_initialize(argc
, argv
, &filelist
);
896 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
897 if (option_file_output
) {
900 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
901 sm_outfd
= fopen(buf
, "w");
903 printf("Error: Cannot open %s\n", base_file
);
907 sym_list
= sparse_keep_tokens(base_file
);
908 split_functions(sym_list
);
909 } END_FOR_EACH_PTR_NOTAG(base_file
);