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() */
172 case EXPR_CONDITIONAL
:
174 if (known_condition_true(expr
->conditional
)) {
175 __split_expr(expr
->cond_true
);
178 if (known_condition_false(expr
->conditional
)) {
179 __split_expr(expr
->cond_false
);
182 __pass_to_client(expr
, SELECT_HOOK
);
183 __split_whole_condition(expr
->conditional
);
184 __split_expr(expr
->cond_true
);
185 __push_true_states();
186 __use_false_states();
187 __split_expr(expr
->cond_false
);
188 __merge_true_states();
191 if (sym_name_is("__builtin_constant_p", expr
->fn
))
193 split_expr_list(expr
->args
);
194 __split_expr(expr
->fn
);
195 if (is_inline_func(expr
->fn
))
196 add_inline_function(expr
->fn
->symbol
);
197 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
199 case EXPR_INITIALIZER
:
200 split_expr_list(expr
->expr_list
);
202 case EXPR_IDENTIFIER
:
203 __split_expr(expr
->ident_expression
);
206 __split_expr(expr
->idx_expression
);
209 __split_expr(expr
->init_expr
);
212 __pass_to_client(expr
, SYM_HOOK
);
215 __pass_to_client(expr
, STRING_HOOK
);
220 pop_expression(&big_expression_stack
);
223 static int is_forever_loop(struct statement
*stmt
)
225 struct expression
*expr
;
227 expr
= strip_expr(stmt
->iterator_pre_condition
);
229 expr
= stmt
->iterator_post_condition
;
231 /* this is a for(;;) loop... */
235 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1)
242 static char *get_loop_name(int num
)
246 snprintf(buf
, 255, "-loop%d", num
);
248 return alloc_sname(buf
);
252 * Pre Loops are while and for loops.
254 static void handle_pre_loop(struct statement
*stmt
)
256 int once_through
; /* we go through the loop at least once */
257 struct sm_state
*extra_sm
= NULL
;
260 struct state_list
*slist
= NULL
;
261 struct sm_state
*sm
= NULL
;
263 loop_name
= get_loop_name(loop_num
);
266 __split_stmt(stmt
->iterator_pre_statement
);
268 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
274 __merge_gotos(loop_name
);
276 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
277 __in_pre_condition
++;
278 __pass_to_client(stmt
, PRELOOP_HOOK
);
279 __split_whole_condition(stmt
->iterator_pre_condition
);
280 __in_pre_condition
--;
281 FOR_EACH_PTR(slist
, sm
) {
282 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
283 } END_FOR_EACH_PTR(sm
);
286 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
288 if (option_assume_loops
)
291 __split_stmt(stmt
->iterator_statement
);
292 __warn_on_silly_pre_loops();
293 if (is_forever_loop(stmt
)) {
294 struct state_list
*slist
;
296 __save_gotos(loop_name
);
298 __push_fake_cur_slist();
299 __split_stmt(stmt
->iterator_post_statement
);
300 slist
= __pop_fake_cur_slist();
302 __discard_continues();
303 __discard_false_states();
306 if (!__path_is_null())
307 __merge_slist_into_cur(slist
);
311 unchanged
= __iterator_unchanged(extra_sm
);
312 __split_stmt(stmt
->iterator_post_statement
);
313 __save_gotos(loop_name
);
314 __split_whole_condition(stmt
->iterator_pre_condition
);
316 __merge_false_states();
318 __discard_false_states();
320 __merge_false_states();
322 if (extra_sm
&& unchanged
)
323 __extra_pre_loop_hook_after(extra_sm
,
324 stmt
->iterator_post_statement
,
325 stmt
->iterator_pre_condition
);
332 * Post loops are do {} while();
334 static void handle_post_loop(struct statement
*stmt
)
338 loop_name
= get_loop_name(loop_num
);
344 __merge_gotos(loop_name
);
345 __split_stmt(stmt
->iterator_statement
);
347 if (!is_zero(stmt
->iterator_post_condition
))
348 __save_gotos(loop_name
);
350 if (is_forever_loop(stmt
)) {
353 __split_whole_condition(stmt
->iterator_post_condition
);
354 __use_false_states();
360 static int empty_statement(struct statement
*stmt
)
364 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
369 static int last_stmt_on_same_line()
371 struct statement
*stmt
;
374 FOR_EACH_PTR_REVERSE(big_statement_stack
, stmt
) {
377 if (stmt
->pos
.line
== get_lineno())
380 } END_FOR_EACH_PTR_REVERSE(stmt
);
384 static struct statement
*last_stmt
;
385 static int is_last_stmt(struct statement
*stmt
)
387 if (stmt
== last_stmt
)
392 static void print_unreached_initializers(struct symbol_list
*sym_list
)
396 FOR_EACH_PTR(sym_list
, sym
) {
397 if (sym
->initializer
)
398 sm_msg("info: '%s' is not actually initialized (unreached code).",
399 (sym
->ident
? sym
->ident
->name
: "this variable"));
400 } END_FOR_EACH_PTR(sym
);
403 static void print_unreached(struct statement
*stmt
)
405 static int print
= 1;
407 if (!__path_is_null()) {
414 switch (stmt
->type
) {
415 case STMT_COMPOUND
: /* after a switch before a case stmt */
420 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
421 print_unreached_initializers(stmt
->declaration
);
423 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
424 if (is_last_stmt(stmt
))
434 if (!option_spammy
&& empty_statement(stmt
))
436 sm_msg("info: ignoring unreachable code.");
440 static void split_asm_constraints(struct expression_list
*expr_list
)
442 struct expression
*expr
;
445 FOR_EACH_PTR(expr_list
, expr
) {
447 case 0: /* identifier */
448 case 1: /* constraint */
451 case 2: /* expression */
456 } END_FOR_EACH_PTR(expr
);
459 static int is_case_val(struct statement
*stmt
, long long val
)
463 if (stmt
->type
!= STMT_CASE
)
465 if (!stmt
->case_expression
) {
469 if (!get_value(stmt
->case_expression
, &case_val
))
476 static void split_known_switch(struct statement
*stmt
, long long val
)
478 struct statement
*tmp
;
480 __split_expr(stmt
->switch_expression
);
482 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
483 __save_switch_states(top_expression(switch_expr_stack
));
488 stmt
= stmt
->switch_statement
;
491 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
493 __push_scope_hooks();
494 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
495 __smatch_lineno
= tmp
->pos
.line
;
496 if (is_case_val(tmp
, val
)) {
497 __merge_switches(top_expression(switch_expr_stack
),
498 stmt
->case_expression
);
499 __pass_case_to_client(top_expression(switch_expr_stack
),
500 stmt
->case_expression
);
502 if (__path_is_null())
505 if (__path_is_null()) {
509 } END_FOR_EACH_PTR(tmp
);
511 __call_scope_hooks();
512 if (!__pop_default())
513 __merge_switches(top_expression(switch_expr_stack
),
515 __discard_switches();
517 pop_expression(&switch_expr_stack
);
520 void __split_stmt(struct statement
*stmt
)
527 if (out_of_memory() || __bail_on_rest_of_function
) {
528 static char *printed
= NULL
;
530 if (printed
!= cur_func
)
531 sm_msg("Function too hairy. Giving up.");
536 add_ptr_list(&big_statement_stack
, stmt
);
537 free_expression_stack(&big_expression_stack
);
538 set_position(stmt
->pos
);
539 print_unreached(stmt
);
540 __pass_to_client(stmt
, STMT_HOOK
);
542 switch (stmt
->type
) {
543 case STMT_DECLARATION
:
544 split_declaration(stmt
->declaration
);
547 __split_expr(stmt
->ret_value
);
548 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
551 case STMT_EXPRESSION
:
552 __split_expr(stmt
->expression
);
554 case STMT_COMPOUND
: {
555 struct statement
*tmp
;
558 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
559 __push_scope_hooks();
560 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
562 } END_FOR_EACH_PTR(tmp
);
563 __call_scope_hooks();
567 if (known_condition_true(stmt
->if_conditional
)) {
568 __split_stmt(stmt
->if_true
);
571 if (known_condition_false(stmt
->if_conditional
)) {
572 __split_stmt(stmt
->if_false
);
575 if (option_known_conditions
&&
576 implied_condition_true(stmt
->if_conditional
)) {
577 sm_info("this condition is true.");
578 __split_stmt(stmt
->if_true
);
581 if (option_known_conditions
&&
582 implied_condition_false(stmt
->if_conditional
)) {
583 sm_info("this condition is false.");
584 __split_stmt(stmt
->if_false
);
587 __split_whole_condition(stmt
->if_conditional
);
588 __split_stmt(stmt
->if_true
);
589 if (empty_statement(stmt
->if_true
) &&
590 last_stmt_on_same_line() &&
591 !get_macro_name(stmt
->if_true
->pos
))
592 sm_msg("warn: if();");
593 __push_true_states();
594 __use_false_states();
595 __split_stmt(stmt
->if_false
);
596 __merge_true_states();
599 if (stmt
->iterator_pre_condition
)
600 handle_pre_loop(stmt
);
601 else if (stmt
->iterator_post_condition
)
602 handle_post_loop(stmt
);
604 // these are for(;;) type loops.
605 handle_pre_loop(stmt
);
609 if (get_value(stmt
->switch_expression
, &val
)) {
610 split_known_switch(stmt
, val
);
613 __split_expr(stmt
->switch_expression
);
614 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
615 __save_switch_states(top_expression(switch_expr_stack
));
619 __split_stmt(stmt
->switch_statement
);
620 if (!__pop_default())
621 __merge_switches(top_expression(switch_expr_stack
),
623 __discard_switches();
625 pop_expression(&switch_expr_stack
);
628 __merge_switches(top_expression(switch_expr_stack
),
629 stmt
->case_expression
);
630 __pass_case_to_client(top_expression(switch_expr_stack
),
631 stmt
->case_expression
);
632 if (!stmt
->case_expression
)
634 __split_expr(stmt
->case_expression
);
635 __split_expr(stmt
->case_to
);
636 __split_stmt(stmt
->case_statement
);
639 if (stmt
->label_identifier
&&
640 stmt
->label_identifier
->type
== SYM_LABEL
&&
641 stmt
->label_identifier
->ident
) {
642 loop_count
= 1000000;
643 __merge_gotos(stmt
->label_identifier
->ident
->name
);
645 __split_stmt(stmt
->label_statement
);
648 __split_expr(stmt
->goto_expression
);
649 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
650 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
652 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
654 __process_continues();
656 } else if (stmt
->goto_label
&&
657 stmt
->goto_label
->type
== SYM_LABEL
&&
658 stmt
->goto_label
->ident
) {
659 __save_gotos(stmt
->goto_label
->ident
->name
);
666 __pass_to_client(stmt
, ASM_HOOK
);
667 __split_expr(stmt
->asm_string
);
668 split_asm_constraints(stmt
->asm_outputs
);
669 split_asm_constraints(stmt
->asm_inputs
);
670 split_asm_constraints(stmt
->asm_clobbers
);
675 __split_expr(stmt
->range_expression
);
676 __split_expr(stmt
->range_low
);
677 __split_expr(stmt
->range_high
);
682 static void split_expr_list(struct expression_list
*expr_list
)
684 struct expression
*expr
;
686 FOR_EACH_PTR(expr_list
, expr
) {
688 } END_FOR_EACH_PTR(expr
);
691 static void split_sym(struct symbol
*sym
)
695 if (!(sym
->namespace & NS_SYMBOL
))
698 __split_stmt(sym
->stmt
);
699 __split_expr(sym
->array_size
);
700 split_symlist(sym
->arguments
);
701 split_symlist(sym
->symbol_list
);
702 __split_stmt(sym
->inline_stmt
);
703 split_symlist(sym
->inline_symbol_list
);
706 static void split_symlist(struct symbol_list
*sym_list
)
710 FOR_EACH_PTR(sym_list
, sym
) {
712 } END_FOR_EACH_PTR(sym
);
715 static struct expression
*fake_assign_expr(struct symbol
*sym
)
717 struct expression
*e_assign
, *e_symbol
;
719 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
720 e_symbol
= alloc_expression(sym
->pos
, EXPR_SYMBOL
);
721 e_assign
->op
= (int)'=';
722 e_symbol
->symbol
= sym
;
723 e_symbol
->symbol_name
= sym
->ident
;
724 e_assign
->left
= e_symbol
;
725 e_assign
->right
= sym
->initializer
;
729 static void do_initializer_stuff(struct symbol
*sym
)
731 struct expression
*assign
;
733 if (!sym
->initializer
)
735 assign
= fake_assign_expr(sym
);
736 __split_expr(assign
);
739 static void split_declaration(struct symbol_list
*sym_list
)
743 FOR_EACH_PTR(sym_list
, sym
) {
744 __pass_to_client(sym
, DECLARATION_HOOK
);
745 do_initializer_stuff(sym
);
747 } END_FOR_EACH_PTR(sym
);
750 static void split_function(struct symbol
*sym
)
752 struct symbol
*base_type
= get_base_type(sym
);
756 line_func_start
= base_type
->stmt
->pos
.line
;
758 cur_func
= sym
->ident
->name
;
759 __smatch_lineno
= sym
->pos
.line
;
762 sm_debug("new function: %s\n", cur_func
);
763 if (option_two_passes
) {
767 __pass_to_client(sym
, FUNC_DEF_HOOK
);
768 __split_stmt(base_type
->stmt
);
769 __split_stmt(base_type
->inline_stmt
);
775 __pass_to_client(sym
, FUNC_DEF_HOOK
);
776 __split_stmt(base_type
->stmt
);
777 __split_stmt(base_type
->inline_stmt
);
778 __pass_to_client(sym
, END_FUNC_HOOK
);
782 free_data_info_allocs();
783 free_expression_stack(&switch_expr_stack
);
784 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
785 __bail_on_rest_of_function
= 0;
788 static struct symbol_list
*inlines_called
;
789 static void add_inline_function(struct symbol
*sym
)
791 static struct symbol_list
*already_added
;
794 FOR_EACH_PTR(already_added
, tmp
) {
797 } END_FOR_EACH_PTR(tmp
);
799 add_ptr_list(&already_added
, sym
);
800 add_ptr_list(&inlines_called
, sym
);
803 static void process_inlines()
807 FOR_EACH_PTR(inlines_called
, tmp
) {
809 } END_FOR_EACH_PTR(tmp
);
810 free_ptr_list(&inlines_called
);
813 static void split_functions(struct symbol_list
*sym_list
)
817 FOR_EACH_PTR(sym_list
, sym
) {
818 set_position(sym
->pos
);
819 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
823 __pass_to_client(sym
, BASE_HOOK
);
825 } END_FOR_EACH_PTR(sym
);
826 __pass_to_client_no_data(END_FILE_HOOK
);
829 void smatch(int argc
, char **argv
)
832 struct string_list
*filelist
= NULL
;
833 struct symbol_list
*sym_list
;
836 printf("Usage: smatch [--debug] <filename.c>\n");
839 sparse_initialize(argc
, argv
, &filelist
);
840 FOR_EACH_PTR_NOTAG(filelist
, base_file
) {
841 if (option_file_output
) {
844 snprintf(buf
, sizeof(buf
), "%s.smatch", base_file
);
845 sm_outfd
= fopen(buf
, "w");
847 printf("Error: Cannot open %s\n", base_file
);
851 sym_list
= sparse_keep_tokens(base_file
);
852 split_functions(sym_list
);
853 } END_FOR_EACH_PTR_NOTAG(base_file
);