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 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
163 __split_expr(expr
->left
);
167 __pass_to_client(expr
, DEREF_HOOK
);
168 __split_expr(expr
->deref
);
171 __split_expr(expr
->base
);
174 case EXPR_FORCE_CAST
:
175 __pass_to_client(expr
, CAST_HOOK
);
176 __split_expr(expr
->cast_expression
);
179 /* there isn't anything to pass a client from inside a sizeof() */
183 evaluate_expression(expr
);
185 case EXPR_CONDITIONAL
:
187 if (known_condition_true(expr
->conditional
)) {
188 __split_expr(expr
->cond_true
);
191 if (known_condition_false(expr
->conditional
)) {
192 __split_expr(expr
->cond_false
);
195 __pass_to_client(expr
, SELECT_HOOK
);
196 __split_whole_condition(expr
->conditional
);
197 __split_expr(expr
->cond_true
);
198 __push_true_states();
199 __use_false_states();
200 __split_expr(expr
->cond_false
);
201 __merge_true_states();
204 if (sym_name_is("__builtin_constant_p", expr
->fn
))
206 split_expr_list(expr
->args
);
207 __split_expr(expr
->fn
);
208 if (is_inline_func(expr
->fn
))
209 add_inline_function(expr
->fn
->symbol
);
210 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
211 if (is_noreturn_func(expr
->fn
))
214 case EXPR_INITIALIZER
:
215 split_expr_list(expr
->expr_list
);
217 case EXPR_IDENTIFIER
:
218 __split_expr(expr
->ident_expression
);
221 __split_expr(expr
->idx_expression
);
224 __split_expr(expr
->init_expr
);
227 __pass_to_client(expr
, SYM_HOOK
);
230 __pass_to_client(expr
, STRING_HOOK
);
235 pop_expression(&big_expression_stack
);
238 static int is_forever_loop(struct statement
*stmt
)
240 struct expression
*expr
;
242 expr
= strip_expr(stmt
->iterator_pre_condition
);
244 expr
= stmt
->iterator_post_condition
;
246 /* this is a for(;;) loop... */
250 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
257 static char *get_loop_name(int num
)
261 snprintf(buf
, 255, "-loop%d", num
);
263 return alloc_sname(buf
);
267 * Pre Loops are while and for loops.
269 static void handle_pre_loop(struct statement
*stmt
)
271 int once_through
; /* we go through the loop at least once */
272 struct sm_state
*extra_sm
= NULL
;
275 struct state_list
*slist
= NULL
;
276 struct sm_state
*sm
= NULL
;
278 loop_name
= get_loop_name(loop_num
);
281 __split_stmt(stmt
->iterator_pre_statement
);
283 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
289 __merge_gotos(loop_name
);
291 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
292 __in_pre_condition
++;
293 __pass_to_client(stmt
, PRELOOP_HOOK
);
294 __split_whole_condition(stmt
->iterator_pre_condition
);
295 __in_pre_condition
--;
296 FOR_EACH_PTR(slist
, sm
) {
297 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
298 } END_FOR_EACH_PTR(sm
);
301 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
303 if (option_assume_loops
)
306 __split_stmt(stmt
->iterator_statement
);
307 __warn_on_silly_pre_loops();
308 if (is_forever_loop(stmt
)) {
309 struct state_list
*slist
;
311 __save_gotos(loop_name
);
313 __push_fake_cur_slist();
314 __split_stmt(stmt
->iterator_post_statement
);
315 slist
= __pop_fake_cur_slist();
317 __discard_continues();
318 __discard_false_states();
321 if (!__path_is_null())
322 __merge_slist_into_cur(slist
);
326 unchanged
= __iterator_unchanged(extra_sm
);
327 __split_stmt(stmt
->iterator_post_statement
);
328 __save_gotos(loop_name
);
329 __split_whole_condition(stmt
->iterator_pre_condition
);
331 __merge_false_states();
333 __discard_false_states();
335 __merge_false_states();
337 if (extra_sm
&& unchanged
)
338 __extra_pre_loop_hook_after(extra_sm
,
339 stmt
->iterator_post_statement
,
340 stmt
->iterator_pre_condition
);
347 * Post loops are do {} while();
349 static void handle_post_loop(struct statement
*stmt
)
353 loop_name
= get_loop_name(loop_num
);
359 __merge_gotos(loop_name
);
360 __split_stmt(stmt
->iterator_statement
);
362 if (!is_zero(stmt
->iterator_post_condition
))
363 __save_gotos(loop_name
);
365 if (is_forever_loop(stmt
)) {
368 __split_whole_condition(stmt
->iterator_post_condition
);
369 __use_false_states();
375 static int empty_statement(struct statement
*stmt
)
379 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
384 static int last_stmt_on_same_line()
386 struct statement
*stmt
;
389 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
392 if (stmt
->pos
.line
== get_lineno())
395 } END_FOR_EACH_PTR_REVERSE(stmt
);
399 static struct statement
*last_stmt
;
400 static int is_last_stmt(struct statement
*stmt
)
402 if (stmt
== last_stmt
)
407 static void print_unreached_initializers(struct symbol_list
*sym_list
)
411 FOR_EACH_PTR(sym_list
, sym
) {
412 if (sym
->initializer
)
413 sm_msg("info: '%s' is not actually initialized (unreached code).",
414 (sym
->ident
? sym
->ident
->name
: "this variable"));
415 } END_FOR_EACH_PTR(sym
);
418 static void print_unreached(struct statement
*stmt
)
420 static int print
= 1;
422 if (!__path_is_null()) {
429 switch (stmt
->type
) {
430 case STMT_COMPOUND
: /* after a switch before a case stmt */
435 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
436 print_unreached_initializers(stmt
->declaration
);
438 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
439 if (is_last_stmt(stmt
))
449 if (!option_spammy
&& empty_statement(stmt
))
451 sm_msg("info: ignoring unreachable code.");
455 static void split_asm_constraints(struct expression_list
*expr_list
)
457 struct expression
*expr
;
460 FOR_EACH_PTR(expr_list
, expr
) {
462 case 0: /* identifier */
463 case 1: /* constraint */
466 case 2: /* expression */
471 } END_FOR_EACH_PTR(expr
);
474 static int is_case_val(struct statement
*stmt
, sval_t sval
)
478 if (stmt
->type
!= STMT_CASE
)
480 if (!stmt
->case_expression
) {
484 if (!get_value_sval(stmt
->case_expression
, &case_sval
))
486 if (case_sval
.value
== sval
.value
)
491 static void split_known_switch(struct statement
*stmt
, sval_t sval
)
493 struct statement
*tmp
;
495 __split_expr(stmt
->switch_expression
);
497 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
498 __save_switch_states(top_expression(switch_expr_stack
));
503 stmt
= stmt
->switch_statement
;
506 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
508 __push_scope_hooks();
509 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
510 __smatch_lineno
= tmp
->pos
.line
;
511 if (is_case_val(tmp
, sval
)) {
512 __merge_switches(top_expression(switch_expr_stack
),
513 stmt
->case_expression
);
514 __pass_case_to_client(top_expression(switch_expr_stack
),
515 stmt
->case_expression
);
517 if (__path_is_null())
520 if (__path_is_null()) {
524 } END_FOR_EACH_PTR(tmp
);
526 __call_scope_hooks();
527 if (!__pop_default())
528 __merge_switches(top_expression(switch_expr_stack
),
530 __discard_switches();
532 pop_expression(&switch_expr_stack
);
535 void __split_stmt(struct statement
*stmt
)
542 if (out_of_memory() || __bail_on_rest_of_function
) {
543 static char *printed
= NULL
;
545 if (printed
!= cur_func
)
546 sm_msg("Function too hairy. Giving up.");
547 final_pass
= 0; /* turn off sm_msg() from here */
552 add_ptr_list(&big_statement_stack
, stmt
);
553 free_expression_stack(&big_expression_stack
);
554 set_position(stmt
->pos
);
555 print_unreached(stmt
);
556 __pass_to_client(stmt
, STMT_HOOK
);
558 switch (stmt
->type
) {
559 case STMT_DECLARATION
:
560 split_declaration(stmt
->declaration
);
563 __split_expr(stmt
->ret_value
);
564 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
567 case STMT_EXPRESSION
:
568 __split_expr(stmt
->expression
);
570 case STMT_COMPOUND
: {
571 struct statement
*tmp
;
574 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
575 __push_scope_hooks();
576 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
578 } END_FOR_EACH_PTR(tmp
);
579 __call_scope_hooks();
583 if (known_condition_true(stmt
->if_conditional
)) {
584 __split_stmt(stmt
->if_true
);
587 if (known_condition_false(stmt
->if_conditional
)) {
588 __split_stmt(stmt
->if_false
);
591 if (option_known_conditions
&&
592 implied_condition_true(stmt
->if_conditional
)) {
593 sm_info("this condition is true.");
594 __split_stmt(stmt
->if_true
);
597 if (option_known_conditions
&&
598 implied_condition_false(stmt
->if_conditional
)) {
599 sm_info("this condition is false.");
600 __split_stmt(stmt
->if_false
);
603 __split_whole_condition(stmt
->if_conditional
);
604 __split_stmt(stmt
->if_true
);
605 if (empty_statement(stmt
->if_true
) &&
606 last_stmt_on_same_line() &&
607 !get_macro_name(stmt
->if_true
->pos
))
608 sm_msg("warn: if();");
609 __push_true_states();
610 __use_false_states();
611 __split_stmt(stmt
->if_false
);
612 __merge_true_states();
615 if (stmt
->iterator_pre_condition
)
616 handle_pre_loop(stmt
);
617 else if (stmt
->iterator_post_condition
)
618 handle_post_loop(stmt
);
620 // these are for(;;) type loops.
621 handle_pre_loop(stmt
);
625 if (get_value_sval(stmt
->switch_expression
, &sval
)) {
626 split_known_switch(stmt
, sval
);
629 __split_expr(stmt
->switch_expression
);
630 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
631 __save_switch_states(top_expression(switch_expr_stack
));
635 __split_stmt(stmt
->switch_statement
);
636 if (!__pop_default())
637 __merge_switches(top_expression(switch_expr_stack
),
639 __discard_switches();
641 pop_expression(&switch_expr_stack
);
644 __merge_switches(top_expression(switch_expr_stack
),
645 stmt
->case_expression
);
646 __pass_case_to_client(top_expression(switch_expr_stack
),
647 stmt
->case_expression
);
648 if (!stmt
->case_expression
)
650 __split_expr(stmt
->case_expression
);
651 __split_expr(stmt
->case_to
);
652 __split_stmt(stmt
->case_statement
);
655 if (stmt
->label_identifier
&&
656 stmt
->label_identifier
->type
== SYM_LABEL
&&
657 stmt
->label_identifier
->ident
) {
658 loop_count
= 1000000;
659 __merge_gotos(stmt
->label_identifier
->ident
->name
);
661 __split_stmt(stmt
->label_statement
);
664 __split_expr(stmt
->goto_expression
);
665 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
666 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
668 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
670 __process_continues();
672 } else if (stmt
->goto_label
&&
673 stmt
->goto_label
->type
== SYM_LABEL
&&
674 stmt
->goto_label
->ident
) {
675 __save_gotos(stmt
->goto_label
->ident
->name
);
682 __pass_to_client(stmt
, ASM_HOOK
);
683 __split_expr(stmt
->asm_string
);
684 split_asm_constraints(stmt
->asm_outputs
);
685 split_asm_constraints(stmt
->asm_inputs
);
686 split_asm_constraints(stmt
->asm_clobbers
);
691 __split_expr(stmt
->range_expression
);
692 __split_expr(stmt
->range_low
);
693 __split_expr(stmt
->range_high
);
698 static void split_expr_list(struct expression_list
*expr_list
)
700 struct expression
*expr
;
702 FOR_EACH_PTR(expr_list
, expr
) {
704 } END_FOR_EACH_PTR(expr
);
707 static void split_sym(struct symbol
*sym
)
711 if (!(sym
->namespace & NS_SYMBOL
))
714 __split_stmt(sym
->stmt
);
715 __split_expr(sym
->array_size
);
716 split_symlist(sym
->arguments
);
717 split_symlist(sym
->symbol_list
);
718 __split_stmt(sym
->inline_stmt
);
719 split_symlist(sym
->inline_symbol_list
);
722 static void split_symlist(struct symbol_list
*sym_list
)
726 FOR_EACH_PTR(sym_list
, sym
) {
728 } END_FOR_EACH_PTR(sym
);
731 static void fake_member_assigns(struct symbol
*sym
)
733 struct expression
*symbol
, *deref
, *assign
, *tmp
;
735 symbol
= symbol_expression(sym
);
736 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
737 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
739 deref
= deref_expression(symbol
, '.', tmp
->expr_ident
);
740 assign
= assign_expression(deref
, tmp
->ident_expression
);
741 __split_expr(assign
);
742 } END_FOR_EACH_PTR(tmp
);
745 static void fake_assign_expr(struct symbol
*sym
)
747 struct expression
*assign
, *symbol
;
749 symbol
= symbol_expression(sym
);
750 assign
= assign_expression(symbol
, sym
->initializer
);
751 __split_expr(assign
);
754 static void do_initializer_stuff(struct symbol
*sym
)
756 if (!sym
->initializer
)
758 if (sym
->initializer
->type
== EXPR_INITIALIZER
)
759 fake_member_assigns(sym
);
761 fake_assign_expr(sym
);
764 static void split_declaration(struct symbol_list
*sym_list
)
768 FOR_EACH_PTR(sym_list
, sym
) {
769 __pass_to_client(sym
, DECLARATION_HOOK
);
770 do_initializer_stuff(sym
);
772 } END_FOR_EACH_PTR(sym
);
775 static void split_function(struct symbol
*sym
)
777 struct symbol
*base_type
= get_base_type(sym
);
781 line_func_start
= base_type
->stmt
->pos
.line
;
783 cur_func
= sym
->ident
->name
;
784 __smatch_lineno
= sym
->pos
.line
;
787 sm_debug("new function: %s\n", cur_func
);
789 if (option_two_passes
) {
793 __pass_to_client(sym
, FUNC_DEF_HOOK
);
794 __split_stmt(base_type
->stmt
);
795 __split_stmt(base_type
->inline_stmt
);
801 __pass_to_client(sym
, FUNC_DEF_HOOK
);
802 __split_stmt(base_type
->stmt
);
803 __split_stmt(base_type
->inline_stmt
);
804 __pass_to_client(sym
, END_FUNC_HOOK
);
808 free_data_info_allocs();
809 free_expression_stack(&switch_expr_stack
);
810 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
811 __bail_on_rest_of_function
= 0;
814 static struct symbol_list
*inlines_called
;
815 static void add_inline_function(struct symbol
*sym
)
817 static struct symbol_list
*already_added
;
820 FOR_EACH_PTR(already_added
, tmp
) {
823 } END_FOR_EACH_PTR(tmp
);
825 add_ptr_list(&already_added
, sym
);
826 add_ptr_list(&inlines_called
, sym
);
829 static void process_inlines()
833 FOR_EACH_PTR(inlines_called
, tmp
) {
835 } END_FOR_EACH_PTR(tmp
);
836 free_ptr_list(&inlines_called
);
839 static void split_functions(struct symbol_list
*sym_list
)
843 FOR_EACH_PTR(sym_list
, sym
) {
844 set_position(sym
->pos
);
845 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
849 __pass_to_client(sym
, BASE_HOOK
);
851 } END_FOR_EACH_PTR(sym
);
852 __pass_to_client_no_data(END_FILE_HOOK
);
855 void smatch(int argc
, char **argv
)
858 struct string_list
*filelist
= NULL
;
859 struct symbol_list
*sym_list
;
862 printf("Usage: smatch [--debug] <filename.c>\n");
865 sparse_initialize(argc
, argv
, &filelist
);
866 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
867 if (option_file_output
) {
870 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
871 sm_outfd
= fopen(buf
, "w");
873 printf("Error: Cannot open %s\n", base_file
);
877 sym_list
= sparse_keep_tokens(base_file
);
878 split_functions(sym_list
);
879 } END_FOR_EACH_PTR_NOTAG(base_file
);