hooks: add a SIZEOF_HOOK
[smatch.git] / smatch_flow.c
blobec811a6848b2232fba349ba27079d8c9424b15c9
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 if (expr->cast_expression)
181 __pass_to_client(strip_parens(expr->cast_expression),
182 SIZEOF_HOOK);
183 break;
184 case EXPR_OFFSETOF:
185 case EXPR_ALIGNOF:
186 evaluate_expression(expr);
187 break;
188 case EXPR_CONDITIONAL:
189 case EXPR_SELECT:
190 if (known_condition_true(expr->conditional)) {
191 __split_expr(expr->cond_true);
192 break;
194 if (known_condition_false(expr->conditional)) {
195 __split_expr(expr->cond_false);
196 break;
198 __pass_to_client(expr, SELECT_HOOK);
199 __split_whole_condition(expr->conditional);
200 __split_expr(expr->cond_true);
201 __push_true_states();
202 __use_false_states();
203 __split_expr(expr->cond_false);
204 __merge_true_states();
205 break;
206 case EXPR_CALL:
207 if (sym_name_is("__builtin_constant_p", expr->fn))
208 break;
209 split_expr_list(expr->args);
210 __split_expr(expr->fn);
211 if (is_inline_func(expr->fn))
212 add_inline_function(expr->fn->symbol);
213 __pass_to_client(expr, FUNCTION_CALL_HOOK);
214 if (is_noreturn_func(expr->fn))
215 nullify_path();
216 break;
217 case EXPR_INITIALIZER:
218 split_expr_list(expr->expr_list);
219 break;
220 case EXPR_IDENTIFIER:
221 __split_expr(expr->ident_expression);
222 break;
223 case EXPR_INDEX:
224 __split_expr(expr->idx_expression);
225 break;
226 case EXPR_POS:
227 __split_expr(expr->init_expr);
228 break;
229 case EXPR_SYMBOL:
230 __pass_to_client(expr, SYM_HOOK);
231 break;
232 case EXPR_STRING:
233 __pass_to_client(expr, STRING_HOOK);
234 break;
235 default:
236 break;
238 pop_expression(&big_expression_stack);
241 static int is_forever_loop(struct statement *stmt)
243 struct expression *expr;
245 expr = strip_expr(stmt->iterator_pre_condition);
246 if (!expr)
247 expr = stmt->iterator_post_condition;
248 if (!expr) {
249 /* this is a for(;;) loop... */
250 return 1;
253 if (expr->type == EXPR_VALUE && expr->value == 1)
254 return 1;
256 return 0;
259 static int loop_num;
260 static char *get_loop_name(int num)
262 char buf[256];
264 snprintf(buf, 255, "-loop%d", num);
265 buf[255] = '\0';
266 return alloc_sname(buf);
270 * Pre Loops are while and for loops.
272 static void handle_pre_loop(struct statement *stmt)
274 int once_through; /* we go through the loop at least once */
275 struct sm_state *extra_sm = NULL;
276 int unchanged = 0;
277 char *loop_name;
278 struct state_list *slist = NULL;
279 struct sm_state *sm = NULL;
281 loop_name = get_loop_name(loop_num);
282 loop_num++;
284 __split_stmt(stmt->iterator_pre_statement);
286 once_through = implied_condition_true(stmt->iterator_pre_condition);
288 loop_count++;
289 __push_continues();
290 __push_breaks();
292 __merge_gotos(loop_name);
294 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
295 __in_pre_condition++;
296 __pass_to_client(stmt, PRELOOP_HOOK);
297 __split_whole_condition(stmt->iterator_pre_condition);
298 __in_pre_condition--;
299 FOR_EACH_PTR(slist, sm) {
300 set_state(sm->owner, sm->name, sm->sym, sm->state);
301 } END_FOR_EACH_PTR(sm);
302 free_slist(&slist);
303 if (extra_sm)
304 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
306 if (option_assume_loops)
307 once_through = 1;
309 __split_stmt(stmt->iterator_statement);
310 __warn_on_silly_pre_loops();
311 if (is_forever_loop(stmt)) {
312 struct state_list *slist;
314 __save_gotos(loop_name);
316 __push_fake_cur_slist();
317 __split_stmt(stmt->iterator_post_statement);
318 slist = __pop_fake_cur_slist();
320 __discard_continues();
321 __discard_false_states();
322 __use_breaks();
324 if (!__path_is_null())
325 __merge_slist_into_cur(slist);
326 free_slist(&slist);
327 } else {
328 __merge_continues();
329 unchanged = __iterator_unchanged(extra_sm);
330 __split_stmt(stmt->iterator_post_statement);
331 __save_gotos(loop_name);
332 __split_whole_condition(stmt->iterator_pre_condition);
333 nullify_path();
334 __merge_false_states();
335 if (once_through)
336 __discard_false_states();
337 else
338 __merge_false_states();
340 if (extra_sm && unchanged)
341 __extra_pre_loop_hook_after(extra_sm,
342 stmt->iterator_post_statement,
343 stmt->iterator_pre_condition);
344 __merge_breaks();
346 loop_count--;
350 * Post loops are do {} while();
352 static void handle_post_loop(struct statement *stmt)
354 char *loop_name;
356 loop_name = get_loop_name(loop_num);
357 loop_num++;
358 loop_count++;
360 __push_continues();
361 __push_breaks();
362 __merge_gotos(loop_name);
363 __split_stmt(stmt->iterator_statement);
364 __merge_continues();
365 if (!is_zero(stmt->iterator_post_condition))
366 __save_gotos(loop_name);
368 if (is_forever_loop(stmt)) {
369 __use_breaks();
370 } else {
371 __split_whole_condition(stmt->iterator_post_condition);
372 __use_false_states();
373 __merge_breaks();
375 loop_count--;
378 static int empty_statement(struct statement *stmt)
380 if (!stmt)
381 return 0;
382 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
383 return 1;
384 return 0;
387 static int last_stmt_on_same_line()
389 struct statement *stmt;
390 int i = 0;
392 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
393 if (!i++)
394 continue;
395 if (stmt->pos.line == get_lineno())
396 return 1;
397 return 0;
398 } END_FOR_EACH_PTR_REVERSE(stmt);
399 return 0;
402 static struct statement *last_stmt;
403 static int is_last_stmt(struct statement *stmt)
405 if (stmt == last_stmt)
406 return 1;
407 return 0;
410 static void print_unreached_initializers(struct symbol_list *sym_list)
412 struct symbol *sym;
414 FOR_EACH_PTR(sym_list, sym) {
415 if (sym->initializer)
416 sm_msg("info: '%s' is not actually initialized (unreached code).",
417 (sym->ident ? sym->ident->name : "this variable"));
418 } END_FOR_EACH_PTR(sym);
421 static void print_unreached(struct statement *stmt)
423 static int print = 1;
425 if (!__path_is_null()) {
426 print = 1;
427 return;
429 if (!print)
430 return;
432 switch (stmt->type) {
433 case STMT_COMPOUND: /* after a switch before a case stmt */
434 case STMT_RANGE:
435 case STMT_CASE:
436 case STMT_LABEL:
437 return;
438 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
439 print_unreached_initializers(stmt->declaration);
440 return;
441 case STMT_RETURN: /* gcc complains if you don't have a return statement */
442 if (is_last_stmt(stmt))
443 return;
444 break;
445 case STMT_GOTO:
446 if (!option_spammy)
447 return;
448 break;
449 default:
450 break;
452 if (!option_spammy && empty_statement(stmt))
453 return;
454 sm_msg("info: ignoring unreachable code.");
455 print = 0;
458 static void split_asm_constraints(struct expression_list *expr_list)
460 struct expression *expr;
461 int state = 0;
463 FOR_EACH_PTR(expr_list, expr) {
464 switch (state) {
465 case 0: /* identifier */
466 case 1: /* constraint */
467 state++;
468 continue;
469 case 2: /* expression */
470 state = 0;
471 __split_expr(expr);
472 continue;
474 } END_FOR_EACH_PTR(expr);
477 static int is_case_val(struct statement *stmt, sval_t sval)
479 sval_t case_sval;
481 if (stmt->type != STMT_CASE)
482 return 0;
483 if (!stmt->case_expression) {
484 __set_default();
485 return 1;
487 if (!get_value(stmt->case_expression, &case_sval))
488 return 0;
489 if (case_sval.value == sval.value)
490 return 1;
491 return 0;
494 static void split_known_switch(struct statement *stmt, sval_t sval)
496 struct statement *tmp;
498 __split_expr(stmt->switch_expression);
500 push_expression(&switch_expr_stack, stmt->switch_expression);
501 __save_switch_states(top_expression(switch_expr_stack));
502 nullify_path();
503 __push_default();
504 __push_breaks();
506 stmt = stmt->switch_statement;
508 if (!last_stmt)
509 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
511 __push_scope_hooks();
512 FOR_EACH_PTR(stmt->stmts, tmp) {
513 __smatch_lineno = tmp->pos.line;
514 if (is_case_val(tmp, sval)) {
515 __merge_switches(top_expression(switch_expr_stack),
516 stmt->case_expression);
517 __pass_case_to_client(top_expression(switch_expr_stack),
518 stmt->case_expression);
520 if (__path_is_null())
521 continue;
522 __split_stmt(tmp);
523 if (__path_is_null()) {
524 __set_default();
525 goto out;
527 } END_FOR_EACH_PTR(tmp);
528 out:
529 __call_scope_hooks();
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);
538 void __split_stmt(struct statement *stmt)
540 sval_t sval;
542 if (!stmt)
543 return;
545 if (out_of_memory() || __bail_on_rest_of_function) {
546 static char *printed = NULL;
548 if (printed != cur_func)
549 sm_msg("Function too hairy. Giving up.");
550 final_pass = 0; /* turn off sm_msg() from here */
551 printed = cur_func;
552 return;
555 add_ptr_list(&big_statement_stack, stmt);
556 free_expression_stack(&big_expression_stack);
557 set_position(stmt->pos);
558 print_unreached(stmt);
559 __pass_to_client(stmt, STMT_HOOK);
561 switch (stmt->type) {
562 case STMT_DECLARATION:
563 split_declaration(stmt->declaration);
564 return;
565 case STMT_RETURN:
566 __split_expr(stmt->ret_value);
567 __pass_to_client(stmt->ret_value, RETURN_HOOK);
568 nullify_path();
569 return;
570 case STMT_EXPRESSION:
571 __split_expr(stmt->expression);
572 return;
573 case STMT_COMPOUND: {
574 struct statement *tmp;
576 if (!last_stmt)
577 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
578 __push_scope_hooks();
579 FOR_EACH_PTR(stmt->stmts, tmp) {
580 __split_stmt(tmp);
581 } END_FOR_EACH_PTR(tmp);
582 __call_scope_hooks();
583 return;
585 case STMT_IF:
586 if (known_condition_true(stmt->if_conditional)) {
587 __split_stmt(stmt->if_true);
588 return;
590 if (known_condition_false(stmt->if_conditional)) {
591 __split_stmt(stmt->if_false);
592 return;
594 if (option_known_conditions &&
595 implied_condition_true(stmt->if_conditional)) {
596 sm_info("this condition is true.");
597 __split_stmt(stmt->if_true);
598 return;
600 if (option_known_conditions &&
601 implied_condition_false(stmt->if_conditional)) {
602 sm_info("this condition is false.");
603 __split_stmt(stmt->if_false);
604 return;
606 __split_whole_condition(stmt->if_conditional);
607 __split_stmt(stmt->if_true);
608 if (empty_statement(stmt->if_true) &&
609 last_stmt_on_same_line() &&
610 !get_macro_name(stmt->if_true->pos))
611 sm_msg("warn: if();");
612 __push_true_states();
613 __use_false_states();
614 __split_stmt(stmt->if_false);
615 __merge_true_states();
616 return;
617 case STMT_ITERATOR:
618 if (stmt->iterator_pre_condition)
619 handle_pre_loop(stmt);
620 else if (stmt->iterator_post_condition)
621 handle_post_loop(stmt);
622 else {
623 // these are for(;;) type loops.
624 handle_pre_loop(stmt);
626 return;
627 case STMT_SWITCH:
628 if (get_value(stmt->switch_expression, &sval)) {
629 split_known_switch(stmt, sval);
630 return;
632 __split_expr(stmt->switch_expression);
633 push_expression(&switch_expr_stack, stmt->switch_expression);
634 __save_switch_states(top_expression(switch_expr_stack));
635 nullify_path();
636 __push_default();
637 __push_breaks();
638 __split_stmt(stmt->switch_statement);
639 if (!__pop_default())
640 __merge_switches(top_expression(switch_expr_stack),
641 NULL);
642 __discard_switches();
643 __merge_breaks();
644 pop_expression(&switch_expr_stack);
645 return;
646 case STMT_CASE:
647 __merge_switches(top_expression(switch_expr_stack),
648 stmt->case_expression);
649 __pass_case_to_client(top_expression(switch_expr_stack),
650 stmt->case_expression);
651 if (!stmt->case_expression)
652 __set_default();
653 __split_expr(stmt->case_expression);
654 __split_expr(stmt->case_to);
655 __split_stmt(stmt->case_statement);
656 return;
657 case STMT_LABEL:
658 if (stmt->label_identifier &&
659 stmt->label_identifier->type == SYM_LABEL &&
660 stmt->label_identifier->ident) {
661 loop_count = 1000000;
662 __merge_gotos(stmt->label_identifier->ident->name);
664 __split_stmt(stmt->label_statement);
665 return;
666 case STMT_GOTO:
667 __split_expr(stmt->goto_expression);
668 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
669 if (!strcmp(stmt->goto_label->ident->name, "break")) {
670 __process_breaks();
671 } else if (!strcmp(stmt->goto_label->ident->name,
672 "continue")) {
673 __process_continues();
675 } else if (stmt->goto_label &&
676 stmt->goto_label->type == SYM_LABEL &&
677 stmt->goto_label->ident) {
678 __save_gotos(stmt->goto_label->ident->name);
680 nullify_path();
681 return;
682 case STMT_NONE:
683 return;
684 case STMT_ASM:
685 __pass_to_client(stmt, ASM_HOOK);
686 __split_expr(stmt->asm_string);
687 split_asm_constraints(stmt->asm_outputs);
688 split_asm_constraints(stmt->asm_inputs);
689 split_asm_constraints(stmt->asm_clobbers);
690 return;
691 case STMT_CONTEXT:
692 return;
693 case STMT_RANGE:
694 __split_expr(stmt->range_expression);
695 __split_expr(stmt->range_low);
696 __split_expr(stmt->range_high);
697 return;
701 static void split_expr_list(struct expression_list *expr_list)
703 struct expression *expr;
705 FOR_EACH_PTR(expr_list, expr) {
706 __split_expr(expr);
707 } END_FOR_EACH_PTR(expr);
710 static void split_sym(struct symbol *sym)
712 if (!sym)
713 return;
714 if (!(sym->namespace & NS_SYMBOL))
715 return;
717 __split_stmt(sym->stmt);
718 __split_expr(sym->array_size);
719 split_symlist(sym->arguments);
720 split_symlist(sym->symbol_list);
721 __split_stmt(sym->inline_stmt);
722 split_symlist(sym->inline_symbol_list);
725 static void split_symlist(struct symbol_list *sym_list)
727 struct symbol *sym;
729 FOR_EACH_PTR(sym_list, sym) {
730 split_sym(sym);
731 } END_FOR_EACH_PTR(sym);
734 static void fake_member_assigns(struct symbol *sym)
736 struct expression *symbol, *deref, *assign, *tmp;
738 symbol = symbol_expression(sym);
739 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
740 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
741 continue;
742 deref = deref_expression(symbol, '.', tmp->expr_ident);
743 assign = assign_expression(deref, tmp->ident_expression);
744 __split_expr(assign);
745 } END_FOR_EACH_PTR(tmp);
748 static void fake_assign_expr(struct symbol *sym)
750 struct expression *assign, *symbol;
752 symbol = symbol_expression(sym);
753 assign = assign_expression(symbol, sym->initializer);
754 __split_expr(assign);
757 static void do_initializer_stuff(struct symbol *sym)
759 if (!sym->initializer)
760 return;
761 if (sym->initializer->type == EXPR_INITIALIZER)
762 fake_member_assigns(sym);
763 else
764 fake_assign_expr(sym);
767 static void split_declaration(struct symbol_list *sym_list)
769 struct symbol *sym;
771 FOR_EACH_PTR(sym_list, sym) {
772 __pass_to_client(sym, DECLARATION_HOOK);
773 do_initializer_stuff(sym);
774 split_sym(sym);
775 } END_FOR_EACH_PTR(sym);
778 static void fake_global_assign(struct symbol *sym)
780 struct expression *assign, *symbol;
782 if (!sym->initializer)
783 return;
784 if (sym->initializer->type == EXPR_INITIALIZER) {
785 struct expression *deref, *tmp;
787 symbol = symbol_expression(sym);
788 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
789 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
790 continue;
791 deref = deref_expression(symbol, '.', tmp->expr_ident);
792 assign = assign_expression(deref, tmp->ident_expression);
793 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
794 } END_FOR_EACH_PTR(tmp);
795 } else {
796 symbol = symbol_expression(sym);
797 assign = assign_expression(symbol, sym->initializer);
798 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
802 static void split_function(struct symbol *sym)
804 struct symbol *base_type = get_base_type(sym);
806 cur_func_sym = sym;
807 if (base_type->stmt)
808 line_func_start = base_type->stmt->pos.line;
809 if (sym->ident)
810 cur_func = sym->ident->name;
811 __smatch_lineno = sym->pos.line;
812 last_stmt = NULL;
813 loop_count = 0;
814 sm_debug("new function: %s\n", cur_func);
815 __slist_id = 0;
816 if (option_two_passes) {
817 __unnullify_path();
818 loop_num = 0;
819 final_pass = 0;
820 __pass_to_client(sym, FUNC_DEF_HOOK);
821 __split_stmt(base_type->stmt);
822 __split_stmt(base_type->inline_stmt);
823 nullify_path();
825 __unnullify_path();
826 loop_num = 0;
827 final_pass = 1;
828 __pass_to_client(sym, FUNC_DEF_HOOK);
829 __split_stmt(base_type->stmt);
830 __split_stmt(base_type->inline_stmt);
831 __pass_to_client(sym, END_FUNC_HOOK);
832 cur_func = NULL;
833 line_func_start = 0;
834 clear_all_states();
835 free_data_info_allocs();
836 free_expression_stack(&switch_expr_stack);
837 __free_ptr_list((struct ptr_list **)&big_statement_stack);
838 __bail_on_rest_of_function = 0;
841 static struct symbol_list *inlines_called;
842 static void add_inline_function(struct symbol *sym)
844 static struct symbol_list *already_added;
845 struct symbol *tmp;
847 FOR_EACH_PTR(already_added, tmp) {
848 if (tmp == sym)
849 return;
850 } END_FOR_EACH_PTR(tmp);
852 add_ptr_list(&already_added, sym);
853 add_ptr_list(&inlines_called, sym);
856 static void process_inlines()
858 struct symbol *tmp;
860 FOR_EACH_PTR(inlines_called, tmp) {
861 split_function(tmp);
862 } END_FOR_EACH_PTR(tmp);
863 free_ptr_list(&inlines_called);
866 static void split_functions(struct symbol_list *sym_list)
868 struct symbol *sym;
870 FOR_EACH_PTR(sym_list, sym) {
871 set_position(sym->pos);
872 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
873 split_function(sym);
874 process_inlines();
875 } else {
876 __pass_to_client(sym, BASE_HOOK);
877 fake_global_assign(sym);
879 } END_FOR_EACH_PTR(sym);
880 __pass_to_client_no_data(END_FILE_HOOK);
883 void smatch(int argc, char **argv)
886 struct string_list *filelist = NULL;
887 struct symbol_list *sym_list;
889 if (argc < 2) {
890 printf("Usage: smatch [--debug] <filename.c>\n");
891 exit(1);
893 sparse_initialize(argc, argv, &filelist);
894 FOR_EACH_PTR_NOTAG(filelist, base_file) {
895 if (option_file_output) {
896 char buf[256];
898 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
899 sm_outfd = fopen(buf, "w");
900 if (!sm_outfd) {
901 printf("Error: Cannot open %s\n", base_file);
902 exit(1);
905 sym_list = sparse_keep_tokens(base_file);
906 split_functions(sym_list);
907 } END_FOR_EACH_PTR_NOTAG(base_file);