preempt_info: add class_raw_spinlock_irq_destructor()
[smatch.git] / smatch_flow.c
blob623a7abd761bb670706315a74c3cd7de48f29879
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 __push_scope_hooks();
862 loop_name = get_loop_name(loop_num);
863 loop_num++;
865 split_declaration(stmt->iterator_syms);
866 if (stmt->iterator_pre_statement) {
867 __split_stmt(stmt->iterator_pre_statement);
868 __prev_stmt = stmt->iterator_pre_statement;
871 once_through = call_once_through_hooks(stmt);
873 loop_count++;
874 __push_continues();
875 __push_breaks();
877 __merge_gotos(loop_name, NULL);
879 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
880 __in_pre_condition++;
881 __pass_to_client(stmt, PRELOOP_HOOK);
882 __split_whole_condition(stmt->iterator_pre_condition);
883 __in_pre_condition--;
884 FOR_EACH_SM(stree, sm) {
885 set_state(sm->owner, sm->name, sm->sym, sm->state);
886 } END_FOR_EACH_SM(sm);
887 free_stree(&stree);
888 if (extra_sm)
889 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
891 __split_stmt(stmt->iterator_statement);
892 if (is_forever_loop(stmt)) {
893 __merge_continues();
894 __save_gotos(loop_name, NULL);
896 __push_fake_cur_stree();
897 __split_stmt(stmt->iterator_post_statement);
898 stree = __pop_fake_cur_stree();
900 __discard_false_states();
901 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
902 __use_breaks();
904 if (!__path_is_null())
905 __merge_stree_into_cur(stree);
906 free_stree(&stree);
907 } else {
908 __merge_continues();
909 unchanged = __iterator_unchanged(extra_sm);
910 __split_stmt(stmt->iterator_post_statement);
911 __prev_stmt = stmt->iterator_post_statement;
912 __cur_stmt = stmt;
914 __save_gotos(loop_name, NULL);
915 __in_pre_condition++;
916 __split_whole_condition(stmt->iterator_pre_condition);
917 __in_pre_condition--;
918 nullify_path();
919 __merge_false_states();
920 if (once_through)
921 __discard_false_states();
922 else
923 __merge_false_states();
925 if (extra_sm && unchanged)
926 __extra_pre_loop_hook_after(extra_sm,
927 stmt->iterator_post_statement,
928 stmt->iterator_pre_condition);
929 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
930 __merge_breaks();
932 loop_count--;
934 __call_scope_hooks();
938 * Post loops are do {} while();
940 static void handle_post_loop(struct statement *stmt)
942 char *loop_name;
944 loop_name = get_loop_name(loop_num);
945 loop_num++;
946 loop_count++;
948 __pass_to_client(stmt, POSTLOOP_HOOK);
950 __push_continues();
951 __push_breaks();
952 __merge_gotos(loop_name, NULL);
953 __split_stmt(stmt->iterator_statement);
954 __merge_continues();
955 if (!expr_is_zero(stmt->iterator_post_condition))
956 __save_gotos(loop_name, NULL);
958 if (is_forever_loop(stmt)) {
959 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
960 __use_breaks();
961 } else {
962 __split_whole_condition(stmt->iterator_post_condition);
963 __use_false_states();
964 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
965 __merge_breaks();
967 loop_count--;
970 static int empty_statement(struct statement *stmt)
972 if (!stmt)
973 return 0;
974 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
975 return 1;
976 return 0;
979 static int last_stmt_on_same_line(void)
981 struct statement *stmt;
982 int i = 0;
984 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
985 if (!i++)
986 continue;
987 if (stmt->pos.line == get_lineno())
988 return 1;
989 return 0;
990 } END_FOR_EACH_PTR_REVERSE(stmt);
991 return 0;
994 static void split_asm_ops(struct asm_operand_list *ops)
996 struct asm_operand *op;
998 FOR_EACH_PTR(ops, op) {
999 __split_expr(op->expr);
1000 } END_FOR_EACH_PTR(op);
1003 static int is_case_val(struct statement *stmt, sval_t sval)
1005 sval_t case_sval;
1007 if (stmt->type != STMT_CASE)
1008 return 0;
1009 if (!stmt->case_expression) {
1010 __set_default();
1011 return 1;
1013 if (!get_value(stmt->case_expression, &case_sval))
1014 return 0;
1015 if (case_sval.value == sval.value)
1016 return 1;
1017 return 0;
1020 static struct range_list *get_case_rl(struct expression *switch_expr,
1021 struct expression *case_expr,
1022 struct expression *case_to)
1024 sval_t start, end;
1025 struct range_list *rl = NULL;
1026 struct symbol *switch_type;
1028 switch_type = get_type(switch_expr);
1029 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
1030 start = sval_cast(switch_type, start);
1031 end = sval_cast(switch_type, end);
1032 add_range(&rl, start, end);
1033 } else if (get_value(case_expr, &start)) {
1034 start = sval_cast(switch_type, start);
1035 add_range(&rl, start, start);
1038 return rl;
1041 static void split_known_switch(struct statement *stmt, sval_t sval)
1043 struct statement *tmp;
1044 struct range_list *rl;
1046 __split_expr(stmt->switch_expression);
1047 sval = sval_cast(get_type(stmt->switch_expression), sval);
1049 push_expression(&switch_expr_stack, stmt->switch_expression);
1050 __save_switch_states(top_expression(switch_expr_stack));
1051 nullify_path();
1052 __push_default();
1053 __push_breaks();
1055 stmt = stmt->switch_statement;
1057 __push_scope_hooks();
1058 FOR_EACH_PTR(stmt->stmts, tmp) {
1059 __smatch_lineno = tmp->pos.line;
1060 // FIXME: what if default comes before the known case statement?
1061 if (is_case_val(tmp, sval)) {
1062 rl = alloc_rl(sval, sval);
1063 __merge_switches(top_expression(switch_expr_stack), rl);
1064 __pass_case_to_client(top_expression(switch_expr_stack), rl);
1065 stmt_set_parent_stmt(tmp->case_statement, tmp);
1066 __split_stmt(tmp->case_statement);
1067 goto next;
1069 if (__path_is_null())
1070 continue;
1071 __split_stmt(tmp);
1072 next:
1073 if (__path_is_null()) {
1074 __set_default();
1075 goto out;
1077 } END_FOR_EACH_PTR(tmp);
1078 out:
1079 __call_scope_hooks();
1080 if (!__pop_default())
1081 __merge_switches(top_expression(switch_expr_stack), NULL);
1082 __discard_switches();
1083 __merge_breaks();
1084 pop_expression(&switch_expr_stack);
1087 static void split_case(struct statement *stmt)
1089 struct range_list *rl = NULL;
1091 expr_set_parent_stmt(stmt->case_expression, stmt);
1092 expr_set_parent_stmt(stmt->case_to, stmt);
1094 rl = get_case_rl(top_expression(switch_expr_stack),
1095 stmt->case_expression, stmt->case_to);
1096 while (stmt->case_statement->type == STMT_CASE) {
1097 struct range_list *tmp;
1099 tmp = get_case_rl(top_expression(switch_expr_stack),
1100 stmt->case_statement->case_expression,
1101 stmt->case_statement->case_to);
1102 if (!tmp)
1103 goto next;
1104 rl = rl_union(rl, tmp);
1105 if (!stmt->case_expression)
1106 __set_default();
1107 next:
1108 stmt = stmt->case_statement;
1111 __merge_switches(top_expression(switch_expr_stack), rl);
1113 if (!stmt->case_expression)
1114 __set_default();
1116 stmt_set_parent_stmt(stmt->case_statement, stmt);
1117 __split_stmt(stmt->case_statement);
1120 int time_parsing_function(void)
1122 return ms_since(&fn_start_time) / 1000;
1125 bool taking_too_long(void)
1127 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1128 return 1;
1129 return 0;
1132 struct statement *get_last_stmt(void)
1134 struct symbol *fn;
1135 struct statement *stmt;
1137 fn = get_base_type(cur_func_sym);
1138 if (!fn)
1139 return NULL;
1140 stmt = fn->stmt;
1141 if (!stmt)
1142 stmt = fn->inline_stmt;
1143 if (!stmt || stmt->type != STMT_COMPOUND)
1144 return NULL;
1145 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1146 if (stmt && stmt->type == STMT_LABEL)
1147 stmt = stmt->label_statement;
1148 return stmt;
1151 int is_last_stmt(struct statement *cur_stmt)
1153 struct statement *last;
1155 last = get_last_stmt();
1156 if (last && last == cur_stmt)
1157 return 1;
1158 return 0;
1161 static bool is_function_scope(struct statement *stmt)
1163 struct symbol *base_type;
1165 if (!cur_func_sym)
1166 return false;
1168 base_type = get_base_type(cur_func_sym);
1169 if (base_type->stmt == stmt ||
1170 base_type->inline_stmt == stmt)
1171 return true;
1173 return false;
1176 static void handle_backward_goto(struct statement *goto_stmt)
1178 const char *goto_name, *label_name;
1179 struct statement *func_stmt;
1180 struct symbol *base_type = get_base_type(cur_func_sym);
1181 struct statement *tmp;
1182 int found = 0;
1184 if (!option_info)
1185 return;
1186 if (last_goto_statement_handled)
1187 return;
1188 last_goto_statement_handled = 1;
1190 if (!goto_stmt->goto_label ||
1191 goto_stmt->goto_label->type != SYM_LABEL ||
1192 !goto_stmt->goto_label->ident)
1193 return;
1194 goto_name = goto_stmt->goto_label->ident->name;
1196 func_stmt = base_type->stmt;
1197 if (!func_stmt)
1198 func_stmt = base_type->inline_stmt;
1199 if (!func_stmt)
1200 return;
1201 if (func_stmt->type != STMT_COMPOUND)
1202 return;
1204 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1205 if (!found) {
1206 if (tmp->type != STMT_LABEL)
1207 continue;
1208 if (!tmp->label_identifier ||
1209 tmp->label_identifier->type != SYM_LABEL ||
1210 !tmp->label_identifier->ident)
1211 continue;
1212 label_name = tmp->label_identifier->ident->name;
1213 if (strcmp(goto_name, label_name) != 0)
1214 continue;
1215 found = 1;
1217 __split_stmt(tmp);
1218 } END_FOR_EACH_PTR(tmp);
1221 static void fake_a_return(void)
1223 struct expression *ret = NULL;
1225 nullify_path();
1226 __unnullify_path();
1228 if (cur_func_return_type() != &void_ctype)
1229 ret = unknown_value_expression(NULL);
1231 __pass_to_client(ret, RETURN_HOOK);
1232 nullify_path();
1235 static void split_ret_value(struct expression *expr)
1237 struct symbol *type;
1239 if (!expr)
1240 return;
1242 type = get_real_base_type(cur_func_sym);
1243 type = get_real_base_type(type);
1244 expr = fake_a_variable_assign(type, NULL, expr, -1);
1246 __in_fake_var_assign++;
1247 __split_expr(expr);
1248 __in_fake_var_assign--;
1251 static void fake_an_empty_default(struct position pos)
1253 static struct statement none = {};
1255 none.pos = pos;
1256 none.type = STMT_NONE;
1257 __merge_switches(top_expression(switch_expr_stack), NULL);
1258 __split_stmt(&none);
1261 static void split_compound(struct statement *stmt)
1263 struct statement *prev = NULL;
1264 struct statement *cur = NULL;
1265 struct statement *next;
1267 __push_scope_hooks();
1269 FOR_EACH_PTR(stmt->stmts, next) {
1270 /* just set them all ahead of time */
1271 stmt_set_parent_stmt(next, stmt);
1273 if (cur) {
1274 __prev_stmt = prev;
1275 __next_stmt = next;
1276 __cur_stmt = cur;
1277 __split_stmt(cur);
1279 prev = cur;
1280 cur = next;
1281 } END_FOR_EACH_PTR(next);
1282 if (cur) {
1283 __prev_stmt = prev;
1284 __cur_stmt = cur;
1285 __next_stmt = NULL;
1286 __split_stmt(cur);
1290 * For function scope, then delay calling the scope hooks until the
1291 * end of function hooks can run.
1293 if (!is_function_scope(stmt))
1294 __call_scope_hooks();
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 static void parse_fn_statements(struct symbol *base)
1996 __split_stmt(base->stmt);
1997 __split_stmt(base->inline_stmt);
2000 void add_function_data(unsigned long *fn_data)
2002 __add_ptr_list(&fn_data_list, fn_data);
2005 static void clear_function_data(void)
2007 unsigned long *tmp;
2009 FOR_EACH_PTR(fn_data_list, tmp) {
2010 *tmp = 0;
2011 } END_FOR_EACH_PTR(tmp);
2014 static void record_func_time(void)
2016 struct timeval stop;
2017 int func_time;
2018 char buf[32];
2020 gettimeofday(&stop, NULL);
2021 func_time = stop.tv_sec - fn_start_time.tv_sec;
2022 snprintf(buf, sizeof(buf), "%d", func_time);
2023 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
2024 if (option_time && func_time > 2) {
2025 final_pass++;
2026 sm_msg("func_time: %d", func_time);
2027 final_pass--;
2031 static void split_function(struct symbol *sym)
2033 struct symbol *base_type = get_base_type(sym);
2035 if (!base_type->stmt && !base_type->inline_stmt)
2036 return;
2038 gettimeofday(&outer_fn_start_time, NULL);
2039 gettimeofday(&fn_start_time, NULL);
2040 cur_func_sym = sym;
2041 if (sym->ident)
2042 cur_func = sym->ident->name;
2043 if (option_process_function && cur_func &&
2044 strcmp(option_process_function, cur_func) != 0)
2045 return;
2046 set_position(sym->pos);
2047 clear_function_data();
2048 loop_count = 0;
2049 last_goto_statement_handled = 0;
2050 sm_debug("new function: %s\n", cur_func);
2051 __stree_id = 0;
2052 if (option_two_passes) {
2053 __unnullify_path();
2054 loop_num = 0;
2055 final_pass = 0;
2056 start_function_definition(sym);
2057 parse_fn_statements(base_type);
2058 __call_scope_hooks();
2059 nullify_path();
2061 __unnullify_path();
2062 loop_num = 0;
2063 final_pass = 1;
2064 start_function_definition(sym);
2065 parse_fn_statements(base_type);
2066 if (!__path_is_null() &&
2067 cur_func_return_type() == &void_ctype &&
2068 !__bail_on_rest_of_function) {
2069 __pass_to_client(NULL, RETURN_HOOK);
2070 nullify_path();
2072 __pass_to_client(sym, END_FUNC_HOOK);
2073 __call_scope_hooks();
2074 __pass_to_client(sym, AFTER_FUNC_HOOK);
2075 sym->parsed = true;
2077 clear_all_states();
2079 record_func_time();
2081 cur_func_sym = NULL;
2082 cur_func = NULL;
2083 free_data_info_allocs();
2084 free_expression_stack(&switch_expr_stack);
2085 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2086 __bail_on_rest_of_function = 0;
2089 static void save_flow_state(void)
2091 unsigned long *tmp;
2093 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
2094 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
2095 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
2097 __add_ptr_list(&backup, big_statement_stack);
2098 __add_ptr_list(&backup, big_expression_stack);
2099 __add_ptr_list(&backup, big_condition_stack);
2100 __add_ptr_list(&backup, switch_expr_stack);
2102 __add_ptr_list(&backup, cur_func_sym);
2104 __add_ptr_list(&backup, parsed_calls);
2106 __add_ptr_list(&backup, __prev_stmt);
2107 __add_ptr_list(&backup, __cur_stmt);
2108 __add_ptr_list(&backup, __next_stmt);
2110 FOR_EACH_PTR(fn_data_list, tmp) {
2111 __add_ptr_list(&backup, (void *)*tmp);
2112 } END_FOR_EACH_PTR(tmp);
2115 static void *pop_backup(void)
2117 void *ret;
2119 ret = last_ptr_list(backup);
2120 delete_ptr_list_last(&backup);
2121 return ret;
2124 static void restore_flow_state(void)
2126 unsigned long *tmp;
2128 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2129 *tmp = (unsigned long)pop_backup();
2130 } END_FOR_EACH_PTR_REVERSE(tmp);
2132 __next_stmt = pop_backup();
2133 __cur_stmt = pop_backup();
2134 __prev_stmt = pop_backup();
2136 parsed_calls = pop_backup();
2138 cur_func_sym = pop_backup();
2139 switch_expr_stack = pop_backup();
2140 big_condition_stack = pop_backup();
2141 big_expression_stack = pop_backup();
2142 big_statement_stack = pop_backup();
2143 final_pass = PTR_INT(pop_backup()) >> 2;
2144 loop_count = PTR_INT(pop_backup()) >> 2;
2145 loop_num = PTR_INT(pop_backup()) >> 2;
2148 void parse_inline(struct expression *call)
2150 struct symbol *base_type;
2151 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2152 struct timeval time_backup = fn_start_time;
2153 struct expression *orig_inline = __inline_fn;
2154 int orig_budget;
2156 if (out_of_memory() || taking_too_long())
2157 return;
2159 if (already_parsed_call(call))
2160 return;
2162 save_flow_state();
2164 __pass_to_client(call, INLINE_FN_START);
2165 final_pass = 0; /* don't print anything */
2166 __inline_fn = call;
2167 orig_budget = inline_budget;
2168 inline_budget = inline_budget - 5;
2170 base_type = get_base_type(call->fn->symbol);
2171 cur_func_sym = call->fn->symbol;
2172 if (call->fn->symbol->ident)
2173 cur_func = call->fn->symbol->ident->name;
2174 else
2175 cur_func = NULL;
2176 set_position(call->fn->symbol->pos);
2178 save_all_states();
2179 big_statement_stack = NULL;
2180 big_expression_stack = NULL;
2181 big_condition_stack = NULL;
2182 switch_expr_stack = NULL;
2183 parsed_calls = NULL;
2185 sm_debug("inline function: %s\n", cur_func);
2186 __unnullify_path();
2187 clear_function_data();
2188 loop_num = 0;
2189 loop_count = 0;
2190 start_function_definition(call->fn->symbol);
2191 parse_fn_statements(base_type);
2192 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2193 __call_scope_hooks();
2194 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2195 call->fn->symbol->parsed = true;
2197 free_expression_stack(&switch_expr_stack);
2198 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2199 nullify_path();
2200 free_goto_stack();
2202 restore_flow_state();
2203 fn_start_time = time_backup;
2204 cur_func = cur_func_bak;
2206 restore_all_states();
2207 set_position(call->pos);
2208 __inline_fn = orig_inline;
2209 inline_budget = orig_budget;
2210 __pass_to_client(call, INLINE_FN_END);
2213 static struct symbol_list *inlines_called;
2214 static void add_inline_function(struct symbol *sym)
2216 static struct symbol_list *already_added;
2217 struct symbol *tmp;
2219 FOR_EACH_PTR(already_added, tmp) {
2220 if (tmp == sym)
2221 return;
2222 } END_FOR_EACH_PTR(tmp);
2224 add_ptr_list(&already_added, sym);
2225 add_ptr_list(&inlines_called, sym);
2228 static void process_inlines(void)
2230 struct symbol *tmp;
2232 FOR_EACH_PTR(inlines_called, tmp) {
2233 split_function(tmp);
2234 } END_FOR_EACH_PTR(tmp);
2235 free_ptr_list(&inlines_called);
2238 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2240 struct symbol *sym;
2242 FOR_EACH_PTR_REVERSE(big_list, sym) {
2243 if (!sym->scope)
2244 continue;
2245 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2246 return sym;
2247 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2248 return sym;
2249 } END_FOR_EACH_PTR_REVERSE(sym);
2251 return NULL;
2254 static bool interesting_function(struct symbol *sym)
2256 static int prev_stream = -1;
2257 static bool prev_answer;
2258 const char *filename;
2259 int len;
2261 if (!(sym->ctype.modifiers & MOD_INLINE))
2262 return true;
2264 if (sym->pos.stream == prev_stream)
2265 return prev_answer;
2267 prev_stream = sym->pos.stream;
2268 prev_answer = false;
2270 filename = stream_name(sym->pos.stream);
2271 len = strlen(filename);
2272 if (len > 0 && filename[len - 1] == 'c')
2273 prev_answer = true;
2274 return prev_answer;
2277 static void split_inlines_in_scope(struct symbol *sym)
2279 struct symbol *base;
2280 struct symbol_list *scope_list;
2281 int stream;
2283 scope_list = sym->scope->symbols;
2284 stream = sym->pos.stream;
2286 /* find the last static symbol in the file */
2287 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2288 if (sym->pos.stream != stream)
2289 continue;
2290 if (sym->type != SYM_NODE)
2291 continue;
2292 base = get_base_type(sym);
2293 if (!base)
2294 continue;
2295 if (base->type != SYM_FN)
2296 continue;
2297 if (!base->inline_stmt)
2298 continue;
2299 if (!interesting_function(sym))
2300 continue;
2301 add_inline_function(sym);
2302 } END_FOR_EACH_PTR_REVERSE(sym);
2304 process_inlines();
2307 static void split_inlines(struct symbol_list *sym_list)
2309 struct symbol *sym;
2311 sym = get_last_scoped_symbol(sym_list, 0);
2312 if (sym)
2313 split_inlines_in_scope(sym);
2314 sym = get_last_scoped_symbol(sym_list, 1);
2315 if (sym)
2316 split_inlines_in_scope(sym);
2319 static struct stree *clone_estates_perm(struct stree *orig)
2321 struct stree *ret = NULL;
2322 struct sm_state *tmp;
2324 FOR_EACH_SM(orig, tmp) {
2325 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2326 } END_FOR_EACH_SM(tmp);
2328 return ret;
2331 struct position last_pos;
2332 static void split_c_file_functions(struct symbol_list *sym_list)
2334 struct symbol *sym;
2336 __unnullify_path();
2337 FOR_EACH_PTR(sym_list, sym) {
2338 set_position(sym->pos);
2339 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2340 __pass_to_client(sym, BASE_HOOK);
2341 fake_global_assign(sym);
2342 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2344 } END_FOR_EACH_PTR(sym);
2345 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2346 nullify_path();
2348 FOR_EACH_PTR(sym_list, sym) {
2349 set_position(sym->pos);
2350 last_pos = sym->pos;
2351 if (!interesting_function(sym))
2352 continue;
2353 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2354 split_function(sym);
2355 process_inlines();
2357 last_pos = sym->pos;
2358 } END_FOR_EACH_PTR(sym);
2359 split_inlines(sym_list);
2360 __pass_to_client(sym_list, END_FILE_HOOK);
2363 static int final_before_fake;
2364 void init_fake_env(void)
2366 if (!in_fake_env)
2367 final_before_fake = final_pass;
2368 in_fake_env++;
2369 __push_fake_cur_stree();
2370 final_pass = 0;
2373 void end_fake_env(void)
2375 __free_fake_cur_stree();
2376 in_fake_env--;
2377 if (!in_fake_env)
2378 final_pass = final_before_fake;
2381 static void open_output_files(char *base_file)
2383 char buf[256];
2385 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2386 sm_outfd = fopen(buf, "w");
2387 if (!sm_outfd)
2388 sm_fatal("Cannot open %s", buf);
2390 if (!option_info)
2391 return;
2393 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2394 sql_outfd = fopen(buf, "w");
2395 if (!sql_outfd)
2396 sm_fatal("Error: Cannot open %s", buf);
2398 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2399 caller_info_fd = fopen(buf, "w");
2400 if (!caller_info_fd)
2401 sm_fatal("Error: Cannot open %s", buf);
2404 void smatch(struct string_list *filelist)
2406 struct symbol_list *sym_list;
2407 struct timeval stop, start;
2408 char *path;
2409 int len;
2411 gettimeofday(&start, NULL);
2413 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2414 path = getcwd(NULL, 0);
2415 free(full_base_file);
2416 if (path) {
2417 len = strlen(path) + 1 + strlen(base_file) + 1;
2418 full_base_file = malloc(len);
2419 snprintf(full_base_file, len, "%s/%s", path, base_file);
2420 } else {
2421 full_base_file = alloc_string(base_file);
2423 if (option_file_output)
2424 open_output_files(base_file);
2425 base_file_stream = input_stream_nr;
2426 sym_list = sparse_keep_tokens(base_file);
2427 split_c_file_functions(sym_list);
2428 } END_FOR_EACH_PTR_NOTAG(base_file);
2430 gettimeofday(&stop, NULL);
2432 set_position(last_pos);
2433 final_pass = 1;
2434 if (option_time)
2435 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2436 if (option_mem)
2437 sm_msg("mem: %luKb", get_max_memory());