flow: fix segfault on parse error
[smatch.git] / smatch_flow.c
blobed90775bb6f3f48b0ead36d3dbe3168417b8aa2a
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 inside_loop(void) { return !!loop_count; }
39 int in_expression_statement(void) { return !!__expr_stmt_count; }
41 static void split_symlist(struct symbol_list *sym_list);
42 static void split_declaration(struct symbol_list *sym_list);
43 static void split_expr_list(struct expression_list *expr_list);
44 static void add_inline_function(struct symbol *sym);
46 int option_assume_loops = 0;
47 int option_known_conditions = 0;
48 int option_two_passes = 0;
49 struct symbol *cur_func_sym = NULL;
51 const char *get_filename(void)
53 if (option_full_path)
54 return full_filename;
55 return filename;
58 static void set_position(struct position pos)
60 int len;
61 static int prev_stream = -1;
63 __smatch_lineno = pos.line;
65 if (pos.stream == prev_stream)
66 return;
68 filename = stream_name(pos.stream);
70 free(full_filename);
71 pathname = getcwd(NULL, 0);
72 if (pathname) {
73 len = strlen(pathname) + 1 + strlen(filename) + 1;
74 full_filename = malloc(len);
75 snprintf(full_filename, len, "%s/%s", pathname, filename);
76 } else {
77 full_filename = alloc_string(filename);
79 free(pathname);
82 static int is_inline_func(struct expression *expr)
84 if (expr->type != EXPR_SYMBOL || !expr->symbol)
85 return 0;
86 if (expr->symbol->ctype.modifiers & MOD_INLINE)
87 return 1;
88 return 0;
91 void __split_expr(struct expression *expr)
93 if (!expr)
94 return;
96 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
98 push_expression(&big_expression_stack, expr);
99 set_position(expr->pos);
100 __pass_to_client(expr, EXPR_HOOK);
102 switch (expr->type) {
103 case EXPR_PREOP:
104 if (expr->op == '*')
105 __pass_to_client(expr, DEREF_HOOK);
106 case EXPR_POSTOP:
107 __pass_to_client(expr, OP_HOOK);
108 __split_expr(expr->unop);
109 break;
110 case EXPR_STATEMENT:
111 __expr_stmt_count++;
112 __split_stmt(expr->statement);
113 __expr_stmt_count--;
114 break;
115 case EXPR_LOGICAL:
116 case EXPR_COMPARE:
117 __pass_to_client(expr, LOGIC_HOOK);
118 __handle_logic(expr);
119 break;
120 case EXPR_BINOP:
121 __pass_to_client(expr, BINOP_HOOK);
122 case EXPR_COMMA:
123 __split_expr(expr->left);
124 __split_expr(expr->right);
125 break;
126 case EXPR_ASSIGNMENT: {
127 struct expression *tmp;
129 if (!expr->right)
130 break;
132 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
134 /* foo = !bar() */
135 if (__handle_condition_assigns(expr))
136 break;
137 /* foo = (x < 5 ? foo : 5); */
138 if (__handle_select_assigns(expr))
139 break;
140 /* foo = ({frob(); frob(); frob(); 1;}) */
141 if (__handle_expr_statement_assigns(expr))
142 break;
144 __split_expr(expr->right);
145 __pass_to_client(expr, ASSIGNMENT_HOOK);
146 tmp = strip_expr(expr->right);
147 if (tmp->type == EXPR_CALL)
148 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
149 if (get_macro_name(&tmp->pos))
150 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
151 __split_expr(expr->left);
152 break;
154 case EXPR_DEREF:
155 __pass_to_client(expr, DEREF_HOOK);
156 __split_expr(expr->deref);
157 break;
158 case EXPR_SLICE:
159 __split_expr(expr->base);
160 break;
161 case EXPR_CAST:
162 case EXPR_FORCE_CAST:
163 __pass_to_client(expr, CAST_HOOK);
164 __split_expr(expr->cast_expression);
165 break;
166 case EXPR_SIZEOF:
167 /* there isn't anything to pass a client from inside a sizeof() */
168 break;
169 case EXPR_CONDITIONAL:
170 case EXPR_SELECT:
171 __pass_to_client(expr, SELECT_HOOK);
172 __split_whole_condition(expr->conditional);
173 __split_expr(expr->cond_true);
174 __push_true_states();
175 __use_false_states();
176 __split_expr(expr->cond_false);
177 __merge_true_states();
178 break;
179 case EXPR_CALL:
180 split_expr_list(expr->args);
181 __split_expr(expr->fn);
182 if (is_inline_func(expr->fn))
183 add_inline_function(expr->fn->symbol);
184 __pass_to_client(expr, FUNCTION_CALL_HOOK);
185 break;
186 case EXPR_INITIALIZER:
187 split_expr_list(expr->expr_list);
188 break;
189 case EXPR_IDENTIFIER:
190 __split_expr(expr->ident_expression);
191 break;
192 case EXPR_INDEX:
193 __split_expr(expr->idx_expression);
194 break;
195 case EXPR_POS:
196 __split_expr(expr->init_expr);
197 break;
198 case EXPR_SYMBOL:
199 __pass_to_client(expr, SYM_HOOK);
200 break;
201 case EXPR_STRING:
202 __pass_to_client(expr, STRING_HOOK);
203 break;
204 default:
205 break;
207 pop_expression(&big_expression_stack);
210 static int is_forever_loop(struct statement *stmt)
212 struct expression *expr;
214 expr = strip_expr(stmt->iterator_pre_condition);
215 if (!expr)
216 expr = stmt->iterator_post_condition;
217 if (!expr) {
218 /* this is a for(;;) loop... */
219 return 1;
222 if (expr->type == EXPR_VALUE && expr->value == 1)
223 return 1;
225 return 0;
228 static int loop_num;
229 static char *get_loop_name(int num)
231 char buf[256];
233 snprintf(buf, 255, "-loop%d", num);
234 buf[255] = '\0';
235 return alloc_sname(buf);
239 * Pre Loops are while and for loops.
241 static void handle_pre_loop(struct statement *stmt)
243 int once_through; /* we go through the loop at least once */
244 struct sm_state *extra_sm = NULL;
245 int unchanged = 0;
246 char *loop_name;
247 struct state_list *slist = NULL;
248 struct sm_state *sm = NULL;
250 loop_name = get_loop_name(loop_num);
251 loop_num++;
253 __split_stmt(stmt->iterator_pre_statement);
255 once_through = implied_condition_true(stmt->iterator_pre_condition);
257 loop_count++;
258 __push_continues();
259 __push_breaks();
261 __merge_gotos(loop_name);
263 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
264 __in_pre_condition++;
265 __pass_to_client(stmt, PRELOOP_HOOK);
266 __split_whole_condition(stmt->iterator_pre_condition);
267 __in_pre_condition--;
268 FOR_EACH_PTR(slist, sm) {
269 set_state(sm->owner, sm->name, sm->sym, sm->state);
270 } END_FOR_EACH_PTR(sm);
271 free_slist(&slist);
272 if (extra_sm)
273 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
275 if (option_assume_loops)
276 once_through = 1;
278 __split_stmt(stmt->iterator_statement);
279 __warn_on_silly_pre_loops();
280 if (is_forever_loop(stmt)) {
281 __save_gotos(loop_name);
282 /* forever loops don't have an iterator_post_statement */
283 __discard_continues();
284 __discard_false_states();
285 __use_breaks();
286 } else {
287 __merge_continues();
288 unchanged = __iterator_unchanged(extra_sm);
289 __split_stmt(stmt->iterator_post_statement);
290 __save_gotos(loop_name);
291 __split_whole_condition(stmt->iterator_pre_condition);
292 nullify_path();
293 __merge_false_states();
294 if (once_through)
295 __discard_false_states();
296 else
297 __merge_false_states();
299 if (extra_sm && unchanged)
300 __extra_pre_loop_hook_after(extra_sm,
301 stmt->iterator_post_statement,
302 stmt->iterator_pre_condition);
303 __merge_breaks();
305 loop_count--;
309 * Post loops are do {} while();
311 static void handle_post_loop(struct statement *stmt)
313 char *loop_name;
315 loop_name = get_loop_name(loop_num);
316 loop_num++;
317 loop_count++;
319 __push_continues();
320 __push_breaks();
321 __merge_gotos(loop_name);
322 __split_stmt(stmt->iterator_statement);
323 __merge_continues();
324 if (!is_zero(stmt->iterator_post_condition))
325 __save_gotos(loop_name);
327 if (is_forever_loop(stmt)) {
328 __use_breaks();
329 } else {
330 __split_whole_condition(stmt->iterator_post_condition);
331 __use_false_states();
332 __merge_breaks();
334 loop_count--;
337 static int empty_statement(struct statement *stmt)
339 if (!stmt)
340 return 0;
341 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
342 return 1;
343 return 0;
346 static int last_stmt_on_same_line()
348 struct statement *stmt;
349 int i = 0;
351 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
352 if (!i++)
353 continue;
354 if (stmt->pos.line == get_lineno())
355 return 1;
356 return 0;
357 } END_FOR_EACH_PTR_REVERSE(stmt);
358 return 0;
361 static struct statement *last_stmt;
362 static int is_last_stmt(struct statement *stmt)
364 if (stmt == last_stmt)
365 return 1;
366 return 0;
369 static void print_unreached_initializers(struct symbol_list *sym_list)
371 struct symbol *sym;
373 FOR_EACH_PTR(sym_list, sym) {
374 if (sym->initializer)
375 sm_msg("info: '%s' is not actually initialized (unreached code).",
376 (sym->ident ? sym->ident->name : "this variable"));
377 } END_FOR_EACH_PTR(sym);
380 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 static int is_case_val(struct statement *stmt, long long val)
438 long long case_val;
440 if (stmt->type != STMT_CASE)
441 return 0;
442 if (!stmt->case_expression) {
443 __set_default();
444 return 1;
446 if (!get_value(stmt->case_expression, &case_val))
447 return 0;
448 if (case_val == val)
449 return 1;
450 return 0;
453 static void split_known_switch(struct statement *stmt, long long val)
455 struct statement *tmp;
457 __split_expr(stmt->switch_expression);
459 push_expression(&switch_expr_stack, stmt->switch_expression);
460 __save_switch_states(top_expression(switch_expr_stack));
461 nullify_path();
462 __push_default();
463 __push_breaks();
465 stmt = stmt->switch_statement;
467 if (!last_stmt)
468 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
470 __push_scope_hooks();
471 FOR_EACH_PTR(stmt->stmts, tmp) {
472 __smatch_lineno = tmp->pos.line;
473 if (is_case_val(tmp, val)) {
474 __merge_switches(top_expression(switch_expr_stack),
475 stmt->case_expression);
476 __pass_case_to_client(top_expression(switch_expr_stack),
477 stmt->case_expression);
479 if (__path_is_null())
480 continue;
481 __split_stmt(tmp);
482 if (__path_is_null()) {
483 __set_default();
484 goto out;
486 } END_FOR_EACH_PTR(tmp);
487 out:
488 __call_scope_hooks();
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);
497 void __split_stmt(struct statement *stmt)
499 long long val;
501 if (!stmt)
502 return;
504 if (out_of_memory() || __bail_on_rest_of_function) {
505 static char *printed = NULL;
507 if (printed != cur_func)
508 sm_msg("Function too hairy. Giving up.");
509 printed = cur_func;
510 return;
513 add_ptr_list(&big_statement_stack, stmt);
514 free_expression_stack(&big_expression_stack);
515 set_position(stmt->pos);
516 print_unreached(stmt);
517 __pass_to_client(stmt, STMT_HOOK);
519 switch (stmt->type) {
520 case STMT_DECLARATION:
521 split_declaration(stmt->declaration);
522 return;
523 case STMT_RETURN:
524 __split_expr(stmt->ret_value);
525 __pass_to_client(stmt->ret_value, RETURN_HOOK);
526 nullify_path();
527 return;
528 case STMT_EXPRESSION:
529 __split_expr(stmt->expression);
530 return;
531 case STMT_COMPOUND: {
532 struct statement *tmp;
534 if (!last_stmt)
535 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
536 __push_scope_hooks();
537 FOR_EACH_PTR(stmt->stmts, tmp) {
538 __split_stmt(tmp);
539 } END_FOR_EACH_PTR(tmp);
540 __call_scope_hooks();
541 return;
543 case STMT_IF:
544 if (known_condition_true(stmt->if_conditional)) {
545 __split_stmt(stmt->if_true);
546 return;
548 if (known_condition_false(stmt->if_conditional)) {
549 __split_stmt(stmt->if_false);
550 return;
552 if (option_known_conditions &&
553 implied_condition_true(stmt->if_conditional)) {
554 sm_info("this condition is true.");
555 __split_stmt(stmt->if_true);
556 return;
558 if (option_known_conditions &&
559 implied_condition_false(stmt->if_conditional)) {
560 sm_info("this condition is false.");
561 __split_stmt(stmt->if_false);
562 return;
564 __split_whole_condition(stmt->if_conditional);
565 __split_stmt(stmt->if_true);
566 if (empty_statement(stmt->if_true) &&
567 last_stmt_on_same_line() &&
568 !get_macro_name(&stmt->if_true->pos))
569 sm_msg("warn: if();");
570 __push_true_states();
571 __use_false_states();
572 __split_stmt(stmt->if_false);
573 __merge_true_states();
574 return;
575 case STMT_ITERATOR:
576 if (stmt->iterator_pre_condition)
577 handle_pre_loop(stmt);
578 else if (stmt->iterator_post_condition)
579 handle_post_loop(stmt);
580 else {
581 // these are for(;;) type loops.
582 handle_pre_loop(stmt);
584 return;
585 case STMT_SWITCH:
586 if (get_value(stmt->switch_expression, &val)) {
587 split_known_switch(stmt, val);
588 return;
590 __split_expr(stmt->switch_expression);
591 push_expression(&switch_expr_stack, stmt->switch_expression);
592 __save_switch_states(top_expression(switch_expr_stack));
593 nullify_path();
594 __push_default();
595 __push_breaks();
596 __split_stmt(stmt->switch_statement);
597 if (!__pop_default())
598 __merge_switches(top_expression(switch_expr_stack),
599 NULL);
600 __discard_switches();
601 __merge_breaks();
602 pop_expression(&switch_expr_stack);
603 return;
604 case STMT_CASE:
605 __merge_switches(top_expression(switch_expr_stack),
606 stmt->case_expression);
607 __pass_case_to_client(top_expression(switch_expr_stack),
608 stmt->case_expression);
609 if (!stmt->case_expression)
610 __set_default();
611 __split_expr(stmt->case_expression);
612 __split_expr(stmt->case_to);
613 __split_stmt(stmt->case_statement);
614 return;
615 case STMT_LABEL:
616 if (stmt->label_identifier &&
617 stmt->label_identifier->type == SYM_LABEL &&
618 stmt->label_identifier->ident) {
619 loop_count = 1000000;
620 __merge_gotos(stmt->label_identifier->ident->name);
622 __split_stmt(stmt->label_statement);
623 return;
624 case STMT_GOTO:
625 __split_expr(stmt->goto_expression);
626 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
627 if (!strcmp(stmt->goto_label->ident->name, "break")) {
628 __process_breaks();
629 } else if (!strcmp(stmt->goto_label->ident->name,
630 "continue")) {
631 __process_continues();
633 } else if (stmt->goto_label &&
634 stmt->goto_label->type == SYM_LABEL &&
635 stmt->goto_label->ident) {
636 __save_gotos(stmt->goto_label->ident->name);
638 nullify_path();
639 return;
640 case STMT_NONE:
641 return;
642 case STMT_ASM:
643 __pass_to_client(stmt, ASM_HOOK);
644 __split_expr(stmt->asm_string);
645 split_asm_constraints(stmt->asm_outputs);
646 split_asm_constraints(stmt->asm_inputs);
647 split_asm_constraints(stmt->asm_clobbers);
648 return;
649 case STMT_CONTEXT:
650 return;
651 case STMT_RANGE:
652 __split_expr(stmt->range_expression);
653 __split_expr(stmt->range_low);
654 __split_expr(stmt->range_high);
655 return;
659 static void split_expr_list(struct expression_list *expr_list)
661 struct expression *expr;
663 FOR_EACH_PTR(expr_list, expr) {
664 __split_expr(expr);
665 } END_FOR_EACH_PTR(expr);
668 static void split_sym(struct symbol *sym)
670 if (!sym)
671 return;
672 if (!(sym->namespace & NS_SYMBOL))
673 return;
675 __split_stmt(sym->stmt);
676 __split_expr(sym->array_size);
677 split_symlist(sym->arguments);
678 split_symlist(sym->symbol_list);
679 __split_stmt(sym->inline_stmt);
680 split_symlist(sym->inline_symbol_list);
683 static void split_symlist(struct symbol_list *sym_list)
685 struct symbol *sym;
687 FOR_EACH_PTR(sym_list, sym) {
688 split_sym(sym);
689 } END_FOR_EACH_PTR(sym);
692 static struct expression *fake_assign_expr(struct symbol *sym)
694 struct expression *e_assign, *e_symbol;
696 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
697 e_symbol = alloc_expression(sym->pos, EXPR_SYMBOL);
698 e_assign->op = (int)'=';
699 e_symbol->symbol = sym;
700 e_symbol->symbol_name = sym->ident;
701 e_assign->left = e_symbol;
702 e_assign->right = sym->initializer;
703 return e_assign;
706 static void do_initializer_stuff(struct symbol *sym)
708 struct expression *assign;
710 if (!sym->initializer)
711 return;
712 assign = fake_assign_expr(sym);
713 __split_expr(assign);
716 static void split_declaration(struct symbol_list *sym_list)
718 struct symbol *sym;
720 FOR_EACH_PTR(sym_list, sym) {
721 __pass_to_client(sym, DECLARATION_HOOK);
722 do_initializer_stuff(sym);
723 split_sym(sym);
724 } END_FOR_EACH_PTR(sym);
727 static void split_function(struct symbol *sym)
729 struct symbol *base_type = get_base_type(sym);
731 cur_func_sym = sym;
732 if (base_type->stmt)
733 line_func_start = base_type->stmt->pos.line;
734 if (sym->ident)
735 cur_func = sym->ident->name;
736 __smatch_lineno = sym->pos.line;
737 last_stmt = NULL;
738 loop_count = 0;
739 sm_debug("new function: %s\n", cur_func);
740 if (option_two_passes) {
741 __unnullify_path();
742 loop_num = 0;
743 final_pass = 0;
744 __pass_to_client(sym, FUNC_DEF_HOOK);
745 __split_stmt(base_type->stmt);
746 __split_stmt(base_type->inline_stmt);
747 nullify_path();
749 __unnullify_path();
750 loop_num = 0;
751 final_pass = 1;
752 __pass_to_client(sym, FUNC_DEF_HOOK);
753 __split_stmt(base_type->stmt);
754 __split_stmt(base_type->inline_stmt);
755 __pass_to_client(sym, END_FUNC_HOOK);
756 cur_func = NULL;
757 line_func_start = 0;
758 clear_all_states();
759 free_data_info_allocs();
760 free_expression_stack(&switch_expr_stack);
761 __free_ptr_list((struct ptr_list **)&big_statement_stack);
762 __bail_on_rest_of_function = 0;
765 static struct symbol_list *inlines_called;
766 static void add_inline_function(struct symbol *sym)
768 static struct symbol_list *already_added;
769 struct symbol *tmp;
771 FOR_EACH_PTR(already_added, tmp) {
772 if (tmp == sym)
773 return;
774 } END_FOR_EACH_PTR(tmp);
776 add_ptr_list(&already_added, sym);
777 add_ptr_list(&inlines_called, sym);
780 static void process_inlines()
782 struct symbol *tmp;
784 FOR_EACH_PTR(inlines_called, tmp) {
785 split_function(tmp);
786 } END_FOR_EACH_PTR(tmp);
787 free_ptr_list(&inlines_called);
790 static void split_functions(struct symbol_list *sym_list)
792 struct symbol *sym;
794 FOR_EACH_PTR(sym_list, sym) {
795 set_position(sym->pos);
796 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
797 split_function(sym);
798 process_inlines();
799 } else {
800 __pass_to_client(sym, BASE_HOOK);
802 } END_FOR_EACH_PTR(sym);
803 __pass_to_client_no_data(END_FILE_HOOK);
806 void smatch(int argc, char **argv)
809 struct string_list *filelist = NULL;
810 struct symbol_list *sym_list;
811 char *file;
813 if (argc < 2) {
814 printf("Usage: smatch [--debug] <filename.c>\n");
815 exit(1);
817 sparse_initialize(argc, argv, &filelist);
818 FOR_EACH_PTR_NOTAG(filelist, file) {
819 if (option_file_output) {
820 char buf[256];
822 snprintf(buf, sizeof(buf), "%s.smatch", file);
823 sm_outfd = fopen(buf, "w");
824 if (!sm_outfd) {
825 printf("Error: Cannot open %s\n", file);
826 exit(1);
829 sym_list = sparse_keep_tokens(file);
830 split_functions(sym_list);
831 } END_FOR_EACH_PTR_NOTAG(file);