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 position pos
)
62 static int prev_stream
= -1;
64 __smatch_lineno
= pos
.line
;
66 if (pos
.stream
== prev_stream
)
69 filename
= stream_name(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
);
100 set_position(expr
->pos
);
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
)
209 struct expression
*expr
;
211 expr
= strip_expr(stmt
->iterator_pre_condition
);
213 expr
= stmt
->iterator_post_condition
;
215 /* this is a for(;;) loop... */
219 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
227 static char *get_loop_name(int num
)
231 snprintf(buf
, 255, "-loop%d", num
);
233 return alloc_sname(buf
);;
237 * Pre Loops are while and for loops.
239 static void handle_pre_loop(struct statement
*stmt
)
241 int once_through
; /* we go through the loop at least once */
242 struct sm_state
*extra_sm
= NULL
;
245 struct state_list
*slist
= NULL
;
246 struct sm_state
*sm
= NULL
;
248 loop_name
= get_loop_name(loop_num
);
251 __split_stmt(stmt
->iterator_pre_statement
);
253 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
259 __merge_gotos(loop_name
);
261 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
262 __in_pre_condition
++;
263 __pass_to_client(stmt
, PRELOOP_HOOK
);
264 __split_whole_condition(stmt
->iterator_pre_condition
);
265 __in_pre_condition
--;
266 FOR_EACH_PTR(slist
, sm
) {
267 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
268 } END_FOR_EACH_PTR(sm
);
271 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
273 if (option_assume_loops
)
276 __split_stmt(stmt
->iterator_statement
);
277 __warn_on_silly_pre_loops();
278 if (is_forever_loop(stmt
)) {
279 __save_gotos(loop_name
);
280 /* forever loops don't have an iterator_post_statement */
281 __discard_continues();
282 __discard_false_states();
286 unchanged
= __iterator_unchanged(extra_sm
);
287 __split_stmt(stmt
->iterator_post_statement
);
288 __save_gotos(loop_name
);
289 __split_whole_condition(stmt
->iterator_pre_condition
);
291 __merge_false_states();
293 __discard_false_states();
295 __merge_false_states();
298 if (extra_sm
&& unchanged
)
299 __extra_pre_loop_hook_after(extra_sm
,
300 stmt
->iterator_post_statement
,
301 stmt
->iterator_pre_condition
);
308 * Post loops are do {} while();
310 static void handle_post_loop(struct statement
*stmt
)
314 loop_name
= get_loop_name(loop_num
);
320 __merge_gotos(loop_name
);
321 __split_stmt(stmt
->iterator_statement
);
323 if (!is_zero(stmt
->iterator_post_condition
))
324 __save_gotos(loop_name
);
326 if (is_forever_loop(stmt
)) {
329 __split_whole_condition(stmt
->iterator_post_condition
);
330 __use_false_states();
336 static int empty_statement(struct statement
*stmt
)
340 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
345 static int last_stmt_on_same_line()
347 struct statement
*stmt
;
350 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
353 if (stmt
->pos
.line
== get_lineno())
356 } END_FOR_EACH_PTR_REVERSE(stmt
);
360 static struct statement
*last_stmt
;
361 static int is_last_stmt(struct statement
*stmt
)
363 if (stmt
== last_stmt
)
368 static void print_unreached_initializers(struct symbol_list
*sym_list
)
372 FOR_EACH_PTR(sym_list
, sym
) {
374 sm_msg("info: '%s' is not actually initialized (unreached code).",
375 (sym
->ident
? sym
->ident
->name
: "this variable"));
376 } END_FOR_EACH_PTR(sym
);
379 static void print_unreached(struct statement
*stmt
)
381 static int print
= 1;
383 if (!__path_is_null()) {
390 switch (stmt
->type
) {
391 case STMT_COMPOUND
: /* after a switch before a case stmt */
396 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
397 print_unreached_initializers(stmt
->declaration
);
399 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
400 if (is_last_stmt(stmt
))
410 if (!option_spammy
&& empty_statement(stmt
))
412 sm_msg("info: ignoring unreachable code.");
416 static void split_asm_constraints(struct expression_list
*expr_list
)
418 struct expression
*expr
;
421 FOR_EACH_PTR(expr_list
, expr
) {
423 case 0: /* identifier */
424 case 1: /* constraint */
427 case 2: /* expression */
432 } END_FOR_EACH_PTR(expr
);
435 static int is_case_val(struct statement
*stmt
, long long val
)
439 if (stmt
->type
!= STMT_CASE
)
441 if (!stmt
->case_expression
) {
445 if (!get_value(stmt
->case_expression
, &case_val
))
452 static void split_known_switch(struct statement
*stmt
, long long val
)
454 struct statement
*tmp
;
456 __split_expr(stmt
->switch_expression
);
458 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
459 __save_switch_states(top_expression(switch_expr_stack
));
464 stmt
= stmt
->switch_statement
;
467 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
469 __push_scope_hooks();
470 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
471 __smatch_lineno
= tmp
->pos
.line
;
472 if (is_case_val(tmp
, val
)) {
473 __merge_switches(top_expression(switch_expr_stack
),
474 stmt
->case_expression
);
475 __pass_case_to_client(top_expression(switch_expr_stack
),
476 stmt
->case_expression
);
478 if (__path_is_null())
481 if (__path_is_null()) {
485 } END_FOR_EACH_PTR(tmp
);
487 __call_scope_hooks();
488 if (!__pop_default())
489 __merge_switches(top_expression(switch_expr_stack
),
491 __discard_switches();
493 pop_expression(&switch_expr_stack
);
496 void __split_stmt(struct statement
*stmt
)
503 if (out_of_memory() || __bail_on_rest_of_function
) {
504 static char *printed
= NULL
;
506 if (printed
!= cur_func
)
507 sm_msg("Function too hairy. Giving up.");
512 add_ptr_list(&big_statement_stack
, stmt
);
513 free_expression_stack(&big_expression_stack
);
514 set_position(stmt
->pos
);
515 print_unreached(stmt
);
516 __pass_to_client(stmt
, STMT_HOOK
);
518 switch (stmt
->type
) {
519 case STMT_DECLARATION
:
520 split_declaration(stmt
->declaration
);
523 __split_expr(stmt
->ret_value
);
524 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
527 case STMT_EXPRESSION
:
528 __split_expr(stmt
->expression
);
530 case STMT_COMPOUND
: {
531 struct statement
*tmp
;
534 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
535 __push_scope_hooks();
536 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
538 } END_FOR_EACH_PTR(tmp
);
539 __call_scope_hooks();
543 if (known_condition_true(stmt
->if_conditional
)) {
544 __split_stmt(stmt
->if_true
);
547 if (known_condition_false(stmt
->if_conditional
)) {
548 __split_stmt(stmt
->if_false
);
551 if (option_known_conditions
&&
552 implied_condition_true(stmt
->if_conditional
)) {
553 sm_info("this condition is true.");
554 __split_stmt(stmt
->if_true
);
557 if (option_known_conditions
&&
558 implied_condition_false(stmt
->if_conditional
)) {
559 sm_info("this condition is false.");
560 __split_stmt(stmt
->if_false
);
563 __split_whole_condition(stmt
->if_conditional
);
564 __split_stmt(stmt
->if_true
);
565 if (empty_statement(stmt
->if_true
) &&
566 last_stmt_on_same_line() &&
567 !get_macro_name(&stmt
->if_true
->pos
))
568 sm_msg("warn: if();");
569 __push_true_states();
570 __use_false_states();
571 __split_stmt(stmt
->if_false
);
572 __merge_true_states();
575 if (stmt
->iterator_pre_condition
)
576 handle_pre_loop(stmt
);
577 else if (stmt
->iterator_post_condition
)
578 handle_post_loop(stmt
);
580 // these are for(;;) type loops.
581 handle_pre_loop(stmt
);
585 if (get_value(stmt
->switch_expression
, &val
)) {
586 split_known_switch(stmt
, val
);
589 __split_expr(stmt
->switch_expression
);
590 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
591 __save_switch_states(top_expression(switch_expr_stack
));
595 __split_stmt(stmt
->switch_statement
);
596 if (!__pop_default())
597 __merge_switches(top_expression(switch_expr_stack
),
599 __discard_switches();
601 pop_expression(&switch_expr_stack
);
604 __merge_switches(top_expression(switch_expr_stack
),
605 stmt
->case_expression
);
606 __pass_case_to_client(top_expression(switch_expr_stack
),
607 stmt
->case_expression
);
608 if (!stmt
->case_expression
)
610 __split_expr(stmt
->case_expression
);
611 __split_expr(stmt
->case_to
);
612 __split_stmt(stmt
->case_statement
);
615 if (stmt
->label_identifier
&&
616 stmt
->label_identifier
->type
== SYM_LABEL
&&
617 stmt
->label_identifier
->ident
) {
618 loop_count
= 1000000;
619 __merge_gotos(stmt
->label_identifier
->ident
->name
);
621 __split_stmt(stmt
->label_statement
);
624 __split_expr(stmt
->goto_expression
);
625 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
626 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
628 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
630 __process_continues();
632 } else if (stmt
->goto_label
&&
633 stmt
->goto_label
->type
== SYM_LABEL
&&
634 stmt
->goto_label
->ident
) {
635 __save_gotos(stmt
->goto_label
->ident
->name
);
642 __pass_to_client(stmt
, ASM_HOOK
);
643 __split_expr(stmt
->asm_string
);
644 split_asm_constraints(stmt
->asm_outputs
);
645 split_asm_constraints(stmt
->asm_inputs
);
646 split_asm_constraints(stmt
->asm_clobbers
);
651 __split_expr(stmt
->range_expression
);
652 __split_expr(stmt
->range_low
);
653 __split_expr(stmt
->range_high
);
658 static void split_expr_list(struct expression_list
*expr_list
)
660 struct expression
*expr
;
662 FOR_EACH_PTR(expr_list
, expr
) {
664 } END_FOR_EACH_PTR(expr
);
668 static void split_sym(struct symbol
*sym
)
672 if (!(sym
->namespace & NS_SYMBOL
))
675 __split_stmt(sym
->stmt
);
676 __split_expr(sym
->array_size
);
677 split_symlist(sym
->arguments
);
678 split_symlist(sym
->symbol_list
);
679 __split_stmt(sym
->inline_stmt
);
680 split_symlist(sym
->inline_symbol_list
);
683 static void split_symlist(struct symbol_list
*sym_list
)
687 FOR_EACH_PTR(sym_list
, sym
) {
689 } END_FOR_EACH_PTR(sym
);
692 static struct expression
*fake_assign_expr(struct symbol
*sym
)
694 struct expression
*e_assign
, *e_symbol
;
696 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
697 e_symbol
= alloc_expression(sym
->pos
, EXPR_SYMBOL
);
698 e_assign
->op
= (int)'=';
699 e_symbol
->symbol
= sym
;
700 e_symbol
->symbol_name
= sym
->ident
;
701 e_assign
->left
= e_symbol
;
702 e_assign
->right
= sym
->initializer
;
706 static void do_initializer_stuff(struct symbol
*sym
)
708 struct expression
*assign
;
710 if(!sym
->initializer
)
712 assign
= fake_assign_expr(sym
);
713 __split_expr(assign
);
716 static void split_declaration(struct symbol_list
*sym_list
)
720 FOR_EACH_PTR(sym_list
, sym
) {
721 __pass_to_client(sym
, DECLARATION_HOOK
);
722 do_initializer_stuff(sym
);
724 } END_FOR_EACH_PTR(sym
);
727 static void split_function(struct symbol
*sym
)
729 struct symbol
*base_type
= get_base_type(sym
);
733 line_func_start
= base_type
->stmt
->pos
.line
;
735 cur_func
= sym
->ident
->name
;
736 __smatch_lineno
= sym
->pos
.line
;
739 sm_debug("new function: %s\n", cur_func
);
740 if (option_two_passes
) {
744 __pass_to_client(sym
, FUNC_DEF_HOOK
);
745 __split_stmt(base_type
->stmt
);
746 __split_stmt(base_type
->inline_stmt
);
752 __pass_to_client(sym
, FUNC_DEF_HOOK
);
753 __split_stmt(base_type
->stmt
);
754 __split_stmt(base_type
->inline_stmt
);
755 __pass_to_client(sym
, END_FUNC_HOOK
);
759 free_data_info_allocs();
760 free_expression_stack(&switch_expr_stack
);
761 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
762 __bail_on_rest_of_function
= 0;
765 static struct symbol_list
*inlines_called
;
766 static void add_inline_function(struct symbol
*sym
)
768 static struct symbol_list
*already_added
;
771 FOR_EACH_PTR(already_added
, tmp
) {
774 } END_FOR_EACH_PTR(tmp
);
776 add_ptr_list(&already_added
, sym
);
777 add_ptr_list(&inlines_called
, sym
);
780 static void process_inlines()
784 FOR_EACH_PTR(inlines_called
, tmp
) {
786 } END_FOR_EACH_PTR(tmp
);
787 free_ptr_list(&inlines_called
);
790 static void split_functions(struct symbol_list
*sym_list
)
794 FOR_EACH_PTR(sym_list
, sym
) {
795 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
799 __pass_to_client(sym
, BASE_HOOK
);
801 } END_FOR_EACH_PTR(sym
);
802 __pass_to_client_no_data(END_FILE_HOOK
);
805 void smatch (int argc
, char **argv
)
808 struct string_list
*filelist
= NULL
;
809 struct symbol_list
*sym_list
;
813 printf("Usage: smatch [--debug] <filename.c>\n");
816 sparse_initialize(argc
, argv
, &filelist
);
817 FOR_EACH_PTR_NOTAG(filelist
, file
) {
818 sym_list
= sparse_keep_tokens(file
);
819 split_functions(sym_list
);
820 } END_FOR_EACH_PTR_NOTAG(file
);