symbol.h: let is_ptr_type() take NULL pointers
[smatch.git] / smatch_flow.c
blob4dbb47d543eddbbd9bed7400ab1d12f0e9ec49c7
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 int __in_fake_struct_assign;
30 static int in_fake_env;
31 int final_pass;
32 int __inline_call;
33 struct expression *__inline_fn;
35 static int __smatch_lineno = 0;
37 static char *base_file;
38 static const char *filename;
39 static char *pathname;
40 static char *full_filename;
41 static char *cur_func;
42 static unsigned int loop_count;
43 static int last_goto_statement_handled;
44 int __expr_stmt_count;
45 int __in_function_def;
46 static struct expression_list *switch_expr_stack = NULL;
47 static struct expression_list *post_op_stack = NULL;
49 static struct ptr_list *backup;
51 struct expression_list *big_expression_stack;
52 struct statement_list *big_statement_stack;
53 struct statement *__prev_stmt;
54 struct statement *__cur_stmt;
55 struct statement *__next_stmt;
56 int __in_pre_condition = 0;
57 int __bail_on_rest_of_function = 0;
58 static struct timeval fn_start_time;
59 char *get_function(void) { return cur_func; }
60 int get_lineno(void) { return __smatch_lineno; }
61 int inside_loop(void) { return !!loop_count; }
62 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
63 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
64 int in_expression_statement(void) { return !!__expr_stmt_count; }
66 static void split_symlist(struct symbol_list *sym_list);
67 static void split_declaration(struct symbol_list *sym_list);
68 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
69 static void add_inline_function(struct symbol *sym);
70 static void parse_inline(struct expression *expr);
72 int option_assume_loops = 0;
73 int option_two_passes = 0;
74 struct symbol *cur_func_sym = NULL;
75 struct stree *global_states;
77 long long valid_ptr_min = 4096;
78 long long valid_ptr_max = 2117777777;
79 sval_t valid_ptr_min_sval = {
80 .type = &ptr_ctype,
81 {.value = 4096},
83 sval_t valid_ptr_max_sval = {
84 .type = &ptr_ctype,
85 {.value = LONG_MAX - 100000},
88 static void set_valid_ptr_max(void)
90 if (type_bits(&ptr_ctype) == 32)
91 valid_ptr_max = 2117777777;
92 else if (type_bits(&ptr_ctype) == 64)
93 valid_ptr_max = 2117777777777777777LL;
95 valid_ptr_max_sval.value = valid_ptr_max;
98 int outside_of_function(void)
100 return cur_func_sym == NULL;
103 const char *get_filename(void)
105 if (option_info)
106 return base_file;
107 if (option_full_path)
108 return full_filename;
109 return filename;
112 const char *get_base_file(void)
114 return base_file;
117 static void set_position(struct position pos)
119 int len;
120 static int prev_stream = -1;
122 if (in_fake_env)
123 return;
125 if (pos.stream == 0 && pos.line == 0)
126 return;
128 __smatch_lineno = pos.line;
130 if (pos.stream == prev_stream)
131 return;
133 filename = stream_name(pos.stream);
135 free(full_filename);
136 pathname = getcwd(NULL, 0);
137 if (pathname) {
138 len = strlen(pathname) + 1 + strlen(filename) + 1;
139 full_filename = malloc(len);
140 snprintf(full_filename, len, "%s/%s", pathname, filename);
141 } else {
142 full_filename = alloc_string(filename);
144 free(pathname);
147 int is_assigned_call(struct expression *expr)
149 struct expression *parent = expr_get_parent_expr(expr);
151 if (parent &&
152 parent->type == EXPR_ASSIGNMENT &&
153 parent->op == '=' &&
154 strip_expr(parent->right) == expr)
155 return 1;
157 return 0;
160 static int is_inline_func(struct expression *expr)
162 if (expr->type != EXPR_SYMBOL || !expr->symbol)
163 return 0;
164 if (expr->symbol->ctype.modifiers & MOD_INLINE)
165 return 1;
166 return 0;
169 static int is_noreturn_func(struct expression *expr)
171 if (expr->type != EXPR_SYMBOL || !expr->symbol)
172 return 0;
173 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
174 return 1;
175 return 0;
178 int inlinable(struct expression *expr)
180 struct symbol *sym;
181 struct statement *last_stmt = NULL;
183 if (__inline_fn) /* don't nest */
184 return 0;
186 if (expr->type != EXPR_SYMBOL || !expr->symbol)
187 return 0;
188 if (is_no_inline_function(expr->symbol->ident->name))
189 return 0;
190 sym = get_base_type(expr->symbol);
191 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
192 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
193 return 0;
194 if (sym->stmt->type != STMT_COMPOUND)
195 return 0;
196 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
198 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
199 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
200 return 0;
201 if (sym->inline_stmt->type != STMT_COMPOUND)
202 return 0;
203 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
206 if (!last_stmt)
207 return 0;
209 /* the magic numbers in this function are pulled out of my bum. */
210 if (last_stmt->pos.line > sym->pos.line + 20)
211 return 0;
213 return 1;
216 void __process_post_op_stack(void)
218 struct expression *expr;
220 FOR_EACH_PTR(post_op_stack, expr) {
221 __pass_to_client(expr, OP_HOOK);
222 } END_FOR_EACH_PTR(expr);
224 __free_ptr_list((struct ptr_list **)&post_op_stack);
227 static int handle_comma_assigns(struct expression *expr)
229 struct expression *right;
230 struct expression *assign;
232 right = strip_expr(expr->right);
233 if (right->type != EXPR_COMMA)
234 return 0;
236 __split_expr(right->left);
237 __process_post_op_stack();
239 assign = assign_expression(expr->left, right->right);
240 __split_expr(assign);
242 return 1;
245 /* This is to handle *p++ = foo; assignments */
246 static int handle_postop_assigns(struct expression *expr)
248 struct expression *left, *fake_left;
249 struct expression *assign;
251 left = strip_expr(expr->left);
252 if (left->type != EXPR_PREOP || left->op != '*')
253 return 0;
254 left = strip_expr(left->unop);
255 if (left->type != EXPR_POSTOP)
256 return 0;
258 fake_left = deref_expression(strip_expr(left->unop));
259 assign = assign_expression(fake_left, expr->right);
261 __split_expr(assign);
262 __split_expr(expr->left);
264 return 1;
267 static int prev_expression_is_getting_address(struct expression *expr)
269 struct expression *parent;
271 do {
272 parent = expr_get_parent_expr(expr);
274 if (!parent)
275 return 0;
276 if (parent->type == EXPR_PREOP && parent->op == '&')
277 return 1;
278 if (parent->type == EXPR_PREOP && parent->op == '(')
279 goto next;
280 if (parent->type == EXPR_DEREF && parent->op == '.')
281 goto next;
283 return 0;
284 next:
285 expr = parent;
286 } while (1);
289 void __split_expr(struct expression *expr)
291 if (!expr)
292 return;
294 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
296 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
297 return;
298 if (__in_fake_assign >= 4) /* don't allow too much nesting */
299 return;
301 push_expression(&big_expression_stack, expr);
302 set_position(expr->pos);
303 __pass_to_client(expr, EXPR_HOOK);
305 switch (expr->type) {
306 case EXPR_PREOP:
307 expr_set_parent_expr(expr->unop, expr);
309 if (expr->op == '*' &&
310 !prev_expression_is_getting_address(expr))
311 __pass_to_client(expr, DEREF_HOOK);
312 __split_expr(expr->unop);
313 __pass_to_client(expr, OP_HOOK);
314 break;
315 case EXPR_POSTOP:
316 expr_set_parent_expr(expr->unop, expr);
318 __split_expr(expr->unop);
319 push_expression(&post_op_stack, expr);
320 break;
321 case EXPR_STATEMENT:
322 __expr_stmt_count++;
323 if (expr->statement && !expr->statement) {
324 stmt_set_parent_stmt(expr->statement,
325 last_ptr_list((struct ptr_list *)big_statement_stack));
327 __split_stmt(expr->statement);
328 __expr_stmt_count--;
329 break;
330 case EXPR_LOGICAL:
331 case EXPR_COMPARE:
332 expr_set_parent_expr(expr->left, expr);
333 expr_set_parent_expr(expr->right, expr);
335 __pass_to_client(expr, LOGIC_HOOK);
336 __handle_logic(expr);
337 break;
338 case EXPR_BINOP:
339 expr_set_parent_expr(expr->left, expr);
340 expr_set_parent_expr(expr->right, expr);
342 __pass_to_client(expr, BINOP_HOOK);
343 case EXPR_COMMA:
344 expr_set_parent_expr(expr->left, expr);
345 expr_set_parent_expr(expr->right, expr);
347 __split_expr(expr->left);
348 __process_post_op_stack();
349 __split_expr(expr->right);
350 break;
351 case EXPR_ASSIGNMENT: {
352 struct expression *right;
354 expr_set_parent_expr(expr->left, expr);
355 expr_set_parent_expr(expr->right, expr);
357 right = strip_expr(expr->right);
358 if (!right)
359 break;
361 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
363 /* foo = !bar() */
364 if (__handle_condition_assigns(expr))
365 break;
366 /* foo = (x < 5 ? foo : 5); */
367 if (__handle_select_assigns(expr))
368 break;
369 /* foo = ({frob(); frob(); frob(); 1;}) */
370 if (__handle_expr_statement_assigns(expr))
371 break;
372 /* foo = (3, 4); */
373 if (handle_comma_assigns(expr))
374 break;
375 if (handle_postop_assigns(expr))
376 break;
378 __split_expr(expr->right);
379 if (outside_of_function())
380 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
381 else
382 __pass_to_client(expr, ASSIGNMENT_HOOK);
384 __fake_struct_member_assignments(expr);
386 if (expr->op == '=' && right->type == EXPR_CALL)
387 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
389 if (get_macro_name(right->pos) &&
390 get_macro_name(expr->pos) != get_macro_name(right->pos))
391 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
393 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
395 __split_expr(expr->left);
396 break;
398 case EXPR_DEREF:
399 expr_set_parent_expr(expr->deref, expr);
401 __pass_to_client(expr, DEREF_HOOK);
402 __split_expr(expr->deref);
403 break;
404 case EXPR_SLICE:
405 expr_set_parent_expr(expr->base, expr);
407 __split_expr(expr->base);
408 break;
409 case EXPR_CAST:
410 case EXPR_FORCE_CAST:
411 expr_set_parent_expr(expr->cast_expression, expr);
413 __pass_to_client(expr, CAST_HOOK);
414 __split_expr(expr->cast_expression);
415 break;
416 case EXPR_SIZEOF:
417 if (expr->cast_expression)
418 __pass_to_client(strip_parens(expr->cast_expression),
419 SIZEOF_HOOK);
420 break;
421 case EXPR_OFFSETOF:
422 case EXPR_ALIGNOF:
423 evaluate_expression(expr);
424 break;
425 case EXPR_CONDITIONAL:
426 case EXPR_SELECT:
427 expr_set_parent_expr(expr->conditional, expr);
428 expr_set_parent_expr(expr->cond_true, expr);
429 expr_set_parent_expr(expr->cond_false, expr);
431 if (known_condition_true(expr->conditional)) {
432 __split_expr(expr->cond_true);
433 break;
435 if (known_condition_false(expr->conditional)) {
436 __split_expr(expr->cond_false);
437 break;
439 __pass_to_client(expr, SELECT_HOOK);
440 __split_whole_condition(expr->conditional);
441 __split_expr(expr->cond_true);
442 __push_true_states();
443 __use_false_states();
444 __split_expr(expr->cond_false);
445 __merge_true_states();
446 break;
447 case EXPR_CALL:
448 expr_set_parent_expr(expr->fn, expr);
450 if (sym_name_is("__builtin_constant_p", expr->fn))
451 break;
452 split_expr_list(expr->args, expr);
453 __split_expr(expr->fn);
454 if (is_inline_func(expr->fn))
455 add_inline_function(expr->fn->symbol);
456 if (inlinable(expr->fn))
457 __inline_call = 1;
458 __process_post_op_stack();
459 __pass_to_client(expr, FUNCTION_CALL_HOOK);
460 __inline_call = 0;
461 if (inlinable(expr->fn)) {
462 parse_inline(expr);
464 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
465 if (is_noreturn_func(expr->fn))
466 nullify_path();
467 break;
468 case EXPR_INITIALIZER:
469 split_expr_list(expr->expr_list, expr);
470 break;
471 case EXPR_IDENTIFIER:
472 expr_set_parent_expr(expr->ident_expression, expr);
473 __split_expr(expr->ident_expression);
474 break;
475 case EXPR_INDEX:
476 expr_set_parent_expr(expr->idx_expression, expr);
477 __split_expr(expr->idx_expression);
478 break;
479 case EXPR_POS:
480 expr_set_parent_expr(expr->init_expr, expr);
481 __split_expr(expr->init_expr);
482 break;
483 case EXPR_SYMBOL:
484 __pass_to_client(expr, SYM_HOOK);
485 break;
486 case EXPR_STRING:
487 __pass_to_client(expr, STRING_HOOK);
488 break;
489 default:
490 break;
492 pop_expression(&big_expression_stack);
495 static int is_forever_loop(struct statement *stmt)
497 struct expression *expr;
498 sval_t sval;
500 expr = strip_expr(stmt->iterator_pre_condition);
501 if (!expr)
502 expr = stmt->iterator_post_condition;
503 if (!expr) {
504 /* this is a for(;;) loop... */
505 return 1;
508 if (get_value(expr, &sval) && sval.value != 0)
509 return 1;
511 return 0;
514 static int loop_num;
515 static char *get_loop_name(int num)
517 char buf[256];
519 snprintf(buf, 255, "-loop%d", num);
520 buf[255] = '\0';
521 return alloc_sname(buf);
525 * Pre Loops are while and for loops.
527 static void handle_pre_loop(struct statement *stmt)
529 int once_through; /* we go through the loop at least once */
530 struct sm_state *extra_sm = NULL;
531 int unchanged = 0;
532 char *loop_name;
533 struct stree *stree = NULL;
534 struct sm_state *sm = NULL;
536 loop_name = get_loop_name(loop_num);
537 loop_num++;
539 __split_stmt(stmt->iterator_pre_statement);
540 __prev_stmt = stmt->iterator_pre_statement;
542 once_through = implied_condition_true(stmt->iterator_pre_condition);
544 loop_count++;
545 __push_continues();
546 __push_breaks();
548 __merge_gotos(loop_name, NULL);
550 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
551 __in_pre_condition++;
552 __pass_to_client(stmt, PRELOOP_HOOK);
553 __split_whole_condition(stmt->iterator_pre_condition);
554 __in_pre_condition--;
555 FOR_EACH_SM(stree, sm) {
556 set_state(sm->owner, sm->name, sm->sym, sm->state);
557 } END_FOR_EACH_SM(sm);
558 free_stree(&stree);
559 if (extra_sm)
560 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
562 if (option_assume_loops)
563 once_through = 1;
565 __split_stmt(stmt->iterator_statement);
566 if (is_forever_loop(stmt)) {
567 __merge_continues();
568 __save_gotos(loop_name, NULL);
570 __push_fake_cur_stree();
571 __split_stmt(stmt->iterator_post_statement);
572 stree = __pop_fake_cur_stree();
574 __discard_false_states();
575 __use_breaks();
577 if (!__path_is_null())
578 __merge_stree_into_cur(stree);
579 free_stree(&stree);
580 } else {
581 __merge_continues();
582 unchanged = __iterator_unchanged(extra_sm);
583 __split_stmt(stmt->iterator_post_statement);
584 __prev_stmt = stmt->iterator_post_statement;
585 __cur_stmt = stmt;
587 __save_gotos(loop_name, NULL);
588 __in_pre_condition++;
589 __split_whole_condition(stmt->iterator_pre_condition);
590 __in_pre_condition--;
591 nullify_path();
592 __merge_false_states();
593 if (once_through)
594 __discard_false_states();
595 else
596 __merge_false_states();
598 if (extra_sm && unchanged)
599 __extra_pre_loop_hook_after(extra_sm,
600 stmt->iterator_post_statement,
601 stmt->iterator_pre_condition);
602 __merge_breaks();
604 loop_count--;
608 * Post loops are do {} while();
610 static void handle_post_loop(struct statement *stmt)
612 char *loop_name;
614 loop_name = get_loop_name(loop_num);
615 loop_num++;
616 loop_count++;
618 __push_continues();
619 __push_breaks();
620 __merge_gotos(loop_name, NULL);
621 __split_stmt(stmt->iterator_statement);
622 __merge_continues();
623 if (!is_zero(stmt->iterator_post_condition))
624 __save_gotos(loop_name, NULL);
626 if (is_forever_loop(stmt)) {
627 __use_breaks();
628 } else {
629 __split_whole_condition(stmt->iterator_post_condition);
630 __use_false_states();
631 __merge_breaks();
633 loop_count--;
636 static int empty_statement(struct statement *stmt)
638 if (!stmt)
639 return 0;
640 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
641 return 1;
642 return 0;
645 static int last_stmt_on_same_line(void)
647 struct statement *stmt;
648 int i = 0;
650 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
651 if (!i++)
652 continue;
653 if (stmt->pos.line == get_lineno())
654 return 1;
655 return 0;
656 } END_FOR_EACH_PTR_REVERSE(stmt);
657 return 0;
660 static void split_asm_constraints(struct expression_list *expr_list)
662 struct expression *expr;
663 int state = 0;
665 FOR_EACH_PTR(expr_list, expr) {
666 switch (state) {
667 case 0: /* identifier */
668 case 1: /* constraint */
669 state++;
670 continue;
671 case 2: /* expression */
672 state = 0;
673 __split_expr(expr);
674 continue;
676 } END_FOR_EACH_PTR(expr);
679 static int is_case_val(struct statement *stmt, sval_t sval)
681 sval_t case_sval;
683 if (stmt->type != STMT_CASE)
684 return 0;
685 if (!stmt->case_expression) {
686 __set_default();
687 return 1;
689 if (!get_value(stmt->case_expression, &case_sval))
690 return 0;
691 if (case_sval.value == sval.value)
692 return 1;
693 return 0;
696 static struct range_list *get_case_rl(struct expression *switch_expr,
697 struct expression *case_expr,
698 struct expression *case_to)
700 sval_t start, end;
701 struct range_list *rl = NULL;
702 struct symbol *switch_type;
704 switch_type = get_type(switch_expr);
705 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
706 start = sval_cast(switch_type, start);
707 end = sval_cast(switch_type, end);
708 add_range(&rl, start, end);
709 } else if (get_value(case_expr, &start)) {
710 start = sval_cast(switch_type, start);
711 add_range(&rl, start, start);
714 return rl;
717 static void split_known_switch(struct statement *stmt, sval_t sval)
719 struct statement *tmp;
720 struct range_list *rl;
722 __split_expr(stmt->switch_expression);
723 sval = sval_cast(get_type(stmt->switch_expression), sval);
725 push_expression(&switch_expr_stack, stmt->switch_expression);
726 __save_switch_states(top_expression(switch_expr_stack));
727 nullify_path();
728 __push_default();
729 __push_breaks();
731 stmt = stmt->switch_statement;
733 __push_scope_hooks();
734 FOR_EACH_PTR(stmt->stmts, tmp) {
735 __smatch_lineno = tmp->pos.line;
736 if (is_case_val(tmp, sval)) {
737 rl = alloc_rl(sval, sval);
738 __merge_switches(top_expression(switch_expr_stack), rl);
739 __pass_case_to_client(top_expression(switch_expr_stack), rl);
741 if (__path_is_null())
742 continue;
743 __split_stmt(tmp);
744 if (__path_is_null()) {
745 __set_default();
746 goto out;
748 } END_FOR_EACH_PTR(tmp);
749 out:
750 __call_scope_hooks();
751 if (!__pop_default())
752 __merge_switches(top_expression(switch_expr_stack), NULL);
753 __discard_switches();
754 __merge_breaks();
755 pop_expression(&switch_expr_stack);
758 static void split_case(struct statement *stmt)
760 struct range_list *rl = NULL;
762 expr_set_parent_stmt(stmt->case_expression, stmt);
763 expr_set_parent_stmt(stmt->case_to, stmt);
765 rl = get_case_rl(top_expression(switch_expr_stack),
766 stmt->case_expression, stmt->case_to);
767 while (stmt->case_statement->type == STMT_CASE) {
768 struct range_list *tmp;
770 tmp = get_case_rl(top_expression(switch_expr_stack),
771 stmt->case_statement->case_expression,
772 stmt->case_statement->case_to);
773 if (!tmp)
774 break;
775 rl = rl_union(rl, tmp);
776 if (!stmt->case_expression)
777 __set_default();
778 stmt = stmt->case_statement;
781 __merge_switches(top_expression(switch_expr_stack), rl);
783 if (!stmt->case_expression)
784 __set_default();
785 __split_stmt(stmt->case_statement);
788 int time_parsing_function(void)
790 return ms_since(&fn_start_time) / 1000;
793 static int taking_too_long(void)
795 if (time_parsing_function() > 60 * 5) /* five minutes */
796 return 1;
797 return 0;
800 static int is_last_stmt(struct statement *cur_stmt)
802 struct symbol *fn = get_base_type(cur_func_sym);
803 struct statement *stmt;
805 if (!fn)
806 return 0;
807 stmt = fn->stmt;
808 if (!stmt)
809 stmt = fn->inline_stmt;
810 if (!stmt || stmt->type != STMT_COMPOUND)
811 return 0;
812 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
813 if (stmt && stmt->type == STMT_LABEL)
814 stmt = stmt->label_statement;
815 if (stmt == cur_stmt)
816 return 1;
817 return 0;
820 static void handle_backward_goto(struct statement *goto_stmt)
822 const char *goto_name, *label_name;
823 struct statement *func_stmt;
824 struct symbol *base_type = get_base_type(cur_func_sym);
825 struct statement *tmp;
826 int found = 0;
828 if (!option_info)
829 return;
830 if (last_goto_statement_handled)
831 return;
832 last_goto_statement_handled = 1;
834 if (!goto_stmt->goto_label ||
835 goto_stmt->goto_label->type != SYM_LABEL ||
836 !goto_stmt->goto_label->ident)
837 return;
838 goto_name = goto_stmt->goto_label->ident->name;
840 func_stmt = base_type->stmt;
841 if (!func_stmt)
842 func_stmt = base_type->inline_stmt;
843 if (!func_stmt)
844 return;
845 if (func_stmt->type != STMT_COMPOUND)
846 return;
848 FOR_EACH_PTR(func_stmt->stmts, tmp) {
849 if (!found) {
850 if (tmp->type != STMT_LABEL)
851 continue;
852 if (!tmp->label_identifier ||
853 tmp->label_identifier->type != SYM_LABEL ||
854 !tmp->label_identifier->ident)
855 continue;
856 label_name = tmp->label_identifier->ident->name;
857 if (strcmp(goto_name, label_name) != 0)
858 continue;
859 found = 1;
861 __split_stmt(tmp);
862 } END_FOR_EACH_PTR(tmp);
865 static void fake_a_return(void)
867 struct symbol *return_type;
869 nullify_path();
870 __unnullify_path();
872 return_type = get_real_base_type(cur_func_sym);
873 return_type = get_real_base_type(return_type);
874 if (return_type != &void_ctype) {
875 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
876 nullify_path();
880 static void fake_an_empty_default(struct position pos)
882 static struct statement none = {};
884 none.pos = pos;
885 none.type = STMT_NONE;
886 __merge_switches(top_expression(switch_expr_stack), NULL);
887 __split_stmt(&none);
890 static void split_compound(struct statement *stmt)
892 struct statement *prev = NULL;
893 struct statement *cur = NULL;
894 struct statement *next;
896 __push_scope_hooks();
898 FOR_EACH_PTR(stmt->stmts, next) {
899 /* just set them all ahead of time */
900 stmt_set_parent_stmt(next, stmt);
902 if (cur) {
903 __prev_stmt = prev;
904 __next_stmt = next;
905 __cur_stmt = cur;
906 __split_stmt(cur);
908 prev = cur;
909 cur = next;
910 } END_FOR_EACH_PTR(next);
911 if (cur) {
912 __prev_stmt = prev;
913 __cur_stmt = cur;
914 __next_stmt = NULL;
915 __split_stmt(cur);
919 * For function scope, then delay calling the scope hooks until the
920 * end of function hooks can run. I'm not positive this is the right
921 * thing...
923 if (!is_last_stmt(cur))
924 __call_scope_hooks();
928 * This is a hack, work around for detecting empty functions.
930 static int need_delayed_scope_hooks(void)
932 struct symbol *fn = get_base_type(cur_func_sym);
933 struct statement *stmt;
935 if (!fn)
936 return 0;
937 stmt = fn->stmt;
938 if (!stmt)
939 stmt = fn->inline_stmt;
940 if (stmt && stmt->type == STMT_COMPOUND)
941 return 1;
942 return 0;
945 void __split_label_stmt(struct statement *stmt)
947 if (stmt->label_identifier &&
948 stmt->label_identifier->type == SYM_LABEL &&
949 stmt->label_identifier->ident) {
950 loop_count |= 0x0800000;
951 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
955 static void find_asm_gotos(struct statement *stmt)
957 struct symbol *sym;
959 FOR_EACH_PTR(stmt->asm_labels, sym) {
960 __save_gotos(sym->ident->name, sym);
961 } END_FOR_EACH_PTR(sym);
964 void __split_stmt(struct statement *stmt)
966 sval_t sval;
968 if (!stmt)
969 goto out;
971 if (!__in_fake_assign)
972 __silence_warnings_for_stmt = false;
974 if (__bail_on_rest_of_function)
975 return;
977 if (out_of_memory() || taking_too_long()) {
978 struct timeval stop;
980 gettimeofday(&stop, NULL);
982 __bail_on_rest_of_function = 1;
983 final_pass = 1;
984 sm_msg("Function too hairy. Giving up. %lu seconds",
985 stop.tv_sec - fn_start_time.tv_sec);
986 fake_a_return();
987 final_pass = 0; /* turn off sm_msg() from here */
988 return;
991 add_ptr_list(&big_statement_stack, stmt);
992 free_expression_stack(&big_expression_stack);
993 set_position(stmt->pos);
994 __pass_to_client(stmt, STMT_HOOK);
996 switch (stmt->type) {
997 case STMT_DECLARATION:
998 split_declaration(stmt->declaration);
999 break;
1000 case STMT_RETURN:
1001 expr_set_parent_stmt(stmt->ret_value, stmt);
1003 __split_expr(stmt->ret_value);
1004 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1005 __process_post_op_stack();
1006 nullify_path();
1007 break;
1008 case STMT_EXPRESSION:
1009 expr_set_parent_stmt(stmt->expression, stmt);
1010 expr_set_parent_stmt(stmt->context, stmt);
1012 __split_expr(stmt->expression);
1013 break;
1014 case STMT_COMPOUND:
1015 split_compound(stmt);
1016 break;
1017 case STMT_IF:
1018 stmt_set_parent_stmt(stmt->if_true, stmt);
1019 stmt_set_parent_stmt(stmt->if_false, stmt);
1020 expr_set_parent_stmt(stmt->if_conditional, stmt);
1022 if (known_condition_true(stmt->if_conditional)) {
1023 __split_stmt(stmt->if_true);
1024 break;
1026 if (known_condition_false(stmt->if_conditional)) {
1027 __split_stmt(stmt->if_false);
1028 break;
1030 __split_whole_condition(stmt->if_conditional);
1031 __split_stmt(stmt->if_true);
1032 if (empty_statement(stmt->if_true) &&
1033 last_stmt_on_same_line() &&
1034 !get_macro_name(stmt->if_true->pos))
1035 sm_msg("warn: if();");
1036 __push_true_states();
1037 __use_false_states();
1038 __split_stmt(stmt->if_false);
1039 __merge_true_states();
1040 break;
1041 case STMT_ITERATOR:
1042 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1043 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1044 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1045 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1046 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1048 if (stmt->iterator_pre_condition)
1049 handle_pre_loop(stmt);
1050 else if (stmt->iterator_post_condition)
1051 handle_post_loop(stmt);
1052 else {
1053 // these are for(;;) type loops.
1054 handle_pre_loop(stmt);
1056 break;
1057 case STMT_SWITCH:
1058 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1059 expr_set_parent_stmt(stmt->switch_expression, stmt);
1061 if (get_value(stmt->switch_expression, &sval)) {
1062 split_known_switch(stmt, sval);
1063 break;
1065 __split_expr(stmt->switch_expression);
1066 push_expression(&switch_expr_stack, stmt->switch_expression);
1067 __save_switch_states(top_expression(switch_expr_stack));
1068 nullify_path();
1069 __push_default();
1070 __push_breaks();
1071 __split_stmt(stmt->switch_statement);
1072 if (!__pop_default() && have_remaining_cases())
1073 fake_an_empty_default(stmt->pos);
1074 __discard_switches();
1075 __merge_breaks();
1076 pop_expression(&switch_expr_stack);
1077 break;
1078 case STMT_CASE:
1079 split_case(stmt);
1080 break;
1081 case STMT_LABEL:
1082 __split_label_stmt(stmt);
1083 __split_stmt(stmt->label_statement);
1084 break;
1085 case STMT_GOTO:
1086 expr_set_parent_stmt(stmt->goto_expression, stmt);
1088 __split_expr(stmt->goto_expression);
1089 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1090 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1091 __process_breaks();
1092 } else if (!strcmp(stmt->goto_label->ident->name,
1093 "continue")) {
1094 __process_continues();
1096 } else if (stmt->goto_label &&
1097 stmt->goto_label->type == SYM_LABEL &&
1098 stmt->goto_label->ident) {
1099 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1101 nullify_path();
1102 if (is_last_stmt(stmt))
1103 handle_backward_goto(stmt);
1104 break;
1105 case STMT_NONE:
1106 break;
1107 case STMT_ASM:
1108 expr_set_parent_stmt(stmt->asm_string, stmt);
1110 find_asm_gotos(stmt);
1111 __pass_to_client(stmt, ASM_HOOK);
1112 __split_expr(stmt->asm_string);
1113 split_asm_constraints(stmt->asm_outputs);
1114 split_asm_constraints(stmt->asm_inputs);
1115 split_asm_constraints(stmt->asm_clobbers);
1116 break;
1117 case STMT_CONTEXT:
1118 break;
1119 case STMT_RANGE:
1120 __split_expr(stmt->range_expression);
1121 __split_expr(stmt->range_low);
1122 __split_expr(stmt->range_high);
1123 break;
1125 __pass_to_client(stmt, STMT_HOOK_AFTER);
1126 out:
1127 __process_post_op_stack();
1130 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1132 struct expression *expr;
1134 FOR_EACH_PTR(expr_list, expr) {
1135 expr_set_parent_expr(expr, parent);
1136 __split_expr(expr);
1137 __process_post_op_stack();
1138 } END_FOR_EACH_PTR(expr);
1141 static void split_sym(struct symbol *sym)
1143 if (!sym)
1144 return;
1145 if (!(sym->namespace & NS_SYMBOL))
1146 return;
1148 __split_stmt(sym->stmt);
1149 __split_expr(sym->array_size);
1150 split_symlist(sym->arguments);
1151 split_symlist(sym->symbol_list);
1152 __split_stmt(sym->inline_stmt);
1153 split_symlist(sym->inline_symbol_list);
1156 static void split_symlist(struct symbol_list *sym_list)
1158 struct symbol *sym;
1160 FOR_EACH_PTR(sym_list, sym) {
1161 split_sym(sym);
1162 } END_FOR_EACH_PTR(sym);
1165 typedef void (fake_cb)(struct expression *expr);
1167 static int member_to_number(struct expression *expr, struct ident *member)
1169 struct symbol *type, *tmp;
1170 char *name;
1171 int i;
1173 if (!member)
1174 return -1;
1175 name = member->name;
1177 type = get_type(expr);
1178 if (!type || type->type != SYM_STRUCT)
1179 return -1;
1181 i = -1;
1182 FOR_EACH_PTR(type->symbol_list, tmp) {
1183 i++;
1184 if (!tmp->ident)
1185 continue;
1186 if (strcmp(name, tmp->ident->name) == 0)
1187 return i;
1188 } END_FOR_EACH_PTR(tmp);
1189 return -1;
1192 static struct ident *number_to_member(struct expression *expr, int num)
1194 struct symbol *type, *member;
1195 int i = 0;
1197 type = get_type(expr);
1198 if (!type || type->type != SYM_STRUCT)
1199 return NULL;
1201 FOR_EACH_PTR(type->symbol_list, member) {
1202 if (i == num)
1203 return member->ident;
1204 i++;
1205 } END_FOR_EACH_PTR(member);
1206 return NULL;
1209 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1211 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1213 struct expression *edge_member, *assign;
1214 struct symbol *base = get_real_base_type(member);
1215 struct symbol *tmp;
1217 if (member->ident)
1218 expr = member_expression(expr, '.', member->ident);
1220 FOR_EACH_PTR(base->symbol_list, tmp) {
1221 struct symbol *type;
1223 type = get_real_base_type(tmp);
1224 if (!type)
1225 continue;
1227 edge_member = member_expression(expr, '.', tmp->ident);
1228 if (get_extra_state(edge_member))
1229 continue;
1231 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1232 set_inner_struct_members(expr, tmp);
1233 continue;
1236 if (!tmp->ident)
1237 continue;
1239 assign = assign_expression(edge_member, zero_expr());
1240 __split_expr(assign);
1241 } END_FOR_EACH_PTR(tmp);
1246 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1248 struct symbol *tmp;
1249 struct expression *member = NULL;
1250 struct expression *assign;
1251 int op = '*';
1253 if (expr->type == EXPR_PREOP && expr->op == '&') {
1254 expr = strip_expr(expr->unop);
1255 op = '.';
1258 FOR_EACH_PTR(type->symbol_list, tmp) {
1259 type = get_real_base_type(tmp);
1260 if (!type)
1261 continue;
1263 if (tmp->ident) {
1264 member = member_expression(expr, op, tmp->ident);
1265 if (get_extra_state(member))
1266 continue;
1269 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1270 set_inner_struct_members(expr, tmp);
1271 continue;
1273 if (type->type == SYM_ARRAY)
1274 continue;
1275 if (!tmp->ident)
1276 continue;
1278 assign = assign_expression(member, zero_expr());
1279 __split_expr(assign);
1280 } END_FOR_EACH_PTR(tmp);
1283 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1285 struct expression *deref, *assign, *tmp, *right;
1286 struct symbol *struct_type, *type;
1287 struct ident *member;
1288 int member_idx;
1290 struct_type = get_type(symbol);
1291 if (!struct_type ||
1292 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1293 return;
1296 * We're parsing an initializer that could look something like this:
1297 * struct foo foo = {
1298 * 42,
1299 * .whatever.xxx = 11,
1300 * .zzz = 12,
1301 * };
1303 * So what we have here is a list with 42, .whatever, and .zzz. We need
1304 * to break it up into left and right sides of the assignments.
1307 member_idx = 0;
1308 FOR_EACH_PTR(members, tmp) {
1309 deref = NULL;
1310 if (tmp->type == EXPR_IDENTIFIER) {
1311 member_idx = member_to_number(symbol, tmp->expr_ident);
1312 while (tmp->type == EXPR_IDENTIFIER) {
1313 member = tmp->expr_ident;
1314 tmp = tmp->ident_expression;
1315 if (deref)
1316 deref = member_expression(deref, '.', member);
1317 else
1318 deref = member_expression(symbol, '.', member);
1320 } else {
1321 member = number_to_member(symbol, member_idx);
1322 deref = member_expression(symbol, '.', member);
1324 right = tmp;
1325 member_idx++;
1326 if (right->type == EXPR_INITIALIZER) {
1327 type = get_type(deref);
1328 if (type && type->type == SYM_ARRAY)
1329 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1330 else
1331 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1332 } else {
1333 assign = assign_expression(deref, right);
1334 fake_cb(assign);
1336 } END_FOR_EACH_PTR(tmp);
1338 set_unset_to_zero(struct_type, symbol);
1341 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1343 fake_member_assigns_helper(symbol_expression(sym),
1344 sym->initializer->expr_list, fake_cb);
1347 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1349 struct expression *offset, *binop, *assign, *tmp;
1350 struct symbol *type;
1351 int idx;
1353 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1354 return;
1356 idx = 0;
1357 FOR_EACH_PTR(expr_list, tmp) {
1358 if (tmp->type == EXPR_INDEX) {
1359 if (tmp->idx_from != tmp->idx_to)
1360 return;
1361 idx = tmp->idx_from;
1362 if (!tmp->idx_expression)
1363 goto next;
1364 tmp = tmp->idx_expression;
1366 offset = value_expr(idx);
1367 binop = array_element_expression(array, offset);
1368 if (tmp->type == EXPR_INITIALIZER) {
1369 type = get_type(binop);
1370 if (type && type->type == SYM_ARRAY)
1371 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1372 else
1373 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1374 } else {
1375 assign = assign_expression(binop, tmp);
1376 fake_cb(assign);
1378 next:
1379 idx++;
1380 } END_FOR_EACH_PTR(tmp);
1383 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1385 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1388 static void fake_assign_expr(struct symbol *sym)
1390 struct expression *assign, *symbol;
1392 symbol = symbol_expression(sym);
1393 assign = assign_expression(symbol, sym->initializer);
1394 __split_expr(assign);
1397 static void call_split_expr(struct expression *expr)
1399 __split_expr(expr);
1402 static void do_initializer_stuff(struct symbol *sym)
1404 if (!sym->initializer)
1405 return;
1407 if (sym->initializer->type == EXPR_INITIALIZER) {
1408 if (get_real_base_type(sym)->type == SYM_ARRAY)
1409 fake_element_assigns(sym, call_split_expr);
1410 else
1411 fake_member_assigns(sym, call_split_expr);
1412 } else {
1413 fake_assign_expr(sym);
1417 static void split_declaration(struct symbol_list *sym_list)
1419 struct symbol *sym;
1421 FOR_EACH_PTR(sym_list, sym) {
1422 __pass_to_client(sym, DECLARATION_HOOK);
1423 do_initializer_stuff(sym);
1424 split_sym(sym);
1425 } END_FOR_EACH_PTR(sym);
1428 static void call_global_assign_hooks(struct expression *assign)
1430 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1433 static void fake_global_assign(struct symbol *sym)
1435 struct expression *assign, *symbol;
1437 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1438 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1439 fake_element_assigns(sym, call_global_assign_hooks);
1440 } else if (sym->initializer) {
1441 symbol = symbol_expression(sym);
1442 assign = assign_expression(symbol, sym->initializer);
1443 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1444 } else {
1445 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1447 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1448 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1449 fake_member_assigns(sym, call_global_assign_hooks);
1450 } else if (sym->initializer) {
1451 symbol = symbol_expression(sym);
1452 assign = assign_expression(symbol, sym->initializer);
1453 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1454 } else {
1455 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1457 } else {
1458 symbol = symbol_expression(sym);
1459 if (sym->initializer)
1460 assign = assign_expression(symbol, sym->initializer);
1461 else
1462 assign = assign_expression(symbol, zero_expr());
1463 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1467 static void start_function_definition(struct symbol *sym)
1469 __in_function_def = 1;
1470 __pass_to_client(sym, FUNC_DEF_HOOK);
1471 __in_function_def = 0;
1472 __pass_to_client(sym, AFTER_DEF_HOOK);
1476 static void split_function(struct symbol *sym)
1478 struct symbol *base_type = get_base_type(sym);
1480 if (!base_type->stmt && !base_type->inline_stmt)
1481 return;
1483 gettimeofday(&fn_start_time, NULL);
1484 cur_func_sym = sym;
1485 if (sym->ident)
1486 cur_func = sym->ident->name;
1487 set_position(sym->pos);
1488 loop_count = 0;
1489 last_goto_statement_handled = 0;
1490 sm_debug("new function: %s\n", cur_func);
1491 __stree_id = 0;
1492 if (option_two_passes) {
1493 __unnullify_path();
1494 loop_num = 0;
1495 final_pass = 0;
1496 start_function_definition(sym);
1497 __split_stmt(base_type->stmt);
1498 __split_stmt(base_type->inline_stmt);
1499 nullify_path();
1501 __unnullify_path();
1502 loop_num = 0;
1503 final_pass = 1;
1504 start_function_definition(sym);
1505 __split_stmt(base_type->stmt);
1506 __split_stmt(base_type->inline_stmt);
1507 __pass_to_client(sym, END_FUNC_HOOK);
1508 if (need_delayed_scope_hooks())
1509 __call_scope_hooks();
1510 __pass_to_client(sym, AFTER_FUNC_HOOK);
1512 clear_all_states();
1513 cur_func_sym = NULL;
1514 cur_func = NULL;
1515 free_data_info_allocs();
1516 free_expression_stack(&switch_expr_stack);
1517 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1518 __bail_on_rest_of_function = 0;
1521 static void save_flow_state(void)
1523 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1524 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1525 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1527 __add_ptr_list(&backup, big_statement_stack, 0);
1528 __add_ptr_list(&backup, big_expression_stack, 0);
1529 __add_ptr_list(&backup, big_condition_stack, 0);
1530 __add_ptr_list(&backup, switch_expr_stack, 0);
1532 __add_ptr_list(&backup, cur_func_sym, 0);
1534 __add_ptr_list(&backup, __prev_stmt, 0);
1535 __add_ptr_list(&backup, __cur_stmt, 0);
1536 __add_ptr_list(&backup, __next_stmt, 0);
1540 static void *pop_backup(void)
1542 void *ret;
1544 ret = last_ptr_list(backup);
1545 delete_ptr_list_last(&backup);
1546 return ret;
1549 static void restore_flow_state(void)
1551 __next_stmt = pop_backup();
1552 __cur_stmt = pop_backup();
1553 __prev_stmt = pop_backup();
1555 cur_func_sym = pop_backup();
1556 switch_expr_stack = pop_backup();
1557 big_condition_stack = pop_backup();
1558 big_expression_stack = pop_backup();
1559 big_statement_stack = pop_backup();
1560 final_pass = PTR_INT(pop_backup()) >> 2;
1561 loop_count = PTR_INT(pop_backup()) >> 2;
1562 loop_num = PTR_INT(pop_backup()) >> 2;
1565 static void parse_inline(struct expression *call)
1567 struct symbol *base_type;
1568 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1569 struct timeval time_backup = fn_start_time;
1571 save_flow_state();
1573 __pass_to_client(call, INLINE_FN_START);
1574 final_pass = 0; /* don't print anything */
1575 __inline_fn = call;
1577 base_type = get_base_type(call->fn->symbol);
1578 cur_func_sym = call->fn->symbol;
1579 if (call->fn->symbol->ident)
1580 cur_func = call->fn->symbol->ident->name;
1581 else
1582 cur_func = NULL;
1583 set_position(call->fn->symbol->pos);
1585 save_all_states();
1586 big_statement_stack = NULL;
1587 big_expression_stack = NULL;
1588 big_condition_stack = NULL;
1589 switch_expr_stack = NULL;
1591 sm_debug("inline function: %s\n", cur_func);
1592 __unnullify_path();
1593 loop_num = 0;
1594 loop_count = 0;
1595 start_function_definition(call->fn->symbol);
1596 __split_stmt(base_type->stmt);
1597 __split_stmt(base_type->inline_stmt);
1598 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1599 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1601 free_expression_stack(&switch_expr_stack);
1602 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1603 nullify_path();
1604 free_goto_stack();
1606 restore_flow_state();
1607 fn_start_time = time_backup;
1608 cur_func = cur_func_bak;
1610 restore_all_states();
1611 set_position(call->pos);
1612 __inline_fn = NULL;
1613 __pass_to_client(call, INLINE_FN_END);
1616 static struct symbol_list *inlines_called;
1617 static void add_inline_function(struct symbol *sym)
1619 static struct symbol_list *already_added;
1620 struct symbol *tmp;
1622 FOR_EACH_PTR(already_added, tmp) {
1623 if (tmp == sym)
1624 return;
1625 } END_FOR_EACH_PTR(tmp);
1627 add_ptr_list(&already_added, sym);
1628 add_ptr_list(&inlines_called, sym);
1631 static void process_inlines(void)
1633 struct symbol *tmp;
1635 FOR_EACH_PTR(inlines_called, tmp) {
1636 split_function(tmp);
1637 } END_FOR_EACH_PTR(tmp);
1638 free_ptr_list(&inlines_called);
1641 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1643 struct symbol *sym;
1645 FOR_EACH_PTR_REVERSE(big_list, sym) {
1646 if (!sym->scope)
1647 continue;
1648 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1649 return sym;
1650 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1651 return sym;
1652 } END_FOR_EACH_PTR_REVERSE(sym);
1654 return NULL;
1657 static void split_inlines_in_scope(struct symbol *sym)
1659 struct symbol *base;
1660 struct symbol_list *scope_list;
1661 int stream;
1663 scope_list = sym->scope->symbols;
1664 stream = sym->pos.stream;
1666 /* find the last static symbol in the file */
1667 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1668 if (sym->pos.stream != stream)
1669 continue;
1670 if (sym->type != SYM_NODE)
1671 continue;
1672 base = get_base_type(sym);
1673 if (!base)
1674 continue;
1675 if (base->type != SYM_FN)
1676 continue;
1677 if (!base->inline_stmt)
1678 continue;
1679 add_inline_function(sym);
1680 } END_FOR_EACH_PTR_REVERSE(sym);
1682 process_inlines();
1685 static void split_inlines(struct symbol_list *sym_list)
1687 struct symbol *sym;
1689 sym = get_last_scoped_symbol(sym_list, 0);
1690 if (sym)
1691 split_inlines_in_scope(sym);
1692 sym = get_last_scoped_symbol(sym_list, 1);
1693 if (sym)
1694 split_inlines_in_scope(sym);
1697 static struct stree *clone_estates_perm(struct stree *orig)
1699 struct stree *ret = NULL;
1700 struct sm_state *tmp;
1702 FOR_EACH_SM(orig, tmp) {
1703 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1704 } END_FOR_EACH_SM(tmp);
1706 return ret;
1709 static bool interesting_function(struct symbol *sym)
1711 static int prev_stream = -1;
1712 static bool prev_answer;
1713 const char *filename;
1714 int len;
1717 if (!(sym->ctype.modifiers & MOD_INLINE))
1718 return true;
1720 if (sym->pos.stream == prev_stream)
1721 return prev_answer;
1723 prev_stream = sym->pos.stream;
1724 prev_answer = false;
1726 filename = stream_name(sym->pos.stream);
1727 len = strlen(filename);
1728 if (len > 0 && filename[len - 1] == 'c')
1729 prev_answer = true;
1730 return prev_answer;
1733 struct position last_pos;
1734 static void split_c_file_functions(struct symbol_list *sym_list)
1736 struct symbol *sym;
1738 __unnullify_path();
1739 FOR_EACH_PTR(sym_list, sym) {
1740 set_position(sym->pos);
1741 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1742 __pass_to_client(sym, BASE_HOOK);
1743 fake_global_assign(sym);
1745 } END_FOR_EACH_PTR(sym);
1746 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1747 nullify_path();
1749 FOR_EACH_PTR(sym_list, sym) {
1750 set_position(sym->pos);
1751 last_pos = sym->pos;
1752 if (!interesting_function(sym))
1753 continue;
1754 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1755 split_function(sym);
1756 process_inlines();
1758 last_pos = sym->pos;
1759 } END_FOR_EACH_PTR(sym);
1760 split_inlines(sym_list);
1761 __pass_to_client(sym_list, END_FILE_HOOK);
1764 static int final_before_fake;
1765 void init_fake_env(void)
1767 if (!in_fake_env)
1768 final_before_fake = final_pass;
1769 in_fake_env++;
1770 __push_fake_cur_stree();
1771 final_pass = 0;
1774 void end_fake_env(void)
1776 __pop_fake_cur_stree();
1777 in_fake_env--;
1778 if (!in_fake_env)
1779 final_pass = final_before_fake;
1782 static void open_output_files(char *base_file)
1784 char buf[256];
1786 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1787 sm_outfd = fopen(buf, "w");
1788 if (!sm_outfd) {
1789 printf("Error: Cannot open %s\n", buf);
1790 exit(1);
1793 if (!option_info)
1794 return;
1796 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1797 sql_outfd = fopen(buf, "w");
1798 if (!sql_outfd) {
1799 printf("Error: Cannot open %s\n", buf);
1800 exit(1);
1803 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1804 caller_info_fd = fopen(buf, "w");
1805 if (!caller_info_fd) {
1806 printf("Error: Cannot open %s\n", buf);
1807 exit(1);
1811 void smatch(int argc, char **argv)
1813 struct string_list *filelist = NULL;
1814 struct symbol_list *sym_list;
1815 struct timeval stop, start;
1817 gettimeofday(&start, NULL);
1819 if (argc < 2) {
1820 printf("Usage: smatch [--debug] <filename.c>\n");
1821 exit(1);
1823 sparse_initialize(argc, argv, &filelist);
1824 set_valid_ptr_max();
1825 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1826 if (option_file_output)
1827 open_output_files(base_file);
1828 sym_list = sparse_keep_tokens(base_file);
1829 split_c_file_functions(sym_list);
1830 } END_FOR_EACH_PTR_NOTAG(base_file);
1832 gettimeofday(&stop, NULL);
1834 set_position(last_pos);
1835 if (option_time)
1836 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);