check_overflow: white space changes
[smatch.git] / smatch_flow.c
blob6266230bb7191366cab101cabf1b511072483d68
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 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)
48 if (option_full_path)
49 return full_filename;
50 return filename;
53 static void set_position(struct expression *expr)
55 int len;
56 static int prev_stream = -1;
58 __smatch_lineno = expr->pos.line;
60 if (expr->pos.stream == prev_stream)
61 return;
63 filename = stream_name(expr->pos.stream);
65 free(full_filename);
66 pathname = getcwd(NULL, 0);
67 if (pathname) {
68 len = strlen(pathname) + 1 + strlen(filename) + 1;
69 full_filename = malloc(len);
70 snprintf(full_filename, len, "%s/%s", pathname, filename);
71 } else {
72 full_filename = alloc_string(filename);
74 free(pathname);
77 void __split_expr(struct expression *expr)
79 if (!expr)
80 return;
82 // printf("%d Debug expr_type %d %s\n", get_lineno(), expr->type, show_special(expr->op));
84 push_expression(&big_expression_stack, expr);
85 set_position(expr);
86 __pass_to_client(expr, EXPR_HOOK);
88 switch (expr->type) {
89 case EXPR_PREOP:
90 if (expr->op == '*')
91 __pass_to_client(expr, DEREF_HOOK);
92 case EXPR_POSTOP:
93 __pass_to_client(expr, OP_HOOK);
94 __split_expr(expr->unop);
95 return;
96 case EXPR_STATEMENT:
97 __split_statements(expr->statement);
98 return;
99 case EXPR_LOGICAL:
100 __split_whole_condition(expr);
101 __push_true_states();
102 __use_false_states();
103 __merge_true_states();
104 return;
105 case EXPR_BINOP:
106 __pass_to_client(expr, BINOP_HOOK);
107 case EXPR_COMMA:
108 case EXPR_COMPARE:
109 __split_expr(expr->left);
110 __split_expr(expr->right);
111 return;
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);
121 return;
123 case EXPR_DEREF:
124 __pass_to_client(expr, DEREF_HOOK);
125 __split_expr(expr->deref);
126 return;
127 case EXPR_SLICE:
128 __split_expr(expr->base);
129 return;
130 case EXPR_CAST:
131 case EXPR_FORCE_CAST:
132 __split_expr(expr->cast_expression);
133 return;
134 case EXPR_SIZEOF:
135 /* there isn't anything to pass a client from inside a sizeof() */
136 return;
137 case EXPR_CONDITIONAL:
138 case EXPR_SELECT:
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();
145 return;
146 case EXPR_CALL:
147 split_expr_list(expr->args);
148 __split_expr(expr->fn);
149 __pass_to_client(expr, FUNCTION_CALL_HOOK);
150 return;
151 case EXPR_INITIALIZER:
152 split_expr_list(expr->expr_list);
153 return;
154 case EXPR_IDENTIFIER:
155 __split_expr(expr->ident_expression);
156 return;
157 case EXPR_INDEX:
158 __split_expr(expr->idx_expression);
159 return;
160 case EXPR_POS:
161 __split_expr(expr->init_expr);
162 return;
163 case EXPR_SYMBOL:
164 __pass_to_client(expr, SYM_HOOK);
165 return;
166 default:
167 return;
171 static int is_forever_loop(struct statement *stmt)
174 struct expression *expr;
176 expr = strip_expr(stmt->iterator_pre_condition);
177 if (!expr)
178 expr = stmt->iterator_post_condition;
179 if (!expr) {
180 /* this is a for(;;) loop... */
181 return 1;
184 if (expr->type == EXPR_VALUE && expr->value == 1) {
185 return 1;
188 return 0;
191 static int loop_num;
192 static char *get_loop_name(int num)
194 char buf[256];
196 snprintf(buf, 255, "-loop%d", num);
197 buf[255] = '\0';
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;
209 int unchanged = 0;
210 char *loop_name;
212 loop_name = get_loop_name(loop_num);
213 loop_num++;
215 __split_statements(stmt->iterator_pre_statement);
217 once_through = implied_condition_true(stmt->iterator_pre_condition);
219 __push_continues();
220 __push_breaks();
222 __merge_gotos(loop_name);
224 __in_pre_condition++;
225 __split_whole_condition(stmt->iterator_pre_condition);
226 __in_pre_condition--;
228 if (once_through)
229 extra_state = __extra_pre_loop_hook_before(stmt->iterator_pre_statement);
230 if (option_assume_loops)
231 once_through = 1;
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 */
238 __pop_continues();
239 __pop_false_states();
240 __use_breaks();
241 } else if (once_through) {
242 __merge_continues();
243 if (extra_state)
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);
248 nullify_path();
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();
254 __merge_breaks();
255 } else {
256 __merge_continues();
257 __split_statements(stmt->iterator_post_statement);
258 __save_gotos(loop_name);
259 __split_whole_condition(stmt->iterator_pre_condition);
260 nullify_path();
261 __merge_false_states();
262 __merge_false_states();
263 __merge_breaks();
268 * Post loops are do {} while();
270 static void handle_post_loop(struct statement *stmt)
272 char *loop_name;
274 loop_name = get_loop_name(loop_num);
275 loop_num++;
277 __push_continues();
278 __push_breaks();
279 __merge_gotos(loop_name);
280 __split_statements(stmt->iterator_statement);
281 __merge_continues();
282 if (!is_zero(stmt->iterator_post_condition))
283 __save_gotos(loop_name);
285 if (is_forever_loop(stmt)) {
286 __use_breaks();
287 } else {
288 __split_whole_condition(stmt->iterator_post_condition);
289 __use_false_states();
290 __merge_breaks();
294 static struct statement *last_stmt;
295 static int is_last_stmt(struct statement *stmt)
297 if (stmt == last_stmt)
298 return 1;
299 return 0;
302 static void print_unreached_initializers(struct symbol_list *sym_list)
304 struct symbol *sym;
306 FOR_EACH_PTR(sym_list, sym) {
307 if(sym->initializer)
308 sm_msg("info: '%s' is not actually initialized (unreached code).",
309 (sym->ident ? sym->ident->name : "this variable"));
310 } END_FOR_EACH_PTR(sym);
313 static void print_unreached(struct statement *stmt)
316 static int print = 1;
318 if (__path_is_null()) {
319 switch (stmt->type) {
320 case STMT_COMPOUND: /* after a switch before a case stmt */
321 case STMT_CASE:
322 case STMT_LABEL:
323 break;
324 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
325 print_unreached_initializers(stmt->declaration);
326 break;
327 case STMT_RETURN: /* gcc complains if you don't have a return statement */
328 if (is_last_stmt(stmt))
329 break;
330 default:
331 if (print)
332 sm_msg("info: ignoring unreachable code.");
333 print = 0;
335 } else {
336 print = 1;
341 static int empty_statement(struct statement *stmt)
343 if (!stmt)
344 return 0;
345 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
346 return 1;
347 return 0;
350 void __split_statements(struct statement *stmt)
352 if (!stmt)
353 return;
355 if (out_of_memory() || __bail_on_rest_of_function) {
356 static char *printed = NULL;
358 if (printed != cur_func)
359 sm_msg("Function too hairy. Giving up.");
360 printed = cur_func;
361 return;
364 add_ptr_list(&big_statement_stack, stmt);
365 free_expression_stack(&big_expression_stack);
366 __smatch_lineno = stmt->pos.line;
367 print_unreached(stmt);
368 __pass_to_client(stmt, STMT_HOOK);
370 switch (stmt->type) {
371 case STMT_DECLARATION:
372 split_declaration(stmt->declaration);
373 return;
374 case STMT_RETURN:
375 __split_expr(stmt->ret_value);
376 __pass_to_client(stmt->ret_value, RETURN_HOOK);
377 nullify_path();
378 return;
379 case STMT_EXPRESSION:
380 __split_expr(stmt->expression);
381 return;
382 case STMT_COMPOUND: {
383 struct statement *s;
385 if (!last_stmt)
386 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
387 __push_scope_hooks();
388 FOR_EACH_PTR(stmt->stmts, s) {
389 __split_statements(s);
390 } END_FOR_EACH_PTR(s);
391 __call_scope_hooks();
392 return;
394 case STMT_IF:
395 if (known_condition_true(stmt->if_conditional)) {
396 __split_statements(stmt->if_true);
397 return;
399 if (known_condition_false(stmt->if_conditional)) {
400 __split_statements(stmt->if_false);
401 return;
403 if (option_known_conditions &&
404 implied_condition_true(stmt->if_conditional)) {
405 sm_info("this condition is true.");
406 __split_statements(stmt->if_true);
407 return;
409 if (option_known_conditions &&
410 implied_condition_false(stmt->if_conditional)) {
411 sm_info("this condition is false.");
412 __split_statements(stmt->if_false);
413 return;
415 __split_whole_condition(stmt->if_conditional);
416 __split_statements(stmt->if_true);
417 if (empty_statement(stmt->if_true))
418 sm_msg("warn: if();");
419 __push_true_states();
420 __use_false_states();
421 __split_statements(stmt->if_false);
422 __merge_true_states();
423 return;
424 case STMT_ITERATOR:
425 if (stmt->iterator_pre_condition)
426 handle_pre_loop(stmt);
427 else if (stmt->iterator_post_condition)
428 handle_post_loop(stmt);
429 else {
430 // these are for(;;) type loops.
431 handle_pre_loop(stmt);
433 return;
434 case STMT_SWITCH:
435 __split_expr(stmt->switch_expression);
436 push_expression(&switch_expr_stack, stmt->switch_expression);
437 __save_switch_states(top_expression(switch_expr_stack));
438 nullify_path();
439 __push_default();
440 __push_breaks();
441 __split_statements(stmt->switch_statement);
442 if (!__pop_default())
443 __merge_switches(top_expression(switch_expr_stack),
444 NULL);
445 __pop_switches();
446 __merge_breaks();
447 pop_expression(&switch_expr_stack);
448 return;
449 case STMT_CASE:
450 __merge_switches(top_expression(switch_expr_stack),
451 stmt->case_expression);
452 __pass_case_to_client(top_expression(switch_expr_stack),
453 stmt->case_expression);
454 if (!stmt->case_expression)
455 __set_default();
456 __split_expr(stmt->case_expression);
457 __split_expr(stmt->case_to);
458 __split_statements(stmt->case_statement);
459 return;
460 case STMT_LABEL:
461 if (stmt->label &&
462 stmt->label->type == SYM_LABEL &&
463 stmt->label->ident) {
464 __merge_gotos(stmt->label->ident->name);
466 __split_statements(stmt->label_statement);
467 return;
468 case STMT_GOTO:
469 __split_expr(stmt->goto_expression);
470 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
471 if (!strcmp(stmt->goto_label->ident->name, "break")) {
472 __process_breaks();
473 } else if (!strcmp(stmt->goto_label->ident->name,
474 "continue")) {
475 __process_continues();
477 } else if (stmt->goto_label &&
478 stmt->goto_label->type == SYM_LABEL &&
479 stmt->goto_label->ident) {
480 __save_gotos(stmt->goto_label->ident->name);
482 nullify_path();
483 return;
484 case STMT_NONE:
485 return;
486 case STMT_ASM:
487 __split_expr(stmt->asm_string);
488 //__split_expr(stmt->asm_outputs);
489 //__split_expr(stmt->asm_inputs);
490 //__split_expr(stmt->asm_clobbers);
491 return;
492 case STMT_CONTEXT:
493 return;
494 case STMT_RANGE:
495 __split_expr(stmt->range_expression);
496 __split_expr(stmt->range_low);
497 __split_expr(stmt->range_high);
498 return;
502 static void split_expr_list(struct expression_list *expr_list)
504 struct expression *expr;
505 FOR_EACH_PTR(expr_list, expr) {
506 __split_expr(expr);
507 } END_FOR_EACH_PTR(expr);
511 static void split_sym(struct symbol *sym)
513 if (!sym)
514 return;
515 if (!(sym->namespace & NS_SYMBOL))
516 return;
518 __split_statements(sym->stmt);
519 __split_expr(sym->array_size);
520 split_symlist(sym->arguments);
521 split_symlist(sym->symbol_list);
522 __split_statements(sym->inline_stmt);
523 split_symlist(sym->inline_symbol_list);
524 __split_expr(sym->initializer);
527 static void split_symlist(struct symbol_list *sym_list)
529 struct symbol *sym;
531 FOR_EACH_PTR(sym_list, sym) {
532 split_sym(sym);
533 } END_FOR_EACH_PTR(sym);
536 static struct expression *fake_assign_expr(struct symbol *sym)
538 struct expression *e_assign, *e_symbol;
540 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
541 e_symbol = alloc_expression(sym->initializer->pos, EXPR_SYMBOL);
542 e_assign->op = (int)'=';
543 e_symbol->symbol = sym;
544 e_symbol->symbol_name = sym->ident;
545 e_assign->left = e_symbol;
546 e_assign->right = sym->initializer;
547 return e_assign;
550 static void split_declaration(struct symbol_list *sym_list)
552 struct symbol *sym;
553 struct expression *assign, *tmp;
555 FOR_EACH_PTR(sym_list, sym) {
556 __pass_to_client(sym, DECLARATION_HOOK);
557 __split_expr(sym->initializer);
558 if(sym->initializer) {
559 assign = fake_assign_expr(sym);
560 __pass_to_client(assign, ASSIGNMENT_HOOK);
561 tmp = strip_expr(assign->right);
562 if (tmp->type == EXPR_CALL)
563 __pass_to_client(assign, CALL_ASSIGNMENT_HOOK);
565 split_sym(sym);
566 } END_FOR_EACH_PTR(sym);
569 static void split_functions(struct symbol_list *sym_list)
571 struct symbol *sym;
573 FOR_EACH_PTR(sym_list, sym) {
574 struct symbol *base_type;
575 base_type = get_base_type(sym);
576 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
577 cur_func_sym = sym;
578 if (base_type->stmt)
579 line_func_start = base_type->stmt->pos.line;
580 if (sym->ident)
581 cur_func = sym->ident->name;
582 __smatch_lineno = sym->pos.line;
583 last_stmt = NULL;
584 sm_debug("new function: %s\n", cur_func);
585 if (option_two_passes) {
586 __unnullify_path();
587 loop_num = 0;
588 final_pass = 0;
589 __pass_to_client(sym, FUNC_DEF_HOOK);
590 __split_statements(base_type->stmt);
591 nullify_path();
593 __unnullify_path();
594 loop_num = 0;
595 final_pass = 1;
596 __pass_to_client(sym, FUNC_DEF_HOOK);
597 __split_statements(base_type->stmt);
598 __pass_to_client(sym, END_FUNC_HOOK);
599 cur_func = NULL;
600 line_func_start = 0;
601 clear_all_states();
602 free_data_info_allocs();
603 free_expression_stack(&switch_expr_stack);
604 __free_ptr_list((struct ptr_list **)&big_statement_stack);
605 __bail_on_rest_of_function = 0;
606 } else {
607 __pass_to_client(sym, BASE_HOOK);
609 } END_FOR_EACH_PTR(sym);
610 __pass_to_client_no_data(END_FILE_HOOK);
613 void smatch (int argc, char **argv)
616 struct string_list *filelist = NULL;
617 struct symbol_list *sym_list;
618 char *file;
620 if (argc < 2) {
621 printf("Usage: smatch [--debug] <filename.c>\n");
622 exit(1);
624 sparse_initialize(argc, argv, &filelist);
625 FOR_EACH_PTR_NOTAG(filelist, file) {
626 sym_list = __sparse(file);
627 split_functions(sym_list);
628 } END_FOR_EACH_PTR_NOTAG(file);