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 static int expr_stmt_count
;
30 static struct expression_list
*switch_expr_stack
= NULL
;
31 static struct statement
*next_stmt
;
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 get_func_pos(void) { return __smatch_lineno
- line_func_start
; }
40 int inside_loop(void) { return !!loop_count
; }
41 int in_expression_statement(void) { return !!expr_stmt_count
; }
42 struct statement
*get_next_stmt(void) { return next_stmt
; }
44 static void split_symlist(struct symbol_list
*sym_list
);
45 static void split_declaration(struct symbol_list
*sym_list
);
46 static void split_expr_list(struct expression_list
*expr_list
);
48 int option_assume_loops
= 0;
49 int option_known_conditions
= 0;
50 int option_two_passes
= 0;
51 struct symbol
*cur_func_sym
= NULL
;
53 const char *get_filename(void)
60 static void set_position(struct expression
*expr
)
63 static int prev_stream
= -1;
65 __smatch_lineno
= expr
->pos
.line
;
67 if (expr
->pos
.stream
== prev_stream
)
70 filename
= stream_name(expr
->pos
.stream
);
73 pathname
= getcwd(NULL
, 0);
75 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
76 full_filename
= malloc(len
);
77 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
79 full_filename
= alloc_string(filename
);
84 void __split_expr(struct expression
*expr
)
89 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
91 push_expression(&big_expression_stack
, expr
);
93 __pass_to_client(expr
, EXPR_HOOK
);
98 __pass_to_client(expr
, DEREF_HOOK
);
100 __pass_to_client(expr
, OP_HOOK
);
101 __split_expr(expr
->unop
);
105 __split_stmt(expr
->statement
);
110 __handle_logic(expr
);
113 __pass_to_client(expr
, BINOP_HOOK
);
115 __split_expr(expr
->left
);
116 __split_expr(expr
->right
);
118 case EXPR_ASSIGNMENT
: {
119 struct expression
*tmp
;
122 if (__handle_condition_assigns(expr
))
125 /* foo = (x < 5 ? foo : 5); */
126 if (__handle_select_assigns(expr
))
129 __split_expr(expr
->right
);
130 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
131 tmp
= strip_expr(expr
->right
);
132 if (tmp
->type
== EXPR_CALL
)
133 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
134 __split_expr(expr
->left
);
138 __pass_to_client(expr
, DEREF_HOOK
);
139 __split_expr(expr
->deref
);
142 __split_expr(expr
->base
);
145 case EXPR_FORCE_CAST
:
146 __split_expr(expr
->cast_expression
);
149 /* there isn't anything to pass a client from inside a sizeof() */
151 case EXPR_CONDITIONAL
:
153 __split_whole_condition(expr
->conditional
);
154 __split_expr(expr
->cond_true
);
155 __push_true_states();
156 __use_false_states();
157 __split_expr(expr
->cond_false
);
158 __merge_true_states();
161 split_expr_list(expr
->args
);
162 __split_expr(expr
->fn
);
163 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
165 case EXPR_INITIALIZER
:
166 split_expr_list(expr
->expr_list
);
168 case EXPR_IDENTIFIER
:
169 __split_expr(expr
->ident_expression
);
172 __split_expr(expr
->idx_expression
);
175 __split_expr(expr
->init_expr
);
178 __pass_to_client(expr
, SYM_HOOK
);
181 __pass_to_client(expr
, STRING_HOOK
);
186 pop_expression(&big_expression_stack
);
189 static int is_forever_loop(struct statement
*stmt
)
192 struct expression
*expr
;
194 expr
= strip_expr(stmt
->iterator_pre_condition
);
196 expr
= stmt
->iterator_post_condition
;
198 /* this is a for(;;) loop... */
202 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
210 static char *get_loop_name(int num
)
214 snprintf(buf
, 255, "-loop%d", num
);
216 return alloc_sname(buf
);;
220 * Pre Loops are while and for loops.
223 static void handle_pre_loop(struct statement
*stmt
)
225 int once_through
; /* we go through the loop at least once */
226 struct sm_state
*extra_sm
= NULL
;
229 struct state_list
*slist
= NULL
;
230 struct sm_state
*sm
= NULL
;
232 loop_name
= get_loop_name(loop_num
);
235 __split_stmt(stmt
->iterator_pre_statement
);
237 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
243 __merge_gotos(loop_name
);
245 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
246 __in_pre_condition
++;
247 __split_whole_condition(stmt
->iterator_pre_condition
);
248 __in_pre_condition
--;
249 FOR_EACH_PTR(slist
, sm
) {
250 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
251 } END_FOR_EACH_PTR(sm
);
254 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
256 if (option_assume_loops
)
259 __split_stmt(stmt
->iterator_statement
);
260 __warn_on_silly_pre_loops();
261 if (is_forever_loop(stmt
)) {
262 __save_gotos(loop_name
);
263 /* forever loops don't have an iterator_post_statement */
264 __discard_continues();
265 __discard_false_states();
269 unchanged
= __iterator_unchanged(extra_sm
);
270 __split_stmt(stmt
->iterator_post_statement
);
271 __save_gotos(loop_name
);
272 __split_whole_condition(stmt
->iterator_pre_condition
);
274 __merge_false_states();
276 __discard_false_states();
278 __merge_false_states();
281 if (extra_sm
&& unchanged
)
282 __extra_pre_loop_hook_after(extra_sm
,
283 stmt
->iterator_post_statement
,
284 stmt
->iterator_pre_condition
);
291 * Post loops are do {} while();
293 static void handle_post_loop(struct statement
*stmt
)
297 loop_name
= get_loop_name(loop_num
);
303 __merge_gotos(loop_name
);
304 __split_stmt(stmt
->iterator_statement
);
306 if (!is_zero(stmt
->iterator_post_condition
))
307 __save_gotos(loop_name
);
309 if (is_forever_loop(stmt
)) {
312 __split_whole_condition(stmt
->iterator_post_condition
);
313 __use_false_states();
319 static int empty_statement(struct statement
*stmt
)
323 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
328 static struct statement
*last_stmt
;
329 static int is_last_stmt(struct statement
*stmt
)
331 if (stmt
== last_stmt
)
336 static void print_unreached_initializers(struct symbol_list
*sym_list
)
340 FOR_EACH_PTR(sym_list
, sym
) {
342 sm_msg("info: '%s' is not actually initialized (unreached code).",
343 (sym
->ident
? sym
->ident
->name
: "this variable"));
344 } END_FOR_EACH_PTR(sym
);
347 static void print_unreached(struct statement
*stmt
)
350 static int print
= 1;
352 if (!__path_is_null()) {
359 switch (stmt
->type
) {
360 case STMT_COMPOUND
: /* after a switch before a case stmt */
365 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
366 print_unreached_initializers(stmt
->declaration
);
368 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
369 if (is_last_stmt(stmt
))
379 if (!option_spammy
&& empty_statement(stmt
))
381 sm_msg("info: ignoring unreachable code.");
385 void __split_stmt(struct statement
*stmt
)
390 if (out_of_memory() || __bail_on_rest_of_function
) {
391 static char *printed
= NULL
;
393 if (printed
!= cur_func
)
394 sm_msg("Function too hairy. Giving up.");
399 add_ptr_list(&big_statement_stack
, stmt
);
400 free_expression_stack(&big_expression_stack
);
401 __smatch_lineno
= stmt
->pos
.line
;
402 print_unreached(stmt
);
403 __pass_to_client(stmt
, STMT_HOOK
);
405 switch (stmt
->type
) {
406 case STMT_DECLARATION
:
407 split_declaration(stmt
->declaration
);
410 __split_expr(stmt
->ret_value
);
411 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
414 case STMT_EXPRESSION
:
415 __split_expr(stmt
->expression
);
417 case STMT_COMPOUND
: {
418 struct statement
*tmp
;
419 struct statement
*prev
= NULL
;
422 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
423 __push_scope_hooks();
424 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
433 } END_FOR_EACH_PTR(tmp
);
434 if (next_stmt
== prev
)
437 __call_scope_hooks();
441 if (known_condition_true(stmt
->if_conditional
)) {
442 __split_stmt(stmt
->if_true
);
445 if (known_condition_false(stmt
->if_conditional
)) {
446 __split_stmt(stmt
->if_false
);
449 if (option_known_conditions
&&
450 implied_condition_true(stmt
->if_conditional
)) {
451 sm_info("this condition is true.");
452 __split_stmt(stmt
->if_true
);
455 if (option_known_conditions
&&
456 implied_condition_false(stmt
->if_conditional
)) {
457 sm_info("this condition is false.");
458 __split_stmt(stmt
->if_false
);
461 __split_whole_condition(stmt
->if_conditional
);
462 __split_stmt(stmt
->if_true
);
463 if (empty_statement(stmt
->if_true
))
464 sm_msg("warn: if();");
465 __push_true_states();
466 __use_false_states();
467 __split_stmt(stmt
->if_false
);
468 __merge_true_states();
471 if (stmt
->iterator_pre_condition
)
472 handle_pre_loop(stmt
);
473 else if (stmt
->iterator_post_condition
)
474 handle_post_loop(stmt
);
476 // these are for(;;) type loops.
477 handle_pre_loop(stmt
);
481 __split_expr(stmt
->switch_expression
);
482 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
483 __save_switch_states(top_expression(switch_expr_stack
));
487 __split_stmt(stmt
->switch_statement
);
488 if (!__pop_default())
489 __merge_switches(top_expression(switch_expr_stack
),
491 __discard_switches();
493 pop_expression(&switch_expr_stack
);
496 __merge_switches(top_expression(switch_expr_stack
),
497 stmt
->case_expression
);
498 __pass_case_to_client(top_expression(switch_expr_stack
),
499 stmt
->case_expression
);
500 if (!stmt
->case_expression
)
502 __split_expr(stmt
->case_expression
);
503 __split_expr(stmt
->case_to
);
504 __split_stmt(stmt
->case_statement
);
508 stmt
->label
->type
== SYM_LABEL
&&
509 stmt
->label
->ident
) {
510 loop_count
= 1000000;
511 __merge_gotos(stmt
->label
->ident
->name
);
513 __split_stmt(stmt
->label_statement
);
516 __split_expr(stmt
->goto_expression
);
517 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
518 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
520 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
522 __process_continues();
524 } else if (stmt
->goto_label
&&
525 stmt
->goto_label
->type
== SYM_LABEL
&&
526 stmt
->goto_label
->ident
) {
527 __save_gotos(stmt
->goto_label
->ident
->name
);
534 __split_expr(stmt
->asm_string
);
535 split_expr_list(stmt
->asm_outputs
);
536 split_expr_list(stmt
->asm_inputs
);
537 split_expr_list(stmt
->asm_clobbers
);
542 __split_expr(stmt
->range_expression
);
543 __split_expr(stmt
->range_low
);
544 __split_expr(stmt
->range_high
);
549 static void split_expr_list(struct expression_list
*expr_list
)
551 struct expression
*expr
;
552 FOR_EACH_PTR(expr_list
, expr
) {
554 } END_FOR_EACH_PTR(expr
);
558 static void split_sym(struct symbol
*sym
)
562 if (!(sym
->namespace & NS_SYMBOL
))
565 __split_stmt(sym
->stmt
);
566 __split_expr(sym
->array_size
);
567 split_symlist(sym
->arguments
);
568 split_symlist(sym
->symbol_list
);
569 __split_stmt(sym
->inline_stmt
);
570 split_symlist(sym
->inline_symbol_list
);
573 static void split_symlist(struct symbol_list
*sym_list
)
577 FOR_EACH_PTR(sym_list
, sym
) {
579 } END_FOR_EACH_PTR(sym
);
582 static struct expression
*fake_assign_expr(struct symbol
*sym
)
584 struct expression
*e_assign
, *e_symbol
;
586 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
587 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
588 e_assign
->op
= (int)'=';
589 e_symbol
->symbol
= sym
;
590 e_symbol
->symbol_name
= sym
->ident
;
591 e_assign
->left
= e_symbol
;
592 e_assign
->right
= sym
->initializer
;
596 static void do_initializer_stuff(struct symbol
*sym
)
598 struct expression
*assign
;
600 if(!sym
->initializer
)
602 assign
= fake_assign_expr(sym
);
603 __split_expr(assign
);
606 static void split_declaration(struct symbol_list
*sym_list
)
610 FOR_EACH_PTR(sym_list
, sym
) {
611 __pass_to_client(sym
, DECLARATION_HOOK
);
612 do_initializer_stuff(sym
);
614 } END_FOR_EACH_PTR(sym
);
617 static void split_function(struct symbol
*sym
)
619 struct symbol
*base_type
= get_base_type(sym
);
623 line_func_start
= base_type
->stmt
->pos
.line
;
625 cur_func
= sym
->ident
->name
;
626 __smatch_lineno
= sym
->pos
.line
;
629 sm_debug("new function: %s\n", cur_func
);
630 if (option_two_passes
) {
634 __pass_to_client(sym
, FUNC_DEF_HOOK
);
635 __split_stmt(base_type
->stmt
);
641 __pass_to_client(sym
, FUNC_DEF_HOOK
);
642 __split_stmt(base_type
->stmt
);
643 __pass_to_client(sym
, END_FUNC_HOOK
);
647 free_data_info_allocs();
648 free_expression_stack(&switch_expr_stack
);
649 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
650 __bail_on_rest_of_function
= 0;
653 static void split_functions(struct symbol_list
*sym_list
)
657 FOR_EACH_PTR(sym_list
, sym
) {
658 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
661 __pass_to_client(sym
, BASE_HOOK
);
663 } END_FOR_EACH_PTR(sym
);
664 __pass_to_client_no_data(END_FILE_HOOK
);
667 void smatch (int argc
, char **argv
)
670 struct string_list
*filelist
= NULL
;
671 struct symbol_list
*sym_list
;
675 printf("Usage: smatch [--debug] <filename.c>\n");
678 sparse_initialize(argc
, argv
, &filelist
);
679 FOR_EACH_PTR_NOTAG(filelist
, file
) {
680 sym_list
= sparse_keep_tokens(file
);
681 split_functions(sym_list
);
682 } END_FOR_EACH_PTR_NOTAG(file
);