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 __pass_to_client(expr
, LOGIC_HOOK
);
111 __handle_logic(expr
);
114 __pass_to_client(expr
, BINOP_HOOK
);
116 __split_expr(expr
->left
);
117 __split_expr(expr
->right
);
119 case EXPR_ASSIGNMENT
: {
120 struct expression
*tmp
;
123 if (__handle_condition_assigns(expr
))
126 /* foo = (x < 5 ? foo : 5); */
127 if (__handle_select_assigns(expr
))
130 __split_expr(expr
->right
);
131 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
132 tmp
= strip_expr(expr
->right
);
133 if (tmp
->type
== EXPR_CALL
)
134 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
135 __split_expr(expr
->left
);
139 __pass_to_client(expr
, DEREF_HOOK
);
140 __split_expr(expr
->deref
);
143 __split_expr(expr
->base
);
146 case EXPR_FORCE_CAST
:
147 __split_expr(expr
->cast_expression
);
150 /* there isn't anything to pass a client from inside a sizeof() */
152 case EXPR_CONDITIONAL
:
154 __split_whole_condition(expr
->conditional
);
155 __split_expr(expr
->cond_true
);
156 __push_true_states();
157 __use_false_states();
158 __split_expr(expr
->cond_false
);
159 __merge_true_states();
162 split_expr_list(expr
->args
);
163 __split_expr(expr
->fn
);
164 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
166 case EXPR_INITIALIZER
:
167 split_expr_list(expr
->expr_list
);
169 case EXPR_IDENTIFIER
:
170 __split_expr(expr
->ident_expression
);
173 __split_expr(expr
->idx_expression
);
176 __split_expr(expr
->init_expr
);
179 __pass_to_client(expr
, SYM_HOOK
);
182 __pass_to_client(expr
, STRING_HOOK
);
187 pop_expression(&big_expression_stack
);
190 static int is_forever_loop(struct statement
*stmt
)
193 struct expression
*expr
;
195 expr
= strip_expr(stmt
->iterator_pre_condition
);
197 expr
= stmt
->iterator_post_condition
;
199 /* this is a for(;;) loop... */
203 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
211 static char *get_loop_name(int num
)
215 snprintf(buf
, 255, "-loop%d", num
);
217 return alloc_sname(buf
);;
221 * Pre Loops are while and for loops.
224 static void handle_pre_loop(struct statement
*stmt
)
226 int once_through
; /* we go through the loop at least once */
227 struct sm_state
*extra_sm
= NULL
;
230 struct state_list
*slist
= NULL
;
231 struct sm_state
*sm
= NULL
;
233 loop_name
= get_loop_name(loop_num
);
236 __split_stmt(stmt
->iterator_pre_statement
);
238 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
244 __merge_gotos(loop_name
);
246 extra_sm
= __extra_handle_canonical_loops(stmt
, &slist
);
247 __in_pre_condition
++;
248 __split_whole_condition(stmt
->iterator_pre_condition
);
249 __in_pre_condition
--;
250 FOR_EACH_PTR(slist
, sm
) {
251 set_state(sm
->owner
, sm
->name
, sm
->sym
, sm
->state
);
252 } END_FOR_EACH_PTR(sm
);
255 extra_sm
= get_sm_state(extra_sm
->owner
, extra_sm
->name
, extra_sm
->sym
);
257 if (option_assume_loops
)
260 __split_stmt(stmt
->iterator_statement
);
261 __warn_on_silly_pre_loops();
262 if (is_forever_loop(stmt
)) {
263 __save_gotos(loop_name
);
264 /* forever loops don't have an iterator_post_statement */
265 __discard_continues();
266 __discard_false_states();
270 unchanged
= __iterator_unchanged(extra_sm
);
271 __split_stmt(stmt
->iterator_post_statement
);
272 __save_gotos(loop_name
);
273 __split_whole_condition(stmt
->iterator_pre_condition
);
275 __merge_false_states();
277 __discard_false_states();
279 __merge_false_states();
282 if (extra_sm
&& unchanged
)
283 __extra_pre_loop_hook_after(extra_sm
,
284 stmt
->iterator_post_statement
,
285 stmt
->iterator_pre_condition
);
292 * Post loops are do {} while();
294 static void handle_post_loop(struct statement
*stmt
)
298 loop_name
= get_loop_name(loop_num
);
304 __merge_gotos(loop_name
);
305 __split_stmt(stmt
->iterator_statement
);
307 if (!is_zero(stmt
->iterator_post_condition
))
308 __save_gotos(loop_name
);
310 if (is_forever_loop(stmt
)) {
313 __split_whole_condition(stmt
->iterator_post_condition
);
314 __use_false_states();
320 static int empty_statement(struct statement
*stmt
)
324 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
329 static struct statement
*last_stmt
;
330 static int is_last_stmt(struct statement
*stmt
)
332 if (stmt
== last_stmt
)
337 static void print_unreached_initializers(struct symbol_list
*sym_list
)
341 FOR_EACH_PTR(sym_list
, sym
) {
343 sm_msg("info: '%s' is not actually initialized (unreached code).",
344 (sym
->ident
? sym
->ident
->name
: "this variable"));
345 } END_FOR_EACH_PTR(sym
);
348 static void print_unreached(struct statement
*stmt
)
351 static int print
= 1;
353 if (!__path_is_null()) {
360 switch (stmt
->type
) {
361 case STMT_COMPOUND
: /* after a switch before a case stmt */
366 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
367 print_unreached_initializers(stmt
->declaration
);
369 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
370 if (is_last_stmt(stmt
))
380 if (!option_spammy
&& empty_statement(stmt
))
382 sm_msg("info: ignoring unreachable code.");
386 void __split_stmt(struct statement
*stmt
)
391 if (out_of_memory() || __bail_on_rest_of_function
) {
392 static char *printed
= NULL
;
394 if (printed
!= cur_func
)
395 sm_msg("Function too hairy. Giving up.");
400 add_ptr_list(&big_statement_stack
, stmt
);
401 free_expression_stack(&big_expression_stack
);
402 __smatch_lineno
= stmt
->pos
.line
;
403 print_unreached(stmt
);
404 __pass_to_client(stmt
, STMT_HOOK
);
406 switch (stmt
->type
) {
407 case STMT_DECLARATION
:
408 split_declaration(stmt
->declaration
);
411 __split_expr(stmt
->ret_value
);
412 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
415 case STMT_EXPRESSION
:
416 __split_expr(stmt
->expression
);
418 case STMT_COMPOUND
: {
419 struct statement
*tmp
;
420 struct statement
*prev
= NULL
;
423 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
424 __push_scope_hooks();
425 FOR_EACH_PTR(stmt
->stmts
, tmp
) {
434 } END_FOR_EACH_PTR(tmp
);
435 if (next_stmt
== prev
)
438 __call_scope_hooks();
442 if (known_condition_true(stmt
->if_conditional
)) {
443 __split_stmt(stmt
->if_true
);
446 if (known_condition_false(stmt
->if_conditional
)) {
447 __split_stmt(stmt
->if_false
);
450 if (option_known_conditions
&&
451 implied_condition_true(stmt
->if_conditional
)) {
452 sm_info("this condition is true.");
453 __split_stmt(stmt
->if_true
);
456 if (option_known_conditions
&&
457 implied_condition_false(stmt
->if_conditional
)) {
458 sm_info("this condition is false.");
459 __split_stmt(stmt
->if_false
);
462 __split_whole_condition(stmt
->if_conditional
);
463 __split_stmt(stmt
->if_true
);
464 if (empty_statement(stmt
->if_true
))
465 sm_msg("warn: if();");
466 __push_true_states();
467 __use_false_states();
468 __split_stmt(stmt
->if_false
);
469 __merge_true_states();
472 if (stmt
->iterator_pre_condition
)
473 handle_pre_loop(stmt
);
474 else if (stmt
->iterator_post_condition
)
475 handle_post_loop(stmt
);
477 // these are for(;;) type loops.
478 handle_pre_loop(stmt
);
482 __split_expr(stmt
->switch_expression
);
483 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
484 __save_switch_states(top_expression(switch_expr_stack
));
488 __split_stmt(stmt
->switch_statement
);
489 if (!__pop_default())
490 __merge_switches(top_expression(switch_expr_stack
),
492 __discard_switches();
494 pop_expression(&switch_expr_stack
);
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
);
501 if (!stmt
->case_expression
)
503 __split_expr(stmt
->case_expression
);
504 __split_expr(stmt
->case_to
);
505 __split_stmt(stmt
->case_statement
);
509 stmt
->label
->type
== SYM_LABEL
&&
510 stmt
->label
->ident
) {
511 loop_count
= 1000000;
512 __merge_gotos(stmt
->label
->ident
->name
);
514 __split_stmt(stmt
->label_statement
);
517 __split_expr(stmt
->goto_expression
);
518 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
519 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
521 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
523 __process_continues();
525 } else if (stmt
->goto_label
&&
526 stmt
->goto_label
->type
== SYM_LABEL
&&
527 stmt
->goto_label
->ident
) {
528 __save_gotos(stmt
->goto_label
->ident
->name
);
535 __split_expr(stmt
->asm_string
);
536 split_expr_list(stmt
->asm_outputs
);
537 split_expr_list(stmt
->asm_inputs
);
538 split_expr_list(stmt
->asm_clobbers
);
543 __split_expr(stmt
->range_expression
);
544 __split_expr(stmt
->range_low
);
545 __split_expr(stmt
->range_high
);
550 static void split_expr_list(struct expression_list
*expr_list
)
552 struct expression
*expr
;
553 FOR_EACH_PTR(expr_list
, expr
) {
555 } END_FOR_EACH_PTR(expr
);
559 static void split_sym(struct symbol
*sym
)
563 if (!(sym
->namespace & NS_SYMBOL
))
566 __split_stmt(sym
->stmt
);
567 __split_expr(sym
->array_size
);
568 split_symlist(sym
->arguments
);
569 split_symlist(sym
->symbol_list
);
570 __split_stmt(sym
->inline_stmt
);
571 split_symlist(sym
->inline_symbol_list
);
574 static void split_symlist(struct symbol_list
*sym_list
)
578 FOR_EACH_PTR(sym_list
, sym
) {
580 } END_FOR_EACH_PTR(sym
);
583 static struct expression
*fake_assign_expr(struct symbol
*sym
)
585 struct expression
*e_assign
, *e_symbol
;
587 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
588 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
589 e_assign
->op
= (int)'=';
590 e_symbol
->symbol
= sym
;
591 e_symbol
->symbol_name
= sym
->ident
;
592 e_assign
->left
= e_symbol
;
593 e_assign
->right
= sym
->initializer
;
597 static void do_initializer_stuff(struct symbol
*sym
)
599 struct expression
*assign
;
601 if(!sym
->initializer
)
603 assign
= fake_assign_expr(sym
);
604 __split_expr(assign
);
607 static void split_declaration(struct symbol_list
*sym_list
)
611 FOR_EACH_PTR(sym_list
, sym
) {
612 __pass_to_client(sym
, DECLARATION_HOOK
);
613 do_initializer_stuff(sym
);
615 } END_FOR_EACH_PTR(sym
);
618 static void split_function(struct symbol
*sym
)
620 struct symbol
*base_type
= get_base_type(sym
);
624 line_func_start
= base_type
->stmt
->pos
.line
;
626 cur_func
= sym
->ident
->name
;
627 __smatch_lineno
= sym
->pos
.line
;
630 sm_debug("new function: %s\n", cur_func
);
631 if (option_two_passes
) {
635 __pass_to_client(sym
, FUNC_DEF_HOOK
);
636 __split_stmt(base_type
->stmt
);
642 __pass_to_client(sym
, FUNC_DEF_HOOK
);
643 __split_stmt(base_type
->stmt
);
644 __pass_to_client(sym
, END_FUNC_HOOK
);
648 free_data_info_allocs();
649 free_expression_stack(&switch_expr_stack
);
650 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
651 __bail_on_rest_of_function
= 0;
654 static void split_functions(struct symbol_list
*sym_list
)
658 FOR_EACH_PTR(sym_list
, sym
) {
659 if (sym
->type
== SYM_NODE
&& get_base_type(sym
)->type
== SYM_FN
) {
662 __pass_to_client(sym
, BASE_HOOK
);
664 } END_FOR_EACH_PTR(sym
);
665 __pass_to_client_no_data(END_FILE_HOOK
);
668 void smatch (int argc
, char **argv
)
671 struct string_list
*filelist
= NULL
;
672 struct symbol_list
*sym_list
;
676 printf("Usage: smatch [--debug] <filename.c>\n");
679 sparse_initialize(argc
, argv
, &filelist
);
680 FOR_EACH_PTR_NOTAG(filelist
, file
) {
681 sym_list
= sparse_keep_tokens(file
);
682 split_functions(sym_list
);
683 } END_FOR_EACH_PTR_NOTAG(file
);