user_data2: use function returns in points_to_user_data()
[smatch.git] / smatch_flow.c
blob9c3c7dc919f4d892abe3f6fe299f514e6a6c3d52
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 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 *full_base_file;
42 static char *cur_func;
43 static unsigned int loop_count;
44 static int last_goto_statement_handled;
45 int __expr_stmt_count;
46 int __in_function_def;
47 static struct expression_list *switch_expr_stack = NULL;
48 static struct expression_list *post_op_stack = NULL;
50 static struct ptr_list *backup;
52 struct expression_list *big_expression_stack;
53 struct statement_list *big_statement_stack;
54 struct statement *__prev_stmt;
55 struct statement *__cur_stmt;
56 struct statement *__next_stmt;
57 int __in_pre_condition = 0;
58 int __bail_on_rest_of_function = 0;
59 static struct timeval fn_start_time;
60 char *get_function(void) { return cur_func; }
61 int get_lineno(void) { return __smatch_lineno; }
62 int inside_loop(void) { return !!loop_count; }
63 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
64 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
65 int in_expression_statement(void) { return !!__expr_stmt_count; }
67 static void split_symlist(struct symbol_list *sym_list);
68 static void split_declaration(struct symbol_list *sym_list);
69 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
70 static void add_inline_function(struct symbol *sym);
71 static void parse_inline(struct expression *expr);
73 int option_assume_loops = 0;
74 int option_two_passes = 0;
75 struct symbol *cur_func_sym = NULL;
76 struct stree *global_states;
78 long long valid_ptr_min = 4096;
79 long long valid_ptr_max = 2117777777;
80 sval_t valid_ptr_min_sval = {
81 .type = &ptr_ctype,
82 {.value = 4096},
84 sval_t valid_ptr_max_sval = {
85 .type = &ptr_ctype,
86 {.value = LONG_MAX - 100000},
88 struct range_list *valid_ptr_rl;
90 static void set_valid_ptr_max(void)
92 if (type_bits(&ptr_ctype) == 32)
93 valid_ptr_max = 2117777777;
94 else if (type_bits(&ptr_ctype) == 64)
95 valid_ptr_max = 2117777777777777777LL;
97 valid_ptr_max_sval.value = valid_ptr_max;
100 static void alloc_valid_ptr_rl(void)
102 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
103 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
104 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
107 int outside_of_function(void)
109 return cur_func_sym == NULL;
112 const char *get_filename(void)
114 if (option_info && option_full_path)
115 return full_base_file;
116 if (option_info)
117 return base_file;
118 if (option_full_path)
119 return full_filename;
120 return filename;
123 const char *get_base_file(void)
125 if (option_full_path)
126 return full_base_file;
127 return base_file;
130 static void set_position(struct position pos)
132 int len;
133 static int prev_stream = -1;
135 if (in_fake_env)
136 return;
138 if (pos.stream == 0 && pos.line == 0)
139 return;
141 __smatch_lineno = pos.line;
143 if (pos.stream == prev_stream)
144 return;
146 filename = stream_name(pos.stream);
148 free(full_filename);
149 pathname = getcwd(NULL, 0);
150 if (pathname) {
151 len = strlen(pathname) + 1 + strlen(filename) + 1;
152 full_filename = malloc(len);
153 snprintf(full_filename, len, "%s/%s", pathname, filename);
154 } else {
155 full_filename = alloc_string(filename);
157 free(pathname);
160 int is_assigned_call(struct expression *expr)
162 struct expression *parent = expr_get_parent_expr(expr);
164 if (parent &&
165 parent->type == EXPR_ASSIGNMENT &&
166 parent->op == '=' &&
167 strip_expr(parent->right) == expr)
168 return 1;
170 return 0;
173 static int is_inline_func(struct expression *expr)
175 if (expr->type != EXPR_SYMBOL || !expr->symbol)
176 return 0;
177 if (expr->symbol->ctype.modifiers & MOD_INLINE)
178 return 1;
179 return 0;
182 static int is_noreturn_func(struct expression *expr)
184 if (expr->type != EXPR_SYMBOL || !expr->symbol)
185 return 0;
186 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
187 return 1;
188 return 0;
191 static int inline_budget = 20;
193 int inlinable(struct expression *expr)
195 struct symbol *sym;
196 struct statement *last_stmt = NULL;
198 if (__inline_fn) /* don't nest */
199 return 0;
201 if (expr->type != EXPR_SYMBOL || !expr->symbol)
202 return 0;
203 if (is_no_inline_function(expr->symbol->ident->name))
204 return 0;
205 sym = get_base_type(expr->symbol);
206 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
207 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
208 return 0;
209 if (sym->stmt->type != STMT_COMPOUND)
210 return 0;
211 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
213 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
214 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
215 return 0;
216 if (sym->inline_stmt->type != STMT_COMPOUND)
217 return 0;
218 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
221 if (!last_stmt)
222 return 0;
224 /* the magic numbers in this function are pulled out of my bum. */
225 if (last_stmt->pos.line > sym->pos.line + inline_budget)
226 return 0;
228 return 1;
231 void __process_post_op_stack(void)
233 struct expression *expr;
235 FOR_EACH_PTR(post_op_stack, expr) {
236 __pass_to_client(expr, OP_HOOK);
237 } END_FOR_EACH_PTR(expr);
239 __free_ptr_list((struct ptr_list **)&post_op_stack);
242 static int handle_comma_assigns(struct expression *expr)
244 struct expression *right;
245 struct expression *assign;
247 right = strip_expr(expr->right);
248 if (right->type != EXPR_COMMA)
249 return 0;
251 __split_expr(right->left);
252 __process_post_op_stack();
254 assign = assign_expression(expr->left, '=', right->right);
255 __split_expr(assign);
257 return 1;
260 /* This is to handle *p++ = foo; assignments */
261 static int handle_postop_assigns(struct expression *expr)
263 struct expression *left, *fake_left;
264 struct expression *assign;
266 left = strip_expr(expr->left);
267 if (left->type != EXPR_PREOP || left->op != '*')
268 return 0;
269 left = strip_expr(left->unop);
270 if (left->type != EXPR_POSTOP)
271 return 0;
273 fake_left = deref_expression(strip_expr(left->unop));
274 assign = assign_expression(fake_left, '=', expr->right);
276 __split_expr(assign);
277 __split_expr(expr->left);
279 return 1;
282 static int prev_expression_is_getting_address(struct expression *expr)
284 struct expression *parent;
286 do {
287 parent = expr_get_parent_expr(expr);
289 if (!parent)
290 return 0;
291 if (parent->type == EXPR_PREOP && parent->op == '&')
292 return 1;
293 if (parent->type == EXPR_PREOP && parent->op == '(')
294 goto next;
295 if (parent->type == EXPR_DEREF && parent->op == '.')
296 goto next;
298 return 0;
299 next:
300 expr = parent;
301 } while (1);
304 static int handle__builtin_choose_expr(struct expression *expr)
306 struct expression *const_expr, *expr1, *expr2;
307 sval_t sval;
309 if (!sym_name_is("__builtin_choose_expr", expr->fn))
310 return 0;
312 const_expr = get_argument_from_call_expr(expr->args, 0);
313 expr1 = get_argument_from_call_expr(expr->args, 1);
314 expr2 = get_argument_from_call_expr(expr->args, 2);
316 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
317 return 0;
318 if (sval.value)
319 __split_expr(expr1);
320 else
321 __split_expr(expr2);
322 return 1;
325 void __split_expr(struct expression *expr)
327 if (!expr)
328 return;
330 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
332 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
333 return;
334 if (__in_fake_assign >= 4) /* don't allow too much nesting */
335 return;
337 push_expression(&big_expression_stack, expr);
338 set_position(expr->pos);
339 __pass_to_client(expr, EXPR_HOOK);
341 switch (expr->type) {
342 case EXPR_PREOP:
343 expr_set_parent_expr(expr->unop, expr);
345 if (expr->op == '*' &&
346 !prev_expression_is_getting_address(expr))
347 __pass_to_client(expr, DEREF_HOOK);
348 __split_expr(expr->unop);
349 __pass_to_client(expr, OP_HOOK);
350 break;
351 case EXPR_POSTOP:
352 expr_set_parent_expr(expr->unop, expr);
354 __split_expr(expr->unop);
355 push_expression(&post_op_stack, expr);
356 break;
357 case EXPR_STATEMENT:
358 __expr_stmt_count++;
359 if (expr->statement && !expr->statement) {
360 stmt_set_parent_stmt(expr->statement,
361 last_ptr_list((struct ptr_list *)big_statement_stack));
363 __split_stmt(expr->statement);
364 __expr_stmt_count--;
365 break;
366 case EXPR_LOGICAL:
367 case EXPR_COMPARE:
368 expr_set_parent_expr(expr->left, expr);
369 expr_set_parent_expr(expr->right, expr);
371 __pass_to_client(expr, LOGIC_HOOK);
372 __handle_logic(expr);
373 break;
374 case EXPR_BINOP:
375 expr_set_parent_expr(expr->left, expr);
376 expr_set_parent_expr(expr->right, expr);
378 __pass_to_client(expr, BINOP_HOOK);
379 case EXPR_COMMA:
380 expr_set_parent_expr(expr->left, expr);
381 expr_set_parent_expr(expr->right, expr);
383 __split_expr(expr->left);
384 __process_post_op_stack();
385 __split_expr(expr->right);
386 break;
387 case EXPR_ASSIGNMENT: {
388 struct expression *right;
390 expr_set_parent_expr(expr->left, expr);
391 expr_set_parent_expr(expr->right, expr);
393 right = strip_expr(expr->right);
394 if (!right)
395 break;
397 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
399 /* foo = !bar() */
400 if (__handle_condition_assigns(expr))
401 break;
402 /* foo = (x < 5 ? foo : 5); */
403 if (__handle_select_assigns(expr))
404 break;
405 /* foo = ({frob(); frob(); frob(); 1;}) */
406 if (__handle_expr_statement_assigns(expr))
407 break;
408 /* foo = (3, 4); */
409 if (handle_comma_assigns(expr))
410 break;
411 if (handle_postop_assigns(expr))
412 break;
414 __split_expr(expr->right);
415 if (outside_of_function())
416 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
417 else
418 __pass_to_client(expr, ASSIGNMENT_HOOK);
420 __fake_struct_member_assignments(expr);
422 if (expr->op == '=' && right->type == EXPR_CALL)
423 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
425 if (get_macro_name(right->pos) &&
426 get_macro_name(expr->pos) != get_macro_name(right->pos))
427 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
429 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
431 __split_expr(expr->left);
432 break;
434 case EXPR_DEREF:
435 expr_set_parent_expr(expr->deref, expr);
437 __pass_to_client(expr, DEREF_HOOK);
438 __split_expr(expr->deref);
439 break;
440 case EXPR_SLICE:
441 expr_set_parent_expr(expr->base, expr);
443 __split_expr(expr->base);
444 break;
445 case EXPR_CAST:
446 case EXPR_FORCE_CAST:
447 expr_set_parent_expr(expr->cast_expression, expr);
449 __pass_to_client(expr, CAST_HOOK);
450 __split_expr(expr->cast_expression);
451 break;
452 case EXPR_SIZEOF:
453 if (expr->cast_expression)
454 __pass_to_client(strip_parens(expr->cast_expression),
455 SIZEOF_HOOK);
456 break;
457 case EXPR_OFFSETOF:
458 case EXPR_ALIGNOF:
459 evaluate_expression(expr);
460 break;
461 case EXPR_CONDITIONAL:
462 case EXPR_SELECT:
463 expr_set_parent_expr(expr->conditional, expr);
464 expr_set_parent_expr(expr->cond_true, expr);
465 expr_set_parent_expr(expr->cond_false, expr);
467 if (known_condition_true(expr->conditional)) {
468 __split_expr(expr->cond_true);
469 break;
471 if (known_condition_false(expr->conditional)) {
472 __split_expr(expr->cond_false);
473 break;
475 __pass_to_client(expr, SELECT_HOOK);
476 __split_whole_condition(expr->conditional);
477 __split_expr(expr->cond_true);
478 __push_true_states();
479 __use_false_states();
480 __split_expr(expr->cond_false);
481 __merge_true_states();
482 break;
483 case EXPR_CALL:
484 expr_set_parent_expr(expr->fn, expr);
486 if (sym_name_is("__builtin_constant_p", expr->fn))
487 break;
488 if (handle__builtin_choose_expr(expr))
489 break;
490 split_expr_list(expr->args, expr);
491 __split_expr(expr->fn);
492 if (is_inline_func(expr->fn))
493 add_inline_function(expr->fn->symbol);
494 if (inlinable(expr->fn))
495 __inline_call = 1;
496 __process_post_op_stack();
497 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
498 __pass_to_client(expr, FUNCTION_CALL_HOOK);
499 __inline_call = 0;
500 if (inlinable(expr->fn)) {
501 parse_inline(expr);
503 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
504 if (is_noreturn_func(expr->fn))
505 nullify_path();
506 break;
507 case EXPR_INITIALIZER:
508 split_expr_list(expr->expr_list, expr);
509 break;
510 case EXPR_IDENTIFIER:
511 expr_set_parent_expr(expr->ident_expression, expr);
512 __split_expr(expr->ident_expression);
513 break;
514 case EXPR_INDEX:
515 expr_set_parent_expr(expr->idx_expression, expr);
516 __split_expr(expr->idx_expression);
517 break;
518 case EXPR_POS:
519 expr_set_parent_expr(expr->init_expr, expr);
520 __split_expr(expr->init_expr);
521 break;
522 case EXPR_SYMBOL:
523 __pass_to_client(expr, SYM_HOOK);
524 break;
525 case EXPR_STRING:
526 __pass_to_client(expr, STRING_HOOK);
527 break;
528 default:
529 break;
531 pop_expression(&big_expression_stack);
534 static int is_forever_loop(struct statement *stmt)
536 struct expression *expr;
537 sval_t sval;
539 expr = strip_expr(stmt->iterator_pre_condition);
540 if (!expr)
541 expr = stmt->iterator_post_condition;
542 if (!expr) {
543 /* this is a for(;;) loop... */
544 return 1;
547 if (get_value(expr, &sval) && sval.value != 0)
548 return 1;
550 return 0;
553 static int loop_num;
554 static char *get_loop_name(int num)
556 char buf[256];
558 snprintf(buf, 255, "-loop%d", num);
559 buf[255] = '\0';
560 return alloc_sname(buf);
564 * Pre Loops are while and for loops.
566 static void handle_pre_loop(struct statement *stmt)
568 int once_through; /* we go through the loop at least once */
569 struct sm_state *extra_sm = NULL;
570 int unchanged = 0;
571 char *loop_name;
572 struct stree *stree = NULL;
573 struct sm_state *sm = NULL;
575 loop_name = get_loop_name(loop_num);
576 loop_num++;
578 __split_stmt(stmt->iterator_pre_statement);
579 __prev_stmt = stmt->iterator_pre_statement;
581 once_through = implied_condition_true(stmt->iterator_pre_condition);
583 loop_count++;
584 __push_continues();
585 __push_breaks();
587 __merge_gotos(loop_name, NULL);
589 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
590 __in_pre_condition++;
591 __pass_to_client(stmt, PRELOOP_HOOK);
592 __split_whole_condition(stmt->iterator_pre_condition);
593 __in_pre_condition--;
594 FOR_EACH_SM(stree, sm) {
595 set_state(sm->owner, sm->name, sm->sym, sm->state);
596 } END_FOR_EACH_SM(sm);
597 free_stree(&stree);
598 if (extra_sm)
599 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
601 if (option_assume_loops)
602 once_through = 1;
604 __split_stmt(stmt->iterator_statement);
605 if (is_forever_loop(stmt)) {
606 __merge_continues();
607 __save_gotos(loop_name, NULL);
609 __push_fake_cur_stree();
610 __split_stmt(stmt->iterator_post_statement);
611 stree = __pop_fake_cur_stree();
613 __discard_false_states();
614 __use_breaks();
616 if (!__path_is_null())
617 __merge_stree_into_cur(stree);
618 free_stree(&stree);
619 } else {
620 __merge_continues();
621 unchanged = __iterator_unchanged(extra_sm);
622 __split_stmt(stmt->iterator_post_statement);
623 __prev_stmt = stmt->iterator_post_statement;
624 __cur_stmt = stmt;
626 __save_gotos(loop_name, NULL);
627 __in_pre_condition++;
628 __split_whole_condition(stmt->iterator_pre_condition);
629 __in_pre_condition--;
630 nullify_path();
631 __merge_false_states();
632 if (once_through)
633 __discard_false_states();
634 else
635 __merge_false_states();
637 if (extra_sm && unchanged)
638 __extra_pre_loop_hook_after(extra_sm,
639 stmt->iterator_post_statement,
640 stmt->iterator_pre_condition);
641 __merge_breaks();
643 loop_count--;
647 * Post loops are do {} while();
649 static void handle_post_loop(struct statement *stmt)
651 char *loop_name;
653 loop_name = get_loop_name(loop_num);
654 loop_num++;
655 loop_count++;
657 __push_continues();
658 __push_breaks();
659 __merge_gotos(loop_name, NULL);
660 __split_stmt(stmt->iterator_statement);
661 __merge_continues();
662 if (!is_zero(stmt->iterator_post_condition))
663 __save_gotos(loop_name, NULL);
665 if (is_forever_loop(stmt)) {
666 __use_breaks();
667 } else {
668 __split_whole_condition(stmt->iterator_post_condition);
669 __use_false_states();
670 __merge_breaks();
672 loop_count--;
675 static int empty_statement(struct statement *stmt)
677 if (!stmt)
678 return 0;
679 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
680 return 1;
681 return 0;
684 static int last_stmt_on_same_line(void)
686 struct statement *stmt;
687 int i = 0;
689 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
690 if (!i++)
691 continue;
692 if (stmt->pos.line == get_lineno())
693 return 1;
694 return 0;
695 } END_FOR_EACH_PTR_REVERSE(stmt);
696 return 0;
699 static void split_asm_constraints(struct expression_list *expr_list)
701 struct expression *expr;
702 int state = 0;
704 FOR_EACH_PTR(expr_list, expr) {
705 switch (state) {
706 case 0: /* identifier */
707 case 1: /* constraint */
708 state++;
709 continue;
710 case 2: /* expression */
711 state = 0;
712 __split_expr(expr);
713 continue;
715 } END_FOR_EACH_PTR(expr);
718 static int is_case_val(struct statement *stmt, sval_t sval)
720 sval_t case_sval;
722 if (stmt->type != STMT_CASE)
723 return 0;
724 if (!stmt->case_expression) {
725 __set_default();
726 return 1;
728 if (!get_value(stmt->case_expression, &case_sval))
729 return 0;
730 if (case_sval.value == sval.value)
731 return 1;
732 return 0;
735 static struct range_list *get_case_rl(struct expression *switch_expr,
736 struct expression *case_expr,
737 struct expression *case_to)
739 sval_t start, end;
740 struct range_list *rl = NULL;
741 struct symbol *switch_type;
743 switch_type = get_type(switch_expr);
744 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
745 start = sval_cast(switch_type, start);
746 end = sval_cast(switch_type, end);
747 add_range(&rl, start, end);
748 } else if (get_value(case_expr, &start)) {
749 start = sval_cast(switch_type, start);
750 add_range(&rl, start, start);
753 return rl;
756 static void split_known_switch(struct statement *stmt, sval_t sval)
758 struct statement *tmp;
759 struct range_list *rl;
761 __split_expr(stmt->switch_expression);
762 sval = sval_cast(get_type(stmt->switch_expression), sval);
764 push_expression(&switch_expr_stack, stmt->switch_expression);
765 __save_switch_states(top_expression(switch_expr_stack));
766 nullify_path();
767 __push_default();
768 __push_breaks();
770 stmt = stmt->switch_statement;
772 __push_scope_hooks();
773 FOR_EACH_PTR(stmt->stmts, tmp) {
774 __smatch_lineno = tmp->pos.line;
775 if (is_case_val(tmp, sval)) {
776 rl = alloc_rl(sval, sval);
777 __merge_switches(top_expression(switch_expr_stack), rl);
778 __pass_case_to_client(top_expression(switch_expr_stack), rl);
780 if (__path_is_null())
781 continue;
782 __split_stmt(tmp);
783 if (__path_is_null()) {
784 __set_default();
785 goto out;
787 } END_FOR_EACH_PTR(tmp);
788 out:
789 __call_scope_hooks();
790 if (!__pop_default())
791 __merge_switches(top_expression(switch_expr_stack), NULL);
792 __discard_switches();
793 __merge_breaks();
794 pop_expression(&switch_expr_stack);
797 static void split_case(struct statement *stmt)
799 struct range_list *rl = NULL;
801 expr_set_parent_stmt(stmt->case_expression, stmt);
802 expr_set_parent_stmt(stmt->case_to, stmt);
804 rl = get_case_rl(top_expression(switch_expr_stack),
805 stmt->case_expression, stmt->case_to);
806 while (stmt->case_statement->type == STMT_CASE) {
807 struct range_list *tmp;
809 tmp = get_case_rl(top_expression(switch_expr_stack),
810 stmt->case_statement->case_expression,
811 stmt->case_statement->case_to);
812 if (!tmp)
813 break;
814 rl = rl_union(rl, tmp);
815 if (!stmt->case_expression)
816 __set_default();
817 stmt = stmt->case_statement;
820 __merge_switches(top_expression(switch_expr_stack), rl);
822 if (!stmt->case_expression)
823 __set_default();
824 __split_stmt(stmt->case_statement);
827 int time_parsing_function(void)
829 return ms_since(&fn_start_time) / 1000;
832 static int taking_too_long(void)
834 if (time_parsing_function() > 60 * 5) /* five minutes */
835 return 1;
836 return 0;
839 static int is_last_stmt(struct statement *cur_stmt)
841 struct symbol *fn = get_base_type(cur_func_sym);
842 struct statement *stmt;
844 if (!fn)
845 return 0;
846 stmt = fn->stmt;
847 if (!stmt)
848 stmt = fn->inline_stmt;
849 if (!stmt || stmt->type != STMT_COMPOUND)
850 return 0;
851 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
852 if (stmt && stmt->type == STMT_LABEL)
853 stmt = stmt->label_statement;
854 if (stmt == cur_stmt)
855 return 1;
856 return 0;
859 static void handle_backward_goto(struct statement *goto_stmt)
861 const char *goto_name, *label_name;
862 struct statement *func_stmt;
863 struct symbol *base_type = get_base_type(cur_func_sym);
864 struct statement *tmp;
865 int found = 0;
867 if (!option_info)
868 return;
869 if (last_goto_statement_handled)
870 return;
871 last_goto_statement_handled = 1;
873 if (!goto_stmt->goto_label ||
874 goto_stmt->goto_label->type != SYM_LABEL ||
875 !goto_stmt->goto_label->ident)
876 return;
877 goto_name = goto_stmt->goto_label->ident->name;
879 func_stmt = base_type->stmt;
880 if (!func_stmt)
881 func_stmt = base_type->inline_stmt;
882 if (!func_stmt)
883 return;
884 if (func_stmt->type != STMT_COMPOUND)
885 return;
887 FOR_EACH_PTR(func_stmt->stmts, tmp) {
888 if (!found) {
889 if (tmp->type != STMT_LABEL)
890 continue;
891 if (!tmp->label_identifier ||
892 tmp->label_identifier->type != SYM_LABEL ||
893 !tmp->label_identifier->ident)
894 continue;
895 label_name = tmp->label_identifier->ident->name;
896 if (strcmp(goto_name, label_name) != 0)
897 continue;
898 found = 1;
900 __split_stmt(tmp);
901 } END_FOR_EACH_PTR(tmp);
904 static void fake_a_return(void)
906 struct symbol *return_type;
908 nullify_path();
909 __unnullify_path();
911 return_type = get_real_base_type(cur_func_sym);
912 return_type = get_real_base_type(return_type);
913 if (return_type != &void_ctype) {
914 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
915 nullify_path();
919 static void fake_an_empty_default(struct position pos)
921 static struct statement none = {};
923 none.pos = pos;
924 none.type = STMT_NONE;
925 __merge_switches(top_expression(switch_expr_stack), NULL);
926 __split_stmt(&none);
929 static void split_compound(struct statement *stmt)
931 struct statement *prev = NULL;
932 struct statement *cur = NULL;
933 struct statement *next;
935 __push_scope_hooks();
937 FOR_EACH_PTR(stmt->stmts, next) {
938 /* just set them all ahead of time */
939 stmt_set_parent_stmt(next, stmt);
941 if (cur) {
942 __prev_stmt = prev;
943 __next_stmt = next;
944 __cur_stmt = cur;
945 __split_stmt(cur);
947 prev = cur;
948 cur = next;
949 } END_FOR_EACH_PTR(next);
950 if (cur) {
951 __prev_stmt = prev;
952 __cur_stmt = cur;
953 __next_stmt = NULL;
954 __split_stmt(cur);
958 * For function scope, then delay calling the scope hooks until the
959 * end of function hooks can run. I'm not positive this is the right
960 * thing...
962 if (!is_last_stmt(cur))
963 __call_scope_hooks();
967 * This is a hack, work around for detecting empty functions.
969 static int need_delayed_scope_hooks(void)
971 struct symbol *fn = get_base_type(cur_func_sym);
972 struct statement *stmt;
974 if (!fn)
975 return 0;
976 stmt = fn->stmt;
977 if (!stmt)
978 stmt = fn->inline_stmt;
979 if (stmt && stmt->type == STMT_COMPOUND)
980 return 1;
981 return 0;
984 void __split_label_stmt(struct statement *stmt)
986 if (stmt->label_identifier &&
987 stmt->label_identifier->type == SYM_LABEL &&
988 stmt->label_identifier->ident) {
989 loop_count |= 0x0800000;
990 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
994 static void find_asm_gotos(struct statement *stmt)
996 struct symbol *sym;
998 FOR_EACH_PTR(stmt->asm_labels, sym) {
999 __save_gotos(sym->ident->name, sym);
1000 } END_FOR_EACH_PTR(sym);
1003 void __split_stmt(struct statement *stmt)
1005 sval_t sval;
1007 if (!stmt)
1008 goto out;
1010 if (!__in_fake_assign)
1011 __silence_warnings_for_stmt = false;
1013 if (__bail_on_rest_of_function)
1014 return;
1016 if (out_of_memory() || taking_too_long()) {
1017 struct timeval stop;
1019 gettimeofday(&stop, NULL);
1021 __bail_on_rest_of_function = 1;
1022 final_pass = 1;
1023 sm_msg("Function too hairy. Giving up. %lu seconds",
1024 stop.tv_sec - fn_start_time.tv_sec);
1025 fake_a_return();
1026 final_pass = 0; /* turn off sm_msg() from here */
1027 return;
1030 add_ptr_list(&big_statement_stack, stmt);
1031 free_expression_stack(&big_expression_stack);
1032 set_position(stmt->pos);
1033 __pass_to_client(stmt, STMT_HOOK);
1035 switch (stmt->type) {
1036 case STMT_DECLARATION:
1037 split_declaration(stmt->declaration);
1038 break;
1039 case STMT_RETURN:
1040 expr_set_parent_stmt(stmt->ret_value, stmt);
1042 __split_expr(stmt->ret_value);
1043 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1044 __process_post_op_stack();
1045 nullify_path();
1046 break;
1047 case STMT_EXPRESSION:
1048 expr_set_parent_stmt(stmt->expression, stmt);
1049 expr_set_parent_stmt(stmt->context, stmt);
1051 __split_expr(stmt->expression);
1052 break;
1053 case STMT_COMPOUND:
1054 split_compound(stmt);
1055 break;
1056 case STMT_IF:
1057 stmt_set_parent_stmt(stmt->if_true, stmt);
1058 stmt_set_parent_stmt(stmt->if_false, stmt);
1059 expr_set_parent_stmt(stmt->if_conditional, stmt);
1061 if (known_condition_true(stmt->if_conditional)) {
1062 __split_stmt(stmt->if_true);
1063 break;
1065 if (known_condition_false(stmt->if_conditional)) {
1066 __split_stmt(stmt->if_false);
1067 break;
1069 __split_whole_condition(stmt->if_conditional);
1070 __split_stmt(stmt->if_true);
1071 if (empty_statement(stmt->if_true) &&
1072 last_stmt_on_same_line() &&
1073 !get_macro_name(stmt->if_true->pos))
1074 sm_msg("warn: if();");
1075 __push_true_states();
1076 __use_false_states();
1077 __split_stmt(stmt->if_false);
1078 __merge_true_states();
1079 break;
1080 case STMT_ITERATOR:
1081 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1082 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1083 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1084 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1085 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1087 if (stmt->iterator_pre_condition)
1088 handle_pre_loop(stmt);
1089 else if (stmt->iterator_post_condition)
1090 handle_post_loop(stmt);
1091 else {
1092 // these are for(;;) type loops.
1093 handle_pre_loop(stmt);
1095 break;
1096 case STMT_SWITCH:
1097 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1098 expr_set_parent_stmt(stmt->switch_expression, stmt);
1100 if (get_value(stmt->switch_expression, &sval)) {
1101 split_known_switch(stmt, sval);
1102 break;
1104 __split_expr(stmt->switch_expression);
1105 push_expression(&switch_expr_stack, stmt->switch_expression);
1106 __save_switch_states(top_expression(switch_expr_stack));
1107 nullify_path();
1108 __push_default();
1109 __push_breaks();
1110 __split_stmt(stmt->switch_statement);
1111 if (!__pop_default() && have_remaining_cases())
1112 fake_an_empty_default(stmt->pos);
1113 __discard_switches();
1114 __merge_breaks();
1115 pop_expression(&switch_expr_stack);
1116 break;
1117 case STMT_CASE:
1118 split_case(stmt);
1119 break;
1120 case STMT_LABEL:
1121 __split_label_stmt(stmt);
1122 __split_stmt(stmt->label_statement);
1123 break;
1124 case STMT_GOTO:
1125 expr_set_parent_stmt(stmt->goto_expression, stmt);
1127 __split_expr(stmt->goto_expression);
1128 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1129 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1130 __process_breaks();
1131 } else if (!strcmp(stmt->goto_label->ident->name,
1132 "continue")) {
1133 __process_continues();
1135 } else if (stmt->goto_label &&
1136 stmt->goto_label->type == SYM_LABEL &&
1137 stmt->goto_label->ident) {
1138 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1140 nullify_path();
1141 if (is_last_stmt(stmt))
1142 handle_backward_goto(stmt);
1143 break;
1144 case STMT_NONE:
1145 break;
1146 case STMT_ASM:
1147 expr_set_parent_stmt(stmt->asm_string, stmt);
1149 find_asm_gotos(stmt);
1150 __pass_to_client(stmt, ASM_HOOK);
1151 __split_expr(stmt->asm_string);
1152 split_asm_constraints(stmt->asm_outputs);
1153 split_asm_constraints(stmt->asm_inputs);
1154 split_asm_constraints(stmt->asm_clobbers);
1155 break;
1156 case STMT_CONTEXT:
1157 break;
1158 case STMT_RANGE:
1159 __split_expr(stmt->range_expression);
1160 __split_expr(stmt->range_low);
1161 __split_expr(stmt->range_high);
1162 break;
1164 __pass_to_client(stmt, STMT_HOOK_AFTER);
1165 out:
1166 __process_post_op_stack();
1169 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1171 struct expression *expr;
1173 FOR_EACH_PTR(expr_list, expr) {
1174 expr_set_parent_expr(expr, parent);
1175 __split_expr(expr);
1176 __process_post_op_stack();
1177 } END_FOR_EACH_PTR(expr);
1180 static void split_sym(struct symbol *sym)
1182 if (!sym)
1183 return;
1184 if (!(sym->namespace & NS_SYMBOL))
1185 return;
1187 __split_stmt(sym->stmt);
1188 __split_expr(sym->array_size);
1189 split_symlist(sym->arguments);
1190 split_symlist(sym->symbol_list);
1191 __split_stmt(sym->inline_stmt);
1192 split_symlist(sym->inline_symbol_list);
1195 static void split_symlist(struct symbol_list *sym_list)
1197 struct symbol *sym;
1199 FOR_EACH_PTR(sym_list, sym) {
1200 split_sym(sym);
1201 } END_FOR_EACH_PTR(sym);
1204 typedef void (fake_cb)(struct expression *expr);
1206 static int member_to_number(struct expression *expr, struct ident *member)
1208 struct symbol *type, *tmp;
1209 char *name;
1210 int i;
1212 if (!member)
1213 return -1;
1214 name = member->name;
1216 type = get_type(expr);
1217 if (!type || type->type != SYM_STRUCT)
1218 return -1;
1220 i = -1;
1221 FOR_EACH_PTR(type->symbol_list, tmp) {
1222 i++;
1223 if (!tmp->ident)
1224 continue;
1225 if (strcmp(name, tmp->ident->name) == 0)
1226 return i;
1227 } END_FOR_EACH_PTR(tmp);
1228 return -1;
1231 static struct ident *number_to_member(struct expression *expr, int num)
1233 struct symbol *type, *member;
1234 int i = 0;
1236 type = get_type(expr);
1237 if (!type || type->type != SYM_STRUCT)
1238 return NULL;
1240 FOR_EACH_PTR(type->symbol_list, member) {
1241 if (i == num)
1242 return member->ident;
1243 i++;
1244 } END_FOR_EACH_PTR(member);
1245 return NULL;
1248 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1250 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1252 struct expression *edge_member, *assign;
1253 struct symbol *base = get_real_base_type(member);
1254 struct symbol *tmp;
1256 if (member->ident)
1257 expr = member_expression(expr, '.', member->ident);
1259 FOR_EACH_PTR(base->symbol_list, tmp) {
1260 struct symbol *type;
1262 type = get_real_base_type(tmp);
1263 if (!type)
1264 continue;
1266 edge_member = member_expression(expr, '.', tmp->ident);
1267 if (get_extra_state(edge_member))
1268 continue;
1270 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1271 set_inner_struct_members(expr, tmp);
1272 continue;
1275 if (!tmp->ident)
1276 continue;
1278 assign = assign_expression(edge_member, '=', zero_expr());
1279 __split_expr(assign);
1280 } END_FOR_EACH_PTR(tmp);
1285 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1287 struct symbol *tmp;
1288 struct expression *member = NULL;
1289 struct expression *assign;
1290 int op = '*';
1292 if (expr->type == EXPR_PREOP && expr->op == '&') {
1293 expr = strip_expr(expr->unop);
1294 op = '.';
1297 FOR_EACH_PTR(type->symbol_list, tmp) {
1298 type = get_real_base_type(tmp);
1299 if (!type)
1300 continue;
1302 if (tmp->ident) {
1303 member = member_expression(expr, op, tmp->ident);
1304 if (get_extra_state(member))
1305 continue;
1308 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1309 set_inner_struct_members(expr, tmp);
1310 continue;
1312 if (type->type == SYM_ARRAY)
1313 continue;
1314 if (!tmp->ident)
1315 continue;
1317 assign = assign_expression(member, '=', zero_expr());
1318 __split_expr(assign);
1319 } END_FOR_EACH_PTR(tmp);
1322 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1324 struct expression *deref, *assign, *tmp, *right;
1325 struct symbol *struct_type, *type;
1326 struct ident *member;
1327 int member_idx;
1329 struct_type = get_type(symbol);
1330 if (!struct_type ||
1331 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1332 return;
1335 * We're parsing an initializer that could look something like this:
1336 * struct foo foo = {
1337 * 42,
1338 * .whatever.xxx = 11,
1339 * .zzz = 12,
1340 * };
1342 * So what we have here is a list with 42, .whatever, and .zzz. We need
1343 * to break it up into left and right sides of the assignments.
1346 member_idx = 0;
1347 FOR_EACH_PTR(members, tmp) {
1348 deref = NULL;
1349 if (tmp->type == EXPR_IDENTIFIER) {
1350 member_idx = member_to_number(symbol, tmp->expr_ident);
1351 while (tmp->type == EXPR_IDENTIFIER) {
1352 member = tmp->expr_ident;
1353 tmp = tmp->ident_expression;
1354 if (deref)
1355 deref = member_expression(deref, '.', member);
1356 else
1357 deref = member_expression(symbol, '.', member);
1359 } else {
1360 member = number_to_member(symbol, member_idx);
1361 deref = member_expression(symbol, '.', member);
1363 right = tmp;
1364 member_idx++;
1365 if (right->type == EXPR_INITIALIZER) {
1366 type = get_type(deref);
1367 if (type && type->type == SYM_ARRAY)
1368 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1369 else
1370 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1371 } else {
1372 assign = assign_expression(deref, '=', right);
1373 fake_cb(assign);
1375 } END_FOR_EACH_PTR(tmp);
1377 set_unset_to_zero(struct_type, symbol);
1380 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1382 fake_member_assigns_helper(symbol_expression(sym),
1383 sym->initializer->expr_list, fake_cb);
1386 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1388 struct expression *offset, *binop, *assign, *tmp;
1389 struct symbol *type;
1390 int idx;
1392 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1393 return;
1395 idx = 0;
1396 FOR_EACH_PTR(expr_list, tmp) {
1397 if (tmp->type == EXPR_INDEX) {
1398 if (tmp->idx_from != tmp->idx_to)
1399 return;
1400 idx = tmp->idx_from;
1401 if (!tmp->idx_expression)
1402 goto next;
1403 tmp = tmp->idx_expression;
1405 offset = value_expr(idx);
1406 binop = array_element_expression(array, offset);
1407 if (tmp->type == EXPR_INITIALIZER) {
1408 type = get_type(binop);
1409 if (type && type->type == SYM_ARRAY)
1410 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1411 else
1412 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1413 } else {
1414 assign = assign_expression(binop, '=', tmp);
1415 fake_cb(assign);
1417 next:
1418 idx++;
1419 } END_FOR_EACH_PTR(tmp);
1422 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1424 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1427 static void fake_assign_expr(struct symbol *sym)
1429 struct expression *assign, *symbol;
1431 symbol = symbol_expression(sym);
1432 assign = assign_expression(symbol, '=', sym->initializer);
1433 __split_expr(assign);
1436 static void do_initializer_stuff(struct symbol *sym)
1438 if (!sym->initializer)
1439 return;
1441 if (sym->initializer->type == EXPR_INITIALIZER) {
1442 if (get_real_base_type(sym)->type == SYM_ARRAY)
1443 fake_element_assigns(sym, __split_expr);
1444 else
1445 fake_member_assigns(sym, __split_expr);
1446 } else {
1447 fake_assign_expr(sym);
1451 static void split_declaration(struct symbol_list *sym_list)
1453 struct symbol *sym;
1455 FOR_EACH_PTR(sym_list, sym) {
1456 __pass_to_client(sym, DECLARATION_HOOK);
1457 do_initializer_stuff(sym);
1458 split_sym(sym);
1459 } END_FOR_EACH_PTR(sym);
1462 static void call_global_assign_hooks(struct expression *assign)
1464 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1467 static void fake_global_assign(struct symbol *sym)
1469 struct expression *assign, *symbol;
1471 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1472 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1473 fake_element_assigns(sym, call_global_assign_hooks);
1474 } else if (sym->initializer) {
1475 symbol = symbol_expression(sym);
1476 assign = assign_expression(symbol, '=', sym->initializer);
1477 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1478 } else {
1479 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1481 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1482 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1483 fake_member_assigns(sym, call_global_assign_hooks);
1484 } else if (sym->initializer) {
1485 symbol = symbol_expression(sym);
1486 assign = assign_expression(symbol, '=', sym->initializer);
1487 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1488 } else {
1489 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1491 } else {
1492 symbol = symbol_expression(sym);
1493 if (sym->initializer)
1494 assign = assign_expression(symbol, '=', sym->initializer);
1495 else
1496 assign = assign_expression(symbol, '=', zero_expr());
1497 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1501 static void start_function_definition(struct symbol *sym)
1503 __in_function_def = 1;
1504 __pass_to_client(sym, FUNC_DEF_HOOK);
1505 __in_function_def = 0;
1506 __pass_to_client(sym, AFTER_DEF_HOOK);
1510 static void split_function(struct symbol *sym)
1512 struct symbol *base_type = get_base_type(sym);
1513 struct timeval stop;
1515 if (!base_type->stmt && !base_type->inline_stmt)
1516 return;
1518 gettimeofday(&fn_start_time, NULL);
1519 cur_func_sym = sym;
1520 if (sym->ident)
1521 cur_func = sym->ident->name;
1522 set_position(sym->pos);
1523 loop_count = 0;
1524 last_goto_statement_handled = 0;
1525 sm_debug("new function: %s\n", cur_func);
1526 __stree_id = 0;
1527 if (option_two_passes) {
1528 __unnullify_path();
1529 loop_num = 0;
1530 final_pass = 0;
1531 start_function_definition(sym);
1532 __split_stmt(base_type->stmt);
1533 __split_stmt(base_type->inline_stmt);
1534 nullify_path();
1536 __unnullify_path();
1537 loop_num = 0;
1538 final_pass = 1;
1539 start_function_definition(sym);
1540 __split_stmt(base_type->stmt);
1541 __split_stmt(base_type->inline_stmt);
1542 __pass_to_client(sym, END_FUNC_HOOK);
1543 if (need_delayed_scope_hooks())
1544 __call_scope_hooks();
1545 __pass_to_client(sym, AFTER_FUNC_HOOK);
1547 clear_all_states();
1549 gettimeofday(&stop, NULL);
1550 if (option_time && stop.tv_sec - fn_start_time.tv_sec > 2) {
1551 final_pass++;
1552 sm_msg("func_time: %lu", stop.tv_sec - fn_start_time.tv_sec);
1553 final_pass--;
1555 cur_func_sym = NULL;
1556 cur_func = NULL;
1557 free_data_info_allocs();
1558 free_expression_stack(&switch_expr_stack);
1559 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1560 __bail_on_rest_of_function = 0;
1563 static void save_flow_state(void)
1565 __add_ptr_list(&backup, INT_PTR(loop_num << 2), 0);
1566 __add_ptr_list(&backup, INT_PTR(loop_count << 2), 0);
1567 __add_ptr_list(&backup, INT_PTR(final_pass << 2), 0);
1569 __add_ptr_list(&backup, big_statement_stack, 0);
1570 __add_ptr_list(&backup, big_expression_stack, 0);
1571 __add_ptr_list(&backup, big_condition_stack, 0);
1572 __add_ptr_list(&backup, switch_expr_stack, 0);
1574 __add_ptr_list(&backup, cur_func_sym, 0);
1576 __add_ptr_list(&backup, __prev_stmt, 0);
1577 __add_ptr_list(&backup, __cur_stmt, 0);
1578 __add_ptr_list(&backup, __next_stmt, 0);
1582 static void *pop_backup(void)
1584 void *ret;
1586 ret = last_ptr_list(backup);
1587 delete_ptr_list_last(&backup);
1588 return ret;
1591 static void restore_flow_state(void)
1593 __next_stmt = pop_backup();
1594 __cur_stmt = pop_backup();
1595 __prev_stmt = pop_backup();
1597 cur_func_sym = pop_backup();
1598 switch_expr_stack = pop_backup();
1599 big_condition_stack = pop_backup();
1600 big_expression_stack = pop_backup();
1601 big_statement_stack = pop_backup();
1602 final_pass = PTR_INT(pop_backup()) >> 2;
1603 loop_count = PTR_INT(pop_backup()) >> 2;
1604 loop_num = PTR_INT(pop_backup()) >> 2;
1607 static void parse_inline(struct expression *call)
1609 struct symbol *base_type;
1610 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1611 struct timeval time_backup = fn_start_time;
1612 struct expression *orig_inline = __inline_fn;
1613 int orig_budget;
1615 save_flow_state();
1617 __pass_to_client(call, INLINE_FN_START);
1618 final_pass = 0; /* don't print anything */
1619 __inline_fn = call;
1620 orig_budget = inline_budget;
1621 inline_budget = inline_budget - 5;
1623 base_type = get_base_type(call->fn->symbol);
1624 cur_func_sym = call->fn->symbol;
1625 if (call->fn->symbol->ident)
1626 cur_func = call->fn->symbol->ident->name;
1627 else
1628 cur_func = NULL;
1629 set_position(call->fn->symbol->pos);
1631 save_all_states();
1632 big_statement_stack = NULL;
1633 big_expression_stack = NULL;
1634 big_condition_stack = NULL;
1635 switch_expr_stack = NULL;
1637 sm_debug("inline function: %s\n", cur_func);
1638 __unnullify_path();
1639 loop_num = 0;
1640 loop_count = 0;
1641 start_function_definition(call->fn->symbol);
1642 __split_stmt(base_type->stmt);
1643 __split_stmt(base_type->inline_stmt);
1644 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1645 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1647 free_expression_stack(&switch_expr_stack);
1648 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1649 nullify_path();
1650 free_goto_stack();
1652 restore_flow_state();
1653 fn_start_time = time_backup;
1654 cur_func = cur_func_bak;
1656 restore_all_states();
1657 set_position(call->pos);
1658 __inline_fn = orig_inline;
1659 inline_budget = orig_budget;
1660 __pass_to_client(call, INLINE_FN_END);
1663 static struct symbol_list *inlines_called;
1664 static void add_inline_function(struct symbol *sym)
1666 static struct symbol_list *already_added;
1667 struct symbol *tmp;
1669 FOR_EACH_PTR(already_added, tmp) {
1670 if (tmp == sym)
1671 return;
1672 } END_FOR_EACH_PTR(tmp);
1674 add_ptr_list(&already_added, sym);
1675 add_ptr_list(&inlines_called, sym);
1678 static void process_inlines(void)
1680 struct symbol *tmp;
1682 FOR_EACH_PTR(inlines_called, tmp) {
1683 split_function(tmp);
1684 } END_FOR_EACH_PTR(tmp);
1685 free_ptr_list(&inlines_called);
1688 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1690 struct symbol *sym;
1692 FOR_EACH_PTR_REVERSE(big_list, sym) {
1693 if (!sym->scope)
1694 continue;
1695 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1696 return sym;
1697 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1698 return sym;
1699 } END_FOR_EACH_PTR_REVERSE(sym);
1701 return NULL;
1704 static bool interesting_function(struct symbol *sym)
1706 static int prev_stream = -1;
1707 static bool prev_answer;
1708 const char *filename;
1709 int len;
1711 if (!(sym->ctype.modifiers & MOD_INLINE))
1712 return true;
1714 if (sym->pos.stream == prev_stream)
1715 return prev_answer;
1717 prev_stream = sym->pos.stream;
1718 prev_answer = false;
1720 filename = stream_name(sym->pos.stream);
1721 len = strlen(filename);
1722 if (len > 0 && filename[len - 1] == 'c')
1723 prev_answer = true;
1724 return prev_answer;
1727 static void split_inlines_in_scope(struct symbol *sym)
1729 struct symbol *base;
1730 struct symbol_list *scope_list;
1731 int stream;
1733 scope_list = sym->scope->symbols;
1734 stream = sym->pos.stream;
1736 /* find the last static symbol in the file */
1737 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1738 if (sym->pos.stream != stream)
1739 continue;
1740 if (sym->type != SYM_NODE)
1741 continue;
1742 base = get_base_type(sym);
1743 if (!base)
1744 continue;
1745 if (base->type != SYM_FN)
1746 continue;
1747 if (!base->inline_stmt)
1748 continue;
1749 if (!interesting_function(sym))
1750 continue;
1751 add_inline_function(sym);
1752 } END_FOR_EACH_PTR_REVERSE(sym);
1754 process_inlines();
1757 static void split_inlines(struct symbol_list *sym_list)
1759 struct symbol *sym;
1761 sym = get_last_scoped_symbol(sym_list, 0);
1762 if (sym)
1763 split_inlines_in_scope(sym);
1764 sym = get_last_scoped_symbol(sym_list, 1);
1765 if (sym)
1766 split_inlines_in_scope(sym);
1769 static struct stree *clone_estates_perm(struct stree *orig)
1771 struct stree *ret = NULL;
1772 struct sm_state *tmp;
1774 FOR_EACH_SM(orig, tmp) {
1775 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1776 } END_FOR_EACH_SM(tmp);
1778 return ret;
1781 struct position last_pos;
1782 static void split_c_file_functions(struct symbol_list *sym_list)
1784 struct symbol *sym;
1786 __unnullify_path();
1787 FOR_EACH_PTR(sym_list, sym) {
1788 set_position(sym->pos);
1789 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1790 __pass_to_client(sym, BASE_HOOK);
1791 fake_global_assign(sym);
1793 } END_FOR_EACH_PTR(sym);
1794 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1795 nullify_path();
1797 FOR_EACH_PTR(sym_list, sym) {
1798 set_position(sym->pos);
1799 last_pos = sym->pos;
1800 if (!interesting_function(sym))
1801 continue;
1802 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1803 split_function(sym);
1804 process_inlines();
1806 last_pos = sym->pos;
1807 } END_FOR_EACH_PTR(sym);
1808 split_inlines(sym_list);
1809 __pass_to_client(sym_list, END_FILE_HOOK);
1812 static int final_before_fake;
1813 void init_fake_env(void)
1815 if (!in_fake_env)
1816 final_before_fake = final_pass;
1817 in_fake_env++;
1818 __push_fake_cur_stree();
1819 final_pass = 0;
1822 void end_fake_env(void)
1824 __pop_fake_cur_stree();
1825 in_fake_env--;
1826 if (!in_fake_env)
1827 final_pass = final_before_fake;
1830 static void open_output_files(char *base_file)
1832 char buf[256];
1834 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1835 sm_outfd = fopen(buf, "w");
1836 if (!sm_outfd) {
1837 printf("Error: Cannot open %s\n", buf);
1838 exit(1);
1841 if (!option_info)
1842 return;
1844 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1845 sql_outfd = fopen(buf, "w");
1846 if (!sql_outfd) {
1847 printf("Error: Cannot open %s\n", buf);
1848 exit(1);
1851 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1852 caller_info_fd = fopen(buf, "w");
1853 if (!caller_info_fd) {
1854 printf("Error: Cannot open %s\n", buf);
1855 exit(1);
1859 void smatch(int argc, char **argv)
1861 struct string_list *filelist = NULL;
1862 struct symbol_list *sym_list;
1863 struct timeval stop, start;
1864 char *path;
1865 int len;
1867 gettimeofday(&start, NULL);
1869 if (argc < 2) {
1870 printf("Usage: smatch [--debug] <filename.c>\n");
1871 exit(1);
1873 sparse_initialize(argc, argv, &filelist);
1874 set_valid_ptr_max();
1875 alloc_valid_ptr_rl();
1876 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1877 path = getcwd(NULL, 0);
1878 free(full_base_file);
1879 if (path) {
1880 len = strlen(path) + 1 + strlen(base_file) + 1;
1881 full_base_file = malloc(len);
1882 snprintf(full_base_file, len, "%s/%s", path, base_file);
1883 } else {
1884 full_base_file = alloc_string(base_file);
1886 if (option_file_output)
1887 open_output_files(base_file);
1888 sym_list = sparse_keep_tokens(base_file);
1889 split_c_file_functions(sym_list);
1890 } END_FOR_EACH_PTR_NOTAG(base_file);
1892 gettimeofday(&stop, NULL);
1894 set_position(last_pos);
1895 if (option_time)
1896 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1897 if (option_mem)
1898 sm_msg("mem: %luKb", get_max_memory());