*new* check_macros: find macro precedence bugs
[smatch.git] / smatch_flow.c
blobb8aec83f2298738f4d8c0ba7f9a701bb1e47e0f3
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"
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 int loop_count;
29 static int expr_stmt_count;
30 static struct expression_list *switch_expr_stack = NULL;
31 static struct statement *next_stmt;
33 struct expression_list *big_expression_stack;
34 struct statement_list *big_statement_stack;
35 int __in_pre_condition = 0;
36 int __bail_on_rest_of_function = 0;
37 char *get_function(void) { return cur_func; }
38 int get_lineno(void) { return __smatch_lineno; }
39 int get_func_pos(void) { return __smatch_lineno - line_func_start; }
40 int inside_loop(void) { return !!loop_count; }
41 int in_expression_statement(void) { return !!expr_stmt_count; }
42 struct statement *get_next_stmt(void) { return next_stmt; }
44 static void split_symlist(struct symbol_list *sym_list);
45 static void split_declaration(struct symbol_list *sym_list);
46 static void split_expr_list(struct expression_list *expr_list);
48 int option_assume_loops = 0;
49 int option_known_conditions = 0;
50 int option_two_passes = 0;
51 struct symbol *cur_func_sym = NULL;
53 const char *get_filename(void)
55 if (option_full_path)
56 return full_filename;
57 return filename;
60 static void set_position(struct expression *expr)
62 int len;
63 static int prev_stream = -1;
65 __smatch_lineno = expr->pos.line;
67 if (expr->pos.stream == prev_stream)
68 return;
70 filename = stream_name(expr->pos.stream);
72 free(full_filename);
73 pathname = getcwd(NULL, 0);
74 if (pathname) {
75 len = strlen(pathname) + 1 + strlen(filename) + 1;
76 full_filename = malloc(len);
77 snprintf(full_filename, len, "%s/%s", pathname, filename);
78 } else {
79 full_filename = alloc_string(filename);
81 free(pathname);
84 void __split_expr(struct expression *expr)
86 if (!expr)
87 return;
89 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
91 push_expression(&big_expression_stack, expr);
92 set_position(expr);
93 __pass_to_client(expr, EXPR_HOOK);
95 switch (expr->type) {
96 case EXPR_PREOP:
97 if (expr->op == '*')
98 __pass_to_client(expr, DEREF_HOOK);
99 case EXPR_POSTOP:
100 __pass_to_client(expr, OP_HOOK);
101 __split_expr(expr->unop);
102 break;
103 case EXPR_STATEMENT:
104 expr_stmt_count++;
105 __split_stmt(expr->statement);
106 expr_stmt_count--;
107 break;
108 case EXPR_LOGICAL:
109 case EXPR_COMPARE:
110 __pass_to_client(expr, LOGIC_HOOK);
111 __handle_logic(expr);
112 break;
113 case EXPR_BINOP:
114 __pass_to_client(expr, BINOP_HOOK);
115 case EXPR_COMMA:
116 __split_expr(expr->left);
117 __split_expr(expr->right);
118 break;
119 case EXPR_ASSIGNMENT: {
120 struct expression *tmp;
122 /* foo = !bar() */
123 if (__handle_condition_assigns(expr))
124 break;
126 /* foo = (x < 5 ? foo : 5); */
127 if (__handle_select_assigns(expr))
128 break;
130 __split_expr(expr->right);
131 __pass_to_client(expr, ASSIGNMENT_HOOK);
132 tmp = strip_expr(expr->right);
133 if (tmp->type == EXPR_CALL)
134 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
135 __split_expr(expr->left);
136 break;
138 case EXPR_DEREF:
139 __pass_to_client(expr, DEREF_HOOK);
140 __split_expr(expr->deref);
141 break;
142 case EXPR_SLICE:
143 __split_expr(expr->base);
144 break;
145 case EXPR_CAST:
146 case EXPR_FORCE_CAST:
147 __split_expr(expr->cast_expression);
148 break;
149 case EXPR_SIZEOF:
150 /* there isn't anything to pass a client from inside a sizeof() */
151 break;
152 case EXPR_CONDITIONAL:
153 case EXPR_SELECT:
154 __split_whole_condition(expr->conditional);
155 __split_expr(expr->cond_true);
156 __push_true_states();
157 __use_false_states();
158 __split_expr(expr->cond_false);
159 __merge_true_states();
160 break;
161 case EXPR_CALL:
162 split_expr_list(expr->args);
163 __split_expr(expr->fn);
164 __pass_to_client(expr, FUNCTION_CALL_HOOK);
165 break;
166 case EXPR_INITIALIZER:
167 split_expr_list(expr->expr_list);
168 break;
169 case EXPR_IDENTIFIER:
170 __split_expr(expr->ident_expression);
171 break;
172 case EXPR_INDEX:
173 __split_expr(expr->idx_expression);
174 break;
175 case EXPR_POS:
176 __split_expr(expr->init_expr);
177 break;
178 case EXPR_SYMBOL:
179 __pass_to_client(expr, SYM_HOOK);
180 break;
181 case EXPR_STRING:
182 __pass_to_client(expr, STRING_HOOK);
183 break;
184 default:
185 break;
187 pop_expression(&big_expression_stack);
190 static int is_forever_loop(struct statement *stmt)
193 struct expression *expr;
195 expr = strip_expr(stmt->iterator_pre_condition);
196 if (!expr)
197 expr = stmt->iterator_post_condition;
198 if (!expr) {
199 /* this is a for(;;) loop... */
200 return 1;
203 if (expr->type == EXPR_VALUE && expr->value == 1) {
204 return 1;
207 return 0;
210 static int loop_num;
211 static char *get_loop_name(int num)
213 char buf[256];
215 snprintf(buf, 255, "-loop%d", num);
216 buf[255] = '\0';
217 return alloc_sname(buf);;
221 * Pre Loops are while and for loops.
224 static void handle_pre_loop(struct statement *stmt)
226 int once_through; /* we go through the loop at least once */
227 struct sm_state *extra_sm = NULL;
228 int unchanged = 0;
229 char *loop_name;
230 struct state_list *slist = NULL;
231 struct sm_state *sm = NULL;
233 loop_name = get_loop_name(loop_num);
234 loop_num++;
236 __split_stmt(stmt->iterator_pre_statement);
238 once_through = implied_condition_true(stmt->iterator_pre_condition);
240 loop_count++;
241 __push_continues();
242 __push_breaks();
244 __merge_gotos(loop_name);
246 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
247 __in_pre_condition++;
248 __split_whole_condition(stmt->iterator_pre_condition);
249 __in_pre_condition--;
250 FOR_EACH_PTR(slist, sm) {
251 set_state(sm->owner, sm->name, sm->sym, sm->state);
252 } END_FOR_EACH_PTR(sm);
253 free_slist(&slist);
254 if (extra_sm)
255 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
257 if (option_assume_loops)
258 once_through = 1;
260 __split_stmt(stmt->iterator_statement);
261 __warn_on_silly_pre_loops();
262 if (is_forever_loop(stmt)) {
263 __save_gotos(loop_name);
264 /* forever loops don't have an iterator_post_statement */
265 __discard_continues();
266 __discard_false_states();
267 __use_breaks();
268 } else {
269 __merge_continues();
270 unchanged = __iterator_unchanged(extra_sm);
271 __split_stmt(stmt->iterator_post_statement);
272 __save_gotos(loop_name);
273 __split_whole_condition(stmt->iterator_pre_condition);
274 nullify_path();
275 __merge_false_states();
276 if (once_through) {
277 __discard_false_states();
278 } else {
279 __merge_false_states();
282 if (extra_sm && unchanged)
283 __extra_pre_loop_hook_after(extra_sm,
284 stmt->iterator_post_statement,
285 stmt->iterator_pre_condition);
286 __merge_breaks();
288 loop_count--;
292 * Post loops are do {} while();
294 static void handle_post_loop(struct statement *stmt)
296 char *loop_name;
298 loop_name = get_loop_name(loop_num);
299 loop_num++;
300 loop_count++;
302 __push_continues();
303 __push_breaks();
304 __merge_gotos(loop_name);
305 __split_stmt(stmt->iterator_statement);
306 __merge_continues();
307 if (!is_zero(stmt->iterator_post_condition))
308 __save_gotos(loop_name);
310 if (is_forever_loop(stmt)) {
311 __use_breaks();
312 } else {
313 __split_whole_condition(stmt->iterator_post_condition);
314 __use_false_states();
315 __merge_breaks();
317 loop_count--;
320 static int empty_statement(struct statement *stmt)
322 if (!stmt)
323 return 0;
324 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
325 return 1;
326 return 0;
329 static struct statement *last_stmt;
330 static int is_last_stmt(struct statement *stmt)
332 if (stmt == last_stmt)
333 return 1;
334 return 0;
337 static void print_unreached_initializers(struct symbol_list *sym_list)
339 struct symbol *sym;
341 FOR_EACH_PTR(sym_list, sym) {
342 if(sym->initializer)
343 sm_msg("info: '%s' is not actually initialized (unreached code).",
344 (sym->ident ? sym->ident->name : "this variable"));
345 } END_FOR_EACH_PTR(sym);
348 static void print_unreached(struct statement *stmt)
351 static int print = 1;
353 if (!__path_is_null()) {
354 print = 1;
355 return;
357 if (!print)
358 return;
360 switch (stmt->type) {
361 case STMT_COMPOUND: /* after a switch before a case stmt */
362 case STMT_RANGE:
363 case STMT_CASE:
364 case STMT_LABEL:
365 return;
366 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
367 print_unreached_initializers(stmt->declaration);
368 return;
369 case STMT_RETURN: /* gcc complains if you don't have a return statement */
370 if (is_last_stmt(stmt))
371 return;
372 break;
373 case STMT_GOTO:
374 if (!option_spammy)
375 return;
376 break;
377 default:
378 break;
380 if (!option_spammy && empty_statement(stmt))
381 return;
382 sm_msg("info: ignoring unreachable code.");
383 print = 0;
386 void __split_stmt(struct statement *stmt)
388 if (!stmt)
389 return;
391 if (out_of_memory() || __bail_on_rest_of_function) {
392 static char *printed = NULL;
394 if (printed != cur_func)
395 sm_msg("Function too hairy. Giving up.");
396 printed = cur_func;
397 return;
400 add_ptr_list(&big_statement_stack, stmt);
401 free_expression_stack(&big_expression_stack);
402 __smatch_lineno = stmt->pos.line;
403 print_unreached(stmt);
404 __pass_to_client(stmt, STMT_HOOK);
406 switch (stmt->type) {
407 case STMT_DECLARATION:
408 split_declaration(stmt->declaration);
409 return;
410 case STMT_RETURN:
411 __split_expr(stmt->ret_value);
412 __pass_to_client(stmt->ret_value, RETURN_HOOK);
413 nullify_path();
414 return;
415 case STMT_EXPRESSION:
416 __split_expr(stmt->expression);
417 return;
418 case STMT_COMPOUND: {
419 struct statement *tmp;
420 struct statement *prev = NULL;
422 if (!last_stmt)
423 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
424 __push_scope_hooks();
425 FOR_EACH_PTR(stmt->stmts, tmp) {
426 if (!prev) {
427 next_stmt = tmp;
428 prev = tmp;
429 continue;
431 next_stmt = tmp;
432 __split_stmt(prev);
433 prev = tmp;
434 } END_FOR_EACH_PTR(tmp);
435 if (next_stmt == prev)
436 next_stmt = NULL;
437 __split_stmt(prev);
438 __call_scope_hooks();
439 return;
441 case STMT_IF:
442 if (known_condition_true(stmt->if_conditional)) {
443 __split_stmt(stmt->if_true);
444 return;
446 if (known_condition_false(stmt->if_conditional)) {
447 __split_stmt(stmt->if_false);
448 return;
450 if (option_known_conditions &&
451 implied_condition_true(stmt->if_conditional)) {
452 sm_info("this condition is true.");
453 __split_stmt(stmt->if_true);
454 return;
456 if (option_known_conditions &&
457 implied_condition_false(stmt->if_conditional)) {
458 sm_info("this condition is false.");
459 __split_stmt(stmt->if_false);
460 return;
462 __split_whole_condition(stmt->if_conditional);
463 __split_stmt(stmt->if_true);
464 if (empty_statement(stmt->if_true))
465 sm_msg("warn: if();");
466 __push_true_states();
467 __use_false_states();
468 __split_stmt(stmt->if_false);
469 __merge_true_states();
470 return;
471 case STMT_ITERATOR:
472 if (stmt->iterator_pre_condition)
473 handle_pre_loop(stmt);
474 else if (stmt->iterator_post_condition)
475 handle_post_loop(stmt);
476 else {
477 // these are for(;;) type loops.
478 handle_pre_loop(stmt);
480 return;
481 case STMT_SWITCH:
482 __split_expr(stmt->switch_expression);
483 push_expression(&switch_expr_stack, stmt->switch_expression);
484 __save_switch_states(top_expression(switch_expr_stack));
485 nullify_path();
486 __push_default();
487 __push_breaks();
488 __split_stmt(stmt->switch_statement);
489 if (!__pop_default())
490 __merge_switches(top_expression(switch_expr_stack),
491 NULL);
492 __discard_switches();
493 __merge_breaks();
494 pop_expression(&switch_expr_stack);
495 return;
496 case STMT_CASE:
497 __merge_switches(top_expression(switch_expr_stack),
498 stmt->case_expression);
499 __pass_case_to_client(top_expression(switch_expr_stack),
500 stmt->case_expression);
501 if (!stmt->case_expression)
502 __set_default();
503 __split_expr(stmt->case_expression);
504 __split_expr(stmt->case_to);
505 __split_stmt(stmt->case_statement);
506 return;
507 case STMT_LABEL:
508 if (stmt->label &&
509 stmt->label->type == SYM_LABEL &&
510 stmt->label->ident) {
511 loop_count = 1000000;
512 __merge_gotos(stmt->label->ident->name);
514 __split_stmt(stmt->label_statement);
515 return;
516 case STMT_GOTO:
517 __split_expr(stmt->goto_expression);
518 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
519 if (!strcmp(stmt->goto_label->ident->name, "break")) {
520 __process_breaks();
521 } else if (!strcmp(stmt->goto_label->ident->name,
522 "continue")) {
523 __process_continues();
525 } else if (stmt->goto_label &&
526 stmt->goto_label->type == SYM_LABEL &&
527 stmt->goto_label->ident) {
528 __save_gotos(stmt->goto_label->ident->name);
530 nullify_path();
531 return;
532 case STMT_NONE:
533 return;
534 case STMT_ASM:
535 __split_expr(stmt->asm_string);
536 split_expr_list(stmt->asm_outputs);
537 split_expr_list(stmt->asm_inputs);
538 split_expr_list(stmt->asm_clobbers);
539 return;
540 case STMT_CONTEXT:
541 return;
542 case STMT_RANGE:
543 __split_expr(stmt->range_expression);
544 __split_expr(stmt->range_low);
545 __split_expr(stmt->range_high);
546 return;
550 static void split_expr_list(struct expression_list *expr_list)
552 struct expression *expr;
553 FOR_EACH_PTR(expr_list, expr) {
554 __split_expr(expr);
555 } END_FOR_EACH_PTR(expr);
559 static void split_sym(struct symbol *sym)
561 if (!sym)
562 return;
563 if (!(sym->namespace & NS_SYMBOL))
564 return;
566 __split_stmt(sym->stmt);
567 __split_expr(sym->array_size);
568 split_symlist(sym->arguments);
569 split_symlist(sym->symbol_list);
570 __split_stmt(sym->inline_stmt);
571 split_symlist(sym->inline_symbol_list);
574 static void split_symlist(struct symbol_list *sym_list)
576 struct symbol *sym;
578 FOR_EACH_PTR(sym_list, sym) {
579 split_sym(sym);
580 } END_FOR_EACH_PTR(sym);
583 static struct expression *fake_assign_expr(struct symbol *sym)
585 struct expression *e_assign, *e_symbol;
587 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
588 e_symbol = alloc_expression(sym->initializer->pos, EXPR_SYMBOL);
589 e_assign->op = (int)'=';
590 e_symbol->symbol = sym;
591 e_symbol->symbol_name = sym->ident;
592 e_assign->left = e_symbol;
593 e_assign->right = sym->initializer;
594 return e_assign;
597 static void do_initializer_stuff(struct symbol *sym)
599 struct expression *assign;
601 if(!sym->initializer)
602 return;
603 assign = fake_assign_expr(sym);
604 __split_expr(assign);
607 static void split_declaration(struct symbol_list *sym_list)
609 struct symbol *sym;
611 FOR_EACH_PTR(sym_list, sym) {
612 __pass_to_client(sym, DECLARATION_HOOK);
613 do_initializer_stuff(sym);
614 split_sym(sym);
615 } END_FOR_EACH_PTR(sym);
618 static void split_function(struct symbol *sym)
620 struct symbol *base_type = get_base_type(sym);
622 cur_func_sym = sym;
623 if (base_type->stmt)
624 line_func_start = base_type->stmt->pos.line;
625 if (sym->ident)
626 cur_func = sym->ident->name;
627 __smatch_lineno = sym->pos.line;
628 last_stmt = NULL;
629 loop_count = 0;
630 sm_debug("new function: %s\n", cur_func);
631 if (option_two_passes) {
632 __unnullify_path();
633 loop_num = 0;
634 final_pass = 0;
635 __pass_to_client(sym, FUNC_DEF_HOOK);
636 __split_stmt(base_type->stmt);
637 nullify_path();
639 __unnullify_path();
640 loop_num = 0;
641 final_pass = 1;
642 __pass_to_client(sym, FUNC_DEF_HOOK);
643 __split_stmt(base_type->stmt);
644 __pass_to_client(sym, END_FUNC_HOOK);
645 cur_func = NULL;
646 line_func_start = 0;
647 clear_all_states();
648 free_data_info_allocs();
649 free_expression_stack(&switch_expr_stack);
650 __free_ptr_list((struct ptr_list **)&big_statement_stack);
651 __bail_on_rest_of_function = 0;
654 static void split_functions(struct symbol_list *sym_list)
656 struct symbol *sym;
658 FOR_EACH_PTR(sym_list, sym) {
659 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
660 split_function(sym);
661 } else {
662 __pass_to_client(sym, BASE_HOOK);
664 } END_FOR_EACH_PTR(sym);
665 __pass_to_client_no_data(END_FILE_HOOK);
668 void smatch (int argc, char **argv)
671 struct string_list *filelist = NULL;
672 struct symbol_list *sym_list;
673 char *file;
675 if (argc < 2) {
676 printf("Usage: smatch [--debug] <filename.c>\n");
677 exit(1);
679 sparse_initialize(argc, argv, &filelist);
680 FOR_EACH_PTR_NOTAG(filelist, file) {
681 sym_list = sparse_keep_tokens(file);
682 split_functions(sym_list);
683 } END_FOR_EACH_PTR_NOTAG(file);