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 const char *filename
;
24 static char *pathname
;
25 static char *full_filename
;
26 static char *cur_func
;
27 static int line_func_start
;
28 static int loop_count
;
29 int __expr_stmt_count
;
30 static struct expression_list
*switch_expr_stack
= NULL
;
32 struct expression_list
*big_expression_stack
;
33 struct statement_list
*big_statement_stack
;
34 int __in_pre_condition
= 0;
35 int __bail_on_rest_of_function
= 0;
36 char *get_function(void) { return cur_func
; }
37 int get_lineno(void) { return __smatch_lineno
; }
38 int get_func_pos(void) { return __smatch_lineno
- line_func_start
; }
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)
59 static void set_position(struct expression
*expr
)
62 static int prev_stream
= -1;
64 __smatch_lineno
= expr
->pos
.line
;
66 if (expr
->pos
.stream
== prev_stream
)
69 filename
= stream_name(expr
->pos
.stream
);
72 pathname
= getcwd(NULL
, 0);
74 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
75 full_filename
= malloc(len
);
76 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
78 full_filename
= alloc_string(filename
);
83 static int is_inline_func(struct expression
*expr
)
85 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
87 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
92 void __split_expr(struct expression
*expr
)
97 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
99 push_expression(&big_expression_stack
, expr
);
101 __pass_to_client(expr
, EXPR_HOOK
);
103 switch (expr
->type
) {
106 __pass_to_client(expr
, DEREF_HOOK
);
108 __pass_to_client(expr
, OP_HOOK
);
109 __split_expr(expr
->unop
);
113 __split_stmt(expr
->statement
);
118 __pass_to_client(expr
, LOGIC_HOOK
);
119 __handle_logic(expr
);
122 __pass_to_client(expr
, BINOP_HOOK
);
124 __split_expr(expr
->left
);
125 __split_expr(expr
->right
);
127 case EXPR_ASSIGNMENT
: {
128 struct expression
*tmp
;
130 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
133 if (__handle_condition_assigns(expr
))
135 /* foo = (x < 5 ? foo : 5); */
136 if (__handle_select_assigns(expr
))
138 /* foo = ({frob(); frob(); frob(); 1;}) */
139 if (__handle_expr_statement_assigns(expr
))
142 __split_expr(expr
->right
);
143 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
144 tmp
= strip_expr(expr
->right
);
145 if (tmp
->type
== EXPR_CALL
)
146 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
147 if (get_macro_name(&tmp
->pos
))
148 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
149 __split_expr(expr
->left
);
153 __pass_to_client(expr
, DEREF_HOOK
);
154 __split_expr(expr
->deref
);
157 __split_expr(expr
->base
);
160 case EXPR_FORCE_CAST
:
161 __split_expr(expr
->cast_expression
);
164 /* there isn't anything to pass a client from inside a sizeof() */
166 case EXPR_CONDITIONAL
:
168 __pass_to_client(expr
, SELECT_HOOK
);
169 __split_whole_condition(expr
->conditional
);
170 __split_expr(expr
->cond_true
);
171 __push_true_states();
172 __use_false_states();
173 __split_expr(expr
->cond_false
);
174 __merge_true_states();
177 split_expr_list(expr
->args
);
178 __split_expr(expr
->fn
);
179 if (is_inline_func(expr
->fn
))
180 add_inline_function(expr
->fn
->symbol
);
181 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
183 case EXPR_INITIALIZER
:
184 split_expr_list(expr
->expr_list
);
186 case EXPR_IDENTIFIER
:
187 __split_expr(expr
->ident_expression
);
190 __split_expr(expr
->idx_expression
);
193 __split_expr(expr
->init_expr
);
196 __pass_to_client(expr
, SYM_HOOK
);
199 __pass_to_client(expr
, STRING_HOOK
);
204 pop_expression(&big_expression_stack
);
207 static int is_forever_loop(struct statement
*stmt
)
210 struct expression
*expr
;
212 expr
= strip_expr(stmt
->iterator_pre_condition
);
214 expr
= stmt
->iterator_post_condition
;
216 /* this is a for(;;) loop... */
220 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
228 static char *get_loop_name(int num
)
232 snprintf(buf
, 255, "-loop%d", num
);
234 return alloc_sname(buf
);;
238 * Pre Loops are while and for loops.
241 static void handle_pre_loop(struct statement
*stmt
)
243 int once_through
; /* we go through the loop at least once */
244 struct sm_state
*extra_sm
= NULL
;
247 struct state_list
*slist
= NULL
;
248 struct sm_state
*sm
= NULL
;
250 loop_name
= get_loop_name(loop_num
);
253 __split_stmt(stmt
->iterator_pre_statement
);
255 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
261 __merge_gotos(loop_name
);
263 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
264 __in_pre_condition
++;
265 __split_whole_condition(stmt
->iterator_pre_condition
);
266 __in_pre_condition
--;
267 FOR_EACH_PTR(slist
, sm
) {
268 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
269 } END_FOR_EACH_PTR(sm
);
272 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
274 if (option_assume_loops
)
277 __split_stmt(stmt
->iterator_statement
);
278 __warn_on_silly_pre_loops();
279 if (is_forever_loop(stmt
)) {
280 __save_gotos(loop_name
);
281 /* forever loops don't have an iterator_post_statement */
282 __discard_continues();
283 __discard_false_states();
287 unchanged
= __iterator_unchanged(extra_sm
);
288 __split_stmt(stmt
->iterator_post_statement
);
289 __save_gotos(loop_name
);
290 __split_whole_condition(stmt
->iterator_pre_condition
);
292 __merge_false_states();
294 __discard_false_states();
296 __merge_false_states();
299 if (extra_sm
&& unchanged
)
300 __extra_pre_loop_hook_after(extra_sm
,
301 stmt
->iterator_post_statement
,
302 stmt
->iterator_pre_condition
);
309 * Post loops are do {} while();
311 static void handle_post_loop(struct statement
*stmt
)
315 loop_name
= get_loop_name(loop_num
);
321 __merge_gotos(loop_name
);
322 __split_stmt(stmt
->iterator_statement
);
324 if (!is_zero(stmt
->iterator_post_condition
))
325 __save_gotos(loop_name
);
327 if (is_forever_loop(stmt
)) {
330 __split_whole_condition(stmt
->iterator_post_condition
);
331 __use_false_states();
337 static int empty_statement(struct statement
*stmt
)
341 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
346 static int last_stmt_on_same_line()
348 struct statement
*stmt
;
351 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
354 if (stmt
->pos
.line
== get_lineno())
357 } END_FOR_EACH_PTR_REVERSE(stmt
);
361 static struct statement
*last_stmt
;
362 static int is_last_stmt(struct statement
*stmt
)
364 if (stmt
== last_stmt
)
369 static void print_unreached_initializers(struct symbol_list
*sym_list
)
373 FOR_EACH_PTR(sym_list
, sym
) {
375 sm_msg("info: '%s' is not actually initialized (unreached code).",
376 (sym
->ident
? sym
->ident
->name
: "this variable"));
377 } END_FOR_EACH_PTR(sym
);
380 static void print_unreached(struct statement
*stmt
)
383 static int print
= 1;
385 if (!__path_is_null()) {
392 switch (stmt
->type
) {
393 case STMT_COMPOUND
: /* after a switch before a case stmt */
398 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
399 print_unreached_initializers(stmt
->declaration
);
401 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
402 if (is_last_stmt(stmt
))
412 if (!option_spammy
&& empty_statement(stmt
))
414 sm_msg("info: ignoring unreachable code.");
418 static void split_asm_constraints(struct expression_list
*expr_list
)
420 struct expression
*expr
;
423 FOR_EACH_PTR(expr_list
, expr
) {
425 case 0: /* identifier */
426 case 1: /* constraint */
429 case 2: /* expression */
434 } END_FOR_EACH_PTR(expr
);
437 static int is_case_val(struct statement
*stmt
, long long val
)
441 if (stmt
->type
!= STMT_CASE
)
443 if (!stmt
->case_expression
) {
447 if (!get_value(stmt
->case_expression
, &case_val
))
454 static void split_known_switch(struct statement
*stmt
, long long val
)
456 struct statement
*tmp
;
458 __split_expr(stmt
->switch_expression
);
460 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
461 __save_switch_states(top_expression(switch_expr_stack
));
466 stmt
= stmt
->switch_statement
;
469 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
471 __push_scope_hooks();
472 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
473 __smatch_lineno
= tmp
->pos
.line
;
474 if (is_case_val(tmp
, val
)) {
475 __merge_switches(top_expression(switch_expr_stack
),
476 stmt
->case_expression
);
477 __pass_case_to_client(top_expression(switch_expr_stack
),
478 stmt
->case_expression
);
480 if (__path_is_null())
483 if (__path_is_null()) {
487 } END_FOR_EACH_PTR(tmp
);
489 __call_scope_hooks();
490 if (!__pop_default())
491 __merge_switches(top_expression(switch_expr_stack
),
493 __discard_switches();
495 pop_expression(&switch_expr_stack
);
498 void __split_stmt(struct statement
*stmt
)
505 if (out_of_memory() || __bail_on_rest_of_function
) {
506 static char *printed
= NULL
;
508 if (printed
!= cur_func
)
509 sm_msg("Function too hairy. Giving up.");
514 add_ptr_list(&big_statement_stack
, stmt
);
515 free_expression_stack(&big_expression_stack
);
516 __smatch_lineno
= stmt
->pos
.line
;
517 print_unreached(stmt
);
518 __pass_to_client(stmt
, STMT_HOOK
);
520 switch (stmt
->type
) {
521 case STMT_DECLARATION
:
522 split_declaration(stmt
->declaration
);
525 __split_expr(stmt
->ret_value
);
526 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
529 case STMT_EXPRESSION
:
530 __split_expr(stmt
->expression
);
532 case STMT_COMPOUND
: {
533 struct statement
*tmp
;
536 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
537 __push_scope_hooks();
538 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
540 } END_FOR_EACH_PTR(tmp
);
541 __call_scope_hooks();
545 if (known_condition_true(stmt
->if_conditional
)) {
546 __split_stmt(stmt
->if_true
);
549 if (known_condition_false(stmt
->if_conditional
)) {
550 __split_stmt(stmt
->if_false
);
553 if (option_known_conditions
&&
554 implied_condition_true(stmt
->if_conditional
)) {
555 sm_info("this condition is true.");
556 __split_stmt(stmt
->if_true
);
559 if (option_known_conditions
&&
560 implied_condition_false(stmt
->if_conditional
)) {
561 sm_info("this condition is false.");
562 __split_stmt(stmt
->if_false
);
565 __split_whole_condition(stmt
->if_conditional
);
566 __split_stmt(stmt
->if_true
);
567 if (empty_statement(stmt
->if_true
) &&
568 last_stmt_on_same_line() &&
569 !get_macro_name(&stmt
->if_true
->pos
))
570 sm_msg("warn: if();");
571 __push_true_states();
572 __use_false_states();
573 __split_stmt(stmt
->if_false
);
574 __merge_true_states();
577 if (stmt
->iterator_pre_condition
)
578 handle_pre_loop(stmt
);
579 else if (stmt
->iterator_post_condition
)
580 handle_post_loop(stmt
);
582 // these are for(;;) type loops.
583 handle_pre_loop(stmt
);
587 if (get_value(stmt
->switch_expression
, &val
)) {
588 split_known_switch(stmt
, val
);
591 __split_expr(stmt
->switch_expression
);
592 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
593 __save_switch_states(top_expression(switch_expr_stack
));
597 __split_stmt(stmt
->switch_statement
);
598 if (!__pop_default())
599 __merge_switches(top_expression(switch_expr_stack
),
601 __discard_switches();
603 pop_expression(&switch_expr_stack
);
606 __merge_switches(top_expression(switch_expr_stack
),
607 stmt
->case_expression
);
608 __pass_case_to_client(top_expression(switch_expr_stack
),
609 stmt
->case_expression
);
610 if (!stmt
->case_expression
)
612 __split_expr(stmt
->case_expression
);
613 __split_expr(stmt
->case_to
);
614 __split_stmt(stmt
->case_statement
);
618 stmt
->label
->type
== SYM_LABEL
&&
619 stmt
->label
->ident
) {
620 loop_count
= 1000000;
621 __merge_gotos(stmt
->label
->ident
->name
);
623 __split_stmt(stmt
->label_statement
);
626 __split_expr(stmt
->goto_expression
);
627 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
628 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
630 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
632 __process_continues();
634 } else if (stmt
->goto_label
&&
635 stmt
->goto_label
->type
== SYM_LABEL
&&
636 stmt
->goto_label
->ident
) {
637 __save_gotos(stmt
->goto_label
->ident
->name
);
644 __pass_to_client(stmt
, ASM_HOOK
);
645 __split_expr(stmt
->asm_string
);
646 split_asm_constraints(stmt
->asm_outputs
);
647 split_asm_constraints(stmt
->asm_inputs
);
648 split_asm_constraints(stmt
->asm_clobbers
);
653 __split_expr(stmt
->range_expression
);
654 __split_expr(stmt
->range_low
);
655 __split_expr(stmt
->range_high
);
660 static void split_expr_list(struct expression_list
*expr_list
)
662 struct expression
*expr
;
664 FOR_EACH_PTR(expr_list
, expr
) {
666 } END_FOR_EACH_PTR(expr
);
670 static void split_sym(struct symbol
*sym
)
674 if (!(sym
->namespace & NS_SYMBOL
))
677 __split_stmt(sym
->stmt
);
678 __split_expr(sym
->array_size
);
679 split_symlist(sym
->arguments
);
680 split_symlist(sym
->symbol_list
);
681 __split_stmt(sym
->inline_stmt
);
682 split_symlist(sym
->inline_symbol_list
);
685 static void split_symlist(struct symbol_list
*sym_list
)
689 FOR_EACH_PTR(sym_list
, sym
) {
691 } END_FOR_EACH_PTR(sym
);
694 static struct expression
*fake_assign_expr(struct symbol
*sym
)
696 struct expression
*e_assign
, *e_symbol
;
698 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
699 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
700 e_assign
->op
= (int)'=';
701 e_symbol
->symbol
= sym
;
702 e_symbol
->symbol_name
= sym
->ident
;
703 e_assign
->left
= e_symbol
;
704 e_assign
->right
= sym
->initializer
;
708 static void do_initializer_stuff(struct symbol
*sym
)
710 struct expression
*assign
;
712 if(!sym
->initializer
)
714 assign
= fake_assign_expr(sym
);
715 __split_expr(assign
);
718 static void split_declaration(struct symbol_list
*sym_list
)
722 FOR_EACH_PTR(sym_list
, sym
) {
723 __pass_to_client(sym
, DECLARATION_HOOK
);
724 do_initializer_stuff(sym
);
726 } END_FOR_EACH_PTR(sym
);
729 static void split_function(struct symbol
*sym
)
731 struct symbol
*base_type
= get_base_type(sym
);
735 line_func_start
= base_type
->stmt
->pos
.line
;
737 cur_func
= sym
->ident
->name
;
738 __smatch_lineno
= sym
->pos
.line
;
741 sm_debug("new function: %s\n", cur_func
);
742 if (option_two_passes
) {
746 __pass_to_client(sym
, FUNC_DEF_HOOK
);
747 __split_stmt(base_type
->stmt
);
748 __split_stmt(base_type
->inline_stmt
);
754 __pass_to_client(sym
, FUNC_DEF_HOOK
);
755 __split_stmt(base_type
->stmt
);
756 __split_stmt(base_type
->inline_stmt
);
757 __pass_to_client(sym
, END_FUNC_HOOK
);
761 free_data_info_allocs();
762 free_expression_stack(&switch_expr_stack
);
763 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
764 __bail_on_rest_of_function
= 0;
767 static struct symbol_list
*inlines_called
;
768 static void add_inline_function(struct symbol
*sym
)
770 static struct symbol_list
*already_added
;
773 FOR_EACH_PTR(already_added
, tmp
) {
776 } END_FOR_EACH_PTR(tmp
);
778 add_ptr_list(&already_added
, sym
);
779 add_ptr_list(&inlines_called
, sym
);
782 static void process_inlines()
786 FOR_EACH_PTR(inlines_called
, tmp
) {
788 } END_FOR_EACH_PTR(tmp
);
789 free_ptr_list(&inlines_called
);
792 static void split_functions(struct symbol_list
*sym_list
)
796 FOR_EACH_PTR(sym_list
, sym
) {
797 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
801 __pass_to_client(sym
, BASE_HOOK
);
803 } END_FOR_EACH_PTR(sym
);
804 __pass_to_client_no_data(END_FILE_HOOK
);
807 void smatch (int argc
, char **argv
)
810 struct string_list
*filelist
= NULL
;
811 struct symbol_list
*sym_list
;
815 printf("Usage: smatch [--debug] <filename.c>\n");
818 sparse_initialize(argc
, argv
, &filelist
);
819 FOR_EACH_PTR_NOTAG(filelist
, file
) {
820 sym_list
= sparse_keep_tokens(file
);
821 split_functions(sym_list
);
822 } END_FOR_EACH_PTR_NOTAG(file
);