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 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
;
31 char *get_function(void) { return cur_func
; }
32 int get_lineno(void) { return __smatch_lineno
; }
33 int get_func_pos(void) { return __smatch_lineno
- line_func_start
; }
35 static void split_symlist(struct symbol_list
*sym_list
);
36 static void split_declaration(struct symbol_list
*sym_list
);
37 static void split_expr_list(struct expression_list
*expr_list
);
39 int option_assume_loops
= 0;
40 int option_known_conditions
= 0;
41 int option_two_passes
= 0;
42 struct symbol
*cur_func_sym
= NULL
;
44 char *get_filename(void)
51 void __split_expr(struct expression
*expr
)
56 // printf("%d Debug expr_type %d %s\n", get_lineno(), expr->type, show_special(expr->op));
58 push_expression(&big_expression_stack
, expr
);
59 __smatch_lineno
= expr
->pos
.line
;
60 __pass_to_client(expr
, EXPR_HOOK
);
65 __pass_to_client(expr
, DEREF_HOOK
);
67 __pass_to_client(expr
, OP_HOOK
);
68 __split_expr(expr
->unop
);
71 __split_statements(expr
->statement
);
74 __split_whole_condition(expr
);
77 __merge_true_states();
80 __pass_to_client(expr
, BINOP_HOOK
);
83 __split_expr(expr
->left
);
84 __split_expr(expr
->right
);
86 case EXPR_ASSIGNMENT
: {
87 struct expression
*tmp
;
89 __split_expr(expr
->right
);
90 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
91 tmp
= strip_expr(expr
->right
);
92 if (tmp
->type
== EXPR_CALL
)
93 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
94 __split_expr(expr
->left
);
98 __pass_to_client(expr
, DEREF_HOOK
);
99 __split_expr(expr
->deref
);
102 __split_expr(expr
->base
);
105 case EXPR_FORCE_CAST
:
106 __split_expr(expr
->cast_expression
);
109 /* there isn't anything to pass a client from inside a sizeof() */
111 case EXPR_CONDITIONAL
:
113 __split_whole_condition(expr
->conditional
);
114 __split_expr(expr
->cond_true
);
115 __push_true_states();
116 __use_false_states();
117 __split_expr(expr
->cond_false
);
118 __merge_true_states();
121 split_expr_list(expr
->args
);
122 __split_expr(expr
->fn
);
123 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
125 case EXPR_INITIALIZER
:
126 split_expr_list(expr
->expr_list
);
128 case EXPR_IDENTIFIER
:
129 __split_expr(expr
->ident_expression
);
132 __split_expr(expr
->idx_expression
);
135 __split_expr(expr
->init_expr
);
138 __pass_to_client(expr
, SYM_HOOK
);
145 static int is_forever_loop(struct statement
*stmt
)
148 struct expression
*expr
;
150 expr
= strip_expr(stmt
->iterator_pre_condition
);
152 expr
= stmt
->iterator_post_condition
;
154 /* this is a for(;;) loop... */
158 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
166 static char *get_loop_name(int num
)
170 snprintf(buf
, 255, "-loop%d", num
);
172 return alloc_sname(buf
);;
176 * Pre Loops are while and for loops.
179 static void handle_pre_loop(struct statement
*stmt
)
181 int once_through
; /* we go through the loop at least once */
182 struct sm_state
*extra_state
= NULL
;
186 loop_name
= get_loop_name(loop_num
);
189 __split_statements(stmt
->iterator_pre_statement
);
191 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
196 __merge_gotos(loop_name
);
197 __split_whole_condition(stmt
->iterator_pre_condition
);
199 extra_state
= __extra_pre_loop_hook_before(stmt
->iterator_pre_statement
);
200 if (option_assume_loops
)
202 __split_statements(stmt
->iterator_statement
);
204 __warn_on_silly_pre_loops();
205 if (is_forever_loop(stmt
)) {
206 __save_gotos(loop_name
);
207 /* forever loops don't have an iterator_post_statement */
209 __pop_false_states();
211 } else if (once_through
) {
214 unchanged
= __iterator_unchanged(extra_state
, stmt
->iterator_post_statement
);
215 __split_statements(stmt
->iterator_post_statement
);
216 __save_gotos(loop_name
);
217 __split_whole_condition(stmt
->iterator_pre_condition
);
219 __merge_false_states();
220 if (extra_state
&& unchanged
)
221 __extra_pre_loop_hook_after(extra_state
,
222 stmt
->iterator_post_statement
, stmt
->iterator_pre_condition
);
223 __pop_false_states();
227 __split_statements(stmt
->iterator_post_statement
);
228 __save_gotos(loop_name
);
229 __split_whole_condition(stmt
->iterator_pre_condition
);
231 __merge_false_states();
232 __merge_false_states();
238 * Post loops are do {} while();
240 static void handle_post_loop(struct statement
*stmt
)
244 loop_name
= get_loop_name(loop_num
);
249 __merge_gotos(loop_name
);
250 __split_statements(stmt
->iterator_statement
);
252 if (!is_zero(stmt
->iterator_post_condition
))
253 __save_gotos(loop_name
);
255 if (is_forever_loop(stmt
)) {
258 __split_whole_condition(stmt
->iterator_post_condition
);
259 __use_false_states();
264 static void print_unreached(struct statement
*stmt
)
268 * GCC insists on a return statement even where it is never
269 * reached. Also BUG() sometimes is a forever loop and
270 * sometimes not so people put code after a BUG(). There
271 * are way to many false positives.
275 if (__path_is_null()) {
277 case STMT_COMPOUND
: /* after a switch before a case stmt */
280 case STMT_DECLARATION
: /* switch(x) { int a; case foo: ... */
283 sm_msg("unreachable code. %d", stmt
->type
);
288 static int empty_statement(struct statement
*stmt
)
292 if (stmt
->type
== STMT_EXPRESSION
&& !stmt
->expression
)
297 void __split_statements(struct statement
*stmt
)
302 if (out_of_memory()) {
303 static char *printed
= NULL
;
305 if (printed
!= cur_func
)
306 sm_msg("Function too big. Giving up.");
311 free_expression_stack(&big_expression_stack
);
312 __smatch_lineno
= stmt
->pos
.line
;
313 print_unreached(stmt
);
314 __pass_to_client(stmt
, STMT_HOOK
);
316 switch (stmt
->type
) {
317 case STMT_DECLARATION
:
318 split_declaration(stmt
->declaration
);
321 __split_expr(stmt
->ret_value
);
322 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
325 case STMT_EXPRESSION
:
326 __split_expr(stmt
->expression
);
328 case STMT_COMPOUND
: {
330 __push_scope_hooks();
331 FOR_EACH_PTR(stmt
->stmts
, s
) {
332 __split_statements(s
);
333 } END_FOR_EACH_PTR(s
);
334 __call_scope_hooks();
338 if (known_condition_true(stmt
->if_conditional
)) {
339 __split_statements(stmt
->if_true
);
342 if (known_condition_false(stmt
->if_conditional
)) {
343 __split_statements(stmt
->if_false
);
346 if (option_known_conditions
&&
347 implied_condition_true(stmt
->if_conditional
)) {
348 sm_info("this condition is true.");
349 __split_statements(stmt
->if_true
);
352 if (option_known_conditions
&&
353 implied_condition_false(stmt
->if_conditional
)) {
354 sm_info("this condition is false.");
355 __split_statements(stmt
->if_false
);
358 __split_whole_condition(stmt
->if_conditional
);
359 __split_statements(stmt
->if_true
);
360 if (empty_statement(stmt
->if_true
))
361 sm_msg("warn: if();");
362 __push_true_states();
363 __use_false_states();
364 __split_statements(stmt
->if_false
);
365 __merge_true_states();
368 if (stmt
->iterator_pre_condition
)
369 handle_pre_loop(stmt
);
370 else if (stmt
->iterator_post_condition
)
371 handle_post_loop(stmt
);
373 // these are for(;;) type loops.
374 handle_pre_loop(stmt
);
378 __split_expr(stmt
->switch_expression
);
379 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
380 __save_switch_states(top_expression(switch_expr_stack
));
384 __split_statements(stmt
->switch_statement
);
385 if (!__pop_default())
386 __merge_switches(top_expression(switch_expr_stack
),
390 pop_expression(&switch_expr_stack
);
393 __merge_switches(top_expression(switch_expr_stack
),
394 stmt
->case_expression
);
395 __pass_case_to_client(top_expression(switch_expr_stack
),
396 stmt
->case_expression
);
397 if (!stmt
->case_expression
)
399 __split_expr(stmt
->case_expression
);
400 __split_expr(stmt
->case_to
);
401 __split_statements(stmt
->case_statement
);
405 stmt
->label
->type
== SYM_LABEL
&&
406 stmt
->label
->ident
) {
407 __merge_gotos(stmt
->label
->ident
->name
);
409 __split_statements(stmt
->label_statement
);
412 __split_expr(stmt
->goto_expression
);
413 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
414 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
416 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
418 __process_continues();
420 } else if (stmt
->goto_label
&&
421 stmt
->goto_label
->type
== SYM_LABEL
&&
422 stmt
->goto_label
->ident
) {
423 __save_gotos(stmt
->goto_label
->ident
->name
);
430 __split_expr(stmt
->asm_string
);
431 //__split_expr(stmt->asm_outputs);
432 //__split_expr(stmt->asm_inputs);
433 //__split_expr(stmt->asm_clobbers);
438 __split_expr(stmt
->range_expression
);
439 __split_expr(stmt
->range_low
);
440 __split_expr(stmt
->range_high
);
445 static void split_expr_list(struct expression_list
*expr_list
)
447 struct expression
*expr
;
448 FOR_EACH_PTR(expr_list
, expr
) {
450 } END_FOR_EACH_PTR(expr
);
454 static void split_sym(struct symbol
*sym
)
458 if (!(sym
->namespace & NS_SYMBOL
))
461 __split_statements(sym
->stmt
);
462 __split_expr(sym
->array_size
);
463 split_symlist(sym
->arguments
);
464 split_symlist(sym
->symbol_list
);
465 __split_statements(sym
->inline_stmt
);
466 split_symlist(sym
->inline_symbol_list
);
467 __split_expr(sym
->initializer
);
470 static void split_symlist(struct symbol_list
*sym_list
)
474 FOR_EACH_PTR(sym_list
, sym
) {
476 } END_FOR_EACH_PTR(sym
);
479 static struct expression
*fake_assign_expr(struct symbol
*sym
)
481 struct expression
*e_assign
, *e_symbol
;
483 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
484 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
485 e_assign
->op
= (int)'=';
486 e_symbol
->symbol
= sym
;
487 e_symbol
->symbol_name
= sym
->ident
;
488 e_assign
->left
= e_symbol
;
489 e_assign
->right
= sym
->initializer
;
493 static void split_declaration(struct symbol_list
*sym_list
)
496 struct expression
*assign
, *tmp
;
498 FOR_EACH_PTR(sym_list
, sym
) {
499 __pass_to_client(sym
, DECLARATION_HOOK
);
500 __split_expr(sym
->initializer
);
501 if(sym
->initializer
) {
502 assign
= fake_assign_expr(sym
);
503 __pass_to_client(assign
, ASSIGNMENT_HOOK
);
504 tmp
= strip_expr(assign
->right
);
505 if (tmp
->type
== EXPR_CALL
)
506 __pass_to_client(assign
, CALL_ASSIGNMENT_HOOK
);
509 } END_FOR_EACH_PTR(sym
);
512 static void split_functions(struct symbol_list
*sym_list
)
516 FOR_EACH_PTR(sym_list
, sym
) {
517 struct symbol
*base_type
;
518 base_type
= get_base_type(sym
);
519 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
522 line_func_start
= base_type
->stmt
->pos
.line
;
524 cur_func
= sym
->ident
->name
;
525 __smatch_lineno
= sym
->pos
.line
;
526 sm_debug("new function: %s\n", cur_func
);
527 if (option_two_passes
) {
531 __pass_to_client(sym
, FUNC_DEF_HOOK
);
532 __split_statements(base_type
->stmt
);
538 __pass_to_client(sym
, FUNC_DEF_HOOK
);
539 __split_statements(base_type
->stmt
);
540 __pass_to_client(sym
, END_FUNC_HOOK
);
544 free_data_info_allocs();
545 free_expression_stack(&switch_expr_stack
);
547 __pass_to_client(sym
, BASE_HOOK
);
549 } END_FOR_EACH_PTR(sym
);
550 __pass_to_client_no_data(END_FILE_HOOK
);
553 void smatch (int argc
, char **argv
)
556 struct string_list
*filelist
= NULL
;
557 struct symbol_list
*sym_list
;
560 printf("Usage: smatch [--debug] <filename.c>\n");
563 sparse_initialize(argc
, argv
, &filelist
);
564 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
567 pathname
= get_current_dir_name();
569 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
570 full_filename
= malloc(len
);
571 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
573 full_filename
= filename
;
576 sym_list
= __sparse(filename
);
577 split_functions(sym_list
);
583 } END_FOR_EACH_PTR_NOTAG(filename
);