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
);
109 __pass_to_client(expr
, BINOP_HOOK
);
113 __split_expr(expr
->left
);
114 __split_expr(expr
->right
);
116 case EXPR_ASSIGNMENT
: {
117 struct expression
*tmp
;
120 if (__handle_condition_assigns(expr
))
123 /* foo = (x < 5 ? foo : 5); */
124 if (__handle_select_assigns(expr
))
127 __split_expr(expr
->right
);
128 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
129 tmp
= strip_expr(expr
->right
);
130 if (tmp
->type
== EXPR_CALL
)
131 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
132 __split_expr(expr
->left
);
136 __pass_to_client(expr
, DEREF_HOOK
);
137 __split_expr(expr
->deref
);
140 __split_expr(expr
->base
);
143 case EXPR_FORCE_CAST
:
144 __split_expr(expr
->cast_expression
);
147 /* there isn't anything to pass a client from inside a sizeof() */
149 case EXPR_CONDITIONAL
:
151 __split_whole_condition(expr
->conditional
);
152 __split_expr(expr
->cond_true
);
153 __push_true_states();
154 __use_false_states();
155 __split_expr(expr
->cond_false
);
156 __merge_true_states();
159 split_expr_list(expr
->args
);
160 __split_expr(expr
->fn
);
161 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
163 case EXPR_INITIALIZER
:
164 split_expr_list(expr
->expr_list
);
166 case EXPR_IDENTIFIER
:
167 __split_expr(expr
->ident_expression
);
170 __split_expr(expr
->idx_expression
);
173 __split_expr(expr
->init_expr
);
176 __pass_to_client(expr
, SYM_HOOK
);
179 __pass_to_client(expr
, STRING_HOOK
);
184 pop_expression(&big_expression_stack
);
187 static int is_forever_loop(struct statement
*stmt
)
190 struct expression
*expr
;
192 expr
= strip_expr(stmt
->iterator_pre_condition
);
194 expr
= stmt
->iterator_post_condition
;
196 /* this is a for(;;) loop... */
200 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
208 static char *get_loop_name(int num
)
212 snprintf(buf
, 255, "-loop%d", num
);
214 return alloc_sname(buf
);;
218 * Pre Loops are while and for loops.
221 static void handle_pre_loop(struct statement
*stmt
)
223 int once_through
; /* we go through the loop at least once */
224 struct sm_state
*extra_sm
= NULL
;
227 struct state_list
*slist
= NULL
;
228 struct sm_state
*sm
= NULL
;
230 loop_name
= get_loop_name(loop_num
);
233 __split_stmt(stmt
->iterator_pre_statement
);
235 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
241 __merge_gotos(loop_name
);
243 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
244 __in_pre_condition
++;
245 __split_whole_condition(stmt
->iterator_pre_condition
);
246 __in_pre_condition
--;
247 FOR_EACH_PTR(slist
, sm
) {
248 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
249 } END_FOR_EACH_PTR(sm
);
252 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
254 if (option_assume_loops
)
257 __split_stmt(stmt
->iterator_statement
);
258 __warn_on_silly_pre_loops();
259 if (is_forever_loop(stmt
)) {
260 __save_gotos(loop_name
);
261 /* forever loops don't have an iterator_post_statement */
262 __discard_continues();
263 __discard_false_states();
267 unchanged
= __iterator_unchanged(extra_sm
);
268 __split_stmt(stmt
->iterator_post_statement
);
269 __save_gotos(loop_name
);
270 __split_whole_condition(stmt
->iterator_pre_condition
);
272 __merge_false_states();
274 __discard_false_states();
276 __merge_false_states();
279 if (extra_sm
&& unchanged
)
280 __extra_pre_loop_hook_after(extra_sm
,
281 stmt
->iterator_post_statement
,
282 stmt
->iterator_pre_condition
);
289 * Post loops are do {} while();
291 static void handle_post_loop(struct statement
*stmt
)
295 loop_name
= get_loop_name(loop_num
);
301 __merge_gotos(loop_name
);
302 __split_stmt(stmt
->iterator_statement
);
304 if (!is_zero(stmt
->iterator_post_condition
))
305 __save_gotos(loop_name
);
307 if (is_forever_loop(stmt
)) {
310 __split_whole_condition(stmt
->iterator_post_condition
);
311 __use_false_states();
317 static int empty_statement(struct statement
*stmt
)
321 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
326 static struct statement
*last_stmt
;
327 static int is_last_stmt(struct statement
*stmt
)
329 if (stmt
== last_stmt
)
334 static void print_unreached_initializers(struct symbol_list
*sym_list
)
338 FOR_EACH_PTR(sym_list
, sym
) {
340 sm_msg("info: '%s' is not actually initialized (unreached code).",
341 (sym
->ident
? sym
->ident
->name
: "this variable"));
342 } END_FOR_EACH_PTR(sym
);
345 static void print_unreached(struct statement
*stmt
)
348 static int print
= 1;
350 if (!__path_is_null()) {
357 switch (stmt
->type
) {
358 case STMT_COMPOUND
: /* after a switch before a case stmt */
363 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
364 print_unreached_initializers(stmt
->declaration
);
366 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
367 if (is_last_stmt(stmt
))
377 if (!option_spammy
&& empty_statement(stmt
))
379 sm_msg("info: ignoring unreachable code.");
383 void __split_stmt(struct statement
*stmt
)
388 if (out_of_memory() || __bail_on_rest_of_function
) {
389 static char *printed
= NULL
;
391 if (printed
!= cur_func
)
392 sm_msg("Function too hairy. Giving up.");
397 add_ptr_list(&big_statement_stack
, stmt
);
398 free_expression_stack(&big_expression_stack
);
399 __smatch_lineno
= stmt
->pos
.line
;
400 print_unreached(stmt
);
401 __pass_to_client(stmt
, STMT_HOOK
);
403 switch (stmt
->type
) {
404 case STMT_DECLARATION
:
405 split_declaration(stmt
->declaration
);
408 __split_expr(stmt
->ret_value
);
409 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
412 case STMT_EXPRESSION
:
413 __split_expr(stmt
->expression
);
415 case STMT_COMPOUND
: {
416 struct statement
*tmp
;
417 struct statement
*prev
= NULL
;
420 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
421 __push_scope_hooks();
422 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
431 } END_FOR_EACH_PTR(tmp
);
432 if (next_stmt
== prev
)
435 __call_scope_hooks();
439 if (known_condition_true(stmt
->if_conditional
)) {
440 __split_stmt(stmt
->if_true
);
443 if (known_condition_false(stmt
->if_conditional
)) {
444 __split_stmt(stmt
->if_false
);
447 if (option_known_conditions
&&
448 implied_condition_true(stmt
->if_conditional
)) {
449 sm_info("this condition is true.");
450 __split_stmt(stmt
->if_true
);
453 if (option_known_conditions
&&
454 implied_condition_false(stmt
->if_conditional
)) {
455 sm_info("this condition is false.");
456 __split_stmt(stmt
->if_false
);
459 __split_whole_condition(stmt
->if_conditional
);
460 __split_stmt(stmt
->if_true
);
461 if (empty_statement(stmt
->if_true
))
462 sm_msg("warn: if();");
463 __push_true_states();
464 __use_false_states();
465 __split_stmt(stmt
->if_false
);
466 __merge_true_states();
469 if (stmt
->iterator_pre_condition
)
470 handle_pre_loop(stmt
);
471 else if (stmt
->iterator_post_condition
)
472 handle_post_loop(stmt
);
474 // these are for(;;) type loops.
475 handle_pre_loop(stmt
);
479 __split_expr(stmt
->switch_expression
);
480 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
481 __save_switch_states(top_expression(switch_expr_stack
));
485 __split_stmt(stmt
->switch_statement
);
486 if (!__pop_default())
487 __merge_switches(top_expression(switch_expr_stack
),
489 __discard_switches();
491 pop_expression(&switch_expr_stack
);
494 __merge_switches(top_expression(switch_expr_stack
),
495 stmt
->case_expression
);
496 __pass_case_to_client(top_expression(switch_expr_stack
),
497 stmt
->case_expression
);
498 if (!stmt
->case_expression
)
500 __split_expr(stmt
->case_expression
);
501 __split_expr(stmt
->case_to
);
502 __split_stmt(stmt
->case_statement
);
506 stmt
->label
->type
== SYM_LABEL
&&
507 stmt
->label
->ident
) {
508 loop_count
= 1000000;
509 __merge_gotos(stmt
->label
->ident
->name
);
511 __split_stmt(stmt
->label_statement
);
514 __split_expr(stmt
->goto_expression
);
515 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
516 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
518 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
520 __process_continues();
522 } else if (stmt
->goto_label
&&
523 stmt
->goto_label
->type
== SYM_LABEL
&&
524 stmt
->goto_label
->ident
) {
525 __save_gotos(stmt
->goto_label
->ident
->name
);
532 __split_expr(stmt
->asm_string
);
533 split_expr_list(stmt
->asm_outputs
);
534 split_expr_list(stmt
->asm_inputs
);
535 split_expr_list(stmt
->asm_clobbers
);
540 __split_expr(stmt
->range_expression
);
541 __split_expr(stmt
->range_low
);
542 __split_expr(stmt
->range_high
);
547 static void split_expr_list(struct expression_list
*expr_list
)
549 struct expression
*expr
;
550 FOR_EACH_PTR(expr_list
, expr
) {
552 } END_FOR_EACH_PTR(expr
);
556 static void split_sym(struct symbol
*sym
)
560 if (!(sym
->namespace & NS_SYMBOL
))
563 __split_stmt(sym
->stmt
);
564 __split_expr(sym
->array_size
);
565 split_symlist(sym
->arguments
);
566 split_symlist(sym
->symbol_list
);
567 __split_stmt(sym
->inline_stmt
);
568 split_symlist(sym
->inline_symbol_list
);
571 static void split_symlist(struct symbol_list
*sym_list
)
575 FOR_EACH_PTR(sym_list
, sym
) {
577 } END_FOR_EACH_PTR(sym
);
580 static struct expression
*fake_assign_expr(struct symbol
*sym
)
582 struct expression
*e_assign
, *e_symbol
;
584 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
585 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
586 e_assign
->op
= (int)'=';
587 e_symbol
->symbol
= sym
;
588 e_symbol
->symbol_name
= sym
->ident
;
589 e_assign
->left
= e_symbol
;
590 e_assign
->right
= sym
->initializer
;
594 static void do_initializer_stuff(struct symbol
*sym
)
596 struct expression
*assign
;
598 if(!sym
->initializer
)
600 assign
= fake_assign_expr(sym
);
601 __split_expr(assign
);
604 static void split_declaration(struct symbol_list
*sym_list
)
608 FOR_EACH_PTR(sym_list
, sym
) {
609 __pass_to_client(sym
, DECLARATION_HOOK
);
610 do_initializer_stuff(sym
);
612 } END_FOR_EACH_PTR(sym
);
615 static void split_function(struct symbol
*sym
)
617 struct symbol
*base_type
= get_base_type(sym
);
621 line_func_start
= base_type
->stmt
->pos
.line
;
623 cur_func
= sym
->ident
->name
;
624 __smatch_lineno
= sym
->pos
.line
;
627 sm_debug("new function: %s\n", cur_func
);
628 if (option_two_passes
) {
632 __pass_to_client(sym
, FUNC_DEF_HOOK
);
633 __split_stmt(base_type
->stmt
);
639 __pass_to_client(sym
, FUNC_DEF_HOOK
);
640 __split_stmt(base_type
->stmt
);
641 __pass_to_client(sym
, END_FUNC_HOOK
);
645 free_data_info_allocs();
646 free_expression_stack(&switch_expr_stack
);
647 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
648 __bail_on_rest_of_function
= 0;
651 static void split_functions(struct symbol_list
*sym_list
)
655 FOR_EACH_PTR(sym_list
, sym
) {
656 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
659 __pass_to_client(sym
, BASE_HOOK
);
661 } END_FOR_EACH_PTR(sym
);
662 __pass_to_client_no_data(END_FILE_HOOK
);
665 void smatch (int argc
, char **argv
)
668 struct string_list
*filelist
= NULL
;
669 struct symbol_list
*sym_list
;
673 printf("Usage: smatch [--debug] <filename.c>\n");
676 sparse_initialize(argc
, argv
, &filelist
);
677 FOR_EACH_PTR_NOTAG(filelist
, file
) {
678 sym_list
= __sparse(file
);
679 split_functions(sym_list
);
680 } END_FOR_EACH_PTR_NOTAG(file
);