Introduce add_macro_assign_hook()
[smatch.git] / smatch_flow.c
blob7d2b8dde21d9f70f7d3d55aa3985273959e5d713
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 int __expr_stmt_count;
30 static struct expression_list *switch_expr_stack = NULL;
32 struct expression_list *big_expression_stack;
33 struct statement_list *big_statement_stack;
34 int __in_pre_condition = 0;
35 int __bail_on_rest_of_function = 0;
36 char *get_function(void) { return cur_func; }
37 int get_lineno(void) { return __smatch_lineno; }
38 int get_func_pos(void) { return __smatch_lineno - line_func_start; }
39 int inside_loop(void) { return !!loop_count; }
40 int in_expression_statement(void) { return !!__expr_stmt_count; }
42 static void split_symlist(struct symbol_list *sym_list);
43 static void split_declaration(struct symbol_list *sym_list);
44 static void split_expr_list(struct expression_list *expr_list);
45 static void add_inline_function(struct symbol *sym);
47 int option_assume_loops = 0;
48 int option_known_conditions = 0;
49 int option_two_passes = 0;
50 struct symbol *cur_func_sym = NULL;
52 const char *get_filename(void)
54 if (option_full_path)
55 return full_filename;
56 return filename;
59 static void set_position(struct expression *expr)
61 int len;
62 static int prev_stream = -1;
64 __smatch_lineno = expr->pos.line;
66 if (expr->pos.stream == prev_stream)
67 return;
69 filename = stream_name(expr->pos.stream);
71 free(full_filename);
72 pathname = getcwd(NULL, 0);
73 if (pathname) {
74 len = strlen(pathname) + 1 + strlen(filename) + 1;
75 full_filename = malloc(len);
76 snprintf(full_filename, len, "%s/%s", pathname, filename);
77 } else {
78 full_filename = alloc_string(filename);
80 free(pathname);
83 static int is_inline_func(struct expression *expr)
85 if (expr->type != EXPR_SYMBOL || !expr->symbol)
86 return 0;
87 if (expr->symbol->ctype.modifiers & MOD_INLINE)
88 return 1;
89 return 0;
92 void __split_expr(struct expression *expr)
94 if (!expr)
95 return;
97 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
99 push_expression(&big_expression_stack, expr);
100 set_position(expr);
101 __pass_to_client(expr, EXPR_HOOK);
103 switch (expr->type) {
104 case EXPR_PREOP:
105 if (expr->op == '*')
106 __pass_to_client(expr, DEREF_HOOK);
107 case EXPR_POSTOP:
108 __pass_to_client(expr, OP_HOOK);
109 __split_expr(expr->unop);
110 break;
111 case EXPR_STATEMENT:
112 __expr_stmt_count++;
113 __split_stmt(expr->statement);
114 __expr_stmt_count--;
115 break;
116 case EXPR_LOGICAL:
117 case EXPR_COMPARE:
118 __pass_to_client(expr, LOGIC_HOOK);
119 __handle_logic(expr);
120 break;
121 case EXPR_BINOP:
122 __pass_to_client(expr, BINOP_HOOK);
123 case EXPR_COMMA:
124 __split_expr(expr->left);
125 __split_expr(expr->right);
126 break;
127 case EXPR_ASSIGNMENT: {
128 struct expression *tmp;
130 /* foo = !bar() */
131 if (__handle_condition_assigns(expr))
132 break;
134 /* foo = (x < 5 ? foo : 5); */
135 if (__handle_select_assigns(expr))
136 break;
138 /* foo = ({frob(); frob(); frob(); 1;}) */
139 if (__handle_expr_statement_assigns(expr))
140 break;
142 __split_expr(expr->right);
143 __pass_to_client(expr, ASSIGNMENT_HOOK);
144 tmp = strip_expr(expr->right);
145 if (tmp->type == EXPR_CALL)
146 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
147 if (get_macro_name(&tmp->pos))
148 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
149 __split_expr(expr->left);
150 break;
152 case EXPR_DEREF:
153 __pass_to_client(expr, DEREF_HOOK);
154 __split_expr(expr->deref);
155 break;
156 case EXPR_SLICE:
157 __split_expr(expr->base);
158 break;
159 case EXPR_CAST:
160 case EXPR_FORCE_CAST:
161 __split_expr(expr->cast_expression);
162 break;
163 case EXPR_SIZEOF:
164 /* there isn't anything to pass a client from inside a sizeof() */
165 break;
166 case EXPR_CONDITIONAL:
167 case EXPR_SELECT:
168 __split_whole_condition(expr->conditional);
169 __split_expr(expr->cond_true);
170 __push_true_states();
171 __use_false_states();
172 __split_expr(expr->cond_false);
173 __merge_true_states();
174 break;
175 case EXPR_CALL:
176 split_expr_list(expr->args);
177 __split_expr(expr->fn);
178 if (is_inline_func(expr->fn))
179 add_inline_function(expr->fn->symbol);
180 __pass_to_client(expr, FUNCTION_CALL_HOOK);
181 break;
182 case EXPR_INITIALIZER:
183 split_expr_list(expr->expr_list);
184 break;
185 case EXPR_IDENTIFIER:
186 __split_expr(expr->ident_expression);
187 break;
188 case EXPR_INDEX:
189 __split_expr(expr->idx_expression);
190 break;
191 case EXPR_POS:
192 __split_expr(expr->init_expr);
193 break;
194 case EXPR_SYMBOL:
195 __pass_to_client(expr, SYM_HOOK);
196 break;
197 case EXPR_STRING:
198 __pass_to_client(expr, STRING_HOOK);
199 break;
200 default:
201 break;
203 pop_expression(&big_expression_stack);
206 static int is_forever_loop(struct statement *stmt)
209 struct expression *expr;
211 expr = strip_expr(stmt->iterator_pre_condition);
212 if (!expr)
213 expr = stmt->iterator_post_condition;
214 if (!expr) {
215 /* this is a for(;;) loop... */
216 return 1;
219 if (expr->type == EXPR_VALUE && expr->value == 1) {
220 return 1;
223 return 0;
226 static int loop_num;
227 static char *get_loop_name(int num)
229 char buf[256];
231 snprintf(buf, 255, "-loop%d", num);
232 buf[255] = '\0';
233 return alloc_sname(buf);;
237 * Pre Loops are while and for loops.
240 static void handle_pre_loop(struct statement *stmt)
242 int once_through; /* we go through the loop at least once */
243 struct sm_state *extra_sm = NULL;
244 int unchanged = 0;
245 char *loop_name;
246 struct state_list *slist = NULL;
247 struct sm_state *sm = NULL;
249 loop_name = get_loop_name(loop_num);
250 loop_num++;
252 __split_stmt(stmt->iterator_pre_statement);
254 once_through = implied_condition_true(stmt->iterator_pre_condition);
256 loop_count++;
257 __push_continues();
258 __push_breaks();
260 __merge_gotos(loop_name);
262 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
263 __in_pre_condition++;
264 __split_whole_condition(stmt->iterator_pre_condition);
265 __in_pre_condition--;
266 FOR_EACH_PTR(slist, sm) {
267 set_state(sm->owner, sm->name, sm->sym, sm->state);
268 } END_FOR_EACH_PTR(sm);
269 free_slist(&slist);
270 if (extra_sm)
271 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
273 if (option_assume_loops)
274 once_through = 1;
276 __split_stmt(stmt->iterator_statement);
277 __warn_on_silly_pre_loops();
278 if (is_forever_loop(stmt)) {
279 __save_gotos(loop_name);
280 /* forever loops don't have an iterator_post_statement */
281 __discard_continues();
282 __discard_false_states();
283 __use_breaks();
284 } else {
285 __merge_continues();
286 unchanged = __iterator_unchanged(extra_sm);
287 __split_stmt(stmt->iterator_post_statement);
288 __save_gotos(loop_name);
289 __split_whole_condition(stmt->iterator_pre_condition);
290 nullify_path();
291 __merge_false_states();
292 if (once_through) {
293 __discard_false_states();
294 } else {
295 __merge_false_states();
298 if (extra_sm && unchanged)
299 __extra_pre_loop_hook_after(extra_sm,
300 stmt->iterator_post_statement,
301 stmt->iterator_pre_condition);
302 __merge_breaks();
304 loop_count--;
308 * Post loops are do {} while();
310 static void handle_post_loop(struct statement *stmt)
312 char *loop_name;
314 loop_name = get_loop_name(loop_num);
315 loop_num++;
316 loop_count++;
318 __push_continues();
319 __push_breaks();
320 __merge_gotos(loop_name);
321 __split_stmt(stmt->iterator_statement);
322 __merge_continues();
323 if (!is_zero(stmt->iterator_post_condition))
324 __save_gotos(loop_name);
326 if (is_forever_loop(stmt)) {
327 __use_breaks();
328 } else {
329 __split_whole_condition(stmt->iterator_post_condition);
330 __use_false_states();
331 __merge_breaks();
333 loop_count--;
336 static int empty_statement(struct statement *stmt)
338 if (!stmt)
339 return 0;
340 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
341 return 1;
342 return 0;
345 static int last_stmt_on_same_line()
347 struct statement *stmt;
348 int i = 0;
350 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
351 if (!i++)
352 continue;
353 if (stmt->pos.line == get_lineno())
354 return 1;
355 return 0;
356 } END_FOR_EACH_PTR_REVERSE(stmt);
357 return 0;
360 static struct statement *last_stmt;
361 static int is_last_stmt(struct statement *stmt)
363 if (stmt == last_stmt)
364 return 1;
365 return 0;
368 static void print_unreached_initializers(struct symbol_list *sym_list)
370 struct symbol *sym;
372 FOR_EACH_PTR(sym_list, sym) {
373 if(sym->initializer)
374 sm_msg("info: '%s' is not actually initialized (unreached code).",
375 (sym->ident ? sym->ident->name : "this variable"));
376 } END_FOR_EACH_PTR(sym);
379 static void print_unreached(struct statement *stmt)
382 static int print = 1;
384 if (!__path_is_null()) {
385 print = 1;
386 return;
388 if (!print)
389 return;
391 switch (stmt->type) {
392 case STMT_COMPOUND: /* after a switch before a case stmt */
393 case STMT_RANGE:
394 case STMT_CASE:
395 case STMT_LABEL:
396 return;
397 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
398 print_unreached_initializers(stmt->declaration);
399 return;
400 case STMT_RETURN: /* gcc complains if you don't have a return statement */
401 if (is_last_stmt(stmt))
402 return;
403 break;
404 case STMT_GOTO:
405 if (!option_spammy)
406 return;
407 break;
408 default:
409 break;
411 if (!option_spammy && empty_statement(stmt))
412 return;
413 sm_msg("info: ignoring unreachable code.");
414 print = 0;
417 static void split_asm_constraints(struct expression_list *expr_list)
419 struct expression *expr;
420 int state = 0;
422 FOR_EACH_PTR(expr_list, expr) {
423 switch (state) {
424 case 0: /* identifier */
425 case 1: /* constraint */
426 state++;
427 continue;
428 case 2: /* expression */
429 state = 0;
430 __split_expr(expr);
431 continue;
433 } END_FOR_EACH_PTR(expr);
436 void __split_stmt(struct statement *stmt)
438 if (!stmt)
439 return;
441 if (out_of_memory() || __bail_on_rest_of_function) {
442 static char *printed = NULL;
444 if (printed != cur_func)
445 sm_msg("Function too hairy. Giving up.");
446 printed = cur_func;
447 return;
450 add_ptr_list(&big_statement_stack, stmt);
451 free_expression_stack(&big_expression_stack);
452 __smatch_lineno = stmt->pos.line;
453 print_unreached(stmt);
454 __pass_to_client(stmt, STMT_HOOK);
456 switch (stmt->type) {
457 case STMT_DECLARATION:
458 split_declaration(stmt->declaration);
459 return;
460 case STMT_RETURN:
461 __split_expr(stmt->ret_value);
462 __pass_to_client(stmt->ret_value, RETURN_HOOK);
463 nullify_path();
464 return;
465 case STMT_EXPRESSION:
466 __split_expr(stmt->expression);
467 return;
468 case STMT_COMPOUND: {
469 struct statement *tmp;
471 if (!last_stmt)
472 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
473 __push_scope_hooks();
474 FOR_EACH_PTR(stmt->stmts, tmp) {
475 __split_stmt(tmp);
476 } END_FOR_EACH_PTR(tmp);
477 __call_scope_hooks();
478 return;
480 case STMT_IF:
481 if (known_condition_true(stmt->if_conditional)) {
482 __split_stmt(stmt->if_true);
483 return;
485 if (known_condition_false(stmt->if_conditional)) {
486 __split_stmt(stmt->if_false);
487 return;
489 if (option_known_conditions &&
490 implied_condition_true(stmt->if_conditional)) {
491 sm_info("this condition is true.");
492 __split_stmt(stmt->if_true);
493 return;
495 if (option_known_conditions &&
496 implied_condition_false(stmt->if_conditional)) {
497 sm_info("this condition is false.");
498 __split_stmt(stmt->if_false);
499 return;
501 __split_whole_condition(stmt->if_conditional);
502 __split_stmt(stmt->if_true);
503 if (empty_statement(stmt->if_true) &&
504 last_stmt_on_same_line() &&
505 !get_macro_name(&stmt->if_true->pos))
506 sm_msg("warn: if();");
507 __push_true_states();
508 __use_false_states();
509 __split_stmt(stmt->if_false);
510 __merge_true_states();
511 return;
512 case STMT_ITERATOR:
513 if (stmt->iterator_pre_condition)
514 handle_pre_loop(stmt);
515 else if (stmt->iterator_post_condition)
516 handle_post_loop(stmt);
517 else {
518 // these are for(;;) type loops.
519 handle_pre_loop(stmt);
521 return;
522 case STMT_SWITCH:
523 __split_expr(stmt->switch_expression);
524 push_expression(&switch_expr_stack, stmt->switch_expression);
525 __save_switch_states(top_expression(switch_expr_stack));
526 nullify_path();
527 __push_default();
528 __push_breaks();
529 __split_stmt(stmt->switch_statement);
530 if (!__pop_default())
531 __merge_switches(top_expression(switch_expr_stack),
532 NULL);
533 __discard_switches();
534 __merge_breaks();
535 pop_expression(&switch_expr_stack);
536 return;
537 case STMT_CASE:
538 __merge_switches(top_expression(switch_expr_stack),
539 stmt->case_expression);
540 __pass_case_to_client(top_expression(switch_expr_stack),
541 stmt->case_expression);
542 if (!stmt->case_expression)
543 __set_default();
544 __split_expr(stmt->case_expression);
545 __split_expr(stmt->case_to);
546 __split_stmt(stmt->case_statement);
547 return;
548 case STMT_LABEL:
549 if (stmt->label &&
550 stmt->label->type == SYM_LABEL &&
551 stmt->label->ident) {
552 loop_count = 1000000;
553 __merge_gotos(stmt->label->ident->name);
555 __split_stmt(stmt->label_statement);
556 return;
557 case STMT_GOTO:
558 __split_expr(stmt->goto_expression);
559 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
560 if (!strcmp(stmt->goto_label->ident->name, "break")) {
561 __process_breaks();
562 } else if (!strcmp(stmt->goto_label->ident->name,
563 "continue")) {
564 __process_continues();
566 } else if (stmt->goto_label &&
567 stmt->goto_label->type == SYM_LABEL &&
568 stmt->goto_label->ident) {
569 __save_gotos(stmt->goto_label->ident->name);
571 nullify_path();
572 return;
573 case STMT_NONE:
574 return;
575 case STMT_ASM:
576 __split_expr(stmt->asm_string);
577 split_asm_constraints(stmt->asm_outputs);
578 split_asm_constraints(stmt->asm_inputs);
579 split_asm_constraints(stmt->asm_clobbers);
580 return;
581 case STMT_CONTEXT:
582 return;
583 case STMT_RANGE:
584 __split_expr(stmt->range_expression);
585 __split_expr(stmt->range_low);
586 __split_expr(stmt->range_high);
587 return;
591 static void split_expr_list(struct expression_list *expr_list)
593 struct expression *expr;
595 FOR_EACH_PTR(expr_list, expr) {
596 __split_expr(expr);
597 } END_FOR_EACH_PTR(expr);
601 static void split_sym(struct symbol *sym)
603 if (!sym)
604 return;
605 if (!(sym->namespace & NS_SYMBOL))
606 return;
608 __split_stmt(sym->stmt);
609 __split_expr(sym->array_size);
610 split_symlist(sym->arguments);
611 split_symlist(sym->symbol_list);
612 __split_stmt(sym->inline_stmt);
613 split_symlist(sym->inline_symbol_list);
616 static void split_symlist(struct symbol_list *sym_list)
618 struct symbol *sym;
620 FOR_EACH_PTR(sym_list, sym) {
621 split_sym(sym);
622 } END_FOR_EACH_PTR(sym);
625 static struct expression *fake_assign_expr(struct symbol *sym)
627 struct expression *e_assign, *e_symbol;
629 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
630 e_symbol = alloc_expression(sym->initializer->pos, EXPR_SYMBOL);
631 e_assign->op = (int)'=';
632 e_symbol->symbol = sym;
633 e_symbol->symbol_name = sym->ident;
634 e_assign->left = e_symbol;
635 e_assign->right = sym->initializer;
636 return e_assign;
639 static void do_initializer_stuff(struct symbol *sym)
641 struct expression *assign;
643 if(!sym->initializer)
644 return;
645 assign = fake_assign_expr(sym);
646 __split_expr(assign);
649 static void split_declaration(struct symbol_list *sym_list)
651 struct symbol *sym;
653 FOR_EACH_PTR(sym_list, sym) {
654 __pass_to_client(sym, DECLARATION_HOOK);
655 do_initializer_stuff(sym);
656 split_sym(sym);
657 } END_FOR_EACH_PTR(sym);
660 static void split_function(struct symbol *sym)
662 struct symbol *base_type = get_base_type(sym);
664 cur_func_sym = sym;
665 if (base_type->stmt)
666 line_func_start = base_type->stmt->pos.line;
667 if (sym->ident)
668 cur_func = sym->ident->name;
669 __smatch_lineno = sym->pos.line;
670 last_stmt = NULL;
671 loop_count = 0;
672 sm_debug("new function: %s\n", cur_func);
673 if (option_two_passes) {
674 __unnullify_path();
675 loop_num = 0;
676 final_pass = 0;
677 __pass_to_client(sym, FUNC_DEF_HOOK);
678 __split_stmt(base_type->stmt);
679 __split_stmt(base_type->inline_stmt);
680 nullify_path();
682 __unnullify_path();
683 loop_num = 0;
684 final_pass = 1;
685 __pass_to_client(sym, FUNC_DEF_HOOK);
686 __split_stmt(base_type->stmt);
687 __split_stmt(base_type->inline_stmt);
688 __pass_to_client(sym, END_FUNC_HOOK);
689 cur_func = NULL;
690 line_func_start = 0;
691 clear_all_states();
692 free_data_info_allocs();
693 free_expression_stack(&switch_expr_stack);
694 __free_ptr_list((struct ptr_list **)&big_statement_stack);
695 __bail_on_rest_of_function = 0;
698 static struct symbol_list *inlines_called;
699 static void add_inline_function(struct symbol *sym)
701 static struct symbol_list *already_added;
702 struct symbol *tmp;
704 FOR_EACH_PTR(already_added, tmp) {
705 if (tmp == sym)
706 return;
707 } END_FOR_EACH_PTR(tmp);
709 add_ptr_list(&already_added, sym);
710 add_ptr_list(&inlines_called, sym);
713 static void process_inlines()
715 struct symbol *tmp;
717 FOR_EACH_PTR(inlines_called, tmp) {
718 split_function(tmp);
719 } END_FOR_EACH_PTR(tmp);
720 free_ptr_list(&inlines_called);
723 static void split_functions(struct symbol_list *sym_list)
725 struct symbol *sym;
727 FOR_EACH_PTR(sym_list, sym) {
728 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
729 split_function(sym);
730 process_inlines();
731 } else {
732 __pass_to_client(sym, BASE_HOOK);
734 } END_FOR_EACH_PTR(sym);
735 __pass_to_client_no_data(END_FILE_HOOK);
738 void smatch (int argc, char **argv)
741 struct string_list *filelist = NULL;
742 struct symbol_list *sym_list;
743 char *file;
745 if (argc < 2) {
746 printf("Usage: smatch [--debug] <filename.c>\n");
747 exit(1);
749 sparse_initialize(argc, argv, &filelist);
750 FOR_EACH_PTR_NOTAG(filelist, file) {
751 sym_list = sparse_keep_tokens(file);
752 split_functions(sym_list);
753 } END_FOR_EACH_PTR_NOTAG(file);