user_data: update a comment
[smatch.git] / smatch_flow.c
blob9d6b961ad27923de74e0332577c0b5ea560bc421
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 int __in_unmatched_hook;
48 static struct expression_list *switch_expr_stack = NULL;
49 static struct expression_list *post_op_stack = NULL;
51 static struct ptr_list *backup;
53 struct expression_list *big_expression_stack;
54 struct statement_list *big_statement_stack;
55 struct statement *__prev_stmt;
56 struct statement *__cur_stmt;
57 struct statement *__next_stmt;
58 int __in_pre_condition = 0;
59 int __bail_on_rest_of_function = 0;
60 static struct timeval fn_start_time;
61 static struct timeval outer_fn_start_time;
62 char *get_function(void) { return cur_func; }
63 int get_lineno(void) { return __smatch_lineno; }
64 int inside_loop(void) { return !!loop_count; }
65 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
66 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
67 int in_expression_statement(void) { return !!__expr_stmt_count; }
69 static void split_symlist(struct symbol_list *sym_list);
70 static void split_declaration(struct symbol_list *sym_list);
71 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
72 static void add_inline_function(struct symbol *sym);
73 static void parse_inline(struct expression *expr);
75 int option_assume_loops = 0;
76 int option_two_passes = 0;
77 struct symbol *cur_func_sym = NULL;
78 struct stree *global_states;
80 const unsigned long valid_ptr_min = 4096;
81 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
82 const sval_t valid_ptr_min_sval = {
83 .type = &ptr_ctype,
84 {.value = 4096},
86 sval_t valid_ptr_max_sval = {
87 .type = &ptr_ctype,
88 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
90 struct range_list *valid_ptr_rl;
92 void alloc_valid_ptr_rl(void)
94 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
95 valid_ptr_max_sval.value = valid_ptr_max;
97 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
98 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
99 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
102 int outside_of_function(void)
104 return cur_func_sym == NULL;
107 const char *get_filename(void)
109 if (option_info && option_full_path)
110 return full_base_file;
111 if (option_info)
112 return base_file;
113 if (option_full_path)
114 return full_filename;
115 return filename;
118 const char *get_base_file(void)
120 if (option_full_path)
121 return full_base_file;
122 return base_file;
125 static void set_position(struct position pos)
127 int len;
128 static int prev_stream = -1;
130 if (in_fake_env)
131 return;
133 if (pos.stream == 0 && pos.line == 0)
134 return;
136 __smatch_lineno = pos.line;
138 if (pos.stream == prev_stream)
139 return;
141 filename = stream_name(pos.stream);
143 free(full_filename);
144 pathname = getcwd(NULL, 0);
145 if (pathname) {
146 len = strlen(pathname) + 1 + strlen(filename) + 1;
147 full_filename = malloc(len);
148 snprintf(full_filename, len, "%s/%s", pathname, filename);
149 } else {
150 full_filename = alloc_string(filename);
152 free(pathname);
155 int is_assigned_call(struct expression *expr)
157 struct expression *parent = expr_get_parent_expr(expr);
159 if (parent &&
160 parent->type == EXPR_ASSIGNMENT &&
161 parent->op == '=' &&
162 strip_expr(parent->right) == expr)
163 return 1;
165 return 0;
168 static int is_inline_func(struct expression *expr)
170 if (expr->type != EXPR_SYMBOL || !expr->symbol)
171 return 0;
172 if (expr->symbol->ctype.modifiers & MOD_INLINE)
173 return 1;
174 return 0;
177 static int is_noreturn_func(struct expression *expr)
179 if (expr->type != EXPR_SYMBOL || !expr->symbol)
180 return 0;
181 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
182 return 1;
183 return 0;
186 static int inline_budget = 20;
188 int inlinable(struct expression *expr)
190 struct symbol *sym;
191 struct statement *last_stmt = NULL;
193 if (__inline_fn) /* don't nest */
194 return 0;
196 if (expr->type != EXPR_SYMBOL || !expr->symbol)
197 return 0;
198 if (is_no_inline_function(expr->symbol->ident->name))
199 return 0;
200 sym = get_base_type(expr->symbol);
201 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
202 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
203 return 0;
204 if (sym->stmt->type != STMT_COMPOUND)
205 return 0;
206 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
208 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
209 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
210 return 0;
211 if (sym->inline_stmt->type != STMT_COMPOUND)
212 return 0;
213 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
216 if (!last_stmt)
217 return 0;
219 /* the magic numbers in this function are pulled out of my bum. */
220 if (last_stmt->pos.line > sym->pos.line + inline_budget)
221 return 0;
223 return 1;
226 void __process_post_op_stack(void)
228 struct expression *expr;
230 FOR_EACH_PTR(post_op_stack, expr) {
231 __pass_to_client(expr, OP_HOOK);
232 } END_FOR_EACH_PTR(expr);
234 __free_ptr_list((struct ptr_list **)&post_op_stack);
237 static int handle_comma_assigns(struct expression *expr)
239 struct expression *right;
240 struct expression *assign;
242 right = strip_expr(expr->right);
243 if (right->type != EXPR_COMMA)
244 return 0;
246 __split_expr(right->left);
247 __process_post_op_stack();
249 assign = assign_expression(expr->left, '=', right->right);
250 __split_expr(assign);
252 return 1;
255 /* This is to handle *p++ = foo; assignments */
256 static int handle_postop_assigns(struct expression *expr)
258 struct expression *left, *fake_left;
259 struct expression *assign;
261 left = strip_expr(expr->left);
262 if (left->type != EXPR_PREOP || left->op != '*')
263 return 0;
264 left = strip_expr(left->unop);
265 if (left->type != EXPR_POSTOP)
266 return 0;
268 fake_left = deref_expression(strip_expr(left->unop));
269 assign = assign_expression(fake_left, '=', expr->right);
271 __split_expr(assign);
272 __split_expr(expr->left);
274 return 1;
277 static int prev_expression_is_getting_address(struct expression *expr)
279 struct expression *parent;
281 do {
282 parent = expr_get_parent_expr(expr);
284 if (!parent)
285 return 0;
286 if (parent->type == EXPR_PREOP && parent->op == '&')
287 return 1;
288 if (parent->type == EXPR_PREOP && parent->op == '(')
289 goto next;
290 if (parent->type == EXPR_DEREF && parent->op == '.')
291 goto next;
293 return 0;
294 next:
295 expr = parent;
296 } while (1);
299 static void handle_builtin_overflow_func(struct expression *expr)
301 struct expression *a, *b, *res, *assign;
302 int op;
304 if (sym_name_is("__builtin_add_overflow", expr->fn))
305 op = '+';
306 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
307 op = '-';
308 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
309 op = '*';
310 else
311 return;
313 a = get_argument_from_call_expr(expr->args, 0);
314 b = get_argument_from_call_expr(expr->args, 1);
315 res = get_argument_from_call_expr(expr->args, 2);
317 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
318 __split_expr(assign);
321 static int handle__builtin_choose_expr(struct expression *expr)
323 struct expression *const_expr, *expr1, *expr2;
324 sval_t sval;
326 if (!sym_name_is("__builtin_choose_expr", expr->fn))
327 return 0;
329 const_expr = get_argument_from_call_expr(expr->args, 0);
330 expr1 = get_argument_from_call_expr(expr->args, 1);
331 expr2 = get_argument_from_call_expr(expr->args, 2);
333 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
334 return 0;
335 if (sval.value)
336 __split_expr(expr1);
337 else
338 __split_expr(expr2);
339 return 1;
342 static int handle__builtin_choose_expr_assigns(struct expression *expr)
344 struct expression *const_expr, *right, *expr1, *expr2, *fake;
345 sval_t sval;
347 right = strip_expr(expr->right);
348 if (right->type != EXPR_CALL)
349 return 0;
350 if (!sym_name_is("__builtin_choose_expr", right->fn))
351 return 0;
353 const_expr = get_argument_from_call_expr(right->args, 0);
354 expr1 = get_argument_from_call_expr(right->args, 1);
355 expr2 = get_argument_from_call_expr(right->args, 2);
357 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
358 return 0;
360 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
361 __split_expr(fake);
362 return 1;
365 void __split_expr(struct expression *expr)
367 if (!expr)
368 return;
370 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
372 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
373 return;
374 if (__in_fake_assign >= 4) /* don't allow too much nesting */
375 return;
377 push_expression(&big_expression_stack, expr);
378 set_position(expr->pos);
379 __pass_to_client(expr, EXPR_HOOK);
381 switch (expr->type) {
382 case EXPR_PREOP:
383 expr_set_parent_expr(expr->unop, expr);
385 if (expr->op == '*' &&
386 !prev_expression_is_getting_address(expr))
387 __pass_to_client(expr, DEREF_HOOK);
388 __split_expr(expr->unop);
389 __pass_to_client(expr, OP_HOOK);
390 break;
391 case EXPR_POSTOP:
392 expr_set_parent_expr(expr->unop, expr);
394 __split_expr(expr->unop);
395 push_expression(&post_op_stack, expr);
396 break;
397 case EXPR_STATEMENT:
398 __expr_stmt_count++;
399 if (expr->statement && !expr->statement) {
400 stmt_set_parent_stmt(expr->statement,
401 last_ptr_list((struct ptr_list *)big_statement_stack));
403 __split_stmt(expr->statement);
404 __expr_stmt_count--;
405 break;
406 case EXPR_LOGICAL:
407 case EXPR_COMPARE:
408 expr_set_parent_expr(expr->left, expr);
409 expr_set_parent_expr(expr->right, expr);
411 __pass_to_client(expr, LOGIC_HOOK);
412 __handle_logic(expr);
413 break;
414 case EXPR_BINOP:
415 expr_set_parent_expr(expr->left, expr);
416 expr_set_parent_expr(expr->right, expr);
418 __pass_to_client(expr, BINOP_HOOK);
419 case EXPR_COMMA:
420 expr_set_parent_expr(expr->left, expr);
421 expr_set_parent_expr(expr->right, expr);
423 __split_expr(expr->left);
424 __process_post_op_stack();
425 __split_expr(expr->right);
426 break;
427 case EXPR_ASSIGNMENT: {
428 struct expression *right;
430 expr_set_parent_expr(expr->left, expr);
431 expr_set_parent_expr(expr->right, expr);
433 right = strip_expr(expr->right);
434 if (!right)
435 break;
437 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
439 /* foo = !bar() */
440 if (__handle_condition_assigns(expr))
441 goto after_assign;
442 /* foo = (x < 5 ? foo : 5); */
443 if (__handle_select_assigns(expr))
444 goto after_assign;
445 /* foo = ({frob(); frob(); frob(); 1;}) */
446 if (__handle_expr_statement_assigns(expr))
447 break; // FIXME: got after
448 /* foo = (3, 4); */
449 if (handle_comma_assigns(expr))
450 goto after_assign;
451 if (handle__builtin_choose_expr_assigns(expr))
452 goto after_assign;
453 if (handle_postop_assigns(expr))
454 break; /* no need to goto after_assign */
456 __split_expr(expr->right);
457 if (outside_of_function())
458 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
459 else
460 __pass_to_client(expr, ASSIGNMENT_HOOK);
462 __fake_struct_member_assignments(expr);
464 /* Re-examine ->right for inlines. See the commit message */
465 right = strip_expr(expr->right);
466 if (expr->op == '=' && right->type == EXPR_CALL)
467 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
469 if (get_macro_name(right->pos) &&
470 get_macro_name(expr->pos) != get_macro_name(right->pos))
471 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
473 after_assign:
474 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
475 __split_expr(expr->left);
476 break;
478 case EXPR_DEREF:
479 expr_set_parent_expr(expr->deref, expr);
481 __pass_to_client(expr, DEREF_HOOK);
482 __split_expr(expr->deref);
483 break;
484 case EXPR_SLICE:
485 expr_set_parent_expr(expr->base, expr);
487 __split_expr(expr->base);
488 break;
489 case EXPR_CAST:
490 case EXPR_FORCE_CAST:
491 expr_set_parent_expr(expr->cast_expression, expr);
493 __pass_to_client(expr, CAST_HOOK);
494 __split_expr(expr->cast_expression);
495 break;
496 case EXPR_SIZEOF:
497 if (expr->cast_expression)
498 __pass_to_client(strip_parens(expr->cast_expression),
499 SIZEOF_HOOK);
500 break;
501 case EXPR_OFFSETOF:
502 case EXPR_ALIGNOF:
503 break;
504 case EXPR_CONDITIONAL:
505 case EXPR_SELECT:
506 expr_set_parent_expr(expr->conditional, expr);
507 expr_set_parent_expr(expr->cond_true, expr);
508 expr_set_parent_expr(expr->cond_false, expr);
510 if (known_condition_true(expr->conditional)) {
511 __split_expr(expr->cond_true);
512 break;
514 if (known_condition_false(expr->conditional)) {
515 __split_expr(expr->cond_false);
516 break;
518 __pass_to_client(expr, SELECT_HOOK);
519 __split_whole_condition(expr->conditional);
520 __split_expr(expr->cond_true);
521 __push_true_states();
522 __use_false_states();
523 __split_expr(expr->cond_false);
524 __merge_true_states();
525 break;
526 case EXPR_CALL:
527 expr_set_parent_expr(expr->fn, expr);
529 if (sym_name_is("__builtin_constant_p", expr->fn))
530 break;
531 if (handle__builtin_choose_expr(expr))
532 break;
533 __split_expr(expr->fn);
534 split_expr_list(expr->args, expr);
535 if (is_inline_func(expr->fn))
536 add_inline_function(expr->fn->symbol);
537 if (inlinable(expr->fn))
538 __inline_call = 1;
539 __process_post_op_stack();
540 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
541 __pass_to_client(expr, FUNCTION_CALL_HOOK);
542 __inline_call = 0;
543 if (inlinable(expr->fn)) {
544 parse_inline(expr);
546 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
547 if (is_noreturn_func(expr->fn))
548 nullify_path();
549 handle_builtin_overflow_func(expr);
550 break;
551 case EXPR_INITIALIZER:
552 split_expr_list(expr->expr_list, expr);
553 break;
554 case EXPR_IDENTIFIER:
555 expr_set_parent_expr(expr->ident_expression, expr);
556 __split_expr(expr->ident_expression);
557 break;
558 case EXPR_INDEX:
559 expr_set_parent_expr(expr->idx_expression, expr);
560 __split_expr(expr->idx_expression);
561 break;
562 case EXPR_POS:
563 expr_set_parent_expr(expr->init_expr, expr);
564 __split_expr(expr->init_expr);
565 break;
566 case EXPR_SYMBOL:
567 __pass_to_client(expr, SYM_HOOK);
568 break;
569 case EXPR_STRING:
570 __pass_to_client(expr, STRING_HOOK);
571 break;
572 default:
573 break;
575 __pass_to_client(expr, EXPR_HOOK_AFTER);
576 pop_expression(&big_expression_stack);
579 static int is_forever_loop(struct statement *stmt)
581 struct expression *expr;
582 sval_t sval;
584 expr = strip_expr(stmt->iterator_pre_condition);
585 if (!expr)
586 expr = stmt->iterator_post_condition;
587 if (!expr) {
588 /* this is a for(;;) loop... */
589 return 1;
592 if (get_value(expr, &sval) && sval.value != 0)
593 return 1;
595 return 0;
598 static int loop_num;
599 static char *get_loop_name(int num)
601 char buf[256];
603 snprintf(buf, 255, "-loop%d", num);
604 buf[255] = '\0';
605 return alloc_sname(buf);
609 * Pre Loops are while and for loops.
611 static void handle_pre_loop(struct statement *stmt)
613 int once_through; /* we go through the loop at least once */
614 struct sm_state *extra_sm = NULL;
615 int unchanged = 0;
616 char *loop_name;
617 struct stree *stree = NULL;
618 struct sm_state *sm = NULL;
620 loop_name = get_loop_name(loop_num);
621 loop_num++;
623 __split_stmt(stmt->iterator_pre_statement);
624 __prev_stmt = stmt->iterator_pre_statement;
626 once_through = implied_condition_true(stmt->iterator_pre_condition);
628 loop_count++;
629 __push_continues();
630 __push_breaks();
632 __merge_gotos(loop_name, NULL);
634 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
635 __in_pre_condition++;
636 __pass_to_client(stmt, PRELOOP_HOOK);
637 __split_whole_condition(stmt->iterator_pre_condition);
638 __in_pre_condition--;
639 FOR_EACH_SM(stree, sm) {
640 set_state(sm->owner, sm->name, sm->sym, sm->state);
641 } END_FOR_EACH_SM(sm);
642 free_stree(&stree);
643 if (extra_sm)
644 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
646 if (option_assume_loops)
647 once_through = 1;
649 __split_stmt(stmt->iterator_statement);
650 if (is_forever_loop(stmt)) {
651 __merge_continues();
652 __save_gotos(loop_name, NULL);
654 __push_fake_cur_stree();
655 __split_stmt(stmt->iterator_post_statement);
656 stree = __pop_fake_cur_stree();
658 __discard_false_states();
659 __use_breaks();
661 if (!__path_is_null())
662 __merge_stree_into_cur(stree);
663 free_stree(&stree);
664 } else {
665 __merge_continues();
666 unchanged = __iterator_unchanged(extra_sm);
667 __split_stmt(stmt->iterator_post_statement);
668 __prev_stmt = stmt->iterator_post_statement;
669 __cur_stmt = stmt;
671 __save_gotos(loop_name, NULL);
672 __in_pre_condition++;
673 __split_whole_condition(stmt->iterator_pre_condition);
674 __in_pre_condition--;
675 nullify_path();
676 __merge_false_states();
677 if (once_through)
678 __discard_false_states();
679 else
680 __merge_false_states();
682 if (extra_sm && unchanged)
683 __extra_pre_loop_hook_after(extra_sm,
684 stmt->iterator_post_statement,
685 stmt->iterator_pre_condition);
686 __merge_breaks();
688 loop_count--;
692 * Post loops are do {} while();
694 static void handle_post_loop(struct statement *stmt)
696 char *loop_name;
698 loop_name = get_loop_name(loop_num);
699 loop_num++;
700 loop_count++;
702 __push_continues();
703 __push_breaks();
704 __merge_gotos(loop_name, NULL);
705 __split_stmt(stmt->iterator_statement);
706 __merge_continues();
707 if (!expr_is_zero(stmt->iterator_post_condition))
708 __save_gotos(loop_name, NULL);
710 if (is_forever_loop(stmt)) {
711 __use_breaks();
712 } else {
713 __split_whole_condition(stmt->iterator_post_condition);
714 __use_false_states();
715 __merge_breaks();
717 loop_count--;
720 static int empty_statement(struct statement *stmt)
722 if (!stmt)
723 return 0;
724 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
725 return 1;
726 return 0;
729 static int last_stmt_on_same_line(void)
731 struct statement *stmt;
732 int i = 0;
734 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
735 if (!i++)
736 continue;
737 if (stmt->pos.line == get_lineno())
738 return 1;
739 return 0;
740 } END_FOR_EACH_PTR_REVERSE(stmt);
741 return 0;
744 static void split_asm_ops(struct asm_operand_list *ops)
746 struct asm_operand *op;
748 FOR_EACH_PTR(ops, op) {
749 __split_expr(op->expr);
750 } END_FOR_EACH_PTR(op);
753 static int is_case_val(struct statement *stmt, sval_t sval)
755 sval_t case_sval;
757 if (stmt->type != STMT_CASE)
758 return 0;
759 if (!stmt->case_expression) {
760 __set_default();
761 return 1;
763 if (!get_value(stmt->case_expression, &case_sval))
764 return 0;
765 if (case_sval.value == sval.value)
766 return 1;
767 return 0;
770 static struct range_list *get_case_rl(struct expression *switch_expr,
771 struct expression *case_expr,
772 struct expression *case_to)
774 sval_t start, end;
775 struct range_list *rl = NULL;
776 struct symbol *switch_type;
778 switch_type = get_type(switch_expr);
779 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
780 start = sval_cast(switch_type, start);
781 end = sval_cast(switch_type, end);
782 add_range(&rl, start, end);
783 } else if (get_value(case_expr, &start)) {
784 start = sval_cast(switch_type, start);
785 add_range(&rl, start, start);
788 return rl;
791 static void split_known_switch(struct statement *stmt, sval_t sval)
793 struct statement *tmp;
794 struct range_list *rl;
796 __split_expr(stmt->switch_expression);
797 sval = sval_cast(get_type(stmt->switch_expression), sval);
799 push_expression(&switch_expr_stack, stmt->switch_expression);
800 __save_switch_states(top_expression(switch_expr_stack));
801 nullify_path();
802 __push_default();
803 __push_breaks();
805 stmt = stmt->switch_statement;
807 __push_scope_hooks();
808 FOR_EACH_PTR(stmt->stmts, tmp) {
809 __smatch_lineno = tmp->pos.line;
810 if (is_case_val(tmp, sval)) {
811 rl = alloc_rl(sval, sval);
812 __merge_switches(top_expression(switch_expr_stack), rl);
813 __pass_case_to_client(top_expression(switch_expr_stack), rl);
815 if (__path_is_null())
816 continue;
817 __split_stmt(tmp);
818 if (__path_is_null()) {
819 __set_default();
820 goto out;
822 } END_FOR_EACH_PTR(tmp);
823 out:
824 __call_scope_hooks();
825 if (!__pop_default())
826 __merge_switches(top_expression(switch_expr_stack), NULL);
827 __discard_switches();
828 __merge_breaks();
829 pop_expression(&switch_expr_stack);
832 static void split_case(struct statement *stmt)
834 struct range_list *rl = NULL;
836 expr_set_parent_stmt(stmt->case_expression, stmt);
837 expr_set_parent_stmt(stmt->case_to, stmt);
839 rl = get_case_rl(top_expression(switch_expr_stack),
840 stmt->case_expression, stmt->case_to);
841 while (stmt->case_statement->type == STMT_CASE) {
842 struct range_list *tmp;
844 tmp = get_case_rl(top_expression(switch_expr_stack),
845 stmt->case_statement->case_expression,
846 stmt->case_statement->case_to);
847 if (!tmp)
848 break;
849 rl = rl_union(rl, tmp);
850 if (!stmt->case_expression)
851 __set_default();
852 stmt = stmt->case_statement;
855 __merge_switches(top_expression(switch_expr_stack), rl);
857 if (!stmt->case_expression)
858 __set_default();
859 __split_stmt(stmt->case_statement);
862 int time_parsing_function(void)
864 return ms_since(&fn_start_time) / 1000;
867 bool taking_too_long(void)
869 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
870 return 1;
871 return 0;
874 static int is_last_stmt(struct statement *cur_stmt)
876 struct symbol *fn;
877 struct statement *stmt;
879 if (!cur_func_sym)
880 return 0;
881 fn = get_base_type(cur_func_sym);
882 if (!fn)
883 return 0;
884 stmt = fn->stmt;
885 if (!stmt)
886 stmt = fn->inline_stmt;
887 if (!stmt || stmt->type != STMT_COMPOUND)
888 return 0;
889 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
890 if (stmt && stmt->type == STMT_LABEL)
891 stmt = stmt->label_statement;
892 if (stmt == cur_stmt)
893 return 1;
894 return 0;
897 static void handle_backward_goto(struct statement *goto_stmt)
899 const char *goto_name, *label_name;
900 struct statement *func_stmt;
901 struct symbol *base_type = get_base_type(cur_func_sym);
902 struct statement *tmp;
903 int found = 0;
905 if (!option_info)
906 return;
907 if (last_goto_statement_handled)
908 return;
909 last_goto_statement_handled = 1;
911 if (!goto_stmt->goto_label ||
912 goto_stmt->goto_label->type != SYM_LABEL ||
913 !goto_stmt->goto_label->ident)
914 return;
915 goto_name = goto_stmt->goto_label->ident->name;
917 func_stmt = base_type->stmt;
918 if (!func_stmt)
919 func_stmt = base_type->inline_stmt;
920 if (!func_stmt)
921 return;
922 if (func_stmt->type != STMT_COMPOUND)
923 return;
925 FOR_EACH_PTR(func_stmt->stmts, tmp) {
926 if (!found) {
927 if (tmp->type != STMT_LABEL)
928 continue;
929 if (!tmp->label_identifier ||
930 tmp->label_identifier->type != SYM_LABEL ||
931 !tmp->label_identifier->ident)
932 continue;
933 label_name = tmp->label_identifier->ident->name;
934 if (strcmp(goto_name, label_name) != 0)
935 continue;
936 found = 1;
938 __split_stmt(tmp);
939 } END_FOR_EACH_PTR(tmp);
942 static void fake_a_return(void)
944 struct symbol *return_type;
946 nullify_path();
947 __unnullify_path();
949 return_type = get_real_base_type(cur_func_sym);
950 return_type = get_real_base_type(return_type);
951 if (return_type != &void_ctype) {
952 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
953 nullify_path();
957 static void fake_an_empty_default(struct position pos)
959 static struct statement none = {};
961 none.pos = pos;
962 none.type = STMT_NONE;
963 __merge_switches(top_expression(switch_expr_stack), NULL);
964 __split_stmt(&none);
967 static void split_compound(struct statement *stmt)
969 struct statement *prev = NULL;
970 struct statement *cur = NULL;
971 struct statement *next;
973 __push_scope_hooks();
975 FOR_EACH_PTR(stmt->stmts, next) {
976 /* just set them all ahead of time */
977 stmt_set_parent_stmt(next, stmt);
979 if (cur) {
980 __prev_stmt = prev;
981 __next_stmt = next;
982 __cur_stmt = cur;
983 __split_stmt(cur);
985 prev = cur;
986 cur = next;
987 } END_FOR_EACH_PTR(next);
988 if (cur) {
989 __prev_stmt = prev;
990 __cur_stmt = cur;
991 __next_stmt = NULL;
992 __split_stmt(cur);
996 * For function scope, then delay calling the scope hooks until the
997 * end of function hooks can run. I'm not positive this is the right
998 * thing...
1000 if (!is_last_stmt(cur))
1001 __call_scope_hooks();
1005 * This is a hack, work around for detecting empty functions.
1007 static int need_delayed_scope_hooks(void)
1009 struct symbol *fn = get_base_type(cur_func_sym);
1010 struct statement *stmt;
1012 if (!fn)
1013 return 0;
1014 stmt = fn->stmt;
1015 if (!stmt)
1016 stmt = fn->inline_stmt;
1017 if (stmt && stmt->type == STMT_COMPOUND)
1018 return 1;
1019 return 0;
1022 void __split_label_stmt(struct statement *stmt)
1024 if (stmt->label_identifier &&
1025 stmt->label_identifier->type == SYM_LABEL &&
1026 stmt->label_identifier->ident) {
1027 loop_count |= 0x0800000;
1028 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1032 static void find_asm_gotos(struct statement *stmt)
1034 struct symbol *sym;
1036 FOR_EACH_PTR(stmt->asm_labels, sym) {
1037 __save_gotos(sym->ident->name, sym);
1038 } END_FOR_EACH_PTR(sym);
1041 void __split_stmt(struct statement *stmt)
1043 sval_t sval;
1045 if (!stmt)
1046 goto out;
1048 if (!__in_fake_assign)
1049 __silence_warnings_for_stmt = false;
1051 if (__bail_on_rest_of_function || is_skipped_function())
1052 return;
1054 if (out_of_memory() || taking_too_long()) {
1055 struct timeval stop;
1057 gettimeofday(&stop, NULL);
1059 __bail_on_rest_of_function = 1;
1060 final_pass = 1;
1061 sm_perror("Function too hairy. Giving up. %lu seconds",
1062 stop.tv_sec - fn_start_time.tv_sec);
1063 fake_a_return();
1064 final_pass = 0; /* turn off sm_msg() from here */
1065 return;
1068 add_ptr_list(&big_statement_stack, stmt);
1069 free_expression_stack(&big_expression_stack);
1070 set_position(stmt->pos);
1071 __pass_to_client(stmt, STMT_HOOK);
1073 switch (stmt->type) {
1074 case STMT_DECLARATION:
1075 split_declaration(stmt->declaration);
1076 break;
1077 case STMT_RETURN:
1078 expr_set_parent_stmt(stmt->ret_value, stmt);
1080 __split_expr(stmt->ret_value);
1081 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1082 __process_post_op_stack();
1083 nullify_path();
1084 break;
1085 case STMT_EXPRESSION:
1086 expr_set_parent_stmt(stmt->expression, stmt);
1087 expr_set_parent_stmt(stmt->context, stmt);
1089 __split_expr(stmt->expression);
1090 break;
1091 case STMT_COMPOUND:
1092 split_compound(stmt);
1093 break;
1094 case STMT_IF:
1095 stmt_set_parent_stmt(stmt->if_true, stmt);
1096 stmt_set_parent_stmt(stmt->if_false, stmt);
1097 expr_set_parent_stmt(stmt->if_conditional, stmt);
1099 if (known_condition_true(stmt->if_conditional)) {
1100 __split_stmt(stmt->if_true);
1101 break;
1103 if (known_condition_false(stmt->if_conditional)) {
1104 __split_stmt(stmt->if_false);
1105 break;
1107 __split_whole_condition(stmt->if_conditional);
1108 __split_stmt(stmt->if_true);
1109 if (empty_statement(stmt->if_true) &&
1110 last_stmt_on_same_line() &&
1111 !get_macro_name(stmt->if_true->pos))
1112 sm_warning("if();");
1113 __push_true_states();
1114 __use_false_states();
1115 __split_stmt(stmt->if_false);
1116 __merge_true_states();
1117 break;
1118 case STMT_ITERATOR:
1119 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1120 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1121 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1122 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1123 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1125 if (stmt->iterator_pre_condition)
1126 handle_pre_loop(stmt);
1127 else if (stmt->iterator_post_condition)
1128 handle_post_loop(stmt);
1129 else {
1130 // these are for(;;) type loops.
1131 handle_pre_loop(stmt);
1133 break;
1134 case STMT_SWITCH:
1135 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1136 expr_set_parent_stmt(stmt->switch_expression, stmt);
1138 if (get_value(stmt->switch_expression, &sval)) {
1139 split_known_switch(stmt, sval);
1140 break;
1142 __split_expr(stmt->switch_expression);
1143 push_expression(&switch_expr_stack, stmt->switch_expression);
1144 __save_switch_states(top_expression(switch_expr_stack));
1145 nullify_path();
1146 __push_default();
1147 __push_breaks();
1148 __split_stmt(stmt->switch_statement);
1149 if (!__pop_default() && have_remaining_cases())
1150 fake_an_empty_default(stmt->pos);
1151 __discard_switches();
1152 __merge_breaks();
1153 pop_expression(&switch_expr_stack);
1154 break;
1155 case STMT_CASE:
1156 split_case(stmt);
1157 break;
1158 case STMT_LABEL:
1159 __split_label_stmt(stmt);
1160 __split_stmt(stmt->label_statement);
1161 break;
1162 case STMT_GOTO:
1163 expr_set_parent_stmt(stmt->goto_expression, stmt);
1165 __split_expr(stmt->goto_expression);
1166 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1167 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1168 __process_breaks();
1169 } else if (!strcmp(stmt->goto_label->ident->name,
1170 "continue")) {
1171 __process_continues();
1173 } else if (stmt->goto_label &&
1174 stmt->goto_label->type == SYM_LABEL &&
1175 stmt->goto_label->ident) {
1176 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1178 nullify_path();
1179 if (is_last_stmt(stmt))
1180 handle_backward_goto(stmt);
1181 break;
1182 case STMT_NONE:
1183 break;
1184 case STMT_ASM:
1185 expr_set_parent_stmt(stmt->asm_string, stmt);
1187 find_asm_gotos(stmt);
1188 __pass_to_client(stmt, ASM_HOOK);
1189 __split_expr(stmt->asm_string);
1190 split_asm_ops(stmt->asm_outputs);
1191 split_asm_ops(stmt->asm_inputs);
1192 split_expr_list(stmt->asm_clobbers, NULL);
1193 break;
1194 case STMT_CONTEXT:
1195 break;
1196 case STMT_RANGE:
1197 __split_expr(stmt->range_expression);
1198 __split_expr(stmt->range_low);
1199 __split_expr(stmt->range_high);
1200 break;
1202 __pass_to_client(stmt, STMT_HOOK_AFTER);
1203 out:
1204 __process_post_op_stack();
1207 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1209 struct expression *expr;
1211 FOR_EACH_PTR(expr_list, expr) {
1212 expr_set_parent_expr(expr, parent);
1213 __split_expr(expr);
1214 __process_post_op_stack();
1215 } END_FOR_EACH_PTR(expr);
1218 static void split_sym(struct symbol *sym)
1220 if (!sym)
1221 return;
1222 if (!(sym->namespace & NS_SYMBOL))
1223 return;
1225 __split_stmt(sym->stmt);
1226 __split_expr(sym->array_size);
1227 split_symlist(sym->arguments);
1228 split_symlist(sym->symbol_list);
1229 __split_stmt(sym->inline_stmt);
1230 split_symlist(sym->inline_symbol_list);
1233 static void split_symlist(struct symbol_list *sym_list)
1235 struct symbol *sym;
1237 FOR_EACH_PTR(sym_list, sym) {
1238 split_sym(sym);
1239 } END_FOR_EACH_PTR(sym);
1242 typedef void (fake_cb)(struct expression *expr);
1244 static int member_to_number(struct expression *expr, struct ident *member)
1246 struct symbol *type, *tmp;
1247 char *name;
1248 int i;
1250 if (!member)
1251 return -1;
1252 name = member->name;
1254 type = get_type(expr);
1255 if (!type || type->type != SYM_STRUCT)
1256 return -1;
1258 i = -1;
1259 FOR_EACH_PTR(type->symbol_list, tmp) {
1260 i++;
1261 if (!tmp->ident)
1262 continue;
1263 if (strcmp(name, tmp->ident->name) == 0)
1264 return i;
1265 } END_FOR_EACH_PTR(tmp);
1266 return -1;
1269 static struct ident *number_to_member(struct expression *expr, int num)
1271 struct symbol *type, *member;
1272 int i = 0;
1274 type = get_type(expr);
1275 if (!type || type->type != SYM_STRUCT)
1276 return NULL;
1278 FOR_EACH_PTR(type->symbol_list, member) {
1279 if (i == num)
1280 return member->ident;
1281 i++;
1282 } END_FOR_EACH_PTR(member);
1283 return NULL;
1286 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1288 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1290 struct expression *edge_member, *assign;
1291 struct symbol *base = get_real_base_type(member);
1292 struct symbol *tmp;
1294 if (member->ident)
1295 expr = member_expression(expr, '.', member->ident);
1297 FOR_EACH_PTR(base->symbol_list, tmp) {
1298 struct symbol *type;
1300 type = get_real_base_type(tmp);
1301 if (!type)
1302 continue;
1304 edge_member = member_expression(expr, '.', tmp->ident);
1305 if (get_extra_state(edge_member))
1306 continue;
1308 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1309 set_inner_struct_members(expr, tmp);
1310 continue;
1313 if (!tmp->ident)
1314 continue;
1316 assign = assign_expression(edge_member, '=', zero_expr());
1317 __split_expr(assign);
1318 } END_FOR_EACH_PTR(tmp);
1323 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1325 struct symbol *tmp;
1326 struct expression *member = NULL;
1327 struct expression *assign;
1328 int op = '*';
1330 if (expr->type == EXPR_PREOP && expr->op == '&') {
1331 expr = strip_expr(expr->unop);
1332 op = '.';
1335 FOR_EACH_PTR(type->symbol_list, tmp) {
1336 type = get_real_base_type(tmp);
1337 if (!type)
1338 continue;
1340 if (tmp->ident) {
1341 member = member_expression(expr, op, tmp->ident);
1342 if (get_extra_state(member))
1343 continue;
1346 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1347 set_inner_struct_members(expr, tmp);
1348 continue;
1350 if (type->type == SYM_ARRAY)
1351 continue;
1352 if (!tmp->ident)
1353 continue;
1355 assign = assign_expression(member, '=', zero_expr());
1356 __split_expr(assign);
1357 } END_FOR_EACH_PTR(tmp);
1360 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1362 struct expression *deref, *assign, *tmp, *right;
1363 struct symbol *struct_type, *type;
1364 struct ident *member;
1365 int member_idx;
1367 struct_type = get_type(symbol);
1368 if (!struct_type ||
1369 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1370 return;
1373 * We're parsing an initializer that could look something like this:
1374 * struct foo foo = {
1375 * 42,
1376 * .whatever.xxx = 11,
1377 * .zzz = 12,
1378 * };
1380 * So what we have here is a list with 42, .whatever, and .zzz. We need
1381 * to break it up into left and right sides of the assignments.
1384 member_idx = 0;
1385 FOR_EACH_PTR(members, tmp) {
1386 deref = NULL;
1387 if (tmp->type == EXPR_IDENTIFIER) {
1388 member_idx = member_to_number(symbol, tmp->expr_ident);
1389 while (tmp->type == EXPR_IDENTIFIER) {
1390 member = tmp->expr_ident;
1391 tmp = tmp->ident_expression;
1392 if (deref)
1393 deref = member_expression(deref, '.', member);
1394 else
1395 deref = member_expression(symbol, '.', member);
1397 } else {
1398 member = number_to_member(symbol, member_idx);
1399 deref = member_expression(symbol, '.', member);
1401 right = tmp;
1402 member_idx++;
1403 if (right->type == EXPR_INITIALIZER) {
1404 type = get_type(deref);
1405 if (type && type->type == SYM_ARRAY)
1406 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1407 else
1408 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1409 } else {
1410 assign = assign_expression(deref, '=', right);
1411 fake_cb(assign);
1413 } END_FOR_EACH_PTR(tmp);
1415 set_unset_to_zero(struct_type, symbol);
1418 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1420 fake_member_assigns_helper(symbol_expression(sym),
1421 sym->initializer->expr_list, fake_cb);
1424 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1426 struct expression *offset, *binop, *assign, *tmp;
1427 struct symbol *type;
1428 int idx;
1430 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1431 return;
1433 idx = 0;
1434 FOR_EACH_PTR(expr_list, tmp) {
1435 if (tmp->type == EXPR_INDEX) {
1436 if (tmp->idx_from != tmp->idx_to)
1437 return;
1438 idx = tmp->idx_from;
1439 if (!tmp->idx_expression)
1440 goto next;
1441 tmp = tmp->idx_expression;
1443 offset = value_expr(idx);
1444 binop = array_element_expression(array, offset);
1445 if (tmp->type == EXPR_INITIALIZER) {
1446 type = get_type(binop);
1447 if (type && type->type == SYM_ARRAY)
1448 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1449 else
1450 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1451 } else {
1452 assign = assign_expression(binop, '=', tmp);
1453 fake_cb(assign);
1455 next:
1456 idx++;
1457 } END_FOR_EACH_PTR(tmp);
1460 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1462 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1465 static void fake_assign_expr(struct symbol *sym)
1467 struct expression *assign, *symbol;
1469 symbol = symbol_expression(sym);
1470 assign = assign_expression(symbol, '=', sym->initializer);
1471 __split_expr(assign);
1474 static void do_initializer_stuff(struct symbol *sym)
1476 if (!sym->initializer)
1477 return;
1479 if (sym->initializer->type == EXPR_INITIALIZER) {
1480 if (get_real_base_type(sym)->type == SYM_ARRAY)
1481 fake_element_assigns(sym, __split_expr);
1482 else
1483 fake_member_assigns(sym, __split_expr);
1484 } else {
1485 fake_assign_expr(sym);
1489 static void split_declaration(struct symbol_list *sym_list)
1491 struct symbol *sym;
1493 FOR_EACH_PTR(sym_list, sym) {
1494 __pass_to_client(sym, DECLARATION_HOOK);
1495 do_initializer_stuff(sym);
1496 split_sym(sym);
1497 } END_FOR_EACH_PTR(sym);
1500 static void call_global_assign_hooks(struct expression *assign)
1502 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1505 static void fake_global_assign(struct symbol *sym)
1507 struct expression *assign, *symbol;
1509 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1510 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1511 fake_element_assigns(sym, call_global_assign_hooks);
1512 } else if (sym->initializer) {
1513 symbol = symbol_expression(sym);
1514 assign = assign_expression(symbol, '=', sym->initializer);
1515 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1516 } else {
1517 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1519 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1520 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1521 fake_member_assigns(sym, call_global_assign_hooks);
1522 } else if (sym->initializer) {
1523 symbol = symbol_expression(sym);
1524 assign = assign_expression(symbol, '=', sym->initializer);
1525 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1526 } else {
1527 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1529 } else {
1530 symbol = symbol_expression(sym);
1531 if (sym->initializer) {
1532 assign = assign_expression(symbol, '=', sym->initializer);
1533 __split_expr(assign);
1534 } else {
1535 assign = assign_expression(symbol, '=', zero_expr());
1537 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1541 static void start_function_definition(struct symbol *sym)
1543 __in_function_def = 1;
1544 __pass_to_client(sym, FUNC_DEF_HOOK);
1545 __in_function_def = 0;
1546 __pass_to_client(sym, AFTER_DEF_HOOK);
1550 static void split_function(struct symbol *sym)
1552 struct symbol *base_type = get_base_type(sym);
1553 struct timeval stop;
1555 if (!base_type->stmt && !base_type->inline_stmt)
1556 return;
1558 gettimeofday(&outer_fn_start_time, NULL);
1559 gettimeofday(&fn_start_time, NULL);
1560 cur_func_sym = sym;
1561 if (sym->ident)
1562 cur_func = sym->ident->name;
1563 set_position(sym->pos);
1564 loop_count = 0;
1565 last_goto_statement_handled = 0;
1566 sm_debug("new function: %s\n", cur_func);
1567 __stree_id = 0;
1568 if (option_two_passes) {
1569 __unnullify_path();
1570 loop_num = 0;
1571 final_pass = 0;
1572 start_function_definition(sym);
1573 __split_stmt(base_type->stmt);
1574 __split_stmt(base_type->inline_stmt);
1575 nullify_path();
1577 __unnullify_path();
1578 loop_num = 0;
1579 final_pass = 1;
1580 start_function_definition(sym);
1581 __split_stmt(base_type->stmt);
1582 __split_stmt(base_type->inline_stmt);
1583 __pass_to_client(sym, END_FUNC_HOOK);
1584 if (need_delayed_scope_hooks())
1585 __call_scope_hooks();
1586 __pass_to_client(sym, AFTER_FUNC_HOOK);
1588 clear_all_states();
1590 gettimeofday(&stop, NULL);
1591 if (option_time && stop.tv_sec - fn_start_time.tv_sec > 2) {
1592 final_pass++;
1593 sm_msg("func_time: %lu", stop.tv_sec - fn_start_time.tv_sec);
1594 final_pass--;
1596 cur_func_sym = NULL;
1597 cur_func = NULL;
1598 free_data_info_allocs();
1599 free_expression_stack(&switch_expr_stack);
1600 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1601 __bail_on_rest_of_function = 0;
1604 static void save_flow_state(void)
1606 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1607 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1608 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1610 __add_ptr_list(&backup, big_statement_stack);
1611 __add_ptr_list(&backup, big_expression_stack);
1612 __add_ptr_list(&backup, big_condition_stack);
1613 __add_ptr_list(&backup, switch_expr_stack);
1615 __add_ptr_list(&backup, cur_func_sym);
1617 __add_ptr_list(&backup, __prev_stmt);
1618 __add_ptr_list(&backup, __cur_stmt);
1619 __add_ptr_list(&backup, __next_stmt);
1622 static void *pop_backup(void)
1624 void *ret;
1626 ret = last_ptr_list(backup);
1627 delete_ptr_list_last(&backup);
1628 return ret;
1631 static void restore_flow_state(void)
1633 __next_stmt = pop_backup();
1634 __cur_stmt = pop_backup();
1635 __prev_stmt = pop_backup();
1637 cur_func_sym = pop_backup();
1638 switch_expr_stack = pop_backup();
1639 big_condition_stack = pop_backup();
1640 big_expression_stack = pop_backup();
1641 big_statement_stack = pop_backup();
1642 final_pass = PTR_INT(pop_backup()) >> 2;
1643 loop_count = PTR_INT(pop_backup()) >> 2;
1644 loop_num = PTR_INT(pop_backup()) >> 2;
1647 static void parse_inline(struct expression *call)
1649 struct symbol *base_type;
1650 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
1651 struct timeval time_backup = fn_start_time;
1652 struct expression *orig_inline = __inline_fn;
1653 int orig_budget;
1655 if (out_of_memory() || taking_too_long())
1656 return;
1658 save_flow_state();
1660 __pass_to_client(call, INLINE_FN_START);
1661 final_pass = 0; /* don't print anything */
1662 __inline_fn = call;
1663 orig_budget = inline_budget;
1664 inline_budget = inline_budget - 5;
1666 base_type = get_base_type(call->fn->symbol);
1667 cur_func_sym = call->fn->symbol;
1668 if (call->fn->symbol->ident)
1669 cur_func = call->fn->symbol->ident->name;
1670 else
1671 cur_func = NULL;
1672 set_position(call->fn->symbol->pos);
1674 save_all_states();
1675 big_statement_stack = NULL;
1676 big_expression_stack = NULL;
1677 big_condition_stack = NULL;
1678 switch_expr_stack = NULL;
1680 sm_debug("inline function: %s\n", cur_func);
1681 __unnullify_path();
1682 loop_num = 0;
1683 loop_count = 0;
1684 start_function_definition(call->fn->symbol);
1685 __split_stmt(base_type->stmt);
1686 __split_stmt(base_type->inline_stmt);
1687 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1688 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1690 free_expression_stack(&switch_expr_stack);
1691 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1692 nullify_path();
1693 free_goto_stack();
1695 restore_flow_state();
1696 fn_start_time = time_backup;
1697 cur_func = cur_func_bak;
1699 restore_all_states();
1700 set_position(call->pos);
1701 __inline_fn = orig_inline;
1702 inline_budget = orig_budget;
1703 __pass_to_client(call, INLINE_FN_END);
1706 static struct symbol_list *inlines_called;
1707 static void add_inline_function(struct symbol *sym)
1709 static struct symbol_list *already_added;
1710 struct symbol *tmp;
1712 FOR_EACH_PTR(already_added, tmp) {
1713 if (tmp == sym)
1714 return;
1715 } END_FOR_EACH_PTR(tmp);
1717 add_ptr_list(&already_added, sym);
1718 add_ptr_list(&inlines_called, sym);
1721 static void process_inlines(void)
1723 struct symbol *tmp;
1725 FOR_EACH_PTR(inlines_called, tmp) {
1726 split_function(tmp);
1727 } END_FOR_EACH_PTR(tmp);
1728 free_ptr_list(&inlines_called);
1731 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1733 struct symbol *sym;
1735 FOR_EACH_PTR_REVERSE(big_list, sym) {
1736 if (!sym->scope)
1737 continue;
1738 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1739 return sym;
1740 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1741 return sym;
1742 } END_FOR_EACH_PTR_REVERSE(sym);
1744 return NULL;
1747 static bool interesting_function(struct symbol *sym)
1749 static int prev_stream = -1;
1750 static bool prev_answer;
1751 const char *filename;
1752 int len;
1754 if (!(sym->ctype.modifiers & MOD_INLINE))
1755 return true;
1757 if (sym->pos.stream == prev_stream)
1758 return prev_answer;
1760 prev_stream = sym->pos.stream;
1761 prev_answer = false;
1763 filename = stream_name(sym->pos.stream);
1764 len = strlen(filename);
1765 if (len > 0 && filename[len - 1] == 'c')
1766 prev_answer = true;
1767 return prev_answer;
1770 static void split_inlines_in_scope(struct symbol *sym)
1772 struct symbol *base;
1773 struct symbol_list *scope_list;
1774 int stream;
1776 scope_list = sym->scope->symbols;
1777 stream = sym->pos.stream;
1779 /* find the last static symbol in the file */
1780 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1781 if (sym->pos.stream != stream)
1782 continue;
1783 if (sym->type != SYM_NODE)
1784 continue;
1785 base = get_base_type(sym);
1786 if (!base)
1787 continue;
1788 if (base->type != SYM_FN)
1789 continue;
1790 if (!base->inline_stmt)
1791 continue;
1792 if (!interesting_function(sym))
1793 continue;
1794 add_inline_function(sym);
1795 } END_FOR_EACH_PTR_REVERSE(sym);
1797 process_inlines();
1800 static void split_inlines(struct symbol_list *sym_list)
1802 struct symbol *sym;
1804 sym = get_last_scoped_symbol(sym_list, 0);
1805 if (sym)
1806 split_inlines_in_scope(sym);
1807 sym = get_last_scoped_symbol(sym_list, 1);
1808 if (sym)
1809 split_inlines_in_scope(sym);
1812 static struct stree *clone_estates_perm(struct stree *orig)
1814 struct stree *ret = NULL;
1815 struct sm_state *tmp;
1817 FOR_EACH_SM(orig, tmp) {
1818 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1819 } END_FOR_EACH_SM(tmp);
1821 return ret;
1824 struct position last_pos;
1825 static void split_c_file_functions(struct symbol_list *sym_list)
1827 struct symbol *sym;
1829 __unnullify_path();
1830 FOR_EACH_PTR(sym_list, sym) {
1831 set_position(sym->pos);
1832 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1833 __pass_to_client(sym, BASE_HOOK);
1834 fake_global_assign(sym);
1836 } END_FOR_EACH_PTR(sym);
1837 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1838 nullify_path();
1840 FOR_EACH_PTR(sym_list, sym) {
1841 set_position(sym->pos);
1842 last_pos = sym->pos;
1843 if (!interesting_function(sym))
1844 continue;
1845 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1846 split_function(sym);
1847 process_inlines();
1849 last_pos = sym->pos;
1850 } END_FOR_EACH_PTR(sym);
1851 split_inlines(sym_list);
1852 __pass_to_client(sym_list, END_FILE_HOOK);
1855 static int final_before_fake;
1856 void init_fake_env(void)
1858 if (!in_fake_env)
1859 final_before_fake = final_pass;
1860 in_fake_env++;
1861 __push_fake_cur_stree();
1862 final_pass = 0;
1865 void end_fake_env(void)
1867 __pop_fake_cur_stree();
1868 in_fake_env--;
1869 if (!in_fake_env)
1870 final_pass = final_before_fake;
1873 static void open_output_files(char *base_file)
1875 char buf[256];
1877 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1878 sm_outfd = fopen(buf, "w");
1879 if (!sm_outfd)
1880 sm_fatal("Cannot open %s", buf);
1882 if (!option_info)
1883 return;
1885 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1886 sql_outfd = fopen(buf, "w");
1887 if (!sql_outfd)
1888 sm_fatal("Error: Cannot open %s", buf);
1890 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1891 caller_info_fd = fopen(buf, "w");
1892 if (!caller_info_fd)
1893 sm_fatal("Error: Cannot open %s", buf);
1896 void smatch(struct string_list *filelist)
1898 struct symbol_list *sym_list;
1899 struct timeval stop, start;
1900 char *path;
1901 int len;
1903 gettimeofday(&start, NULL);
1905 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1906 path = getcwd(NULL, 0);
1907 free(full_base_file);
1908 if (path) {
1909 len = strlen(path) + 1 + strlen(base_file) + 1;
1910 full_base_file = malloc(len);
1911 snprintf(full_base_file, len, "%s/%s", path, base_file);
1912 } else {
1913 full_base_file = alloc_string(base_file);
1915 if (option_file_output)
1916 open_output_files(base_file);
1917 sym_list = sparse_keep_tokens(base_file);
1918 split_c_file_functions(sym_list);
1919 } END_FOR_EACH_PTR_NOTAG(base_file);
1921 gettimeofday(&stop, NULL);
1923 set_position(last_pos);
1924 final_pass = 1;
1925 if (option_time)
1926 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1927 if (option_mem)
1928 sm_msg("mem: %luKb", get_max_memory());