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 inside_loop(void) { return !!loop_count
; }
39 int in_expression_statement(void) { return !!__expr_stmt_count
; }
41 static void split_symlist(struct symbol_list
*sym_list
);
42 static void split_declaration(struct symbol_list
*sym_list
);
43 static void split_expr_list(struct expression_list
*expr_list
);
44 static void add_inline_function(struct symbol
*sym
);
46 int option_assume_loops
= 0;
47 int option_known_conditions
= 0;
48 int option_two_passes
= 0;
49 struct symbol
*cur_func_sym
= NULL
;
51 const char *get_filename(void)
58 static void set_position(struct position pos
)
61 static int prev_stream
= -1;
63 __smatch_lineno
= pos
.line
;
65 if (pos
.stream
== prev_stream
)
68 filename
= stream_name(pos
.stream
);
71 pathname
= getcwd(NULL
, 0);
73 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
74 full_filename
= malloc(len
);
75 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
77 full_filename
= alloc_string(filename
);
82 static int is_inline_func(struct expression
*expr
)
84 if (expr
->type
!= EXPR_SYMBOL
|| !expr
->symbol
)
86 if (expr
->symbol
->ctype
.modifiers
& MOD_INLINE
)
91 void __split_expr(struct expression
*expr
)
96 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
98 push_expression(&big_expression_stack
, expr
);
99 set_position(expr
->pos
);
100 __pass_to_client(expr
, EXPR_HOOK
);
102 switch (expr
->type
) {
105 __pass_to_client(expr
, DEREF_HOOK
);
107 __pass_to_client(expr
, OP_HOOK
);
108 __split_expr(expr
->unop
);
112 __split_stmt(expr
->statement
);
117 __pass_to_client(expr
, LOGIC_HOOK
);
118 __handle_logic(expr
);
121 __pass_to_client(expr
, BINOP_HOOK
);
123 __split_expr(expr
->left
);
124 __split_expr(expr
->right
);
126 case EXPR_ASSIGNMENT
: {
127 struct expression
*tmp
;
129 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
132 if (__handle_condition_assigns(expr
))
134 /* foo = (x < 5 ? foo : 5); */
135 if (__handle_select_assigns(expr
))
137 /* foo = ({frob(); frob(); frob(); 1;}) */
138 if (__handle_expr_statement_assigns(expr
))
141 __split_expr(expr
->right
);
142 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
143 tmp
= strip_expr(expr
->right
);
144 if (tmp
->type
== EXPR_CALL
)
145 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
146 if (get_macro_name(&tmp
->pos
))
147 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
148 __split_expr(expr
->left
);
152 __pass_to_client(expr
, DEREF_HOOK
);
153 __split_expr(expr
->deref
);
156 __split_expr(expr
->base
);
159 case EXPR_FORCE_CAST
:
160 __split_expr(expr
->cast_expression
);
163 /* there isn't anything to pass a client from inside a sizeof() */
165 case EXPR_CONDITIONAL
:
167 __pass_to_client(expr
, SELECT_HOOK
);
168 __split_whole_condition(expr
->conditional
);
169 __split_expr(expr
->cond_true
);
170 __push_true_states();
171 __use_false_states();
172 __split_expr(expr
->cond_false
);
173 __merge_true_states();
176 split_expr_list(expr
->args
);
177 __split_expr(expr
->fn
);
178 if (is_inline_func(expr
->fn
))
179 add_inline_function(expr
->fn
->symbol
);
180 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
182 case EXPR_INITIALIZER
:
183 split_expr_list(expr
->expr_list
);
185 case EXPR_IDENTIFIER
:
186 __split_expr(expr
->ident_expression
);
189 __split_expr(expr
->idx_expression
);
192 __split_expr(expr
->init_expr
);
195 __pass_to_client(expr
, SYM_HOOK
);
198 __pass_to_client(expr
, STRING_HOOK
);
203 pop_expression(&big_expression_stack
);
206 static int is_forever_loop(struct statement
*stmt
)
208 struct expression
*expr
;
210 expr
= strip_expr(stmt
->iterator_pre_condition
);
212 expr
= stmt
->iterator_post_condition
;
214 /* this is a for(;;) loop... */
218 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
225 static char *get_loop_name(int num
)
229 snprintf(buf
, 255, "-loop%d", num
);
231 return alloc_sname(buf
);
235 * Pre Loops are while and for loops.
237 static void handle_pre_loop(struct statement
*stmt
)
239 int once_through
; /* we go through the loop at least once */
240 struct sm_state
*extra_sm
= NULL
;
243 struct state_list
*slist
= NULL
;
244 struct sm_state
*sm
= NULL
;
246 loop_name
= get_loop_name(loop_num
);
249 __split_stmt(stmt
->iterator_pre_statement
);
251 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
257 __merge_gotos(loop_name
);
259 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
260 __in_pre_condition
++;
261 __pass_to_client(stmt
, PRELOOP_HOOK
);
262 __split_whole_condition(stmt
->iterator_pre_condition
);
263 __in_pre_condition
--;
264 FOR_EACH_PTR(slist
, sm
) {
265 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
266 } END_FOR_EACH_PTR(sm
);
269 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
271 if (option_assume_loops
)
274 __split_stmt(stmt
->iterator_statement
);
275 __warn_on_silly_pre_loops();
276 if (is_forever_loop(stmt
)) {
277 __save_gotos(loop_name
);
278 /* forever loops don't have an iterator_post_statement */
279 __discard_continues();
280 __discard_false_states();
284 unchanged
= __iterator_unchanged(extra_sm
);
285 __split_stmt(stmt
->iterator_post_statement
);
286 __save_gotos(loop_name
);
287 __split_whole_condition(stmt
->iterator_pre_condition
);
289 __merge_false_states();
291 __discard_false_states();
293 __merge_false_states();
295 if (extra_sm
&& unchanged
)
296 __extra_pre_loop_hook_after(extra_sm
,
297 stmt
->iterator_post_statement
,
298 stmt
->iterator_pre_condition
);
305 * Post loops are do {} while();
307 static void handle_post_loop(struct statement
*stmt
)
311 loop_name
= get_loop_name(loop_num
);
317 __merge_gotos(loop_name
);
318 __split_stmt(stmt
->iterator_statement
);
320 if (!is_zero(stmt
->iterator_post_condition
))
321 __save_gotos(loop_name
);
323 if (is_forever_loop(stmt
)) {
326 __split_whole_condition(stmt
->iterator_post_condition
);
327 __use_false_states();
333 static int empty_statement(struct statement
*stmt
)
337 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
342 static int last_stmt_on_same_line()
344 struct statement
*stmt
;
347 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
350 if (stmt
->pos
.line
== get_lineno())
353 } END_FOR_EACH_PTR_REVERSE(stmt
);
357 static struct statement
*last_stmt
;
358 static int is_last_stmt(struct statement
*stmt
)
360 if (stmt
== last_stmt
)
365 static void print_unreached_initializers(struct symbol_list
*sym_list
)
369 FOR_EACH_PTR(sym_list
, sym
) {
370 if (sym
->initializer
)
371 sm_msg("info: '%s' is not actually initialized (unreached code).",
372 (sym
->ident
? sym
->ident
->name
: "this variable"));
373 } END_FOR_EACH_PTR(sym
);
376 static void print_unreached(struct statement
*stmt
)
378 static int print
= 1;
380 if (!__path_is_null()) {
387 switch (stmt
->type
) {
388 case STMT_COMPOUND
: /* after a switch before a case stmt */
393 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
394 print_unreached_initializers(stmt
->declaration
);
396 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
397 if (is_last_stmt(stmt
))
407 if (!option_spammy
&& empty_statement(stmt
))
409 sm_msg("info: ignoring unreachable code.");
413 static void split_asm_constraints(struct expression_list
*expr_list
)
415 struct expression
*expr
;
418 FOR_EACH_PTR(expr_list
, expr
) {
420 case 0: /* identifier */
421 case 1: /* constraint */
424 case 2: /* expression */
429 } END_FOR_EACH_PTR(expr
);
432 static int is_case_val(struct statement
*stmt
, long long val
)
436 if (stmt
->type
!= STMT_CASE
)
438 if (!stmt
->case_expression
) {
442 if (!get_value(stmt
->case_expression
, &case_val
))
449 static void split_known_switch(struct statement
*stmt
, long long val
)
451 struct statement
*tmp
;
453 __split_expr(stmt
->switch_expression
);
455 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
456 __save_switch_states(top_expression(switch_expr_stack
));
461 stmt
= stmt
->switch_statement
;
464 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
466 __push_scope_hooks();
467 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
468 __smatch_lineno
= tmp
->pos
.line
;
469 if (is_case_val(tmp
, val
)) {
470 __merge_switches(top_expression(switch_expr_stack
),
471 stmt
->case_expression
);
472 __pass_case_to_client(top_expression(switch_expr_stack
),
473 stmt
->case_expression
);
475 if (__path_is_null())
478 if (__path_is_null()) {
482 } END_FOR_EACH_PTR(tmp
);
484 __call_scope_hooks();
485 if (!__pop_default())
486 __merge_switches(top_expression(switch_expr_stack
),
488 __discard_switches();
490 pop_expression(&switch_expr_stack
);
493 void __split_stmt(struct statement
*stmt
)
500 if (out_of_memory() || __bail_on_rest_of_function
) {
501 static char *printed
= NULL
;
503 if (printed
!= cur_func
)
504 sm_msg("Function too hairy. Giving up.");
509 add_ptr_list(&big_statement_stack
, stmt
);
510 free_expression_stack(&big_expression_stack
);
511 set_position(stmt
->pos
);
512 print_unreached(stmt
);
513 __pass_to_client(stmt
, STMT_HOOK
);
515 switch (stmt
->type
) {
516 case STMT_DECLARATION
:
517 split_declaration(stmt
->declaration
);
520 __split_expr(stmt
->ret_value
);
521 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
524 case STMT_EXPRESSION
:
525 __split_expr(stmt
->expression
);
527 case STMT_COMPOUND
: {
528 struct statement
*tmp
;
531 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
532 __push_scope_hooks();
533 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
535 } END_FOR_EACH_PTR(tmp
);
536 __call_scope_hooks();
540 if (known_condition_true(stmt
->if_conditional
)) {
541 __split_stmt(stmt
->if_true
);
544 if (known_condition_false(stmt
->if_conditional
)) {
545 __split_stmt(stmt
->if_false
);
548 if (option_known_conditions
&&
549 implied_condition_true(stmt
->if_conditional
)) {
550 sm_info("this condition is true.");
551 __split_stmt(stmt
->if_true
);
554 if (option_known_conditions
&&
555 implied_condition_false(stmt
->if_conditional
)) {
556 sm_info("this condition is false.");
557 __split_stmt(stmt
->if_false
);
560 __split_whole_condition(stmt
->if_conditional
);
561 __split_stmt(stmt
->if_true
);
562 if (empty_statement(stmt
->if_true
) &&
563 last_stmt_on_same_line() &&
564 !get_macro_name(&stmt
->if_true
->pos
))
565 sm_msg("warn: if();");
566 __push_true_states();
567 __use_false_states();
568 __split_stmt(stmt
->if_false
);
569 __merge_true_states();
572 if (stmt
->iterator_pre_condition
)
573 handle_pre_loop(stmt
);
574 else if (stmt
->iterator_post_condition
)
575 handle_post_loop(stmt
);
577 // these are for(;;) type loops.
578 handle_pre_loop(stmt
);
582 if (get_value(stmt
->switch_expression
, &val
)) {
583 split_known_switch(stmt
, val
);
586 __split_expr(stmt
->switch_expression
);
587 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
588 __save_switch_states(top_expression(switch_expr_stack
));
592 __split_stmt(stmt
->switch_statement
);
593 if (!__pop_default())
594 __merge_switches(top_expression(switch_expr_stack
),
596 __discard_switches();
598 pop_expression(&switch_expr_stack
);
601 __merge_switches(top_expression(switch_expr_stack
),
602 stmt
->case_expression
);
603 __pass_case_to_client(top_expression(switch_expr_stack
),
604 stmt
->case_expression
);
605 if (!stmt
->case_expression
)
607 __split_expr(stmt
->case_expression
);
608 __split_expr(stmt
->case_to
);
609 __split_stmt(stmt
->case_statement
);
612 if (stmt
->label_identifier
&&
613 stmt
->label_identifier
->type
== SYM_LABEL
&&
614 stmt
->label_identifier
->ident
) {
615 loop_count
= 1000000;
616 __merge_gotos(stmt
->label_identifier
->ident
->name
);
618 __split_stmt(stmt
->label_statement
);
621 __split_expr(stmt
->goto_expression
);
622 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
623 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
625 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
627 __process_continues();
629 } else if (stmt
->goto_label
&&
630 stmt
->goto_label
->type
== SYM_LABEL
&&
631 stmt
->goto_label
->ident
) {
632 __save_gotos(stmt
->goto_label
->ident
->name
);
639 __pass_to_client(stmt
, ASM_HOOK
);
640 __split_expr(stmt
->asm_string
);
641 split_asm_constraints(stmt
->asm_outputs
);
642 split_asm_constraints(stmt
->asm_inputs
);
643 split_asm_constraints(stmt
->asm_clobbers
);
648 __split_expr(stmt
->range_expression
);
649 __split_expr(stmt
->range_low
);
650 __split_expr(stmt
->range_high
);
655 static void split_expr_list(struct expression_list
*expr_list
)
657 struct expression
*expr
;
659 FOR_EACH_PTR(expr_list
, expr
) {
661 } END_FOR_EACH_PTR(expr
);
664 static void split_sym(struct symbol
*sym
)
668 if (!(sym
->namespace & NS_SYMBOL
))
671 __split_stmt(sym
->stmt
);
672 __split_expr(sym
->array_size
);
673 split_symlist(sym
->arguments
);
674 split_symlist(sym
->symbol_list
);
675 __split_stmt(sym
->inline_stmt
);
676 split_symlist(sym
->inline_symbol_list
);
679 static void split_symlist(struct symbol_list
*sym_list
)
683 FOR_EACH_PTR(sym_list
, sym
) {
685 } END_FOR_EACH_PTR(sym
);
688 static struct expression
*fake_assign_expr(struct symbol
*sym
)
690 struct expression
*e_assign
, *e_symbol
;
692 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
693 e_symbol
= alloc_expression(sym
->pos
, EXPR_SYMBOL
);
694 e_assign
->op
= (int)'=';
695 e_symbol
->symbol
= sym
;
696 e_symbol
->symbol_name
= sym
->ident
;
697 e_assign
->left
= e_symbol
;
698 e_assign
->right
= sym
->initializer
;
702 static void do_initializer_stuff(struct symbol
*sym
)
704 struct expression
*assign
;
706 if (!sym
->initializer
)
708 assign
= fake_assign_expr(sym
);
709 __split_expr(assign
);
712 static void split_declaration(struct symbol_list
*sym_list
)
716 FOR_EACH_PTR(sym_list
, sym
) {
717 __pass_to_client(sym
, DECLARATION_HOOK
);
718 do_initializer_stuff(sym
);
720 } END_FOR_EACH_PTR(sym
);
723 static void split_function(struct symbol
*sym
)
725 struct symbol
*base_type
= get_base_type(sym
);
729 line_func_start
= base_type
->stmt
->pos
.line
;
731 cur_func
= sym
->ident
->name
;
732 __smatch_lineno
= sym
->pos
.line
;
735 sm_debug("new function: %s\n", cur_func
);
736 if (option_two_passes
) {
740 __pass_to_client(sym
, FUNC_DEF_HOOK
);
741 __split_stmt(base_type
->stmt
);
742 __split_stmt(base_type
->inline_stmt
);
748 __pass_to_client(sym
, FUNC_DEF_HOOK
);
749 __split_stmt(base_type
->stmt
);
750 __split_stmt(base_type
->inline_stmt
);
751 __pass_to_client(sym
, END_FUNC_HOOK
);
755 free_data_info_allocs();
756 free_expression_stack(&switch_expr_stack
);
757 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
758 __bail_on_rest_of_function
= 0;
761 static struct symbol_list
*inlines_called
;
762 static void add_inline_function(struct symbol
*sym
)
764 static struct symbol_list
*already_added
;
767 FOR_EACH_PTR(already_added
, tmp
) {
770 } END_FOR_EACH_PTR(tmp
);
772 add_ptr_list(&already_added
, sym
);
773 add_ptr_list(&inlines_called
, sym
);
776 static void process_inlines()
780 FOR_EACH_PTR(inlines_called
, tmp
) {
782 } END_FOR_EACH_PTR(tmp
);
783 free_ptr_list(&inlines_called
);
786 static void split_functions(struct symbol_list
*sym_list
)
790 FOR_EACH_PTR(sym_list
, sym
) {
791 set_position(sym
->pos
);
792 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
796 __pass_to_client(sym
, BASE_HOOK
);
798 } END_FOR_EACH_PTR(sym
);
799 __pass_to_client_no_data(END_FILE_HOOK
);
802 void smatch(int argc
, char **argv
)
805 struct string_list
*filelist
= NULL
;
806 struct symbol_list
*sym_list
;
810 printf("Usage: smatch [--debug] <filename.c>\n");
813 sparse_initialize(argc
, argv
, &filelist
);
814 FOR_EACH_PTR_NOTAG(filelist
, file
) {
815 if (option_file_output
) {
818 snprintf(buf
, sizeof(buf
), "%s.smatch", file
);
819 sm_outfd
= fopen(buf
, "w");
821 printf("Error: Cannot open %s\n", file
);
825 sym_list
= sparse_keep_tokens(file
);
826 split_functions(sym_list
);
827 } END_FOR_EACH_PTR_NOTAG(file
);