function_hooks: fix a type issue (affects ranged function hooks)
[smatch.git] / smatch_flow.c
blob5f7dddfaf856246de15c9e584a885fe41af9bbe6
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);
393 if (get_macro_name(tmp->pos) &&
394 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
395 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
396 __split_expr(expr->left);
397 break;
399 case EXPR_DEREF:
400 set_parent_expr(expr->deref, expr);
402 __pass_to_client(expr, DEREF_HOOK);
403 __split_expr(expr->deref);
404 break;
405 case EXPR_SLICE:
406 set_parent_expr(expr->base, expr);
408 __split_expr(expr->base);
409 break;
410 case EXPR_CAST:
411 case EXPR_FORCE_CAST:
412 set_parent_expr(expr->cast_expression, expr);
414 __pass_to_client(expr, CAST_HOOK);
415 __split_expr(expr->cast_expression);
416 break;
417 case EXPR_SIZEOF:
418 if (expr->cast_expression)
419 __pass_to_client(strip_parens(expr->cast_expression),
420 SIZEOF_HOOK);
421 break;
422 case EXPR_OFFSETOF:
423 case EXPR_ALIGNOF:
424 evaluate_expression(expr);
425 break;
426 case EXPR_CONDITIONAL:
427 case EXPR_SELECT:
428 set_parent_expr(expr->conditional, expr);
429 set_parent_expr(expr->cond_true, expr);
430 set_parent_expr(expr->cond_false, expr);
432 if (known_condition_true(expr->conditional)) {
433 __split_expr(expr->cond_true);
434 break;
436 if (known_condition_false(expr->conditional)) {
437 __split_expr(expr->cond_false);
438 break;
440 __pass_to_client(expr, SELECT_HOOK);
441 __split_whole_condition(expr->conditional);
442 __split_expr(expr->cond_true);
443 __push_true_states();
444 __use_false_states();
445 __split_expr(expr->cond_false);
446 __merge_true_states();
447 break;
448 case EXPR_CALL:
449 set_parent_expr(expr->fn, expr);
451 if (sym_name_is("__builtin_constant_p", expr->fn))
452 break;
453 split_expr_list(expr->args, expr);
454 __split_expr(expr->fn);
455 if (is_inline_func(expr->fn))
456 add_inline_function(expr->fn->symbol);
457 if (inlinable(expr->fn))
458 __inline_call = 1;
459 __process_post_op_stack();
460 __pass_to_client(expr, FUNCTION_CALL_HOOK);
461 __inline_call = 0;
462 if (inlinable(expr->fn)) {
463 parse_inline(expr);
465 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
466 if (is_noreturn_func(expr->fn))
467 nullify_path();
468 break;
469 case EXPR_INITIALIZER:
470 split_expr_list(expr->expr_list, expr);
471 break;
472 case EXPR_IDENTIFIER:
473 set_parent_expr(expr->ident_expression, expr);
474 __split_expr(expr->ident_expression);
475 break;
476 case EXPR_INDEX:
477 set_parent_expr(expr->idx_expression, expr);
478 __split_expr(expr->idx_expression);
479 break;
480 case EXPR_POS:
481 set_parent_expr(expr->init_expr, expr);
482 __split_expr(expr->init_expr);
483 break;
484 case EXPR_SYMBOL:
485 __pass_to_client(expr, SYM_HOOK);
486 break;
487 case EXPR_STRING:
488 __pass_to_client(expr, STRING_HOOK);
489 break;
490 default:
491 break;
493 pop_expression(&big_expression_stack);
496 static int is_forever_loop(struct statement *stmt)
498 struct expression *expr;
500 expr = strip_expr(stmt->iterator_pre_condition);
501 if (!expr)
502 expr = stmt->iterator_post_condition;
503 if (!expr) {
504 /* this is a for(;;) loop... */
505 return 1;
508 if (expr->type == EXPR_VALUE && expr->value == 1)
509 return 1;
511 return 0;
514 static int loop_num;
515 static char *get_loop_name(int num)
517 char buf[256];
519 snprintf(buf, 255, "-loop%d", num);
520 buf[255] = '\0';
521 return alloc_sname(buf);
525 * Pre Loops are while and for loops.
527 static void handle_pre_loop(struct statement *stmt)
529 int once_through; /* we go through the loop at least once */
530 struct sm_state *extra_sm = NULL;
531 int unchanged = 0;
532 char *loop_name;
533 struct stree *stree = NULL;
534 struct sm_state *sm = NULL;
536 loop_name = get_loop_name(loop_num);
537 loop_num++;
539 __split_stmt(stmt->iterator_pre_statement);
540 __prev_stmt = stmt->iterator_pre_statement;
542 once_through = implied_condition_true(stmt->iterator_pre_condition);
544 loop_count++;
545 __push_continues();
546 __push_breaks();
548 __merge_gotos(loop_name);
550 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
551 __in_pre_condition++;
552 __pass_to_client(stmt, PRELOOP_HOOK);
553 __split_whole_condition(stmt->iterator_pre_condition);
554 __in_pre_condition--;
555 FOR_EACH_SM(stree, sm) {
556 set_state(sm->owner, sm->name, sm->sym, sm->state);
557 } END_FOR_EACH_SM(sm);
558 free_stree(&stree);
559 if (extra_sm)
560 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
562 if (option_assume_loops)
563 once_through = 1;
565 __split_stmt(stmt->iterator_statement);
566 if (is_forever_loop(stmt)) {
567 __merge_continues();
568 __save_gotos(loop_name);
570 __push_fake_cur_stree();
571 __split_stmt(stmt->iterator_post_statement);
572 stree = __pop_fake_cur_stree();
574 __discard_false_states();
575 __use_breaks();
577 if (!__path_is_null())
578 __merge_stree_into_cur(stree);
579 free_stree(&stree);
580 } else {
581 __merge_continues();
582 unchanged = __iterator_unchanged(extra_sm);
583 __split_stmt(stmt->iterator_post_statement);
584 __prev_stmt = stmt->iterator_post_statement;
585 __cur_stmt = stmt;
587 __save_gotos(loop_name);
588 __in_pre_condition++;
589 __split_whole_condition(stmt->iterator_pre_condition);
590 __in_pre_condition--;
591 nullify_path();
592 __merge_false_states();
593 if (once_through)
594 __discard_false_states();
595 else
596 __merge_false_states();
598 if (extra_sm && unchanged)
599 __extra_pre_loop_hook_after(extra_sm,
600 stmt->iterator_post_statement,
601 stmt->iterator_pre_condition);
602 __merge_breaks();
604 loop_count--;
608 * Post loops are do {} while();
610 static void handle_post_loop(struct statement *stmt)
612 char *loop_name;
614 loop_name = get_loop_name(loop_num);
615 loop_num++;
616 loop_count++;
618 __push_continues();
619 __push_breaks();
620 __merge_gotos(loop_name);
621 __split_stmt(stmt->iterator_statement);
622 __merge_continues();
623 if (!is_zero(stmt->iterator_post_condition))
624 __save_gotos(loop_name);
626 if (is_forever_loop(stmt)) {
627 __use_breaks();
628 } else {
629 __split_whole_condition(stmt->iterator_post_condition);
630 __use_false_states();
631 __merge_breaks();
633 loop_count--;
636 static int empty_statement(struct statement *stmt)
638 if (!stmt)
639 return 0;
640 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
641 return 1;
642 return 0;
645 static int last_stmt_on_same_line(void)
647 struct statement *stmt;
648 int i = 0;
650 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
651 if (!i++)
652 continue;
653 if (stmt->pos.line == get_lineno())
654 return 1;
655 return 0;
656 } END_FOR_EACH_PTR_REVERSE(stmt);
657 return 0;
660 static void split_asm_constraints(struct expression_list *expr_list)
662 struct expression *expr;
663 int state = 0;
665 FOR_EACH_PTR(expr_list, expr) {
666 switch (state) {
667 case 0: /* identifier */
668 case 1: /* constraint */
669 state++;
670 continue;
671 case 2: /* expression */
672 state = 0;
673 __split_expr(expr);
674 continue;
676 } END_FOR_EACH_PTR(expr);
679 static int is_case_val(struct statement *stmt, sval_t sval)
681 sval_t case_sval;
683 if (stmt->type != STMT_CASE)
684 return 0;
685 if (!stmt->case_expression) {
686 __set_default();
687 return 1;
689 if (!get_value(stmt->case_expression, &case_sval))
690 return 0;
691 if (case_sval.value == sval.value)
692 return 1;
693 return 0;
696 static struct range_list *get_case_rl(struct expression *switch_expr,
697 struct expression *case_expr,
698 struct expression *case_to)
700 sval_t start, end;
701 struct range_list *rl = NULL;
702 struct symbol *switch_type;
704 switch_type = get_type(switch_expr);
705 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
706 start = sval_cast(switch_type, start);
707 end = sval_cast(switch_type, end);
708 add_range(&rl, start, end);
709 } else if (get_value(case_expr, &start)) {
710 start = sval_cast(switch_type, start);
711 add_range(&rl, start, start);
714 return rl;
717 static void split_known_switch(struct statement *stmt, sval_t sval)
719 struct statement *tmp;
720 struct range_list *rl;
722 __split_expr(stmt->switch_expression);
723 sval = sval_cast(get_type(stmt->switch_expression), sval);
725 push_expression(&switch_expr_stack, stmt->switch_expression);
726 __save_switch_states(top_expression(switch_expr_stack));
727 nullify_path();
728 __push_default();
729 __push_breaks();
731 stmt = stmt->switch_statement;
733 __push_scope_hooks();
734 FOR_EACH_PTR(stmt->stmts, tmp) {
735 __smatch_lineno = tmp->pos.line;
736 if (is_case_val(tmp, sval)) {
737 rl = alloc_rl(sval, sval);
738 __merge_switches(top_expression(switch_expr_stack), rl);
739 __pass_case_to_client(top_expression(switch_expr_stack), rl);
741 if (__path_is_null())
742 continue;
743 __split_stmt(tmp);
744 if (__path_is_null()) {
745 __set_default();
746 goto out;
748 } END_FOR_EACH_PTR(tmp);
749 out:
750 __call_scope_hooks();
751 if (!__pop_default())
752 __merge_switches(top_expression(switch_expr_stack), NULL);
753 __discard_switches();
754 __merge_breaks();
755 pop_expression(&switch_expr_stack);
758 static void split_case(struct statement *stmt)
760 struct range_list *rl = NULL;
762 rl = get_case_rl(top_expression(switch_expr_stack),
763 stmt->case_expression, stmt->case_to);
764 while (stmt->case_statement->type == STMT_CASE) {
765 struct range_list *tmp;
767 tmp = get_case_rl(top_expression(switch_expr_stack),
768 stmt->case_statement->case_expression,
769 stmt->case_statement->case_to);
770 if (!tmp)
771 break;
772 rl = rl_union(rl, tmp);
773 if (!stmt->case_expression)
774 __set_default();
775 stmt = stmt->case_statement;
778 __merge_switches(top_expression(switch_expr_stack), rl);
780 if (!stmt->case_expression)
781 __set_default();
782 __split_stmt(stmt->case_statement);
785 static int taking_too_long(void)
787 int ms;
789 ms = ms_since(&fn_start_time);
790 if (ms > 1000 * 60 * 5) /* five minutes */
791 return 1;
792 return 0;
795 static int is_last_stmt(struct statement *cur_stmt)
797 struct symbol *fn = get_base_type(cur_func_sym);
798 struct statement *stmt;
800 if (!fn)
801 return 0;
802 stmt = fn->stmt;
803 if (!stmt)
804 stmt = fn->inline_stmt;
805 if (!stmt || stmt->type != STMT_COMPOUND)
806 return 0;
807 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
808 if (stmt && stmt->type == STMT_LABEL)
809 stmt = stmt->label_statement;
810 if (stmt == cur_stmt)
811 return 1;
812 return 0;
815 static void handle_backward_goto(struct statement *goto_stmt)
817 const char *goto_name, *label_name;
818 struct statement *func_stmt;
819 struct symbol *base_type = get_base_type(cur_func_sym);
820 struct statement *tmp;
821 int found = 0;
823 if (!option_info)
824 return;
825 if (last_goto_statement_handled)
826 return;
827 last_goto_statement_handled = 1;
829 if (!goto_stmt->goto_label ||
830 goto_stmt->goto_label->type != SYM_LABEL ||
831 !goto_stmt->goto_label->ident)
832 return;
833 goto_name = goto_stmt->goto_label->ident->name;
835 func_stmt = base_type->stmt;
836 if (!func_stmt)
837 func_stmt = base_type->inline_stmt;
838 if (!func_stmt)
839 return;
840 if (func_stmt->type != STMT_COMPOUND)
841 return;
843 FOR_EACH_PTR(func_stmt->stmts, tmp) {
844 if (!found) {
845 if (tmp->type != STMT_LABEL)
846 continue;
847 if (!tmp->label_identifier ||
848 tmp->label_identifier->type != SYM_LABEL ||
849 !tmp->label_identifier->ident)
850 continue;
851 label_name = tmp->label_identifier->ident->name;
852 if (strcmp(goto_name, label_name) != 0)
853 continue;
854 found = 1;
856 __split_stmt(tmp);
857 } END_FOR_EACH_PTR(tmp);
860 static void fake_a_return(void)
862 struct symbol *return_type;
864 nullify_path();
865 __unnullify_path();
867 return_type = get_real_base_type(cur_func_sym);
868 return_type = get_real_base_type(return_type);
869 if (return_type != &void_ctype) {
870 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
871 nullify_path();
875 static void fake_an_empty_default(struct position pos)
877 static struct statement none = {};
879 none.pos = pos;
880 none.type = STMT_NONE;
881 __merge_switches(top_expression(switch_expr_stack), NULL);
882 __split_stmt(&none);
885 static void split_compound(struct statement *stmt)
887 struct statement *prev = NULL;
888 struct statement *cur = NULL;
889 struct statement *next;
891 __push_scope_hooks();
893 FOR_EACH_PTR(stmt->stmts, next) {
894 /* just set them all ahead of time */
895 set_parent_stmt(next, stmt);
897 if (cur) {
898 __prev_stmt = prev;
899 __next_stmt = next;
900 __cur_stmt = cur;
901 __split_stmt(cur);
903 prev = cur;
904 cur = next;
905 } END_FOR_EACH_PTR(next);
906 if (cur) {
907 __prev_stmt = prev;
908 __cur_stmt = cur;
909 __next_stmt = NULL;
910 __split_stmt(cur);
913 __call_scope_hooks();
916 void __split_label_stmt(struct statement *stmt)
918 if (stmt->label_identifier &&
919 stmt->label_identifier->type == SYM_LABEL &&
920 stmt->label_identifier->ident) {
921 loop_count |= 0x80000000;
922 __merge_gotos(stmt->label_identifier->ident->name);
926 void __split_stmt(struct statement *stmt)
928 sval_t sval;
930 if (!stmt)
931 goto out;
933 if (__bail_on_rest_of_function)
934 return;
936 if (out_of_memory() || taking_too_long()) {
938 __bail_on_rest_of_function = 1;
939 final_pass = 1;
940 sm_msg("Function too hairy. Giving up.");
941 fake_a_return();
942 final_pass = 0; /* turn off sm_msg() from here */
943 return;
946 add_ptr_list(&big_statement_stack, stmt);
947 free_expression_stack(&big_expression_stack);
948 set_position(stmt->pos);
949 __pass_to_client(stmt, STMT_HOOK);
951 switch (stmt->type) {
952 case STMT_DECLARATION:
953 split_declaration(stmt->declaration);
954 break;
955 case STMT_RETURN:
956 __split_expr(stmt->ret_value);
957 __pass_to_client(stmt->ret_value, RETURN_HOOK);
958 __process_post_op_stack();
959 nullify_path();
960 break;
961 case STMT_EXPRESSION:
962 __split_expr(stmt->expression);
963 break;
964 case STMT_COMPOUND:
965 split_compound(stmt);
966 break;
967 case STMT_IF:
968 set_parent_stmt(stmt->if_true, stmt);
969 set_parent_stmt(stmt->if_false, stmt);
971 if (known_condition_true(stmt->if_conditional)) {
972 __split_stmt(stmt->if_true);
973 break;
975 if (known_condition_false(stmt->if_conditional)) {
976 __split_stmt(stmt->if_false);
977 break;
979 __split_whole_condition(stmt->if_conditional);
980 __split_stmt(stmt->if_true);
981 if (empty_statement(stmt->if_true) &&
982 last_stmt_on_same_line() &&
983 !get_macro_name(stmt->if_true->pos))
984 sm_msg("warn: if();");
985 __push_true_states();
986 __use_false_states();
987 __split_stmt(stmt->if_false);
988 __merge_true_states();
989 break;
990 case STMT_ITERATOR:
991 set_parent_stmt(stmt->iterator_pre_statement, stmt);
992 set_parent_stmt(stmt->iterator_statement, stmt);
993 set_parent_stmt(stmt->iterator_post_statement, stmt);
995 if (stmt->iterator_pre_condition)
996 handle_pre_loop(stmt);
997 else if (stmt->iterator_post_condition)
998 handle_post_loop(stmt);
999 else {
1000 // these are for(;;) type loops.
1001 handle_pre_loop(stmt);
1003 break;
1004 case STMT_SWITCH:
1005 set_parent_stmt(stmt->switch_statement, stmt);
1007 if (get_value(stmt->switch_expression, &sval)) {
1008 split_known_switch(stmt, sval);
1009 break;
1011 __split_expr(stmt->switch_expression);
1012 push_expression(&switch_expr_stack, stmt->switch_expression);
1013 __save_switch_states(top_expression(switch_expr_stack));
1014 nullify_path();
1015 __push_default();
1016 __push_breaks();
1017 __split_stmt(stmt->switch_statement);
1018 if (!__pop_default())
1019 fake_an_empty_default(stmt->pos);
1020 __discard_switches();
1021 __merge_breaks();
1022 pop_expression(&switch_expr_stack);
1023 break;
1024 case STMT_CASE:
1025 split_case(stmt);
1026 break;
1027 case STMT_LABEL:
1028 __split_label_stmt(stmt);
1029 __split_stmt(stmt->label_statement);
1030 break;
1031 case STMT_GOTO:
1032 __split_expr(stmt->goto_expression);
1033 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1034 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1035 __process_breaks();
1036 } else if (!strcmp(stmt->goto_label->ident->name,
1037 "continue")) {
1038 __process_continues();
1040 } else if (stmt->goto_label &&
1041 stmt->goto_label->type == SYM_LABEL &&
1042 stmt->goto_label->ident) {
1043 __save_gotos(stmt->goto_label->ident->name);
1045 nullify_path();
1046 if (is_last_stmt(stmt))
1047 handle_backward_goto(stmt);
1048 break;
1049 case STMT_NONE:
1050 break;
1051 case STMT_ASM:
1052 __pass_to_client(stmt, ASM_HOOK);
1053 __split_expr(stmt->asm_string);
1054 split_asm_constraints(stmt->asm_outputs);
1055 split_asm_constraints(stmt->asm_inputs);
1056 split_asm_constraints(stmt->asm_clobbers);
1057 break;
1058 case STMT_CONTEXT:
1059 break;
1060 case STMT_RANGE:
1061 __split_expr(stmt->range_expression);
1062 __split_expr(stmt->range_low);
1063 __split_expr(stmt->range_high);
1064 break;
1066 __pass_to_client(stmt, STMT_HOOK_AFTER);
1067 out:
1068 __process_post_op_stack();
1071 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1073 struct expression *expr;
1075 FOR_EACH_PTR(expr_list, expr) {
1076 set_parent_expr(expr, parent);
1077 __split_expr(expr);
1078 __process_post_op_stack();
1079 } END_FOR_EACH_PTR(expr);
1082 static void split_sym(struct symbol *sym)
1084 if (!sym)
1085 return;
1086 if (!(sym->namespace & NS_SYMBOL))
1087 return;
1089 __split_stmt(sym->stmt);
1090 __split_expr(sym->array_size);
1091 split_symlist(sym->arguments);
1092 split_symlist(sym->symbol_list);
1093 __split_stmt(sym->inline_stmt);
1094 split_symlist(sym->inline_symbol_list);
1097 static void split_symlist(struct symbol_list *sym_list)
1099 struct symbol *sym;
1101 FOR_EACH_PTR(sym_list, sym) {
1102 split_sym(sym);
1103 } END_FOR_EACH_PTR(sym);
1106 typedef void (fake_cb)(struct expression *expr);
1108 static int member_to_number(struct expression *expr, struct ident *member)
1110 struct symbol *type, *tmp;
1111 char *name;
1112 int i;
1114 if (!member)
1115 return -1;
1116 name = member->name;
1118 type = get_type(expr);
1119 if (!type || type->type != SYM_STRUCT)
1120 return -1;
1122 i = -1;
1123 FOR_EACH_PTR(type->symbol_list, tmp) {
1124 i++;
1125 if (!tmp->ident)
1126 continue;
1127 if (strcmp(name, tmp->ident->name) == 0)
1128 return i;
1129 } END_FOR_EACH_PTR(tmp);
1130 return -1;
1133 static struct ident *number_to_member(struct expression *expr, int num)
1135 struct symbol *type, *member;
1136 int i = 0;
1138 type = get_type(expr);
1139 if (!type || type->type != SYM_STRUCT)
1140 return NULL;
1142 FOR_EACH_PTR(type->symbol_list, member) {
1143 if (i == num)
1144 return member->ident;
1145 i++;
1146 } END_FOR_EACH_PTR(member);
1147 return NULL;
1150 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1152 struct member_set {
1153 struct ident *ident;
1154 int set;
1157 static struct member_set *alloc_member_set(struct symbol *type)
1159 struct member_set *member_set;
1160 struct symbol *member;
1161 int member_count;
1162 int member_idx;
1164 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1165 member_set = malloc(member_count * sizeof(*member_set));
1166 member_idx = 0;
1167 FOR_EACH_PTR(type->symbol_list, member) {
1168 member_set[member_idx].ident = member->ident;
1169 member_set[member_idx].set = 0;
1170 member_idx++;
1171 } END_FOR_EACH_PTR(member);
1173 return member_set;
1176 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1178 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1179 int i;
1181 for (i = 0; i < member_count; i++) {
1182 if (member_set[i].ident == ident) {
1183 member_set[i].set = 1;
1184 return;
1187 // crap. this is buggy.
1188 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1191 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1193 struct expression *edge_member, *assign;
1194 struct symbol *base = get_real_base_type(member);
1195 struct symbol *tmp;
1197 if (member->ident)
1198 expr = member_expression(expr, '.', member->ident);
1200 FOR_EACH_PTR(base->symbol_list, tmp) {
1201 struct symbol *type;
1203 type = get_real_base_type(tmp);
1204 if (!type)
1205 continue;
1207 if (tmp->ident) {
1208 edge_member = member_expression(expr, '.', tmp->ident);
1209 if (get_state_expr(SMATCH_EXTRA, edge_member))
1210 continue;
1213 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1214 set_inner_struct_members(expr, tmp);
1215 continue;
1218 if (!tmp->ident)
1219 continue;
1221 assign = assign_expression(edge_member, zero_expr());
1222 __split_expr(assign);
1223 } END_FOR_EACH_PTR(tmp);
1228 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1230 struct symbol *tmp;
1231 struct expression *member = NULL;
1232 struct expression *assign;
1233 int op = '*';
1235 if (expr->type == EXPR_PREOP && expr->op == '&') {
1236 expr = strip_expr(expr->unop);
1237 op = '.';
1240 FOR_EACH_PTR(type->symbol_list, tmp) {
1241 type = get_real_base_type(tmp);
1242 if (!type)
1243 continue;
1245 if (tmp->ident) {
1246 member = member_expression(expr, op, tmp->ident);
1247 if (get_state_expr(SMATCH_EXTRA, member))
1248 continue;
1251 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1252 set_inner_struct_members(expr, tmp);
1253 continue;
1255 if (type->type == SYM_ARRAY)
1256 continue;
1257 if (!tmp->ident)
1258 continue;
1260 assign = assign_expression(member, zero_expr());
1261 __split_expr(assign);
1262 } END_FOR_EACH_PTR(tmp);
1265 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1267 struct expression *deref, *assign, *tmp;
1268 struct symbol *struct_type, *type;
1269 struct ident *member;
1270 int member_idx;
1271 struct member_set *member_set;
1273 struct_type = get_type(symbol);
1274 if (!struct_type ||
1275 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1276 return;
1278 member_set = alloc_member_set(struct_type);
1280 member_idx = 0;
1281 FOR_EACH_PTR(members, tmp) {
1282 member = number_to_member(symbol, member_idx);
1283 while (tmp->type == EXPR_IDENTIFIER) {
1284 member = tmp->expr_ident;
1285 member_idx = member_to_number(symbol, member);
1286 tmp = tmp->ident_expression;
1288 mark_member_as_set(struct_type, member_set, member);
1289 member_idx++;
1290 deref = member_expression(symbol, '.', member);
1291 if (tmp->type == EXPR_INITIALIZER) {
1292 type = get_type(deref);
1293 if (type && type->type == SYM_ARRAY)
1294 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1295 else
1296 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1297 } else {
1298 assign = assign_expression(deref, tmp);
1299 fake_cb(assign);
1301 } END_FOR_EACH_PTR(tmp);
1303 set_unset_to_zero(struct_type, symbol);
1306 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1308 fake_member_assigns_helper(symbol_expression(sym),
1309 sym->initializer->expr_list, fake_cb);
1312 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1314 struct expression *offset, *binop, *assign, *tmp;
1315 struct symbol *type;
1316 int idx;
1318 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1319 return;
1321 idx = 0;
1322 FOR_EACH_PTR(expr_list, tmp) {
1323 if (tmp->type == EXPR_INDEX) {
1324 if (tmp->idx_from != tmp->idx_to)
1325 return;
1326 idx = tmp->idx_from;
1327 if (!tmp->idx_expression)
1328 goto next;
1329 tmp = tmp->idx_expression;
1331 offset = value_expr(idx);
1332 binop = array_element_expression(array, offset);
1333 if (tmp->type == EXPR_INITIALIZER) {
1334 type = get_type(binop);
1335 if (type && type->type == SYM_ARRAY)
1336 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1337 else
1338 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1339 } else {
1340 assign = assign_expression(binop, tmp);
1341 fake_cb(assign);
1343 next:
1344 idx++;
1345 } END_FOR_EACH_PTR(tmp);
1348 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1350 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1353 static void fake_assign_expr(struct symbol *sym)
1355 struct expression *assign, *symbol;
1357 symbol = symbol_expression(sym);
1358 assign = assign_expression(symbol, sym->initializer);
1359 __split_expr(assign);
1362 static void call_split_expr(struct expression *expr)
1364 __split_expr(expr);
1367 static void do_initializer_stuff(struct symbol *sym)
1369 if (!sym->initializer)
1370 return;
1372 if (sym->initializer->type == EXPR_INITIALIZER) {
1373 if (get_real_base_type(sym)->type == SYM_ARRAY)
1374 fake_element_assigns(sym, call_split_expr);
1375 else
1376 fake_member_assigns(sym, call_split_expr);
1377 } else {
1378 fake_assign_expr(sym);
1382 static void split_declaration(struct symbol_list *sym_list)
1384 struct symbol *sym;
1386 FOR_EACH_PTR(sym_list, sym) {
1387 __pass_to_client(sym, DECLARATION_HOOK);
1388 do_initializer_stuff(sym);
1389 split_sym(sym);
1390 } END_FOR_EACH_PTR(sym);
1393 static void call_global_assign_hooks(struct expression *assign)
1395 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1398 static void fake_global_assign(struct symbol *sym)
1400 struct expression *assign, *symbol;
1402 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1403 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1404 fake_element_assigns(sym, call_global_assign_hooks);
1405 } else if (sym->initializer) {
1406 symbol = symbol_expression(sym);
1407 assign = assign_expression(symbol, sym->initializer);
1408 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1409 } else {
1410 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1412 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1413 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1414 fake_member_assigns(sym, call_global_assign_hooks);
1415 } else if (sym->initializer) {
1416 symbol = symbol_expression(sym);
1417 assign = assign_expression(symbol, sym->initializer);
1418 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1419 } else {
1420 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1422 } else {
1423 symbol = symbol_expression(sym);
1424 if (sym->initializer)
1425 assign = assign_expression(symbol, sym->initializer);
1426 else
1427 assign = assign_expression(symbol, zero_expr());
1428 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1432 static void start_function_definition(struct symbol *sym)
1434 __in_function_def = 1;
1435 __pass_to_client(sym, FUNC_DEF_HOOK);
1436 __in_function_def = 0;
1437 __pass_to_client(sym, AFTER_DEF_HOOK);
1441 static void split_function(struct symbol *sym)
1443 struct symbol *base_type = get_base_type(sym);
1445 if (!base_type->stmt && !base_type->inline_stmt)
1446 return;
1448 gettimeofday(&fn_start_time, NULL);
1449 cur_func_sym = sym;
1450 if (sym->ident)
1451 cur_func = sym->ident->name;
1452 __smatch_lineno = sym->pos.line;
1453 loop_count = 0;
1454 last_goto_statement_handled = 0;
1455 sm_debug("new function: %s\n", cur_func);
1456 __stree_id = 0;
1457 if (option_two_passes) {
1458 __unnullify_path();
1459 loop_num = 0;
1460 final_pass = 0;
1461 start_function_definition(sym);
1462 __split_stmt(base_type->stmt);
1463 __split_stmt(base_type->inline_stmt);
1464 nullify_path();
1466 __unnullify_path();
1467 loop_num = 0;
1468 final_pass = 1;
1469 start_function_definition(sym);
1470 __split_stmt(base_type->stmt);
1471 __split_stmt(base_type->inline_stmt);
1472 __pass_to_client(sym, END_FUNC_HOOK);
1473 __pass_to_client(sym, AFTER_FUNC_HOOK);
1475 clear_all_states();
1476 cur_func_sym = NULL;
1477 cur_func = NULL;
1478 free_data_info_allocs();
1479 free_expression_stack(&switch_expr_stack);
1480 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1481 __bail_on_rest_of_function = 0;
1484 static void parse_inline(struct expression *call)
1486 struct symbol *base_type;
1487 int loop_num_bak = loop_num;
1488 int loop_count_bak = loop_count;
1489 int final_pass_bak = final_pass;
1490 char *cur_func_bak = cur_func;
1491 struct statement_list *big_statement_stack_bak = big_statement_stack;
1492 struct expression_list *big_expression_stack_bak = big_expression_stack;
1493 struct expression_list *big_condition_stack_bak = big_condition_stack;
1494 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1495 struct symbol *cur_func_sym_bak = cur_func_sym;
1497 __pass_to_client(call, INLINE_FN_START);
1498 final_pass = 0; /* don't print anything */
1499 __inline_fn = call;
1501 base_type = get_base_type(call->fn->symbol);
1502 cur_func_sym = call->fn->symbol;
1503 if (call->fn->symbol->ident)
1504 cur_func = call->fn->symbol->ident->name;
1505 else
1506 cur_func = NULL;
1507 set_position(call->fn->symbol->pos);
1509 save_all_states();
1510 big_statement_stack = NULL;
1511 big_expression_stack = NULL;
1512 big_condition_stack = NULL;
1513 switch_expr_stack = NULL;
1515 sm_debug("inline function: %s\n", cur_func);
1516 __unnullify_path();
1517 loop_num = 0;
1518 start_function_definition(call->fn->symbol);
1519 __split_stmt(base_type->stmt);
1520 __split_stmt(base_type->inline_stmt);
1521 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1522 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1524 free_expression_stack(&switch_expr_stack);
1525 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1526 nullify_path();
1527 free_goto_stack();
1529 loop_num = loop_num_bak;
1530 loop_count = loop_count_bak;
1531 final_pass = final_pass_bak;
1532 cur_func_sym = cur_func_sym_bak;
1533 cur_func = cur_func_bak;
1534 big_statement_stack = big_statement_stack_bak;
1535 big_expression_stack = big_expression_stack_bak;
1536 big_condition_stack = big_condition_stack_bak;
1537 switch_expr_stack = switch_expr_stack_bak;
1539 restore_all_states();
1540 set_position(call->pos);
1541 __inline_fn = NULL;
1542 __pass_to_client(call, INLINE_FN_END);
1545 static struct symbol_list *inlines_called;
1546 static void add_inline_function(struct symbol *sym)
1548 static struct symbol_list *already_added;
1549 struct symbol *tmp;
1551 FOR_EACH_PTR(already_added, tmp) {
1552 if (tmp == sym)
1553 return;
1554 } END_FOR_EACH_PTR(tmp);
1556 add_ptr_list(&already_added, sym);
1557 add_ptr_list(&inlines_called, sym);
1560 static void process_inlines(void)
1562 struct symbol *tmp;
1564 FOR_EACH_PTR(inlines_called, tmp) {
1565 split_function(tmp);
1566 } END_FOR_EACH_PTR(tmp);
1567 free_ptr_list(&inlines_called);
1570 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1572 struct symbol *sym;
1574 FOR_EACH_PTR_REVERSE(big_list, sym) {
1575 if (!sym->scope)
1576 continue;
1577 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1578 return sym;
1579 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1580 return sym;
1581 } END_FOR_EACH_PTR_REVERSE(sym);
1583 return NULL;
1586 static void split_inlines_in_scope(struct symbol *sym)
1588 struct symbol *base;
1589 struct symbol_list *scope_list;
1590 int stream;
1592 scope_list = sym->scope->symbols;
1593 stream = sym->pos.stream;
1595 /* find the last static symbol in the file */
1596 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1597 if (sym->pos.stream != stream)
1598 continue;
1599 if (sym->type != SYM_NODE)
1600 continue;
1601 base = get_base_type(sym);
1602 if (!base)
1603 continue;
1604 if (base->type != SYM_FN)
1605 continue;
1606 if (!base->inline_stmt)
1607 continue;
1608 add_inline_function(sym);
1609 } END_FOR_EACH_PTR_REVERSE(sym);
1611 process_inlines();
1614 static void split_inlines(struct symbol_list *sym_list)
1616 struct symbol *sym;
1618 sym = get_last_scoped_symbol(sym_list, 0);
1619 if (sym)
1620 split_inlines_in_scope(sym);
1621 sym = get_last_scoped_symbol(sym_list, 1);
1622 if (sym)
1623 split_inlines_in_scope(sym);
1626 static struct stree *clone_estates_perm(struct stree *orig)
1628 struct stree *ret = NULL;
1629 struct sm_state *tmp;
1631 FOR_EACH_SM(orig, tmp) {
1632 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1633 } END_FOR_EACH_SM(tmp);
1635 return ret;
1638 struct position last_pos;
1639 static void split_functions(struct symbol_list *sym_list)
1641 struct symbol *sym;
1643 __unnullify_path();
1644 FOR_EACH_PTR(sym_list, sym) {
1645 set_position(sym->pos);
1646 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1647 __pass_to_client(sym, BASE_HOOK);
1648 fake_global_assign(sym);
1650 } END_FOR_EACH_PTR(sym);
1651 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1652 nullify_path();
1654 FOR_EACH_PTR(sym_list, sym) {
1655 set_position(sym->pos);
1656 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1657 split_function(sym);
1658 process_inlines();
1660 last_pos = sym->pos;
1661 } END_FOR_EACH_PTR(sym);
1662 split_inlines(sym_list);
1663 __pass_to_client(sym_list, END_FILE_HOOK);
1666 void smatch(int argc, char **argv)
1668 struct string_list *filelist = NULL;
1669 struct symbol_list *sym_list;
1670 struct timeval stop, start;
1672 gettimeofday(&start, NULL);
1674 if (argc < 2) {
1675 printf("Usage: smatch [--debug] <filename.c>\n");
1676 exit(1);
1678 sparse_initialize(argc, argv, &filelist);
1679 set_valid_ptr_max();
1680 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1681 if (option_file_output) {
1682 char buf[256];
1684 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1685 sm_outfd = fopen(buf, "w");
1686 if (!sm_outfd) {
1687 printf("Error: Cannot open %s\n", base_file);
1688 exit(1);
1691 sym_list = sparse_keep_tokens(base_file);
1692 split_functions(sym_list);
1693 } END_FOR_EACH_PTR_NOTAG(base_file);
1695 gettimeofday(&stop, NULL);
1697 set_position(last_pos);
1698 if (option_time)
1699 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);