sval: make cast_rl() preserve things better across signed casts
[smatch.git] / smatch_flow.c
blob2ede12e4d3b49b628b064e3d2f41482a7e3ad4fb
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 char *base_file;
24 static const char *filename;
25 static char *pathname;
26 static char *full_filename;
27 static char *cur_func;
28 static int line_func_start;
29 static int loop_count;
30 int __expr_stmt_count;
31 static struct expression_list *switch_expr_stack = NULL;
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 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_info)
55 return base_file;
56 if (option_full_path)
57 return full_filename;
58 return filename;
61 static void set_position(struct position pos)
63 int len;
64 static int prev_stream = -1;
66 __smatch_lineno = pos.line;
68 if (pos.stream == prev_stream)
69 return;
71 filename = stream_name(pos.stream);
73 free(full_filename);
74 pathname = getcwd(NULL, 0);
75 if (pathname) {
76 len = strlen(pathname) + 1 + strlen(filename) + 1;
77 full_filename = malloc(len);
78 snprintf(full_filename, len, "%s/%s", pathname, filename);
79 } else {
80 full_filename = alloc_string(filename);
82 free(pathname);
85 static int is_inline_func(struct expression *expr)
87 if (expr->type != EXPR_SYMBOL || !expr->symbol)
88 return 0;
89 if (expr->symbol->ctype.modifiers & MOD_INLINE)
90 return 1;
91 return 0;
94 static int is_noreturn_func(struct expression *expr)
96 if (expr->type != EXPR_SYMBOL || !expr->symbol)
97 return 0;
98 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
99 return 1;
100 return 0;
103 void __split_expr(struct expression *expr)
105 if (!expr)
106 return;
108 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
110 push_expression(&big_expression_stack, expr);
111 set_position(expr->pos);
112 __pass_to_client(expr, EXPR_HOOK);
114 switch (expr->type) {
115 case EXPR_PREOP:
116 if (expr->op == '*')
117 __pass_to_client(expr, DEREF_HOOK);
118 case EXPR_POSTOP:
119 __pass_to_client(expr, OP_HOOK);
120 __split_expr(expr->unop);
121 break;
122 case EXPR_STATEMENT:
123 __expr_stmt_count++;
124 __split_stmt(expr->statement);
125 __expr_stmt_count--;
126 break;
127 case EXPR_LOGICAL:
128 case EXPR_COMPARE:
129 __pass_to_client(expr, LOGIC_HOOK);
130 __handle_logic(expr);
131 break;
132 case EXPR_BINOP:
133 __pass_to_client(expr, BINOP_HOOK);
134 case EXPR_COMMA:
135 __split_expr(expr->left);
136 __split_expr(expr->right);
137 break;
138 case EXPR_ASSIGNMENT: {
139 struct expression *tmp;
141 if (!expr->right)
142 break;
144 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
146 /* foo = !bar() */
147 if (__handle_condition_assigns(expr))
148 break;
149 /* foo = (x < 5 ? foo : 5); */
150 if (__handle_select_assigns(expr))
151 break;
152 /* foo = ({frob(); frob(); frob(); 1;}) */
153 if (__handle_expr_statement_assigns(expr))
154 break;
156 __split_expr(expr->right);
157 __pass_to_client(expr, ASSIGNMENT_HOOK);
158 tmp = strip_expr(expr->right);
159 if (tmp->type == EXPR_CALL)
160 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
161 if (get_macro_name(tmp->pos) &&
162 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
163 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
164 __split_expr(expr->left);
165 break;
167 case EXPR_DEREF:
168 __pass_to_client(expr, DEREF_HOOK);
169 __split_expr(expr->deref);
170 break;
171 case EXPR_SLICE:
172 __split_expr(expr->base);
173 break;
174 case EXPR_CAST:
175 case EXPR_FORCE_CAST:
176 __pass_to_client(expr, CAST_HOOK);
177 __split_expr(expr->cast_expression);
178 break;
179 case EXPR_SIZEOF:
180 /* there isn't anything to pass a client from inside a sizeof() */
181 break;
182 case EXPR_OFFSETOF:
183 case EXPR_ALIGNOF:
184 evaluate_expression(expr);
185 break;
186 case EXPR_CONDITIONAL:
187 case EXPR_SELECT:
188 if (known_condition_true(expr->conditional)) {
189 __split_expr(expr->cond_true);
190 break;
192 if (known_condition_false(expr->conditional)) {
193 __split_expr(expr->cond_false);
194 break;
196 __pass_to_client(expr, SELECT_HOOK);
197 __split_whole_condition(expr->conditional);
198 __split_expr(expr->cond_true);
199 __push_true_states();
200 __use_false_states();
201 __split_expr(expr->cond_false);
202 __merge_true_states();
203 break;
204 case EXPR_CALL:
205 if (sym_name_is("__builtin_constant_p", expr->fn))
206 break;
207 split_expr_list(expr->args);
208 __split_expr(expr->fn);
209 if (is_inline_func(expr->fn))
210 add_inline_function(expr->fn->symbol);
211 __pass_to_client(expr, FUNCTION_CALL_HOOK);
212 if (is_noreturn_func(expr->fn))
213 nullify_path();
214 break;
215 case EXPR_INITIALIZER:
216 split_expr_list(expr->expr_list);
217 break;
218 case EXPR_IDENTIFIER:
219 __split_expr(expr->ident_expression);
220 break;
221 case EXPR_INDEX:
222 __split_expr(expr->idx_expression);
223 break;
224 case EXPR_POS:
225 __split_expr(expr->init_expr);
226 break;
227 case EXPR_SYMBOL:
228 __pass_to_client(expr, SYM_HOOK);
229 break;
230 case EXPR_STRING:
231 __pass_to_client(expr, STRING_HOOK);
232 break;
233 default:
234 break;
236 pop_expression(&big_expression_stack);
239 static int is_forever_loop(struct statement *stmt)
241 struct expression *expr;
243 expr = strip_expr(stmt->iterator_pre_condition);
244 if (!expr)
245 expr = stmt->iterator_post_condition;
246 if (!expr) {
247 /* this is a for(;;) loop... */
248 return 1;
251 if (expr->type == EXPR_VALUE && expr->value == 1)
252 return 1;
254 return 0;
257 static int loop_num;
258 static char *get_loop_name(int num)
260 char buf[256];
262 snprintf(buf, 255, "-loop%d", num);
263 buf[255] = '\0';
264 return alloc_sname(buf);
268 * Pre Loops are while and for loops.
270 static void handle_pre_loop(struct statement *stmt)
272 int once_through; /* we go through the loop at least once */
273 struct sm_state *extra_sm = NULL;
274 int unchanged = 0;
275 char *loop_name;
276 struct state_list *slist = NULL;
277 struct sm_state *sm = NULL;
279 loop_name = get_loop_name(loop_num);
280 loop_num++;
282 __split_stmt(stmt->iterator_pre_statement);
284 once_through = implied_condition_true(stmt->iterator_pre_condition);
286 loop_count++;
287 __push_continues();
288 __push_breaks();
290 __merge_gotos(loop_name);
292 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
293 __in_pre_condition++;
294 __pass_to_client(stmt, PRELOOP_HOOK);
295 __split_whole_condition(stmt->iterator_pre_condition);
296 __in_pre_condition--;
297 FOR_EACH_PTR(slist, sm) {
298 set_state(sm->owner, sm->name, sm->sym, sm->state);
299 } END_FOR_EACH_PTR(sm);
300 free_slist(&slist);
301 if (extra_sm)
302 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
304 if (option_assume_loops)
305 once_through = 1;
307 __split_stmt(stmt->iterator_statement);
308 __warn_on_silly_pre_loops();
309 if (is_forever_loop(stmt)) {
310 struct state_list *slist;
312 __save_gotos(loop_name);
314 __push_fake_cur_slist();
315 __split_stmt(stmt->iterator_post_statement);
316 slist = __pop_fake_cur_slist();
318 __discard_continues();
319 __discard_false_states();
320 __use_breaks();
322 if (!__path_is_null())
323 __merge_slist_into_cur(slist);
324 free_slist(&slist);
325 } else {
326 __merge_continues();
327 unchanged = __iterator_unchanged(extra_sm);
328 __split_stmt(stmt->iterator_post_statement);
329 __save_gotos(loop_name);
330 __split_whole_condition(stmt->iterator_pre_condition);
331 nullify_path();
332 __merge_false_states();
333 if (once_through)
334 __discard_false_states();
335 else
336 __merge_false_states();
338 if (extra_sm && unchanged)
339 __extra_pre_loop_hook_after(extra_sm,
340 stmt->iterator_post_statement,
341 stmt->iterator_pre_condition);
342 __merge_breaks();
344 loop_count--;
348 * Post loops are do {} while();
350 static void handle_post_loop(struct statement *stmt)
352 char *loop_name;
354 loop_name = get_loop_name(loop_num);
355 loop_num++;
356 loop_count++;
358 __push_continues();
359 __push_breaks();
360 __merge_gotos(loop_name);
361 __split_stmt(stmt->iterator_statement);
362 __merge_continues();
363 if (!is_zero(stmt->iterator_post_condition))
364 __save_gotos(loop_name);
366 if (is_forever_loop(stmt)) {
367 __use_breaks();
368 } else {
369 __split_whole_condition(stmt->iterator_post_condition);
370 __use_false_states();
371 __merge_breaks();
373 loop_count--;
376 static int empty_statement(struct statement *stmt)
378 if (!stmt)
379 return 0;
380 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
381 return 1;
382 return 0;
385 static int last_stmt_on_same_line()
387 struct statement *stmt;
388 int i = 0;
390 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
391 if (!i++)
392 continue;
393 if (stmt->pos.line == get_lineno())
394 return 1;
395 return 0;
396 } END_FOR_EACH_PTR_REVERSE(stmt);
397 return 0;
400 static struct statement *last_stmt;
401 static int is_last_stmt(struct statement *stmt)
403 if (stmt == last_stmt)
404 return 1;
405 return 0;
408 static void print_unreached_initializers(struct symbol_list *sym_list)
410 struct symbol *sym;
412 FOR_EACH_PTR(sym_list, sym) {
413 if (sym->initializer)
414 sm_msg("info: '%s' is not actually initialized (unreached code).",
415 (sym->ident ? sym->ident->name : "this variable"));
416 } END_FOR_EACH_PTR(sym);
419 static void print_unreached(struct statement *stmt)
421 static int print = 1;
423 if (!__path_is_null()) {
424 print = 1;
425 return;
427 if (!print)
428 return;
430 switch (stmt->type) {
431 case STMT_COMPOUND: /* after a switch before a case stmt */
432 case STMT_RANGE:
433 case STMT_CASE:
434 case STMT_LABEL:
435 return;
436 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
437 print_unreached_initializers(stmt->declaration);
438 return;
439 case STMT_RETURN: /* gcc complains if you don't have a return statement */
440 if (is_last_stmt(stmt))
441 return;
442 break;
443 case STMT_GOTO:
444 if (!option_spammy)
445 return;
446 break;
447 default:
448 break;
450 if (!option_spammy && empty_statement(stmt))
451 return;
452 sm_msg("info: ignoring unreachable code.");
453 print = 0;
456 static void split_asm_constraints(struct expression_list *expr_list)
458 struct expression *expr;
459 int state = 0;
461 FOR_EACH_PTR(expr_list, expr) {
462 switch (state) {
463 case 0: /* identifier */
464 case 1: /* constraint */
465 state++;
466 continue;
467 case 2: /* expression */
468 state = 0;
469 __split_expr(expr);
470 continue;
472 } END_FOR_EACH_PTR(expr);
475 static int is_case_val(struct statement *stmt, sval_t sval)
477 sval_t case_sval;
479 if (stmt->type != STMT_CASE)
480 return 0;
481 if (!stmt->case_expression) {
482 __set_default();
483 return 1;
485 if (!get_value(stmt->case_expression, &case_sval))
486 return 0;
487 if (case_sval.value == sval.value)
488 return 1;
489 return 0;
492 static void split_known_switch(struct statement *stmt, sval_t sval)
494 struct statement *tmp;
496 __split_expr(stmt->switch_expression);
498 push_expression(&switch_expr_stack, stmt->switch_expression);
499 __save_switch_states(top_expression(switch_expr_stack));
500 nullify_path();
501 __push_default();
502 __push_breaks();
504 stmt = stmt->switch_statement;
506 if (!last_stmt)
507 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
509 __push_scope_hooks();
510 FOR_EACH_PTR(stmt->stmts, tmp) {
511 __smatch_lineno = tmp->pos.line;
512 if (is_case_val(tmp, sval)) {
513 __merge_switches(top_expression(switch_expr_stack),
514 stmt->case_expression);
515 __pass_case_to_client(top_expression(switch_expr_stack),
516 stmt->case_expression);
518 if (__path_is_null())
519 continue;
520 __split_stmt(tmp);
521 if (__path_is_null()) {
522 __set_default();
523 goto out;
525 } END_FOR_EACH_PTR(tmp);
526 out:
527 __call_scope_hooks();
528 if (!__pop_default())
529 __merge_switches(top_expression(switch_expr_stack),
530 NULL);
531 __discard_switches();
532 __merge_breaks();
533 pop_expression(&switch_expr_stack);
536 void __split_stmt(struct statement *stmt)
538 sval_t sval;
540 if (!stmt)
541 return;
543 if (out_of_memory() || __bail_on_rest_of_function) {
544 static char *printed = NULL;
546 if (printed != cur_func)
547 sm_msg("Function too hairy. Giving up.");
548 final_pass = 0; /* turn off sm_msg() from here */
549 printed = cur_func;
550 return;
553 add_ptr_list(&big_statement_stack, stmt);
554 free_expression_stack(&big_expression_stack);
555 set_position(stmt->pos);
556 print_unreached(stmt);
557 __pass_to_client(stmt, STMT_HOOK);
559 switch (stmt->type) {
560 case STMT_DECLARATION:
561 split_declaration(stmt->declaration);
562 return;
563 case STMT_RETURN:
564 __split_expr(stmt->ret_value);
565 __pass_to_client(stmt->ret_value, RETURN_HOOK);
566 nullify_path();
567 return;
568 case STMT_EXPRESSION:
569 __split_expr(stmt->expression);
570 return;
571 case STMT_COMPOUND: {
572 struct statement *tmp;
574 if (!last_stmt)
575 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
576 __push_scope_hooks();
577 FOR_EACH_PTR(stmt->stmts, tmp) {
578 __split_stmt(tmp);
579 } END_FOR_EACH_PTR(tmp);
580 __call_scope_hooks();
581 return;
583 case STMT_IF:
584 if (known_condition_true(stmt->if_conditional)) {
585 __split_stmt(stmt->if_true);
586 return;
588 if (known_condition_false(stmt->if_conditional)) {
589 __split_stmt(stmt->if_false);
590 return;
592 if (option_known_conditions &&
593 implied_condition_true(stmt->if_conditional)) {
594 sm_info("this condition is true.");
595 __split_stmt(stmt->if_true);
596 return;
598 if (option_known_conditions &&
599 implied_condition_false(stmt->if_conditional)) {
600 sm_info("this condition is false.");
601 __split_stmt(stmt->if_false);
602 return;
604 __split_whole_condition(stmt->if_conditional);
605 __split_stmt(stmt->if_true);
606 if (empty_statement(stmt->if_true) &&
607 last_stmt_on_same_line() &&
608 !get_macro_name(stmt->if_true->pos))
609 sm_msg("warn: if();");
610 __push_true_states();
611 __use_false_states();
612 __split_stmt(stmt->if_false);
613 __merge_true_states();
614 return;
615 case STMT_ITERATOR:
616 if (stmt->iterator_pre_condition)
617 handle_pre_loop(stmt);
618 else if (stmt->iterator_post_condition)
619 handle_post_loop(stmt);
620 else {
621 // these are for(;;) type loops.
622 handle_pre_loop(stmt);
624 return;
625 case STMT_SWITCH:
626 if (get_value(stmt->switch_expression, &sval)) {
627 split_known_switch(stmt, sval);
628 return;
630 __split_expr(stmt->switch_expression);
631 push_expression(&switch_expr_stack, stmt->switch_expression);
632 __save_switch_states(top_expression(switch_expr_stack));
633 nullify_path();
634 __push_default();
635 __push_breaks();
636 __split_stmt(stmt->switch_statement);
637 if (!__pop_default())
638 __merge_switches(top_expression(switch_expr_stack),
639 NULL);
640 __discard_switches();
641 __merge_breaks();
642 pop_expression(&switch_expr_stack);
643 return;
644 case STMT_CASE:
645 __merge_switches(top_expression(switch_expr_stack),
646 stmt->case_expression);
647 __pass_case_to_client(top_expression(switch_expr_stack),
648 stmt->case_expression);
649 if (!stmt->case_expression)
650 __set_default();
651 __split_expr(stmt->case_expression);
652 __split_expr(stmt->case_to);
653 __split_stmt(stmt->case_statement);
654 return;
655 case STMT_LABEL:
656 if (stmt->label_identifier &&
657 stmt->label_identifier->type == SYM_LABEL &&
658 stmt->label_identifier->ident) {
659 loop_count = 1000000;
660 __merge_gotos(stmt->label_identifier->ident->name);
662 __split_stmt(stmt->label_statement);
663 return;
664 case STMT_GOTO:
665 __split_expr(stmt->goto_expression);
666 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
667 if (!strcmp(stmt->goto_label->ident->name, "break")) {
668 __process_breaks();
669 } else if (!strcmp(stmt->goto_label->ident->name,
670 "continue")) {
671 __process_continues();
673 } else if (stmt->goto_label &&
674 stmt->goto_label->type == SYM_LABEL &&
675 stmt->goto_label->ident) {
676 __save_gotos(stmt->goto_label->ident->name);
678 nullify_path();
679 return;
680 case STMT_NONE:
681 return;
682 case STMT_ASM:
683 __pass_to_client(stmt, ASM_HOOK);
684 __split_expr(stmt->asm_string);
685 split_asm_constraints(stmt->asm_outputs);
686 split_asm_constraints(stmt->asm_inputs);
687 split_asm_constraints(stmt->asm_clobbers);
688 return;
689 case STMT_CONTEXT:
690 return;
691 case STMT_RANGE:
692 __split_expr(stmt->range_expression);
693 __split_expr(stmt->range_low);
694 __split_expr(stmt->range_high);
695 return;
699 static void split_expr_list(struct expression_list *expr_list)
701 struct expression *expr;
703 FOR_EACH_PTR(expr_list, expr) {
704 __split_expr(expr);
705 } END_FOR_EACH_PTR(expr);
708 static void split_sym(struct symbol *sym)
710 if (!sym)
711 return;
712 if (!(sym->namespace & NS_SYMBOL))
713 return;
715 __split_stmt(sym->stmt);
716 __split_expr(sym->array_size);
717 split_symlist(sym->arguments);
718 split_symlist(sym->symbol_list);
719 __split_stmt(sym->inline_stmt);
720 split_symlist(sym->inline_symbol_list);
723 static void split_symlist(struct symbol_list *sym_list)
725 struct symbol *sym;
727 FOR_EACH_PTR(sym_list, sym) {
728 split_sym(sym);
729 } END_FOR_EACH_PTR(sym);
732 static void fake_member_assigns(struct symbol *sym)
734 struct expression *symbol, *deref, *assign, *tmp;
736 symbol = symbol_expression(sym);
737 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
738 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
739 continue;
740 deref = deref_expression(symbol, '.', tmp->expr_ident);
741 assign = assign_expression(deref, tmp->ident_expression);
742 __split_expr(assign);
743 } END_FOR_EACH_PTR(tmp);
746 static void fake_assign_expr(struct symbol *sym)
748 struct expression *assign, *symbol;
750 symbol = symbol_expression(sym);
751 assign = assign_expression(symbol, sym->initializer);
752 __split_expr(assign);
755 static void do_initializer_stuff(struct symbol *sym)
757 if (!sym->initializer)
758 return;
759 if (sym->initializer->type == EXPR_INITIALIZER)
760 fake_member_assigns(sym);
761 else
762 fake_assign_expr(sym);
765 static void split_declaration(struct symbol_list *sym_list)
767 struct symbol *sym;
769 FOR_EACH_PTR(sym_list, sym) {
770 __pass_to_client(sym, DECLARATION_HOOK);
771 do_initializer_stuff(sym);
772 split_sym(sym);
773 } END_FOR_EACH_PTR(sym);
776 static void fake_global_assign(struct symbol *sym)
778 struct expression *assign, *symbol;
780 if (!sym->initializer)
781 return;
782 if (sym->initializer->type == EXPR_INITIALIZER) {
783 struct expression *deref, *tmp;
785 symbol = symbol_expression(sym);
786 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
787 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
788 continue;
789 deref = deref_expression(symbol, '.', tmp->expr_ident);
790 assign = assign_expression(deref, tmp->ident_expression);
791 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
792 } END_FOR_EACH_PTR(tmp);
793 } else {
794 symbol = symbol_expression(sym);
795 assign = assign_expression(symbol, sym->initializer);
796 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
800 static void split_function(struct symbol *sym)
802 struct symbol *base_type = get_base_type(sym);
804 cur_func_sym = sym;
805 if (base_type->stmt)
806 line_func_start = base_type->stmt->pos.line;
807 if (sym->ident)
808 cur_func = sym->ident->name;
809 __smatch_lineno = sym->pos.line;
810 last_stmt = NULL;
811 loop_count = 0;
812 sm_debug("new function: %s\n", cur_func);
813 __slist_id = 0;
814 if (option_two_passes) {
815 __unnullify_path();
816 loop_num = 0;
817 final_pass = 0;
818 __pass_to_client(sym, FUNC_DEF_HOOK);
819 __split_stmt(base_type->stmt);
820 __split_stmt(base_type->inline_stmt);
821 nullify_path();
823 __unnullify_path();
824 loop_num = 0;
825 final_pass = 1;
826 __pass_to_client(sym, FUNC_DEF_HOOK);
827 __split_stmt(base_type->stmt);
828 __split_stmt(base_type->inline_stmt);
829 __pass_to_client(sym, END_FUNC_HOOK);
830 cur_func = NULL;
831 line_func_start = 0;
832 clear_all_states();
833 free_data_info_allocs();
834 free_expression_stack(&switch_expr_stack);
835 __free_ptr_list((struct ptr_list **)&big_statement_stack);
836 __bail_on_rest_of_function = 0;
839 static struct symbol_list *inlines_called;
840 static void add_inline_function(struct symbol *sym)
842 static struct symbol_list *already_added;
843 struct symbol *tmp;
845 FOR_EACH_PTR(already_added, tmp) {
846 if (tmp == sym)
847 return;
848 } END_FOR_EACH_PTR(tmp);
850 add_ptr_list(&already_added, sym);
851 add_ptr_list(&inlines_called, sym);
854 static void process_inlines()
856 struct symbol *tmp;
858 FOR_EACH_PTR(inlines_called, tmp) {
859 split_function(tmp);
860 } END_FOR_EACH_PTR(tmp);
861 free_ptr_list(&inlines_called);
864 static void split_functions(struct symbol_list *sym_list)
866 struct symbol *sym;
868 FOR_EACH_PTR(sym_list, sym) {
869 set_position(sym->pos);
870 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
871 split_function(sym);
872 process_inlines();
873 } else {
874 __pass_to_client(sym, BASE_HOOK);
875 fake_global_assign(sym);
877 } END_FOR_EACH_PTR(sym);
878 __pass_to_client_no_data(END_FILE_HOOK);
881 void smatch(int argc, char **argv)
884 struct string_list *filelist = NULL;
885 struct symbol_list *sym_list;
887 if (argc < 2) {
888 printf("Usage: smatch [--debug] <filename.c>\n");
889 exit(1);
891 sparse_initialize(argc, argv, &filelist);
892 FOR_EACH_PTR_NOTAG(filelist, base_file) {
893 if (option_file_output) {
894 char buf[256];
896 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
897 sm_outfd = fopen(buf, "w");
898 if (!sm_outfd) {
899 printf("Error: Cannot open %s\n", base_file);
900 exit(1);
903 sym_list = sparse_keep_tokens(base_file);
904 split_functions(sym_list);
905 } END_FOR_EACH_PTR_NOTAG(base_file);