function_hooks: introduce return_implies_param_key_expr()
[smatch.git] / smatch_flow.c
blob2150561ab6e48cfb2f815b0d70f45efffa063ec6
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_buf_clear;
31 int __in_fake_var_assign;
32 int __fake_state_cnt;
33 int __debug_skip;
34 int in_fake_env;
35 int final_pass;
36 int __inline_call;
37 struct expression *__inline_fn;
39 int __smatch_lineno = 0;
41 static char *base_file;
42 static const char *filename;
43 static char *pathname;
44 static char *full_filename;
45 static char *full_base_file;
46 static char *cur_func;
47 int base_file_stream;
48 static unsigned int loop_count;
49 static int last_goto_statement_handled;
50 int __expr_stmt_count;
51 int __in_function_def;
52 int __in_unmatched_hook;
53 static struct expression_list *switch_expr_stack = NULL;
54 static struct expression_list *post_op_stack = NULL;
56 static struct ptr_list *fn_data_list;
57 static struct ptr_list *backup;
59 struct expression_list *big_expression_stack;
60 struct statement_list *big_statement_stack;
61 struct statement *__prev_stmt;
62 struct statement *__cur_stmt;
63 struct statement *__next_stmt;
64 static struct expression_list *parsed_calls;
65 static int indent_cnt;
66 int __in_pre_condition = 0;
67 int __bail_on_rest_of_function = 0;
68 static struct timeval fn_start_time;
69 static struct timeval outer_fn_start_time;
70 char *get_function(void) { return cur_func; }
71 int get_lineno(void) { return __smatch_lineno; }
72 int inside_loop(void) { return !!loop_count; }
73 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
74 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
75 int in_expression_statement(void) { return !!__expr_stmt_count; }
77 static void split_symlist(struct symbol_list *sym_list);
78 static void split_declaration(struct symbol_list *sym_list);
79 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
80 static void split_args(struct expression *expr);
81 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
82 static void add_inline_function(struct symbol *sym);
83 static void parse_inline(struct expression *expr);
85 int option_assume_loops = 0;
86 int option_two_passes = 0;
87 struct symbol *cur_func_sym = NULL;
88 struct stree *global_states;
90 const unsigned long valid_ptr_min = 4096;
91 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
92 const sval_t valid_ptr_min_sval = {
93 .type = &ptr_ctype,
94 {.value = 4096},
96 sval_t ptr_err_min = { .type = &ptr_ctype };
97 sval_t ptr_err_max = { .type = &ptr_ctype };
98 sval_t ulong_ULONG_MAX = { .type = &ulong_ctype };
100 sval_t valid_ptr_max_sval = {
101 .type = &ptr_ctype,
102 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
104 struct range_list *valid_ptr_rl;
106 void alloc_ptr_constants(void)
108 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
109 valid_ptr_max_sval.value = valid_ptr_max;
111 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
112 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
113 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
115 ptr_err_min = sval_cast(&ptr_ctype, err_min);
116 ptr_err_max = sval_cast(&ptr_ctype, err_max);
117 ulong_ULONG_MAX = sval_type_max(&ulong_ctype);
120 int outside_of_function(void)
122 return cur_func_sym == NULL;
125 const char *get_filename(void)
127 if (option_info && option_full_path)
128 return full_base_file;
129 if (option_info)
130 return base_file;
131 if (option_full_path)
132 return full_filename;
133 return filename;
136 const char *get_base_file(void)
138 if (option_full_path)
139 return full_base_file;
140 return base_file;
143 unsigned long long get_file_id(void)
145 return str_to_llu_hash(get_filename());
148 unsigned long long get_base_file_id(void)
150 return str_to_llu_hash(get_base_file());
153 static void set_position(struct position pos)
155 int len;
156 static int prev_stream = -1;
158 if (in_fake_env)
159 return;
161 if (pos.stream == 0 && pos.line == 0)
162 return;
164 __smatch_lineno = pos.line;
166 if (pos.stream == prev_stream)
167 return;
169 filename = stream_name(pos.stream);
171 free(full_filename);
172 pathname = getcwd(NULL, 0);
173 if (pathname) {
174 len = strlen(pathname) + 1 + strlen(filename) + 1;
175 full_filename = malloc(len);
176 snprintf(full_filename, len, "%s/%s", pathname, filename);
177 } else {
178 full_filename = alloc_string(filename);
180 free(pathname);
183 int is_assigned_call(struct expression *expr)
185 struct expression *parent = expr_get_parent_expr(expr);
187 if (parent &&
188 parent->type == EXPR_ASSIGNMENT &&
189 parent->op == '=' &&
190 strip_expr(parent->right) == expr)
191 return 1;
193 return 0;
196 int is_fake_assigned_call(struct expression *expr)
198 struct expression *parent = expr_get_fake_parent_expr(expr);
200 if (parent &&
201 parent->type == EXPR_ASSIGNMENT &&
202 parent->op == '=' &&
203 strip_expr(parent->right) == expr)
204 return 1;
206 return 0;
209 static bool is_inline_func(struct expression *expr)
211 if (expr->type != EXPR_SYMBOL || !expr->symbol)
212 return false;
213 if (!expr->symbol->definition)
214 return false;
215 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
216 return true;
218 return 0;
221 static int is_noreturn_func(struct expression *expr)
223 if (expr->type != EXPR_SYMBOL || !expr->symbol)
224 return 0;
227 * It's almost impossible for Smatch to handle __builtin_constant_p()
228 * the same way that GCC does so Smatch ends up making some functions
229 * as no return functions incorrectly.
232 if (option_project == PROJ_KERNEL && expr->symbol->ident &&
233 strstr(expr->symbol->ident->name, "__compiletime_assert"))
234 return 0;
236 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
237 return 1;
238 return 0;
241 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
243 unsigned long *rl = _rl;
245 *rl = strtoul(argv[0], NULL, 10);
246 return 0;
249 static int get_func_time(struct symbol *sym)
251 unsigned long time = 0;
253 run_sql(&save_func_time, &time,
254 "select key from return_implies where %s and type = %d;",
255 get_static_filter(sym), FUNC_TIME);
257 return time;
260 static int inline_budget = 20;
262 int inlinable(struct expression *expr)
264 struct symbol *sym;
265 struct statement *last_stmt = NULL;
267 if (__inline_fn) /* don't nest */
268 return 0;
270 if (expr->type != EXPR_SYMBOL || !expr->symbol)
271 return 0;
272 if (is_no_inline_function(expr->symbol->ident->name))
273 return 0;
274 sym = get_base_type(expr->symbol);
275 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
276 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
277 return 0;
278 if (sym->stmt->type != STMT_COMPOUND)
279 return 0;
280 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
282 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
283 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
284 return 0;
285 if (sym->inline_stmt->type != STMT_COMPOUND)
286 return 0;
287 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
290 if (!last_stmt)
291 return 0;
293 /* the magic numbers in this function are pulled out of my bum. */
294 if (last_stmt->pos.line > sym->pos.line + inline_budget)
295 return 0;
297 if (get_func_time(expr->symbol) >= 2)
298 return 0;
300 return 1;
303 void __process_post_op_stack(void)
305 struct expression *expr;
307 FOR_EACH_PTR(post_op_stack, expr) {
308 __pass_to_client(expr, OP_HOOK);
309 } END_FOR_EACH_PTR(expr);
311 __free_ptr_list((struct ptr_list **)&post_op_stack);
314 static int handle_comma_assigns(struct expression *expr)
316 struct expression *right;
317 struct expression *assign;
319 right = strip_expr(expr->right);
320 if (right->type != EXPR_COMMA)
321 return 0;
323 __split_expr(right->left);
324 __process_post_op_stack();
326 assign = assign_expression(expr->left, '=', right->right);
327 __split_expr(assign);
329 return 1;
332 /* This is to handle *p++ = foo; assignments */
333 static int handle_postop_assigns(struct expression *expr)
335 struct expression *left, *fake_left;
336 struct expression *assign;
338 left = strip_expr(expr->left);
339 if (left->type != EXPR_PREOP || left->op != '*')
340 return 0;
341 left = strip_expr(left->unop);
342 if (left->type != EXPR_POSTOP)
343 return 0;
345 fake_left = deref_expression(strip_expr(left->unop));
346 assign = assign_expression(fake_left, '=', expr->right);
348 __split_expr(assign);
349 __split_expr(expr->left);
351 return 1;
354 static int prev_expression_is_getting_address(struct expression *expr)
356 struct expression *parent;
358 do {
359 parent = expr_get_parent_expr(expr);
361 if (!parent)
362 return 0;
363 if (parent->type == EXPR_PREOP && parent->op == '&')
364 return 1;
365 if (parent->type == EXPR_PREOP && parent->op == '(')
366 goto next;
367 if (parent->type == EXPR_DEREF && parent->op == '.')
368 goto next;
369 /* Handle &foo->array[offset] */
370 if (parent->type == EXPR_BINOP && parent->op == '+') {
371 parent = expr_get_parent_expr(parent);
372 if (!parent)
373 return 0;
374 if (parent->type == EXPR_PREOP && parent->op == '*')
375 goto next;
378 return 0;
379 next:
380 expr = parent;
381 } while (1);
384 int __in_builtin_overflow_func;
385 static void handle_builtin_overflow_func(struct expression *expr)
387 struct expression *a, *b, *res, *assign;
388 int op;
390 if (sym_name_is("__builtin_add_overflow", expr->fn))
391 op = '+';
392 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
393 op = '-';
394 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
395 op = '*';
396 else
397 return;
399 a = get_argument_from_call_expr(expr->args, 0);
400 b = get_argument_from_call_expr(expr->args, 1);
401 res = get_argument_from_call_expr(expr->args, 2);
403 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
405 __in_builtin_overflow_func++;
406 __split_expr(assign);
407 __in_builtin_overflow_func--;
410 static int handle__builtin_choose_expr(struct expression *expr)
412 struct expression *const_expr, *expr1, *expr2;
413 sval_t sval;
415 if (!sym_name_is("__builtin_choose_expr", expr->fn))
416 return 0;
418 const_expr = get_argument_from_call_expr(expr->args, 0);
419 expr1 = get_argument_from_call_expr(expr->args, 1);
420 expr2 = get_argument_from_call_expr(expr->args, 2);
422 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
423 return 0;
424 if (sval.value)
425 __split_expr(expr1);
426 else
427 __split_expr(expr2);
428 return 1;
431 static int handle__builtin_choose_expr_assigns(struct expression *expr)
433 struct expression *const_expr, *right, *expr1, *expr2, *fake;
434 sval_t sval;
437 * We can't use strip_no_cast() because it strips out
438 * __builtin_choose_expr() which turns this function into a no-op.
441 right = strip_parens(expr->right);
442 if (right->type != EXPR_CALL)
443 return 0;
444 if (!sym_name_is("__builtin_choose_expr", right->fn))
445 return 0;
447 const_expr = get_argument_from_call_expr(right->args, 0);
448 expr1 = get_argument_from_call_expr(right->args, 1);
449 expr2 = get_argument_from_call_expr(right->args, 2);
451 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
452 return 0;
454 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
455 __split_expr(fake);
456 return 1;
459 int is_condition_call(struct expression *expr)
461 struct expression *tmp;
463 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
464 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
465 return 1;
466 if (tmp->pos.line < expr->pos.line)
467 return 0;
468 } END_FOR_EACH_PTR_REVERSE(tmp);
470 return 0;
473 static struct expression *expr_get_parent_no_parens(struct expression *expr)
475 do {
476 expr = expr_get_parent_expr(expr);
477 } while (expr &&
478 expr->type == EXPR_PREOP &&
479 expr->op == '(');
481 return expr;
484 static bool gen_fake_function_assign(struct expression *expr)
486 static struct expression *parsed;
487 struct expression *assign, *parent;
488 struct symbol *type;
489 char buf[64];
491 /* The rule is that every non-void function call has to be part of an
492 * assignment. TODO: Should we create a fake non-casted assignment
493 * for casted assignments? Also faked assigns for += assignments?
495 type = get_type(expr);
496 if (!type || type == &void_ctype)
497 return false;
499 parent = expr_get_parent_no_parens(expr);
500 if (parent && parent->type == EXPR_ASSIGNMENT)
501 return false;
503 parent = expr_get_fake_parent_expr(expr);
504 if (parent) {
505 struct expression *left = parent->left;
507 if (parent == parsed)
508 return false;
509 if (!left || left->type != EXPR_SYMBOL)
510 return false;
511 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
512 return false;
513 parsed = parent;
514 __split_expr(parent);
515 return true;
518 // TODO: faked_assign skipping conditions is a hack
519 if (is_condition_call(expr))
520 return false;
522 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
523 assign = create_fake_assign(buf, get_type(expr), expr);
525 parsed = assign;
526 __split_expr(assign);
527 return true;
530 static void split_call(struct expression *expr)
532 if (gen_fake_function_assign(expr))
533 return;
535 expr_set_parent_expr(expr->fn, expr);
537 if (sym_name_is("__builtin_constant_p", expr->fn))
538 return;
539 if (handle__builtin_choose_expr(expr))
540 return;
541 __split_expr(expr->fn);
542 split_args(expr);
543 if (is_inline_func(expr->fn))
544 add_inline_function(expr->fn->symbol->definition);
545 if (inlinable(expr->fn))
546 __inline_call = 1;
547 __process_post_op_stack();
548 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
549 __pass_to_client(expr, FUNCTION_CALL_HOOK);
550 __inline_call = 0;
551 if (inlinable(expr->fn))
552 parse_inline(expr);
553 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
554 if (is_noreturn_func(expr->fn))
555 nullify_path();
556 if (!expr_get_parent_expr(expr) && indent_cnt == 1)
557 __discard_fake_states(expr);
558 handle_builtin_overflow_func(expr);
559 __add_ptr_list((struct ptr_list **)&parsed_calls, expr);
562 static unsigned long skip_split;
563 void parse_assignment(struct expression *expr, bool shallow)
565 struct expression *right;
567 expr_set_parent_expr(expr->left, expr);
568 expr_set_parent_expr(expr->right, expr);
570 right = strip_expr(expr->right);
571 if (!right)
572 return;
574 if (shallow)
575 skip_split++;
577 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
579 /* foo = !bar() */
580 if (__handle_condition_assigns(expr))
581 goto after_assign;
582 /* foo = (x < 5 ? foo : 5); */
583 if (__handle_select_assigns(expr))
584 goto after_assign;
585 /* foo = ({frob(); frob(); frob(); 1;}) */
586 if (__handle_expr_statement_assigns(expr))
587 goto done; // FIXME: goto after
588 /* foo = (3, 4); */
589 if (handle_comma_assigns(expr))
590 goto after_assign;
591 if (handle__builtin_choose_expr_assigns(expr))
592 goto after_assign;
593 if (handle_postop_assigns(expr))
594 goto done; /* no need to goto after_assign */
596 __split_expr(expr->right);
597 if (outside_of_function())
598 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
599 else
600 __pass_to_client(expr, ASSIGNMENT_HOOK);
603 // FIXME: the ordering of this is tricky
604 __fake_struct_member_assignments(expr);
606 /* Re-examine ->right for inlines. See the commit message */
607 right = strip_expr(expr->right);
608 if (expr->op == '=' && right->type == EXPR_CALL)
609 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
611 after_assign:
612 if (get_macro_name(right->pos) &&
613 get_macro_name(expr->left->pos) != get_macro_name(right->pos))
614 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
616 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
617 __split_expr(expr->left);
619 done:
620 if (shallow)
621 skip_split--;
624 static bool skip_split_off(struct expression *expr)
626 if (expr->type == EXPR_CALL &&
627 sym_name_is("__smatch_stop_skip", expr->fn))
628 return true;
629 return false;
632 void __split_expr(struct expression *expr)
634 if (!expr)
635 return;
637 if (skip_split_off(expr))
638 __debug_skip = 0;
639 if (__debug_skip)
640 return;
642 if (skip_split)
643 return;
645 // if (local_debug)
646 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
648 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
649 return;
650 if (__in_fake_assign >= 4) /* don't allow too much nesting */
651 return;
653 push_expression(&big_expression_stack, expr);
654 set_position(expr->pos);
655 __pass_to_client(expr, EXPR_HOOK);
657 switch (expr->type) {
658 case EXPR_PREOP:
659 expr_set_parent_expr(expr->unop, expr);
661 if (expr->op == '*' &&
662 !prev_expression_is_getting_address(expr))
663 __pass_to_client(expr, DEREF_HOOK);
664 __split_expr(expr->unop);
665 __pass_to_client(expr, OP_HOOK);
666 break;
667 case EXPR_POSTOP:
668 expr_set_parent_expr(expr->unop, expr);
670 __split_expr(expr->unop);
671 push_expression(&post_op_stack, expr);
672 break;
673 case EXPR_STATEMENT:
674 __expr_stmt_count++;
675 if (expr->statement && !expr->statement) {
676 stmt_set_parent_stmt(expr->statement,
677 last_ptr_list((struct ptr_list *)big_statement_stack));
679 __split_stmt(expr->statement);
680 __expr_stmt_count--;
681 break;
682 case EXPR_LOGICAL:
683 case EXPR_COMPARE:
684 expr_set_parent_expr(expr->left, expr);
685 expr_set_parent_expr(expr->right, expr);
687 __pass_to_client(expr, LOGIC_HOOK);
688 __handle_logic(expr);
689 break;
690 case EXPR_BINOP:
691 expr_set_parent_expr(expr->left, expr);
692 expr_set_parent_expr(expr->right, expr);
694 __pass_to_client(expr, BINOP_HOOK);
695 __split_expr(expr->left);
696 __split_expr(expr->right);
697 break;
698 case EXPR_COMMA:
699 expr_set_parent_expr(expr->left, expr);
700 expr_set_parent_expr(expr->right, expr);
702 __split_expr(expr->left);
703 __process_post_op_stack();
704 __split_expr(expr->right);
705 break;
706 case EXPR_ASSIGNMENT:
707 parse_assignment(expr, false);
708 break;
709 case EXPR_DEREF:
710 expr_set_parent_expr(expr->deref, expr);
712 __pass_to_client(expr, DEREF_HOOK);
713 __split_expr(expr->deref);
714 break;
715 case EXPR_SLICE:
716 expr_set_parent_expr(expr->base, expr);
718 __split_expr(expr->base);
719 break;
720 case EXPR_CAST:
721 case EXPR_FORCE_CAST:
722 expr_set_parent_expr(expr->cast_expression, expr);
724 __pass_to_client(expr, CAST_HOOK);
725 __split_expr(expr->cast_expression);
726 break;
727 case EXPR_SIZEOF:
728 if (expr->cast_expression)
729 __pass_to_client(strip_parens(expr->cast_expression),
730 SIZEOF_HOOK);
731 break;
732 case EXPR_OFFSETOF:
733 case EXPR_ALIGNOF:
734 break;
735 case EXPR_CONDITIONAL:
736 case EXPR_SELECT:
737 expr_set_parent_expr(expr->conditional, expr);
738 expr_set_parent_expr(expr->cond_true, expr);
739 expr_set_parent_expr(expr->cond_false, expr);
741 if (known_condition_true(expr->conditional)) {
742 __split_expr(expr->cond_true);
743 break;
745 if (known_condition_false(expr->conditional)) {
746 __split_expr(expr->cond_false);
747 break;
749 __pass_to_client(expr, SELECT_HOOK);
750 __split_whole_condition(expr->conditional);
751 __split_expr(expr->cond_true);
752 __push_true_states();
753 __use_false_states();
754 __split_expr(expr->cond_false);
755 __merge_true_states();
756 break;
757 case EXPR_CALL:
758 split_call(expr);
759 break;
760 case EXPR_INITIALIZER:
761 split_expr_list(expr->expr_list, expr);
762 break;
763 case EXPR_IDENTIFIER:
764 expr_set_parent_expr(expr->ident_expression, expr);
765 __split_expr(expr->ident_expression);
766 break;
767 case EXPR_INDEX:
768 expr_set_parent_expr(expr->idx_expression, expr);
769 __split_expr(expr->idx_expression);
770 break;
771 case EXPR_POS:
772 expr_set_parent_expr(expr->init_expr, expr);
773 __split_expr(expr->init_expr);
774 break;
775 case EXPR_SYMBOL:
776 __pass_to_client(expr, SYM_HOOK);
777 break;
778 case EXPR_STRING:
779 __pass_to_client(expr, STRING_HOOK);
780 break;
781 case EXPR_GENERIC: {
782 struct expression *tmp;
784 tmp = strip_Generic(expr);
785 if (tmp != expr)
786 __split_expr(tmp);
787 break;
789 default:
790 break;
792 __pass_to_client(expr, EXPR_HOOK_AFTER);
793 pop_expression(&big_expression_stack);
796 static int is_forever_loop(struct statement *stmt)
798 struct expression *expr;
799 sval_t sval;
801 expr = strip_expr(stmt->iterator_pre_condition);
802 if (!expr)
803 expr = stmt->iterator_post_condition;
804 if (!expr) {
805 /* this is a for(;;) loop... */
806 return 1;
809 if (get_value(expr, &sval) && sval.value != 0)
810 return 1;
812 return 0;
815 static int loop_num;
816 static char *get_loop_name(int num)
818 char buf[256];
820 snprintf(buf, 255, "-loop%d", num);
821 buf[255] = '\0';
822 return alloc_sname(buf);
825 static struct bool_stmt_fn_list *once_through_hooks;
826 void add_once_through_hook(bool_stmt_func *fn)
828 add_ptr_list(&once_through_hooks, fn);
831 static bool call_once_through_hooks(struct statement *stmt)
833 bool_stmt_func *fn;
835 if (implied_condition_true(stmt->iterator_pre_condition))
836 return true;
837 if (option_assume_loops)
838 return true;
840 FOR_EACH_PTR(once_through_hooks, fn) {
841 if ((fn)(stmt))
842 return true;
843 } END_FOR_EACH_PTR(fn);
845 return false;
849 * Pre Loops are while and for loops.
851 static void handle_pre_loop(struct statement *stmt)
853 int once_through; /* we go through the loop at least once */
854 struct sm_state *extra_sm = NULL;
855 int unchanged = 0;
856 char *loop_name;
857 struct stree *stree = NULL;
858 struct sm_state *sm = NULL;
860 loop_name = get_loop_name(loop_num);
861 loop_num++;
863 split_declaration(stmt->iterator_syms);
864 if (stmt->iterator_pre_statement) {
865 __split_stmt(stmt->iterator_pre_statement);
866 __prev_stmt = stmt->iterator_pre_statement;
869 once_through = call_once_through_hooks(stmt);
871 loop_count++;
872 __push_continues();
873 __push_breaks();
875 __merge_gotos(loop_name, NULL);
877 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
878 __in_pre_condition++;
879 __pass_to_client(stmt, PRELOOP_HOOK);
880 __split_whole_condition(stmt->iterator_pre_condition);
881 __in_pre_condition--;
882 FOR_EACH_SM(stree, sm) {
883 set_state(sm->owner, sm->name, sm->sym, sm->state);
884 } END_FOR_EACH_SM(sm);
885 free_stree(&stree);
886 if (extra_sm)
887 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
889 __split_stmt(stmt->iterator_statement);
890 if (is_forever_loop(stmt)) {
891 __merge_continues();
892 __save_gotos(loop_name, NULL);
894 __push_fake_cur_stree();
895 __split_stmt(stmt->iterator_post_statement);
896 stree = __pop_fake_cur_stree();
898 __discard_false_states();
899 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
900 __use_breaks();
902 if (!__path_is_null())
903 __merge_stree_into_cur(stree);
904 free_stree(&stree);
905 } else {
906 __merge_continues();
907 unchanged = __iterator_unchanged(extra_sm);
908 __split_stmt(stmt->iterator_post_statement);
909 __prev_stmt = stmt->iterator_post_statement;
910 __cur_stmt = stmt;
912 __save_gotos(loop_name, NULL);
913 __in_pre_condition++;
914 __split_whole_condition(stmt->iterator_pre_condition);
915 __in_pre_condition--;
916 nullify_path();
917 __merge_false_states();
918 if (once_through)
919 __discard_false_states();
920 else
921 __merge_false_states();
923 if (extra_sm && unchanged)
924 __extra_pre_loop_hook_after(extra_sm,
925 stmt->iterator_post_statement,
926 stmt->iterator_pre_condition);
927 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
928 __merge_breaks();
930 loop_count--;
934 * Post loops are do {} while();
936 static void handle_post_loop(struct statement *stmt)
938 char *loop_name;
940 loop_name = get_loop_name(loop_num);
941 loop_num++;
942 loop_count++;
944 __pass_to_client(stmt, POSTLOOP_HOOK);
946 __push_continues();
947 __push_breaks();
948 __merge_gotos(loop_name, NULL);
949 __split_stmt(stmt->iterator_statement);
950 __merge_continues();
951 if (!expr_is_zero(stmt->iterator_post_condition))
952 __save_gotos(loop_name, NULL);
954 if (is_forever_loop(stmt)) {
955 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
956 __use_breaks();
957 } else {
958 __split_whole_condition(stmt->iterator_post_condition);
959 __use_false_states();
960 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
961 __merge_breaks();
963 loop_count--;
966 static int empty_statement(struct statement *stmt)
968 if (!stmt)
969 return 0;
970 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
971 return 1;
972 return 0;
975 static int last_stmt_on_same_line(void)
977 struct statement *stmt;
978 int i = 0;
980 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
981 if (!i++)
982 continue;
983 if (stmt->pos.line == get_lineno())
984 return 1;
985 return 0;
986 } END_FOR_EACH_PTR_REVERSE(stmt);
987 return 0;
990 static void split_asm_ops(struct asm_operand_list *ops)
992 struct asm_operand *op;
994 FOR_EACH_PTR(ops, op) {
995 __split_expr(op->expr);
996 } END_FOR_EACH_PTR(op);
999 static int is_case_val(struct statement *stmt, sval_t sval)
1001 sval_t case_sval;
1003 if (stmt->type != STMT_CASE)
1004 return 0;
1005 if (!stmt->case_expression) {
1006 __set_default();
1007 return 1;
1009 if (!get_value(stmt->case_expression, &case_sval))
1010 return 0;
1011 if (case_sval.value == sval.value)
1012 return 1;
1013 return 0;
1016 static struct range_list *get_case_rl(struct expression *switch_expr,
1017 struct expression *case_expr,
1018 struct expression *case_to)
1020 sval_t start, end;
1021 struct range_list *rl = NULL;
1022 struct symbol *switch_type;
1024 switch_type = get_type(switch_expr);
1025 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
1026 start = sval_cast(switch_type, start);
1027 end = sval_cast(switch_type, end);
1028 add_range(&rl, start, end);
1029 } else if (get_value(case_expr, &start)) {
1030 start = sval_cast(switch_type, start);
1031 add_range(&rl, start, start);
1034 return rl;
1037 static void split_known_switch(struct statement *stmt, sval_t sval)
1039 struct statement *tmp;
1040 struct range_list *rl;
1042 __split_expr(stmt->switch_expression);
1043 sval = sval_cast(get_type(stmt->switch_expression), sval);
1045 push_expression(&switch_expr_stack, stmt->switch_expression);
1046 __save_switch_states(top_expression(switch_expr_stack));
1047 nullify_path();
1048 __push_default();
1049 __push_breaks();
1051 stmt = stmt->switch_statement;
1053 __push_scope_hooks();
1054 FOR_EACH_PTR(stmt->stmts, tmp) {
1055 __smatch_lineno = tmp->pos.line;
1056 // FIXME: what if default comes before the known case statement?
1057 if (is_case_val(tmp, sval)) {
1058 rl = alloc_rl(sval, sval);
1059 __merge_switches(top_expression(switch_expr_stack), rl);
1060 __pass_case_to_client(top_expression(switch_expr_stack), rl);
1061 stmt_set_parent_stmt(tmp->case_statement, tmp);
1062 __split_stmt(tmp->case_statement);
1063 goto next;
1065 if (__path_is_null())
1066 continue;
1067 __split_stmt(tmp);
1068 next:
1069 if (__path_is_null()) {
1070 __set_default();
1071 goto out;
1073 } END_FOR_EACH_PTR(tmp);
1074 out:
1075 __call_scope_hooks();
1076 if (!__pop_default())
1077 __merge_switches(top_expression(switch_expr_stack), NULL);
1078 __discard_switches();
1079 __merge_breaks();
1080 pop_expression(&switch_expr_stack);
1083 static void split_case(struct statement *stmt)
1085 struct range_list *rl = NULL;
1087 expr_set_parent_stmt(stmt->case_expression, stmt);
1088 expr_set_parent_stmt(stmt->case_to, stmt);
1090 rl = get_case_rl(top_expression(switch_expr_stack),
1091 stmt->case_expression, stmt->case_to);
1092 while (stmt->case_statement->type == STMT_CASE) {
1093 struct range_list *tmp;
1095 tmp = get_case_rl(top_expression(switch_expr_stack),
1096 stmt->case_statement->case_expression,
1097 stmt->case_statement->case_to);
1098 if (!tmp)
1099 goto next;
1100 rl = rl_union(rl, tmp);
1101 if (!stmt->case_expression)
1102 __set_default();
1103 next:
1104 stmt = stmt->case_statement;
1107 __merge_switches(top_expression(switch_expr_stack), rl);
1109 if (!stmt->case_expression)
1110 __set_default();
1112 stmt_set_parent_stmt(stmt->case_statement, stmt);
1113 __split_stmt(stmt->case_statement);
1116 int time_parsing_function(void)
1118 return ms_since(&fn_start_time) / 1000;
1121 bool taking_too_long(void)
1123 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1124 return 1;
1125 return 0;
1128 struct statement *get_last_stmt(void)
1130 struct symbol *fn;
1131 struct statement *stmt;
1133 fn = get_base_type(cur_func_sym);
1134 if (!fn)
1135 return NULL;
1136 stmt = fn->stmt;
1137 if (!stmt)
1138 stmt = fn->inline_stmt;
1139 if (!stmt || stmt->type != STMT_COMPOUND)
1140 return NULL;
1141 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1142 if (stmt && stmt->type == STMT_LABEL)
1143 stmt = stmt->label_statement;
1144 return stmt;
1147 int is_last_stmt(struct statement *cur_stmt)
1149 struct statement *last;
1151 last = get_last_stmt();
1152 if (last && last == cur_stmt)
1153 return 1;
1154 return 0;
1157 static void handle_backward_goto(struct statement *goto_stmt)
1159 const char *goto_name, *label_name;
1160 struct statement *func_stmt;
1161 struct symbol *base_type = get_base_type(cur_func_sym);
1162 struct statement *tmp;
1163 int found = 0;
1165 if (!option_info)
1166 return;
1167 if (last_goto_statement_handled)
1168 return;
1169 last_goto_statement_handled = 1;
1171 if (!goto_stmt->goto_label ||
1172 goto_stmt->goto_label->type != SYM_LABEL ||
1173 !goto_stmt->goto_label->ident)
1174 return;
1175 goto_name = goto_stmt->goto_label->ident->name;
1177 func_stmt = base_type->stmt;
1178 if (!func_stmt)
1179 func_stmt = base_type->inline_stmt;
1180 if (!func_stmt)
1181 return;
1182 if (func_stmt->type != STMT_COMPOUND)
1183 return;
1185 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1186 if (!found) {
1187 if (tmp->type != STMT_LABEL)
1188 continue;
1189 if (!tmp->label_identifier ||
1190 tmp->label_identifier->type != SYM_LABEL ||
1191 !tmp->label_identifier->ident)
1192 continue;
1193 label_name = tmp->label_identifier->ident->name;
1194 if (strcmp(goto_name, label_name) != 0)
1195 continue;
1196 found = 1;
1198 __split_stmt(tmp);
1199 } END_FOR_EACH_PTR(tmp);
1202 static void fake_a_return(void)
1204 struct expression *ret = NULL;
1206 nullify_path();
1207 __unnullify_path();
1209 if (cur_func_return_type() != &void_ctype)
1210 ret = unknown_value_expression(NULL);
1212 __pass_to_client(ret, RETURN_HOOK);
1213 nullify_path();
1216 static void split_ret_value(struct expression *expr)
1218 struct symbol *type;
1220 if (!expr)
1221 return;
1223 type = get_real_base_type(cur_func_sym);
1224 type = get_real_base_type(type);
1225 expr = fake_a_variable_assign(type, NULL, expr, -1);
1227 __in_fake_var_assign++;
1228 __split_expr(expr);
1229 __in_fake_var_assign--;
1232 static void fake_an_empty_default(struct position pos)
1234 static struct statement none = {};
1236 none.pos = pos;
1237 none.type = STMT_NONE;
1238 __merge_switches(top_expression(switch_expr_stack), NULL);
1239 __split_stmt(&none);
1242 static void split_compound(struct statement *stmt)
1244 struct statement *prev = NULL;
1245 struct statement *cur = NULL;
1246 struct statement *next;
1248 __push_scope_hooks();
1250 FOR_EACH_PTR(stmt->stmts, next) {
1251 /* just set them all ahead of time */
1252 stmt_set_parent_stmt(next, stmt);
1254 if (cur) {
1255 __prev_stmt = prev;
1256 __next_stmt = next;
1257 __cur_stmt = cur;
1258 __split_stmt(cur);
1260 prev = cur;
1261 cur = next;
1262 } END_FOR_EACH_PTR(next);
1263 if (cur) {
1264 __prev_stmt = prev;
1265 __cur_stmt = cur;
1266 __next_stmt = NULL;
1267 __split_stmt(cur);
1271 * For function scope, then delay calling the scope hooks until the
1272 * end of function hooks can run. I'm not positive this is the right
1273 * thing...
1275 if (!is_last_stmt(cur))
1276 __call_scope_hooks();
1280 * This is a hack, work around for detecting empty functions.
1282 static int need_delayed_scope_hooks(void)
1284 struct symbol *fn = get_base_type(cur_func_sym);
1285 struct statement *stmt;
1287 if (!fn)
1288 return 0;
1289 stmt = fn->stmt;
1290 if (!stmt)
1291 stmt = fn->inline_stmt;
1292 if (stmt && stmt->type == STMT_COMPOUND)
1293 return 1;
1294 return 0;
1297 void __split_label_stmt(struct statement *stmt)
1299 if (stmt->label_identifier &&
1300 stmt->label_identifier->type == SYM_LABEL &&
1301 stmt->label_identifier->ident) {
1302 loop_count |= 0x0800000;
1303 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1307 static void find_asm_gotos(struct statement *stmt)
1309 struct symbol *sym;
1311 FOR_EACH_PTR(stmt->asm_labels, sym) {
1312 __save_gotos(sym->ident->name, sym);
1313 } END_FOR_EACH_PTR(sym);
1316 static bool already_parsed_call(struct expression *call)
1318 struct expression *expr;
1320 FOR_EACH_PTR(parsed_calls, expr) {
1321 if (expr == call)
1322 return true;
1323 } END_FOR_EACH_PTR(expr);
1324 return false;
1327 static void free_parsed_call_stuff(bool free_fake_states)
1329 free_expression_stack(&parsed_calls);
1330 if (free_fake_states)
1331 __discard_fake_states(NULL);
1334 void __split_stmt(struct statement *stmt)
1336 sval_t sval;
1337 struct timeval start, stop;
1338 bool skip_after = false;
1340 gettimeofday(&start, NULL);
1342 if (!stmt)
1343 goto out;
1345 if (!__in_fake_assign)
1346 __silence_warnings_for_stmt = false;
1348 if (__bail_on_rest_of_function || is_skipped_function())
1349 return;
1351 if (out_of_memory() || taking_too_long()) {
1352 gettimeofday(&start, NULL);
1354 __bail_on_rest_of_function = 1;
1355 final_pass = 1;
1356 sm_perror("Function too hairy. Giving up. %lu seconds",
1357 start.tv_sec - fn_start_time.tv_sec);
1358 fake_a_return();
1359 final_pass = 0; /* turn off sm_msg() from here */
1360 return;
1363 indent_cnt++;
1365 add_ptr_list(&big_statement_stack, stmt);
1366 free_expression_stack(&big_expression_stack);
1367 free_parsed_call_stuff(indent_cnt == 1);
1368 set_position(stmt->pos);
1369 __pass_to_client(stmt, STMT_HOOK);
1371 switch (stmt->type) {
1372 case STMT_DECLARATION:
1373 split_declaration(stmt->declaration);
1374 break;
1375 case STMT_RETURN:
1376 expr_set_parent_stmt(stmt->ret_value, stmt);
1378 split_ret_value(stmt->ret_value);
1379 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1380 __process_post_op_stack();
1381 nullify_path();
1382 break;
1383 case STMT_EXPRESSION:
1384 expr_set_parent_stmt(stmt->expression, stmt);
1385 expr_set_parent_stmt(stmt->context, stmt);
1387 __split_expr(stmt->expression);
1388 break;
1389 case STMT_COMPOUND:
1390 split_compound(stmt);
1391 break;
1392 case STMT_IF:
1393 stmt_set_parent_stmt(stmt->if_true, stmt);
1394 stmt_set_parent_stmt(stmt->if_false, stmt);
1395 expr_set_parent_stmt(stmt->if_conditional, stmt);
1397 if (known_condition_true(stmt->if_conditional)) {
1398 __split_stmt(stmt->if_true);
1399 break;
1401 if (known_condition_false(stmt->if_conditional)) {
1402 __split_stmt(stmt->if_false);
1403 break;
1405 __split_whole_condition(stmt->if_conditional);
1406 __split_stmt(stmt->if_true);
1407 if (empty_statement(stmt->if_true) &&
1408 last_stmt_on_same_line() &&
1409 !get_macro_name(stmt->if_true->pos))
1410 sm_warning("if();");
1411 __push_true_states();
1412 __use_false_states();
1413 __split_stmt(stmt->if_false);
1414 __merge_true_states();
1415 break;
1416 case STMT_ITERATOR:
1417 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1418 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1419 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1420 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1421 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1423 if (stmt->iterator_pre_condition)
1424 handle_pre_loop(stmt);
1425 else if (stmt->iterator_post_condition)
1426 handle_post_loop(stmt);
1427 else {
1428 // these are for(;;) type loops.
1429 handle_pre_loop(stmt);
1431 break;
1432 case STMT_SWITCH:
1433 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1434 expr_set_parent_stmt(stmt->switch_expression, stmt);
1436 if (get_value(stmt->switch_expression, &sval)) {
1437 split_known_switch(stmt, sval);
1438 break;
1440 __split_expr(stmt->switch_expression);
1441 push_expression(&switch_expr_stack, stmt->switch_expression);
1442 __save_switch_states(top_expression(switch_expr_stack));
1443 nullify_path();
1444 __push_default();
1445 __push_breaks();
1446 __split_stmt(stmt->switch_statement);
1447 if (!__pop_default() && have_remaining_cases())
1448 fake_an_empty_default(stmt->pos);
1449 __discard_switches();
1450 __merge_breaks();
1451 pop_expression(&switch_expr_stack);
1452 break;
1453 case STMT_CASE:
1454 split_case(stmt);
1455 break;
1456 case STMT_LABEL:
1457 __split_label_stmt(stmt);
1458 __pass_to_client(stmt, STMT_HOOK_AFTER);
1459 skip_after = true;
1460 __split_stmt(stmt->label_statement);
1461 break;
1462 case STMT_GOTO:
1463 expr_set_parent_stmt(stmt->goto_expression, stmt);
1465 __split_expr(stmt->goto_expression);
1466 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1467 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1468 __process_breaks();
1469 } else if (!strcmp(stmt->goto_label->ident->name,
1470 "continue")) {
1471 __process_continues();
1473 } else if (stmt->goto_label &&
1474 stmt->goto_label->type == SYM_LABEL &&
1475 stmt->goto_label->ident) {
1476 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1478 nullify_path();
1479 if (is_last_stmt(stmt))
1480 handle_backward_goto(stmt);
1481 break;
1482 case STMT_NONE:
1483 break;
1484 case STMT_ASM:
1485 expr_set_parent_stmt(stmt->asm_string, stmt);
1487 find_asm_gotos(stmt);
1488 __pass_to_client(stmt, ASM_HOOK);
1489 __split_expr(stmt->asm_string);
1490 split_asm_ops(stmt->asm_outputs);
1491 split_asm_ops(stmt->asm_inputs);
1492 split_expr_list(stmt->asm_clobbers, NULL);
1493 break;
1494 case STMT_CONTEXT:
1495 break;
1496 case STMT_RANGE:
1497 __split_expr(stmt->range_expression);
1498 __split_expr(stmt->range_low);
1499 __split_expr(stmt->range_high);
1500 break;
1502 if (!skip_after)
1503 __pass_to_client(stmt, STMT_HOOK_AFTER);
1504 if (--indent_cnt == 1)
1505 free_parsed_call_stuff(true);
1507 out:
1508 __process_post_op_stack();
1510 gettimeofday(&stop, NULL);
1511 if (option_time_stmt && stmt)
1512 sm_msg("stmt_time%s: %ld",
1513 stmt->type == STMT_COMPOUND ? "_block" : "",
1514 stop.tv_sec - start.tv_sec);
1517 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1519 struct expression *expr;
1521 FOR_EACH_PTR(expr_list, expr) {
1522 expr_set_parent_expr(expr, parent);
1523 __split_expr(expr);
1524 __process_post_op_stack();
1525 } END_FOR_EACH_PTR(expr);
1528 static bool cast_arg(struct symbol *type, struct expression *arg)
1530 struct symbol *orig;
1532 if (!type)
1533 return false;
1535 arg = strip_parens(arg);
1536 if (arg != strip_expr(arg))
1537 return true;
1539 orig = get_type(arg);
1540 if (!orig)
1541 return true;
1542 if (types_equiv(orig, type))
1543 return false;
1545 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1546 return true;
1549 * I would have expected that we could just do use (orig == type) but I
1550 * guess for pointers we need to get the basetype to do that comparison.
1554 if (orig->type != SYM_PTR ||
1555 type->type != SYM_PTR) {
1556 if (type_fits(type, orig))
1557 return false;
1558 return true;
1560 orig = get_real_base_type(orig);
1561 type = get_real_base_type(type);
1562 if (orig == type)
1563 return false;
1565 return true;
1568 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1570 char buf[64];
1571 bool cast;
1573 if (!expr || !cur_func_sym)
1574 return NULL;
1576 if (already_parsed_call(call))
1577 return NULL;
1579 if (expr->type == EXPR_ASSIGNMENT)
1580 return expr;
1582 /* for va_args then we don't know the type */
1583 if (!type)
1584 type = get_type(expr);
1586 cast = cast_arg(type, expr);
1588 * Using expr_to_sym() here is a hack. We want to say that we don't
1589 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1590 * It turns out faking these assignments is way more expensive than I
1591 * would have imagined. I'm not sure why exactly.
1594 if (!cast) {
1596 * if the code is "return *p;" where "p" is a user pointer then
1597 * we want to create a fake assignment so that it sets the state
1598 * in check_kernel_user_data.c.
1601 if (expr->type != EXPR_PREOP &&
1602 expr->op != '*' && expr->op != '&' &&
1603 expr_to_sym(expr))
1604 return expr;
1607 if (nr == -1)
1608 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1609 else
1610 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1612 return create_fake_assign(buf, type, expr);
1615 static void split_args(struct expression *expr)
1617 struct expression *arg, *tmp;
1618 struct symbol *type;
1619 int i;
1621 i = -1;
1622 FOR_EACH_PTR(expr->args, arg) {
1623 i++;
1624 expr_set_parent_expr(arg, expr);
1625 type = get_arg_type(expr->fn, i);
1626 tmp = fake_a_variable_assign(type, expr, arg, i);
1627 if (tmp != arg)
1628 __in_fake_var_assign++;
1629 __split_expr(tmp);
1630 if (tmp != arg)
1631 __in_fake_var_assign--;
1632 __process_post_op_stack();
1633 } END_FOR_EACH_PTR(arg);
1636 static void call_cleanup_fn(void *_sym)
1638 struct symbol *sym = _sym;
1639 struct expression *call, *arg;
1640 struct expression_list *args = NULL;
1642 if (!sym->cleanup)
1643 return;
1645 arg = symbol_expression(sym);
1646 arg = preop_expression(arg, '&');
1647 add_ptr_list(&args, arg);
1648 call = call_expression(sym->cleanup, args);
1650 __split_expr(call);
1653 static void add_cleanup_hook(struct symbol *sym)
1655 add_scope_hook(&call_cleanup_fn, sym);
1658 static void split_sym(struct symbol *sym)
1660 if (!sym)
1661 return;
1662 if (!(sym->namespace & NS_SYMBOL))
1663 return;
1665 __split_stmt(sym->stmt);
1666 __split_expr(sym->array_size);
1667 if (sym->cleanup)
1668 add_cleanup_hook(sym);
1669 split_symlist(sym->arguments);
1670 split_symlist(sym->symbol_list);
1671 __split_stmt(sym->inline_stmt);
1672 split_symlist(sym->inline_symbol_list);
1675 static void split_symlist(struct symbol_list *sym_list)
1677 struct symbol *sym;
1679 FOR_EACH_PTR(sym_list, sym) {
1680 split_sym(sym);
1681 } END_FOR_EACH_PTR(sym);
1684 typedef void (fake_cb)(struct expression *expr);
1686 static int member_to_number(struct expression *expr, struct ident *member)
1688 struct symbol *type, *tmp;
1689 char *name;
1690 int i;
1692 if (!member)
1693 return -1;
1694 name = member->name;
1696 type = get_type(expr);
1697 if (!type || type->type != SYM_STRUCT)
1698 return -1;
1700 i = -1;
1701 FOR_EACH_PTR(type->symbol_list, tmp) {
1702 i++;
1703 if (!tmp->ident)
1704 continue;
1705 if (strcmp(name, tmp->ident->name) == 0)
1706 return i;
1707 } END_FOR_EACH_PTR(tmp);
1708 return -1;
1711 static struct ident *number_to_member(struct expression *expr, int num)
1713 struct symbol *type, *member;
1714 int i = 0;
1716 type = get_type(expr);
1717 if (!type || type->type != SYM_STRUCT)
1718 return NULL;
1720 FOR_EACH_PTR(type->symbol_list, member) {
1721 if (i == num)
1722 return member->ident;
1723 i++;
1724 } END_FOR_EACH_PTR(member);
1725 return NULL;
1728 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1730 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1732 struct expression *edge_member, *assign;
1733 struct symbol *base = get_real_base_type(member);
1734 struct symbol *tmp;
1736 if (member->ident)
1737 expr = member_expression(expr, '.', member->ident);
1739 FOR_EACH_PTR(base->symbol_list, tmp) {
1740 struct symbol *type;
1742 type = get_real_base_type(tmp);
1743 if (!type)
1744 continue;
1746 edge_member = member_expression(expr, '.', tmp->ident);
1747 if (get_extra_state(edge_member))
1748 continue;
1750 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1751 set_inner_struct_members(expr, tmp);
1752 continue;
1755 if (!tmp->ident)
1756 continue;
1758 assign = assign_expression(edge_member, '=', zero_expr());
1759 __split_expr(assign);
1760 } END_FOR_EACH_PTR(tmp);
1765 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1767 struct symbol *tmp;
1768 struct expression *member = NULL;
1769 struct expression *assign;
1771 FOR_EACH_PTR(type->symbol_list, tmp) {
1772 type = get_real_base_type(tmp);
1773 if (!type)
1774 continue;
1776 if (tmp->ident) {
1777 member = member_expression(expr, '.', tmp->ident);
1778 if (get_extra_state(member))
1779 continue;
1782 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1783 set_inner_struct_members(expr, tmp);
1784 continue;
1786 if (type->type == SYM_ARRAY)
1787 continue;
1788 if (!tmp->ident)
1789 continue;
1791 assign = assign_expression(member, '=', zero_expr());
1792 __split_expr(assign);
1793 } END_FOR_EACH_PTR(tmp);
1796 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1798 struct expression *deref, *assign, *tmp, *right;
1799 struct symbol *struct_type, *type;
1800 struct ident *member;
1801 int member_idx;
1803 struct_type = get_type(symbol);
1804 if (!struct_type ||
1805 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1806 return;
1809 * We're parsing an initializer that could look something like this:
1810 * struct foo foo = {
1811 * 42,
1812 * .whatever.xxx = 11,
1813 * .zzz = 12,
1814 * };
1816 * So what we have here is a list with 42, .whatever, and .zzz. We need
1817 * to break it up into left and right sides of the assignments.
1820 member_idx = 0;
1821 FOR_EACH_PTR(members, tmp) {
1822 deref = NULL;
1823 if (tmp->type == EXPR_IDENTIFIER) {
1824 member_idx = member_to_number(symbol, tmp->expr_ident);
1825 while (tmp->type == EXPR_IDENTIFIER) {
1826 member = tmp->expr_ident;
1827 tmp = tmp->ident_expression;
1828 if (deref)
1829 deref = member_expression(deref, '.', member);
1830 else
1831 deref = member_expression(symbol, '.', member);
1833 } else {
1834 member = number_to_member(symbol, member_idx);
1835 deref = member_expression(symbol, '.', member);
1837 right = tmp;
1838 member_idx++;
1839 if (right->type == EXPR_INITIALIZER) {
1840 type = get_type(deref);
1841 if (type && type->type == SYM_ARRAY)
1842 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1843 else
1844 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1845 } else {
1846 assign = assign_expression(deref, '=', right);
1847 fake_cb(assign);
1849 } END_FOR_EACH_PTR(tmp);
1851 set_unset_to_zero(struct_type, symbol);
1854 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1856 fake_member_assigns_helper(symbol_expression(sym),
1857 sym->initializer->expr_list, fake_cb);
1860 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1862 struct expression *offset, *binop, *assign, *tmp;
1863 struct symbol *type;
1864 int idx, max;
1866 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1867 return;
1869 max = 0;
1870 idx = 0;
1871 FOR_EACH_PTR(expr_list, tmp) {
1872 if (tmp->type == EXPR_INDEX) {
1873 if (tmp->idx_from != tmp->idx_to)
1874 return;
1875 idx = tmp->idx_from;
1876 if (idx > max)
1877 max = idx;
1878 if (!tmp->idx_expression)
1879 goto next;
1880 tmp = tmp->idx_expression;
1882 offset = value_expr(idx);
1883 binop = array_element_expression(array, offset);
1884 if (tmp->type == EXPR_INITIALIZER) {
1885 type = get_type(binop);
1886 if (type && type->type == SYM_ARRAY)
1887 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1888 else
1889 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1890 } else {
1891 assign = assign_expression(binop, '=', tmp);
1892 fake_cb(assign);
1894 next:
1895 idx++;
1896 if (idx > max)
1897 max = idx;
1898 } END_FOR_EACH_PTR(tmp);
1900 __call_array_initialized_hooks(array, max);
1903 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1905 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1908 static void fake_assign_expr(struct symbol *sym)
1910 struct expression *assign, *symbol;
1912 symbol = symbol_expression(sym);
1913 assign = assign_expression(symbol, '=', sym->initializer);
1914 __split_expr(assign);
1917 static void do_initializer_stuff(struct symbol *sym)
1919 if (!sym->initializer)
1920 return;
1922 if (sym->initializer->type == EXPR_INITIALIZER) {
1923 if (get_real_base_type(sym)->type == SYM_ARRAY)
1924 fake_element_assigns(sym, __split_expr);
1925 else
1926 fake_member_assigns(sym, __split_expr);
1927 } else {
1928 fake_assign_expr(sym);
1932 static void split_declaration(struct symbol_list *sym_list)
1934 struct symbol *sym;
1936 FOR_EACH_PTR(sym_list, sym) {
1937 __pass_to_client(sym, DECLARATION_HOOK);
1938 do_initializer_stuff(sym);
1939 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1940 split_sym(sym);
1941 } END_FOR_EACH_PTR(sym);
1944 static void call_global_assign_hooks(struct expression *assign)
1946 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1949 static void fake_global_assign(struct symbol *sym)
1951 struct expression *assign, *symbol;
1953 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1954 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1955 fake_element_assigns(sym, call_global_assign_hooks);
1956 } else if (sym->initializer) {
1957 symbol = symbol_expression(sym);
1958 assign = assign_expression(symbol, '=', sym->initializer);
1959 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1960 } else {
1961 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1963 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1964 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1965 fake_member_assigns(sym, call_global_assign_hooks);
1966 } else if (sym->initializer) {
1967 symbol = symbol_expression(sym);
1968 assign = assign_expression(symbol, '=', sym->initializer);
1969 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1970 } else {
1971 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1973 } else {
1974 symbol = symbol_expression(sym);
1975 if (sym->initializer) {
1976 assign = assign_expression(symbol, '=', sym->initializer);
1977 __split_expr(assign);
1978 } else {
1979 assign = assign_expression(symbol, '=', zero_expr());
1981 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1985 static void start_function_definition(struct symbol *sym)
1987 __in_function_def = 1;
1988 __pass_to_client(sym, FUNC_DEF_HOOK);
1989 __in_function_def = 0;
1990 __pass_to_client(sym, AFTER_DEF_HOOK);
1994 void add_function_data(unsigned long *fn_data)
1996 __add_ptr_list(&fn_data_list, fn_data);
1999 static void clear_function_data(void)
2001 unsigned long *tmp;
2003 FOR_EACH_PTR(fn_data_list, tmp) {
2004 *tmp = 0;
2005 } END_FOR_EACH_PTR(tmp);
2008 static void record_func_time(void)
2010 struct timeval stop;
2011 int func_time;
2012 char buf[32];
2014 gettimeofday(&stop, NULL);
2015 func_time = stop.tv_sec - fn_start_time.tv_sec;
2016 snprintf(buf, sizeof(buf), "%d", func_time);
2017 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
2018 if (option_time && func_time > 2) {
2019 final_pass++;
2020 sm_msg("func_time: %d", func_time);
2021 final_pass--;
2025 static void split_function(struct symbol *sym)
2027 struct symbol *base_type = get_base_type(sym);
2029 if (!base_type->stmt && !base_type->inline_stmt)
2030 return;
2032 gettimeofday(&outer_fn_start_time, NULL);
2033 gettimeofday(&fn_start_time, NULL);
2034 cur_func_sym = sym;
2035 if (sym->ident)
2036 cur_func = sym->ident->name;
2037 if (option_process_function && cur_func &&
2038 strcmp(option_process_function, cur_func) != 0)
2039 return;
2040 set_position(sym->pos);
2041 clear_function_data();
2042 loop_count = 0;
2043 last_goto_statement_handled = 0;
2044 sm_debug("new function: %s\n", cur_func);
2045 __stree_id = 0;
2046 if (option_two_passes) {
2047 __unnullify_path();
2048 loop_num = 0;
2049 final_pass = 0;
2050 start_function_definition(sym);
2051 __split_stmt(base_type->stmt);
2052 __split_stmt(base_type->inline_stmt);
2053 nullify_path();
2055 __unnullify_path();
2056 loop_num = 0;
2057 final_pass = 1;
2058 start_function_definition(sym);
2059 __split_stmt(base_type->stmt);
2060 __split_stmt(base_type->inline_stmt);
2061 if (!__path_is_null() &&
2062 cur_func_return_type() == &void_ctype &&
2063 !__bail_on_rest_of_function) {
2064 __pass_to_client(NULL, RETURN_HOOK);
2065 nullify_path();
2067 __pass_to_client(sym, END_FUNC_HOOK);
2068 if (need_delayed_scope_hooks())
2069 __call_scope_hooks();
2070 __pass_to_client(sym, AFTER_FUNC_HOOK);
2071 sym->parsed = true;
2073 clear_all_states();
2075 record_func_time();
2077 cur_func_sym = NULL;
2078 cur_func = NULL;
2079 free_data_info_allocs();
2080 free_expression_stack(&switch_expr_stack);
2081 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2082 __bail_on_rest_of_function = 0;
2085 static void save_flow_state(void)
2087 unsigned long *tmp;
2089 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
2090 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
2091 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
2093 __add_ptr_list(&backup, big_statement_stack);
2094 __add_ptr_list(&backup, big_expression_stack);
2095 __add_ptr_list(&backup, big_condition_stack);
2096 __add_ptr_list(&backup, switch_expr_stack);
2098 __add_ptr_list(&backup, cur_func_sym);
2100 __add_ptr_list(&backup, parsed_calls);
2102 __add_ptr_list(&backup, __prev_stmt);
2103 __add_ptr_list(&backup, __cur_stmt);
2104 __add_ptr_list(&backup, __next_stmt);
2106 FOR_EACH_PTR(fn_data_list, tmp) {
2107 __add_ptr_list(&backup, (void *)*tmp);
2108 } END_FOR_EACH_PTR(tmp);
2111 static void *pop_backup(void)
2113 void *ret;
2115 ret = last_ptr_list(backup);
2116 delete_ptr_list_last(&backup);
2117 return ret;
2120 static void restore_flow_state(void)
2122 unsigned long *tmp;
2124 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2125 *tmp = (unsigned long)pop_backup();
2126 } END_FOR_EACH_PTR_REVERSE(tmp);
2128 __next_stmt = pop_backup();
2129 __cur_stmt = pop_backup();
2130 __prev_stmt = pop_backup();
2132 parsed_calls = pop_backup();
2134 cur_func_sym = pop_backup();
2135 switch_expr_stack = pop_backup();
2136 big_condition_stack = pop_backup();
2137 big_expression_stack = pop_backup();
2138 big_statement_stack = pop_backup();
2139 final_pass = PTR_INT(pop_backup()) >> 2;
2140 loop_count = PTR_INT(pop_backup()) >> 2;
2141 loop_num = PTR_INT(pop_backup()) >> 2;
2144 void parse_inline(struct expression *call)
2146 struct symbol *base_type;
2147 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2148 struct timeval time_backup = fn_start_time;
2149 struct expression *orig_inline = __inline_fn;
2150 int orig_budget;
2152 if (out_of_memory() || taking_too_long())
2153 return;
2155 if (already_parsed_call(call))
2156 return;
2158 save_flow_state();
2160 __pass_to_client(call, INLINE_FN_START);
2161 final_pass = 0; /* don't print anything */
2162 __inline_fn = call;
2163 orig_budget = inline_budget;
2164 inline_budget = inline_budget - 5;
2166 base_type = get_base_type(call->fn->symbol);
2167 cur_func_sym = call->fn->symbol;
2168 if (call->fn->symbol->ident)
2169 cur_func = call->fn->symbol->ident->name;
2170 else
2171 cur_func = NULL;
2172 set_position(call->fn->symbol->pos);
2174 save_all_states();
2175 big_statement_stack = NULL;
2176 big_expression_stack = NULL;
2177 big_condition_stack = NULL;
2178 switch_expr_stack = NULL;
2179 parsed_calls = NULL;
2181 sm_debug("inline function: %s\n", cur_func);
2182 __unnullify_path();
2183 clear_function_data();
2184 loop_num = 0;
2185 loop_count = 0;
2186 start_function_definition(call->fn->symbol);
2187 __split_stmt(base_type->stmt);
2188 __split_stmt(base_type->inline_stmt);
2189 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2190 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2191 call->fn->symbol->parsed = true;
2193 free_expression_stack(&switch_expr_stack);
2194 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2195 nullify_path();
2196 free_goto_stack();
2198 restore_flow_state();
2199 fn_start_time = time_backup;
2200 cur_func = cur_func_bak;
2202 restore_all_states();
2203 set_position(call->pos);
2204 __inline_fn = orig_inline;
2205 inline_budget = orig_budget;
2206 __pass_to_client(call, INLINE_FN_END);
2209 static struct symbol_list *inlines_called;
2210 static void add_inline_function(struct symbol *sym)
2212 static struct symbol_list *already_added;
2213 struct symbol *tmp;
2215 FOR_EACH_PTR(already_added, tmp) {
2216 if (tmp == sym)
2217 return;
2218 } END_FOR_EACH_PTR(tmp);
2220 add_ptr_list(&already_added, sym);
2221 add_ptr_list(&inlines_called, sym);
2224 static void process_inlines(void)
2226 struct symbol *tmp;
2228 FOR_EACH_PTR(inlines_called, tmp) {
2229 split_function(tmp);
2230 } END_FOR_EACH_PTR(tmp);
2231 free_ptr_list(&inlines_called);
2234 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2236 struct symbol *sym;
2238 FOR_EACH_PTR_REVERSE(big_list, sym) {
2239 if (!sym->scope)
2240 continue;
2241 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2242 return sym;
2243 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2244 return sym;
2245 } END_FOR_EACH_PTR_REVERSE(sym);
2247 return NULL;
2250 static bool interesting_function(struct symbol *sym)
2252 static int prev_stream = -1;
2253 static bool prev_answer;
2254 const char *filename;
2255 int len;
2257 if (!(sym->ctype.modifiers & MOD_INLINE))
2258 return true;
2260 if (sym->pos.stream == prev_stream)
2261 return prev_answer;
2263 prev_stream = sym->pos.stream;
2264 prev_answer = false;
2266 filename = stream_name(sym->pos.stream);
2267 len = strlen(filename);
2268 if (len > 0 && filename[len - 1] == 'c')
2269 prev_answer = true;
2270 return prev_answer;
2273 static void split_inlines_in_scope(struct symbol *sym)
2275 struct symbol *base;
2276 struct symbol_list *scope_list;
2277 int stream;
2279 scope_list = sym->scope->symbols;
2280 stream = sym->pos.stream;
2282 /* find the last static symbol in the file */
2283 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2284 if (sym->pos.stream != stream)
2285 continue;
2286 if (sym->type != SYM_NODE)
2287 continue;
2288 base = get_base_type(sym);
2289 if (!base)
2290 continue;
2291 if (base->type != SYM_FN)
2292 continue;
2293 if (!base->inline_stmt)
2294 continue;
2295 if (!interesting_function(sym))
2296 continue;
2297 add_inline_function(sym);
2298 } END_FOR_EACH_PTR_REVERSE(sym);
2300 process_inlines();
2303 static void split_inlines(struct symbol_list *sym_list)
2305 struct symbol *sym;
2307 sym = get_last_scoped_symbol(sym_list, 0);
2308 if (sym)
2309 split_inlines_in_scope(sym);
2310 sym = get_last_scoped_symbol(sym_list, 1);
2311 if (sym)
2312 split_inlines_in_scope(sym);
2315 static struct stree *clone_estates_perm(struct stree *orig)
2317 struct stree *ret = NULL;
2318 struct sm_state *tmp;
2320 FOR_EACH_SM(orig, tmp) {
2321 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2322 } END_FOR_EACH_SM(tmp);
2324 return ret;
2327 struct position last_pos;
2328 static void split_c_file_functions(struct symbol_list *sym_list)
2330 struct symbol *sym;
2332 __unnullify_path();
2333 FOR_EACH_PTR(sym_list, sym) {
2334 set_position(sym->pos);
2335 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2336 __pass_to_client(sym, BASE_HOOK);
2337 fake_global_assign(sym);
2338 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2340 } END_FOR_EACH_PTR(sym);
2341 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2342 nullify_path();
2344 FOR_EACH_PTR(sym_list, sym) {
2345 set_position(sym->pos);
2346 last_pos = sym->pos;
2347 if (!interesting_function(sym))
2348 continue;
2349 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2350 split_function(sym);
2351 process_inlines();
2353 last_pos = sym->pos;
2354 } END_FOR_EACH_PTR(sym);
2355 split_inlines(sym_list);
2356 __pass_to_client(sym_list, END_FILE_HOOK);
2359 static int final_before_fake;
2360 void init_fake_env(void)
2362 if (!in_fake_env)
2363 final_before_fake = final_pass;
2364 in_fake_env++;
2365 __push_fake_cur_stree();
2366 final_pass = 0;
2369 void end_fake_env(void)
2371 __free_fake_cur_stree();
2372 in_fake_env--;
2373 if (!in_fake_env)
2374 final_pass = final_before_fake;
2377 static void open_output_files(char *base_file)
2379 char buf[256];
2381 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2382 sm_outfd = fopen(buf, "w");
2383 if (!sm_outfd)
2384 sm_fatal("Cannot open %s", buf);
2386 if (!option_info)
2387 return;
2389 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2390 sql_outfd = fopen(buf, "w");
2391 if (!sql_outfd)
2392 sm_fatal("Error: Cannot open %s", buf);
2394 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2395 caller_info_fd = fopen(buf, "w");
2396 if (!caller_info_fd)
2397 sm_fatal("Error: Cannot open %s", buf);
2400 void smatch(struct string_list *filelist)
2402 struct symbol_list *sym_list;
2403 struct timeval stop, start;
2404 char *path;
2405 int len;
2407 gettimeofday(&start, NULL);
2409 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2410 path = getcwd(NULL, 0);
2411 free(full_base_file);
2412 if (path) {
2413 len = strlen(path) + 1 + strlen(base_file) + 1;
2414 full_base_file = malloc(len);
2415 snprintf(full_base_file, len, "%s/%s", path, base_file);
2416 } else {
2417 full_base_file = alloc_string(base_file);
2419 if (option_file_output)
2420 open_output_files(base_file);
2421 base_file_stream = input_stream_nr;
2422 sym_list = sparse_keep_tokens(base_file);
2423 split_c_file_functions(sym_list);
2424 } END_FOR_EACH_PTR_NOTAG(base_file);
2426 gettimeofday(&stop, NULL);
2428 set_position(last_pos);
2429 final_pass = 1;
2430 if (option_time)
2431 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2432 if (option_mem)
2433 sm_msg("mem: %luKb", get_max_memory());