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();
78 __pop_false_only_stack();
81 __pass_to_client(expr
, BINOP_HOOK
);
84 __split_expr(expr
->left
);
85 __split_expr(expr
->right
);
87 case EXPR_ASSIGNMENT
: {
88 struct expression
*tmp
;
90 __split_expr(expr
->right
);
91 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
92 tmp
= strip_expr(expr
->right
);
93 if (tmp
->type
== EXPR_CALL
)
94 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
95 __split_expr(expr
->left
);
99 __pass_to_client(expr
, DEREF_HOOK
);
100 __split_expr(expr
->deref
);
103 __split_expr(expr
->base
);
106 case EXPR_FORCE_CAST
:
107 __split_expr(expr
->cast_expression
);
110 /* there isn't anything to pass a client from inside a sizeof() */
112 case EXPR_CONDITIONAL
:
114 __split_whole_condition(expr
->conditional
);
115 __split_expr(expr
->cond_true
);
116 __push_true_states();
117 __use_false_states();
118 __split_expr(expr
->cond_false
);
119 __merge_true_states();
120 __pop_false_only_stack();
123 split_expr_list(expr
->args
);
124 __split_expr(expr
->fn
);
125 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
127 case EXPR_INITIALIZER
:
128 split_expr_list(expr
->expr_list
);
130 case EXPR_IDENTIFIER
:
131 __split_expr(expr
->ident_expression
);
134 __split_expr(expr
->idx_expression
);
137 __split_expr(expr
->init_expr
);
140 __pass_to_client(expr
, SYM_HOOK
);
147 static int is_forever_loop(struct statement
*stmt
)
150 struct expression
*expr
;
152 expr
= strip_expr(stmt
->iterator_pre_condition
);
154 expr
= stmt
->iterator_post_condition
;
156 /* this is a for(;;) loop... */
160 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
168 static char *get_loop_name(int num
)
172 snprintf(buf
, 255, "-loop%d", num
);
174 return alloc_sname(buf
);;
178 * Pre Loops are while and for loops.
181 static void handle_pre_loop(struct statement
*stmt
)
183 int once_through
; /* we go through the loop at least once */
184 struct sm_state
*extra_state
= NULL
;
188 loop_name
= get_loop_name(loop_num
);
191 __split_statements(stmt
->iterator_pre_statement
);
193 once_through
= implied_condition_true(stmt
->iterator_pre_condition
);
198 __merge_gotos(loop_name
);
199 __split_whole_condition(stmt
->iterator_pre_condition
);
201 extra_state
= __extra_pre_loop_hook_before(stmt
->iterator_pre_statement
);
202 if (option_assume_loops
)
204 __split_statements(stmt
->iterator_statement
);
206 __warn_on_silly_pre_loops();
207 if (is_forever_loop(stmt
)) {
208 __save_gotos(loop_name
);
209 __pop_false_only_stack();
210 /* forever loops don't have an iterator_post_statement */
212 __pop_false_states();
214 } else if (once_through
) {
217 unchanged
= __iterator_unchanged(extra_state
, stmt
->iterator_post_statement
);
218 __split_statements(stmt
->iterator_post_statement
);
219 __save_gotos(loop_name
);
220 __split_whole_condition(stmt
->iterator_pre_condition
);
222 __merge_false_states();
223 if (extra_state
&& unchanged
)
224 __extra_pre_loop_hook_after(extra_state
,
225 stmt
->iterator_post_statement
, stmt
->iterator_pre_condition
);
226 __pop_false_states();
227 __pop_false_only_stack();
228 __pop_false_only_stack();
232 __split_statements(stmt
->iterator_post_statement
);
233 __save_gotos(loop_name
);
234 __merge_false_states();
235 __use_false_only_stack();
241 * Post loops are do {} while();
243 static void handle_post_loop(struct statement
*stmt
)
247 loop_name
= get_loop_name(loop_num
);
252 __merge_gotos(loop_name
);
253 __split_statements(stmt
->iterator_statement
);
255 if (!is_zero(stmt
->iterator_post_condition
))
256 __save_gotos(loop_name
);
258 if (is_forever_loop(stmt
)) {
261 __split_whole_condition(stmt
->iterator_post_condition
);
262 __use_false_states();
265 __pop_false_only_stack();
268 static void print_unreached(struct statement
*stmt
)
272 * GCC insists on a return statement even where it is never
273 * reached. Also BUG() sometimes is a forever loop and
274 * sometimes not so people put code after a BUG(). There
275 * are way to many false positives.
279 if (__path_is_null()) {
281 case STMT_COMPOUND
: /* after a switch before a case stmt */
284 case STMT_DECLARATION
: /* switch(x) { int a; case foo: ... */
287 sm_msg("unreachable code. %d", stmt
->type
);
292 void __split_statements(struct statement
*stmt
)
297 if (out_of_memory()) {
298 static char *printed
= NULL
;
300 if (printed
!= cur_func
)
301 sm_msg("Function too big. Giving up.");
306 free_expression_stack(&big_expression_stack
);
307 __smatch_lineno
= stmt
->pos
.line
;
308 print_unreached(stmt
);
309 __pass_to_client(stmt
, STMT_HOOK
);
311 switch (stmt
->type
) {
312 case STMT_DECLARATION
:
313 split_declaration(stmt
->declaration
);
316 __split_expr(stmt
->ret_value
);
317 __pass_to_client(stmt
->ret_value
, RETURN_HOOK
);
320 case STMT_EXPRESSION
:
321 __split_expr(stmt
->expression
);
323 case STMT_COMPOUND
: {
325 __push_scope_hooks();
326 FOR_EACH_PTR(stmt
->stmts
, s
) {
327 __split_statements(s
);
328 } END_FOR_EACH_PTR(s
);
329 __call_scope_hooks();
333 if (known_condition_true(stmt
->if_conditional
)) {
334 __split_statements(stmt
->if_true
);
337 if (known_condition_false(stmt
->if_conditional
)) {
338 __split_statements(stmt
->if_false
);
341 if (option_known_conditions
&&
342 implied_condition_true(stmt
->if_conditional
)) {
343 sm_msg("info: this condition is true.");
344 __split_statements(stmt
->if_true
);
347 if (option_known_conditions
&&
348 implied_condition_false(stmt
->if_conditional
)) {
349 sm_msg("info: this condition is false.");
350 __split_statements(stmt
->if_false
);
353 __split_whole_condition(stmt
->if_conditional
);
354 __split_statements(stmt
->if_true
);
355 __push_true_states();
356 __use_false_states();
357 __split_statements(stmt
->if_false
);
358 __merge_true_states();
359 __pop_false_only_stack();
362 if (stmt
->iterator_pre_condition
)
363 handle_pre_loop(stmt
);
364 else if (stmt
->iterator_post_condition
)
365 handle_post_loop(stmt
);
367 // these are for(;;) type loops.
368 handle_pre_loop(stmt
);
372 __split_expr(stmt
->switch_expression
);
373 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
374 __save_switch_states(top_expression(switch_expr_stack
));
378 __split_statements(stmt
->switch_statement
);
379 if (!__pop_default())
380 __merge_switches(top_expression(switch_expr_stack
),
384 pop_expression(&switch_expr_stack
);
387 __merge_switches(top_expression(switch_expr_stack
),
388 stmt
->case_expression
);
389 __pass_case_to_client(top_expression(switch_expr_stack
),
390 stmt
->case_expression
);
391 if (!stmt
->case_expression
)
393 __split_expr(stmt
->case_expression
);
394 __split_expr(stmt
->case_to
);
395 __split_statements(stmt
->case_statement
);
399 stmt
->label
->type
== SYM_LABEL
&&
400 stmt
->label
->ident
) {
401 __merge_gotos(stmt
->label
->ident
->name
);
403 __split_statements(stmt
->label_statement
);
406 __split_expr(stmt
->goto_expression
);
407 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
408 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
410 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
412 __process_continues();
414 } else if (stmt
->goto_label
&&
415 stmt
->goto_label
->type
== SYM_LABEL
&&
416 stmt
->goto_label
->ident
) {
417 __save_gotos(stmt
->goto_label
->ident
->name
);
424 __split_expr(stmt
->asm_string
);
425 //__split_expr(stmt->asm_outputs);
426 //__split_expr(stmt->asm_inputs);
427 //__split_expr(stmt->asm_clobbers);
432 __split_expr(stmt
->range_expression
);
433 __split_expr(stmt
->range_low
);
434 __split_expr(stmt
->range_high
);
439 static void split_expr_list(struct expression_list
*expr_list
)
441 struct expression
*expr
;
442 FOR_EACH_PTR(expr_list
, expr
) {
444 } END_FOR_EACH_PTR(expr
);
448 static void split_sym(struct symbol
*sym
)
452 if (!(sym
->namespace & NS_SYMBOL
))
455 __split_statements(sym
->stmt
);
456 __split_expr(sym
->array_size
);
457 split_symlist(sym
->arguments
);
458 split_symlist(sym
->symbol_list
);
459 __split_statements(sym
->inline_stmt
);
460 split_symlist(sym
->inline_symbol_list
);
461 __split_expr(sym
->initializer
);
464 static void split_symlist(struct symbol_list
*sym_list
)
468 FOR_EACH_PTR(sym_list
, sym
) {
470 } END_FOR_EACH_PTR(sym
);
473 static struct expression
*fake_assign_expr(struct symbol
*sym
)
475 struct expression
*e_assign
, *e_symbol
;
477 e_assign
= alloc_expression(sym
->initializer
->pos
, EXPR_ASSIGNMENT
);
478 e_symbol
= alloc_expression(sym
->initializer
->pos
, EXPR_SYMBOL
);
479 e_assign
->op
= (int)'=';
480 e_symbol
->symbol
= sym
;
481 e_symbol
->symbol_name
= sym
->ident
;
482 e_assign
->left
= e_symbol
;
483 e_assign
->right
= sym
->initializer
;
487 static void split_declaration(struct symbol_list
*sym_list
)
490 struct expression
*assign
, *tmp
;
492 FOR_EACH_PTR(sym_list
, sym
) {
493 __pass_to_client(sym
, DECLARATION_HOOK
);
494 __split_expr(sym
->initializer
);
495 if(sym
->initializer
) {
496 assign
= fake_assign_expr(sym
);
497 __pass_to_client(assign
, ASSIGNMENT_HOOK
);
498 tmp
= strip_expr(assign
->right
);
499 if (tmp
->type
== EXPR_CALL
)
500 __pass_to_client(assign
, CALL_ASSIGNMENT_HOOK
);
503 } END_FOR_EACH_PTR(sym
);
506 static void split_functions(struct symbol_list
*sym_list
)
510 FOR_EACH_PTR(sym_list
, sym
) {
511 struct symbol
*base_type
;
512 base_type
= get_base_type(sym
);
513 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
516 line_func_start
= base_type
->stmt
->pos
.line
;
518 cur_func
= sym
->ident
->name
;
519 __smatch_lineno
= sym
->pos
.line
;
520 sm_debug("new function: %s\n", cur_func
);
521 if (option_two_passes
) {
525 __pass_to_client(sym
, FUNC_DEF_HOOK
);
526 __split_statements(base_type
->stmt
);
532 __pass_to_client(sym
, FUNC_DEF_HOOK
);
533 __split_statements(base_type
->stmt
);
534 __pass_to_client(sym
, END_FUNC_HOOK
);
538 free_data_info_allocs();
539 free_expression_stack(&switch_expr_stack
);
541 __pass_to_client(sym
, BASE_HOOK
);
543 } END_FOR_EACH_PTR(sym
);
544 __pass_to_client_no_data(END_FILE_HOOK
);
547 void smatch (int argc
, char **argv
)
550 struct string_list
*filelist
= NULL
;
551 struct symbol_list
*sym_list
;
554 printf("Usage: smatch [--debug] <filename.c>\n");
557 sparse_initialize(argc
, argv
, &filelist
);
558 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
561 pathname
= get_current_dir_name();
563 len
= strlen(pathname
) + 1 + strlen(filename
) + 1;
564 full_filename
= malloc(len
);
565 snprintf(full_filename
, len
, "%s/%s", pathname
, filename
);
567 full_filename
= filename
;
570 sym_list
= __sparse(filename
);
571 split_functions(sym_list
);
577 } END_FOR_EACH_PTR_NOTAG(filename
);