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 void __split_expr(struct expression
*expr
)
99 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
101 push_expression(&big_expression_stack
, expr
);
102 set_position(expr
->pos
);
103 __pass_to_client(expr
, EXPR_HOOK
);
105 switch (expr
->type
) {
108 __pass_to_client(expr
, DEREF_HOOK
);
110 __pass_to_client(expr
, OP_HOOK
);
111 __split_expr(expr
->unop
);
115 __split_stmt(expr
->statement
);
120 __pass_to_client(expr
, LOGIC_HOOK
);
121 __handle_logic(expr
);
124 __pass_to_client(expr
, BINOP_HOOK
);
126 __split_expr(expr
->left
);
127 __split_expr(expr
->right
);
129 case EXPR_ASSIGNMENT
: {
130 struct expression
*tmp
;
135 __pass_to_client(expr
, RAW_ASSIGNMENT_HOOK
);
138 if (__handle_condition_assigns(expr
))
140 /* foo = (x < 5 ? foo : 5); */
141 if (__handle_select_assigns(expr
))
143 /* foo = ({frob(); frob(); frob(); 1;}) */
144 if (__handle_expr_statement_assigns(expr
))
147 __split_expr(expr
->right
);
148 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
149 tmp
= strip_expr(expr
->right
);
150 if (tmp
->type
== EXPR_CALL
)
151 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
152 if (get_macro_name(tmp
->pos
))
153 __pass_to_client(expr
, MACRO_ASSIGNMENT_HOOK
);
154 __split_expr(expr
->left
);
158 __pass_to_client(expr
, DEREF_HOOK
);
159 __split_expr(expr
->deref
);
162 __split_expr(expr
->base
);
165 case EXPR_FORCE_CAST
:
166 __pass_to_client(expr
, CAST_HOOK
);
167 __split_expr(expr
->cast_expression
);
170 /* there isn't anything to pass a client from inside a sizeof() */
174 evaluate_expression(expr
);
176 case EXPR_CONDITIONAL
:
178 if (known_condition_true(expr
->conditional
)) {
179 __split_expr(expr
->cond_true
);
182 if (known_condition_false(expr
->conditional
)) {
183 __split_expr(expr
->cond_false
);
186 __pass_to_client(expr
, SELECT_HOOK
);
187 __split_whole_condition(expr
->conditional
);
188 __split_expr(expr
->cond_true
);
189 __push_true_states();
190 __use_false_states();
191 __split_expr(expr
->cond_false
);
192 __merge_true_states();
195 if (sym_name_is("__builtin_constant_p", expr
->fn
))
197 split_expr_list(expr
->args
);
198 __split_expr(expr
->fn
);
199 if (is_inline_func(expr
->fn
))
200 add_inline_function(expr
->fn
->symbol
);
201 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
203 case EXPR_INITIALIZER
:
204 split_expr_list(expr
->expr_list
);
206 case EXPR_IDENTIFIER
:
207 __split_expr(expr
->ident_expression
);
210 __split_expr(expr
->idx_expression
);
213 __split_expr(expr
->init_expr
);
216 __pass_to_client(expr
, SYM_HOOK
);
219 __pass_to_client(expr
, STRING_HOOK
);
224 pop_expression(&big_expression_stack
);
227 static int is_forever_loop(struct statement
*stmt
)
229 struct expression
*expr
;
231 expr
= strip_expr(stmt
->iterator_pre_condition
);
233 expr
= stmt
->iterator_post_condition
;
235 /* this is a for(;;) loop... */
239 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
246 static char *get_loop_name(int num
)
250 snprintf(buf
, 255, "-loop%d", num
);
252 return alloc_sname(buf
);
256 * Pre Loops are while and for loops.
258 static void handle_pre_loop(struct statement
*stmt
)
260 int once_through
; /* we go through the loop at least once */
261 struct sm_state
*extra_sm
= NULL
;
264 struct state_list
*slist
= NULL
;
265 struct sm_state
*sm
= NULL
;
267 loop_name
= get_loop_name(loop_num
);
270 __split_stmt(stmt
->iterator_pre_statement
);
272 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
278 __merge_gotos(loop_name
);
280 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
281 __in_pre_condition
++;
282 __pass_to_client(stmt
, PRELOOP_HOOK
);
283 __split_whole_condition(stmt
->iterator_pre_condition
);
284 __in_pre_condition
--;
285 FOR_EACH_PTR(slist
, sm
) {
286 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
287 } END_FOR_EACH_PTR(sm
);
290 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
292 if (option_assume_loops
)
295 __split_stmt(stmt
->iterator_statement
);
296 __warn_on_silly_pre_loops();
297 if (is_forever_loop(stmt
)) {
298 struct state_list
*slist
;
300 __save_gotos(loop_name
);
302 __push_fake_cur_slist();
303 __split_stmt(stmt
->iterator_post_statement
);
304 slist
= __pop_fake_cur_slist();
306 __discard_continues();
307 __discard_false_states();
310 if (!__path_is_null())
311 __merge_slist_into_cur(slist
);
315 unchanged
= __iterator_unchanged(extra_sm
);
316 __split_stmt(stmt
->iterator_post_statement
);
317 __save_gotos(loop_name
);
318 __split_whole_condition(stmt
->iterator_pre_condition
);
320 __merge_false_states();
322 __discard_false_states();
324 __merge_false_states();
326 if (extra_sm
&& unchanged
)
327 __extra_pre_loop_hook_after(extra_sm
,
328 stmt
->iterator_post_statement
,
329 stmt
->iterator_pre_condition
);
336 * Post loops are do {} while();
338 static void handle_post_loop(struct statement
*stmt
)
342 loop_name
= get_loop_name(loop_num
);
348 __merge_gotos(loop_name
);
349 __split_stmt(stmt
->iterator_statement
);
351 if (!is_zero(stmt
->iterator_post_condition
))
352 __save_gotos(loop_name
);
354 if (is_forever_loop(stmt
)) {
357 __split_whole_condition(stmt
->iterator_post_condition
);
358 __use_false_states();
364 static int empty_statement(struct statement
*stmt
)
368 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
373 static int last_stmt_on_same_line()
375 struct statement
*stmt
;
378 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
381 if (stmt
->pos
.line
== get_lineno())
384 } END_FOR_EACH_PTR_REVERSE(stmt
);
388 static struct statement
*last_stmt
;
389 static int is_last_stmt(struct statement
*stmt
)
391 if (stmt
== last_stmt
)
396 static void print_unreached_initializers(struct symbol_list
*sym_list
)
400 FOR_EACH_PTR(sym_list
, sym
) {
401 if (sym
->initializer
)
402 sm_msg("info: '%s' is not actually initialized (unreached code).",
403 (sym
->ident
? sym
->ident
->name
: "this variable"));
404 } END_FOR_EACH_PTR(sym
);
407 static void print_unreached(struct statement
*stmt
)
409 static int print
= 1;
411 if (!__path_is_null()) {
418 switch (stmt
->type
) {
419 case STMT_COMPOUND
: /* after a switch before a case stmt */
424 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
425 print_unreached_initializers(stmt
->declaration
);
427 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
428 if (is_last_stmt(stmt
))
438 if (!option_spammy
&& empty_statement(stmt
))
440 sm_msg("info: ignoring unreachable code.");
444 static void split_asm_constraints(struct expression_list
*expr_list
)
446 struct expression
*expr
;
449 FOR_EACH_PTR(expr_list
, expr
) {
451 case 0: /* identifier */
452 case 1: /* constraint */
455 case 2: /* expression */
460 } END_FOR_EACH_PTR(expr
);
463 static int is_case_val(struct statement
*stmt
, long long val
)
467 if (stmt
->type
!= STMT_CASE
)
469 if (!stmt
->case_expression
) {
473 if (!get_value(stmt
->case_expression
, &case_val
))
480 static void split_known_switch(struct statement
*stmt
, long long val
)
482 struct statement
*tmp
;
484 __split_expr(stmt
->switch_expression
);
486 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
487 __save_switch_states(top_expression(switch_expr_stack
));
492 stmt
= stmt
->switch_statement
;
495 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
497 __push_scope_hooks();
498 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
499 __smatch_lineno
= tmp
->pos
.line
;
500 if (is_case_val(tmp
, val
)) {
501 __merge_switches(top_expression(switch_expr_stack
),
502 stmt
->case_expression
);
503 __pass_case_to_client(top_expression(switch_expr_stack
),
504 stmt
->case_expression
);
506 if (__path_is_null())
509 if (__path_is_null()) {
513 } END_FOR_EACH_PTR(tmp
);
515 __call_scope_hooks();
516 if (!__pop_default())
517 __merge_switches(top_expression(switch_expr_stack
),
519 __discard_switches();
521 pop_expression(&switch_expr_stack
);
524 void __split_stmt(struct statement
*stmt
)
531 if (out_of_memory() || __bail_on_rest_of_function
) {
532 static char *printed
= NULL
;
534 if (printed
!= cur_func
)
535 sm_msg("Function too hairy. Giving up.");
536 final_pass
= 0; /* turn off sm_msg() from here */
541 add_ptr_list(&big_statement_stack
, stmt
);
542 free_expression_stack(&big_expression_stack
);
543 set_position(stmt
->pos
);
544 print_unreached(stmt
);
545 __pass_to_client(stmt
, STMT_HOOK
);
547 switch (stmt
->type
) {
548 case STMT_DECLARATION
:
549 split_declaration(stmt
->declaration
);
552 __split_expr(stmt
->ret_value
);
553 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
556 case STMT_EXPRESSION
:
557 __split_expr(stmt
->expression
);
559 case STMT_COMPOUND
: {
560 struct statement
*tmp
;
563 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
564 __push_scope_hooks();
565 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
567 } END_FOR_EACH_PTR(tmp
);
568 __call_scope_hooks();
572 if (known_condition_true(stmt
->if_conditional
)) {
573 __split_stmt(stmt
->if_true
);
576 if (known_condition_false(stmt
->if_conditional
)) {
577 __split_stmt(stmt
->if_false
);
580 if (option_known_conditions
&&
581 implied_condition_true(stmt
->if_conditional
)) {
582 sm_info("this condition is true.");
583 __split_stmt(stmt
->if_true
);
586 if (option_known_conditions
&&
587 implied_condition_false(stmt
->if_conditional
)) {
588 sm_info("this condition is false.");
589 __split_stmt(stmt
->if_false
);
592 __split_whole_condition(stmt
->if_conditional
);
593 __split_stmt(stmt
->if_true
);
594 if (empty_statement(stmt
->if_true
) &&
595 last_stmt_on_same_line() &&
596 !get_macro_name(stmt
->if_true
->pos
))
597 sm_msg("warn: if();");
598 __push_true_states();
599 __use_false_states();
600 __split_stmt(stmt
->if_false
);
601 __merge_true_states();
604 if (stmt
->iterator_pre_condition
)
605 handle_pre_loop(stmt
);
606 else if (stmt
->iterator_post_condition
)
607 handle_post_loop(stmt
);
609 // these are for(;;) type loops.
610 handle_pre_loop(stmt
);
614 if (get_value(stmt
->switch_expression
, &val
)) {
615 split_known_switch(stmt
, val
);
618 __split_expr(stmt
->switch_expression
);
619 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
620 __save_switch_states(top_expression(switch_expr_stack
));
624 __split_stmt(stmt
->switch_statement
);
625 if (!__pop_default())
626 __merge_switches(top_expression(switch_expr_stack
),
628 __discard_switches();
630 pop_expression(&switch_expr_stack
);
633 __merge_switches(top_expression(switch_expr_stack
),
634 stmt
->case_expression
);
635 __pass_case_to_client(top_expression(switch_expr_stack
),
636 stmt
->case_expression
);
637 if (!stmt
->case_expression
)
639 __split_expr(stmt
->case_expression
);
640 __split_expr(stmt
->case_to
);
641 __split_stmt(stmt
->case_statement
);
644 if (stmt
->label_identifier
&&
645 stmt
->label_identifier
->type
== SYM_LABEL
&&
646 stmt
->label_identifier
->ident
) {
647 loop_count
= 1000000;
648 __merge_gotos(stmt
->label_identifier
->ident
->name
);
650 __split_stmt(stmt
->label_statement
);
653 __split_expr(stmt
->goto_expression
);
654 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
655 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
657 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
659 __process_continues();
661 } else if (stmt
->goto_label
&&
662 stmt
->goto_label
->type
== SYM_LABEL
&&
663 stmt
->goto_label
->ident
) {
664 __save_gotos(stmt
->goto_label
->ident
->name
);
671 __pass_to_client(stmt
, ASM_HOOK
);
672 __split_expr(stmt
->asm_string
);
673 split_asm_constraints(stmt
->asm_outputs
);
674 split_asm_constraints(stmt
->asm_inputs
);
675 split_asm_constraints(stmt
->asm_clobbers
);
680 __split_expr(stmt
->range_expression
);
681 __split_expr(stmt
->range_low
);
682 __split_expr(stmt
->range_high
);
687 static void split_expr_list(struct expression_list
*expr_list
)
689 struct expression
*expr
;
691 FOR_EACH_PTR(expr_list
, expr
) {
693 } END_FOR_EACH_PTR(expr
);
696 static void split_sym(struct symbol
*sym
)
700 if (!(sym
->namespace & NS_SYMBOL
))
703 __split_stmt(sym
->stmt
);
704 __split_expr(sym
->array_size
);
705 split_symlist(sym
->arguments
);
706 split_symlist(sym
->symbol_list
);
707 __split_stmt(sym
->inline_stmt
);
708 split_symlist(sym
->inline_symbol_list
);
711 static void split_symlist(struct symbol_list
*sym_list
)
715 FOR_EACH_PTR(sym_list
, sym
) {
717 } END_FOR_EACH_PTR(sym
);
720 static void fake_member_assigns(struct symbol
*sym
)
722 struct expression
*symbol
, *deref
, *assign
, *tmp
;
724 symbol
= symbol_expression(sym
);
725 FOR_EACH_PTR(sym
->initializer
->expr_list
, tmp
) {
726 if (tmp
->type
!= EXPR_IDENTIFIER
) /* how to handle arrays?? */
728 deref
= deref_expression(symbol
, '.', tmp
->expr_ident
);
729 assign
= assign_expression(deref
, tmp
->ident_expression
);
730 __split_expr(assign
);
731 } END_FOR_EACH_PTR(tmp
);
734 static void fake_assign_expr(struct symbol
*sym
)
736 struct expression
*assign
, *symbol
;
738 symbol
= symbol_expression(sym
);
739 assign
= assign_expression(symbol
, sym
->initializer
);
740 __split_expr(assign
);
743 static void do_initializer_stuff(struct symbol
*sym
)
745 if (!sym
->initializer
)
747 if (sym
->initializer
->type
== EXPR_INITIALIZER
)
748 fake_member_assigns(sym
);
750 fake_assign_expr(sym
);
753 static void split_declaration(struct symbol_list
*sym_list
)
757 FOR_EACH_PTR(sym_list
, sym
) {
758 __pass_to_client(sym
, DECLARATION_HOOK
);
759 do_initializer_stuff(sym
);
761 } END_FOR_EACH_PTR(sym
);
764 static void split_function(struct symbol
*sym
)
766 struct symbol
*base_type
= get_base_type(sym
);
770 line_func_start
= base_type
->stmt
->pos
.line
;
772 cur_func
= sym
->ident
->name
;
773 __smatch_lineno
= sym
->pos
.line
;
776 sm_debug("new function: %s\n", cur_func
);
778 if (option_two_passes
) {
782 __pass_to_client(sym
, FUNC_DEF_HOOK
);
783 __split_stmt(base_type
->stmt
);
784 __split_stmt(base_type
->inline_stmt
);
790 __pass_to_client(sym
, FUNC_DEF_HOOK
);
791 __split_stmt(base_type
->stmt
);
792 __split_stmt(base_type
->inline_stmt
);
793 __pass_to_client(sym
, END_FUNC_HOOK
);
797 free_data_info_allocs();
798 free_expression_stack(&switch_expr_stack
);
799 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
800 __bail_on_rest_of_function
= 0;
803 static struct symbol_list
*inlines_called
;
804 static void add_inline_function(struct symbol
*sym
)
806 static struct symbol_list
*already_added
;
809 FOR_EACH_PTR(already_added
, tmp
) {
812 } END_FOR_EACH_PTR(tmp
);
814 add_ptr_list(&already_added
, sym
);
815 add_ptr_list(&inlines_called
, sym
);
818 static void process_inlines()
822 FOR_EACH_PTR(inlines_called
, tmp
) {
824 } END_FOR_EACH_PTR(tmp
);
825 free_ptr_list(&inlines_called
);
828 static void split_functions(struct symbol_list
*sym_list
)
832 FOR_EACH_PTR(sym_list
, sym
) {
833 set_position(sym
->pos
);
834 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
838 __pass_to_client(sym
, BASE_HOOK
);
840 } END_FOR_EACH_PTR(sym
);
841 __pass_to_client_no_data(END_FILE_HOOK
);
844 void smatch(int argc
, char **argv
)
847 struct string_list
*filelist
= NULL
;
848 struct symbol_list
*sym_list
;
851 printf("Usage: smatch [--debug] <filename.c>\n");
854 sparse_initialize(argc
, argv
, &filelist
);
855 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
856 if (option_file_output
) {
859 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
860 sm_outfd
= fopen(buf
, "w");
862 printf("Error: Cannot open %s\n", base_file
);
866 sym_list
= sparse_keep_tokens(base_file
);
867 split_functions(sym_list
);
868 } END_FOR_EACH_PTR_NOTAG(base_file
);