check_kernel: fix the build
[smatch.git] / smatch_flow.c
bloba0268d41bdd879e9bfedcea0fd11241ea76c1d7c
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 if (expr->op == '*' &&
681 !prev_expression_is_getting_address(expr))
682 __pass_to_client(expr, DEREF_HOOK);
683 __split_expr(expr->unop);
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 __pass_to_client(expr, DEREF_HOOK);
732 __split_expr(expr->deref);
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 (implied_condition_true(stmt->iterator_pre_condition))
855 return true;
856 if (option_assume_loops)
857 return true;
859 FOR_EACH_PTR(once_through_hooks, fn) {
860 if ((fn)(stmt))
861 return true;
862 } END_FOR_EACH_PTR(fn);
864 return false;
868 * Pre Loops are while and for loops.
870 static void handle_pre_loop(struct statement *stmt)
872 int once_through; /* we go through the loop at least once */
873 struct sm_state *extra_sm = NULL;
874 int unchanged = 0;
875 char *loop_name;
876 struct stree *stree = NULL;
877 struct sm_state *sm = NULL;
879 __push_scope_hooks();
881 loop_name = get_loop_name(loop_num);
882 loop_num++;
884 split_declaration(stmt->iterator_syms);
885 if (stmt->iterator_pre_statement) {
886 __split_stmt(stmt->iterator_pre_statement);
887 __prev_stmt = stmt->iterator_pre_statement;
890 once_through = call_once_through_hooks(stmt);
892 loop_count++;
893 __push_continues();
894 __push_breaks();
896 __merge_gotos(loop_name, NULL);
898 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
899 __in_pre_condition++;
900 __pass_to_client(stmt, PRELOOP_HOOK);
901 __split_whole_condition(stmt->iterator_pre_condition);
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)
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 bool already_parsed_call(struct expression *call)
1337 struct expression *expr;
1339 FOR_EACH_PTR(parsed_calls, expr) {
1340 if (expr == call)
1341 return true;
1342 } END_FOR_EACH_PTR(expr);
1343 return false;
1346 static void free_parsed_call_stuff(bool free_fake_states)
1348 free_expression_stack(&parsed_calls);
1349 if (free_fake_states)
1350 __discard_fake_states(NULL);
1353 void __split_stmt(struct statement *stmt)
1355 sval_t sval;
1356 struct timeval start, stop;
1357 bool skip_after = false;
1359 gettimeofday(&start, NULL);
1361 if (!stmt)
1362 goto out;
1364 if (!__in_fake_assign)
1365 __silence_warnings_for_stmt = false;
1367 if (__bail_on_rest_of_function || is_skipped_function())
1368 return;
1370 if (out_of_memory() || taking_too_long()) {
1371 gettimeofday(&start, NULL);
1373 __bail_on_rest_of_function = 1;
1374 final_pass = 1;
1375 sm_perror("Function too hairy. Giving up. %lu seconds",
1376 start.tv_sec - fn_start_time.tv_sec);
1377 fake_a_return();
1378 final_pass = 0; /* turn off sm_msg() from here */
1379 return;
1382 indent_cnt++;
1384 add_ptr_list(&big_statement_stack, stmt);
1385 free_expression_stack(&big_expression_stack);
1386 free_parsed_call_stuff(indent_cnt == 1);
1387 set_position(stmt->pos);
1388 __pass_to_client(stmt, STMT_HOOK);
1390 switch (stmt->type) {
1391 case STMT_DECLARATION:
1392 split_declaration(stmt->declaration);
1393 break;
1394 case STMT_RETURN:
1395 expr_set_parent_stmt(stmt->ret_value, stmt);
1397 split_ret_value(stmt->ret_value);
1398 __process_post_op_stack();
1399 __call_all_scope_hooks();
1400 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1401 nullify_path();
1402 break;
1403 case STMT_EXPRESSION:
1404 expr_set_parent_stmt(stmt->expression, stmt);
1405 expr_set_parent_stmt(stmt->context, stmt);
1407 __split_expr(stmt->expression);
1408 break;
1409 case STMT_COMPOUND:
1410 split_compound(stmt);
1411 break;
1412 case STMT_IF:
1413 stmt_set_parent_stmt(stmt->if_true, stmt);
1414 stmt_set_parent_stmt(stmt->if_false, stmt);
1415 expr_set_parent_stmt(stmt->if_conditional, stmt);
1417 if (known_condition_true(stmt->if_conditional)) {
1418 __split_stmt(stmt->if_true);
1419 break;
1421 if (known_condition_false(stmt->if_conditional)) {
1422 __split_stmt(stmt->if_false);
1423 break;
1425 __split_whole_condition(stmt->if_conditional);
1426 __split_stmt(stmt->if_true);
1427 if (empty_statement(stmt->if_true) &&
1428 last_stmt_on_same_line() &&
1429 !get_macro_name(stmt->if_true->pos))
1430 sm_warning("if();");
1431 __push_true_states();
1432 __use_false_states();
1433 __split_stmt(stmt->if_false);
1434 __merge_true_states();
1435 break;
1436 case STMT_ITERATOR:
1437 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1438 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1439 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1440 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1441 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1443 if (stmt->iterator_pre_condition)
1444 handle_pre_loop(stmt);
1445 else if (stmt->iterator_post_condition)
1446 handle_post_loop(stmt);
1447 else {
1448 // these are for(;;) type loops.
1449 handle_pre_loop(stmt);
1451 break;
1452 case STMT_SWITCH:
1453 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1454 expr_set_parent_stmt(stmt->switch_expression, stmt);
1456 if (get_value(stmt->switch_expression, &sval)) {
1457 split_known_switch(stmt, sval);
1458 break;
1460 __split_expr(stmt->switch_expression);
1461 push_expression(&switch_expr_stack, stmt->switch_expression);
1462 __save_switch_states(top_expression(switch_expr_stack));
1463 nullify_path();
1464 __push_default();
1465 __push_breaks();
1466 __split_stmt(stmt->switch_statement);
1467 if (!__pop_default() && have_remaining_cases())
1468 fake_an_empty_default(stmt->pos);
1469 __discard_switches();
1470 __merge_breaks();
1471 pop_expression(&switch_expr_stack);
1472 break;
1473 case STMT_CASE:
1474 split_case(stmt);
1475 break;
1476 case STMT_LABEL:
1477 __split_label_stmt(stmt);
1478 __pass_to_client(stmt, STMT_HOOK_AFTER);
1479 skip_after = true;
1480 __split_stmt(stmt->label_statement);
1481 break;
1482 case STMT_GOTO:
1483 expr_set_parent_stmt(stmt->goto_expression, stmt);
1485 __split_expr(stmt->goto_expression);
1486 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1487 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1488 __process_breaks();
1489 } else if (!strcmp(stmt->goto_label->ident->name,
1490 "continue")) {
1491 __process_continues();
1493 } else if (stmt->goto_label &&
1494 stmt->goto_label->type == SYM_LABEL &&
1495 stmt->goto_label->ident) {
1496 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1498 nullify_path();
1499 if (is_last_stmt(stmt))
1500 handle_backward_goto(stmt);
1501 break;
1502 case STMT_NONE:
1503 break;
1504 case STMT_ASM:
1505 expr_set_parent_stmt(stmt->asm_string, stmt);
1507 find_asm_gotos(stmt);
1508 __pass_to_client(stmt, ASM_HOOK);
1509 __split_expr(stmt->asm_string);
1510 split_asm_ops(stmt->asm_outputs);
1511 split_asm_ops(stmt->asm_inputs);
1512 split_expr_list(stmt->asm_clobbers, NULL);
1513 break;
1514 case STMT_CONTEXT:
1515 break;
1516 case STMT_RANGE:
1517 __split_expr(stmt->range_expression);
1518 __split_expr(stmt->range_low);
1519 __split_expr(stmt->range_high);
1520 break;
1522 if (!skip_after)
1523 __pass_to_client(stmt, STMT_HOOK_AFTER);
1524 if (--indent_cnt == 1)
1525 free_parsed_call_stuff(true);
1527 out:
1528 __process_post_op_stack();
1530 gettimeofday(&stop, NULL);
1531 if (option_time_stmt && stmt)
1532 sm_msg("stmt_time%s: %ld",
1533 stmt->type == STMT_COMPOUND ? "_block" : "",
1534 stop.tv_sec - start.tv_sec);
1537 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1539 struct expression *expr;
1541 FOR_EACH_PTR(expr_list, expr) {
1542 expr_set_parent_expr(expr, parent);
1543 __split_expr(expr);
1544 __process_post_op_stack();
1545 } END_FOR_EACH_PTR(expr);
1548 static bool cast_arg(struct symbol *type, struct expression *arg)
1550 struct symbol *orig;
1552 if (!type)
1553 return false;
1555 arg = strip_parens(arg);
1556 if (arg != strip_expr(arg))
1557 return true;
1559 orig = get_type(arg);
1560 if (!orig)
1561 return true;
1562 if (types_equiv(orig, type))
1563 return false;
1565 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1566 return true;
1569 * I would have expected that we could just do use (orig == type) but I
1570 * guess for pointers we need to get the basetype to do that comparison.
1574 if (orig->type != SYM_PTR ||
1575 type->type != SYM_PTR) {
1576 if (type_fits(type, orig))
1577 return false;
1578 return true;
1580 orig = get_real_base_type(orig);
1581 type = get_real_base_type(type);
1582 if (orig == type)
1583 return false;
1585 return true;
1588 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1590 char buf[64];
1591 bool cast;
1593 if (!expr || !cur_func_sym)
1594 return NULL;
1596 if (already_parsed_call(call))
1597 return NULL;
1599 if (expr->type == EXPR_ASSIGNMENT)
1600 return expr;
1602 /* for va_args then we don't know the type */
1603 if (!type)
1604 type = get_type(expr);
1606 cast = cast_arg(type, expr);
1608 * Using expr_to_sym() here is a hack. We want to say that we don't
1609 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1610 * It turns out faking these assignments is way more expensive than I
1611 * would have imagined. I'm not sure why exactly.
1614 if (!cast) {
1616 * if the code is "return *p;" where "p" is a user pointer then
1617 * we want to create a fake assignment so that it sets the state
1618 * in check_kernel_user_data.c.
1621 if (expr->type != EXPR_PREOP &&
1622 expr->op != '*' && expr->op != '&' &&
1623 expr_to_sym(expr))
1624 return expr;
1627 if (nr == -1)
1628 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1629 else
1630 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1632 return create_fake_assign(buf, type, expr);
1635 static void split_args(struct expression *expr)
1637 struct expression *arg, *tmp;
1638 struct symbol *type;
1639 int i;
1641 i = -1;
1642 FOR_EACH_PTR(expr->args, arg) {
1643 i++;
1644 expr_set_parent_expr(arg, expr);
1645 type = get_arg_type(expr->fn, i);
1646 tmp = fake_a_variable_assign(type, expr, arg, i);
1647 if (tmp != arg)
1648 __in_fake_var_assign++;
1649 __split_expr(tmp);
1650 if (tmp != arg)
1651 __in_fake_var_assign--;
1652 __process_post_op_stack();
1653 } END_FOR_EACH_PTR(arg);
1656 static void call_cleanup_fn(void *_sym)
1658 struct symbol *sym = _sym;
1659 struct expression *call, *arg;
1660 struct expression_list *args = NULL;
1663 arg = symbol_expression(sym);
1664 arg = preop_expression(arg, '&');
1665 add_ptr_list(&args, arg);
1666 call = call_expression(sym->cleanup, args);
1668 __split_expr(call);
1671 static void add_cleanup_hook(struct symbol *sym)
1673 if (!sym->cleanup)
1674 return;
1675 add_scope_hook(&call_cleanup_fn, sym);
1678 static void split_sym(struct symbol *sym)
1680 if (!sym)
1681 return;
1682 if (!(sym->namespace & NS_SYMBOL))
1683 return;
1685 __split_stmt(sym->stmt);
1686 __split_expr(sym->array_size);
1687 if (sym->cleanup)
1688 add_cleanup_hook(sym);
1689 split_symlist(sym->arguments);
1690 split_symlist(sym->symbol_list);
1691 __split_stmt(sym->inline_stmt);
1692 split_symlist(sym->inline_symbol_list);
1695 static void split_symlist(struct symbol_list *sym_list)
1697 struct symbol *sym;
1699 FOR_EACH_PTR(sym_list, sym) {
1700 split_sym(sym);
1701 } END_FOR_EACH_PTR(sym);
1704 typedef void (fake_cb)(struct expression *expr);
1706 static int member_to_number(struct expression *expr, struct ident *member)
1708 struct symbol *type, *tmp;
1709 char *name;
1710 int i;
1712 if (!member)
1713 return -1;
1714 name = member->name;
1716 type = get_type(expr);
1717 if (!type || type->type != SYM_STRUCT)
1718 return -1;
1720 i = -1;
1721 FOR_EACH_PTR(type->symbol_list, tmp) {
1722 i++;
1723 if (!tmp->ident)
1724 continue;
1725 if (strcmp(name, tmp->ident->name) == 0)
1726 return i;
1727 } END_FOR_EACH_PTR(tmp);
1728 return -1;
1731 static struct ident *number_to_member(struct expression *expr, int num)
1733 struct symbol *type, *member;
1734 int i = 0;
1736 type = get_type(expr);
1737 if (!type || type->type != SYM_STRUCT)
1738 return NULL;
1740 FOR_EACH_PTR(type->symbol_list, member) {
1741 if (i == num)
1742 return member->ident;
1743 i++;
1744 } END_FOR_EACH_PTR(member);
1745 return NULL;
1748 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1750 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1752 struct expression *edge_member, *assign;
1753 struct symbol *base = get_real_base_type(member);
1754 struct symbol *tmp;
1756 if (member->ident)
1757 expr = member_expression(expr, '.', member->ident);
1759 FOR_EACH_PTR(base->symbol_list, tmp) {
1760 struct symbol *type;
1762 type = get_real_base_type(tmp);
1763 if (!type)
1764 continue;
1766 edge_member = member_expression(expr, '.', tmp->ident);
1767 if (get_extra_state(edge_member))
1768 continue;
1770 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1771 set_inner_struct_members(expr, tmp);
1772 continue;
1775 if (!tmp->ident)
1776 continue;
1778 assign = assign_expression(edge_member, '=', zero_expr());
1779 __split_expr(assign);
1780 } END_FOR_EACH_PTR(tmp);
1785 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1787 struct symbol *tmp;
1788 struct expression *member = NULL;
1789 struct expression *assign;
1791 FOR_EACH_PTR(type->symbol_list, tmp) {
1792 type = get_real_base_type(tmp);
1793 if (!type)
1794 continue;
1796 if (tmp->ident) {
1797 member = member_expression(expr, '.', tmp->ident);
1798 if (get_extra_state(member))
1799 continue;
1802 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1803 set_inner_struct_members(expr, tmp);
1804 continue;
1806 if (type->type == SYM_ARRAY)
1807 continue;
1808 if (!tmp->ident)
1809 continue;
1811 assign = assign_expression(member, '=', zero_expr());
1812 __split_expr(assign);
1813 } END_FOR_EACH_PTR(tmp);
1816 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1818 struct expression *deref, *assign, *tmp, *right;
1819 struct symbol *struct_type, *type;
1820 struct ident *member;
1821 int member_idx;
1823 struct_type = get_type(symbol);
1824 if (!struct_type ||
1825 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1826 return;
1829 * We're parsing an initializer that could look something like this:
1830 * struct foo foo = {
1831 * 42,
1832 * .whatever.xxx = 11,
1833 * .zzz = 12,
1834 * };
1836 * So what we have here is a list with 42, .whatever, and .zzz. We need
1837 * to break it up into left and right sides of the assignments.
1840 member_idx = 0;
1841 FOR_EACH_PTR(members, tmp) {
1842 deref = NULL;
1843 if (tmp->type == EXPR_IDENTIFIER) {
1844 member_idx = member_to_number(symbol, tmp->expr_ident);
1845 while (tmp->type == EXPR_IDENTIFIER) {
1846 member = tmp->expr_ident;
1847 tmp = tmp->ident_expression;
1848 if (deref)
1849 deref = member_expression(deref, '.', member);
1850 else
1851 deref = member_expression(symbol, '.', member);
1853 } else {
1854 member = number_to_member(symbol, member_idx);
1855 deref = member_expression(symbol, '.', member);
1857 right = tmp;
1858 member_idx++;
1859 if (right->type == EXPR_INITIALIZER) {
1860 type = get_type(deref);
1861 if (type && type->type == SYM_ARRAY)
1862 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1863 else
1864 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1865 } else {
1866 assign = assign_expression(deref, '=', right);
1867 fake_cb(assign);
1869 } END_FOR_EACH_PTR(tmp);
1871 set_unset_to_zero(struct_type, symbol);
1874 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1876 fake_member_assigns_helper(symbol_expression(sym),
1877 sym->initializer->expr_list, fake_cb);
1880 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1882 struct expression *offset, *binop, *assign, *tmp;
1883 struct symbol *type;
1884 int idx, max;
1886 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1887 return;
1889 max = 0;
1890 idx = 0;
1891 FOR_EACH_PTR(expr_list, tmp) {
1892 if (tmp->type == EXPR_INDEX) {
1893 if (tmp->idx_from != tmp->idx_to)
1894 return;
1895 idx = tmp->idx_from;
1896 if (idx > max)
1897 max = idx;
1898 if (!tmp->idx_expression)
1899 goto next;
1900 tmp = tmp->idx_expression;
1902 offset = value_expr(idx);
1903 binop = array_element_expression(array, offset);
1904 if (tmp->type == EXPR_INITIALIZER) {
1905 type = get_type(binop);
1906 if (type && type->type == SYM_ARRAY)
1907 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1908 else
1909 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1910 } else {
1911 assign = assign_expression(binop, '=', tmp);
1912 fake_cb(assign);
1914 next:
1915 idx++;
1916 if (idx > max)
1917 max = idx;
1918 } END_FOR_EACH_PTR(tmp);
1920 __call_array_initialized_hooks(array, max);
1923 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1925 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1928 static void fake_assign_expr(struct symbol *sym)
1930 struct expression *assign, *symbol;
1932 symbol = symbol_expression(sym);
1933 assign = assign_expression(symbol, '=', sym->initializer);
1934 __split_expr(assign);
1937 static void do_initializer_stuff(struct symbol *sym)
1939 if (!sym->initializer)
1940 return;
1942 if (sym->initializer->type == EXPR_INITIALIZER) {
1943 if (get_real_base_type(sym)->type == SYM_ARRAY)
1944 fake_element_assigns(sym, __split_expr);
1945 else
1946 fake_member_assigns(sym, __split_expr);
1947 } else {
1948 fake_assign_expr(sym);
1952 static void split_declaration(struct symbol_list *sym_list)
1954 struct symbol *sym;
1956 FOR_EACH_PTR(sym_list, sym) {
1957 __pass_to_client(sym, DECLARATION_HOOK);
1958 do_initializer_stuff(sym);
1959 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1960 split_sym(sym);
1961 } END_FOR_EACH_PTR(sym);
1964 static void call_global_assign_hooks(struct expression *assign)
1966 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1969 static void fake_global_assign(struct symbol *sym)
1971 struct expression *assign, *symbol;
1973 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1974 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1975 fake_element_assigns(sym, call_global_assign_hooks);
1976 } else if (sym->initializer) {
1977 symbol = symbol_expression(sym);
1978 assign = assign_expression(symbol, '=', sym->initializer);
1979 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1980 } else {
1981 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1983 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1984 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1985 fake_member_assigns(sym, call_global_assign_hooks);
1986 } else if (sym->initializer) {
1987 symbol = symbol_expression(sym);
1988 assign = assign_expression(symbol, '=', sym->initializer);
1989 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1990 } else {
1991 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1993 } else {
1994 symbol = symbol_expression(sym);
1995 if (sym->initializer) {
1996 assign = assign_expression(symbol, '=', sym->initializer);
1997 __split_expr(assign);
1998 } else {
1999 assign = assign_expression(symbol, '=', zero_expr());
2001 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
2005 static void start_function_definition(struct symbol *sym)
2007 __in_function_def = 1;
2008 __pass_to_client(sym, FUNC_DEF_HOOK);
2009 __in_function_def = 0;
2010 __pass_to_client(sym, AFTER_DEF_HOOK);
2014 static void parse_fn_statements(struct symbol *base)
2016 __split_stmt(base->stmt);
2017 __split_stmt(base->inline_stmt);
2020 void add_function_data(unsigned long *fn_data)
2022 __add_ptr_list(&fn_data_list, fn_data);
2025 static void clear_function_data(void)
2027 unsigned long *tmp;
2029 FOR_EACH_PTR(fn_data_list, tmp) {
2030 *tmp = 0;
2031 } END_FOR_EACH_PTR(tmp);
2034 static void record_func_time(void)
2036 struct timeval stop;
2037 int func_time;
2038 char buf[32];
2040 gettimeofday(&stop, NULL);
2041 func_time = stop.tv_sec - fn_start_time.tv_sec;
2042 snprintf(buf, sizeof(buf), "%d", func_time);
2043 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
2044 if (option_time && func_time > 2) {
2045 final_pass++;
2046 sm_msg("func_time: %d", func_time);
2047 final_pass--;
2051 static void split_function(struct symbol *sym)
2053 struct symbol *base_type = get_base_type(sym);
2055 if (!base_type->stmt && !base_type->inline_stmt)
2056 return;
2058 gettimeofday(&outer_fn_start_time, NULL);
2059 gettimeofday(&fn_start_time, NULL);
2060 cur_func_sym = sym;
2061 if (sym->ident)
2062 cur_func = sym->ident->name;
2063 if (option_process_function && cur_func &&
2064 strcmp(option_process_function, cur_func) != 0)
2065 return;
2066 set_position(sym->pos);
2067 clear_function_data();
2068 loop_count = 0;
2069 last_goto_statement_handled = 0;
2070 sm_debug("new function: %s\n", cur_func);
2071 __stree_id = 0;
2072 if (option_two_passes) {
2073 __unnullify_path();
2074 loop_num = 0;
2075 final_pass = 0;
2076 start_function_definition(sym);
2077 parse_fn_statements(base_type);
2078 __call_scope_hooks();
2079 nullify_path();
2081 __unnullify_path();
2082 loop_num = 0;
2083 final_pass = 1;
2084 start_function_definition(sym);
2085 parse_fn_statements(base_type);
2086 if (!__path_is_null() &&
2087 cur_func_return_type() == &void_ctype &&
2088 !__bail_on_rest_of_function) {
2089 __call_all_scope_hooks();
2090 __pass_to_client(NULL, RETURN_HOOK);
2091 nullify_path();
2093 __pass_to_client(sym, END_FUNC_HOOK);
2094 __call_scope_hooks();
2095 __pass_to_client(sym, AFTER_FUNC_HOOK);
2096 sym->parsed = true;
2098 clear_all_states();
2100 record_func_time();
2102 cur_func_sym = NULL;
2103 cur_func = NULL;
2104 free_data_info_allocs();
2105 free_expression_stack(&switch_expr_stack);
2106 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2107 __bail_on_rest_of_function = 0;
2110 static void save_flow_state(void)
2112 unsigned long *tmp;
2114 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
2115 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
2116 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
2118 __add_ptr_list(&backup, big_statement_stack);
2119 __add_ptr_list(&backup, big_expression_stack);
2120 __add_ptr_list(&backup, big_condition_stack);
2121 __add_ptr_list(&backup, switch_expr_stack);
2123 __add_ptr_list(&backup, cur_func_sym);
2125 __add_ptr_list(&backup, parsed_calls);
2127 __add_ptr_list(&backup, __prev_stmt);
2128 __add_ptr_list(&backup, __cur_stmt);
2129 __add_ptr_list(&backup, __next_stmt);
2131 FOR_EACH_PTR(fn_data_list, tmp) {
2132 __add_ptr_list(&backup, (void *)*tmp);
2133 } END_FOR_EACH_PTR(tmp);
2136 static void *pop_backup(void)
2138 void *ret;
2140 ret = last_ptr_list(backup);
2141 delete_ptr_list_last(&backup);
2142 return ret;
2145 static void restore_flow_state(void)
2147 unsigned long *tmp;
2149 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2150 *tmp = (unsigned long)pop_backup();
2151 } END_FOR_EACH_PTR_REVERSE(tmp);
2153 __next_stmt = pop_backup();
2154 __cur_stmt = pop_backup();
2155 __prev_stmt = pop_backup();
2157 parsed_calls = pop_backup();
2159 cur_func_sym = pop_backup();
2160 switch_expr_stack = pop_backup();
2161 big_condition_stack = pop_backup();
2162 big_expression_stack = pop_backup();
2163 big_statement_stack = pop_backup();
2164 final_pass = PTR_INT(pop_backup()) >> 2;
2165 loop_count = PTR_INT(pop_backup()) >> 2;
2166 loop_num = PTR_INT(pop_backup()) >> 2;
2169 void parse_inline(struct expression *call)
2171 struct symbol *base_type;
2172 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2173 struct timeval time_backup = fn_start_time;
2174 struct expression *orig_inline = __inline_fn;
2175 int orig_budget;
2177 if (out_of_memory() || taking_too_long())
2178 return;
2180 if (already_parsed_call(call))
2181 return;
2183 save_flow_state();
2185 __pass_to_client(call, INLINE_FN_START);
2186 final_pass = 0; /* don't print anything */
2187 __inline_fn = call;
2188 orig_budget = inline_budget;
2189 inline_budget = inline_budget - 5;
2191 base_type = get_base_type(call->fn->symbol);
2192 cur_func_sym = call->fn->symbol;
2193 if (call->fn->symbol->ident)
2194 cur_func = call->fn->symbol->ident->name;
2195 else
2196 cur_func = NULL;
2197 set_position(call->fn->symbol->pos);
2199 save_all_states();
2200 big_statement_stack = NULL;
2201 big_expression_stack = NULL;
2202 big_condition_stack = NULL;
2203 switch_expr_stack = NULL;
2204 parsed_calls = NULL;
2206 sm_debug("inline function: %s\n", cur_func);
2207 __unnullify_path();
2208 clear_function_data();
2209 loop_num = 0;
2210 loop_count = 0;
2211 start_function_definition(call->fn->symbol);
2212 parse_fn_statements(base_type);
2213 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2214 __call_scope_hooks();
2215 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2216 call->fn->symbol->parsed = true;
2218 free_expression_stack(&switch_expr_stack);
2219 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2220 nullify_path();
2221 free_goto_stack();
2223 restore_flow_state();
2224 fn_start_time = time_backup;
2225 cur_func = cur_func_bak;
2227 restore_all_states();
2228 set_position(call->pos);
2229 __inline_fn = orig_inline;
2230 inline_budget = orig_budget;
2231 __pass_to_client(call, INLINE_FN_END);
2234 static struct symbol_list *inlines_called;
2235 static void add_inline_function(struct symbol *sym)
2237 static struct symbol_list *already_added;
2238 struct symbol *tmp;
2240 FOR_EACH_PTR(already_added, tmp) {
2241 if (tmp == sym)
2242 return;
2243 } END_FOR_EACH_PTR(tmp);
2245 add_ptr_list(&already_added, sym);
2246 add_ptr_list(&inlines_called, sym);
2249 static void process_inlines(void)
2251 struct symbol *tmp;
2253 FOR_EACH_PTR(inlines_called, tmp) {
2254 split_function(tmp);
2255 } END_FOR_EACH_PTR(tmp);
2256 free_ptr_list(&inlines_called);
2259 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2261 struct symbol *sym;
2263 FOR_EACH_PTR_REVERSE(big_list, sym) {
2264 if (!sym->scope)
2265 continue;
2266 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2267 return sym;
2268 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2269 return sym;
2270 } END_FOR_EACH_PTR_REVERSE(sym);
2272 return NULL;
2275 static bool interesting_function(struct symbol *sym)
2277 static int prev_stream = -1;
2278 static bool prev_answer;
2279 const char *filename;
2280 int len;
2282 if (!(sym->ctype.modifiers & MOD_INLINE))
2283 return true;
2285 if (sym->pos.stream == prev_stream)
2286 return prev_answer;
2288 prev_stream = sym->pos.stream;
2289 prev_answer = false;
2291 filename = stream_name(sym->pos.stream);
2292 len = strlen(filename);
2293 if (len > 0 && filename[len - 1] == 'c')
2294 prev_answer = true;
2295 return prev_answer;
2298 static void split_inlines_in_scope(struct symbol *sym)
2300 struct symbol *base;
2301 struct symbol_list *scope_list;
2302 int stream;
2304 scope_list = sym->scope->symbols;
2305 stream = sym->pos.stream;
2307 /* find the last static symbol in the file */
2308 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2309 if (sym->pos.stream != stream)
2310 continue;
2311 if (sym->type != SYM_NODE)
2312 continue;
2313 base = get_base_type(sym);
2314 if (!base)
2315 continue;
2316 if (base->type != SYM_FN)
2317 continue;
2318 if (!base->inline_stmt)
2319 continue;
2320 if (!interesting_function(sym))
2321 continue;
2322 add_inline_function(sym);
2323 } END_FOR_EACH_PTR_REVERSE(sym);
2325 process_inlines();
2328 static void split_inlines(struct symbol_list *sym_list)
2330 struct symbol *sym;
2332 sym = get_last_scoped_symbol(sym_list, 0);
2333 if (sym)
2334 split_inlines_in_scope(sym);
2335 sym = get_last_scoped_symbol(sym_list, 1);
2336 if (sym)
2337 split_inlines_in_scope(sym);
2340 static struct stree *clone_estates_perm(struct stree *orig)
2342 struct stree *ret = NULL;
2343 struct sm_state *tmp;
2345 FOR_EACH_SM(orig, tmp) {
2346 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2347 } END_FOR_EACH_SM(tmp);
2349 return ret;
2352 struct position last_pos;
2353 static void split_c_file_functions(struct symbol_list *sym_list)
2355 struct symbol *sym;
2357 __unnullify_path();
2358 FOR_EACH_PTR(sym_list, sym) {
2359 set_position(sym->pos);
2360 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2361 __pass_to_client(sym, BASE_HOOK);
2362 fake_global_assign(sym);
2363 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2365 } END_FOR_EACH_PTR(sym);
2366 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2367 nullify_path();
2369 FOR_EACH_PTR(sym_list, sym) {
2370 set_position(sym->pos);
2371 last_pos = sym->pos;
2372 if (!interesting_function(sym))
2373 continue;
2374 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2375 split_function(sym);
2376 process_inlines();
2378 last_pos = sym->pos;
2379 } END_FOR_EACH_PTR(sym);
2380 split_inlines(sym_list);
2381 __pass_to_client(sym_list, END_FILE_HOOK);
2384 static int final_before_fake;
2385 void init_fake_env(void)
2387 if (!in_fake_env)
2388 final_before_fake = final_pass;
2389 in_fake_env++;
2390 __push_fake_cur_stree();
2391 final_pass = 0;
2394 void end_fake_env(void)
2396 __free_fake_cur_stree();
2397 in_fake_env--;
2398 if (!in_fake_env)
2399 final_pass = final_before_fake;
2402 static void open_output_files(char *base_file)
2404 char buf[256];
2406 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2407 sm_outfd = fopen(buf, "w");
2408 if (!sm_outfd)
2409 sm_fatal("Cannot open %s", buf);
2411 if (!option_info)
2412 return;
2414 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2415 sql_outfd = fopen(buf, "w");
2416 if (!sql_outfd)
2417 sm_fatal("Error: Cannot open %s", buf);
2419 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2420 caller_info_fd = fopen(buf, "w");
2421 if (!caller_info_fd)
2422 sm_fatal("Error: Cannot open %s", buf);
2425 void smatch(struct string_list *filelist)
2427 struct symbol_list *sym_list;
2428 struct timeval stop, start;
2429 char *path;
2430 int len;
2432 gettimeofday(&start, NULL);
2434 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2435 path = getcwd(NULL, 0);
2436 free(full_base_file);
2437 if (path) {
2438 len = strlen(path) + 1 + strlen(base_file) + 1;
2439 full_base_file = malloc(len);
2440 snprintf(full_base_file, len, "%s/%s", path, base_file);
2441 } else {
2442 full_base_file = alloc_string(base_file);
2444 if (option_file_output)
2445 open_output_files(base_file);
2446 base_file_stream = input_stream_nr;
2447 sym_list = sparse_keep_tokens(base_file);
2448 split_c_file_functions(sym_list);
2449 } END_FOR_EACH_PTR_NOTAG(base_file);
2451 gettimeofday(&stop, NULL);
2453 set_position(last_pos);
2454 final_pass = 1;
2455 if (option_time)
2456 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2457 if (option_mem)
2458 sm_msg("mem: %luKb", get_max_memory());