db: silence a compile warning on 32 bit (use %zd for sizeof)
[smatch.git] / smatch_flow.c
blob5b0f39ff59907b0a18ff5682fde6ec642cc40aa9
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 int __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 static int should_inline(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 (should_inline(expr->fn))
240 __inline_call = 1;
241 __pass_to_client(expr, FUNCTION_CALL_HOOK);
242 __inline_call = 0;
243 if (should_inline(expr->fn)) {
244 parse_inline(expr->fn);
246 if (is_noreturn_func(expr->fn))
247 nullify_path();
248 break;
249 case EXPR_INITIALIZER:
250 split_expr_list(expr->expr_list);
251 break;
252 case EXPR_IDENTIFIER:
253 __split_expr(expr->ident_expression);
254 break;
255 case EXPR_INDEX:
256 __split_expr(expr->idx_expression);
257 break;
258 case EXPR_POS:
259 __split_expr(expr->init_expr);
260 break;
261 case EXPR_SYMBOL:
262 __pass_to_client(expr, SYM_HOOK);
263 break;
264 case EXPR_STRING:
265 __pass_to_client(expr, STRING_HOOK);
266 break;
267 default:
268 break;
270 pop_expression(&big_expression_stack);
273 static int is_forever_loop(struct statement *stmt)
275 struct expression *expr;
277 expr = strip_expr(stmt->iterator_pre_condition);
278 if (!expr)
279 expr = stmt->iterator_post_condition;
280 if (!expr) {
281 /* this is a for(;;) loop... */
282 return 1;
285 if (expr->type == EXPR_VALUE && expr->value == 1)
286 return 1;
288 return 0;
291 static int loop_num;
292 static char *get_loop_name(int num)
294 char buf[256];
296 snprintf(buf, 255, "-loop%d", num);
297 buf[255] = '\0';
298 return alloc_sname(buf);
302 * Pre Loops are while and for loops.
304 static void handle_pre_loop(struct statement *stmt)
306 int once_through; /* we go through the loop at least once */
307 struct sm_state *extra_sm = NULL;
308 int unchanged = 0;
309 char *loop_name;
310 struct state_list *slist = NULL;
311 struct sm_state *sm = NULL;
313 loop_name = get_loop_name(loop_num);
314 loop_num++;
316 __split_stmt(stmt->iterator_pre_statement);
318 once_through = implied_condition_true(stmt->iterator_pre_condition);
320 loop_count++;
321 __push_continues();
322 __push_breaks();
324 __merge_gotos(loop_name);
326 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
327 __in_pre_condition++;
328 __pass_to_client(stmt, PRELOOP_HOOK);
329 __split_whole_condition(stmt->iterator_pre_condition);
330 __in_pre_condition--;
331 FOR_EACH_PTR(slist, sm) {
332 set_state(sm->owner, sm->name, sm->sym, sm->state);
333 } END_FOR_EACH_PTR(sm);
334 free_slist(&slist);
335 if (extra_sm)
336 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
338 if (option_assume_loops)
339 once_through = 1;
341 __split_stmt(stmt->iterator_statement);
342 __warn_on_silly_pre_loops();
343 if (is_forever_loop(stmt)) {
344 struct state_list *slist;
346 __save_gotos(loop_name);
348 __push_fake_cur_slist();
349 __split_stmt(stmt->iterator_post_statement);
350 slist = __pop_fake_cur_slist();
352 __discard_continues();
353 __discard_false_states();
354 __use_breaks();
356 if (!__path_is_null())
357 __merge_slist_into_cur(slist);
358 free_slist(&slist);
359 } else {
360 __merge_continues();
361 unchanged = __iterator_unchanged(extra_sm);
362 __split_stmt(stmt->iterator_post_statement);
363 __save_gotos(loop_name);
364 __split_whole_condition(stmt->iterator_pre_condition);
365 nullify_path();
366 __merge_false_states();
367 if (once_through)
368 __discard_false_states();
369 else
370 __merge_false_states();
372 if (extra_sm && unchanged)
373 __extra_pre_loop_hook_after(extra_sm,
374 stmt->iterator_post_statement,
375 stmt->iterator_pre_condition);
376 __merge_breaks();
378 loop_count--;
382 * Post loops are do {} while();
384 static void handle_post_loop(struct statement *stmt)
386 char *loop_name;
388 loop_name = get_loop_name(loop_num);
389 loop_num++;
390 loop_count++;
392 __push_continues();
393 __push_breaks();
394 __merge_gotos(loop_name);
395 __split_stmt(stmt->iterator_statement);
396 __merge_continues();
397 if (!is_zero(stmt->iterator_post_condition))
398 __save_gotos(loop_name);
400 if (is_forever_loop(stmt)) {
401 __use_breaks();
402 } else {
403 __split_whole_condition(stmt->iterator_post_condition);
404 __use_false_states();
405 __merge_breaks();
407 loop_count--;
410 static int empty_statement(struct statement *stmt)
412 if (!stmt)
413 return 0;
414 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
415 return 1;
416 return 0;
419 static int last_stmt_on_same_line()
421 struct statement *stmt;
422 int i = 0;
424 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
425 if (!i++)
426 continue;
427 if (stmt->pos.line == get_lineno())
428 return 1;
429 return 0;
430 } END_FOR_EACH_PTR_REVERSE(stmt);
431 return 0;
434 static struct statement *last_stmt;
435 static int is_last_stmt(struct statement *stmt)
437 if (stmt == last_stmt)
438 return 1;
439 return 0;
442 static void print_unreached_initializers(struct symbol_list *sym_list)
444 struct symbol *sym;
446 FOR_EACH_PTR(sym_list, sym) {
447 if (sym->initializer)
448 sm_msg("info: '%s' is not actually initialized (unreached code).",
449 (sym->ident ? sym->ident->name : "this variable"));
450 } END_FOR_EACH_PTR(sym);
453 static void print_unreached(struct statement *stmt)
455 static int print = 1;
457 if (!__path_is_null()) {
458 print = 1;
459 return;
461 if (!print)
462 return;
464 switch (stmt->type) {
465 case STMT_COMPOUND: /* after a switch before a case stmt */
466 case STMT_RANGE:
467 case STMT_CASE:
468 case STMT_LABEL:
469 return;
470 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
471 print_unreached_initializers(stmt->declaration);
472 return;
473 case STMT_RETURN: /* gcc complains if you don't have a return statement */
474 if (is_last_stmt(stmt))
475 return;
476 break;
477 case STMT_GOTO:
478 if (!option_spammy)
479 return;
480 break;
481 default:
482 break;
484 if (!option_spammy && empty_statement(stmt))
485 return;
486 sm_msg("info: ignoring unreachable code.");
487 print = 0;
490 static void split_asm_constraints(struct expression_list *expr_list)
492 struct expression *expr;
493 int state = 0;
495 FOR_EACH_PTR(expr_list, expr) {
496 switch (state) {
497 case 0: /* identifier */
498 case 1: /* constraint */
499 state++;
500 continue;
501 case 2: /* expression */
502 state = 0;
503 __split_expr(expr);
504 continue;
506 } END_FOR_EACH_PTR(expr);
509 static int is_case_val(struct statement *stmt, sval_t sval)
511 sval_t case_sval;
513 if (stmt->type != STMT_CASE)
514 return 0;
515 if (!stmt->case_expression) {
516 __set_default();
517 return 1;
519 if (!get_value(stmt->case_expression, &case_sval))
520 return 0;
521 if (case_sval.value == sval.value)
522 return 1;
523 return 0;
526 static void split_known_switch(struct statement *stmt, sval_t sval)
528 struct statement *tmp;
530 __split_expr(stmt->switch_expression);
532 push_expression(&switch_expr_stack, stmt->switch_expression);
533 __save_switch_states(top_expression(switch_expr_stack));
534 nullify_path();
535 __push_default();
536 __push_breaks();
538 stmt = stmt->switch_statement;
540 if (!last_stmt)
541 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
543 __push_scope_hooks();
544 FOR_EACH_PTR(stmt->stmts, tmp) {
545 __smatch_lineno = tmp->pos.line;
546 if (is_case_val(tmp, sval)) {
547 __merge_switches(top_expression(switch_expr_stack),
548 stmt->case_expression);
549 __pass_case_to_client(top_expression(switch_expr_stack),
550 stmt->case_expression);
552 if (__path_is_null())
553 continue;
554 __split_stmt(tmp);
555 if (__path_is_null()) {
556 __set_default();
557 goto out;
559 } END_FOR_EACH_PTR(tmp);
560 out:
561 __call_scope_hooks();
562 if (!__pop_default())
563 __merge_switches(top_expression(switch_expr_stack),
564 NULL);
565 __discard_switches();
566 __merge_breaks();
567 pop_expression(&switch_expr_stack);
570 void __split_stmt(struct statement *stmt)
572 sval_t sval;
574 if (!stmt)
575 return;
577 if (out_of_memory() || __bail_on_rest_of_function) {
578 static char *printed = NULL;
580 if (printed != cur_func)
581 sm_msg("Function too hairy. Giving up.");
582 final_pass = 0; /* turn off sm_msg() from here */
583 printed = cur_func;
584 return;
587 add_ptr_list(&big_statement_stack, stmt);
588 free_expression_stack(&big_expression_stack);
589 set_position(stmt->pos);
590 print_unreached(stmt);
591 __pass_to_client(stmt, STMT_HOOK);
593 switch (stmt->type) {
594 case STMT_DECLARATION:
595 split_declaration(stmt->declaration);
596 return;
597 case STMT_RETURN:
598 __split_expr(stmt->ret_value);
599 __pass_to_client(stmt->ret_value, RETURN_HOOK);
600 nullify_path();
601 return;
602 case STMT_EXPRESSION:
603 __split_expr(stmt->expression);
604 return;
605 case STMT_COMPOUND: {
606 struct statement *tmp;
608 if (!last_stmt)
609 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
610 __push_scope_hooks();
611 FOR_EACH_PTR(stmt->stmts, tmp) {
612 __split_stmt(tmp);
613 } END_FOR_EACH_PTR(tmp);
614 __call_scope_hooks();
615 return;
617 case STMT_IF:
618 if (known_condition_true(stmt->if_conditional)) {
619 __split_stmt(stmt->if_true);
620 return;
622 if (known_condition_false(stmt->if_conditional)) {
623 __split_stmt(stmt->if_false);
624 return;
626 if (option_known_conditions &&
627 implied_condition_true(stmt->if_conditional)) {
628 sm_info("this condition is true.");
629 __split_stmt(stmt->if_true);
630 return;
632 if (option_known_conditions &&
633 implied_condition_false(stmt->if_conditional)) {
634 sm_info("this condition is false.");
635 __split_stmt(stmt->if_false);
636 return;
638 __split_whole_condition(stmt->if_conditional);
639 __split_stmt(stmt->if_true);
640 if (empty_statement(stmt->if_true) &&
641 last_stmt_on_same_line() &&
642 !get_macro_name(stmt->if_true->pos))
643 sm_msg("warn: if();");
644 __push_true_states();
645 __use_false_states();
646 __split_stmt(stmt->if_false);
647 __merge_true_states();
648 return;
649 case STMT_ITERATOR:
650 if (stmt->iterator_pre_condition)
651 handle_pre_loop(stmt);
652 else if (stmt->iterator_post_condition)
653 handle_post_loop(stmt);
654 else {
655 // these are for(;;) type loops.
656 handle_pre_loop(stmt);
658 return;
659 case STMT_SWITCH:
660 if (get_value(stmt->switch_expression, &sval)) {
661 split_known_switch(stmt, sval);
662 return;
664 __split_expr(stmt->switch_expression);
665 push_expression(&switch_expr_stack, stmt->switch_expression);
666 __save_switch_states(top_expression(switch_expr_stack));
667 nullify_path();
668 __push_default();
669 __push_breaks();
670 __split_stmt(stmt->switch_statement);
671 if (!__pop_default())
672 __merge_switches(top_expression(switch_expr_stack),
673 NULL);
674 __discard_switches();
675 __merge_breaks();
676 pop_expression(&switch_expr_stack);
677 return;
678 case STMT_CASE:
679 __merge_switches(top_expression(switch_expr_stack),
680 stmt->case_expression);
681 __pass_case_to_client(top_expression(switch_expr_stack),
682 stmt->case_expression);
683 if (!stmt->case_expression)
684 __set_default();
685 __split_expr(stmt->case_expression);
686 __split_expr(stmt->case_to);
687 __split_stmt(stmt->case_statement);
688 return;
689 case STMT_LABEL:
690 if (stmt->label_identifier &&
691 stmt->label_identifier->type == SYM_LABEL &&
692 stmt->label_identifier->ident) {
693 loop_count = 1000000;
694 __merge_gotos(stmt->label_identifier->ident->name);
696 __split_stmt(stmt->label_statement);
697 return;
698 case STMT_GOTO:
699 __split_expr(stmt->goto_expression);
700 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
701 if (!strcmp(stmt->goto_label->ident->name, "break")) {
702 __process_breaks();
703 } else if (!strcmp(stmt->goto_label->ident->name,
704 "continue")) {
705 __process_continues();
707 } else if (stmt->goto_label &&
708 stmt->goto_label->type == SYM_LABEL &&
709 stmt->goto_label->ident) {
710 __save_gotos(stmt->goto_label->ident->name);
712 nullify_path();
713 return;
714 case STMT_NONE:
715 return;
716 case STMT_ASM:
717 __pass_to_client(stmt, ASM_HOOK);
718 __split_expr(stmt->asm_string);
719 split_asm_constraints(stmt->asm_outputs);
720 split_asm_constraints(stmt->asm_inputs);
721 split_asm_constraints(stmt->asm_clobbers);
722 return;
723 case STMT_CONTEXT:
724 return;
725 case STMT_RANGE:
726 __split_expr(stmt->range_expression);
727 __split_expr(stmt->range_low);
728 __split_expr(stmt->range_high);
729 return;
733 static void split_expr_list(struct expression_list *expr_list)
735 struct expression *expr;
737 FOR_EACH_PTR(expr_list, expr) {
738 __split_expr(expr);
739 } END_FOR_EACH_PTR(expr);
742 static void split_sym(struct symbol *sym)
744 if (!sym)
745 return;
746 if (!(sym->namespace & NS_SYMBOL))
747 return;
749 __split_stmt(sym->stmt);
750 __split_expr(sym->array_size);
751 split_symlist(sym->arguments);
752 split_symlist(sym->symbol_list);
753 __split_stmt(sym->inline_stmt);
754 split_symlist(sym->inline_symbol_list);
757 static void split_symlist(struct symbol_list *sym_list)
759 struct symbol *sym;
761 FOR_EACH_PTR(sym_list, sym) {
762 split_sym(sym);
763 } END_FOR_EACH_PTR(sym);
766 static void fake_member_assigns(struct symbol *sym)
768 struct expression *symbol, *deref, *assign, *tmp;
770 symbol = symbol_expression(sym);
771 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
772 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
773 continue;
774 deref = member_expression(symbol, '.', tmp->expr_ident);
775 assign = assign_expression(deref, tmp->ident_expression);
776 __split_expr(assign);
777 } END_FOR_EACH_PTR(tmp);
780 static void fake_assign_expr(struct symbol *sym)
782 struct expression *assign, *symbol;
784 symbol = symbol_expression(sym);
785 assign = assign_expression(symbol, sym->initializer);
786 __split_expr(assign);
789 static void do_initializer_stuff(struct symbol *sym)
791 if (!sym->initializer)
792 return;
793 if (sym->initializer->type == EXPR_INITIALIZER)
794 fake_member_assigns(sym);
795 else
796 fake_assign_expr(sym);
799 static void split_declaration(struct symbol_list *sym_list)
801 struct symbol *sym;
803 FOR_EACH_PTR(sym_list, sym) {
804 __pass_to_client(sym, DECLARATION_HOOK);
805 do_initializer_stuff(sym);
806 split_sym(sym);
807 } END_FOR_EACH_PTR(sym);
810 static void fake_global_assign(struct symbol *sym)
812 struct expression *assign, *symbol;
814 if (!sym->initializer)
815 return;
816 if (sym->initializer->type == EXPR_INITIALIZER) {
817 struct expression *deref, *tmp;
819 symbol = symbol_expression(sym);
820 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
821 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
822 continue;
823 deref = member_expression(symbol, '.', tmp->expr_ident);
824 assign = assign_expression(deref, tmp->ident_expression);
825 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
826 } END_FOR_EACH_PTR(tmp);
827 } else {
828 symbol = symbol_expression(sym);
829 assign = assign_expression(symbol, sym->initializer);
830 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
834 static void split_function(struct symbol *sym)
836 struct symbol *base_type = get_base_type(sym);
838 cur_func_sym = sym;
839 if (sym->ident)
840 cur_func = sym->ident->name;
841 __smatch_lineno = sym->pos.line;
842 last_stmt = NULL;
843 loop_count = 0;
844 sm_debug("new function: %s\n", cur_func);
845 __slist_id = 0;
846 if (option_two_passes) {
847 __unnullify_path();
848 loop_num = 0;
849 final_pass = 0;
850 __pass_to_client(sym, FUNC_DEF_HOOK);
851 __pass_to_client(sym, AFTER_DEF_HOOK);
852 __split_stmt(base_type->stmt);
853 __split_stmt(base_type->inline_stmt);
854 nullify_path();
856 __unnullify_path();
857 loop_num = 0;
858 final_pass = 1;
859 __pass_to_client(sym, FUNC_DEF_HOOK);
860 __pass_to_client(sym, AFTER_DEF_HOOK);
861 __split_stmt(base_type->stmt);
862 __split_stmt(base_type->inline_stmt);
863 __pass_to_client(sym, END_FUNC_HOOK);
864 cur_func = NULL;
865 clear_all_states();
866 free_data_info_allocs();
867 free_expression_stack(&switch_expr_stack);
868 __free_ptr_list((struct ptr_list **)&big_statement_stack);
869 __bail_on_rest_of_function = 0;
872 static void parse_inline(struct expression *expr)
874 struct symbol *base_type;
875 int loop_num_bak = loop_num;
876 int final_pass_bak = final_pass;
877 char *cur_func_bak = cur_func;
878 struct statement_list *big_statement_stack_bak = big_statement_stack;
879 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
880 struct symbol *cur_func_sym_bak = cur_func_sym;
882 sm_debug("inline function: %s\n", cur_func);
883 final_pass = 0; /* don't print anything */
884 __inline_fn = 1;
886 base_type = get_base_type(expr->symbol);
887 cur_func_sym = expr->symbol;
888 if (expr->symbol->ident)
889 cur_func = expr->symbol->ident->name;
890 else
891 cur_func = NULL;
892 set_position(expr->symbol->pos);
894 save_all_states();
895 nullify_all_states();
896 big_statement_stack = NULL;
897 switch_expr_stack = NULL;
899 __unnullify_path();
900 loop_num = 0;
901 __pass_to_client(expr->symbol, FUNC_DEF_HOOK);
902 __pass_to_client(expr->symbol, AFTER_DEF_HOOK);
903 __split_stmt(base_type->stmt);
904 __split_stmt(base_type->inline_stmt);
905 __pass_to_client(expr->symbol, END_FUNC_HOOK);
907 free_expression_stack(&switch_expr_stack);
908 __free_ptr_list((struct ptr_list **)&big_statement_stack);
909 nullify_path();
911 loop_num = loop_num_bak;
912 final_pass = final_pass_bak;
913 cur_func_sym = cur_func_sym_bak;
914 cur_func = cur_func_bak;
915 big_statement_stack = big_statement_stack_bak;
916 switch_expr_stack = switch_expr_stack_bak;
918 restore_all_states();
919 set_position(expr->pos);
920 __inline_fn = 0;
923 static struct symbol_list *inlines_called;
924 static void add_inline_function(struct symbol *sym)
926 static struct symbol_list *already_added;
927 struct symbol *tmp;
929 FOR_EACH_PTR(already_added, tmp) {
930 if (tmp == sym)
931 return;
932 } END_FOR_EACH_PTR(tmp);
934 add_ptr_list(&already_added, sym);
935 add_ptr_list(&inlines_called, sym);
938 static void process_inlines()
940 struct symbol *tmp;
942 FOR_EACH_PTR(inlines_called, tmp) {
943 split_function(tmp);
944 } END_FOR_EACH_PTR(tmp);
945 free_ptr_list(&inlines_called);
948 static void split_functions(struct symbol_list *sym_list)
950 struct symbol *sym;
952 FOR_EACH_PTR(sym_list, sym) {
953 set_position(sym->pos);
954 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
955 split_function(sym);
956 process_inlines();
957 } else {
958 __pass_to_client(sym, BASE_HOOK);
959 fake_global_assign(sym);
961 } END_FOR_EACH_PTR(sym);
962 __pass_to_client_no_data(END_FILE_HOOK);
965 void smatch(int argc, char **argv)
968 struct string_list *filelist = NULL;
969 struct symbol_list *sym_list;
971 if (argc < 2) {
972 printf("Usage: smatch [--debug] <filename.c>\n");
973 exit(1);
975 sparse_initialize(argc, argv, &filelist);
976 FOR_EACH_PTR_NOTAG(filelist, base_file) {
977 if (option_file_output) {
978 char buf[256];
980 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
981 sm_outfd = fopen(buf, "w");
982 if (!sm_outfd) {
983 printf("Error: Cannot open %s\n", base_file);
984 exit(1);
987 sym_list = sparse_keep_tokens(base_file);
988 split_functions(sym_list);
989 } END_FOR_EACH_PTR_NOTAG(base_file);