flow: save and restore the big_expression_stack when processing inlines
[smatch.git] / smatch_flow.c
blob401e599d0d60138bf7fb17a224b29ceed310dac0
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;
20 int __inline_call;
21 struct expression *__inline_fn;
23 static int __smatch_lineno = 0;
25 static char *base_file;
26 static const char *filename;
27 static char *pathname;
28 static char *full_filename;
29 static char *cur_func;
30 static int loop_count;
31 int __expr_stmt_count;
32 static struct expression_list *switch_expr_stack = NULL;
34 struct expression_list *big_expression_stack;
35 struct statement_list *big_statement_stack;
36 int __in_pre_condition = 0;
37 int __bail_on_rest_of_function = 0;
38 char *get_function(void) { return cur_func; }
39 int get_lineno(void) { return __smatch_lineno; }
40 int inside_loop(void) { return !!loop_count; }
41 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
42 int in_expression_statement(void) { return !!__expr_stmt_count; }
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);
47 static void add_inline_function(struct symbol *sym);
48 static void parse_inline(struct expression *expr);
50 int option_assume_loops = 0;
51 int option_known_conditions = 0;
52 int option_two_passes = 0;
53 struct symbol *cur_func_sym = NULL;
55 const char *get_filename(void)
57 if (option_info)
58 return base_file;
59 if (option_full_path)
60 return full_filename;
61 return filename;
64 static void set_position(struct position pos)
66 int len;
67 static int prev_stream = -1;
69 __smatch_lineno = pos.line;
71 if (pos.stream == prev_stream)
72 return;
74 filename = stream_name(pos.stream);
76 free(full_filename);
77 pathname = getcwd(NULL, 0);
78 if (pathname) {
79 len = strlen(pathname) + 1 + strlen(filename) + 1;
80 full_filename = malloc(len);
81 snprintf(full_filename, len, "%s/%s", pathname, filename);
82 } else {
83 full_filename = alloc_string(filename);
85 free(pathname);
88 static int is_inline_func(struct expression *expr)
90 if (expr->type != EXPR_SYMBOL || !expr->symbol)
91 return 0;
92 if (expr->symbol->ctype.modifiers & MOD_INLINE)
93 return 1;
94 return 0;
97 static int is_noreturn_func(struct expression *expr)
99 if (expr->type != EXPR_SYMBOL || !expr->symbol)
100 return 0;
101 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
102 return 1;
103 return 0;
106 int inlinable(struct expression *expr)
108 struct symbol *sym;
110 if (__inline_fn) /* don't nest */
111 return 0;
113 if (expr->type != EXPR_SYMBOL || !expr->symbol)
114 return 0;
115 sym = get_base_type(expr->symbol);
116 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
117 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
118 return 1;
119 return 0;
121 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
122 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
123 return 1;
124 return 0;
126 return 0;
129 void __split_expr(struct expression *expr)
131 if (!expr)
132 return;
134 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
136 push_expression(&big_expression_stack, expr);
137 set_position(expr->pos);
138 __pass_to_client(expr, EXPR_HOOK);
140 switch (expr->type) {
141 case EXPR_PREOP:
142 if (expr->op == '*')
143 __pass_to_client(expr, DEREF_HOOK);
144 case EXPR_POSTOP:
145 __pass_to_client(expr, OP_HOOK);
146 __split_expr(expr->unop);
147 break;
148 case EXPR_STATEMENT:
149 __expr_stmt_count++;
150 __split_stmt(expr->statement);
151 __expr_stmt_count--;
152 break;
153 case EXPR_LOGICAL:
154 case EXPR_COMPARE:
155 __pass_to_client(expr, LOGIC_HOOK);
156 __handle_logic(expr);
157 break;
158 case EXPR_BINOP:
159 __pass_to_client(expr, BINOP_HOOK);
160 case EXPR_COMMA:
161 __split_expr(expr->left);
162 __split_expr(expr->right);
163 break;
164 case EXPR_ASSIGNMENT: {
165 struct expression *tmp;
167 if (!expr->right)
168 break;
170 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
172 /* foo = !bar() */
173 if (__handle_condition_assigns(expr))
174 break;
175 /* foo = (x < 5 ? foo : 5); */
176 if (__handle_select_assigns(expr))
177 break;
178 /* foo = ({frob(); frob(); frob(); 1;}) */
179 if (__handle_expr_statement_assigns(expr))
180 break;
182 __split_expr(expr->right);
183 __pass_to_client(expr, ASSIGNMENT_HOOK);
184 tmp = strip_expr(expr->right);
185 if (tmp->type == EXPR_CALL)
186 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
187 if (get_macro_name(tmp->pos) &&
188 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
189 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
190 __split_expr(expr->left);
191 break;
193 case EXPR_DEREF:
194 __pass_to_client(expr, DEREF_HOOK);
195 __split_expr(expr->deref);
196 break;
197 case EXPR_SLICE:
198 __split_expr(expr->base);
199 break;
200 case EXPR_CAST:
201 case EXPR_FORCE_CAST:
202 __pass_to_client(expr, CAST_HOOK);
203 __split_expr(expr->cast_expression);
204 break;
205 case EXPR_SIZEOF:
206 if (expr->cast_expression)
207 __pass_to_client(strip_parens(expr->cast_expression),
208 SIZEOF_HOOK);
209 break;
210 case EXPR_OFFSETOF:
211 case EXPR_ALIGNOF:
212 evaluate_expression(expr);
213 break;
214 case EXPR_CONDITIONAL:
215 case EXPR_SELECT:
216 if (known_condition_true(expr->conditional)) {
217 __split_expr(expr->cond_true);
218 break;
220 if (known_condition_false(expr->conditional)) {
221 __split_expr(expr->cond_false);
222 break;
224 __pass_to_client(expr, SELECT_HOOK);
225 __split_whole_condition(expr->conditional);
226 __split_expr(expr->cond_true);
227 __push_true_states();
228 __use_false_states();
229 __split_expr(expr->cond_false);
230 __merge_true_states();
231 break;
232 case EXPR_CALL:
233 if (sym_name_is("__builtin_constant_p", expr->fn))
234 break;
235 split_expr_list(expr->args);
236 __split_expr(expr->fn);
237 if (is_inline_func(expr->fn))
238 add_inline_function(expr->fn->symbol);
239 if (inlinable(expr->fn))
240 __inline_call = 1;
241 __pass_to_client(expr, FUNCTION_CALL_HOOK);
242 __inline_call = 0;
243 if (inlinable(expr->fn)) {
244 parse_inline(expr);
246 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
247 if (is_noreturn_func(expr->fn))
248 nullify_path();
249 break;
250 case EXPR_INITIALIZER:
251 split_expr_list(expr->expr_list);
252 break;
253 case EXPR_IDENTIFIER:
254 __split_expr(expr->ident_expression);
255 break;
256 case EXPR_INDEX:
257 __split_expr(expr->idx_expression);
258 break;
259 case EXPR_POS:
260 __split_expr(expr->init_expr);
261 break;
262 case EXPR_SYMBOL:
263 __pass_to_client(expr, SYM_HOOK);
264 break;
265 case EXPR_STRING:
266 __pass_to_client(expr, STRING_HOOK);
267 break;
268 default:
269 break;
271 pop_expression(&big_expression_stack);
274 static int is_forever_loop(struct statement *stmt)
276 struct expression *expr;
278 expr = strip_expr(stmt->iterator_pre_condition);
279 if (!expr)
280 expr = stmt->iterator_post_condition;
281 if (!expr) {
282 /* this is a for(;;) loop... */
283 return 1;
286 if (expr->type == EXPR_VALUE && expr->value == 1)
287 return 1;
289 return 0;
292 static int loop_num;
293 static char *get_loop_name(int num)
295 char buf[256];
297 snprintf(buf, 255, "-loop%d", num);
298 buf[255] = '\0';
299 return alloc_sname(buf);
303 * Pre Loops are while and for loops.
305 static void handle_pre_loop(struct statement *stmt)
307 int once_through; /* we go through the loop at least once */
308 struct sm_state *extra_sm = NULL;
309 int unchanged = 0;
310 char *loop_name;
311 struct state_list *slist = NULL;
312 struct sm_state *sm = NULL;
314 loop_name = get_loop_name(loop_num);
315 loop_num++;
317 __split_stmt(stmt->iterator_pre_statement);
319 once_through = implied_condition_true(stmt->iterator_pre_condition);
321 loop_count++;
322 __push_continues();
323 __push_breaks();
325 __merge_gotos(loop_name);
327 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
328 __in_pre_condition++;
329 __pass_to_client(stmt, PRELOOP_HOOK);
330 __split_whole_condition(stmt->iterator_pre_condition);
331 __in_pre_condition--;
332 FOR_EACH_PTR(slist, sm) {
333 set_state(sm->owner, sm->name, sm->sym, sm->state);
334 } END_FOR_EACH_PTR(sm);
335 free_slist(&slist);
336 if (extra_sm)
337 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
339 if (option_assume_loops)
340 once_through = 1;
342 __split_stmt(stmt->iterator_statement);
343 __warn_on_silly_pre_loops();
344 if (is_forever_loop(stmt)) {
345 struct state_list *slist;
347 __save_gotos(loop_name);
349 __push_fake_cur_slist();
350 __split_stmt(stmt->iterator_post_statement);
351 slist = __pop_fake_cur_slist();
353 __discard_continues();
354 __discard_false_states();
355 __use_breaks();
357 if (!__path_is_null())
358 __merge_slist_into_cur(slist);
359 free_slist(&slist);
360 } else {
361 __merge_continues();
362 unchanged = __iterator_unchanged(extra_sm);
363 __split_stmt(stmt->iterator_post_statement);
364 __save_gotos(loop_name);
365 __split_whole_condition(stmt->iterator_pre_condition);
366 nullify_path();
367 __merge_false_states();
368 if (once_through)
369 __discard_false_states();
370 else
371 __merge_false_states();
373 if (extra_sm && unchanged)
374 __extra_pre_loop_hook_after(extra_sm,
375 stmt->iterator_post_statement,
376 stmt->iterator_pre_condition);
377 __merge_breaks();
379 loop_count--;
383 * Post loops are do {} while();
385 static void handle_post_loop(struct statement *stmt)
387 char *loop_name;
389 loop_name = get_loop_name(loop_num);
390 loop_num++;
391 loop_count++;
393 __push_continues();
394 __push_breaks();
395 __merge_gotos(loop_name);
396 __split_stmt(stmt->iterator_statement);
397 __merge_continues();
398 if (!is_zero(stmt->iterator_post_condition))
399 __save_gotos(loop_name);
401 if (is_forever_loop(stmt)) {
402 __use_breaks();
403 } else {
404 __split_whole_condition(stmt->iterator_post_condition);
405 __use_false_states();
406 __merge_breaks();
408 loop_count--;
411 static int empty_statement(struct statement *stmt)
413 if (!stmt)
414 return 0;
415 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
416 return 1;
417 return 0;
420 static int last_stmt_on_same_line()
422 struct statement *stmt;
423 int i = 0;
425 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
426 if (!i++)
427 continue;
428 if (stmt->pos.line == get_lineno())
429 return 1;
430 return 0;
431 } END_FOR_EACH_PTR_REVERSE(stmt);
432 return 0;
435 static struct statement *last_stmt;
436 static int is_last_stmt(struct statement *stmt)
438 if (stmt == last_stmt)
439 return 1;
440 return 0;
443 static void print_unreached_initializers(struct symbol_list *sym_list)
445 struct symbol *sym;
447 FOR_EACH_PTR(sym_list, sym) {
448 if (sym->initializer)
449 sm_msg("info: '%s' is not actually initialized (unreached code).",
450 (sym->ident ? sym->ident->name : "this variable"));
451 } END_FOR_EACH_PTR(sym);
454 static void print_unreached(struct statement *stmt)
456 static int print = 1;
458 if (!__path_is_null()) {
459 print = 1;
460 return;
462 if (!print)
463 return;
465 switch (stmt->type) {
466 case STMT_COMPOUND: /* after a switch before a case stmt */
467 case STMT_RANGE:
468 case STMT_CASE:
469 case STMT_LABEL:
470 return;
471 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
472 print_unreached_initializers(stmt->declaration);
473 return;
474 case STMT_RETURN: /* gcc complains if you don't have a return statement */
475 if (is_last_stmt(stmt))
476 return;
477 break;
478 case STMT_GOTO:
479 if (!option_spammy)
480 return;
481 break;
482 default:
483 break;
485 if (!option_spammy && empty_statement(stmt))
486 return;
487 sm_msg("info: ignoring unreachable code.");
488 print = 0;
491 static void split_asm_constraints(struct expression_list *expr_list)
493 struct expression *expr;
494 int state = 0;
496 FOR_EACH_PTR(expr_list, expr) {
497 switch (state) {
498 case 0: /* identifier */
499 case 1: /* constraint */
500 state++;
501 continue;
502 case 2: /* expression */
503 state = 0;
504 __split_expr(expr);
505 continue;
507 } END_FOR_EACH_PTR(expr);
510 static int is_case_val(struct statement *stmt, sval_t sval)
512 sval_t case_sval;
514 if (stmt->type != STMT_CASE)
515 return 0;
516 if (!stmt->case_expression) {
517 __set_default();
518 return 1;
520 if (!get_value(stmt->case_expression, &case_sval))
521 return 0;
522 if (case_sval.value == sval.value)
523 return 1;
524 return 0;
527 static void split_known_switch(struct statement *stmt, sval_t sval)
529 struct statement *tmp;
531 __split_expr(stmt->switch_expression);
533 push_expression(&switch_expr_stack, stmt->switch_expression);
534 __save_switch_states(top_expression(switch_expr_stack));
535 nullify_path();
536 __push_default();
537 __push_breaks();
539 stmt = stmt->switch_statement;
541 if (!last_stmt)
542 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
544 __push_scope_hooks();
545 FOR_EACH_PTR(stmt->stmts, tmp) {
546 __smatch_lineno = tmp->pos.line;
547 if (is_case_val(tmp, sval)) {
548 __merge_switches(top_expression(switch_expr_stack),
549 stmt->case_expression);
550 __pass_case_to_client(top_expression(switch_expr_stack),
551 stmt->case_expression);
553 if (__path_is_null())
554 continue;
555 __split_stmt(tmp);
556 if (__path_is_null()) {
557 __set_default();
558 goto out;
560 } END_FOR_EACH_PTR(tmp);
561 out:
562 __call_scope_hooks();
563 if (!__pop_default())
564 __merge_switches(top_expression(switch_expr_stack),
565 NULL);
566 __discard_switches();
567 __merge_breaks();
568 pop_expression(&switch_expr_stack);
571 void __split_stmt(struct statement *stmt)
573 sval_t sval;
575 if (!stmt)
576 return;
578 if (out_of_memory() || __bail_on_rest_of_function) {
579 static char *printed = NULL;
581 if (printed != cur_func)
582 sm_msg("Function too hairy. Giving up.");
583 final_pass = 0; /* turn off sm_msg() from here */
584 printed = cur_func;
585 return;
588 add_ptr_list(&big_statement_stack, stmt);
589 free_expression_stack(&big_expression_stack);
590 set_position(stmt->pos);
591 print_unreached(stmt);
592 __pass_to_client(stmt, STMT_HOOK);
594 switch (stmt->type) {
595 case STMT_DECLARATION:
596 split_declaration(stmt->declaration);
597 return;
598 case STMT_RETURN:
599 __split_expr(stmt->ret_value);
600 __pass_to_client(stmt->ret_value, RETURN_HOOK);
601 nullify_path();
602 return;
603 case STMT_EXPRESSION:
604 __split_expr(stmt->expression);
605 return;
606 case STMT_COMPOUND: {
607 struct statement *tmp;
609 if (!last_stmt)
610 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
611 __push_scope_hooks();
612 FOR_EACH_PTR(stmt->stmts, tmp) {
613 __split_stmt(tmp);
614 } END_FOR_EACH_PTR(tmp);
615 __call_scope_hooks();
616 return;
618 case STMT_IF:
619 if (known_condition_true(stmt->if_conditional)) {
620 __split_stmt(stmt->if_true);
621 return;
623 if (known_condition_false(stmt->if_conditional)) {
624 __split_stmt(stmt->if_false);
625 return;
627 if (option_known_conditions &&
628 implied_condition_true(stmt->if_conditional)) {
629 sm_info("this condition is true.");
630 __split_stmt(stmt->if_true);
631 return;
633 if (option_known_conditions &&
634 implied_condition_false(stmt->if_conditional)) {
635 sm_info("this condition is false.");
636 __split_stmt(stmt->if_false);
637 return;
639 __split_whole_condition(stmt->if_conditional);
640 __split_stmt(stmt->if_true);
641 if (empty_statement(stmt->if_true) &&
642 last_stmt_on_same_line() &&
643 !get_macro_name(stmt->if_true->pos))
644 sm_msg("warn: if();");
645 __push_true_states();
646 __use_false_states();
647 __split_stmt(stmt->if_false);
648 __merge_true_states();
649 return;
650 case STMT_ITERATOR:
651 if (stmt->iterator_pre_condition)
652 handle_pre_loop(stmt);
653 else if (stmt->iterator_post_condition)
654 handle_post_loop(stmt);
655 else {
656 // these are for(;;) type loops.
657 handle_pre_loop(stmt);
659 return;
660 case STMT_SWITCH:
661 if (get_value(stmt->switch_expression, &sval)) {
662 split_known_switch(stmt, sval);
663 return;
665 __split_expr(stmt->switch_expression);
666 push_expression(&switch_expr_stack, stmt->switch_expression);
667 __save_switch_states(top_expression(switch_expr_stack));
668 nullify_path();
669 __push_default();
670 __push_breaks();
671 __split_stmt(stmt->switch_statement);
672 if (!__pop_default())
673 __merge_switches(top_expression(switch_expr_stack),
674 NULL);
675 __discard_switches();
676 __merge_breaks();
677 pop_expression(&switch_expr_stack);
678 return;
679 case STMT_CASE:
680 __merge_switches(top_expression(switch_expr_stack),
681 stmt->case_expression);
682 __pass_case_to_client(top_expression(switch_expr_stack),
683 stmt->case_expression);
684 if (!stmt->case_expression)
685 __set_default();
686 __split_expr(stmt->case_expression);
687 __split_expr(stmt->case_to);
688 __split_stmt(stmt->case_statement);
689 return;
690 case STMT_LABEL:
691 if (stmt->label_identifier &&
692 stmt->label_identifier->type == SYM_LABEL &&
693 stmt->label_identifier->ident) {
694 loop_count = 1000000;
695 __merge_gotos(stmt->label_identifier->ident->name);
697 __split_stmt(stmt->label_statement);
698 return;
699 case STMT_GOTO:
700 __split_expr(stmt->goto_expression);
701 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
702 if (!strcmp(stmt->goto_label->ident->name, "break")) {
703 __process_breaks();
704 } else if (!strcmp(stmt->goto_label->ident->name,
705 "continue")) {
706 __process_continues();
708 } else if (stmt->goto_label &&
709 stmt->goto_label->type == SYM_LABEL &&
710 stmt->goto_label->ident) {
711 __save_gotos(stmt->goto_label->ident->name);
713 nullify_path();
714 return;
715 case STMT_NONE:
716 return;
717 case STMT_ASM:
718 __pass_to_client(stmt, ASM_HOOK);
719 __split_expr(stmt->asm_string);
720 split_asm_constraints(stmt->asm_outputs);
721 split_asm_constraints(stmt->asm_inputs);
722 split_asm_constraints(stmt->asm_clobbers);
723 return;
724 case STMT_CONTEXT:
725 return;
726 case STMT_RANGE:
727 __split_expr(stmt->range_expression);
728 __split_expr(stmt->range_low);
729 __split_expr(stmt->range_high);
730 return;
734 static void split_expr_list(struct expression_list *expr_list)
736 struct expression *expr;
738 FOR_EACH_PTR(expr_list, expr) {
739 __split_expr(expr);
740 } END_FOR_EACH_PTR(expr);
743 static void split_sym(struct symbol *sym)
745 if (!sym)
746 return;
747 if (!(sym->namespace & NS_SYMBOL))
748 return;
750 __split_stmt(sym->stmt);
751 __split_expr(sym->array_size);
752 split_symlist(sym->arguments);
753 split_symlist(sym->symbol_list);
754 __split_stmt(sym->inline_stmt);
755 split_symlist(sym->inline_symbol_list);
758 static void split_symlist(struct symbol_list *sym_list)
760 struct symbol *sym;
762 FOR_EACH_PTR(sym_list, sym) {
763 split_sym(sym);
764 } END_FOR_EACH_PTR(sym);
767 static void fake_member_assigns(struct symbol *sym)
769 struct expression *symbol, *deref, *assign, *tmp;
771 symbol = symbol_expression(sym);
772 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
773 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
774 continue;
775 deref = member_expression(symbol, '.', tmp->expr_ident);
776 assign = assign_expression(deref, tmp->ident_expression);
777 __split_expr(assign);
778 } END_FOR_EACH_PTR(tmp);
781 static void fake_assign_expr(struct symbol *sym)
783 struct expression *assign, *symbol;
785 symbol = symbol_expression(sym);
786 assign = assign_expression(symbol, sym->initializer);
787 __split_expr(assign);
790 static void do_initializer_stuff(struct symbol *sym)
792 if (!sym->initializer)
793 return;
794 if (sym->initializer->type == EXPR_INITIALIZER)
795 fake_member_assigns(sym);
796 else
797 fake_assign_expr(sym);
800 static void split_declaration(struct symbol_list *sym_list)
802 struct symbol *sym;
804 FOR_EACH_PTR(sym_list, sym) {
805 __pass_to_client(sym, DECLARATION_HOOK);
806 do_initializer_stuff(sym);
807 split_sym(sym);
808 } END_FOR_EACH_PTR(sym);
811 static void fake_global_assign(struct symbol *sym)
813 struct expression *assign, *symbol;
815 if (!sym->initializer)
816 return;
817 if (sym->initializer->type == EXPR_INITIALIZER) {
818 struct expression *deref, *tmp;
820 symbol = symbol_expression(sym);
821 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
822 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
823 continue;
824 deref = member_expression(symbol, '.', tmp->expr_ident);
825 assign = assign_expression(deref, tmp->ident_expression);
826 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
827 } END_FOR_EACH_PTR(tmp);
828 } else {
829 symbol = symbol_expression(sym);
830 assign = assign_expression(symbol, sym->initializer);
831 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
835 static void split_function(struct symbol *sym)
837 struct symbol *base_type = get_base_type(sym);
839 cur_func_sym = sym;
840 if (sym->ident)
841 cur_func = sym->ident->name;
842 __smatch_lineno = sym->pos.line;
843 last_stmt = NULL;
844 loop_count = 0;
845 sm_debug("new function: %s\n", cur_func);
846 __slist_id = 0;
847 if (option_two_passes) {
848 __unnullify_path();
849 loop_num = 0;
850 final_pass = 0;
851 __pass_to_client(sym, FUNC_DEF_HOOK);
852 __pass_to_client(sym, AFTER_DEF_HOOK);
853 __split_stmt(base_type->stmt);
854 __split_stmt(base_type->inline_stmt);
855 nullify_path();
857 __unnullify_path();
858 loop_num = 0;
859 final_pass = 1;
860 __pass_to_client(sym, FUNC_DEF_HOOK);
861 __pass_to_client(sym, AFTER_DEF_HOOK);
862 __split_stmt(base_type->stmt);
863 __split_stmt(base_type->inline_stmt);
864 __pass_to_client(sym, END_FUNC_HOOK);
865 cur_func = NULL;
866 clear_all_states();
867 free_data_info_allocs();
868 free_expression_stack(&switch_expr_stack);
869 __free_ptr_list((struct ptr_list **)&big_statement_stack);
870 __bail_on_rest_of_function = 0;
873 static void parse_inline(struct expression *call)
875 struct symbol *base_type;
876 int loop_num_bak = loop_num;
877 int final_pass_bak = final_pass;
878 char *cur_func_bak = cur_func;
879 struct statement_list *big_statement_stack_bak = big_statement_stack;
880 struct expression_list *big_expression_stack_bak = big_expression_stack;
881 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
882 struct symbol *cur_func_sym_bak = cur_func_sym;
884 sm_debug("inline function: %s\n", cur_func);
885 __pass_to_client(call, INLINE_FN_START);
886 final_pass = 0; /* don't print anything */
887 __inline_fn = call;
889 base_type = get_base_type(call->fn->symbol);
890 cur_func_sym = call->fn->symbol;
891 if (call->fn->symbol->ident)
892 cur_func = call->fn->symbol->ident->name;
893 else
894 cur_func = NULL;
895 set_position(call->fn->symbol->pos);
897 save_all_states();
898 nullify_all_states();
899 big_statement_stack = NULL;
900 big_expression_stack = NULL;
901 switch_expr_stack = NULL;
903 __unnullify_path();
904 loop_num = 0;
905 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
906 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
907 __split_stmt(base_type->stmt);
908 __split_stmt(base_type->inline_stmt);
909 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
911 free_expression_stack(&switch_expr_stack);
912 __free_ptr_list((struct ptr_list **)&big_statement_stack);
913 nullify_path();
915 loop_num = loop_num_bak;
916 final_pass = final_pass_bak;
917 cur_func_sym = cur_func_sym_bak;
918 cur_func = cur_func_bak;
919 big_statement_stack = big_statement_stack_bak;
920 big_expression_stack = big_expression_stack_bak;
921 switch_expr_stack = switch_expr_stack_bak;
923 restore_all_states();
924 set_position(call->pos);
925 __inline_fn = NULL;
926 __pass_to_client(call, INLINE_FN_END);
929 static struct symbol_list *inlines_called;
930 static void add_inline_function(struct symbol *sym)
932 static struct symbol_list *already_added;
933 struct symbol *tmp;
935 FOR_EACH_PTR(already_added, tmp) {
936 if (tmp == sym)
937 return;
938 } END_FOR_EACH_PTR(tmp);
940 add_ptr_list(&already_added, sym);
941 add_ptr_list(&inlines_called, sym);
944 static void process_inlines()
946 struct symbol *tmp;
948 FOR_EACH_PTR(inlines_called, tmp) {
949 split_function(tmp);
950 } END_FOR_EACH_PTR(tmp);
951 free_ptr_list(&inlines_called);
954 static void split_functions(struct symbol_list *sym_list)
956 struct symbol *sym;
958 FOR_EACH_PTR(sym_list, sym) {
959 set_position(sym->pos);
960 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
961 split_function(sym);
962 process_inlines();
963 } else {
964 __pass_to_client(sym, BASE_HOOK);
965 fake_global_assign(sym);
967 } END_FOR_EACH_PTR(sym);
968 __pass_to_client_no_data(END_FILE_HOOK);
971 void smatch(int argc, char **argv)
974 struct string_list *filelist = NULL;
975 struct symbol_list *sym_list;
977 if (argc < 2) {
978 printf("Usage: smatch [--debug] <filename.c>\n");
979 exit(1);
981 sparse_initialize(argc, argv, &filelist);
982 FOR_EACH_PTR_NOTAG(filelist, base_file) {
983 if (option_file_output) {
984 char buf[256];
986 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
987 sm_outfd = fopen(buf, "w");
988 if (!sm_outfd) {
989 printf("Error: Cannot open %s\n", base_file);
990 exit(1);
993 sym_list = sparse_keep_tokens(base_file);
994 split_functions(sym_list);
995 } END_FOR_EACH_PTR_NOTAG(base_file);