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 __split_whole_condition(expr
);
101 __push_true_states();
102 __use_false_states();
103 __merge_true_states();
106 __pass_to_client(expr
, BINOP_HOOK
);
109 __split_expr(expr
->left
);
110 __split_expr(expr
->right
);
112 case EXPR_ASSIGNMENT
: {
113 struct expression
*tmp
;
115 __split_expr(expr
->right
);
116 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
117 tmp
= strip_expr(expr
->right
);
118 if (tmp
->type
== EXPR_CALL
)
119 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
120 __split_expr(expr
->left
);
124 __pass_to_client(expr
, DEREF_HOOK
);
125 __split_expr(expr
->deref
);
128 __split_expr(expr
->base
);
131 case EXPR_FORCE_CAST
:
132 __split_expr(expr
->cast_expression
);
135 /* there isn't anything to pass a client from inside a sizeof() */
137 case EXPR_CONDITIONAL
:
139 __split_whole_condition(expr
->conditional
);
140 __split_expr(expr
->cond_true
);
141 __push_true_states();
142 __use_false_states();
143 __split_expr(expr
->cond_false
);
144 __merge_true_states();
147 split_expr_list(expr
->args
);
148 __split_expr(expr
->fn
);
149 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
151 case EXPR_INITIALIZER
:
152 split_expr_list(expr
->expr_list
);
154 case EXPR_IDENTIFIER
:
155 __split_expr(expr
->ident_expression
);
158 __split_expr(expr
->idx_expression
);
161 __split_expr(expr
->init_expr
);
164 __pass_to_client(expr
, SYM_HOOK
);
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
--;
229 extra_state
= __extra_pre_loop_hook_before(stmt
->iterator_pre_statement
);
230 if (option_assume_loops
)
232 __split_statements(stmt
->iterator_statement
);
234 __warn_on_silly_pre_loops();
235 if (is_forever_loop(stmt
)) {
236 __save_gotos(loop_name
);
237 /* forever loops don't have an iterator_post_statement */
239 __pop_false_states();
241 } else if (once_through
) {
244 unchanged
= __iterator_unchanged(extra_state
, stmt
->iterator_post_statement
);
245 __split_statements(stmt
->iterator_post_statement
);
246 __save_gotos(loop_name
);
247 __split_whole_condition(stmt
->iterator_pre_condition
);
249 __merge_false_states();
250 if (extra_state
&& unchanged
)
251 __extra_pre_loop_hook_after(extra_state
,
252 stmt
->iterator_post_statement
, stmt
->iterator_pre_condition
);
253 __pop_false_states();
257 __split_statements(stmt
->iterator_post_statement
);
258 __save_gotos(loop_name
);
259 __split_whole_condition(stmt
->iterator_pre_condition
);
261 __merge_false_states();
262 __merge_false_states();
268 * Post loops are do {} while();
270 static void handle_post_loop(struct statement
*stmt
)
274 loop_name
= get_loop_name(loop_num
);
279 __merge_gotos(loop_name
);
280 __split_statements(stmt
->iterator_statement
);
282 if (!is_zero(stmt
->iterator_post_condition
))
283 __save_gotos(loop_name
);
285 if (is_forever_loop(stmt
)) {
288 __split_whole_condition(stmt
->iterator_post_condition
);
289 __use_false_states();
294 static void print_unreached_initializers(struct symbol_list
*sym_list
)
298 FOR_EACH_PTR(sym_list
, sym
) {
300 sm_msg("error: '%s' is not actually initialized (unreached code).",
301 (sym
->ident
? sym
->ident
->name
: "this variable"));
302 } END_FOR_EACH_PTR(sym
);
305 static void print_unreached(struct statement
*stmt
)
308 static int print
= 1;
310 if (!option_spammy
&& !option_debug
)
313 if (__path_is_null()) {
314 switch (stmt
->type
) {
315 case STMT_COMPOUND
: /* after a switch before a case stmt */
319 case STMT_DECLARATION
: /* switch (x) { int a; case foo: ... */
320 print_unreached_initializers(stmt
->declaration
);
324 sm_msg("info: ignoring unreachable code.");
333 static int empty_statement(struct statement
*stmt
)
337 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
342 void __split_statements(struct statement
*stmt
)
347 if (out_of_memory() || __bail_on_rest_of_function
) {
348 static char *printed
= NULL
;
350 if (printed
!= cur_func
)
351 sm_msg("Function too hairy. Giving up.");
356 add_ptr_list(&big_statement_stack
, stmt
);
357 free_expression_stack(&big_expression_stack
);
358 __smatch_lineno
= stmt
->pos
.line
;
359 print_unreached(stmt
);
360 __pass_to_client(stmt
, STMT_HOOK
);
362 switch (stmt
->type
) {
363 case STMT_DECLARATION
:
364 split_declaration(stmt
->declaration
);
367 __split_expr(stmt
->ret_value
);
368 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
371 case STMT_EXPRESSION
:
372 __split_expr(stmt
->expression
);
374 case STMT_COMPOUND
: {
376 __push_scope_hooks();
377 FOR_EACH_PTR(stmt
->stmts
, s
) {
378 __split_statements(s
);
379 } END_FOR_EACH_PTR(s
);
380 __call_scope_hooks();
384 if (known_condition_true(stmt
->if_conditional
)) {
385 __split_statements(stmt
->if_true
);
388 if (known_condition_false(stmt
->if_conditional
)) {
389 __split_statements(stmt
->if_false
);
392 if (option_known_conditions
&&
393 implied_condition_true(stmt
->if_conditional
)) {
394 sm_info("this condition is true.");
395 __split_statements(stmt
->if_true
);
398 if (option_known_conditions
&&
399 implied_condition_false(stmt
->if_conditional
)) {
400 sm_info("this condition is false.");
401 __split_statements(stmt
->if_false
);
404 __split_whole_condition(stmt
->if_conditional
);
405 __split_statements(stmt
->if_true
);
406 if (empty_statement(stmt
->if_true
))
407 sm_msg("warn: if();");
408 __push_true_states();
409 __use_false_states();
410 __split_statements(stmt
->if_false
);
411 __merge_true_states();
414 if (stmt
->iterator_pre_condition
)
415 handle_pre_loop(stmt
);
416 else if (stmt
->iterator_post_condition
)
417 handle_post_loop(stmt
);
419 // these are for(;;) type loops.
420 handle_pre_loop(stmt
);
424 __split_expr(stmt
->switch_expression
);
425 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
426 __save_switch_states(top_expression(switch_expr_stack
));
430 __split_statements(stmt
->switch_statement
);
431 if (!__pop_default())
432 __merge_switches(top_expression(switch_expr_stack
),
436 pop_expression(&switch_expr_stack
);
439 __merge_switches(top_expression(switch_expr_stack
),
440 stmt
->case_expression
);
441 __pass_case_to_client(top_expression(switch_expr_stack
),
442 stmt
->case_expression
);
443 if (!stmt
->case_expression
)
445 __split_expr(stmt
->case_expression
);
446 __split_expr(stmt
->case_to
);
447 __split_statements(stmt
->case_statement
);
451 stmt
->label
->type
== SYM_LABEL
&&
452 stmt
->label
->ident
) {
453 __merge_gotos(stmt
->label
->ident
->name
);
455 __split_statements(stmt
->label_statement
);
458 __split_expr(stmt
->goto_expression
);
459 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
460 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
462 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
464 __process_continues();
466 } else if (stmt
->goto_label
&&
467 stmt
->goto_label
->type
== SYM_LABEL
&&
468 stmt
->goto_label
->ident
) {
469 __save_gotos(stmt
->goto_label
->ident
->name
);
476 __split_expr(stmt
->asm_string
);
477 //__split_expr(stmt->asm_outputs);
478 //__split_expr(stmt->asm_inputs);
479 //__split_expr(stmt->asm_clobbers);
484 __split_expr(stmt
->range_expression
);
485 __split_expr(stmt
->range_low
);
486 __split_expr(stmt
->range_high
);
491 static void split_expr_list(struct expression_list
*expr_list
)
493 struct expression
*expr
;
494 FOR_EACH_PTR(expr_list
, expr
) {
496 } END_FOR_EACH_PTR(expr
);
500 static void split_sym(struct symbol
*sym
)
504 if (!(sym
->namespace & NS_SYMBOL
))
507 __split_statements(sym
->stmt
);
508 __split_expr(sym
->array_size
);
509 split_symlist(sym
->arguments
);
510 split_symlist(sym
->symbol_list
);
511 __split_statements(sym
->inline_stmt
);
512 split_symlist(sym
->inline_symbol_list
);
513 __split_expr(sym
->initializer
);
516 static void split_symlist(struct symbol_list
*sym_list
)
520 FOR_EACH_PTR(sym_list
, sym
) {
522 } END_FOR_EACH_PTR(sym
);
525 static struct expression
*fake_assign_expr(struct symbol
*sym
)
527 struct expression
*e_assign
, *e_symbol
;
529 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
530 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
531 e_assign
->op
= (int)'=';
532 e_symbol
->symbol
= sym
;
533 e_symbol
->symbol_name
= sym
->ident
;
534 e_assign
->left
= e_symbol
;
535 e_assign
->right
= sym
->initializer
;
539 static void split_declaration(struct symbol_list
*sym_list
)
542 struct expression
*assign
, *tmp
;
544 FOR_EACH_PTR(sym_list
, sym
) {
545 __pass_to_client(sym
, DECLARATION_HOOK
);
546 __split_expr(sym
->initializer
);
547 if(sym
->initializer
) {
548 assign
= fake_assign_expr(sym
);
549 __pass_to_client(assign
, ASSIGNMENT_HOOK
);
550 tmp
= strip_expr(assign
->right
);
551 if (tmp
->type
== EXPR_CALL
)
552 __pass_to_client(assign
, CALL_ASSIGNMENT_HOOK
);
555 } END_FOR_EACH_PTR(sym
);
558 static void split_functions(struct symbol_list
*sym_list
)
562 FOR_EACH_PTR(sym_list
, sym
) {
563 struct symbol
*base_type
;
564 base_type
= get_base_type(sym
);
565 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
568 line_func_start
= base_type
->stmt
->pos
.line
;
570 cur_func
= sym
->ident
->name
;
571 __smatch_lineno
= sym
->pos
.line
;
572 sm_debug("new function: %s\n", cur_func
);
573 if (option_two_passes
) {
577 __pass_to_client(sym
, FUNC_DEF_HOOK
);
578 __split_statements(base_type
->stmt
);
584 __pass_to_client(sym
, FUNC_DEF_HOOK
);
585 __split_statements(base_type
->stmt
);
586 __pass_to_client(sym
, END_FUNC_HOOK
);
590 free_data_info_allocs();
591 free_expression_stack(&switch_expr_stack
);
592 __free_ptr_list((struct ptr_list
**)&big_statement_stack
);
593 __bail_on_rest_of_function
= 0;
595 __pass_to_client(sym
, BASE_HOOK
);
597 } END_FOR_EACH_PTR(sym
);
598 __pass_to_client_no_data(END_FILE_HOOK
);
601 void smatch (int argc
, char **argv
)
604 struct string_list
*filelist
= NULL
;
605 struct symbol_list
*sym_list
;
609 printf("Usage: smatch [--debug] <filename.c>\n");
612 sparse_initialize(argc
, argv
, &filelist
);
613 FOR_EACH_PTR_NOTAG(filelist
, file
) {
614 sym_list
= __sparse(file
);
615 split_functions(sym_list
);
616 } END_FOR_EACH_PTR_NOTAG(file
);