extra: don't overwrite the implications for useless pointer limits
[smatch.git] / smatch_flow.c
blobb9b58518ef7b37e9f7a8615097b4917972167ee9
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 static int in_fake_env;
30 int final_pass;
31 int __inline_call;
32 struct expression *__inline_fn;
34 static int __smatch_lineno = 0;
36 static char *base_file;
37 static const char *filename;
38 static char *pathname;
39 static char *full_filename;
40 static char *cur_func;
41 static unsigned int loop_count;
42 static int last_goto_statement_handled;
43 int __expr_stmt_count;
44 int __in_function_def;
45 static struct expression_list *switch_expr_stack = NULL;
46 static struct expression_list *post_op_stack = NULL;
48 struct expression_list *big_expression_stack;
49 struct statement_list *big_statement_stack;
50 struct statement *__prev_stmt;
51 struct statement *__cur_stmt;
52 struct statement *__next_stmt;
53 int __in_pre_condition = 0;
54 int __bail_on_rest_of_function = 0;
55 static struct timeval fn_start_time;
56 char *get_function(void) { return cur_func; }
57 int get_lineno(void) { return __smatch_lineno; }
58 int inside_loop(void) { return !!loop_count; }
59 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
60 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
61 int in_expression_statement(void) { return !!__expr_stmt_count; }
63 static void split_symlist(struct symbol_list *sym_list);
64 static void split_declaration(struct symbol_list *sym_list);
65 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
66 static void add_inline_function(struct symbol *sym);
67 static void parse_inline(struct expression *expr);
69 int option_assume_loops = 0;
70 int option_two_passes = 0;
71 struct symbol *cur_func_sym = NULL;
72 struct stree *global_states;
74 long long valid_ptr_min = 4096;
75 long long valid_ptr_max = 2117777777;
76 sval_t valid_ptr_min_sval = {
77 .type = &ptr_ctype,
78 {.value = 4096},
80 sval_t valid_ptr_max_sval = {
81 .type = &ptr_ctype,
82 {.value = LONG_MAX - 100000},
85 static void set_valid_ptr_max(void)
87 if (type_bits(&ptr_ctype) == 32)
88 valid_ptr_max = 2117777777;
89 else if (type_bits(&ptr_ctype) == 64)
90 valid_ptr_max = 2117777777777777777LL;
92 valid_ptr_max_sval.value = valid_ptr_max;
95 int outside_of_function(void)
97 return cur_func_sym == NULL;
100 const char *get_filename(void)
102 if (option_info)
103 return base_file;
104 if (option_full_path)
105 return full_filename;
106 return filename;
109 const char *get_base_file(void)
111 return base_file;
114 static void set_position(struct position pos)
116 int len;
117 static int prev_stream = -1;
119 if (in_fake_env)
120 return;
122 if (pos.stream == 0 && pos.line == 0)
123 return;
125 __smatch_lineno = pos.line;
127 if (pos.stream == prev_stream)
128 return;
130 filename = stream_name(pos.stream);
132 free(full_filename);
133 pathname = getcwd(NULL, 0);
134 if (pathname) {
135 len = strlen(pathname) + 1 + strlen(filename) + 1;
136 full_filename = malloc(len);
137 snprintf(full_filename, len, "%s/%s", pathname, filename);
138 } else {
139 full_filename = alloc_string(filename);
141 free(pathname);
144 int is_assigned_call(struct expression *expr)
146 struct expression *parent = expr_get_parent_expr(expr);
148 if (parent &&
149 parent->type == EXPR_ASSIGNMENT &&
150 parent->op == '=' &&
151 strip_expr(parent->right) == expr)
152 return 1;
154 return 0;
157 static int is_inline_func(struct expression *expr)
159 if (expr->type != EXPR_SYMBOL || !expr->symbol)
160 return 0;
161 if (expr->symbol->ctype.modifiers & MOD_INLINE)
162 return 1;
163 return 0;
166 static int is_noreturn_func(struct expression *expr)
168 if (expr->type != EXPR_SYMBOL || !expr->symbol)
169 return 0;
170 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
171 return 1;
172 return 0;
175 int inlinable(struct expression *expr)
177 struct symbol *sym;
178 struct statement *last_stmt = NULL;
180 if (__inline_fn) /* don't nest */
181 return 0;
183 if (expr->type != EXPR_SYMBOL || !expr->symbol)
184 return 0;
185 if (is_no_inline_function(expr->symbol->ident->name))
186 return 0;
187 sym = get_base_type(expr->symbol);
188 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
189 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
190 return 0;
191 if (sym->stmt->type != STMT_COMPOUND)
192 return 0;
193 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
195 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
196 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
197 return 0;
198 if (sym->inline_stmt->type != STMT_COMPOUND)
199 return 0;
200 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
203 if (!last_stmt)
204 return 0;
206 /* the magic numbers in this function are pulled out of my bum. */
207 if (last_stmt->pos.line > sym->pos.line + 20)
208 return 0;
210 return 1;
213 void __process_post_op_stack(void)
215 struct expression *expr;
217 FOR_EACH_PTR(post_op_stack, expr) {
218 __pass_to_client(expr, OP_HOOK);
219 } END_FOR_EACH_PTR(expr);
221 __free_ptr_list((struct ptr_list **)&post_op_stack);
224 static int handle_comma_assigns(struct expression *expr)
226 struct expression *right;
227 struct expression *assign;
229 right = strip_expr(expr->right);
230 if (right->type != EXPR_COMMA)
231 return 0;
233 __split_expr(right->left);
234 __process_post_op_stack();
236 assign = assign_expression(expr->left, right->right);
237 __split_expr(assign);
239 return 1;
242 /* This is to handle *p++ = foo; assignments */
243 static int handle_postop_assigns(struct expression *expr)
245 struct expression *left, *fake_left;
246 struct expression *assign;
248 left = strip_expr(expr->left);
249 if (left->type != EXPR_PREOP || left->op != '*')
250 return 0;
251 left = strip_expr(left->unop);
252 if (left->type != EXPR_POSTOP)
253 return 0;
255 fake_left = deref_expression(strip_expr(left->unop));
256 assign = assign_expression(fake_left, expr->right);
258 __split_expr(assign);
259 __split_expr(expr->left);
261 return 1;
264 static int prev_expression_is_getting_address(struct expression *expr)
266 struct expression *parent;
268 do {
269 parent = expr_get_parent_expr(expr);
271 if (!parent)
272 return 0;
273 if (parent->type == EXPR_PREOP && parent->op == '&')
274 return 1;
275 if (parent->type == EXPR_PREOP && parent->op == '(')
276 goto next;
277 if (parent->type == EXPR_DEREF && parent->op == '.')
278 goto next;
280 return 0;
281 next:
282 expr = parent;
283 } while (1);
286 void __split_expr(struct expression *expr)
288 if (!expr)
289 return;
291 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
293 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
294 return;
295 if (__in_fake_assign >= 4) /* don't allow too much nesting */
296 return;
298 push_expression(&big_expression_stack, expr);
299 set_position(expr->pos);
300 __pass_to_client(expr, EXPR_HOOK);
302 switch (expr->type) {
303 case EXPR_PREOP:
304 expr_set_parent_expr(expr->unop, expr);
306 if (expr->op == '*' &&
307 !prev_expression_is_getting_address(expr))
308 __pass_to_client(expr, DEREF_HOOK);
309 __split_expr(expr->unop);
310 __pass_to_client(expr, OP_HOOK);
311 break;
312 case EXPR_POSTOP:
313 expr_set_parent_expr(expr->unop, expr);
315 __split_expr(expr->unop);
316 push_expression(&post_op_stack, expr);
317 break;
318 case EXPR_STATEMENT:
319 __expr_stmt_count++;
320 if (expr->statement && !expr->statement) {
321 stmt_set_parent_stmt(expr->statement,
322 last_ptr_list((struct ptr_list *)big_statement_stack));
324 __split_stmt(expr->statement);
325 __expr_stmt_count--;
326 break;
327 case EXPR_LOGICAL:
328 case EXPR_COMPARE:
329 expr_set_parent_expr(expr->left, expr);
330 expr_set_parent_expr(expr->right, expr);
332 __pass_to_client(expr, LOGIC_HOOK);
333 __handle_logic(expr);
334 break;
335 case EXPR_BINOP:
336 expr_set_parent_expr(expr->left, expr);
337 expr_set_parent_expr(expr->right, expr);
339 __pass_to_client(expr, BINOP_HOOK);
340 case EXPR_COMMA:
341 expr_set_parent_expr(expr->left, expr);
342 expr_set_parent_expr(expr->right, expr);
344 __split_expr(expr->left);
345 __process_post_op_stack();
346 __split_expr(expr->right);
347 break;
348 case EXPR_ASSIGNMENT: {
349 struct expression *right;
351 expr_set_parent_expr(expr->left, expr);
352 expr_set_parent_expr(expr->right, expr);
354 right = strip_expr(expr->right);
355 if (!right)
356 break;
358 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
360 /* foo = !bar() */
361 if (__handle_condition_assigns(expr))
362 break;
363 /* foo = (x < 5 ? foo : 5); */
364 if (__handle_select_assigns(expr))
365 break;
366 /* foo = ({frob(); frob(); frob(); 1;}) */
367 if (__handle_expr_statement_assigns(expr))
368 break;
369 /* foo = (3, 4); */
370 if (handle_comma_assigns(expr))
371 break;
372 if (handle_postop_assigns(expr))
373 break;
375 __split_expr(expr->right);
376 if (outside_of_function())
377 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
378 else
379 __pass_to_client(expr, ASSIGNMENT_HOOK);
381 __fake_struct_member_assignments(expr);
383 if (expr->op == '=' && right->type == EXPR_CALL)
384 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
386 if (get_macro_name(right->pos) &&
387 get_macro_name(expr->pos) != get_macro_name(right->pos))
388 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
390 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
392 __split_expr(expr->left);
393 break;
395 case EXPR_DEREF:
396 expr_set_parent_expr(expr->deref, expr);
398 __pass_to_client(expr, DEREF_HOOK);
399 __split_expr(expr->deref);
400 break;
401 case EXPR_SLICE:
402 expr_set_parent_expr(expr->base, expr);
404 __split_expr(expr->base);
405 break;
406 case EXPR_CAST:
407 case EXPR_FORCE_CAST:
408 expr_set_parent_expr(expr->cast_expression, expr);
410 __pass_to_client(expr, CAST_HOOK);
411 __split_expr(expr->cast_expression);
412 break;
413 case EXPR_SIZEOF:
414 if (expr->cast_expression)
415 __pass_to_client(strip_parens(expr->cast_expression),
416 SIZEOF_HOOK);
417 break;
418 case EXPR_OFFSETOF:
419 case EXPR_ALIGNOF:
420 evaluate_expression(expr);
421 break;
422 case EXPR_CONDITIONAL:
423 case EXPR_SELECT:
424 expr_set_parent_expr(expr->conditional, expr);
425 expr_set_parent_expr(expr->cond_true, expr);
426 expr_set_parent_expr(expr->cond_false, expr);
428 if (known_condition_true(expr->conditional)) {
429 __split_expr(expr->cond_true);
430 break;
432 if (known_condition_false(expr->conditional)) {
433 __split_expr(expr->cond_false);
434 break;
436 __pass_to_client(expr, SELECT_HOOK);
437 __split_whole_condition(expr->conditional);
438 __split_expr(expr->cond_true);
439 __push_true_states();
440 __use_false_states();
441 __split_expr(expr->cond_false);
442 __merge_true_states();
443 break;
444 case EXPR_CALL:
445 expr_set_parent_expr(expr->fn, expr);
447 if (sym_name_is("__builtin_constant_p", expr->fn))
448 break;
449 split_expr_list(expr->args, expr);
450 __split_expr(expr->fn);
451 if (is_inline_func(expr->fn))
452 add_inline_function(expr->fn->symbol);
453 if (inlinable(expr->fn))
454 __inline_call = 1;
455 __process_post_op_stack();
456 __pass_to_client(expr, FUNCTION_CALL_HOOK);
457 __inline_call = 0;
458 if (inlinable(expr->fn)) {
459 parse_inline(expr);
461 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
462 if (is_noreturn_func(expr->fn))
463 nullify_path();
464 break;
465 case EXPR_INITIALIZER:
466 split_expr_list(expr->expr_list, expr);
467 break;
468 case EXPR_IDENTIFIER:
469 expr_set_parent_expr(expr->ident_expression, expr);
470 __split_expr(expr->ident_expression);
471 break;
472 case EXPR_INDEX:
473 expr_set_parent_expr(expr->idx_expression, expr);
474 __split_expr(expr->idx_expression);
475 break;
476 case EXPR_POS:
477 expr_set_parent_expr(expr->init_expr, expr);
478 __split_expr(expr->init_expr);
479 break;
480 case EXPR_SYMBOL:
481 __pass_to_client(expr, SYM_HOOK);
482 break;
483 case EXPR_STRING:
484 __pass_to_client(expr, STRING_HOOK);
485 break;
486 default:
487 break;
489 pop_expression(&big_expression_stack);
492 static int is_forever_loop(struct statement *stmt)
494 struct expression *expr;
495 sval_t sval;
497 expr = strip_expr(stmt->iterator_pre_condition);
498 if (!expr)
499 expr = stmt->iterator_post_condition;
500 if (!expr) {
501 /* this is a for(;;) loop... */
502 return 1;
505 if (get_value(expr, &sval) && sval.value != 0)
506 return 1;
508 return 0;
511 static int loop_num;
512 static char *get_loop_name(int num)
514 char buf[256];
516 snprintf(buf, 255, "-loop%d", num);
517 buf[255] = '\0';
518 return alloc_sname(buf);
522 * Pre Loops are while and for loops.
524 static void handle_pre_loop(struct statement *stmt)
526 int once_through; /* we go through the loop at least once */
527 struct sm_state *extra_sm = NULL;
528 int unchanged = 0;
529 char *loop_name;
530 struct stree *stree = NULL;
531 struct sm_state *sm = NULL;
533 loop_name = get_loop_name(loop_num);
534 loop_num++;
536 __split_stmt(stmt->iterator_pre_statement);
537 __prev_stmt = stmt->iterator_pre_statement;
539 once_through = implied_condition_true(stmt->iterator_pre_condition);
541 loop_count++;
542 __push_continues();
543 __push_breaks();
545 __merge_gotos(loop_name, NULL);
547 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
548 __in_pre_condition++;
549 __pass_to_client(stmt, PRELOOP_HOOK);
550 __split_whole_condition(stmt->iterator_pre_condition);
551 __in_pre_condition--;
552 FOR_EACH_SM(stree, sm) {
553 set_state(sm->owner, sm->name, sm->sym, sm->state);
554 } END_FOR_EACH_SM(sm);
555 free_stree(&stree);
556 if (extra_sm)
557 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
559 if (option_assume_loops)
560 once_through = 1;
562 __split_stmt(stmt->iterator_statement);
563 if (is_forever_loop(stmt)) {
564 __merge_continues();
565 __save_gotos(loop_name, NULL);
567 __push_fake_cur_stree();
568 __split_stmt(stmt->iterator_post_statement);
569 stree = __pop_fake_cur_stree();
571 __discard_false_states();
572 __use_breaks();
574 if (!__path_is_null())
575 __merge_stree_into_cur(stree);
576 free_stree(&stree);
577 } else {
578 __merge_continues();
579 unchanged = __iterator_unchanged(extra_sm);
580 __split_stmt(stmt->iterator_post_statement);
581 __prev_stmt = stmt->iterator_post_statement;
582 __cur_stmt = stmt;
584 __save_gotos(loop_name, NULL);
585 __in_pre_condition++;
586 __split_whole_condition(stmt->iterator_pre_condition);
587 __in_pre_condition--;
588 nullify_path();
589 __merge_false_states();
590 if (once_through)
591 __discard_false_states();
592 else
593 __merge_false_states();
595 if (extra_sm && unchanged)
596 __extra_pre_loop_hook_after(extra_sm,
597 stmt->iterator_post_statement,
598 stmt->iterator_pre_condition);
599 __merge_breaks();
601 loop_count--;
605 * Post loops are do {} while();
607 static void handle_post_loop(struct statement *stmt)
609 char *loop_name;
611 loop_name = get_loop_name(loop_num);
612 loop_num++;
613 loop_count++;
615 __push_continues();
616 __push_breaks();
617 __merge_gotos(loop_name, NULL);
618 __split_stmt(stmt->iterator_statement);
619 __merge_continues();
620 if (!is_zero(stmt->iterator_post_condition))
621 __save_gotos(loop_name, NULL);
623 if (is_forever_loop(stmt)) {
624 __use_breaks();
625 } else {
626 __split_whole_condition(stmt->iterator_post_condition);
627 __use_false_states();
628 __merge_breaks();
630 loop_count--;
633 static int empty_statement(struct statement *stmt)
635 if (!stmt)
636 return 0;
637 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
638 return 1;
639 return 0;
642 static int last_stmt_on_same_line(void)
644 struct statement *stmt;
645 int i = 0;
647 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
648 if (!i++)
649 continue;
650 if (stmt->pos.line == get_lineno())
651 return 1;
652 return 0;
653 } END_FOR_EACH_PTR_REVERSE(stmt);
654 return 0;
657 static void split_asm_constraints(struct expression_list *expr_list)
659 struct expression *expr;
660 int state = 0;
662 FOR_EACH_PTR(expr_list, expr) {
663 switch (state) {
664 case 0: /* identifier */
665 case 1: /* constraint */
666 state++;
667 continue;
668 case 2: /* expression */
669 state = 0;
670 __split_expr(expr);
671 continue;
673 } END_FOR_EACH_PTR(expr);
676 static int is_case_val(struct statement *stmt, sval_t sval)
678 sval_t case_sval;
680 if (stmt->type != STMT_CASE)
681 return 0;
682 if (!stmt->case_expression) {
683 __set_default();
684 return 1;
686 if (!get_value(stmt->case_expression, &case_sval))
687 return 0;
688 if (case_sval.value == sval.value)
689 return 1;
690 return 0;
693 static struct range_list *get_case_rl(struct expression *switch_expr,
694 struct expression *case_expr,
695 struct expression *case_to)
697 sval_t start, end;
698 struct range_list *rl = NULL;
699 struct symbol *switch_type;
701 switch_type = get_type(switch_expr);
702 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
703 start = sval_cast(switch_type, start);
704 end = sval_cast(switch_type, end);
705 add_range(&rl, start, end);
706 } else if (get_value(case_expr, &start)) {
707 start = sval_cast(switch_type, start);
708 add_range(&rl, start, start);
711 return rl;
714 static void split_known_switch(struct statement *stmt, sval_t sval)
716 struct statement *tmp;
717 struct range_list *rl;
719 __split_expr(stmt->switch_expression);
720 sval = sval_cast(get_type(stmt->switch_expression), sval);
722 push_expression(&switch_expr_stack, stmt->switch_expression);
723 __save_switch_states(top_expression(switch_expr_stack));
724 nullify_path();
725 __push_default();
726 __push_breaks();
728 stmt = stmt->switch_statement;
730 __push_scope_hooks();
731 FOR_EACH_PTR(stmt->stmts, tmp) {
732 __smatch_lineno = tmp->pos.line;
733 if (is_case_val(tmp, sval)) {
734 rl = alloc_rl(sval, sval);
735 __merge_switches(top_expression(switch_expr_stack), rl);
736 __pass_case_to_client(top_expression(switch_expr_stack), rl);
738 if (__path_is_null())
739 continue;
740 __split_stmt(tmp);
741 if (__path_is_null()) {
742 __set_default();
743 goto out;
745 } END_FOR_EACH_PTR(tmp);
746 out:
747 __call_scope_hooks();
748 if (!__pop_default())
749 __merge_switches(top_expression(switch_expr_stack), NULL);
750 __discard_switches();
751 __merge_breaks();
752 pop_expression(&switch_expr_stack);
755 static void split_case(struct statement *stmt)
757 struct range_list *rl = NULL;
759 expr_set_parent_stmt(stmt->case_expression, stmt);
760 expr_set_parent_stmt(stmt->case_to, stmt);
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 stmt_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);
914 * For function scope, then delay calling the scope hooks until the
915 * end of function hooks can run. I'm not positive this is the right
916 * thing...
918 if (!is_last_stmt(cur))
919 __call_scope_hooks();
923 * This is a hack, work around for detecting empty functions.
925 static int need_delayed_scope_hooks(void)
927 struct symbol *fn = get_base_type(cur_func_sym);
928 struct statement *stmt;
930 if (!fn)
931 return 0;
932 stmt = fn->stmt;
933 if (!stmt)
934 stmt = fn->inline_stmt;
935 if (stmt && stmt->type == STMT_COMPOUND)
936 return 1;
937 return 0;
940 void __split_label_stmt(struct statement *stmt)
942 if (stmt->label_identifier &&
943 stmt->label_identifier->type == SYM_LABEL &&
944 stmt->label_identifier->ident) {
945 loop_count |= 0x80000000;
946 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
950 static void find_asm_gotos(struct statement *stmt)
952 struct symbol *sym;
954 FOR_EACH_PTR(stmt->asm_labels, sym) {
955 __save_gotos(sym->ident->name, sym);
956 } END_FOR_EACH_PTR(sym);
959 void __split_stmt(struct statement *stmt)
961 sval_t sval;
963 if (!stmt)
964 goto out;
966 if (__bail_on_rest_of_function)
967 return;
969 if (out_of_memory() || taking_too_long()) {
970 struct timeval stop;
972 gettimeofday(&stop, NULL);
974 __bail_on_rest_of_function = 1;
975 final_pass = 1;
976 sm_msg("Function too hairy. Giving up. %lu seconds",
977 stop.tv_sec - fn_start_time.tv_sec);
978 fake_a_return();
979 final_pass = 0; /* turn off sm_msg() from here */
980 return;
983 add_ptr_list(&big_statement_stack, stmt);
984 free_expression_stack(&big_expression_stack);
985 set_position(stmt->pos);
986 __pass_to_client(stmt, STMT_HOOK);
988 switch (stmt->type) {
989 case STMT_DECLARATION:
990 split_declaration(stmt->declaration);
991 break;
992 case STMT_RETURN:
993 expr_set_parent_stmt(stmt->ret_value, stmt);
995 __split_expr(stmt->ret_value);
996 __pass_to_client(stmt->ret_value, RETURN_HOOK);
997 __process_post_op_stack();
998 nullify_path();
999 break;
1000 case STMT_EXPRESSION:
1001 expr_set_parent_stmt(stmt->expression, stmt);
1002 expr_set_parent_stmt(stmt->context, stmt);
1004 __split_expr(stmt->expression);
1005 break;
1006 case STMT_COMPOUND:
1007 split_compound(stmt);
1008 break;
1009 case STMT_IF:
1010 stmt_set_parent_stmt(stmt->if_true, stmt);
1011 stmt_set_parent_stmt(stmt->if_false, stmt);
1012 expr_set_parent_stmt(stmt->if_conditional, stmt);
1014 if (known_condition_true(stmt->if_conditional)) {
1015 __split_stmt(stmt->if_true);
1016 break;
1018 if (known_condition_false(stmt->if_conditional)) {
1019 __split_stmt(stmt->if_false);
1020 break;
1022 __split_whole_condition(stmt->if_conditional);
1023 __split_stmt(stmt->if_true);
1024 if (empty_statement(stmt->if_true) &&
1025 last_stmt_on_same_line() &&
1026 !get_macro_name(stmt->if_true->pos))
1027 sm_msg("warn: if();");
1028 __push_true_states();
1029 __use_false_states();
1030 __split_stmt(stmt->if_false);
1031 __merge_true_states();
1032 break;
1033 case STMT_ITERATOR:
1034 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1035 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1036 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1037 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1038 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1040 if (stmt->iterator_pre_condition)
1041 handle_pre_loop(stmt);
1042 else if (stmt->iterator_post_condition)
1043 handle_post_loop(stmt);
1044 else {
1045 // these are for(;;) type loops.
1046 handle_pre_loop(stmt);
1048 break;
1049 case STMT_SWITCH:
1050 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1051 expr_set_parent_stmt(stmt->switch_expression, stmt);
1053 if (get_value(stmt->switch_expression, &sval)) {
1054 split_known_switch(stmt, sval);
1055 break;
1057 __split_expr(stmt->switch_expression);
1058 push_expression(&switch_expr_stack, stmt->switch_expression);
1059 __save_switch_states(top_expression(switch_expr_stack));
1060 nullify_path();
1061 __push_default();
1062 __push_breaks();
1063 __split_stmt(stmt->switch_statement);
1064 if (!__pop_default())
1065 fake_an_empty_default(stmt->pos);
1066 __discard_switches();
1067 __merge_breaks();
1068 pop_expression(&switch_expr_stack);
1069 break;
1070 case STMT_CASE:
1071 split_case(stmt);
1072 break;
1073 case STMT_LABEL:
1074 __split_label_stmt(stmt);
1075 __split_stmt(stmt->label_statement);
1076 break;
1077 case STMT_GOTO:
1078 expr_set_parent_stmt(stmt->goto_expression, stmt);
1080 __split_expr(stmt->goto_expression);
1081 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1082 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1083 __process_breaks();
1084 } else if (!strcmp(stmt->goto_label->ident->name,
1085 "continue")) {
1086 __process_continues();
1088 } else if (stmt->goto_label &&
1089 stmt->goto_label->type == SYM_LABEL &&
1090 stmt->goto_label->ident) {
1091 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1093 nullify_path();
1094 if (is_last_stmt(stmt))
1095 handle_backward_goto(stmt);
1096 break;
1097 case STMT_NONE:
1098 break;
1099 case STMT_ASM:
1100 expr_set_parent_stmt(stmt->asm_string, stmt);
1102 find_asm_gotos(stmt);
1103 __pass_to_client(stmt, ASM_HOOK);
1104 __split_expr(stmt->asm_string);
1105 split_asm_constraints(stmt->asm_outputs);
1106 split_asm_constraints(stmt->asm_inputs);
1107 split_asm_constraints(stmt->asm_clobbers);
1108 break;
1109 case STMT_CONTEXT:
1110 break;
1111 case STMT_RANGE:
1112 __split_expr(stmt->range_expression);
1113 __split_expr(stmt->range_low);
1114 __split_expr(stmt->range_high);
1115 break;
1117 __pass_to_client(stmt, STMT_HOOK_AFTER);
1118 out:
1119 __process_post_op_stack();
1122 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1124 struct expression *expr;
1126 FOR_EACH_PTR(expr_list, expr) {
1127 expr_set_parent_expr(expr, parent);
1128 __split_expr(expr);
1129 __process_post_op_stack();
1130 } END_FOR_EACH_PTR(expr);
1133 static void split_sym(struct symbol *sym)
1135 if (!sym)
1136 return;
1137 if (!(sym->namespace & NS_SYMBOL))
1138 return;
1140 __split_stmt(sym->stmt);
1141 __split_expr(sym->array_size);
1142 split_symlist(sym->arguments);
1143 split_symlist(sym->symbol_list);
1144 __split_stmt(sym->inline_stmt);
1145 split_symlist(sym->inline_symbol_list);
1148 static void split_symlist(struct symbol_list *sym_list)
1150 struct symbol *sym;
1152 FOR_EACH_PTR(sym_list, sym) {
1153 split_sym(sym);
1154 } END_FOR_EACH_PTR(sym);
1157 typedef void (fake_cb)(struct expression *expr);
1159 static int member_to_number(struct expression *expr, struct ident *member)
1161 struct symbol *type, *tmp;
1162 char *name;
1163 int i;
1165 if (!member)
1166 return -1;
1167 name = member->name;
1169 type = get_type(expr);
1170 if (!type || type->type != SYM_STRUCT)
1171 return -1;
1173 i = -1;
1174 FOR_EACH_PTR(type->symbol_list, tmp) {
1175 i++;
1176 if (!tmp->ident)
1177 continue;
1178 if (strcmp(name, tmp->ident->name) == 0)
1179 return i;
1180 } END_FOR_EACH_PTR(tmp);
1181 return -1;
1184 static struct ident *number_to_member(struct expression *expr, int num)
1186 struct symbol *type, *member;
1187 int i = 0;
1189 type = get_type(expr);
1190 if (!type || type->type != SYM_STRUCT)
1191 return NULL;
1193 FOR_EACH_PTR(type->symbol_list, member) {
1194 if (i == num)
1195 return member->ident;
1196 i++;
1197 } END_FOR_EACH_PTR(member);
1198 return NULL;
1201 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1203 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1205 struct expression *edge_member, *assign;
1206 struct symbol *base = get_real_base_type(member);
1207 struct symbol *tmp;
1209 if (member->ident)
1210 expr = member_expression(expr, '.', member->ident);
1212 FOR_EACH_PTR(base->symbol_list, tmp) {
1213 struct symbol *type;
1215 type = get_real_base_type(tmp);
1216 if (!type)
1217 continue;
1219 edge_member = member_expression(expr, '.', tmp->ident);
1220 if (get_state_expr(SMATCH_EXTRA, edge_member))
1221 continue;
1223 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1224 set_inner_struct_members(expr, tmp);
1225 continue;
1228 if (!tmp->ident)
1229 continue;
1231 assign = assign_expression(edge_member, zero_expr());
1232 __split_expr(assign);
1233 } END_FOR_EACH_PTR(tmp);
1238 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1240 struct symbol *tmp;
1241 struct expression *member = NULL;
1242 struct expression *assign;
1243 int op = '*';
1245 if (expr->type == EXPR_PREOP && expr->op == '&') {
1246 expr = strip_expr(expr->unop);
1247 op = '.';
1250 FOR_EACH_PTR(type->symbol_list, tmp) {
1251 type = get_real_base_type(tmp);
1252 if (!type)
1253 continue;
1255 if (tmp->ident) {
1256 member = member_expression(expr, op, tmp->ident);
1257 if (get_state_expr(SMATCH_EXTRA, member))
1258 continue;
1261 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1262 set_inner_struct_members(expr, tmp);
1263 continue;
1265 if (type->type == SYM_ARRAY)
1266 continue;
1267 if (!tmp->ident)
1268 continue;
1270 assign = assign_expression(member, zero_expr());
1271 __split_expr(assign);
1272 } END_FOR_EACH_PTR(tmp);
1275 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1277 struct expression *deref, *assign, *tmp, *right;
1278 struct symbol *struct_type, *type;
1279 struct ident *member;
1280 int member_idx;
1282 struct_type = get_type(symbol);
1283 if (!struct_type ||
1284 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1285 return;
1288 * We're parsing an initializer that could look something like this:
1289 * struct foo foo = {
1290 * 42,
1291 * .whatever.xxx = 11,
1292 * .zzz = 12,
1293 * };
1295 * So what we have here is a list with 42, .whatever, and .zzz. We need
1296 * to break it up into left and right sides of the assignments.
1299 member_idx = 0;
1300 FOR_EACH_PTR(members, tmp) {
1301 deref = NULL;
1302 if (tmp->type == EXPR_IDENTIFIER) {
1303 member_idx = member_to_number(symbol, tmp->expr_ident);
1304 while (tmp->type == EXPR_IDENTIFIER) {
1305 member = tmp->expr_ident;
1306 tmp = tmp->ident_expression;
1307 if (deref)
1308 deref = member_expression(deref, '.', member);
1309 else
1310 deref = member_expression(symbol, '.', member);
1312 } else {
1313 member = number_to_member(symbol, member_idx);
1314 deref = member_expression(symbol, '.', member);
1316 right = tmp;
1317 member_idx++;
1318 if (right->type == EXPR_INITIALIZER) {
1319 type = get_type(deref);
1320 if (type && type->type == SYM_ARRAY)
1321 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1322 else
1323 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1324 } else {
1325 assign = assign_expression(deref, right);
1326 fake_cb(assign);
1328 } END_FOR_EACH_PTR(tmp);
1330 set_unset_to_zero(struct_type, symbol);
1333 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1335 fake_member_assigns_helper(symbol_expression(sym),
1336 sym->initializer->expr_list, fake_cb);
1339 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1341 struct expression *offset, *binop, *assign, *tmp;
1342 struct symbol *type;
1343 int idx;
1345 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1346 return;
1348 idx = 0;
1349 FOR_EACH_PTR(expr_list, tmp) {
1350 if (tmp->type == EXPR_INDEX) {
1351 if (tmp->idx_from != tmp->idx_to)
1352 return;
1353 idx = tmp->idx_from;
1354 if (!tmp->idx_expression)
1355 goto next;
1356 tmp = tmp->idx_expression;
1358 offset = value_expr(idx);
1359 binop = array_element_expression(array, offset);
1360 if (tmp->type == EXPR_INITIALIZER) {
1361 type = get_type(binop);
1362 if (type && type->type == SYM_ARRAY)
1363 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1364 else
1365 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1366 } else {
1367 assign = assign_expression(binop, tmp);
1368 fake_cb(assign);
1370 next:
1371 idx++;
1372 } END_FOR_EACH_PTR(tmp);
1375 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1377 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1380 static void fake_assign_expr(struct symbol *sym)
1382 struct expression *assign, *symbol;
1384 symbol = symbol_expression(sym);
1385 assign = assign_expression(symbol, sym->initializer);
1386 __split_expr(assign);
1389 static void call_split_expr(struct expression *expr)
1391 __split_expr(expr);
1394 static void do_initializer_stuff(struct symbol *sym)
1396 if (!sym->initializer)
1397 return;
1399 if (sym->initializer->type == EXPR_INITIALIZER) {
1400 if (get_real_base_type(sym)->type == SYM_ARRAY)
1401 fake_element_assigns(sym, call_split_expr);
1402 else
1403 fake_member_assigns(sym, call_split_expr);
1404 } else {
1405 fake_assign_expr(sym);
1409 static void split_declaration(struct symbol_list *sym_list)
1411 struct symbol *sym;
1413 FOR_EACH_PTR(sym_list, sym) {
1414 __pass_to_client(sym, DECLARATION_HOOK);
1415 do_initializer_stuff(sym);
1416 split_sym(sym);
1417 } END_FOR_EACH_PTR(sym);
1420 static void call_global_assign_hooks(struct expression *assign)
1422 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1425 static void fake_global_assign(struct symbol *sym)
1427 struct expression *assign, *symbol;
1429 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1430 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1431 fake_element_assigns(sym, call_global_assign_hooks);
1432 } else if (sym->initializer) {
1433 symbol = symbol_expression(sym);
1434 assign = assign_expression(symbol, sym->initializer);
1435 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1436 } else {
1437 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1439 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1440 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1441 fake_member_assigns(sym, call_global_assign_hooks);
1442 } else if (sym->initializer) {
1443 symbol = symbol_expression(sym);
1444 assign = assign_expression(symbol, sym->initializer);
1445 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1446 } else {
1447 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1449 } else {
1450 symbol = symbol_expression(sym);
1451 if (sym->initializer)
1452 assign = assign_expression(symbol, sym->initializer);
1453 else
1454 assign = assign_expression(symbol, zero_expr());
1455 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1459 static void start_function_definition(struct symbol *sym)
1461 __in_function_def = 1;
1462 __pass_to_client(sym, FUNC_DEF_HOOK);
1463 __in_function_def = 0;
1464 __pass_to_client(sym, AFTER_DEF_HOOK);
1468 static void split_function(struct symbol *sym)
1470 struct symbol *base_type = get_base_type(sym);
1472 if (!base_type->stmt && !base_type->inline_stmt)
1473 return;
1475 gettimeofday(&fn_start_time, NULL);
1476 cur_func_sym = sym;
1477 if (sym->ident)
1478 cur_func = sym->ident->name;
1479 set_position(sym->pos);
1480 loop_count = 0;
1481 last_goto_statement_handled = 0;
1482 sm_debug("new function: %s\n", cur_func);
1483 __stree_id = 0;
1484 if (option_two_passes) {
1485 __unnullify_path();
1486 loop_num = 0;
1487 final_pass = 0;
1488 start_function_definition(sym);
1489 __split_stmt(base_type->stmt);
1490 __split_stmt(base_type->inline_stmt);
1491 nullify_path();
1493 __unnullify_path();
1494 loop_num = 0;
1495 final_pass = 1;
1496 start_function_definition(sym);
1497 __split_stmt(base_type->stmt);
1498 __split_stmt(base_type->inline_stmt);
1499 __pass_to_client(sym, END_FUNC_HOOK);
1500 if (need_delayed_scope_hooks())
1501 __call_scope_hooks();
1502 __pass_to_client(sym, AFTER_FUNC_HOOK);
1504 clear_all_states();
1505 cur_func_sym = NULL;
1506 cur_func = NULL;
1507 free_data_info_allocs();
1508 free_expression_stack(&switch_expr_stack);
1509 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1510 __bail_on_rest_of_function = 0;
1513 static void parse_inline(struct expression *call)
1515 struct symbol *base_type;
1516 int loop_num_bak = loop_num;
1517 int loop_count_bak = loop_count;
1518 int final_pass_bak = final_pass;
1519 char *cur_func_bak = cur_func;
1520 struct statement_list *big_statement_stack_bak = big_statement_stack;
1521 struct expression_list *big_expression_stack_bak = big_expression_stack;
1522 struct expression_list *big_condition_stack_bak = big_condition_stack;
1523 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1524 struct symbol *cur_func_sym_bak = cur_func_sym;
1526 __pass_to_client(call, INLINE_FN_START);
1527 final_pass = 0; /* don't print anything */
1528 __inline_fn = call;
1530 base_type = get_base_type(call->fn->symbol);
1531 cur_func_sym = call->fn->symbol;
1532 if (call->fn->symbol->ident)
1533 cur_func = call->fn->symbol->ident->name;
1534 else
1535 cur_func = NULL;
1536 set_position(call->fn->symbol->pos);
1538 save_all_states();
1539 big_statement_stack = NULL;
1540 big_expression_stack = NULL;
1541 big_condition_stack = NULL;
1542 switch_expr_stack = NULL;
1544 sm_debug("inline function: %s\n", cur_func);
1545 __unnullify_path();
1546 loop_num = 0;
1547 start_function_definition(call->fn->symbol);
1548 __split_stmt(base_type->stmt);
1549 __split_stmt(base_type->inline_stmt);
1550 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1551 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1553 free_expression_stack(&switch_expr_stack);
1554 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1555 nullify_path();
1556 free_goto_stack();
1558 loop_num = loop_num_bak;
1559 loop_count = loop_count_bak;
1560 final_pass = final_pass_bak;
1561 cur_func_sym = cur_func_sym_bak;
1562 cur_func = cur_func_bak;
1563 big_statement_stack = big_statement_stack_bak;
1564 big_expression_stack = big_expression_stack_bak;
1565 big_condition_stack = big_condition_stack_bak;
1566 switch_expr_stack = switch_expr_stack_bak;
1568 restore_all_states();
1569 set_position(call->pos);
1570 __inline_fn = NULL;
1571 __pass_to_client(call, INLINE_FN_END);
1574 static struct symbol_list *inlines_called;
1575 static void add_inline_function(struct symbol *sym)
1577 static struct symbol_list *already_added;
1578 struct symbol *tmp;
1580 FOR_EACH_PTR(already_added, tmp) {
1581 if (tmp == sym)
1582 return;
1583 } END_FOR_EACH_PTR(tmp);
1585 add_ptr_list(&already_added, sym);
1586 add_ptr_list(&inlines_called, sym);
1589 static void process_inlines(void)
1591 struct symbol *tmp;
1593 FOR_EACH_PTR(inlines_called, tmp) {
1594 split_function(tmp);
1595 } END_FOR_EACH_PTR(tmp);
1596 free_ptr_list(&inlines_called);
1599 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1601 struct symbol *sym;
1603 FOR_EACH_PTR_REVERSE(big_list, sym) {
1604 if (!sym->scope)
1605 continue;
1606 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1607 return sym;
1608 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1609 return sym;
1610 } END_FOR_EACH_PTR_REVERSE(sym);
1612 return NULL;
1615 static void split_inlines_in_scope(struct symbol *sym)
1617 struct symbol *base;
1618 struct symbol_list *scope_list;
1619 int stream;
1621 scope_list = sym->scope->symbols;
1622 stream = sym->pos.stream;
1624 /* find the last static symbol in the file */
1625 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1626 if (sym->pos.stream != stream)
1627 continue;
1628 if (sym->type != SYM_NODE)
1629 continue;
1630 base = get_base_type(sym);
1631 if (!base)
1632 continue;
1633 if (base->type != SYM_FN)
1634 continue;
1635 if (!base->inline_stmt)
1636 continue;
1637 add_inline_function(sym);
1638 } END_FOR_EACH_PTR_REVERSE(sym);
1640 process_inlines();
1643 static void split_inlines(struct symbol_list *sym_list)
1645 struct symbol *sym;
1647 sym = get_last_scoped_symbol(sym_list, 0);
1648 if (sym)
1649 split_inlines_in_scope(sym);
1650 sym = get_last_scoped_symbol(sym_list, 1);
1651 if (sym)
1652 split_inlines_in_scope(sym);
1655 static struct stree *clone_estates_perm(struct stree *orig)
1657 struct stree *ret = NULL;
1658 struct sm_state *tmp;
1660 FOR_EACH_SM(orig, tmp) {
1661 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1662 } END_FOR_EACH_SM(tmp);
1664 return ret;
1667 struct position last_pos;
1668 static void split_functions(struct symbol_list *sym_list)
1670 struct symbol *sym;
1672 __unnullify_path();
1673 FOR_EACH_PTR(sym_list, sym) {
1674 set_position(sym->pos);
1675 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1676 __pass_to_client(sym, BASE_HOOK);
1677 fake_global_assign(sym);
1679 } END_FOR_EACH_PTR(sym);
1680 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1681 nullify_path();
1683 FOR_EACH_PTR(sym_list, sym) {
1684 set_position(sym->pos);
1685 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1686 split_function(sym);
1687 process_inlines();
1689 last_pos = sym->pos;
1690 } END_FOR_EACH_PTR(sym);
1691 split_inlines(sym_list);
1692 __pass_to_client(sym_list, END_FILE_HOOK);
1695 static int final_before_fake;
1696 void init_fake_env(void)
1698 if (!in_fake_env)
1699 final_before_fake = final_pass;
1700 in_fake_env++;
1701 __push_fake_cur_stree();
1702 final_pass = 0;
1705 void end_fake_env(void)
1707 __pop_fake_cur_stree();
1708 in_fake_env--;
1709 if (!in_fake_env)
1710 final_pass = final_before_fake;
1713 void smatch(int argc, char **argv)
1715 struct string_list *filelist = NULL;
1716 struct symbol_list *sym_list;
1717 struct timeval stop, start;
1719 gettimeofday(&start, NULL);
1721 if (argc < 2) {
1722 printf("Usage: smatch [--debug] <filename.c>\n");
1723 exit(1);
1725 sparse_initialize(argc, argv, &filelist);
1726 set_valid_ptr_max();
1727 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1728 if (option_file_output) {
1729 char buf[256];
1731 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1732 sm_outfd = fopen(buf, "w");
1733 if (!sm_outfd) {
1734 printf("Error: Cannot open %s\n", base_file);
1735 exit(1);
1738 sym_list = sparse_keep_tokens(base_file);
1739 split_functions(sym_list);
1740 } END_FOR_EACH_PTR_NOTAG(base_file);
1742 gettimeofday(&stop, NULL);
1744 set_position(last_pos);
1745 if (option_time)
1746 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);