sm_info: fix formatting
[smatch.git] / smatch_flow.c
blob32aa6b5485cc61bf1c452f0da953dc9130f8994e
1 /*
2 * sparse/smatch_flow.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #define _GNU_SOURCE 1
11 #include <unistd.h>
12 #include <stdio.h>
13 #include "token.h"
14 #include "smatch.h"
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h" // just for sname.
19 int final_pass;
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)
46 if (option_full_path)
47 return full_filename;
48 return filename;
51 void __split_expr(struct expression *expr)
53 if (!expr)
54 return;
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);
62 switch (expr->type) {
63 case EXPR_PREOP:
64 if (expr->op == '*')
65 __pass_to_client(expr, DEREF_HOOK);
66 case EXPR_POSTOP:
67 __pass_to_client(expr, OP_HOOK);
68 __split_expr(expr->unop);
69 return;
70 case EXPR_STATEMENT:
71 __split_statements(expr->statement);
72 return;
73 case EXPR_LOGICAL:
74 __split_whole_condition(expr);
75 __push_true_states();
76 __use_false_states();
77 __merge_true_states();
78 __pop_false_only_stack();
79 return;
80 case EXPR_BINOP:
81 __pass_to_client(expr, BINOP_HOOK);
82 case EXPR_COMMA:
83 case EXPR_COMPARE:
84 __split_expr(expr->left);
85 __split_expr(expr->right);
86 return;
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);
96 return;
98 case EXPR_DEREF:
99 __pass_to_client(expr, DEREF_HOOK);
100 __split_expr(expr->deref);
101 return;
102 case EXPR_SLICE:
103 __split_expr(expr->base);
104 return;
105 case EXPR_CAST:
106 case EXPR_FORCE_CAST:
107 __split_expr(expr->cast_expression);
108 return;
109 case EXPR_SIZEOF:
110 /* there isn't anything to pass a client from inside a sizeof() */
111 return;
112 case EXPR_CONDITIONAL:
113 case EXPR_SELECT:
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();
121 return;
122 case EXPR_CALL:
123 split_expr_list(expr->args);
124 __split_expr(expr->fn);
125 __pass_to_client(expr, FUNCTION_CALL_HOOK);
126 return;
127 case EXPR_INITIALIZER:
128 split_expr_list(expr->expr_list);
129 return;
130 case EXPR_IDENTIFIER:
131 __split_expr(expr->ident_expression);
132 return;
133 case EXPR_INDEX:
134 __split_expr(expr->idx_expression);
135 return;
136 case EXPR_POS:
137 __split_expr(expr->init_expr);
138 return;
139 case EXPR_SYMBOL:
140 __pass_to_client(expr, SYM_HOOK);
141 return;
142 default:
143 return;
147 static int is_forever_loop(struct statement *stmt)
150 struct expression *expr;
152 expr = strip_expr(stmt->iterator_pre_condition);
153 if (!expr)
154 expr = stmt->iterator_post_condition;
155 if (!expr) {
156 /* this is a for(;;) loop... */
157 return 1;
160 if (expr->type == EXPR_VALUE && expr->value == 1) {
161 return 1;
164 return 0;
167 static int loop_num;
168 static char *get_loop_name(int num)
170 char buf[256];
172 snprintf(buf, 255, "-loop%d", num);
173 buf[255] = '\0';
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;
185 int unchanged = 0;
186 char *loop_name;
188 loop_name = get_loop_name(loop_num);
189 loop_num++;
191 __split_statements(stmt->iterator_pre_statement);
193 once_through = implied_condition_true(stmt->iterator_pre_condition);
195 __push_continues();
196 __push_breaks();
198 __merge_gotos(loop_name);
199 __split_whole_condition(stmt->iterator_pre_condition);
200 if (once_through)
201 extra_state = __extra_pre_loop_hook_before(stmt->iterator_pre_statement);
202 if (option_assume_loops)
203 once_through = 1;
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 */
211 __pop_continues();
212 __pop_false_states();
213 __use_breaks();
214 } else if (once_through) {
215 __merge_continues();
216 if (extra_state)
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);
221 nullify_path();
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();
229 __merge_breaks();
230 } else {
231 __merge_continues();
232 __split_statements(stmt->iterator_post_statement);
233 __save_gotos(loop_name);
234 __merge_false_states();
235 __use_false_only_stack();
236 __merge_breaks();
241 * Post loops are do {} while();
243 static void handle_post_loop(struct statement *stmt)
245 char *loop_name;
247 loop_name = get_loop_name(loop_num);
248 loop_num++;
250 __push_continues();
251 __push_breaks();
252 __merge_gotos(loop_name);
253 __split_statements(stmt->iterator_statement);
254 __merge_continues();
255 if (!is_zero(stmt->iterator_post_condition))
256 __save_gotos(loop_name);
258 if (is_forever_loop(stmt)) {
259 __use_breaks();
260 } else {
261 __split_whole_condition(stmt->iterator_post_condition);
262 __use_false_states();
263 __merge_breaks();
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.
277 return;
279 if (__path_is_null()) {
280 switch(stmt->type) {
281 case STMT_COMPOUND: /* after a switch before a case stmt */
282 case STMT_CASE:
283 case STMT_LABEL:
284 case STMT_DECLARATION: /* switch(x) { int a; case foo: ... */
285 break;
286 default:
287 sm_msg("unreachable code. %d", stmt->type);
292 static int empty_statement(struct statement *stmt)
294 if (!stmt)
295 return 0;
296 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
297 return 1;
298 return 0;
301 void __split_statements(struct statement *stmt)
303 if (!stmt)
304 return;
306 if (out_of_memory()) {
307 static char *printed = NULL;
309 if (printed != cur_func)
310 sm_msg("Function too big. Giving up.");
311 printed = cur_func;
312 return;
315 free_expression_stack(&big_expression_stack);
316 __smatch_lineno = stmt->pos.line;
317 print_unreached(stmt);
318 __pass_to_client(stmt, STMT_HOOK);
320 switch (stmt->type) {
321 case STMT_DECLARATION:
322 split_declaration(stmt->declaration);
323 return;
324 case STMT_RETURN:
325 __split_expr(stmt->ret_value);
326 __pass_to_client(stmt->ret_value, RETURN_HOOK);
327 nullify_path();
328 return;
329 case STMT_EXPRESSION:
330 __split_expr(stmt->expression);
331 return;
332 case STMT_COMPOUND: {
333 struct statement *s;
334 __push_scope_hooks();
335 FOR_EACH_PTR(stmt->stmts, s) {
336 __split_statements(s);
337 } END_FOR_EACH_PTR(s);
338 __call_scope_hooks();
339 return;
341 case STMT_IF:
342 if (known_condition_true(stmt->if_conditional)) {
343 __split_statements(stmt->if_true);
344 return;
346 if (known_condition_false(stmt->if_conditional)) {
347 __split_statements(stmt->if_false);
348 return;
350 if (option_known_conditions &&
351 implied_condition_true(stmt->if_conditional)) {
352 sm_info("this condition is true.");
353 __split_statements(stmt->if_true);
354 return;
356 if (option_known_conditions &&
357 implied_condition_false(stmt->if_conditional)) {
358 sm_info("this condition is false.");
359 __split_statements(stmt->if_false);
360 return;
362 __split_whole_condition(stmt->if_conditional);
363 __split_statements(stmt->if_true);
364 if (empty_statement(stmt->if_true))
365 sm_msg("warning: if();");
366 __push_true_states();
367 __use_false_states();
368 __split_statements(stmt->if_false);
369 __merge_true_states();
370 __pop_false_only_stack();
371 return;
372 case STMT_ITERATOR:
373 if (stmt->iterator_pre_condition)
374 handle_pre_loop(stmt);
375 else if (stmt->iterator_post_condition)
376 handle_post_loop(stmt);
377 else {
378 // these are for(;;) type loops.
379 handle_pre_loop(stmt);
381 return;
382 case STMT_SWITCH:
383 __split_expr(stmt->switch_expression);
384 push_expression(&switch_expr_stack, stmt->switch_expression);
385 __save_switch_states(top_expression(switch_expr_stack));
386 nullify_path();
387 __push_default();
388 __push_breaks();
389 __split_statements(stmt->switch_statement);
390 if (!__pop_default())
391 __merge_switches(top_expression(switch_expr_stack),
392 NULL);
393 __pop_switches();
394 __merge_breaks();
395 pop_expression(&switch_expr_stack);
396 return;
397 case STMT_CASE:
398 __merge_switches(top_expression(switch_expr_stack),
399 stmt->case_expression);
400 __pass_case_to_client(top_expression(switch_expr_stack),
401 stmt->case_expression);
402 if (!stmt->case_expression)
403 __set_default();
404 __split_expr(stmt->case_expression);
405 __split_expr(stmt->case_to);
406 __split_statements(stmt->case_statement);
407 return;
408 case STMT_LABEL:
409 if (stmt->label &&
410 stmt->label->type == SYM_LABEL &&
411 stmt->label->ident) {
412 __merge_gotos(stmt->label->ident->name);
414 __split_statements(stmt->label_statement);
415 return;
416 case STMT_GOTO:
417 __split_expr(stmt->goto_expression);
418 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
419 if (!strcmp(stmt->goto_label->ident->name, "break")) {
420 __process_breaks();
421 } else if (!strcmp(stmt->goto_label->ident->name,
422 "continue")) {
423 __process_continues();
425 } else if (stmt->goto_label &&
426 stmt->goto_label->type == SYM_LABEL &&
427 stmt->goto_label->ident) {
428 __save_gotos(stmt->goto_label->ident->name);
430 nullify_path();
431 return;
432 case STMT_NONE:
433 return;
434 case STMT_ASM:
435 __split_expr(stmt->asm_string);
436 //__split_expr(stmt->asm_outputs);
437 //__split_expr(stmt->asm_inputs);
438 //__split_expr(stmt->asm_clobbers);
439 return;
440 case STMT_CONTEXT:
441 return;
442 case STMT_RANGE:
443 __split_expr(stmt->range_expression);
444 __split_expr(stmt->range_low);
445 __split_expr(stmt->range_high);
446 return;
450 static void split_expr_list(struct expression_list *expr_list)
452 struct expression *expr;
453 FOR_EACH_PTR(expr_list, expr) {
454 __split_expr(expr);
455 } END_FOR_EACH_PTR(expr);
459 static void split_sym(struct symbol *sym)
461 if (!sym)
462 return;
463 if (!(sym->namespace & NS_SYMBOL))
464 return;
466 __split_statements(sym->stmt);
467 __split_expr(sym->array_size);
468 split_symlist(sym->arguments);
469 split_symlist(sym->symbol_list);
470 __split_statements(sym->inline_stmt);
471 split_symlist(sym->inline_symbol_list);
472 __split_expr(sym->initializer);
475 static void split_symlist(struct symbol_list *sym_list)
477 struct symbol *sym;
479 FOR_EACH_PTR(sym_list, sym) {
480 split_sym(sym);
481 } END_FOR_EACH_PTR(sym);
484 static struct expression *fake_assign_expr(struct symbol *sym)
486 struct expression *e_assign, *e_symbol;
488 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
489 e_symbol = alloc_expression(sym->initializer->pos, EXPR_SYMBOL);
490 e_assign->op = (int)'=';
491 e_symbol->symbol = sym;
492 e_symbol->symbol_name = sym->ident;
493 e_assign->left = e_symbol;
494 e_assign->right = sym->initializer;
495 return e_assign;
498 static void split_declaration(struct symbol_list *sym_list)
500 struct symbol *sym;
501 struct expression *assign, *tmp;
503 FOR_EACH_PTR(sym_list, sym) {
504 __pass_to_client(sym, DECLARATION_HOOK);
505 __split_expr(sym->initializer);
506 if(sym->initializer) {
507 assign = fake_assign_expr(sym);
508 __pass_to_client(assign, ASSIGNMENT_HOOK);
509 tmp = strip_expr(assign->right);
510 if (tmp->type == EXPR_CALL)
511 __pass_to_client(assign, CALL_ASSIGNMENT_HOOK);
513 split_sym(sym);
514 } END_FOR_EACH_PTR(sym);
517 static void split_functions(struct symbol_list *sym_list)
519 struct symbol *sym;
521 FOR_EACH_PTR(sym_list, sym) {
522 struct symbol *base_type;
523 base_type = get_base_type(sym);
524 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
525 cur_func_sym = sym;
526 if (base_type->stmt)
527 line_func_start = base_type->stmt->pos.line;
528 if (sym->ident)
529 cur_func = sym->ident->name;
530 __smatch_lineno = sym->pos.line;
531 sm_debug("new function: %s\n", cur_func);
532 if (option_two_passes) {
533 __unnullify_path();
534 loop_num = 0;
535 final_pass = 0;
536 __pass_to_client(sym, FUNC_DEF_HOOK);
537 __split_statements(base_type->stmt);
538 nullify_path();
540 __unnullify_path();
541 loop_num = 0;
542 final_pass = 1;
543 __pass_to_client(sym, FUNC_DEF_HOOK);
544 __split_statements(base_type->stmt);
545 __pass_to_client(sym, END_FUNC_HOOK);
546 cur_func = NULL;
547 line_func_start = 0;
548 clear_all_states();
549 free_data_info_allocs();
550 free_expression_stack(&switch_expr_stack);
551 } else {
552 __pass_to_client(sym, BASE_HOOK);
554 } END_FOR_EACH_PTR(sym);
555 __pass_to_client_no_data(END_FILE_HOOK);
558 void smatch (int argc, char **argv)
561 struct string_list *filelist = NULL;
562 struct symbol_list *sym_list;
564 if (argc < 2) {
565 printf("Usage: smatch [--debug] <filename.c>\n");
566 exit(1);
568 sparse_initialize(argc, argv, &filelist);
569 FOR_EACH_PTR_NOTAG(filelist, filename) {
570 int len;
572 pathname = get_current_dir_name();
573 if (pathname) {
574 len = strlen(pathname) + 1 + strlen(filename) + 1;
575 full_filename = malloc(len);
576 snprintf(full_filename, len, "%s/%s", pathname, filename);
577 } else {
578 full_filename = filename;
581 sym_list = __sparse(filename);
582 split_functions(sym_list);
584 if (pathname) {
585 free(full_filename);
586 free(pathname);
588 } END_FOR_EACH_PTR_NOTAG(filename);