flow: add init_fake_env()/end_fake_env()
[smatch.git] / smatch_flow.c
blobd3bcc79d846350e6aa9a4adf18d5247c4156d09c
1 /*
2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #define _GNU_SOURCE 1
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "token.h"
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 int __in_fake_assign;
29 static int in_fake_env;
30 int final_pass;
31 int __inline_call;
32 struct expression *__inline_fn;
34 static int __smatch_lineno = 0;
36 static char *base_file;
37 static const char *filename;
38 static char *pathname;
39 static char *full_filename;
40 static char *cur_func;
41 static unsigned int loop_count;
42 static int last_goto_statement_handled;
43 int __expr_stmt_count;
44 int __in_function_def;
45 static struct expression_list *switch_expr_stack = NULL;
46 static struct expression_list *post_op_stack = NULL;
48 struct expression_list *big_expression_stack;
49 struct statement_list *big_statement_stack;
50 struct statement *__prev_stmt;
51 struct statement *__cur_stmt;
52 struct statement *__next_stmt;
53 int __in_pre_condition = 0;
54 int __bail_on_rest_of_function = 0;
55 static struct timeval fn_start_time;
56 char *get_function(void) { return cur_func; }
57 int get_lineno(void) { return __smatch_lineno; }
58 int inside_loop(void) { return !!loop_count; }
59 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
60 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
61 int in_expression_statement(void) { return !!__expr_stmt_count; }
63 static void split_symlist(struct symbol_list *sym_list);
64 static void split_declaration(struct symbol_list *sym_list);
65 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
66 static void add_inline_function(struct symbol *sym);
67 static void parse_inline(struct expression *expr);
69 int option_assume_loops = 0;
70 int option_two_passes = 0;
71 struct symbol *cur_func_sym = NULL;
72 struct stree *global_states;
74 long long valid_ptr_min = 4096;
75 long long valid_ptr_max = 2117777777;
76 sval_t valid_ptr_min_sval = {
77 .type = &ptr_ctype,
78 {.value = 4096},
80 sval_t valid_ptr_max_sval = {
81 .type = &ptr_ctype,
82 {.value = LONG_MAX - 100000},
85 static void set_valid_ptr_max(void)
87 if (type_bits(&ptr_ctype) == 32)
88 valid_ptr_max = 2117777777;
89 else if (type_bits(&ptr_ctype) == 64)
90 valid_ptr_max = 2117777777777777777LL;
92 valid_ptr_max_sval.value = valid_ptr_max;
95 int outside_of_function(void)
97 return cur_func_sym == NULL;
100 const char *get_filename(void)
102 if (option_info)
103 return base_file;
104 if (option_full_path)
105 return full_filename;
106 return filename;
109 const char *get_base_file(void)
111 return base_file;
114 static void set_position(struct position pos)
116 int len;
117 static int prev_stream = -1;
119 if (in_fake_env)
120 return;
122 if (pos.stream == 0 && pos.line == 0)
123 return;
125 __smatch_lineno = pos.line;
127 if (pos.stream == prev_stream)
128 return;
130 filename = stream_name(pos.stream);
132 free(full_filename);
133 pathname = getcwd(NULL, 0);
134 if (pathname) {
135 len = strlen(pathname) + 1 + strlen(filename) + 1;
136 full_filename = malloc(len);
137 snprintf(full_filename, len, "%s/%s", pathname, filename);
138 } else {
139 full_filename = alloc_string(filename);
141 free(pathname);
144 void set_parent_expr(struct expression *expr, struct expression *parent)
146 if (!expr)
147 return;
148 expr->parent = parent;
151 static void set_parent_stmt(struct statement *stmt, struct statement *parent)
153 if (!stmt)
154 return;
155 stmt->parent = parent;
158 int is_assigned_call(struct expression *expr)
160 if (expr->parent &&
161 expr->parent->type == EXPR_ASSIGNMENT &&
162 expr->parent->op == '=' &&
163 strip_expr(expr->parent->right) == expr)
164 return 1;
166 return 0;
169 static int is_inline_func(struct expression *expr)
171 if (expr->type != EXPR_SYMBOL || !expr->symbol)
172 return 0;
173 if (expr->symbol->ctype.modifiers & MOD_INLINE)
174 return 1;
175 return 0;
178 static int is_noreturn_func(struct expression *expr)
180 if (expr->type != EXPR_SYMBOL || !expr->symbol)
181 return 0;
182 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
183 return 1;
184 return 0;
187 int inlinable(struct expression *expr)
189 struct symbol *sym;
190 struct statement *last_stmt = NULL;
192 if (__inline_fn) /* don't nest */
193 return 0;
195 if (expr->type != EXPR_SYMBOL || !expr->symbol)
196 return 0;
197 if (is_no_inline_function(expr->symbol->ident->name))
198 return 0;
199 sym = get_base_type(expr->symbol);
200 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
201 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
202 return 0;
203 if (sym->stmt->type != STMT_COMPOUND)
204 return 0;
205 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
207 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
208 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
209 return 0;
210 if (sym->inline_stmt->type != STMT_COMPOUND)
211 return 0;
212 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
215 if (!last_stmt)
216 return 0;
218 /* the magic numbers in this function are pulled out of my bum. */
219 if (last_stmt->pos.line > sym->pos.line + 20)
220 return 0;
222 return 1;
225 void __process_post_op_stack(void)
227 struct expression *expr;
229 FOR_EACH_PTR(post_op_stack, expr) {
230 __pass_to_client(expr, OP_HOOK);
231 } END_FOR_EACH_PTR(expr);
233 __free_ptr_list((struct ptr_list **)&post_op_stack);
236 static int handle_comma_assigns(struct expression *expr)
238 struct expression *right;
239 struct expression *assign;
241 right = strip_expr(expr->right);
242 if (right->type != EXPR_COMMA)
243 return 0;
245 __split_expr(right->left);
246 __process_post_op_stack();
248 assign = assign_expression(expr->left, right->right);
249 __split_expr(assign);
251 return 1;
254 /* This is to handle *p++ = foo; assignments */
255 static int handle_postop_assigns(struct expression *expr)
257 struct expression *left, *fake_left;
258 struct expression *assign;
260 left = strip_expr(expr->left);
261 if (left->type != EXPR_PREOP || left->op != '*')
262 return 0;
263 left = strip_expr(left->unop);
264 if (left->type != EXPR_POSTOP)
265 return 0;
267 fake_left = deref_expression(strip_expr(left->unop));
268 assign = assign_expression(fake_left, expr->right);
270 __split_expr(assign);
271 __split_expr(expr->left);
273 return 1;
276 static int prev_expression_is_getting_address(struct expression *expr)
278 struct expression *parent;
280 do {
281 parent = expr->parent;
283 if (!parent)
284 return 0;
285 if (parent->type == EXPR_PREOP && parent->op == '&')
286 return 1;
287 if (parent->type == EXPR_PREOP && parent->op == '(')
288 goto next;
289 if (parent->type == EXPR_DEREF && parent->op == '.')
290 goto next;
292 return 0;
293 next:
294 expr = parent;
295 } while (1);
298 void __split_expr(struct expression *expr)
300 if (!expr)
301 return;
303 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
305 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
306 return;
307 if (__in_fake_assign >= 4) /* don't allow too much nesting */
308 return;
310 push_expression(&big_expression_stack, expr);
311 set_position(expr->pos);
312 __pass_to_client(expr, EXPR_HOOK);
314 switch (expr->type) {
315 case EXPR_PREOP:
316 set_parent_expr(expr->unop, expr);
318 if (expr->op == '*' &&
319 !prev_expression_is_getting_address(expr))
320 __pass_to_client(expr, DEREF_HOOK);
321 __split_expr(expr->unop);
322 __pass_to_client(expr, OP_HOOK);
323 break;
324 case EXPR_POSTOP:
325 set_parent_expr(expr->unop, expr);
327 __split_expr(expr->unop);
328 push_expression(&post_op_stack, expr);
329 break;
330 case EXPR_STATEMENT:
331 __expr_stmt_count++;
332 if (expr->statement && !expr->statement->parent) {
333 set_parent_stmt(expr->statement,
334 last_ptr_list((struct ptr_list *)big_statement_stack));
336 __split_stmt(expr->statement);
337 __expr_stmt_count--;
338 break;
339 case EXPR_LOGICAL:
340 case EXPR_COMPARE:
341 set_parent_expr(expr->left, expr);
342 set_parent_expr(expr->right, expr);
344 __pass_to_client(expr, LOGIC_HOOK);
345 __handle_logic(expr);
346 break;
347 case EXPR_BINOP:
348 set_parent_expr(expr->left, expr);
349 set_parent_expr(expr->right, expr);
351 __pass_to_client(expr, BINOP_HOOK);
352 case EXPR_COMMA:
353 set_parent_expr(expr->left, expr);
354 set_parent_expr(expr->right, expr);
356 __split_expr(expr->left);
357 __process_post_op_stack();
358 __split_expr(expr->right);
359 break;
360 case EXPR_ASSIGNMENT: {
361 struct expression *right;
363 set_parent_expr(expr->left, expr);
364 set_parent_expr(expr->right, expr);
366 if (!expr->right)
367 break;
369 right = strip_expr(expr->right);
371 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
373 /* foo = !bar() */
374 if (__handle_condition_assigns(expr))
375 break;
376 /* foo = (x < 5 ? foo : 5); */
377 if (__handle_select_assigns(expr))
378 break;
379 /* foo = ({frob(); frob(); frob(); 1;}) */
380 if (__handle_expr_statement_assigns(expr))
381 break;
382 /* foo = (3, 4); */
383 if (handle_comma_assigns(expr))
384 break;
385 if (handle_postop_assigns(expr))
386 break;
388 __split_expr(expr->right);
389 if (outside_of_function())
390 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
391 else
392 __pass_to_client(expr, ASSIGNMENT_HOOK);
394 __fake_struct_member_assignments(expr);
396 if (expr->op == '=' && right->type == EXPR_CALL)
397 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
399 if (get_macro_name(right->pos) &&
400 get_macro_name(expr->pos) != get_macro_name(right->pos))
401 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
403 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
405 __split_expr(expr->left);
406 break;
408 case EXPR_DEREF:
409 set_parent_expr(expr->deref, expr);
411 __pass_to_client(expr, DEREF_HOOK);
412 __split_expr(expr->deref);
413 break;
414 case EXPR_SLICE:
415 set_parent_expr(expr->base, expr);
417 __split_expr(expr->base);
418 break;
419 case EXPR_CAST:
420 case EXPR_FORCE_CAST:
421 set_parent_expr(expr->cast_expression, expr);
423 __pass_to_client(expr, CAST_HOOK);
424 __split_expr(expr->cast_expression);
425 break;
426 case EXPR_SIZEOF:
427 if (expr->cast_expression)
428 __pass_to_client(strip_parens(expr->cast_expression),
429 SIZEOF_HOOK);
430 break;
431 case EXPR_OFFSETOF:
432 case EXPR_ALIGNOF:
433 evaluate_expression(expr);
434 break;
435 case EXPR_CONDITIONAL:
436 case EXPR_SELECT:
437 set_parent_expr(expr->conditional, expr);
438 set_parent_expr(expr->cond_true, expr);
439 set_parent_expr(expr->cond_false, expr);
441 if (known_condition_true(expr->conditional)) {
442 __split_expr(expr->cond_true);
443 break;
445 if (known_condition_false(expr->conditional)) {
446 __split_expr(expr->cond_false);
447 break;
449 __pass_to_client(expr, SELECT_HOOK);
450 __split_whole_condition(expr->conditional);
451 __split_expr(expr->cond_true);
452 __push_true_states();
453 __use_false_states();
454 __split_expr(expr->cond_false);
455 __merge_true_states();
456 break;
457 case EXPR_CALL:
458 set_parent_expr(expr->fn, expr);
460 if (sym_name_is("__builtin_constant_p", expr->fn))
461 break;
462 split_expr_list(expr->args, expr);
463 __split_expr(expr->fn);
464 if (is_inline_func(expr->fn))
465 add_inline_function(expr->fn->symbol);
466 if (inlinable(expr->fn))
467 __inline_call = 1;
468 __process_post_op_stack();
469 __pass_to_client(expr, FUNCTION_CALL_HOOK);
470 __inline_call = 0;
471 if (inlinable(expr->fn)) {
472 parse_inline(expr);
474 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
475 if (is_noreturn_func(expr->fn))
476 nullify_path();
477 break;
478 case EXPR_INITIALIZER:
479 split_expr_list(expr->expr_list, expr);
480 break;
481 case EXPR_IDENTIFIER:
482 set_parent_expr(expr->ident_expression, expr);
483 __split_expr(expr->ident_expression);
484 break;
485 case EXPR_INDEX:
486 set_parent_expr(expr->idx_expression, expr);
487 __split_expr(expr->idx_expression);
488 break;
489 case EXPR_POS:
490 set_parent_expr(expr->init_expr, expr);
491 __split_expr(expr->init_expr);
492 break;
493 case EXPR_SYMBOL:
494 __pass_to_client(expr, SYM_HOOK);
495 break;
496 case EXPR_STRING:
497 __pass_to_client(expr, STRING_HOOK);
498 break;
499 default:
500 break;
502 pop_expression(&big_expression_stack);
505 static int is_forever_loop(struct statement *stmt)
507 struct expression *expr;
508 sval_t sval;
510 expr = strip_expr(stmt->iterator_pre_condition);
511 if (!expr)
512 expr = stmt->iterator_post_condition;
513 if (!expr) {
514 /* this is a for(;;) loop... */
515 return 1;
518 if (get_value(expr, &sval) && sval.value != 0)
519 return 1;
521 return 0;
524 static int loop_num;
525 static char *get_loop_name(int num)
527 char buf[256];
529 snprintf(buf, 255, "-loop%d", num);
530 buf[255] = '\0';
531 return alloc_sname(buf);
535 * Pre Loops are while and for loops.
537 static void handle_pre_loop(struct statement *stmt)
539 int once_through; /* we go through the loop at least once */
540 struct sm_state *extra_sm = NULL;
541 int unchanged = 0;
542 char *loop_name;
543 struct stree *stree = NULL;
544 struct sm_state *sm = NULL;
546 loop_name = get_loop_name(loop_num);
547 loop_num++;
549 __split_stmt(stmt->iterator_pre_statement);
550 __prev_stmt = stmt->iterator_pre_statement;
552 once_through = implied_condition_true(stmt->iterator_pre_condition);
554 loop_count++;
555 __push_continues();
556 __push_breaks();
558 __merge_gotos(loop_name, NULL);
560 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
561 __in_pre_condition++;
562 __pass_to_client(stmt, PRELOOP_HOOK);
563 __split_whole_condition(stmt->iterator_pre_condition);
564 __in_pre_condition--;
565 FOR_EACH_SM(stree, sm) {
566 set_state(sm->owner, sm->name, sm->sym, sm->state);
567 } END_FOR_EACH_SM(sm);
568 free_stree(&stree);
569 if (extra_sm)
570 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
572 if (option_assume_loops)
573 once_through = 1;
575 __split_stmt(stmt->iterator_statement);
576 if (is_forever_loop(stmt)) {
577 __merge_continues();
578 __save_gotos(loop_name, NULL);
580 __push_fake_cur_stree();
581 __split_stmt(stmt->iterator_post_statement);
582 stree = __pop_fake_cur_stree();
584 __discard_false_states();
585 __use_breaks();
587 if (!__path_is_null())
588 __merge_stree_into_cur(stree);
589 free_stree(&stree);
590 } else {
591 __merge_continues();
592 unchanged = __iterator_unchanged(extra_sm);
593 __split_stmt(stmt->iterator_post_statement);
594 __prev_stmt = stmt->iterator_post_statement;
595 __cur_stmt = stmt;
597 __save_gotos(loop_name, NULL);
598 __in_pre_condition++;
599 __split_whole_condition(stmt->iterator_pre_condition);
600 __in_pre_condition--;
601 nullify_path();
602 __merge_false_states();
603 if (once_through)
604 __discard_false_states();
605 else
606 __merge_false_states();
608 if (extra_sm && unchanged)
609 __extra_pre_loop_hook_after(extra_sm,
610 stmt->iterator_post_statement,
611 stmt->iterator_pre_condition);
612 __merge_breaks();
614 loop_count--;
618 * Post loops are do {} while();
620 static void handle_post_loop(struct statement *stmt)
622 char *loop_name;
624 loop_name = get_loop_name(loop_num);
625 loop_num++;
626 loop_count++;
628 __push_continues();
629 __push_breaks();
630 __merge_gotos(loop_name, NULL);
631 __split_stmt(stmt->iterator_statement);
632 __merge_continues();
633 if (!is_zero(stmt->iterator_post_condition))
634 __save_gotos(loop_name, NULL);
636 if (is_forever_loop(stmt)) {
637 __use_breaks();
638 } else {
639 __split_whole_condition(stmt->iterator_post_condition);
640 __use_false_states();
641 __merge_breaks();
643 loop_count--;
646 static int empty_statement(struct statement *stmt)
648 if (!stmt)
649 return 0;
650 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
651 return 1;
652 return 0;
655 static int last_stmt_on_same_line(void)
657 struct statement *stmt;
658 int i = 0;
660 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
661 if (!i++)
662 continue;
663 if (stmt->pos.line == get_lineno())
664 return 1;
665 return 0;
666 } END_FOR_EACH_PTR_REVERSE(stmt);
667 return 0;
670 static void split_asm_constraints(struct expression_list *expr_list)
672 struct expression *expr;
673 int state = 0;
675 FOR_EACH_PTR(expr_list, expr) {
676 switch (state) {
677 case 0: /* identifier */
678 case 1: /* constraint */
679 state++;
680 continue;
681 case 2: /* expression */
682 state = 0;
683 __split_expr(expr);
684 continue;
686 } END_FOR_EACH_PTR(expr);
689 static int is_case_val(struct statement *stmt, sval_t sval)
691 sval_t case_sval;
693 if (stmt->type != STMT_CASE)
694 return 0;
695 if (!stmt->case_expression) {
696 __set_default();
697 return 1;
699 if (!get_value(stmt->case_expression, &case_sval))
700 return 0;
701 if (case_sval.value == sval.value)
702 return 1;
703 return 0;
706 static struct range_list *get_case_rl(struct expression *switch_expr,
707 struct expression *case_expr,
708 struct expression *case_to)
710 sval_t start, end;
711 struct range_list *rl = NULL;
712 struct symbol *switch_type;
714 switch_type = get_type(switch_expr);
715 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
716 start = sval_cast(switch_type, start);
717 end = sval_cast(switch_type, end);
718 add_range(&rl, start, end);
719 } else if (get_value(case_expr, &start)) {
720 start = sval_cast(switch_type, start);
721 add_range(&rl, start, start);
724 return rl;
727 static void split_known_switch(struct statement *stmt, sval_t sval)
729 struct statement *tmp;
730 struct range_list *rl;
732 __split_expr(stmt->switch_expression);
733 sval = sval_cast(get_type(stmt->switch_expression), sval);
735 push_expression(&switch_expr_stack, stmt->switch_expression);
736 __save_switch_states(top_expression(switch_expr_stack));
737 nullify_path();
738 __push_default();
739 __push_breaks();
741 stmt = stmt->switch_statement;
743 __push_scope_hooks();
744 FOR_EACH_PTR(stmt->stmts, tmp) {
745 __smatch_lineno = tmp->pos.line;
746 if (is_case_val(tmp, sval)) {
747 rl = alloc_rl(sval, sval);
748 __merge_switches(top_expression(switch_expr_stack), rl);
749 __pass_case_to_client(top_expression(switch_expr_stack), rl);
751 if (__path_is_null())
752 continue;
753 __split_stmt(tmp);
754 if (__path_is_null()) {
755 __set_default();
756 goto out;
758 } END_FOR_EACH_PTR(tmp);
759 out:
760 __call_scope_hooks();
761 if (!__pop_default())
762 __merge_switches(top_expression(switch_expr_stack), NULL);
763 __discard_switches();
764 __merge_breaks();
765 pop_expression(&switch_expr_stack);
768 static void split_case(struct statement *stmt)
770 struct range_list *rl = NULL;
772 rl = get_case_rl(top_expression(switch_expr_stack),
773 stmt->case_expression, stmt->case_to);
774 while (stmt->case_statement->type == STMT_CASE) {
775 struct range_list *tmp;
777 tmp = get_case_rl(top_expression(switch_expr_stack),
778 stmt->case_statement->case_expression,
779 stmt->case_statement->case_to);
780 if (!tmp)
781 break;
782 rl = rl_union(rl, tmp);
783 if (!stmt->case_expression)
784 __set_default();
785 stmt = stmt->case_statement;
788 __merge_switches(top_expression(switch_expr_stack), rl);
790 if (!stmt->case_expression)
791 __set_default();
792 __split_stmt(stmt->case_statement);
795 static int taking_too_long(void)
797 int ms;
799 ms = ms_since(&fn_start_time);
800 if (ms > 1000 * 60 * 5) /* five minutes */
801 return 1;
802 return 0;
805 static int is_last_stmt(struct statement *cur_stmt)
807 struct symbol *fn = get_base_type(cur_func_sym);
808 struct statement *stmt;
810 if (!fn)
811 return 0;
812 stmt = fn->stmt;
813 if (!stmt)
814 stmt = fn->inline_stmt;
815 if (!stmt || stmt->type != STMT_COMPOUND)
816 return 0;
817 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
818 if (stmt && stmt->type == STMT_LABEL)
819 stmt = stmt->label_statement;
820 if (stmt == cur_stmt)
821 return 1;
822 return 0;
825 static void handle_backward_goto(struct statement *goto_stmt)
827 const char *goto_name, *label_name;
828 struct statement *func_stmt;
829 struct symbol *base_type = get_base_type(cur_func_sym);
830 struct statement *tmp;
831 int found = 0;
833 if (!option_info)
834 return;
835 if (last_goto_statement_handled)
836 return;
837 last_goto_statement_handled = 1;
839 if (!goto_stmt->goto_label ||
840 goto_stmt->goto_label->type != SYM_LABEL ||
841 !goto_stmt->goto_label->ident)
842 return;
843 goto_name = goto_stmt->goto_label->ident->name;
845 func_stmt = base_type->stmt;
846 if (!func_stmt)
847 func_stmt = base_type->inline_stmt;
848 if (!func_stmt)
849 return;
850 if (func_stmt->type != STMT_COMPOUND)
851 return;
853 FOR_EACH_PTR(func_stmt->stmts, tmp) {
854 if (!found) {
855 if (tmp->type != STMT_LABEL)
856 continue;
857 if (!tmp->label_identifier ||
858 tmp->label_identifier->type != SYM_LABEL ||
859 !tmp->label_identifier->ident)
860 continue;
861 label_name = tmp->label_identifier->ident->name;
862 if (strcmp(goto_name, label_name) != 0)
863 continue;
864 found = 1;
866 __split_stmt(tmp);
867 } END_FOR_EACH_PTR(tmp);
870 static void fake_a_return(void)
872 struct symbol *return_type;
874 nullify_path();
875 __unnullify_path();
877 return_type = get_real_base_type(cur_func_sym);
878 return_type = get_real_base_type(return_type);
879 if (return_type != &void_ctype) {
880 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
881 nullify_path();
885 static void fake_an_empty_default(struct position pos)
887 static struct statement none = {};
889 none.pos = pos;
890 none.type = STMT_NONE;
891 __merge_switches(top_expression(switch_expr_stack), NULL);
892 __split_stmt(&none);
895 static void split_compound(struct statement *stmt)
897 struct statement *prev = NULL;
898 struct statement *cur = NULL;
899 struct statement *next;
901 __push_scope_hooks();
903 FOR_EACH_PTR(stmt->stmts, next) {
904 /* just set them all ahead of time */
905 set_parent_stmt(next, stmt);
907 if (cur) {
908 __prev_stmt = prev;
909 __next_stmt = next;
910 __cur_stmt = cur;
911 __split_stmt(cur);
913 prev = cur;
914 cur = next;
915 } END_FOR_EACH_PTR(next);
916 if (cur) {
917 __prev_stmt = prev;
918 __cur_stmt = cur;
919 __next_stmt = NULL;
920 __split_stmt(cur);
924 * For function scope, then delay calling the scope hooks until the
925 * end of function hooks can run. I'm not positive this is the right
926 * thing...
928 if (!is_last_stmt(cur))
929 __call_scope_hooks();
933 * This is a hack, work around for detecting empty functions.
935 static int need_delayed_scope_hooks(void)
937 struct symbol *fn = get_base_type(cur_func_sym);
938 struct statement *stmt;
940 if (!fn)
941 return 0;
942 stmt = fn->stmt;
943 if (!stmt)
944 stmt = fn->inline_stmt;
945 if (stmt && stmt->type == STMT_COMPOUND)
946 return 1;
947 return 0;
950 void __split_label_stmt(struct statement *stmt)
952 if (stmt->label_identifier &&
953 stmt->label_identifier->type == SYM_LABEL &&
954 stmt->label_identifier->ident) {
955 loop_count |= 0x80000000;
956 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
960 static void find_asm_gotos(struct statement *stmt)
962 struct symbol *sym;
964 FOR_EACH_PTR(stmt->asm_labels, sym) {
965 __save_gotos(sym->ident->name, sym);
966 } END_FOR_EACH_PTR(sym);
969 void __split_stmt(struct statement *stmt)
971 sval_t sval;
973 if (!stmt)
974 goto out;
976 if (__bail_on_rest_of_function)
977 return;
979 if (out_of_memory() || taking_too_long()) {
981 __bail_on_rest_of_function = 1;
982 final_pass = 1;
983 sm_msg("Function too hairy. Giving up.");
984 fake_a_return();
985 final_pass = 0; /* turn off sm_msg() from here */
986 return;
989 add_ptr_list(&big_statement_stack, stmt);
990 free_expression_stack(&big_expression_stack);
991 set_position(stmt->pos);
992 __pass_to_client(stmt, STMT_HOOK);
994 switch (stmt->type) {
995 case STMT_DECLARATION:
996 split_declaration(stmt->declaration);
997 break;
998 case STMT_RETURN:
999 __split_expr(stmt->ret_value);
1000 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1001 __process_post_op_stack();
1002 nullify_path();
1003 break;
1004 case STMT_EXPRESSION:
1005 __split_expr(stmt->expression);
1006 break;
1007 case STMT_COMPOUND:
1008 split_compound(stmt);
1009 break;
1010 case STMT_IF:
1011 set_parent_stmt(stmt->if_true, stmt);
1012 set_parent_stmt(stmt->if_false, stmt);
1014 if (known_condition_true(stmt->if_conditional)) {
1015 __split_stmt(stmt->if_true);
1016 break;
1018 if (known_condition_false(stmt->if_conditional)) {
1019 __split_stmt(stmt->if_false);
1020 break;
1022 __split_whole_condition(stmt->if_conditional);
1023 __split_stmt(stmt->if_true);
1024 if (empty_statement(stmt->if_true) &&
1025 last_stmt_on_same_line() &&
1026 !get_macro_name(stmt->if_true->pos))
1027 sm_msg("warn: if();");
1028 __push_true_states();
1029 __use_false_states();
1030 __split_stmt(stmt->if_false);
1031 __merge_true_states();
1032 break;
1033 case STMT_ITERATOR:
1034 set_parent_stmt(stmt->iterator_pre_statement, stmt);
1035 set_parent_stmt(stmt->iterator_statement, stmt);
1036 set_parent_stmt(stmt->iterator_post_statement, stmt);
1038 if (stmt->iterator_pre_condition)
1039 handle_pre_loop(stmt);
1040 else if (stmt->iterator_post_condition)
1041 handle_post_loop(stmt);
1042 else {
1043 // these are for(;;) type loops.
1044 handle_pre_loop(stmt);
1046 break;
1047 case STMT_SWITCH:
1048 set_parent_stmt(stmt->switch_statement, stmt);
1050 if (get_value(stmt->switch_expression, &sval)) {
1051 split_known_switch(stmt, sval);
1052 break;
1054 __split_expr(stmt->switch_expression);
1055 push_expression(&switch_expr_stack, stmt->switch_expression);
1056 __save_switch_states(top_expression(switch_expr_stack));
1057 nullify_path();
1058 __push_default();
1059 __push_breaks();
1060 __split_stmt(stmt->switch_statement);
1061 if (!__pop_default())
1062 fake_an_empty_default(stmt->pos);
1063 __discard_switches();
1064 __merge_breaks();
1065 pop_expression(&switch_expr_stack);
1066 break;
1067 case STMT_CASE:
1068 split_case(stmt);
1069 break;
1070 case STMT_LABEL:
1071 __split_label_stmt(stmt);
1072 __split_stmt(stmt->label_statement);
1073 break;
1074 case STMT_GOTO:
1075 __split_expr(stmt->goto_expression);
1076 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1077 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1078 __process_breaks();
1079 } else if (!strcmp(stmt->goto_label->ident->name,
1080 "continue")) {
1081 __process_continues();
1083 } else if (stmt->goto_label &&
1084 stmt->goto_label->type == SYM_LABEL &&
1085 stmt->goto_label->ident) {
1086 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1088 nullify_path();
1089 if (is_last_stmt(stmt))
1090 handle_backward_goto(stmt);
1091 break;
1092 case STMT_NONE:
1093 break;
1094 case STMT_ASM:
1095 find_asm_gotos(stmt);
1096 __pass_to_client(stmt, ASM_HOOK);
1097 __split_expr(stmt->asm_string);
1098 split_asm_constraints(stmt->asm_outputs);
1099 split_asm_constraints(stmt->asm_inputs);
1100 split_asm_constraints(stmt->asm_clobbers);
1101 break;
1102 case STMT_CONTEXT:
1103 break;
1104 case STMT_RANGE:
1105 __split_expr(stmt->range_expression);
1106 __split_expr(stmt->range_low);
1107 __split_expr(stmt->range_high);
1108 break;
1110 __pass_to_client(stmt, STMT_HOOK_AFTER);
1111 out:
1112 __process_post_op_stack();
1115 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1117 struct expression *expr;
1119 FOR_EACH_PTR(expr_list, expr) {
1120 set_parent_expr(expr, parent);
1121 __split_expr(expr);
1122 __process_post_op_stack();
1123 } END_FOR_EACH_PTR(expr);
1126 static void split_sym(struct symbol *sym)
1128 if (!sym)
1129 return;
1130 if (!(sym->namespace & NS_SYMBOL))
1131 return;
1133 __split_stmt(sym->stmt);
1134 __split_expr(sym->array_size);
1135 split_symlist(sym->arguments);
1136 split_symlist(sym->symbol_list);
1137 __split_stmt(sym->inline_stmt);
1138 split_symlist(sym->inline_symbol_list);
1141 static void split_symlist(struct symbol_list *sym_list)
1143 struct symbol *sym;
1145 FOR_EACH_PTR(sym_list, sym) {
1146 split_sym(sym);
1147 } END_FOR_EACH_PTR(sym);
1150 typedef void (fake_cb)(struct expression *expr);
1152 static int member_to_number(struct expression *expr, struct ident *member)
1154 struct symbol *type, *tmp;
1155 char *name;
1156 int i;
1158 if (!member)
1159 return -1;
1160 name = member->name;
1162 type = get_type(expr);
1163 if (!type || type->type != SYM_STRUCT)
1164 return -1;
1166 i = -1;
1167 FOR_EACH_PTR(type->symbol_list, tmp) {
1168 i++;
1169 if (!tmp->ident)
1170 continue;
1171 if (strcmp(name, tmp->ident->name) == 0)
1172 return i;
1173 } END_FOR_EACH_PTR(tmp);
1174 return -1;
1177 static struct ident *number_to_member(struct expression *expr, int num)
1179 struct symbol *type, *member;
1180 int i = 0;
1182 type = get_type(expr);
1183 if (!type || type->type != SYM_STRUCT)
1184 return NULL;
1186 FOR_EACH_PTR(type->symbol_list, member) {
1187 if (i == num)
1188 return member->ident;
1189 i++;
1190 } END_FOR_EACH_PTR(member);
1191 return NULL;
1194 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1196 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1198 struct expression *edge_member, *assign;
1199 struct symbol *base = get_real_base_type(member);
1200 struct symbol *tmp;
1202 if (member->ident)
1203 expr = member_expression(expr, '.', member->ident);
1205 FOR_EACH_PTR(base->symbol_list, tmp) {
1206 struct symbol *type;
1208 type = get_real_base_type(tmp);
1209 if (!type)
1210 continue;
1212 edge_member = member_expression(expr, '.', tmp->ident);
1213 if (get_state_expr(SMATCH_EXTRA, edge_member))
1214 continue;
1216 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1217 set_inner_struct_members(expr, tmp);
1218 continue;
1221 if (!tmp->ident)
1222 continue;
1224 assign = assign_expression(edge_member, zero_expr());
1225 __split_expr(assign);
1226 } END_FOR_EACH_PTR(tmp);
1231 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1233 struct symbol *tmp;
1234 struct expression *member = NULL;
1235 struct expression *assign;
1236 int op = '*';
1238 if (expr->type == EXPR_PREOP && expr->op == '&') {
1239 expr = strip_expr(expr->unop);
1240 op = '.';
1243 FOR_EACH_PTR(type->symbol_list, tmp) {
1244 type = get_real_base_type(tmp);
1245 if (!type)
1246 continue;
1248 if (tmp->ident) {
1249 member = member_expression(expr, op, tmp->ident);
1250 if (get_state_expr(SMATCH_EXTRA, member))
1251 continue;
1254 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1255 set_inner_struct_members(expr, tmp);
1256 continue;
1258 if (type->type == SYM_ARRAY)
1259 continue;
1260 if (!tmp->ident)
1261 continue;
1263 assign = assign_expression(member, zero_expr());
1264 __split_expr(assign);
1265 } END_FOR_EACH_PTR(tmp);
1268 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1270 struct expression *deref, *assign, *tmp, *right;
1271 struct symbol *struct_type, *type;
1272 struct ident *member;
1273 int member_idx;
1275 struct_type = get_type(symbol);
1276 if (!struct_type ||
1277 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1278 return;
1281 * We're parsing an initializer that could look something like this:
1282 * struct foo foo = {
1283 * 42,
1284 * .whatever.xxx = 11,
1285 * .zzz = 12,
1286 * };
1288 * So what we have here is a list with 42, .whatever, and .zzz. We need
1289 * to break it up into left and right sides of the assignments.
1292 member_idx = 0;
1293 FOR_EACH_PTR(members, tmp) {
1294 deref = NULL;
1295 if (tmp->type == EXPR_IDENTIFIER) {
1296 member_idx = member_to_number(symbol, tmp->expr_ident);
1297 while (tmp->type == EXPR_IDENTIFIER) {
1298 member = tmp->expr_ident;
1299 tmp = tmp->ident_expression;
1300 if (deref)
1301 deref = member_expression(deref, '.', member);
1302 else
1303 deref = member_expression(symbol, '.', member);
1305 } else {
1306 member = number_to_member(symbol, member_idx);
1307 deref = member_expression(symbol, '.', member);
1309 right = tmp;
1310 member_idx++;
1311 if (right->type == EXPR_INITIALIZER) {
1312 type = get_type(deref);
1313 if (type && type->type == SYM_ARRAY)
1314 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1315 else
1316 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1317 } else {
1318 assign = assign_expression(deref, right);
1319 fake_cb(assign);
1321 } END_FOR_EACH_PTR(tmp);
1323 set_unset_to_zero(struct_type, symbol);
1326 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1328 fake_member_assigns_helper(symbol_expression(sym),
1329 sym->initializer->expr_list, fake_cb);
1332 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1334 struct expression *offset, *binop, *assign, *tmp;
1335 struct symbol *type;
1336 int idx;
1338 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1339 return;
1341 idx = 0;
1342 FOR_EACH_PTR(expr_list, tmp) {
1343 if (tmp->type == EXPR_INDEX) {
1344 if (tmp->idx_from != tmp->idx_to)
1345 return;
1346 idx = tmp->idx_from;
1347 if (!tmp->idx_expression)
1348 goto next;
1349 tmp = tmp->idx_expression;
1351 offset = value_expr(idx);
1352 binop = array_element_expression(array, offset);
1353 if (tmp->type == EXPR_INITIALIZER) {
1354 type = get_type(binop);
1355 if (type && type->type == SYM_ARRAY)
1356 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1357 else
1358 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1359 } else {
1360 assign = assign_expression(binop, tmp);
1361 fake_cb(assign);
1363 next:
1364 idx++;
1365 } END_FOR_EACH_PTR(tmp);
1368 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1370 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1373 static void fake_assign_expr(struct symbol *sym)
1375 struct expression *assign, *symbol;
1377 symbol = symbol_expression(sym);
1378 assign = assign_expression(symbol, sym->initializer);
1379 __split_expr(assign);
1382 static void call_split_expr(struct expression *expr)
1384 __split_expr(expr);
1387 static void do_initializer_stuff(struct symbol *sym)
1389 if (!sym->initializer)
1390 return;
1392 if (sym->initializer->type == EXPR_INITIALIZER) {
1393 if (get_real_base_type(sym)->type == SYM_ARRAY)
1394 fake_element_assigns(sym, call_split_expr);
1395 else
1396 fake_member_assigns(sym, call_split_expr);
1397 } else {
1398 fake_assign_expr(sym);
1402 static void split_declaration(struct symbol_list *sym_list)
1404 struct symbol *sym;
1406 FOR_EACH_PTR(sym_list, sym) {
1407 __pass_to_client(sym, DECLARATION_HOOK);
1408 do_initializer_stuff(sym);
1409 split_sym(sym);
1410 } END_FOR_EACH_PTR(sym);
1413 static void call_global_assign_hooks(struct expression *assign)
1415 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1418 static void fake_global_assign(struct symbol *sym)
1420 struct expression *assign, *symbol;
1422 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1423 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1424 fake_element_assigns(sym, call_global_assign_hooks);
1425 } else if (sym->initializer) {
1426 symbol = symbol_expression(sym);
1427 assign = assign_expression(symbol, sym->initializer);
1428 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1429 } else {
1430 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1432 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1433 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1434 fake_member_assigns(sym, call_global_assign_hooks);
1435 } else if (sym->initializer) {
1436 symbol = symbol_expression(sym);
1437 assign = assign_expression(symbol, sym->initializer);
1438 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1439 } else {
1440 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1442 } else {
1443 symbol = symbol_expression(sym);
1444 if (sym->initializer)
1445 assign = assign_expression(symbol, sym->initializer);
1446 else
1447 assign = assign_expression(symbol, zero_expr());
1448 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1452 static void start_function_definition(struct symbol *sym)
1454 __in_function_def = 1;
1455 __pass_to_client(sym, FUNC_DEF_HOOK);
1456 __in_function_def = 0;
1457 __pass_to_client(sym, AFTER_DEF_HOOK);
1461 static void split_function(struct symbol *sym)
1463 struct symbol *base_type = get_base_type(sym);
1465 if (!base_type->stmt && !base_type->inline_stmt)
1466 return;
1468 gettimeofday(&fn_start_time, NULL);
1469 cur_func_sym = sym;
1470 if (sym->ident)
1471 cur_func = sym->ident->name;
1472 set_position(sym->pos);
1473 loop_count = 0;
1474 last_goto_statement_handled = 0;
1475 sm_debug("new function: %s\n", cur_func);
1476 __stree_id = 0;
1477 if (option_two_passes) {
1478 __unnullify_path();
1479 loop_num = 0;
1480 final_pass = 0;
1481 start_function_definition(sym);
1482 __split_stmt(base_type->stmt);
1483 __split_stmt(base_type->inline_stmt);
1484 nullify_path();
1486 __unnullify_path();
1487 loop_num = 0;
1488 final_pass = 1;
1489 start_function_definition(sym);
1490 __split_stmt(base_type->stmt);
1491 __split_stmt(base_type->inline_stmt);
1492 __pass_to_client(sym, END_FUNC_HOOK);
1493 if (need_delayed_scope_hooks())
1494 __call_scope_hooks();
1495 __pass_to_client(sym, AFTER_FUNC_HOOK);
1497 clear_all_states();
1498 cur_func_sym = NULL;
1499 cur_func = NULL;
1500 free_data_info_allocs();
1501 free_expression_stack(&switch_expr_stack);
1502 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1503 __bail_on_rest_of_function = 0;
1506 static void parse_inline(struct expression *call)
1508 struct symbol *base_type;
1509 int loop_num_bak = loop_num;
1510 int loop_count_bak = loop_count;
1511 int final_pass_bak = final_pass;
1512 char *cur_func_bak = cur_func;
1513 struct statement_list *big_statement_stack_bak = big_statement_stack;
1514 struct expression_list *big_expression_stack_bak = big_expression_stack;
1515 struct expression_list *big_condition_stack_bak = big_condition_stack;
1516 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1517 struct symbol *cur_func_sym_bak = cur_func_sym;
1519 __pass_to_client(call, INLINE_FN_START);
1520 final_pass = 0; /* don't print anything */
1521 __inline_fn = call;
1523 base_type = get_base_type(call->fn->symbol);
1524 cur_func_sym = call->fn->symbol;
1525 if (call->fn->symbol->ident)
1526 cur_func = call->fn->symbol->ident->name;
1527 else
1528 cur_func = NULL;
1529 set_position(call->fn->symbol->pos);
1531 save_all_states();
1532 big_statement_stack = NULL;
1533 big_expression_stack = NULL;
1534 big_condition_stack = NULL;
1535 switch_expr_stack = NULL;
1537 sm_debug("inline function: %s\n", cur_func);
1538 __unnullify_path();
1539 loop_num = 0;
1540 start_function_definition(call->fn->symbol);
1541 __split_stmt(base_type->stmt);
1542 __split_stmt(base_type->inline_stmt);
1543 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1544 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1546 free_expression_stack(&switch_expr_stack);
1547 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1548 nullify_path();
1549 free_goto_stack();
1551 loop_num = loop_num_bak;
1552 loop_count = loop_count_bak;
1553 final_pass = final_pass_bak;
1554 cur_func_sym = cur_func_sym_bak;
1555 cur_func = cur_func_bak;
1556 big_statement_stack = big_statement_stack_bak;
1557 big_expression_stack = big_expression_stack_bak;
1558 big_condition_stack = big_condition_stack_bak;
1559 switch_expr_stack = switch_expr_stack_bak;
1561 restore_all_states();
1562 set_position(call->pos);
1563 __inline_fn = NULL;
1564 __pass_to_client(call, INLINE_FN_END);
1567 static struct symbol_list *inlines_called;
1568 static void add_inline_function(struct symbol *sym)
1570 static struct symbol_list *already_added;
1571 struct symbol *tmp;
1573 FOR_EACH_PTR(already_added, tmp) {
1574 if (tmp == sym)
1575 return;
1576 } END_FOR_EACH_PTR(tmp);
1578 add_ptr_list(&already_added, sym);
1579 add_ptr_list(&inlines_called, sym);
1582 static void process_inlines(void)
1584 struct symbol *tmp;
1586 FOR_EACH_PTR(inlines_called, tmp) {
1587 split_function(tmp);
1588 } END_FOR_EACH_PTR(tmp);
1589 free_ptr_list(&inlines_called);
1592 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1594 struct symbol *sym;
1596 FOR_EACH_PTR_REVERSE(big_list, sym) {
1597 if (!sym->scope)
1598 continue;
1599 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1600 return sym;
1601 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1602 return sym;
1603 } END_FOR_EACH_PTR_REVERSE(sym);
1605 return NULL;
1608 static void split_inlines_in_scope(struct symbol *sym)
1610 struct symbol *base;
1611 struct symbol_list *scope_list;
1612 int stream;
1614 scope_list = sym->scope->symbols;
1615 stream = sym->pos.stream;
1617 /* find the last static symbol in the file */
1618 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1619 if (sym->pos.stream != stream)
1620 continue;
1621 if (sym->type != SYM_NODE)
1622 continue;
1623 base = get_base_type(sym);
1624 if (!base)
1625 continue;
1626 if (base->type != SYM_FN)
1627 continue;
1628 if (!base->inline_stmt)
1629 continue;
1630 add_inline_function(sym);
1631 } END_FOR_EACH_PTR_REVERSE(sym);
1633 process_inlines();
1636 static void split_inlines(struct symbol_list *sym_list)
1638 struct symbol *sym;
1640 sym = get_last_scoped_symbol(sym_list, 0);
1641 if (sym)
1642 split_inlines_in_scope(sym);
1643 sym = get_last_scoped_symbol(sym_list, 1);
1644 if (sym)
1645 split_inlines_in_scope(sym);
1648 static struct stree *clone_estates_perm(struct stree *orig)
1650 struct stree *ret = NULL;
1651 struct sm_state *tmp;
1653 FOR_EACH_SM(orig, tmp) {
1654 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1655 } END_FOR_EACH_SM(tmp);
1657 return ret;
1660 struct position last_pos;
1661 static void split_functions(struct symbol_list *sym_list)
1663 struct symbol *sym;
1665 __unnullify_path();
1666 FOR_EACH_PTR(sym_list, sym) {
1667 set_position(sym->pos);
1668 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1669 __pass_to_client(sym, BASE_HOOK);
1670 fake_global_assign(sym);
1672 } END_FOR_EACH_PTR(sym);
1673 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1674 nullify_path();
1676 FOR_EACH_PTR(sym_list, sym) {
1677 set_position(sym->pos);
1678 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1679 split_function(sym);
1680 process_inlines();
1682 last_pos = sym->pos;
1683 } END_FOR_EACH_PTR(sym);
1684 split_inlines(sym_list);
1685 __pass_to_client(sym_list, END_FILE_HOOK);
1688 static int final_before_fake;
1689 void init_fake_env(void)
1691 if (!in_fake_env)
1692 final_before_fake = final_pass;
1693 in_fake_env++;
1694 __push_fake_cur_stree();
1695 final_pass = 0;
1698 void end_fake_env(void)
1700 __push_fake_cur_stree();
1701 in_fake_env--;
1702 if (!in_fake_env)
1703 final_pass = final_before_fake;
1706 void smatch(int argc, char **argv)
1708 struct string_list *filelist = NULL;
1709 struct symbol_list *sym_list;
1710 struct timeval stop, start;
1712 gettimeofday(&start, NULL);
1714 if (argc < 2) {
1715 printf("Usage: smatch [--debug] <filename.c>\n");
1716 exit(1);
1718 sparse_initialize(argc, argv, &filelist);
1719 set_valid_ptr_max();
1720 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1721 if (option_file_output) {
1722 char buf[256];
1724 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1725 sm_outfd = fopen(buf, "w");
1726 if (!sm_outfd) {
1727 printf("Error: Cannot open %s\n", base_file);
1728 exit(1);
1731 sym_list = sparse_keep_tokens(base_file);
1732 split_functions(sym_list);
1733 } END_FOR_EACH_PTR_NOTAG(base_file);
1735 gettimeofday(&stop, NULL);
1737 set_position(last_pos);
1738 if (option_time)
1739 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);