db/kernel.return_fixes: add more guard lock annotations
[smatch.git] / smatch_flow.c
blobede328d63f8f1571f7b990678133422ced6a6fad
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 bool parent_is_dereference(struct expression *expr)
356 struct expression *parent;
358 parent = expr;
359 while ((parent = expr_get_parent_expr(parent))) {
360 if (parent->type == EXPR_DEREF)
361 return true;
362 if (parent->type == EXPR_PREOP &&
363 parent->op == '*')
364 return true;
367 return false;
370 static int prev_expression_is_getting_address(struct expression *expr)
372 struct expression *parent;
374 do {
375 parent = expr_get_parent_expr(expr);
377 if (!parent)
378 return 0;
379 if (parent->type == EXPR_PREOP && parent->op == '&') {
380 if (parent_is_dereference(parent))
381 return false;
382 return true;
384 if (parent->type == EXPR_PREOP && parent->op == '(')
385 goto next;
386 if (parent->type == EXPR_DEREF && parent->op == '.')
387 goto next;
388 /* Handle &foo->array[offset] */
389 if (parent->type == EXPR_BINOP && parent->op == '+') {
390 parent = expr_get_parent_expr(parent);
391 if (!parent)
392 return 0;
393 if (parent->type == EXPR_PREOP && parent->op == '*')
394 goto next;
397 return 0;
398 next:
399 expr = parent;
400 } while (1);
403 int __in_builtin_overflow_func;
404 static void handle_builtin_overflow_func(struct expression *expr)
406 struct expression *a, *b, *res, *assign;
407 int op;
409 if (sym_name_is("__builtin_add_overflow", expr->fn))
410 op = '+';
411 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
412 op = '-';
413 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
414 op = '*';
415 else
416 return;
418 a = get_argument_from_call_expr(expr->args, 0);
419 b = get_argument_from_call_expr(expr->args, 1);
420 res = get_argument_from_call_expr(expr->args, 2);
422 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
424 __in_builtin_overflow_func++;
425 __split_expr(assign);
426 __in_builtin_overflow_func--;
429 static int handle__builtin_choose_expr(struct expression *expr)
431 struct expression *const_expr, *expr1, *expr2;
432 sval_t sval;
434 if (!sym_name_is("__builtin_choose_expr", expr->fn))
435 return 0;
437 const_expr = get_argument_from_call_expr(expr->args, 0);
438 expr1 = get_argument_from_call_expr(expr->args, 1);
439 expr2 = get_argument_from_call_expr(expr->args, 2);
441 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
442 return 0;
443 if (sval.value)
444 __split_expr(expr1);
445 else
446 __split_expr(expr2);
447 return 1;
450 static int handle__builtin_choose_expr_assigns(struct expression *expr)
452 struct expression *const_expr, *right, *expr1, *expr2, *fake;
453 sval_t sval;
456 * We can't use strip_no_cast() because it strips out
457 * __builtin_choose_expr() which turns this function into a no-op.
460 right = strip_parens(expr->right);
461 if (right->type != EXPR_CALL)
462 return 0;
463 if (!sym_name_is("__builtin_choose_expr", right->fn))
464 return 0;
466 const_expr = get_argument_from_call_expr(right->args, 0);
467 expr1 = get_argument_from_call_expr(right->args, 1);
468 expr2 = get_argument_from_call_expr(right->args, 2);
470 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
471 return 0;
473 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
474 __split_expr(fake);
475 return 1;
478 int is_condition_call(struct expression *expr)
480 struct expression *tmp;
482 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
483 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
484 return 1;
485 if (tmp->pos.line < expr->pos.line)
486 return 0;
487 } END_FOR_EACH_PTR_REVERSE(tmp);
489 return 0;
492 static struct expression *expr_get_parent_no_parens(struct expression *expr)
494 do {
495 expr = expr_get_parent_expr(expr);
496 } while (expr &&
497 expr->type == EXPR_PREOP &&
498 expr->op == '(');
500 return expr;
503 static bool gen_fake_function_assign(struct expression *expr)
505 static struct expression *parsed;
506 struct expression *assign, *parent;
507 struct symbol *type;
508 char buf[64];
510 /* The rule is that every non-void function call has to be part of an
511 * assignment. TODO: Should we create a fake non-casted assignment
512 * for casted assignments? Also faked assigns for += assignments?
514 type = get_type(expr);
515 if (!type || type == &void_ctype)
516 return false;
518 parent = expr_get_parent_no_parens(expr);
519 if (parent && parent->type == EXPR_ASSIGNMENT)
520 return false;
522 parent = expr_get_fake_parent_expr(expr);
523 if (parent) {
524 struct expression *left = parent->left;
526 if (parent == parsed)
527 return false;
528 if (!left || left->type != EXPR_SYMBOL)
529 return false;
530 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
531 return false;
532 parsed = parent;
533 __split_expr(parent);
534 return true;
537 // TODO: faked_assign skipping conditions is a hack
538 if (is_condition_call(expr))
539 return false;
541 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
542 assign = create_fake_assign(buf, get_type(expr), expr);
544 parsed = assign;
545 __split_expr(assign);
546 return true;
549 static void split_call(struct expression *expr)
551 if (gen_fake_function_assign(expr))
552 return;
554 expr_set_parent_expr(expr->fn, expr);
556 if (sym_name_is("__builtin_constant_p", expr->fn))
557 return;
558 if (handle__builtin_choose_expr(expr))
559 return;
560 __split_expr(expr->fn);
561 split_args(expr);
562 if (is_inline_func(expr->fn))
563 add_inline_function(expr->fn->symbol->definition);
564 if (inlinable(expr->fn))
565 __inline_call = 1;
566 __process_post_op_stack();
567 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
568 __pass_to_client(expr, FUNCTION_CALL_HOOK);
569 __inline_call = 0;
570 if (inlinable(expr->fn))
571 parse_inline(expr);
572 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
573 if (is_noreturn_func(expr->fn))
574 nullify_path();
575 if (!expr_get_parent_expr(expr) && indent_cnt == 1)
576 __discard_fake_states(expr);
577 handle_builtin_overflow_func(expr);
578 __add_ptr_list((struct ptr_list **)&parsed_calls, expr);
581 static unsigned long skip_split;
582 void parse_assignment(struct expression *expr, bool shallow)
584 struct expression *right;
586 expr_set_parent_expr(expr->left, expr);
587 expr_set_parent_expr(expr->right, expr);
589 right = strip_expr(expr->right);
590 if (!right)
591 return;
593 if (shallow)
594 skip_split++;
596 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
598 /* foo = !bar() */
599 if (__handle_condition_assigns(expr))
600 goto after_assign;
601 /* foo = (x < 5 ? foo : 5); */
602 if (__handle_select_assigns(expr))
603 goto after_assign;
604 /* foo = ({frob(); frob(); frob(); 1;}) */
605 if (__handle_expr_statement_assigns(expr))
606 goto done; // FIXME: goto after
607 /* foo = (3, 4); */
608 if (handle_comma_assigns(expr))
609 goto after_assign;
610 if (handle__builtin_choose_expr_assigns(expr))
611 goto after_assign;
612 if (handle_postop_assigns(expr))
613 goto done; /* no need to goto after_assign */
615 __split_expr(expr->right);
616 if (outside_of_function())
617 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
618 else
619 __pass_to_client(expr, ASSIGNMENT_HOOK);
622 // FIXME: the ordering of this is tricky
623 __fake_struct_member_assignments(expr);
625 /* Re-examine ->right for inlines. See the commit message */
626 right = strip_expr(expr->right);
627 if (expr->op == '=' && right->type == EXPR_CALL)
628 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
630 after_assign:
631 if (get_macro_name(right->pos) &&
632 get_macro_name(expr->left->pos) != get_macro_name(right->pos))
633 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
635 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
636 __split_expr(expr->left);
638 done:
639 if (shallow)
640 skip_split--;
643 static bool skip_split_off(struct expression *expr)
645 if (expr->type == EXPR_CALL &&
646 sym_name_is("__smatch_stop_skip", expr->fn))
647 return true;
648 return false;
651 void __split_expr(struct expression *expr)
653 if (!expr)
654 return;
656 if (skip_split_off(expr))
657 __debug_skip = 0;
658 if (__debug_skip)
659 return;
661 if (skip_split)
662 return;
664 // if (local_debug)
665 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
667 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
668 return;
669 if (__in_fake_assign >= 4) /* don't allow too much nesting */
670 return;
672 push_expression(&big_expression_stack, expr);
673 set_position(expr->pos);
674 __pass_to_client(expr, EXPR_HOOK);
676 switch (expr->type) {
677 case EXPR_PREOP:
678 expr_set_parent_expr(expr->unop, expr);
680 __split_expr(expr->unop);
681 if (expr->op == '*' &&
682 !prev_expression_is_getting_address(expr))
683 __pass_to_client(expr, DEREF_HOOK);
684 __pass_to_client(expr, OP_HOOK);
685 break;
686 case EXPR_POSTOP:
687 expr_set_parent_expr(expr->unop, expr);
689 __split_expr(expr->unop);
690 push_expression(&post_op_stack, expr);
691 break;
692 case EXPR_STATEMENT:
693 __expr_stmt_count++;
694 if (expr->statement && !expr->statement) {
695 stmt_set_parent_stmt(expr->statement,
696 last_ptr_list((struct ptr_list *)big_statement_stack));
698 __split_stmt(expr->statement);
699 __expr_stmt_count--;
700 break;
701 case EXPR_LOGICAL:
702 case EXPR_COMPARE:
703 expr_set_parent_expr(expr->left, expr);
704 expr_set_parent_expr(expr->right, expr);
706 __pass_to_client(expr, LOGIC_HOOK);
707 __handle_logic(expr);
708 break;
709 case EXPR_BINOP:
710 expr_set_parent_expr(expr->left, expr);
711 expr_set_parent_expr(expr->right, expr);
713 __pass_to_client(expr, BINOP_HOOK);
714 __split_expr(expr->left);
715 __split_expr(expr->right);
716 break;
717 case EXPR_COMMA:
718 expr_set_parent_expr(expr->left, expr);
719 expr_set_parent_expr(expr->right, expr);
721 __split_expr(expr->left);
722 __process_post_op_stack();
723 __split_expr(expr->right);
724 break;
725 case EXPR_ASSIGNMENT:
726 parse_assignment(expr, false);
727 break;
728 case EXPR_DEREF:
729 expr_set_parent_expr(expr->deref, expr);
731 __split_expr(expr->deref);
732 __pass_to_client(expr, DEREF_HOOK);
733 break;
734 case EXPR_SLICE:
735 expr_set_parent_expr(expr->base, expr);
737 __split_expr(expr->base);
738 break;
739 case EXPR_CAST:
740 case EXPR_FORCE_CAST:
741 expr_set_parent_expr(expr->cast_expression, expr);
743 __pass_to_client(expr, CAST_HOOK);
744 __split_expr(expr->cast_expression);
745 break;
746 case EXPR_SIZEOF:
747 if (expr->cast_expression)
748 __pass_to_client(strip_parens(expr->cast_expression),
749 SIZEOF_HOOK);
750 break;
751 case EXPR_OFFSETOF:
752 case EXPR_ALIGNOF:
753 break;
754 case EXPR_CONDITIONAL:
755 case EXPR_SELECT:
756 expr_set_parent_expr(expr->conditional, expr);
757 expr_set_parent_expr(expr->cond_true, expr);
758 expr_set_parent_expr(expr->cond_false, expr);
760 if (known_condition_true(expr->conditional)) {
761 __split_expr(expr->cond_true);
762 break;
764 if (known_condition_false(expr->conditional)) {
765 __split_expr(expr->cond_false);
766 break;
768 __pass_to_client(expr, SELECT_HOOK);
769 __split_whole_condition(expr->conditional);
770 __split_expr(expr->cond_true);
771 __push_true_states();
772 __use_false_states();
773 __split_expr(expr->cond_false);
774 __merge_true_states();
775 break;
776 case EXPR_CALL:
777 split_call(expr);
778 break;
779 case EXPR_INITIALIZER:
780 split_expr_list(expr->expr_list, expr);
781 break;
782 case EXPR_IDENTIFIER:
783 expr_set_parent_expr(expr->ident_expression, expr);
784 __split_expr(expr->ident_expression);
785 break;
786 case EXPR_INDEX:
787 expr_set_parent_expr(expr->idx_expression, expr);
788 __split_expr(expr->idx_expression);
789 break;
790 case EXPR_POS:
791 expr_set_parent_expr(expr->init_expr, expr);
792 __split_expr(expr->init_expr);
793 break;
794 case EXPR_SYMBOL:
795 __pass_to_client(expr, SYM_HOOK);
796 break;
797 case EXPR_STRING:
798 __pass_to_client(expr, STRING_HOOK);
799 break;
800 case EXPR_GENERIC: {
801 struct expression *tmp;
803 tmp = strip_Generic(expr);
804 if (tmp != expr)
805 __split_expr(tmp);
806 break;
808 default:
809 break;
811 __pass_to_client(expr, EXPR_HOOK_AFTER);
812 pop_expression(&big_expression_stack);
815 static int is_forever_loop(struct statement *stmt)
817 struct expression *expr;
818 sval_t sval;
820 expr = strip_expr(stmt->iterator_pre_condition);
821 if (!expr)
822 expr = stmt->iterator_post_condition;
823 if (!expr) {
824 /* this is a for(;;) loop... */
825 return 1;
828 if (get_value(expr, &sval) && sval.value != 0)
829 return 1;
831 return 0;
834 static int loop_num;
835 static char *get_loop_name(int num)
837 char buf[256];
839 snprintf(buf, 255, "-loop%d", num);
840 buf[255] = '\0';
841 return alloc_sname(buf);
844 static struct bool_stmt_fn_list *once_through_hooks;
845 void add_once_through_hook(bool_stmt_func *fn)
847 add_ptr_list(&once_through_hooks, fn);
850 static bool call_once_through_hooks(struct statement *stmt)
852 bool_stmt_func *fn;
854 if (option_assume_loops)
855 return true;
857 FOR_EACH_PTR(once_through_hooks, fn) {
858 if ((fn)(stmt))
859 return true;
860 } END_FOR_EACH_PTR(fn);
862 return false;
866 * Pre Loops are while and for loops.
868 static void handle_pre_loop(struct statement *stmt)
870 int once_through; /* we go through the loop at least once */
871 struct sm_state *extra_sm = NULL;
872 int unchanged = 0;
873 char *loop_name;
874 struct stree *stree = NULL;
875 struct sm_state *sm = NULL;
877 __push_scope_hooks();
879 loop_name = get_loop_name(loop_num);
880 loop_num++;
882 split_declaration(stmt->iterator_syms);
883 if (stmt->iterator_pre_statement) {
884 __split_stmt(stmt->iterator_pre_statement);
885 __prev_stmt = stmt->iterator_pre_statement;
888 loop_count++;
889 __push_continues();
890 __push_breaks();
892 __merge_gotos(loop_name, NULL);
894 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
895 __in_pre_condition++;
896 __set_confidence_implied();
897 __split_whole_condition_tf(stmt->iterator_pre_condition, &once_through);
898 __unset_confidence();
899 if (once_through != true)
900 once_through = call_once_through_hooks(stmt);
901 __pass_to_client(stmt, PRELOOP_HOOK);
902 __in_pre_condition--;
903 FOR_EACH_SM(stree, sm) {
904 set_state(sm->owner, sm->name, sm->sym, sm->state);
905 } END_FOR_EACH_SM(sm);
906 free_stree(&stree);
907 if (extra_sm)
908 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
910 __split_stmt(stmt->iterator_statement);
911 if (is_forever_loop(stmt)) {
912 __merge_continues();
913 __save_gotos(loop_name, NULL);
915 __push_fake_cur_stree();
916 __split_stmt(stmt->iterator_post_statement);
917 stree = __pop_fake_cur_stree();
919 __discard_false_states();
920 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
921 __use_breaks();
923 if (!__path_is_null())
924 __merge_stree_into_cur(stree);
925 free_stree(&stree);
926 } else {
927 __merge_continues();
928 unchanged = __iterator_unchanged(extra_sm);
929 __split_stmt(stmt->iterator_post_statement);
930 __prev_stmt = stmt->iterator_post_statement;
931 __cur_stmt = stmt;
933 __save_gotos(loop_name, NULL);
934 __in_pre_condition++;
935 __split_whole_condition(stmt->iterator_pre_condition);
936 __in_pre_condition--;
937 nullify_path();
938 __merge_false_states();
939 if (once_through == true)
940 __discard_false_states();
941 else
942 __merge_false_states();
944 if (extra_sm && unchanged)
945 __extra_pre_loop_hook_after(extra_sm,
946 stmt->iterator_post_statement,
947 stmt->iterator_pre_condition);
948 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
949 __merge_breaks();
951 loop_count--;
953 __call_scope_hooks();
957 * Post loops are do {} while();
959 static void handle_post_loop(struct statement *stmt)
961 char *loop_name;
963 loop_name = get_loop_name(loop_num);
964 loop_num++;
965 loop_count++;
967 __pass_to_client(stmt, POSTLOOP_HOOK);
969 __push_continues();
970 __push_breaks();
971 __merge_gotos(loop_name, NULL);
972 __split_stmt(stmt->iterator_statement);
973 __merge_continues();
974 if (!expr_is_zero(stmt->iterator_post_condition))
975 __save_gotos(loop_name, NULL);
977 if (is_forever_loop(stmt)) {
978 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
979 __use_breaks();
980 } else {
981 __split_whole_condition(stmt->iterator_post_condition);
982 __use_false_states();
983 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
984 __merge_breaks();
986 loop_count--;
989 static int empty_statement(struct statement *stmt)
991 if (!stmt)
992 return 0;
993 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
994 return 1;
995 return 0;
998 static int last_stmt_on_same_line(void)
1000 struct statement *stmt;
1001 int i = 0;
1003 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
1004 if (!i++)
1005 continue;
1006 if (stmt->pos.line == get_lineno())
1007 return 1;
1008 return 0;
1009 } END_FOR_EACH_PTR_REVERSE(stmt);
1010 return 0;
1013 static void split_asm_ops(struct asm_operand_list *ops)
1015 struct asm_operand *op;
1017 FOR_EACH_PTR(ops, op) {
1018 __split_expr(op->expr);
1019 } END_FOR_EACH_PTR(op);
1022 static int is_case_val(struct statement *stmt, sval_t sval)
1024 sval_t case_sval;
1026 if (stmt->type != STMT_CASE)
1027 return 0;
1028 if (!stmt->case_expression) {
1029 __set_default();
1030 return 1;
1032 if (!get_value(stmt->case_expression, &case_sval))
1033 return 0;
1034 if (case_sval.value == sval.value)
1035 return 1;
1036 return 0;
1039 static struct range_list *get_case_rl(struct expression *switch_expr,
1040 struct expression *case_expr,
1041 struct expression *case_to)
1043 sval_t start, end;
1044 struct range_list *rl = NULL;
1045 struct symbol *switch_type;
1047 switch_type = get_type(switch_expr);
1048 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
1049 start = sval_cast(switch_type, start);
1050 end = sval_cast(switch_type, end);
1051 add_range(&rl, start, end);
1052 } else if (get_value(case_expr, &start)) {
1053 start = sval_cast(switch_type, start);
1054 add_range(&rl, start, start);
1057 return rl;
1060 static void split_known_switch(struct statement *stmt, sval_t sval)
1062 struct statement *tmp;
1063 struct range_list *rl;
1065 __split_expr(stmt->switch_expression);
1066 sval = sval_cast(get_type(stmt->switch_expression), sval);
1068 push_expression(&switch_expr_stack, stmt->switch_expression);
1069 __save_switch_states(top_expression(switch_expr_stack));
1070 nullify_path();
1071 __push_default();
1072 __push_breaks();
1074 stmt = stmt->switch_statement;
1076 __push_scope_hooks();
1077 FOR_EACH_PTR(stmt->stmts, tmp) {
1078 __smatch_lineno = tmp->pos.line;
1079 // FIXME: what if default comes before the known case statement?
1080 if (is_case_val(tmp, sval)) {
1081 rl = alloc_rl(sval, sval);
1082 __merge_switches(top_expression(switch_expr_stack), rl);
1083 __pass_case_to_client(top_expression(switch_expr_stack), rl);
1084 stmt_set_parent_stmt(tmp->case_statement, tmp);
1085 __split_stmt(tmp->case_statement);
1086 goto next;
1088 if (__path_is_null())
1089 continue;
1090 __split_stmt(tmp);
1091 next:
1092 if (__path_is_null()) {
1093 __set_default();
1094 goto out;
1096 } END_FOR_EACH_PTR(tmp);
1097 out:
1098 __call_scope_hooks();
1099 if (!__pop_default())
1100 __merge_switches(top_expression(switch_expr_stack), NULL);
1101 __discard_switches();
1102 __merge_breaks();
1103 pop_expression(&switch_expr_stack);
1106 static void split_case(struct statement *stmt)
1108 struct range_list *rl = NULL;
1110 expr_set_parent_stmt(stmt->case_expression, stmt);
1111 expr_set_parent_stmt(stmt->case_to, stmt);
1113 rl = get_case_rl(top_expression(switch_expr_stack),
1114 stmt->case_expression, stmt->case_to);
1115 while (stmt->case_statement->type == STMT_CASE) {
1116 struct range_list *tmp;
1118 tmp = get_case_rl(top_expression(switch_expr_stack),
1119 stmt->case_statement->case_expression,
1120 stmt->case_statement->case_to);
1121 if (!tmp)
1122 goto next;
1123 rl = rl_union(rl, tmp);
1124 if (!stmt->case_expression)
1125 __set_default();
1126 next:
1127 stmt = stmt->case_statement;
1130 __merge_switches(top_expression(switch_expr_stack), rl);
1132 if (!stmt->case_expression)
1133 __set_default();
1135 stmt_set_parent_stmt(stmt->case_statement, stmt);
1136 __split_stmt(stmt->case_statement);
1139 int time_parsing_function(void)
1141 return ms_since(&fn_start_time) / 1000;
1144 bool taking_too_long(void)
1146 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1147 return 1;
1148 return 0;
1151 struct statement *get_last_stmt(void)
1153 struct symbol *fn;
1154 struct statement *stmt;
1156 fn = get_base_type(cur_func_sym);
1157 if (!fn)
1158 return NULL;
1159 stmt = fn->stmt;
1160 if (!stmt)
1161 stmt = fn->inline_stmt;
1162 if (!stmt || stmt->type != STMT_COMPOUND)
1163 return NULL;
1164 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1165 if (stmt && stmt->type == STMT_LABEL)
1166 stmt = stmt->label_statement;
1167 return stmt;
1170 int is_last_stmt(struct statement *cur_stmt)
1172 struct statement *last;
1174 last = get_last_stmt();
1175 if (last && last == cur_stmt)
1176 return 1;
1177 return 0;
1180 static bool is_function_scope(struct statement *stmt)
1182 struct symbol *base_type;
1184 if (!cur_func_sym)
1185 return false;
1187 base_type = get_base_type(cur_func_sym);
1188 if (base_type->stmt == stmt ||
1189 base_type->inline_stmt == stmt)
1190 return true;
1192 return false;
1195 static void handle_backward_goto(struct statement *goto_stmt)
1197 const char *goto_name, *label_name;
1198 struct statement *func_stmt;
1199 struct symbol *base_type = get_base_type(cur_func_sym);
1200 struct statement *tmp;
1201 int found = 0;
1203 if (!option_info)
1204 return;
1205 if (last_goto_statement_handled)
1206 return;
1207 last_goto_statement_handled = 1;
1209 if (!goto_stmt->goto_label ||
1210 goto_stmt->goto_label->type != SYM_LABEL ||
1211 !goto_stmt->goto_label->ident)
1212 return;
1213 goto_name = goto_stmt->goto_label->ident->name;
1215 func_stmt = base_type->stmt;
1216 if (!func_stmt)
1217 func_stmt = base_type->inline_stmt;
1218 if (!func_stmt)
1219 return;
1220 if (func_stmt->type != STMT_COMPOUND)
1221 return;
1223 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1224 if (!found) {
1225 if (tmp->type != STMT_LABEL)
1226 continue;
1227 if (!tmp->label_identifier ||
1228 tmp->label_identifier->type != SYM_LABEL ||
1229 !tmp->label_identifier->ident)
1230 continue;
1231 label_name = tmp->label_identifier->ident->name;
1232 if (strcmp(goto_name, label_name) != 0)
1233 continue;
1234 found = 1;
1236 __split_stmt(tmp);
1237 } END_FOR_EACH_PTR(tmp);
1240 static void fake_a_return(void)
1242 struct expression *ret = NULL;
1244 nullify_path();
1245 __unnullify_path();
1247 if (cur_func_return_type() != &void_ctype)
1248 ret = unknown_value_expression(NULL);
1250 __pass_to_client(ret, RETURN_HOOK);
1251 nullify_path();
1254 static void split_ret_value(struct expression *expr)
1256 struct symbol *type;
1258 if (!expr)
1259 return;
1261 type = get_real_base_type(cur_func_sym);
1262 type = get_real_base_type(type);
1263 expr = fake_a_variable_assign(type, NULL, expr, -1);
1265 __in_fake_var_assign++;
1266 __split_expr(expr);
1267 __in_fake_var_assign--;
1270 static void fake_an_empty_default(struct position pos)
1272 static struct statement none = {};
1274 none.pos = pos;
1275 none.type = STMT_NONE;
1276 __merge_switches(top_expression(switch_expr_stack), NULL);
1277 __split_stmt(&none);
1280 static void split_compound(struct statement *stmt)
1282 struct statement *prev = NULL;
1283 struct statement *cur = NULL;
1284 struct statement *next;
1286 __push_scope_hooks();
1288 FOR_EACH_PTR(stmt->stmts, next) {
1289 /* just set them all ahead of time */
1290 stmt_set_parent_stmt(next, stmt);
1292 if (cur) {
1293 __prev_stmt = prev;
1294 __next_stmt = next;
1295 __cur_stmt = cur;
1296 __split_stmt(cur);
1298 prev = cur;
1299 cur = next;
1300 } END_FOR_EACH_PTR(next);
1301 if (cur) {
1302 __prev_stmt = prev;
1303 __cur_stmt = cur;
1304 __next_stmt = NULL;
1305 __split_stmt(cur);
1309 * For function scope, then delay calling the scope hooks until the
1310 * end of function hooks can run.
1312 if (!is_function_scope(stmt))
1313 __call_scope_hooks();
1316 void __split_label_stmt(struct statement *stmt)
1318 if (stmt->label_identifier &&
1319 stmt->label_identifier->type == SYM_LABEL &&
1320 stmt->label_identifier->ident) {
1321 loop_count |= 0x0800000;
1322 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1326 static void find_asm_gotos(struct statement *stmt)
1328 struct symbol *sym;
1330 FOR_EACH_PTR(stmt->asm_labels, sym) {
1331 __save_gotos(sym->ident->name, sym);
1332 } END_FOR_EACH_PTR(sym);
1335 static void split_if_statement(struct statement *stmt)
1337 int known_tf;
1339 stmt_set_parent_stmt(stmt->if_true, stmt);
1340 stmt_set_parent_stmt(stmt->if_false, stmt);
1341 expr_set_parent_stmt(stmt->if_conditional, stmt);
1343 if (empty_statement(stmt->if_true) &&
1344 last_stmt_on_same_line() &&
1345 !get_macro_name(stmt->if_true->pos))
1346 sm_warning("if();");
1348 __split_whole_condition_tf(stmt->if_conditional, &known_tf);
1349 if (known_tf == true) {
1350 __split_stmt(stmt->if_true);
1351 __discard_false_states();
1352 return;
1353 } else if (known_tf == false) {
1354 __use_false_states();
1355 __split_stmt(stmt->if_false);
1356 return;
1359 __split_stmt(stmt->if_true);
1360 __push_true_states();
1361 __use_false_states();
1362 __split_stmt(stmt->if_false);
1363 __merge_true_states();
1366 static bool already_parsed_call(struct expression *call)
1368 struct expression *expr;
1370 FOR_EACH_PTR(parsed_calls, expr) {
1371 if (expr == call)
1372 return true;
1373 } END_FOR_EACH_PTR(expr);
1374 return false;
1377 static void free_parsed_call_stuff(bool free_fake_states)
1379 free_expression_stack(&parsed_calls);
1380 if (free_fake_states)
1381 __discard_fake_states(NULL);
1384 void __split_stmt(struct statement *stmt)
1386 sval_t sval;
1387 struct timeval start, stop;
1388 bool skip_after = false;
1390 gettimeofday(&start, NULL);
1392 if (!stmt)
1393 goto out;
1395 if (!__in_fake_assign)
1396 __silence_warnings_for_stmt = false;
1398 if (__bail_on_rest_of_function || is_skipped_function())
1399 return;
1401 if (out_of_memory() || taking_too_long()) {
1402 gettimeofday(&start, NULL);
1404 __bail_on_rest_of_function = 1;
1405 final_pass = 1;
1406 sm_perror("Function too hairy. Giving up. %lu seconds",
1407 start.tv_sec - fn_start_time.tv_sec);
1408 fake_a_return();
1409 final_pass = 0; /* turn off sm_msg() from here */
1410 return;
1413 indent_cnt++;
1415 add_ptr_list(&big_statement_stack, stmt);
1416 free_expression_stack(&big_expression_stack);
1417 free_parsed_call_stuff(indent_cnt == 1);
1418 set_position(stmt->pos);
1419 __pass_to_client(stmt, STMT_HOOK);
1421 switch (stmt->type) {
1422 case STMT_DECLARATION:
1423 split_declaration(stmt->declaration);
1424 break;
1425 case STMT_RETURN:
1426 expr_set_parent_stmt(stmt->ret_value, stmt);
1428 split_ret_value(stmt->ret_value);
1429 __process_post_op_stack();
1430 __call_all_scope_hooks();
1431 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1432 nullify_path();
1433 break;
1434 case STMT_EXPRESSION:
1435 expr_set_parent_stmt(stmt->expression, stmt);
1436 expr_set_parent_stmt(stmt->context, stmt);
1438 __split_expr(stmt->expression);
1439 break;
1440 case STMT_COMPOUND:
1441 split_compound(stmt);
1442 break;
1443 case STMT_IF:
1444 split_if_statement(stmt);
1445 break;
1446 case STMT_ITERATOR:
1447 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1448 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1449 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1450 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1451 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1453 if (stmt->iterator_pre_condition)
1454 handle_pre_loop(stmt);
1455 else if (stmt->iterator_post_condition)
1456 handle_post_loop(stmt);
1457 else {
1458 // these are for(;;) type loops.
1459 handle_pre_loop(stmt);
1461 break;
1462 case STMT_SWITCH:
1463 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1464 expr_set_parent_stmt(stmt->switch_expression, stmt);
1466 if (get_value(stmt->switch_expression, &sval)) {
1467 split_known_switch(stmt, sval);
1468 break;
1470 __split_expr(stmt->switch_expression);
1471 push_expression(&switch_expr_stack, stmt->switch_expression);
1472 __save_switch_states(top_expression(switch_expr_stack));
1473 nullify_path();
1474 __push_default();
1475 __push_breaks();
1476 __split_stmt(stmt->switch_statement);
1477 if (!__pop_default() && have_remaining_cases())
1478 fake_an_empty_default(stmt->pos);
1479 __discard_switches();
1480 __merge_breaks();
1481 pop_expression(&switch_expr_stack);
1482 break;
1483 case STMT_CASE:
1484 split_case(stmt);
1485 break;
1486 case STMT_LABEL:
1487 __split_label_stmt(stmt);
1488 __pass_to_client(stmt, STMT_HOOK_AFTER);
1489 skip_after = true;
1490 __split_stmt(stmt->label_statement);
1491 break;
1492 case STMT_GOTO:
1493 expr_set_parent_stmt(stmt->goto_expression, stmt);
1495 __split_expr(stmt->goto_expression);
1496 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1497 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1498 __process_breaks();
1499 } else if (!strcmp(stmt->goto_label->ident->name,
1500 "continue")) {
1501 __process_continues();
1503 } else if (stmt->goto_label &&
1504 stmt->goto_label->type == SYM_LABEL &&
1505 stmt->goto_label->ident) {
1506 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1508 nullify_path();
1509 if (is_last_stmt(stmt))
1510 handle_backward_goto(stmt);
1511 break;
1512 case STMT_NONE:
1513 break;
1514 case STMT_ASM:
1515 expr_set_parent_stmt(stmt->asm_string, stmt);
1517 find_asm_gotos(stmt);
1518 __pass_to_client(stmt, ASM_HOOK);
1519 __split_expr(stmt->asm_string);
1520 split_asm_ops(stmt->asm_outputs);
1521 split_asm_ops(stmt->asm_inputs);
1522 split_expr_list(stmt->asm_clobbers, NULL);
1523 break;
1524 case STMT_CONTEXT:
1525 break;
1526 case STMT_RANGE:
1527 __split_expr(stmt->range_expression);
1528 __split_expr(stmt->range_low);
1529 __split_expr(stmt->range_high);
1530 break;
1532 if (!skip_after)
1533 __pass_to_client(stmt, STMT_HOOK_AFTER);
1534 if (--indent_cnt == 1)
1535 free_parsed_call_stuff(true);
1537 out:
1538 __process_post_op_stack();
1540 gettimeofday(&stop, NULL);
1541 if (option_time_stmt && stmt)
1542 sm_msg("stmt_time%s: %ld",
1543 stmt->type == STMT_COMPOUND ? "_block" : "",
1544 stop.tv_sec - start.tv_sec);
1547 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1549 struct expression *expr;
1551 FOR_EACH_PTR(expr_list, expr) {
1552 expr_set_parent_expr(expr, parent);
1553 __split_expr(expr);
1554 __process_post_op_stack();
1555 } END_FOR_EACH_PTR(expr);
1558 static bool cast_arg(struct symbol *type, struct expression *arg)
1560 struct symbol *orig;
1562 if (!type)
1563 return false;
1565 arg = strip_parens(arg);
1566 if (arg != strip_expr(arg))
1567 return true;
1569 orig = get_type(arg);
1570 if (!orig)
1571 return true;
1572 if (types_equiv(orig, type))
1573 return false;
1575 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1576 return true;
1579 * I would have expected that we could just do use (orig == type) but I
1580 * guess for pointers we need to get the basetype to do that comparison.
1584 if (orig->type != SYM_PTR ||
1585 type->type != SYM_PTR) {
1586 if (type_fits(type, orig))
1587 return false;
1588 return true;
1590 orig = get_real_base_type(orig);
1591 type = get_real_base_type(type);
1592 if (orig == type)
1593 return false;
1595 return true;
1598 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1600 char buf[64];
1601 bool cast;
1603 if (!expr || !cur_func_sym)
1604 return NULL;
1606 if (already_parsed_call(call))
1607 return NULL;
1609 if (expr->type == EXPR_ASSIGNMENT)
1610 return expr;
1612 /* for va_args then we don't know the type */
1613 if (!type)
1614 type = get_type(expr);
1616 cast = cast_arg(type, expr);
1618 * Using expr_to_sym() here is a hack. We want to say that we don't
1619 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1620 * It turns out faking these assignments is way more expensive than I
1621 * would have imagined. I'm not sure why exactly.
1624 if (!cast) {
1626 * if the code is "return *p;" where "p" is a user pointer then
1627 * we want to create a fake assignment so that it sets the state
1628 * in check_kernel_user_data.c.
1631 if (expr->type != EXPR_PREOP &&
1632 expr->op != '*' && expr->op != '&' &&
1633 expr_to_sym(expr))
1634 return expr;
1637 if (nr == -1)
1638 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1639 else
1640 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1642 return create_fake_assign(buf, type, expr);
1645 static void split_args(struct expression *expr)
1647 struct expression *arg, *tmp;
1648 struct symbol *type;
1649 int i;
1651 i = -1;
1652 FOR_EACH_PTR(expr->args, arg) {
1653 i++;
1654 expr_set_parent_expr(arg, expr);
1655 type = get_arg_type(expr->fn, i);
1656 tmp = fake_a_variable_assign(type, expr, arg, i);
1657 if (tmp != arg)
1658 __in_fake_var_assign++;
1659 __split_expr(tmp);
1660 if (tmp != arg)
1661 __in_fake_var_assign--;
1662 __process_post_op_stack();
1663 } END_FOR_EACH_PTR(arg);
1666 static void call_cleanup_fn(void *_sym)
1668 struct symbol *sym = _sym;
1669 struct expression *call, *arg;
1670 struct expression_list *args = NULL;
1672 arg = symbol_expression(sym);
1673 arg = preop_expression(arg, '&');
1674 add_ptr_list(&args, arg);
1675 call = call_expression(sym->cleanup, args);
1677 __split_expr(call);
1680 static void add_cleanup_hook(struct symbol *sym)
1682 if (!sym->cleanup)
1683 return;
1684 add_scope_hook(&call_cleanup_fn, sym);
1687 static void split_sym(struct symbol *sym)
1689 if (!sym)
1690 return;
1691 if (!(sym->namespace & NS_SYMBOL))
1692 return;
1694 __split_stmt(sym->stmt);
1695 __split_expr(sym->array_size);
1696 if (sym->cleanup)
1697 add_cleanup_hook(sym);
1698 split_symlist(sym->arguments);
1699 split_symlist(sym->symbol_list);
1700 __split_stmt(sym->inline_stmt);
1701 split_symlist(sym->inline_symbol_list);
1704 static void split_symlist(struct symbol_list *sym_list)
1706 struct symbol *sym;
1708 FOR_EACH_PTR(sym_list, sym) {
1709 split_sym(sym);
1710 } END_FOR_EACH_PTR(sym);
1713 typedef void (fake_cb)(struct expression *expr);
1715 static int member_to_number(struct expression *expr, struct ident *member)
1717 struct symbol *type, *tmp;
1718 char *name;
1719 int i;
1721 if (!member)
1722 return -1;
1723 name = member->name;
1725 type = get_type(expr);
1726 if (!type || type->type != SYM_STRUCT)
1727 return -1;
1729 i = -1;
1730 FOR_EACH_PTR(type->symbol_list, tmp) {
1731 i++;
1732 if (!tmp->ident)
1733 continue;
1734 if (strcmp(name, tmp->ident->name) == 0)
1735 return i;
1736 } END_FOR_EACH_PTR(tmp);
1737 return -1;
1740 static struct ident *number_to_member(struct expression *expr, int num)
1742 struct symbol *type, *member;
1743 int i = 0;
1745 type = get_type(expr);
1746 if (!type || type->type != SYM_STRUCT)
1747 return NULL;
1749 FOR_EACH_PTR(type->symbol_list, member) {
1750 if (i == num)
1751 return member->ident;
1752 i++;
1753 } END_FOR_EACH_PTR(member);
1754 return NULL;
1757 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1759 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1761 struct expression *edge_member, *assign;
1762 struct symbol *base = get_real_base_type(member);
1763 struct symbol *tmp;
1765 if (member->ident)
1766 expr = member_expression(expr, '.', member->ident);
1768 FOR_EACH_PTR(base->symbol_list, tmp) {
1769 struct symbol *type;
1771 type = get_real_base_type(tmp);
1772 if (!type)
1773 continue;
1775 edge_member = member_expression(expr, '.', tmp->ident);
1776 if (get_extra_state(edge_member))
1777 continue;
1779 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1780 set_inner_struct_members(expr, tmp);
1781 continue;
1784 if (!tmp->ident)
1785 continue;
1787 assign = assign_expression(edge_member, '=', zero_expr());
1788 __split_expr(assign);
1789 } END_FOR_EACH_PTR(tmp);
1794 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1796 struct symbol *tmp;
1797 struct expression *member = NULL;
1798 struct expression *assign;
1800 FOR_EACH_PTR(type->symbol_list, tmp) {
1801 type = get_real_base_type(tmp);
1802 if (!type)
1803 continue;
1805 if (tmp->ident) {
1806 member = member_expression(expr, '.', tmp->ident);
1807 if (get_extra_state(member))
1808 continue;
1811 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1812 set_inner_struct_members(expr, tmp);
1813 continue;
1815 if (type->type == SYM_ARRAY)
1816 continue;
1817 if (!tmp->ident)
1818 continue;
1820 assign = assign_expression(member, '=', zero_expr());
1821 __split_expr(assign);
1822 } END_FOR_EACH_PTR(tmp);
1825 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1827 struct expression *deref, *assign, *tmp, *right;
1828 struct symbol *struct_type, *type;
1829 struct ident *member;
1830 int member_idx;
1832 struct_type = get_type(symbol);
1833 if (!struct_type ||
1834 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1835 return;
1838 * We're parsing an initializer that could look something like this:
1839 * struct foo foo = {
1840 * 42,
1841 * .whatever.xxx = 11,
1842 * .zzz = 12,
1843 * };
1845 * So what we have here is a list with 42, .whatever, and .zzz. We need
1846 * to break it up into left and right sides of the assignments.
1849 member_idx = 0;
1850 FOR_EACH_PTR(members, tmp) {
1851 deref = NULL;
1852 if (tmp->type == EXPR_IDENTIFIER) {
1853 member_idx = member_to_number(symbol, tmp->expr_ident);
1854 while (tmp->type == EXPR_IDENTIFIER) {
1855 member = tmp->expr_ident;
1856 tmp = tmp->ident_expression;
1857 if (deref)
1858 deref = member_expression(deref, '.', member);
1859 else
1860 deref = member_expression(symbol, '.', member);
1862 } else {
1863 member = number_to_member(symbol, member_idx);
1864 deref = member_expression(symbol, '.', member);
1866 right = tmp;
1867 member_idx++;
1868 if (right->type == EXPR_INITIALIZER) {
1869 type = get_type(deref);
1870 if (type && type->type == SYM_ARRAY)
1871 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1872 else
1873 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1874 } else {
1875 assign = assign_expression(deref, '=', right);
1876 fake_cb(assign);
1878 } END_FOR_EACH_PTR(tmp);
1880 set_unset_to_zero(struct_type, symbol);
1883 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1885 fake_member_assigns_helper(symbol_expression(sym),
1886 sym->initializer->expr_list, fake_cb);
1889 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1891 struct expression *offset, *binop, *assign, *tmp;
1892 struct symbol *type;
1893 int idx, max;
1895 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1896 return;
1898 max = 0;
1899 idx = 0;
1900 FOR_EACH_PTR(expr_list, tmp) {
1901 if (tmp->type == EXPR_INDEX) {
1902 if (tmp->idx_from != tmp->idx_to)
1903 return;
1904 idx = tmp->idx_from;
1905 if (idx > max)
1906 max = idx;
1907 if (!tmp->idx_expression)
1908 goto next;
1909 tmp = tmp->idx_expression;
1911 offset = value_expr(idx);
1912 binop = array_element_expression(array, offset);
1913 if (tmp->type == EXPR_INITIALIZER) {
1914 type = get_type(binop);
1915 if (type && type->type == SYM_ARRAY)
1916 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1917 else
1918 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1919 } else {
1920 assign = assign_expression(binop, '=', tmp);
1921 fake_cb(assign);
1923 next:
1924 idx++;
1925 if (idx > max)
1926 max = idx;
1927 } END_FOR_EACH_PTR(tmp);
1929 __call_array_initialized_hooks(array, max);
1932 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1934 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1937 static void fake_assign_expr(struct symbol *sym)
1939 struct expression *assign, *symbol;
1941 symbol = symbol_expression(sym);
1942 assign = assign_expression(symbol, '=', sym->initializer);
1943 __split_expr(assign);
1946 static void do_initializer_stuff(struct symbol *sym)
1948 if (!sym->initializer)
1949 return;
1951 if (sym->initializer->type == EXPR_INITIALIZER) {
1952 if (get_real_base_type(sym)->type == SYM_ARRAY)
1953 fake_element_assigns(sym, __split_expr);
1954 else
1955 fake_member_assigns(sym, __split_expr);
1956 } else {
1957 fake_assign_expr(sym);
1961 static void split_declaration(struct symbol_list *sym_list)
1963 struct symbol *sym;
1965 FOR_EACH_PTR(sym_list, sym) {
1966 __pass_to_client(sym, DECLARATION_HOOK);
1967 do_initializer_stuff(sym);
1968 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1969 split_sym(sym);
1970 } END_FOR_EACH_PTR(sym);
1973 static void call_global_assign_hooks(struct expression *assign)
1975 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1978 static void fake_global_assign(struct symbol *sym)
1980 struct expression *assign, *symbol;
1982 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1983 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1984 fake_element_assigns(sym, call_global_assign_hooks);
1985 } else if (sym->initializer) {
1986 symbol = symbol_expression(sym);
1987 assign = assign_expression(symbol, '=', sym->initializer);
1988 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1989 } else {
1990 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1992 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1993 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1994 fake_member_assigns(sym, call_global_assign_hooks);
1995 } else if (sym->initializer) {
1996 symbol = symbol_expression(sym);
1997 assign = assign_expression(symbol, '=', sym->initializer);
1998 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1999 } else {
2000 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
2002 } else {
2003 symbol = symbol_expression(sym);
2004 if (sym->initializer) {
2005 assign = assign_expression(symbol, '=', sym->initializer);
2006 __split_expr(assign);
2007 } else {
2008 assign = assign_expression(symbol, '=', zero_expr());
2010 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
2014 static void start_function_definition(struct symbol *sym)
2016 __in_function_def = 1;
2017 __pass_to_client(sym, FUNC_DEF_HOOK);
2018 __in_function_def = 0;
2019 __pass_to_client(sym, AFTER_DEF_HOOK);
2023 static void parse_fn_statements(struct symbol *base)
2025 __split_stmt(base->stmt);
2026 __split_stmt(base->inline_stmt);
2029 void add_function_data(unsigned long *fn_data)
2031 __add_ptr_list(&fn_data_list, fn_data);
2034 static void clear_function_data(void)
2036 unsigned long *tmp;
2038 FOR_EACH_PTR(fn_data_list, tmp) {
2039 *tmp = 0;
2040 } END_FOR_EACH_PTR(tmp);
2043 static void record_func_time(void)
2045 struct timeval stop;
2046 int func_time;
2047 char buf[32];
2049 gettimeofday(&stop, NULL);
2050 func_time = stop.tv_sec - fn_start_time.tv_sec;
2051 snprintf(buf, sizeof(buf), "%d", func_time);
2052 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
2053 if (option_time && func_time > 2) {
2054 final_pass++;
2055 sm_msg("func_time: %d", func_time);
2056 final_pass--;
2060 static void split_function(struct symbol *sym)
2062 struct symbol *base_type = get_base_type(sym);
2064 if (!base_type->stmt && !base_type->inline_stmt)
2065 return;
2067 gettimeofday(&outer_fn_start_time, NULL);
2068 gettimeofday(&fn_start_time, NULL);
2069 cur_func_sym = sym;
2070 if (sym->ident)
2071 cur_func = sym->ident->name;
2072 if (option_process_function && cur_func &&
2073 strcmp(option_process_function, cur_func) != 0)
2074 return;
2075 set_position(sym->pos);
2076 clear_function_data();
2077 loop_count = 0;
2078 last_goto_statement_handled = 0;
2079 sm_debug("new function: %s\n", cur_func);
2080 __stree_id = 0;
2081 if (option_two_passes) {
2082 __unnullify_path();
2083 loop_num = 0;
2084 final_pass = 0;
2085 start_function_definition(sym);
2086 parse_fn_statements(base_type);
2087 __call_scope_hooks();
2088 nullify_path();
2090 __unnullify_path();
2091 loop_num = 0;
2092 final_pass = 1;
2093 start_function_definition(sym);
2094 parse_fn_statements(base_type);
2095 if (!__path_is_null() &&
2096 cur_func_return_type() == &void_ctype &&
2097 !__bail_on_rest_of_function) {
2098 __call_all_scope_hooks();
2099 __pass_to_client(NULL, RETURN_HOOK);
2100 nullify_path();
2102 __pass_to_client(sym, END_FUNC_HOOK);
2103 __free_scope_hooks();
2104 __pass_to_client(sym, AFTER_FUNC_HOOK);
2105 sym->parsed = true;
2107 clear_all_states();
2109 record_func_time();
2111 cur_func_sym = NULL;
2112 cur_func = NULL;
2113 free_data_info_allocs();
2114 free_expression_stack(&switch_expr_stack);
2115 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2116 __bail_on_rest_of_function = 0;
2119 static void save_flow_state(void)
2121 unsigned long *tmp;
2123 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
2124 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
2125 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
2127 __add_ptr_list(&backup, big_statement_stack);
2128 __add_ptr_list(&backup, big_expression_stack);
2129 __add_ptr_list(&backup, big_condition_stack);
2130 __add_ptr_list(&backup, switch_expr_stack);
2132 __add_ptr_list(&backup, cur_func_sym);
2134 __add_ptr_list(&backup, parsed_calls);
2136 __add_ptr_list(&backup, __prev_stmt);
2137 __add_ptr_list(&backup, __cur_stmt);
2138 __add_ptr_list(&backup, __next_stmt);
2140 FOR_EACH_PTR(fn_data_list, tmp) {
2141 __add_ptr_list(&backup, (void *)*tmp);
2142 } END_FOR_EACH_PTR(tmp);
2145 static void *pop_backup(void)
2147 void *ret;
2149 ret = last_ptr_list(backup);
2150 delete_ptr_list_last(&backup);
2151 return ret;
2154 static void restore_flow_state(void)
2156 unsigned long *tmp;
2158 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2159 *tmp = (unsigned long)pop_backup();
2160 } END_FOR_EACH_PTR_REVERSE(tmp);
2162 __next_stmt = pop_backup();
2163 __cur_stmt = pop_backup();
2164 __prev_stmt = pop_backup();
2166 parsed_calls = pop_backup();
2168 cur_func_sym = pop_backup();
2169 switch_expr_stack = pop_backup();
2170 big_condition_stack = pop_backup();
2171 big_expression_stack = pop_backup();
2172 big_statement_stack = pop_backup();
2173 final_pass = PTR_INT(pop_backup()) >> 2;
2174 loop_count = PTR_INT(pop_backup()) >> 2;
2175 loop_num = PTR_INT(pop_backup()) >> 2;
2178 void parse_inline(struct expression *call)
2180 struct symbol *base_type;
2181 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2182 struct timeval time_backup = fn_start_time;
2183 struct expression *orig_inline = __inline_fn;
2184 int orig_budget;
2186 if (out_of_memory() || taking_too_long())
2187 return;
2189 if (already_parsed_call(call))
2190 return;
2192 save_flow_state();
2194 __pass_to_client(call, INLINE_FN_START);
2195 final_pass = 0; /* don't print anything */
2196 __inline_fn = call;
2197 orig_budget = inline_budget;
2198 inline_budget = inline_budget - 5;
2200 base_type = get_base_type(call->fn->symbol);
2201 cur_func_sym = call->fn->symbol;
2202 if (call->fn->symbol->ident)
2203 cur_func = call->fn->symbol->ident->name;
2204 else
2205 cur_func = NULL;
2206 set_position(call->fn->symbol->pos);
2208 save_all_states();
2209 big_statement_stack = NULL;
2210 big_expression_stack = NULL;
2211 big_condition_stack = NULL;
2212 switch_expr_stack = NULL;
2213 parsed_calls = NULL;
2215 sm_debug("inline function: %s\n", cur_func);
2216 __unnullify_path();
2217 clear_function_data();
2218 loop_num = 0;
2219 loop_count = 0;
2220 start_function_definition(call->fn->symbol);
2221 parse_fn_statements(base_type);
2222 if (!__path_is_null() &&
2223 cur_func_return_type() == &void_ctype &&
2224 !__bail_on_rest_of_function) {
2225 __call_all_scope_hooks();
2226 __pass_to_client(NULL, RETURN_HOOK);
2227 nullify_path();
2229 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2230 __free_scope_hooks();
2231 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2232 call->fn->symbol->parsed = true;
2234 free_expression_stack(&switch_expr_stack);
2235 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2236 nullify_path();
2237 free_goto_stack();
2239 restore_flow_state();
2240 fn_start_time = time_backup;
2241 cur_func = cur_func_bak;
2243 restore_all_states();
2244 set_position(call->pos);
2245 __inline_fn = orig_inline;
2246 inline_budget = orig_budget;
2247 __pass_to_client(call, INLINE_FN_END);
2250 static struct symbol_list *inlines_called;
2251 static void add_inline_function(struct symbol *sym)
2253 static struct symbol_list *already_added;
2254 struct symbol *tmp;
2256 FOR_EACH_PTR(already_added, tmp) {
2257 if (tmp == sym)
2258 return;
2259 } END_FOR_EACH_PTR(tmp);
2261 add_ptr_list(&already_added, sym);
2262 add_ptr_list(&inlines_called, sym);
2265 static void process_inlines(void)
2267 struct symbol *tmp;
2269 FOR_EACH_PTR(inlines_called, tmp) {
2270 split_function(tmp);
2271 } END_FOR_EACH_PTR(tmp);
2272 free_ptr_list(&inlines_called);
2275 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2277 struct symbol *sym;
2279 FOR_EACH_PTR_REVERSE(big_list, sym) {
2280 if (!sym->scope)
2281 continue;
2282 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2283 return sym;
2284 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2285 return sym;
2286 } END_FOR_EACH_PTR_REVERSE(sym);
2288 return NULL;
2291 static bool interesting_function(struct symbol *sym)
2293 static int prev_stream = -1;
2294 static bool prev_answer;
2295 const char *filename;
2296 int len;
2298 if (!(sym->ctype.modifiers & MOD_INLINE))
2299 return true;
2301 if (sym->pos.stream == prev_stream)
2302 return prev_answer;
2304 prev_stream = sym->pos.stream;
2305 prev_answer = false;
2307 filename = stream_name(sym->pos.stream);
2308 len = strlen(filename);
2309 if (len > 0 && filename[len - 1] == 'c')
2310 prev_answer = true;
2311 return prev_answer;
2314 static void split_inlines_in_scope(struct symbol *sym)
2316 struct symbol *base;
2317 struct symbol_list *scope_list;
2318 int stream;
2320 scope_list = sym->scope->symbols;
2321 stream = sym->pos.stream;
2323 /* find the last static symbol in the file */
2324 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2325 if (sym->pos.stream != stream)
2326 continue;
2327 if (sym->type != SYM_NODE)
2328 continue;
2329 base = get_base_type(sym);
2330 if (!base)
2331 continue;
2332 if (base->type != SYM_FN)
2333 continue;
2334 if (!base->inline_stmt)
2335 continue;
2336 if (!interesting_function(sym))
2337 continue;
2338 add_inline_function(sym);
2339 } END_FOR_EACH_PTR_REVERSE(sym);
2341 process_inlines();
2344 static void split_inlines(struct symbol_list *sym_list)
2346 struct symbol *sym;
2348 sym = get_last_scoped_symbol(sym_list, 0);
2349 if (sym)
2350 split_inlines_in_scope(sym);
2351 sym = get_last_scoped_symbol(sym_list, 1);
2352 if (sym)
2353 split_inlines_in_scope(sym);
2356 static struct stree *clone_estates_perm(struct stree *orig)
2358 struct stree *ret = NULL;
2359 struct sm_state *tmp;
2361 FOR_EACH_SM(orig, tmp) {
2362 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2363 } END_FOR_EACH_SM(tmp);
2365 return ret;
2368 struct position last_pos;
2369 static void split_c_file_functions(struct symbol_list *sym_list)
2371 struct symbol *sym;
2373 __unnullify_path();
2374 FOR_EACH_PTR(sym_list, sym) {
2375 set_position(sym->pos);
2376 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2377 __pass_to_client(sym, BASE_HOOK);
2378 fake_global_assign(sym);
2379 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2381 } END_FOR_EACH_PTR(sym);
2382 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2383 nullify_path();
2385 FOR_EACH_PTR(sym_list, sym) {
2386 set_position(sym->pos);
2387 last_pos = sym->pos;
2388 if (!interesting_function(sym))
2389 continue;
2390 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2391 split_function(sym);
2392 process_inlines();
2394 last_pos = sym->pos;
2395 } END_FOR_EACH_PTR(sym);
2396 split_inlines(sym_list);
2397 __pass_to_client(sym_list, END_FILE_HOOK);
2400 static int final_before_fake;
2401 void init_fake_env(void)
2403 if (!in_fake_env)
2404 final_before_fake = final_pass;
2405 in_fake_env++;
2406 __push_fake_cur_stree();
2407 final_pass = 0;
2410 void end_fake_env(void)
2412 __free_fake_cur_stree();
2413 in_fake_env--;
2414 if (!in_fake_env)
2415 final_pass = final_before_fake;
2418 static void open_output_files(char *base_file)
2420 char buf[256];
2422 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2423 sm_outfd = fopen(buf, "w");
2424 if (!sm_outfd)
2425 sm_fatal("Cannot open %s", buf);
2427 if (!option_info)
2428 return;
2430 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2431 sql_outfd = fopen(buf, "w");
2432 if (!sql_outfd)
2433 sm_fatal("Error: Cannot open %s", buf);
2435 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2436 caller_info_fd = fopen(buf, "w");
2437 if (!caller_info_fd)
2438 sm_fatal("Error: Cannot open %s", buf);
2441 void smatch(struct string_list *filelist)
2443 struct symbol_list *sym_list;
2444 struct timeval stop, start;
2445 char *path;
2446 int len;
2448 gettimeofday(&start, NULL);
2450 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2451 path = getcwd(NULL, 0);
2452 free(full_base_file);
2453 if (path) {
2454 len = strlen(path) + 1 + strlen(base_file) + 1;
2455 full_base_file = malloc(len);
2456 snprintf(full_base_file, len, "%s/%s", path, base_file);
2457 } else {
2458 full_base_file = alloc_string(base_file);
2460 if (option_file_output)
2461 open_output_files(base_file);
2462 base_file_stream = input_stream_nr;
2463 sym_list = sparse_keep_tokens(base_file);
2464 split_c_file_functions(sym_list);
2465 } END_FOR_EACH_PTR_NOTAG(base_file);
2467 gettimeofday(&stop, NULL);
2469 set_position(last_pos);
2470 final_pass = 1;
2471 if (option_time)
2472 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2473 if (option_mem)
2474 sm_msg("mem: %luKb", get_max_memory());