states: make debug output more consistent
[smatch.git] / smatch_flow.c
blobe5a1cd5aeeda573e1f96e5c1a8cdd824120ad804
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 final_pass;
30 int __inline_call;
31 struct expression *__inline_fn;
33 static int __smatch_lineno = 0;
35 static char *base_file;
36 static const char *filename;
37 static char *pathname;
38 static char *full_filename;
39 static char *cur_func;
40 static unsigned int loop_count;
41 static int last_goto_statement_handled;
42 int __expr_stmt_count;
43 int __in_function_def;
44 static struct expression_list *switch_expr_stack = NULL;
45 static struct expression_list *post_op_stack = NULL;
47 struct expression_list *big_expression_stack;
48 struct statement_list *big_statement_stack;
49 struct statement *__prev_stmt;
50 struct statement *__cur_stmt;
51 struct statement *__next_stmt;
52 int __in_pre_condition = 0;
53 int __bail_on_rest_of_function = 0;
54 static struct timeval fn_start_time;
55 char *get_function(void) { return cur_func; }
56 int get_lineno(void) { return __smatch_lineno; }
57 int inside_loop(void) { return !!loop_count; }
58 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
59 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
60 int in_expression_statement(void) { return !!__expr_stmt_count; }
62 static void split_symlist(struct symbol_list *sym_list);
63 static void split_declaration(struct symbol_list *sym_list);
64 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
65 static void add_inline_function(struct symbol *sym);
66 static void parse_inline(struct expression *expr);
68 int option_assume_loops = 0;
69 int option_two_passes = 0;
70 struct symbol *cur_func_sym = NULL;
71 struct stree *global_states;
73 long long valid_ptr_min = 4096;
74 long long valid_ptr_max = 2117777777;
75 sval_t valid_ptr_min_sval = {
76 .type = &ptr_ctype,
77 {.value = 4096},
79 sval_t valid_ptr_max_sval = {
80 .type = &ptr_ctype,
81 {.value = LONG_MAX - 100000},
84 static void set_valid_ptr_max(void)
86 if (type_bits(&ptr_ctype) == 32)
87 valid_ptr_max = 2117777777;
88 else if (type_bits(&ptr_ctype) == 64)
89 valid_ptr_max = 2117777777777777777LL;
91 valid_ptr_max_sval.value = valid_ptr_max;
94 int outside_of_function(void)
96 return cur_func_sym == NULL;
99 const char *get_filename(void)
101 if (option_info)
102 return base_file;
103 if (option_full_path)
104 return full_filename;
105 return filename;
108 const char *get_base_file(void)
110 return base_file;
113 static void set_position(struct position pos)
115 int len;
116 static int prev_stream = -1;
118 if (pos.stream == 0 && pos.line == 0)
119 return;
121 __smatch_lineno = pos.line;
123 if (pos.stream == prev_stream)
124 return;
126 filename = stream_name(pos.stream);
128 free(full_filename);
129 pathname = getcwd(NULL, 0);
130 if (pathname) {
131 len = strlen(pathname) + 1 + strlen(filename) + 1;
132 full_filename = malloc(len);
133 snprintf(full_filename, len, "%s/%s", pathname, filename);
134 } else {
135 full_filename = alloc_string(filename);
137 free(pathname);
140 void set_parent_expr(struct expression *expr, struct expression *parent)
142 if (!expr)
143 return;
144 expr->parent = parent;
147 static void set_parent_stmt(struct statement *stmt, struct statement *parent)
149 if (!stmt)
150 return;
151 stmt->parent = parent;
154 int is_assigned_call(struct expression *expr)
156 struct expression *tmp;
158 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
159 if (tmp->type == EXPR_ASSIGNMENT && tmp->op == '=' &&
160 strip_expr(tmp->right) == expr)
161 return 1;
162 if (tmp->pos.line < expr->pos.line)
163 return 0;
164 } END_FOR_EACH_PTR_REVERSE(tmp);
165 return 0;
168 static int is_inline_func(struct expression *expr)
170 if (expr->type != EXPR_SYMBOL || !expr->symbol)
171 return 0;
172 if (expr->symbol->ctype.modifiers & MOD_INLINE)
173 return 1;
174 return 0;
177 static int is_noreturn_func(struct expression *expr)
179 if (expr->type != EXPR_SYMBOL || !expr->symbol)
180 return 0;
181 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
182 return 1;
183 return 0;
186 int inlinable(struct expression *expr)
188 struct symbol *sym;
189 struct statement *last_stmt = NULL;
191 if (__inline_fn) /* don't nest */
192 return 0;
194 if (expr->type != EXPR_SYMBOL || !expr->symbol)
195 return 0;
196 if (is_no_inline_function(expr->symbol->ident->name))
197 return 0;
198 sym = get_base_type(expr->symbol);
199 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
200 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
201 return 0;
202 if (sym->stmt->type != STMT_COMPOUND)
203 return 0;
204 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
206 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
207 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
208 return 0;
209 if (sym->inline_stmt->type != STMT_COMPOUND)
210 return 0;
211 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
214 if (!last_stmt)
215 return 0;
217 /* the magic numbers in this function are pulled out of my bum. */
218 if (last_stmt->pos.line > sym->pos.line + 20)
219 return 0;
221 return 1;
224 void __process_post_op_stack(void)
226 struct expression *expr;
228 FOR_EACH_PTR(post_op_stack, expr) {
229 __pass_to_client(expr, OP_HOOK);
230 } END_FOR_EACH_PTR(expr);
232 __free_ptr_list((struct ptr_list **)&post_op_stack);
235 static int handle_comma_assigns(struct expression *expr)
237 struct expression *right;
238 struct expression *assign;
240 right = strip_expr(expr->right);
241 if (right->type != EXPR_COMMA)
242 return 0;
244 __split_expr(right->left);
245 __process_post_op_stack();
247 assign = assign_expression(expr->left, right->right);
248 __split_expr(assign);
250 return 1;
253 /* This is to handle *p++ = foo; assignments */
254 static int handle_postop_assigns(struct expression *expr)
256 struct expression *left, *fake_left;
257 struct expression *assign;
259 left = strip_expr(expr->left);
260 if (left->type != EXPR_PREOP || left->op != '*')
261 return 0;
262 left = strip_expr(left->unop);
263 if (left->type != EXPR_POSTOP)
264 return 0;
266 fake_left = deref_expression(strip_expr(left->unop));
267 assign = assign_expression(fake_left, expr->right);
269 __split_expr(assign);
270 __split_expr(expr->left);
272 return 1;
275 static int prev_expression_is_getting_address(struct expression *expr)
277 struct expression *parent;
279 do {
280 parent = expr->parent;
282 if (!parent)
283 return 0;
284 if (parent->type == EXPR_PREOP && parent->op == '&')
285 return 1;
286 if (parent->type == EXPR_PREOP && parent->op == '(')
287 goto next;
288 if (parent->type == EXPR_DEREF && parent->op == '.')
289 goto next;
291 return 0;
292 next:
293 expr = parent;
294 } while (1);
297 void __split_expr(struct expression *expr)
299 if (!expr)
300 return;
302 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
304 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
305 return;
306 if (__in_fake_assign >= 4) /* don't allow too much nesting */
307 return;
309 push_expression(&big_expression_stack, expr);
310 set_position(expr->pos);
311 __pass_to_client(expr, EXPR_HOOK);
313 switch (expr->type) {
314 case EXPR_PREOP:
315 set_parent_expr(expr->unop, expr);
317 if (expr->op == '*' &&
318 !prev_expression_is_getting_address(expr))
319 __pass_to_client(expr, DEREF_HOOK);
320 __split_expr(expr->unop);
321 __pass_to_client(expr, OP_HOOK);
322 break;
323 case EXPR_POSTOP:
324 set_parent_expr(expr->unop, expr);
326 __split_expr(expr->unop);
327 push_expression(&post_op_stack, expr);
328 break;
329 case EXPR_STATEMENT:
330 __expr_stmt_count++;
331 __split_stmt(expr->statement);
332 __expr_stmt_count--;
333 break;
334 case EXPR_LOGICAL:
335 case EXPR_COMPARE:
336 set_parent_expr(expr->left, expr);
337 set_parent_expr(expr->right, expr);
339 __pass_to_client(expr, LOGIC_HOOK);
340 __handle_logic(expr);
341 break;
342 case EXPR_BINOP:
343 set_parent_expr(expr->left, expr);
344 set_parent_expr(expr->right, expr);
346 __pass_to_client(expr, BINOP_HOOK);
347 case EXPR_COMMA:
348 set_parent_expr(expr->left, expr);
349 set_parent_expr(expr->right, expr);
351 __split_expr(expr->left);
352 __process_post_op_stack();
353 __split_expr(expr->right);
354 break;
355 case EXPR_ASSIGNMENT: {
356 struct expression *tmp;
358 set_parent_expr(expr->left, expr);
359 set_parent_expr(expr->right, expr);
361 if (!expr->right)
362 break;
364 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
366 /* foo = !bar() */
367 if (__handle_condition_assigns(expr))
368 break;
369 /* foo = (x < 5 ? foo : 5); */
370 if (__handle_select_assigns(expr))
371 break;
372 /* foo = ({frob(); frob(); frob(); 1;}) */
373 if (__handle_expr_statement_assigns(expr))
374 break;
375 /* foo = (3, 4); */
376 if (handle_comma_assigns(expr))
377 break;
378 if (handle_postop_assigns(expr))
379 break;
381 __split_expr(expr->right);
382 if (outside_of_function())
383 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
384 else
385 __pass_to_client(expr, ASSIGNMENT_HOOK);
387 __fake_struct_member_assignments(expr);
389 tmp = strip_expr(expr->right);
390 if (expr->op == '=' && tmp->type == EXPR_CALL) {
391 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
392 if (!is_fake_call(tmp))
393 __pass_to_client(tmp, FUNCTION_CALL_HOOK_AFTER);
395 if (get_macro_name(tmp->pos) &&
396 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
397 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
398 __split_expr(expr->left);
399 break;
401 case EXPR_DEREF:
402 set_parent_expr(expr->deref, expr);
404 __pass_to_client(expr, DEREF_HOOK);
405 __split_expr(expr->deref);
406 break;
407 case EXPR_SLICE:
408 set_parent_expr(expr->base, expr);
410 __split_expr(expr->base);
411 break;
412 case EXPR_CAST:
413 case EXPR_FORCE_CAST:
414 set_parent_expr(expr->cast_expression, expr);
416 __pass_to_client(expr, CAST_HOOK);
417 __split_expr(expr->cast_expression);
418 break;
419 case EXPR_SIZEOF:
420 if (expr->cast_expression)
421 __pass_to_client(strip_parens(expr->cast_expression),
422 SIZEOF_HOOK);
423 break;
424 case EXPR_OFFSETOF:
425 case EXPR_ALIGNOF:
426 evaluate_expression(expr);
427 break;
428 case EXPR_CONDITIONAL:
429 case EXPR_SELECT:
430 set_parent_expr(expr->conditional, expr);
431 set_parent_expr(expr->cond_true, expr);
432 set_parent_expr(expr->cond_false, expr);
434 if (known_condition_true(expr->conditional)) {
435 __split_expr(expr->cond_true);
436 break;
438 if (known_condition_false(expr->conditional)) {
439 __split_expr(expr->cond_false);
440 break;
442 __pass_to_client(expr, SELECT_HOOK);
443 __split_whole_condition(expr->conditional);
444 __split_expr(expr->cond_true);
445 __push_true_states();
446 __use_false_states();
447 __split_expr(expr->cond_false);
448 __merge_true_states();
449 break;
450 case EXPR_CALL:
451 set_parent_expr(expr->fn, expr);
453 if (sym_name_is("__builtin_constant_p", expr->fn))
454 break;
455 split_expr_list(expr->args, expr);
456 __split_expr(expr->fn);
457 if (is_inline_func(expr->fn))
458 add_inline_function(expr->fn->symbol);
459 if (inlinable(expr->fn))
460 __inline_call = 1;
461 __process_post_op_stack();
462 __pass_to_client(expr, FUNCTION_CALL_HOOK);
463 __inline_call = 0;
464 if (inlinable(expr->fn)) {
465 parse_inline(expr);
467 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
468 if (!is_assigned_call(expr))
469 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER);
470 if (is_noreturn_func(expr->fn))
471 nullify_path();
472 break;
473 case EXPR_INITIALIZER:
474 split_expr_list(expr->expr_list, expr);
475 break;
476 case EXPR_IDENTIFIER:
477 set_parent_expr(expr->ident_expression, expr);
478 __split_expr(expr->ident_expression);
479 break;
480 case EXPR_INDEX:
481 set_parent_expr(expr->idx_expression, expr);
482 __split_expr(expr->idx_expression);
483 break;
484 case EXPR_POS:
485 set_parent_expr(expr->init_expr, expr);
486 __split_expr(expr->init_expr);
487 break;
488 case EXPR_SYMBOL:
489 __pass_to_client(expr, SYM_HOOK);
490 break;
491 case EXPR_STRING:
492 __pass_to_client(expr, STRING_HOOK);
493 break;
494 default:
495 break;
497 pop_expression(&big_expression_stack);
500 static int is_forever_loop(struct statement *stmt)
502 struct expression *expr;
504 expr = strip_expr(stmt->iterator_pre_condition);
505 if (!expr)
506 expr = stmt->iterator_post_condition;
507 if (!expr) {
508 /* this is a for(;;) loop... */
509 return 1;
512 if (expr->type == EXPR_VALUE && expr->value == 1)
513 return 1;
515 return 0;
518 static int loop_num;
519 static char *get_loop_name(int num)
521 char buf[256];
523 snprintf(buf, 255, "-loop%d", num);
524 buf[255] = '\0';
525 return alloc_sname(buf);
529 * Pre Loops are while and for loops.
531 static void handle_pre_loop(struct statement *stmt)
533 int once_through; /* we go through the loop at least once */
534 struct sm_state *extra_sm = NULL;
535 int unchanged = 0;
536 char *loop_name;
537 struct stree *stree = NULL;
538 struct sm_state *sm = NULL;
540 loop_name = get_loop_name(loop_num);
541 loop_num++;
543 __split_stmt(stmt->iterator_pre_statement);
544 __prev_stmt = stmt->iterator_pre_statement;
546 once_through = implied_condition_true(stmt->iterator_pre_condition);
548 loop_count++;
549 __push_continues();
550 __push_breaks();
552 __merge_gotos(loop_name);
554 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
555 __in_pre_condition++;
556 __pass_to_client(stmt, PRELOOP_HOOK);
557 __split_whole_condition(stmt->iterator_pre_condition);
558 __in_pre_condition--;
559 FOR_EACH_SM(stree, sm) {
560 set_state(sm->owner, sm->name, sm->sym, sm->state);
561 } END_FOR_EACH_SM(sm);
562 free_stree(&stree);
563 if (extra_sm)
564 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
566 if (option_assume_loops)
567 once_through = 1;
569 __split_stmt(stmt->iterator_statement);
570 if (is_forever_loop(stmt)) {
571 __merge_continues();
572 __save_gotos(loop_name);
574 __push_fake_cur_stree();
575 __split_stmt(stmt->iterator_post_statement);
576 stree = __pop_fake_cur_stree();
578 __discard_false_states();
579 __use_breaks();
581 if (!__path_is_null())
582 __merge_stree_into_cur(stree);
583 free_stree(&stree);
584 } else {
585 __merge_continues();
586 unchanged = __iterator_unchanged(extra_sm);
587 __split_stmt(stmt->iterator_post_statement);
588 __prev_stmt = stmt->iterator_post_statement;
589 __cur_stmt = stmt;
591 __save_gotos(loop_name);
592 __in_pre_condition++;
593 __split_whole_condition(stmt->iterator_pre_condition);
594 __in_pre_condition--;
595 nullify_path();
596 __merge_false_states();
597 if (once_through)
598 __discard_false_states();
599 else
600 __merge_false_states();
602 if (extra_sm && unchanged)
603 __extra_pre_loop_hook_after(extra_sm,
604 stmt->iterator_post_statement,
605 stmt->iterator_pre_condition);
606 __merge_breaks();
608 loop_count--;
612 * Post loops are do {} while();
614 static void handle_post_loop(struct statement *stmt)
616 char *loop_name;
618 loop_name = get_loop_name(loop_num);
619 loop_num++;
620 loop_count++;
622 __push_continues();
623 __push_breaks();
624 __merge_gotos(loop_name);
625 __split_stmt(stmt->iterator_statement);
626 __merge_continues();
627 if (!is_zero(stmt->iterator_post_condition))
628 __save_gotos(loop_name);
630 if (is_forever_loop(stmt)) {
631 __use_breaks();
632 } else {
633 __split_whole_condition(stmt->iterator_post_condition);
634 __use_false_states();
635 __merge_breaks();
637 loop_count--;
640 static int empty_statement(struct statement *stmt)
642 if (!stmt)
643 return 0;
644 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
645 return 1;
646 return 0;
649 static int last_stmt_on_same_line(void)
651 struct statement *stmt;
652 int i = 0;
654 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
655 if (!i++)
656 continue;
657 if (stmt->pos.line == get_lineno())
658 return 1;
659 return 0;
660 } END_FOR_EACH_PTR_REVERSE(stmt);
661 return 0;
664 static void split_asm_constraints(struct expression_list *expr_list)
666 struct expression *expr;
667 int state = 0;
669 FOR_EACH_PTR(expr_list, expr) {
670 switch (state) {
671 case 0: /* identifier */
672 case 1: /* constraint */
673 state++;
674 continue;
675 case 2: /* expression */
676 state = 0;
677 __split_expr(expr);
678 continue;
680 } END_FOR_EACH_PTR(expr);
683 static int is_case_val(struct statement *stmt, sval_t sval)
685 sval_t case_sval;
687 if (stmt->type != STMT_CASE)
688 return 0;
689 if (!stmt->case_expression) {
690 __set_default();
691 return 1;
693 if (!get_value(stmt->case_expression, &case_sval))
694 return 0;
695 if (case_sval.value == sval.value)
696 return 1;
697 return 0;
700 static void split_known_switch(struct statement *stmt, sval_t sval)
702 struct statement *tmp;
704 __split_expr(stmt->switch_expression);
706 push_expression(&switch_expr_stack, stmt->switch_expression);
707 __save_switch_states(top_expression(switch_expr_stack));
708 nullify_path();
709 __push_default();
710 __push_breaks();
712 stmt = stmt->switch_statement;
714 __push_scope_hooks();
715 FOR_EACH_PTR(stmt->stmts, tmp) {
716 __smatch_lineno = tmp->pos.line;
717 if (is_case_val(tmp, sval)) {
718 __merge_switches(top_expression(switch_expr_stack),
719 stmt->case_expression, stmt->case_to);
720 __pass_case_to_client(top_expression(switch_expr_stack),
721 stmt->case_expression);
723 if (__path_is_null())
724 continue;
725 __split_stmt(tmp);
726 if (__path_is_null()) {
727 __set_default();
728 goto out;
730 } END_FOR_EACH_PTR(tmp);
731 out:
732 __call_scope_hooks();
733 if (!__pop_default())
734 __merge_switches(top_expression(switch_expr_stack),
735 NULL, NULL);
736 __discard_switches();
737 __merge_breaks();
738 pop_expression(&switch_expr_stack);
741 static int taking_too_long(void)
743 int ms;
745 ms = ms_since(&fn_start_time);
746 if (ms > 1000 * 60 * 5) /* five minutes */
747 return 1;
748 return 0;
751 static int is_last_stmt(struct statement *cur_stmt)
753 struct symbol *fn = get_base_type(cur_func_sym);
754 struct statement *stmt;
756 if (!fn)
757 return 0;
758 stmt = fn->stmt;
759 if (!stmt)
760 stmt = fn->inline_stmt;
761 if (!stmt || stmt->type != STMT_COMPOUND)
762 return 0;
763 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
764 if (stmt && stmt->type == STMT_LABEL)
765 stmt = stmt->label_statement;
766 if (stmt == cur_stmt)
767 return 1;
768 return 0;
771 static void handle_backward_goto(struct statement *goto_stmt)
773 const char *goto_name, *label_name;
774 struct statement *func_stmt;
775 struct symbol *base_type = get_base_type(cur_func_sym);
776 struct statement *tmp;
777 int found = 0;
779 if (!option_info)
780 return;
781 if (last_goto_statement_handled)
782 return;
783 last_goto_statement_handled = 1;
785 if (!goto_stmt->goto_label ||
786 goto_stmt->goto_label->type != SYM_LABEL ||
787 !goto_stmt->goto_label->ident)
788 return;
789 goto_name = goto_stmt->goto_label->ident->name;
791 func_stmt = base_type->stmt;
792 if (!func_stmt)
793 func_stmt = base_type->inline_stmt;
794 if (!func_stmt)
795 return;
796 if (func_stmt->type != STMT_COMPOUND)
797 return;
799 FOR_EACH_PTR(func_stmt->stmts, tmp) {
800 if (!found) {
801 if (tmp->type != STMT_LABEL)
802 continue;
803 if (!tmp->label_identifier ||
804 tmp->label_identifier->type != SYM_LABEL ||
805 !tmp->label_identifier->ident)
806 continue;
807 label_name = tmp->label_identifier->ident->name;
808 if (strcmp(goto_name, label_name) != 0)
809 continue;
810 found = 1;
812 __split_stmt(tmp);
813 } END_FOR_EACH_PTR(tmp);
816 static void fake_a_return(void)
818 struct symbol *return_type;
820 nullify_path();
821 __unnullify_path();
823 return_type = get_real_base_type(cur_func_sym);
824 return_type = get_real_base_type(return_type);
825 if (return_type != &void_ctype) {
826 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
827 nullify_path();
831 static void split_compound(struct statement *stmt)
833 struct statement *prev = NULL;
834 struct statement *cur = NULL;
835 struct statement *next;
837 __push_scope_hooks();
839 FOR_EACH_PTR(stmt->stmts, next) {
840 /* just set them all ahead of time */
841 set_parent_stmt(next, stmt);
843 if (cur) {
844 __prev_stmt = prev;
845 __next_stmt = next;
846 __cur_stmt = cur;
847 __split_stmt(cur);
849 prev = cur;
850 cur = next;
851 } END_FOR_EACH_PTR(next);
852 if (cur) {
853 __prev_stmt = prev;
854 __cur_stmt = cur;
855 __next_stmt = NULL;
856 __split_stmt(cur);
859 __call_scope_hooks();
862 void __split_label_stmt(struct statement *stmt)
864 if (stmt->label_identifier &&
865 stmt->label_identifier->type == SYM_LABEL &&
866 stmt->label_identifier->ident) {
867 loop_count |= 0x80000000;
868 __merge_gotos(stmt->label_identifier->ident->name);
872 void __split_stmt(struct statement *stmt)
874 sval_t sval;
876 if (!stmt)
877 goto out;
879 if (__bail_on_rest_of_function)
880 return;
882 if (out_of_memory() || taking_too_long()) {
884 __bail_on_rest_of_function = 1;
885 sm_msg("Function too hairy. Giving up.");
886 fake_a_return();
887 final_pass = 0; /* turn off sm_msg() from here */
888 return;
891 add_ptr_list(&big_statement_stack, stmt);
892 free_expression_stack(&big_expression_stack);
893 set_position(stmt->pos);
894 __pass_to_client(stmt, STMT_HOOK);
896 switch (stmt->type) {
897 case STMT_DECLARATION:
898 split_declaration(stmt->declaration);
899 break;
900 case STMT_RETURN:
901 __split_expr(stmt->ret_value);
902 __pass_to_client(stmt->ret_value, RETURN_HOOK);
903 __process_post_op_stack();
904 nullify_path();
905 break;
906 case STMT_EXPRESSION:
907 __split_expr(stmt->expression);
908 break;
909 case STMT_COMPOUND:
910 split_compound(stmt);
911 break;
912 case STMT_IF:
913 set_parent_stmt(stmt->if_true, stmt);
914 set_parent_stmt(stmt->if_false, stmt);
916 if (known_condition_true(stmt->if_conditional)) {
917 __split_stmt(stmt->if_true);
918 break;
920 if (known_condition_false(stmt->if_conditional)) {
921 __split_stmt(stmt->if_false);
922 break;
924 __split_whole_condition(stmt->if_conditional);
925 __split_stmt(stmt->if_true);
926 if (empty_statement(stmt->if_true) &&
927 last_stmt_on_same_line() &&
928 !get_macro_name(stmt->if_true->pos))
929 sm_msg("warn: if();");
930 __push_true_states();
931 __use_false_states();
932 __split_stmt(stmt->if_false);
933 __merge_true_states();
934 break;
935 case STMT_ITERATOR:
936 set_parent_stmt(stmt->iterator_pre_statement, stmt);
937 set_parent_stmt(stmt->iterator_statement, stmt);
938 set_parent_stmt(stmt->iterator_post_statement, stmt);
940 if (stmt->iterator_pre_condition)
941 handle_pre_loop(stmt);
942 else if (stmt->iterator_post_condition)
943 handle_post_loop(stmt);
944 else {
945 // these are for(;;) type loops.
946 handle_pre_loop(stmt);
948 break;
949 case STMT_SWITCH:
950 set_parent_stmt(stmt->switch_statement, stmt);
952 if (get_value(stmt->switch_expression, &sval)) {
953 split_known_switch(stmt, sval);
954 break;
956 __split_expr(stmt->switch_expression);
957 push_expression(&switch_expr_stack, stmt->switch_expression);
958 __save_switch_states(top_expression(switch_expr_stack));
959 nullify_path();
960 __push_default();
961 __push_breaks();
962 __split_stmt(stmt->switch_statement);
963 if (!__pop_default())
964 __merge_switches(top_expression(switch_expr_stack),
965 NULL, NULL);
966 __discard_switches();
967 __merge_breaks();
968 pop_expression(&switch_expr_stack);
969 break;
970 case STMT_CASE:
971 __merge_switches(top_expression(switch_expr_stack),
972 stmt->case_expression, stmt->case_to);
973 __pass_case_to_client(top_expression(switch_expr_stack),
974 stmt->case_expression);
975 if (!stmt->case_expression)
976 __set_default();
977 __split_expr(stmt->case_expression);
978 __split_expr(stmt->case_to);
979 __split_stmt(stmt->case_statement);
980 break;
981 case STMT_LABEL:
982 __split_label_stmt(stmt);
983 __split_stmt(stmt->label_statement);
984 break;
985 case STMT_GOTO:
986 __split_expr(stmt->goto_expression);
987 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
988 if (!strcmp(stmt->goto_label->ident->name, "break")) {
989 __process_breaks();
990 } else if (!strcmp(stmt->goto_label->ident->name,
991 "continue")) {
992 __process_continues();
994 } else if (stmt->goto_label &&
995 stmt->goto_label->type == SYM_LABEL &&
996 stmt->goto_label->ident) {
997 __save_gotos(stmt->goto_label->ident->name);
999 nullify_path();
1000 if (is_last_stmt(stmt))
1001 handle_backward_goto(stmt);
1002 break;
1003 case STMT_NONE:
1004 break;
1005 case STMT_ASM:
1006 __pass_to_client(stmt, ASM_HOOK);
1007 __split_expr(stmt->asm_string);
1008 split_asm_constraints(stmt->asm_outputs);
1009 split_asm_constraints(stmt->asm_inputs);
1010 split_asm_constraints(stmt->asm_clobbers);
1011 break;
1012 case STMT_CONTEXT:
1013 break;
1014 case STMT_RANGE:
1015 __split_expr(stmt->range_expression);
1016 __split_expr(stmt->range_low);
1017 __split_expr(stmt->range_high);
1018 break;
1020 __pass_to_client(stmt, STMT_HOOK_AFTER);
1021 out:
1022 __process_post_op_stack();
1025 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1027 struct expression *expr;
1029 FOR_EACH_PTR(expr_list, expr) {
1030 set_parent_expr(expr, parent);
1031 __split_expr(expr);
1032 __process_post_op_stack();
1033 } END_FOR_EACH_PTR(expr);
1036 static void split_sym(struct symbol *sym)
1038 if (!sym)
1039 return;
1040 if (!(sym->namespace & NS_SYMBOL))
1041 return;
1043 __split_stmt(sym->stmt);
1044 __split_expr(sym->array_size);
1045 split_symlist(sym->arguments);
1046 split_symlist(sym->symbol_list);
1047 __split_stmt(sym->inline_stmt);
1048 split_symlist(sym->inline_symbol_list);
1051 static void split_symlist(struct symbol_list *sym_list)
1053 struct symbol *sym;
1055 FOR_EACH_PTR(sym_list, sym) {
1056 split_sym(sym);
1057 } END_FOR_EACH_PTR(sym);
1060 typedef void (fake_cb)(struct expression *expr);
1062 static int member_to_number(struct expression *expr, struct ident *member)
1064 struct symbol *type, *tmp;
1065 char *name;
1066 int i;
1068 if (!member)
1069 return -1;
1070 name = member->name;
1072 type = get_type(expr);
1073 if (!type || type->type != SYM_STRUCT)
1074 return -1;
1076 i = -1;
1077 FOR_EACH_PTR(type->symbol_list, tmp) {
1078 i++;
1079 if (!tmp->ident)
1080 continue;
1081 if (strcmp(name, tmp->ident->name) == 0)
1082 return i;
1083 } END_FOR_EACH_PTR(tmp);
1084 return -1;
1087 static struct ident *number_to_member(struct expression *expr, int num)
1089 struct symbol *type, *member;
1090 int i = 0;
1092 type = get_type(expr);
1093 if (!type || type->type != SYM_STRUCT)
1094 return NULL;
1096 FOR_EACH_PTR(type->symbol_list, member) {
1097 if (i == num)
1098 return member->ident;
1099 i++;
1100 } END_FOR_EACH_PTR(member);
1101 return NULL;
1104 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1106 struct member_set {
1107 struct ident *ident;
1108 int set;
1111 static struct member_set *alloc_member_set(struct symbol *type)
1113 struct member_set *member_set;
1114 struct symbol *member;
1115 int member_count;
1116 int member_idx;
1118 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1119 member_set = malloc(member_count * sizeof(*member_set));
1120 member_idx = 0;
1121 FOR_EACH_PTR(type->symbol_list, member) {
1122 member_set[member_idx].ident = member->ident;
1123 member_set[member_idx].set = 0;
1124 member_idx++;
1125 } END_FOR_EACH_PTR(member);
1127 return member_set;
1130 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1132 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1133 int i;
1135 for (i = 0; i < member_count; i++) {
1136 if (member_set[i].ident == ident) {
1137 member_set[i].set = 1;
1138 return;
1141 // crap. this is buggy.
1142 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1145 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1147 struct expression *edge_member, *assign;
1148 struct symbol *base = get_real_base_type(member);
1149 struct symbol *tmp;
1151 if (member->ident)
1152 expr = member_expression(expr, '.', member->ident);
1154 FOR_EACH_PTR(base->symbol_list, tmp) {
1155 struct symbol *type;
1157 type = get_real_base_type(tmp);
1158 if (!type)
1159 continue;
1161 if (tmp->ident) {
1162 edge_member = member_expression(expr, '.', tmp->ident);
1163 if (get_state_expr(SMATCH_EXTRA, edge_member))
1164 continue;
1167 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1168 set_inner_struct_members(expr, tmp);
1169 continue;
1172 if (!tmp->ident)
1173 continue;
1175 assign = assign_expression(edge_member, zero_expr());
1176 __split_expr(assign);
1177 } END_FOR_EACH_PTR(tmp);
1182 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1184 struct symbol *tmp;
1185 struct expression *member = NULL;
1186 struct expression *assign;
1187 int op = '*';
1189 if (expr->type == EXPR_PREOP && expr->op == '&') {
1190 expr = strip_expr(expr->unop);
1191 op = '.';
1194 FOR_EACH_PTR(type->symbol_list, tmp) {
1195 type = get_real_base_type(tmp);
1196 if (!type)
1197 continue;
1199 if (tmp->ident) {
1200 member = member_expression(expr, op, tmp->ident);
1201 if (get_state_expr(SMATCH_EXTRA, member))
1202 continue;
1205 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1206 set_inner_struct_members(expr, tmp);
1207 continue;
1209 if (type->type == SYM_ARRAY)
1210 continue;
1211 if (!tmp->ident)
1212 continue;
1214 assign = assign_expression(member, zero_expr());
1215 __split_expr(assign);
1216 } END_FOR_EACH_PTR(tmp);
1219 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1221 struct expression *deref, *assign, *tmp;
1222 struct symbol *struct_type, *type;
1223 struct ident *member;
1224 int member_idx;
1225 struct member_set *member_set;
1227 struct_type = get_type(symbol);
1228 if (!struct_type ||
1229 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1230 return;
1232 member_set = alloc_member_set(struct_type);
1234 member_idx = 0;
1235 FOR_EACH_PTR(members, tmp) {
1236 member = number_to_member(symbol, member_idx);
1237 while (tmp->type == EXPR_IDENTIFIER) {
1238 member = tmp->expr_ident;
1239 member_idx = member_to_number(symbol, member);
1240 tmp = tmp->ident_expression;
1242 mark_member_as_set(struct_type, member_set, member);
1243 member_idx++;
1244 deref = member_expression(symbol, '.', member);
1245 if (tmp->type == EXPR_INITIALIZER) {
1246 type = get_type(deref);
1247 if (type && type->type == SYM_ARRAY)
1248 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1249 else
1250 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1251 } else {
1252 assign = assign_expression(deref, tmp);
1253 fake_cb(assign);
1255 } END_FOR_EACH_PTR(tmp);
1257 set_unset_to_zero(struct_type, symbol);
1260 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1262 fake_member_assigns_helper(symbol_expression(sym),
1263 sym->initializer->expr_list, fake_cb);
1266 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1268 struct expression *offset, *binop, *assign, *tmp;
1269 struct symbol *type;
1270 int idx;
1272 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1273 return;
1275 idx = 0;
1276 FOR_EACH_PTR(expr_list, tmp) {
1277 if (tmp->type == EXPR_INDEX) {
1278 if (tmp->idx_from != tmp->idx_to)
1279 return;
1280 idx = tmp->idx_from;
1281 if (!tmp->idx_expression)
1282 goto next;
1283 tmp = tmp->idx_expression;
1285 offset = value_expr(idx);
1286 binop = array_element_expression(array, offset);
1287 if (tmp->type == EXPR_INITIALIZER) {
1288 type = get_type(binop);
1289 if (type && type->type == SYM_ARRAY)
1290 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1291 else
1292 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1293 } else {
1294 assign = assign_expression(binop, tmp);
1295 fake_cb(assign);
1297 next:
1298 idx++;
1299 } END_FOR_EACH_PTR(tmp);
1302 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1304 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1307 static void fake_assign_expr(struct symbol *sym)
1309 struct expression *assign, *symbol;
1311 symbol = symbol_expression(sym);
1312 assign = assign_expression(symbol, sym->initializer);
1313 __split_expr(assign);
1316 static void call_split_expr(struct expression *expr)
1318 __split_expr(expr);
1321 static void do_initializer_stuff(struct symbol *sym)
1323 if (!sym->initializer)
1324 return;
1326 if (sym->initializer->type == EXPR_INITIALIZER) {
1327 if (get_real_base_type(sym)->type == SYM_ARRAY)
1328 fake_element_assigns(sym, call_split_expr);
1329 else
1330 fake_member_assigns(sym, call_split_expr);
1331 } else {
1332 fake_assign_expr(sym);
1336 static void split_declaration(struct symbol_list *sym_list)
1338 struct symbol *sym;
1340 FOR_EACH_PTR(sym_list, sym) {
1341 __pass_to_client(sym, DECLARATION_HOOK);
1342 do_initializer_stuff(sym);
1343 split_sym(sym);
1344 } END_FOR_EACH_PTR(sym);
1347 static void call_global_assign_hooks(struct expression *assign)
1349 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1352 static void fake_global_assign(struct symbol *sym)
1354 struct expression *assign, *symbol;
1356 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1357 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1358 fake_element_assigns(sym, call_global_assign_hooks);
1359 } else if (sym->initializer) {
1360 symbol = symbol_expression(sym);
1361 assign = assign_expression(symbol, sym->initializer);
1362 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1363 } else {
1364 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1366 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1367 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1368 fake_member_assigns(sym, call_global_assign_hooks);
1369 } else if (sym->initializer) {
1370 symbol = symbol_expression(sym);
1371 assign = assign_expression(symbol, sym->initializer);
1372 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1373 } else {
1374 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1376 } else {
1377 symbol = symbol_expression(sym);
1378 if (sym->initializer)
1379 assign = assign_expression(symbol, sym->initializer);
1380 else
1381 assign = assign_expression(symbol, zero_expr());
1382 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1386 static void start_function_definition(struct symbol *sym)
1388 __in_function_def = 1;
1389 __pass_to_client(sym, FUNC_DEF_HOOK);
1390 __in_function_def = 0;
1391 __pass_to_client(sym, AFTER_DEF_HOOK);
1395 static void split_function(struct symbol *sym)
1397 struct symbol *base_type = get_base_type(sym);
1399 if (!base_type->stmt && !base_type->inline_stmt)
1400 return;
1402 gettimeofday(&fn_start_time, NULL);
1403 cur_func_sym = sym;
1404 if (sym->ident)
1405 cur_func = sym->ident->name;
1406 __smatch_lineno = sym->pos.line;
1407 loop_count = 0;
1408 last_goto_statement_handled = 0;
1409 sm_debug("new function: %s\n", cur_func);
1410 __stree_id = 0;
1411 if (option_two_passes) {
1412 __unnullify_path();
1413 loop_num = 0;
1414 final_pass = 0;
1415 start_function_definition(sym);
1416 __split_stmt(base_type->stmt);
1417 __split_stmt(base_type->inline_stmt);
1418 nullify_path();
1420 __unnullify_path();
1421 loop_num = 0;
1422 final_pass = 1;
1423 start_function_definition(sym);
1424 __split_stmt(base_type->stmt);
1425 __split_stmt(base_type->inline_stmt);
1426 __pass_to_client(sym, END_FUNC_HOOK);
1427 __pass_to_client(sym, AFTER_FUNC_HOOK);
1429 clear_all_states();
1430 cur_func_sym = NULL;
1431 cur_func = NULL;
1432 free_data_info_allocs();
1433 free_expression_stack(&switch_expr_stack);
1434 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1435 __bail_on_rest_of_function = 0;
1438 static void parse_inline(struct expression *call)
1440 struct symbol *base_type;
1441 int loop_num_bak = loop_num;
1442 int loop_count_bak = loop_count;
1443 int final_pass_bak = final_pass;
1444 char *cur_func_bak = cur_func;
1445 struct statement_list *big_statement_stack_bak = big_statement_stack;
1446 struct expression_list *big_expression_stack_bak = big_expression_stack;
1447 struct expression_list *big_condition_stack_bak = big_condition_stack;
1448 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1449 struct symbol *cur_func_sym_bak = cur_func_sym;
1451 __pass_to_client(call, INLINE_FN_START);
1452 final_pass = 0; /* don't print anything */
1453 __inline_fn = call;
1455 base_type = get_base_type(call->fn->symbol);
1456 cur_func_sym = call->fn->symbol;
1457 if (call->fn->symbol->ident)
1458 cur_func = call->fn->symbol->ident->name;
1459 else
1460 cur_func = NULL;
1461 set_position(call->fn->symbol->pos);
1463 save_all_states();
1464 big_statement_stack = NULL;
1465 big_expression_stack = NULL;
1466 big_condition_stack = NULL;
1467 switch_expr_stack = NULL;
1469 sm_debug("inline function: %s\n", cur_func);
1470 __unnullify_path();
1471 loop_num = 0;
1472 loop_count = 0;
1473 start_function_definition(call->fn->symbol);
1474 __split_stmt(base_type->stmt);
1475 __split_stmt(base_type->inline_stmt);
1476 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1477 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1479 free_expression_stack(&switch_expr_stack);
1480 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1481 nullify_path();
1482 free_goto_stack();
1484 loop_num = loop_num_bak;
1485 loop_count = loop_count_bak;
1486 final_pass = final_pass_bak;
1487 cur_func_sym = cur_func_sym_bak;
1488 cur_func = cur_func_bak;
1489 big_statement_stack = big_statement_stack_bak;
1490 big_expression_stack = big_expression_stack_bak;
1491 big_condition_stack = big_condition_stack_bak;
1492 switch_expr_stack = switch_expr_stack_bak;
1494 restore_all_states();
1495 set_position(call->pos);
1496 __inline_fn = NULL;
1497 __pass_to_client(call, INLINE_FN_END);
1500 static struct symbol_list *inlines_called;
1501 static void add_inline_function(struct symbol *sym)
1503 static struct symbol_list *already_added;
1504 struct symbol *tmp;
1506 FOR_EACH_PTR(already_added, tmp) {
1507 if (tmp == sym)
1508 return;
1509 } END_FOR_EACH_PTR(tmp);
1511 add_ptr_list(&already_added, sym);
1512 add_ptr_list(&inlines_called, sym);
1515 static void process_inlines(void)
1517 struct symbol *tmp;
1519 FOR_EACH_PTR(inlines_called, tmp) {
1520 split_function(tmp);
1521 } END_FOR_EACH_PTR(tmp);
1522 free_ptr_list(&inlines_called);
1525 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1527 struct symbol *sym;
1529 FOR_EACH_PTR_REVERSE(big_list, sym) {
1530 if (!sym->scope)
1531 continue;
1532 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1533 return sym;
1534 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1535 return sym;
1536 } END_FOR_EACH_PTR_REVERSE(sym);
1538 return NULL;
1541 static void split_inlines_in_scope(struct symbol *sym)
1543 struct symbol *base;
1544 struct symbol_list *scope_list;
1545 int stream;
1547 scope_list = sym->scope->symbols;
1548 stream = sym->pos.stream;
1550 /* find the last static symbol in the file */
1551 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1552 if (sym->pos.stream != stream)
1553 continue;
1554 if (sym->type != SYM_NODE)
1555 continue;
1556 base = get_base_type(sym);
1557 if (!base)
1558 continue;
1559 if (base->type != SYM_FN)
1560 continue;
1561 if (!base->inline_stmt)
1562 continue;
1563 add_inline_function(sym);
1564 } END_FOR_EACH_PTR_REVERSE(sym);
1566 process_inlines();
1569 static void split_inlines(struct symbol_list *sym_list)
1571 struct symbol *sym;
1573 sym = get_last_scoped_symbol(sym_list, 0);
1574 if (sym)
1575 split_inlines_in_scope(sym);
1576 sym = get_last_scoped_symbol(sym_list, 1);
1577 if (sym)
1578 split_inlines_in_scope(sym);
1581 static struct stree *clone_estates_perm(struct stree *orig)
1583 struct stree *ret = NULL;
1584 struct sm_state *tmp;
1586 FOR_EACH_SM(orig, tmp) {
1587 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1588 } END_FOR_EACH_SM(tmp);
1590 return ret;
1593 struct position last_pos;
1594 static void split_functions(struct symbol_list *sym_list)
1596 struct symbol *sym;
1598 __unnullify_path();
1599 FOR_EACH_PTR(sym_list, sym) {
1600 set_position(sym->pos);
1601 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1602 __pass_to_client(sym, BASE_HOOK);
1603 fake_global_assign(sym);
1605 } END_FOR_EACH_PTR(sym);
1606 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1607 nullify_path();
1609 FOR_EACH_PTR(sym_list, sym) {
1610 set_position(sym->pos);
1611 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1612 split_function(sym);
1613 process_inlines();
1615 last_pos = sym->pos;
1616 } END_FOR_EACH_PTR(sym);
1617 split_inlines(sym_list);
1618 __pass_to_client(sym_list, END_FILE_HOOK);
1621 void smatch(int argc, char **argv)
1623 struct string_list *filelist = NULL;
1624 struct symbol_list *sym_list;
1625 struct timeval stop, start;
1627 gettimeofday(&start, NULL);
1629 if (argc < 2) {
1630 printf("Usage: smatch [--debug] <filename.c>\n");
1631 exit(1);
1633 sparse_initialize(argc, argv, &filelist);
1634 set_valid_ptr_max();
1635 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1636 if (option_file_output) {
1637 char buf[256];
1639 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1640 sm_outfd = fopen(buf, "w");
1641 if (!sm_outfd) {
1642 printf("Error: Cannot open %s\n", base_file);
1643 exit(1);
1646 sym_list = sparse_keep_tokens(base_file);
1647 split_functions(sym_list);
1648 } END_FOR_EACH_PTR_NOTAG(base_file);
1650 gettimeofday(&stop, NULL);
1652 set_position(last_pos);
1653 if (option_time)
1654 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);