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" // just for sname.
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 struct expression_list
*switch_expr_stack
= NULL
;
29 struct expression_list
*big_expression_stack
;
30 struct statement_list
*big_statement_stack
;
31 int __in_pre_condition
= 0;
32 int __bail_on_rest_of_function
= 0;
33 char *get_function(void) { return cur_func
; }
34 int get_lineno(void) { return __smatch_lineno
; }
35 int get_func_pos(void) { return __smatch_lineno
- line_func_start
; }
37 static void split_symlist(struct symbol_list
*sym_list
);
38 static void split_declaration(struct symbol_list
*sym_list
);
39 static void split_expr_list(struct expression_list
*expr_list
);
41 int option_assume_loops
= 0;
42 int option_known_conditions
= 0;
43 int option_two_passes
= 0;
44 struct symbol
*cur_func_sym
= NULL
;
46 const char *get_filename(void)
53 static void set_position(struct expression
*expr
)
56 static int prev_stream
= -1;
58 __smatch_lineno
= expr
->pos
.line
;
60 if (expr
->pos
.stream
== prev_stream
)
63 filename
= stream_name(expr
->pos
.stream
);
66 pathname
= getcwd(NULL
, 0);
68 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
69 full_filename
= malloc(len
);
70 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
72 full_filename
= alloc_string(filename
);
77 void __split_expr(struct expression
*expr
)
82 // printf("%d Debug expr_type %d %s\n", get_lineno(), expr->type, show_special(expr->op));
84 push_expression(&big_expression_stack
, expr
);
86 __pass_to_client(expr
, EXPR_HOOK
);
91 __pass_to_client(expr
, DEREF_HOOK
);
93 __pass_to_client(expr
, OP_HOOK
);
94 __split_expr(expr
->unop
);
97 __split_statements(expr
->statement
);
100 __pass_to_client(expr
, BINOP_HOOK
);
104 __split_expr(expr
->left
);
105 __split_expr(expr
->right
);
107 case EXPR_ASSIGNMENT
: {
108 struct expression
*tmp
;
111 if (__handle_condition_assigns(expr
))
114 __split_expr(expr
->right
);
115 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
116 tmp
= strip_expr(expr
->right
);
117 if (tmp
->type
== EXPR_CALL
)
118 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
119 __split_expr(expr
->left
);
123 __pass_to_client(expr
, DEREF_HOOK
);
124 __split_expr(expr
->deref
);
127 __split_expr(expr
->base
);
130 case EXPR_FORCE_CAST
:
131 __split_expr(expr
->cast_expression
);
134 /* there isn't anything to pass a client from inside a sizeof() */
136 case EXPR_CONDITIONAL
:
138 __split_whole_condition(expr
->conditional
);
139 __split_expr(expr
->cond_true
);
140 __push_true_states();
141 __use_false_states();
142 __split_expr(expr
->cond_false
);
143 __merge_true_states();
146 split_expr_list(expr
->args
);
147 __split_expr(expr
->fn
);
148 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
150 case EXPR_INITIALIZER
:
151 split_expr_list(expr
->expr_list
);
153 case EXPR_IDENTIFIER
:
154 __split_expr(expr
->ident_expression
);
157 __split_expr(expr
->idx_expression
);
160 __split_expr(expr
->init_expr
);
163 __pass_to_client(expr
, SYM_HOOK
);
168 pop_expression(&big_expression_stack
);
171 static int is_forever_loop(struct statement
*stmt
)
174 struct expression
*expr
;
176 expr
= strip_expr(stmt
->iterator_pre_condition
);
178 expr
= stmt
->iterator_post_condition
;
180 /* this is a for(;;) loop... */
184 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
192 static char *get_loop_name(int num
)
196 snprintf(buf
, 255, "-loop%d", num
);
198 return alloc_sname(buf
);;
202 * Pre Loops are while and for loops.
205 static void handle_pre_loop(struct statement
*stmt
)
207 int once_through
; /* we go through the loop at least once */
208 struct sm_state
*extra_state
= NULL
;
212 loop_name
= get_loop_name(loop_num
);
215 __split_statements(stmt
->iterator_pre_statement
);
217 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
222 __merge_gotos(loop_name
);
224 __in_pre_condition
++;
225 __split_whole_condition(stmt
->iterator_pre_condition
);
226 __in_pre_condition
--;
228 __extra_handle_canonical_for_loop(stmt
);
230 extra_state
= __extra_pre_loop_hook_before(stmt
->iterator_pre_statement
);
231 if (option_assume_loops
)
234 __split_statements(stmt
->iterator_statement
);
235 __warn_on_silly_pre_loops();
236 if (is_forever_loop(stmt
)) {
237 __save_gotos(loop_name
);
238 /* forever loops don't have an iterator_post_statement */
240 __pop_false_states();
242 } else if (once_through
) {
245 unchanged
= __iterator_unchanged(extra_state
, stmt
->iterator_post_statement
);
246 __split_statements(stmt
->iterator_post_statement
);
247 __save_gotos(loop_name
);
248 __split_whole_condition(stmt
->iterator_pre_condition
);
250 __merge_false_states();
251 if (extra_state
&& unchanged
)
252 __extra_pre_loop_hook_after(extra_state
,
253 stmt
->iterator_post_statement
, stmt
->iterator_pre_condition
);
254 __pop_false_states();
258 __split_statements(stmt
->iterator_post_statement
);
259 __save_gotos(loop_name
);
260 __split_whole_condition(stmt
->iterator_pre_condition
);
262 __merge_false_states();
263 __merge_false_states();
269 * Post loops are do {} while();
271 static void handle_post_loop(struct statement
*stmt
)
275 loop_name
= get_loop_name(loop_num
);
280 __merge_gotos(loop_name
);
281 __split_statements(stmt
->iterator_statement
);
283 if (!is_zero(stmt
->iterator_post_condition
))
284 __save_gotos(loop_name
);
286 if (is_forever_loop(stmt
)) {
289 __split_whole_condition(stmt
->iterator_post_condition
);
290 __use_false_states();
295 static int empty_statement(struct statement
*stmt
)
299 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
304 static struct statement
*last_stmt
;
305 static int is_last_stmt(struct statement
*stmt
)
307 if (stmt
== last_stmt
)
312 static void print_unreached_initializers(struct symbol_list
*sym_list
)
316 FOR_EACH_PTR(sym_list
, sym
) {
318 sm_msg("info: '%s' is not actually initialized (unreached code).",
319 (sym
->ident
? sym
->ident
->name
: "this variable"));
320 } END_FOR_EACH_PTR(sym
);
323 static void print_unreached(struct statement
*stmt
)
326 static int print
= 1;
328 if (!__path_is_null()) {
335 switch (stmt
->type
) {
336 case STMT_COMPOUND
: /* after a switch before a case stmt */
341 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
342 print_unreached_initializers(stmt
->declaration
);
344 case STMT_RETURN
: /* gcc complains if you don't have a return statement */
345 if (is_last_stmt(stmt
))
355 if (!option_spammy
&& empty_statement(stmt
))
357 sm_msg("info: ignoring unreachable code.");
361 void __split_statements(struct statement
*stmt
)
366 if (out_of_memory() || __bail_on_rest_of_function
) {
367 static char *printed
= NULL
;
369 if (printed
!= cur_func
)
370 sm_msg("Function too hairy. Giving up.");
375 add_ptr_list(&big_statement_stack
, stmt
);
376 free_expression_stack(&big_expression_stack
);
377 __smatch_lineno
= stmt
->pos
.line
;
378 print_unreached(stmt
);
379 __pass_to_client(stmt
, STMT_HOOK
);
381 switch (stmt
->type
) {
382 case STMT_DECLARATION
:
383 split_declaration(stmt
->declaration
);
386 __split_expr(stmt
->ret_value
);
387 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
390 case STMT_EXPRESSION
:
391 __split_expr(stmt
->expression
);
393 case STMT_COMPOUND
: {
397 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
398 __push_scope_hooks();
399 FOR_EACH_PTR(stmt
->stmts
, s
) {
400 __split_statements(s
);
401 } END_FOR_EACH_PTR(s
);
402 __call_scope_hooks();
406 if (known_condition_true(stmt
->if_conditional
)) {
407 __split_statements(stmt
->if_true
);
410 if (known_condition_false(stmt
->if_conditional
)) {
411 __split_statements(stmt
->if_false
);
414 if (option_known_conditions
&&
415 implied_condition_true(stmt
->if_conditional
)) {
416 sm_info("this condition is true.");
417 __split_statements(stmt
->if_true
);
420 if (option_known_conditions
&&
421 implied_condition_false(stmt
->if_conditional
)) {
422 sm_info("this condition is false.");
423 __split_statements(stmt
->if_false
);
426 __split_whole_condition(stmt
->if_conditional
);
427 __split_statements(stmt
->if_true
);
428 if (empty_statement(stmt
->if_true
))
429 sm_msg("warn: if();");
430 __push_true_states();
431 __use_false_states();
432 __split_statements(stmt
->if_false
);
433 __merge_true_states();
436 if (stmt
->iterator_pre_condition
)
437 handle_pre_loop(stmt
);
438 else if (stmt
->iterator_post_condition
)
439 handle_post_loop(stmt
);
441 // these are for(;;) type loops.
442 handle_pre_loop(stmt
);
446 __split_expr(stmt
->switch_expression
);
447 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
448 __save_switch_states(top_expression(switch_expr_stack
));
452 __split_statements(stmt
->switch_statement
);
453 if (!__pop_default())
454 __merge_switches(top_expression(switch_expr_stack
),
458 pop_expression(&switch_expr_stack
);
461 __merge_switches(top_expression(switch_expr_stack
),
462 stmt
->case_expression
);
463 __pass_case_to_client(top_expression(switch_expr_stack
),
464 stmt
->case_expression
);
465 if (!stmt
->case_expression
)
467 __split_expr(stmt
->case_expression
);
468 __split_expr(stmt
->case_to
);
469 __split_statements(stmt
->case_statement
);
473 stmt
->label
->type
== SYM_LABEL
&&
474 stmt
->label
->ident
) {
475 __merge_gotos(stmt
->label
->ident
->name
);
477 __split_statements(stmt
->label_statement
);
480 __split_expr(stmt
->goto_expression
);
481 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
482 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
484 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
486 __process_continues();
488 } else if (stmt
->goto_label
&&
489 stmt
->goto_label
->type
== SYM_LABEL
&&
490 stmt
->goto_label
->ident
) {
491 __save_gotos(stmt
->goto_label
->ident
->name
);
498 __split_expr(stmt
->asm_string
);
499 //__split_expr(stmt->asm_outputs);
500 //__split_expr(stmt->asm_inputs);
501 //__split_expr(stmt->asm_clobbers);
506 __split_expr(stmt
->range_expression
);
507 __split_expr(stmt
->range_low
);
508 __split_expr(stmt
->range_high
);
513 static void split_expr_list(struct expression_list
*expr_list
)
515 struct expression
*expr
;
516 FOR_EACH_PTR(expr_list
, expr
) {
518 } END_FOR_EACH_PTR(expr
);
522 static void split_sym(struct symbol
*sym
)
526 if (!(sym
->namespace & NS_SYMBOL
))
529 __split_statements(sym
->stmt
);
530 __split_expr(sym
->array_size
);
531 split_symlist(sym
->arguments
);
532 split_symlist(sym
->symbol_list
);
533 __split_statements(sym
->inline_stmt
);
534 split_symlist(sym
->inline_symbol_list
);
537 static void split_symlist(struct symbol_list
*sym_list
)
541 FOR_EACH_PTR(sym_list
, sym
) {
543 } END_FOR_EACH_PTR(sym
);
546 static struct expression
*fake_assign_expr(struct symbol
*sym
)
548 struct expression
*e_assign
, *e_symbol
;
550 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
551 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
552 e_assign
->op
= (int)'=';
553 e_symbol
->symbol
= sym
;
554 e_symbol
->symbol_name
= sym
->ident
;
555 e_assign
->left
= e_symbol
;
556 e_assign
->right
= sym
->initializer
;
560 static void do_initializer_stuff(struct symbol
*sym
)
562 struct expression
*assign
;
564 if(!sym
->initializer
)
566 assign
= fake_assign_expr(sym
);
567 __split_expr(assign
);
570 static void split_declaration(struct symbol_list
*sym_list
)
574 FOR_EACH_PTR(sym_list
, sym
) {
575 __pass_to_client(sym
, DECLARATION_HOOK
);
576 do_initializer_stuff(sym
);
578 } END_FOR_EACH_PTR(sym
);
581 static void split_functions(struct symbol_list
*sym_list
)
585 FOR_EACH_PTR(sym_list
, sym
) {
586 struct symbol
*base_type
;
587 base_type
= get_base_type(sym
);
588 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
591 line_func_start
= base_type
->stmt
->pos
.line
;
593 cur_func
= sym
->ident
->name
;
594 __smatch_lineno
= sym
->pos
.line
;
596 sm_debug("new function: %s\n", cur_func
);
597 if (option_two_passes
) {
601 __pass_to_client(sym
, FUNC_DEF_HOOK
);
602 __split_statements(base_type
->stmt
);
608 __pass_to_client(sym
, FUNC_DEF_HOOK
);
609 __split_statements(base_type
->stmt
);
610 __pass_to_client(sym
, END_FUNC_HOOK
);
614 free_data_info_allocs();
615 free_expression_stack(&switch_expr_stack
);
616 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
617 __bail_on_rest_of_function
= 0;
619 __pass_to_client(sym
, BASE_HOOK
);
621 } END_FOR_EACH_PTR(sym
);
622 __pass_to_client_no_data(END_FILE_HOOK
);
625 void smatch (int argc
, char **argv
)
628 struct string_list
*filelist
= NULL
;
629 struct symbol_list
*sym_list
;
633 printf("Usage: smatch [--debug] <filename.c>\n");
636 sparse_initialize(argc
, argv
, &filelist
);
637 FOR_EACH_PTR_NOTAG(filelist
, file
) {
638 sym_list
= __sparse(file
);
639 split_functions(sym_list
);
640 } END_FOR_EACH_PTR_NOTAG(file
);