unused_ret: make warning messages consistent with everyone else
[smatch.git] / smatch_flow.c
blobdb38bc89ba2a929fa91e2cc716908b34b261d5e4
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 if (!expr->right)
355 break;
357 right = strip_expr(expr->right);
359 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
361 /* foo = !bar() */
362 if (__handle_condition_assigns(expr))
363 break;
364 /* foo = (x < 5 ? foo : 5); */
365 if (__handle_select_assigns(expr))
366 break;
367 /* foo = ({frob(); frob(); frob(); 1;}) */
368 if (__handle_expr_statement_assigns(expr))
369 break;
370 /* foo = (3, 4); */
371 if (handle_comma_assigns(expr))
372 break;
373 if (handle_postop_assigns(expr))
374 break;
376 __split_expr(expr->right);
377 if (outside_of_function())
378 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
379 else
380 __pass_to_client(expr, ASSIGNMENT_HOOK);
382 __fake_struct_member_assignments(expr);
384 if (expr->op == '=' && right->type == EXPR_CALL)
385 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
387 if (get_macro_name(right->pos) &&
388 get_macro_name(expr->pos) != get_macro_name(right->pos))
389 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
391 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
393 __split_expr(expr->left);
394 break;
396 case EXPR_DEREF:
397 expr_set_parent_expr(expr->deref, expr);
399 __pass_to_client(expr, DEREF_HOOK);
400 __split_expr(expr->deref);
401 break;
402 case EXPR_SLICE:
403 expr_set_parent_expr(expr->base, expr);
405 __split_expr(expr->base);
406 break;
407 case EXPR_CAST:
408 case EXPR_FORCE_CAST:
409 expr_set_parent_expr(expr->cast_expression, expr);
411 __pass_to_client(expr, CAST_HOOK);
412 __split_expr(expr->cast_expression);
413 break;
414 case EXPR_SIZEOF:
415 if (expr->cast_expression)
416 __pass_to_client(strip_parens(expr->cast_expression),
417 SIZEOF_HOOK);
418 break;
419 case EXPR_OFFSETOF:
420 case EXPR_ALIGNOF:
421 evaluate_expression(expr);
422 break;
423 case EXPR_CONDITIONAL:
424 case EXPR_SELECT:
425 expr_set_parent_expr(expr->conditional, expr);
426 expr_set_parent_expr(expr->cond_true, expr);
427 expr_set_parent_expr(expr->cond_false, expr);
429 if (known_condition_true(expr->conditional)) {
430 __split_expr(expr->cond_true);
431 break;
433 if (known_condition_false(expr->conditional)) {
434 __split_expr(expr->cond_false);
435 break;
437 __pass_to_client(expr, SELECT_HOOK);
438 __split_whole_condition(expr->conditional);
439 __split_expr(expr->cond_true);
440 __push_true_states();
441 __use_false_states();
442 __split_expr(expr->cond_false);
443 __merge_true_states();
444 break;
445 case EXPR_CALL:
446 expr_set_parent_expr(expr->fn, expr);
448 if (sym_name_is("__builtin_constant_p", expr->fn))
449 break;
450 split_expr_list(expr->args, expr);
451 __split_expr(expr->fn);
452 if (is_inline_func(expr->fn))
453 add_inline_function(expr->fn->symbol);
454 if (inlinable(expr->fn))
455 __inline_call = 1;
456 __process_post_op_stack();
457 __pass_to_client(expr, FUNCTION_CALL_HOOK);
458 __inline_call = 0;
459 if (inlinable(expr->fn)) {
460 parse_inline(expr);
462 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
463 if (is_noreturn_func(expr->fn))
464 nullify_path();
465 break;
466 case EXPR_INITIALIZER:
467 split_expr_list(expr->expr_list, expr);
468 break;
469 case EXPR_IDENTIFIER:
470 expr_set_parent_expr(expr->ident_expression, expr);
471 __split_expr(expr->ident_expression);
472 break;
473 case EXPR_INDEX:
474 expr_set_parent_expr(expr->idx_expression, expr);
475 __split_expr(expr->idx_expression);
476 break;
477 case EXPR_POS:
478 expr_set_parent_expr(expr->init_expr, expr);
479 __split_expr(expr->init_expr);
480 break;
481 case EXPR_SYMBOL:
482 __pass_to_client(expr, SYM_HOOK);
483 break;
484 case EXPR_STRING:
485 __pass_to_client(expr, STRING_HOOK);
486 break;
487 default:
488 break;
490 pop_expression(&big_expression_stack);
493 static int is_forever_loop(struct statement *stmt)
495 struct expression *expr;
496 sval_t sval;
498 expr = strip_expr(stmt->iterator_pre_condition);
499 if (!expr)
500 expr = stmt->iterator_post_condition;
501 if (!expr) {
502 /* this is a for(;;) loop... */
503 return 1;
506 if (get_value(expr, &sval) && sval.value != 0)
507 return 1;
509 return 0;
512 static int loop_num;
513 static char *get_loop_name(int num)
515 char buf[256];
517 snprintf(buf, 255, "-loop%d", num);
518 buf[255] = '\0';
519 return alloc_sname(buf);
523 * Pre Loops are while and for loops.
525 static void handle_pre_loop(struct statement *stmt)
527 int once_through; /* we go through the loop at least once */
528 struct sm_state *extra_sm = NULL;
529 int unchanged = 0;
530 char *loop_name;
531 struct stree *stree = NULL;
532 struct sm_state *sm = NULL;
534 loop_name = get_loop_name(loop_num);
535 loop_num++;
537 __split_stmt(stmt->iterator_pre_statement);
538 __prev_stmt = stmt->iterator_pre_statement;
540 once_through = implied_condition_true(stmt->iterator_pre_condition);
542 loop_count++;
543 __push_continues();
544 __push_breaks();
546 __merge_gotos(loop_name, NULL);
548 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
549 __in_pre_condition++;
550 __pass_to_client(stmt, PRELOOP_HOOK);
551 __split_whole_condition(stmt->iterator_pre_condition);
552 __in_pre_condition--;
553 FOR_EACH_SM(stree, sm) {
554 set_state(sm->owner, sm->name, sm->sym, sm->state);
555 } END_FOR_EACH_SM(sm);
556 free_stree(&stree);
557 if (extra_sm)
558 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
560 if (option_assume_loops)
561 once_through = 1;
563 __split_stmt(stmt->iterator_statement);
564 if (is_forever_loop(stmt)) {
565 __merge_continues();
566 __save_gotos(loop_name, NULL);
568 __push_fake_cur_stree();
569 __split_stmt(stmt->iterator_post_statement);
570 stree = __pop_fake_cur_stree();
572 __discard_false_states();
573 __use_breaks();
575 if (!__path_is_null())
576 __merge_stree_into_cur(stree);
577 free_stree(&stree);
578 } else {
579 __merge_continues();
580 unchanged = __iterator_unchanged(extra_sm);
581 __split_stmt(stmt->iterator_post_statement);
582 __prev_stmt = stmt->iterator_post_statement;
583 __cur_stmt = stmt;
585 __save_gotos(loop_name, NULL);
586 __in_pre_condition++;
587 __split_whole_condition(stmt->iterator_pre_condition);
588 __in_pre_condition--;
589 nullify_path();
590 __merge_false_states();
591 if (once_through)
592 __discard_false_states();
593 else
594 __merge_false_states();
596 if (extra_sm && unchanged)
597 __extra_pre_loop_hook_after(extra_sm,
598 stmt->iterator_post_statement,
599 stmt->iterator_pre_condition);
600 __merge_breaks();
602 loop_count--;
606 * Post loops are do {} while();
608 static void handle_post_loop(struct statement *stmt)
610 char *loop_name;
612 loop_name = get_loop_name(loop_num);
613 loop_num++;
614 loop_count++;
616 __push_continues();
617 __push_breaks();
618 __merge_gotos(loop_name, NULL);
619 __split_stmt(stmt->iterator_statement);
620 __merge_continues();
621 if (!is_zero(stmt->iterator_post_condition))
622 __save_gotos(loop_name, NULL);
624 if (is_forever_loop(stmt)) {
625 __use_breaks();
626 } else {
627 __split_whole_condition(stmt->iterator_post_condition);
628 __use_false_states();
629 __merge_breaks();
631 loop_count--;
634 static int empty_statement(struct statement *stmt)
636 if (!stmt)
637 return 0;
638 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
639 return 1;
640 return 0;
643 static int last_stmt_on_same_line(void)
645 struct statement *stmt;
646 int i = 0;
648 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
649 if (!i++)
650 continue;
651 if (stmt->pos.line == get_lineno())
652 return 1;
653 return 0;
654 } END_FOR_EACH_PTR_REVERSE(stmt);
655 return 0;
658 static void split_asm_constraints(struct expression_list *expr_list)
660 struct expression *expr;
661 int state = 0;
663 FOR_EACH_PTR(expr_list, expr) {
664 switch (state) {
665 case 0: /* identifier */
666 case 1: /* constraint */
667 state++;
668 continue;
669 case 2: /* expression */
670 state = 0;
671 __split_expr(expr);
672 continue;
674 } END_FOR_EACH_PTR(expr);
677 static int is_case_val(struct statement *stmt, sval_t sval)
679 sval_t case_sval;
681 if (stmt->type != STMT_CASE)
682 return 0;
683 if (!stmt->case_expression) {
684 __set_default();
685 return 1;
687 if (!get_value(stmt->case_expression, &case_sval))
688 return 0;
689 if (case_sval.value == sval.value)
690 return 1;
691 return 0;
694 static struct range_list *get_case_rl(struct expression *switch_expr,
695 struct expression *case_expr,
696 struct expression *case_to)
698 sval_t start, end;
699 struct range_list *rl = NULL;
700 struct symbol *switch_type;
702 switch_type = get_type(switch_expr);
703 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
704 start = sval_cast(switch_type, start);
705 end = sval_cast(switch_type, end);
706 add_range(&rl, start, end);
707 } else if (get_value(case_expr, &start)) {
708 start = sval_cast(switch_type, start);
709 add_range(&rl, start, start);
712 return rl;
715 static void split_known_switch(struct statement *stmt, sval_t sval)
717 struct statement *tmp;
718 struct range_list *rl;
720 __split_expr(stmt->switch_expression);
721 sval = sval_cast(get_type(stmt->switch_expression), sval);
723 push_expression(&switch_expr_stack, stmt->switch_expression);
724 __save_switch_states(top_expression(switch_expr_stack));
725 nullify_path();
726 __push_default();
727 __push_breaks();
729 stmt = stmt->switch_statement;
731 __push_scope_hooks();
732 FOR_EACH_PTR(stmt->stmts, tmp) {
733 __smatch_lineno = tmp->pos.line;
734 if (is_case_val(tmp, sval)) {
735 rl = alloc_rl(sval, sval);
736 __merge_switches(top_expression(switch_expr_stack), rl);
737 __pass_case_to_client(top_expression(switch_expr_stack), rl);
739 if (__path_is_null())
740 continue;
741 __split_stmt(tmp);
742 if (__path_is_null()) {
743 __set_default();
744 goto out;
746 } END_FOR_EACH_PTR(tmp);
747 out:
748 __call_scope_hooks();
749 if (!__pop_default())
750 __merge_switches(top_expression(switch_expr_stack), NULL);
751 __discard_switches();
752 __merge_breaks();
753 pop_expression(&switch_expr_stack);
756 static void split_case(struct statement *stmt)
758 struct range_list *rl = NULL;
760 expr_set_parent_stmt(stmt->case_expression, stmt);
761 expr_set_parent_stmt(stmt->case_to, stmt);
763 rl = get_case_rl(top_expression(switch_expr_stack),
764 stmt->case_expression, stmt->case_to);
765 while (stmt->case_statement->type == STMT_CASE) {
766 struct range_list *tmp;
768 tmp = get_case_rl(top_expression(switch_expr_stack),
769 stmt->case_statement->case_expression,
770 stmt->case_statement->case_to);
771 if (!tmp)
772 break;
773 rl = rl_union(rl, tmp);
774 if (!stmt->case_expression)
775 __set_default();
776 stmt = stmt->case_statement;
779 __merge_switches(top_expression(switch_expr_stack), rl);
781 if (!stmt->case_expression)
782 __set_default();
783 __split_stmt(stmt->case_statement);
786 static int taking_too_long(void)
788 int ms;
790 ms = ms_since(&fn_start_time);
791 if (ms > 1000 * 60 * 5) /* five minutes */
792 return 1;
793 return 0;
796 static int is_last_stmt(struct statement *cur_stmt)
798 struct symbol *fn = get_base_type(cur_func_sym);
799 struct statement *stmt;
801 if (!fn)
802 return 0;
803 stmt = fn->stmt;
804 if (!stmt)
805 stmt = fn->inline_stmt;
806 if (!stmt || stmt->type != STMT_COMPOUND)
807 return 0;
808 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
809 if (stmt && stmt->type == STMT_LABEL)
810 stmt = stmt->label_statement;
811 if (stmt == cur_stmt)
812 return 1;
813 return 0;
816 static void handle_backward_goto(struct statement *goto_stmt)
818 const char *goto_name, *label_name;
819 struct statement *func_stmt;
820 struct symbol *base_type = get_base_type(cur_func_sym);
821 struct statement *tmp;
822 int found = 0;
824 if (!option_info)
825 return;
826 if (last_goto_statement_handled)
827 return;
828 last_goto_statement_handled = 1;
830 if (!goto_stmt->goto_label ||
831 goto_stmt->goto_label->type != SYM_LABEL ||
832 !goto_stmt->goto_label->ident)
833 return;
834 goto_name = goto_stmt->goto_label->ident->name;
836 func_stmt = base_type->stmt;
837 if (!func_stmt)
838 func_stmt = base_type->inline_stmt;
839 if (!func_stmt)
840 return;
841 if (func_stmt->type != STMT_COMPOUND)
842 return;
844 FOR_EACH_PTR(func_stmt->stmts, tmp) {
845 if (!found) {
846 if (tmp->type != STMT_LABEL)
847 continue;
848 if (!tmp->label_identifier ||
849 tmp->label_identifier->type != SYM_LABEL ||
850 !tmp->label_identifier->ident)
851 continue;
852 label_name = tmp->label_identifier->ident->name;
853 if (strcmp(goto_name, label_name) != 0)
854 continue;
855 found = 1;
857 __split_stmt(tmp);
858 } END_FOR_EACH_PTR(tmp);
861 static void fake_a_return(void)
863 struct symbol *return_type;
865 nullify_path();
866 __unnullify_path();
868 return_type = get_real_base_type(cur_func_sym);
869 return_type = get_real_base_type(return_type);
870 if (return_type != &void_ctype) {
871 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
872 nullify_path();
876 static void fake_an_empty_default(struct position pos)
878 static struct statement none = {};
880 none.pos = pos;
881 none.type = STMT_NONE;
882 __merge_switches(top_expression(switch_expr_stack), NULL);
883 __split_stmt(&none);
886 static void split_compound(struct statement *stmt)
888 struct statement *prev = NULL;
889 struct statement *cur = NULL;
890 struct statement *next;
892 __push_scope_hooks();
894 FOR_EACH_PTR(stmt->stmts, next) {
895 /* just set them all ahead of time */
896 stmt_set_parent_stmt(next, stmt);
898 if (cur) {
899 __prev_stmt = prev;
900 __next_stmt = next;
901 __cur_stmt = cur;
902 __split_stmt(cur);
904 prev = cur;
905 cur = next;
906 } END_FOR_EACH_PTR(next);
907 if (cur) {
908 __prev_stmt = prev;
909 __cur_stmt = cur;
910 __next_stmt = NULL;
911 __split_stmt(cur);
915 * For function scope, then delay calling the scope hooks until the
916 * end of function hooks can run. I'm not positive this is the right
917 * thing...
919 if (!is_last_stmt(cur))
920 __call_scope_hooks();
924 * This is a hack, work around for detecting empty functions.
926 static int need_delayed_scope_hooks(void)
928 struct symbol *fn = get_base_type(cur_func_sym);
929 struct statement *stmt;
931 if (!fn)
932 return 0;
933 stmt = fn->stmt;
934 if (!stmt)
935 stmt = fn->inline_stmt;
936 if (stmt && stmt->type == STMT_COMPOUND)
937 return 1;
938 return 0;
941 void __split_label_stmt(struct statement *stmt)
943 if (stmt->label_identifier &&
944 stmt->label_identifier->type == SYM_LABEL &&
945 stmt->label_identifier->ident) {
946 loop_count |= 0x80000000;
947 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
951 static void find_asm_gotos(struct statement *stmt)
953 struct symbol *sym;
955 FOR_EACH_PTR(stmt->asm_labels, sym) {
956 __save_gotos(sym->ident->name, sym);
957 } END_FOR_EACH_PTR(sym);
960 void __split_stmt(struct statement *stmt)
962 sval_t sval;
964 if (!stmt)
965 goto out;
967 if (__bail_on_rest_of_function)
968 return;
970 if (out_of_memory() || taking_too_long()) {
972 __bail_on_rest_of_function = 1;
973 final_pass = 1;
974 sm_msg("Function too hairy. Giving up.");
975 fake_a_return();
976 final_pass = 0; /* turn off sm_msg() from here */
977 return;
980 add_ptr_list(&big_statement_stack, stmt);
981 free_expression_stack(&big_expression_stack);
982 set_position(stmt->pos);
983 __pass_to_client(stmt, STMT_HOOK);
985 switch (stmt->type) {
986 case STMT_DECLARATION:
987 split_declaration(stmt->declaration);
988 break;
989 case STMT_RETURN:
990 expr_set_parent_stmt(stmt->ret_value, stmt);
992 __split_expr(stmt->ret_value);
993 __pass_to_client(stmt->ret_value, RETURN_HOOK);
994 __process_post_op_stack();
995 nullify_path();
996 break;
997 case STMT_EXPRESSION:
998 expr_set_parent_stmt(stmt->expression, stmt);
999 expr_set_parent_stmt(stmt->context, stmt);
1001 __split_expr(stmt->expression);
1002 break;
1003 case STMT_COMPOUND:
1004 split_compound(stmt);
1005 break;
1006 case STMT_IF:
1007 stmt_set_parent_stmt(stmt->if_true, stmt);
1008 stmt_set_parent_stmt(stmt->if_false, stmt);
1009 expr_set_parent_stmt(stmt->if_conditional, stmt);
1011 if (known_condition_true(stmt->if_conditional)) {
1012 __split_stmt(stmt->if_true);
1013 break;
1015 if (known_condition_false(stmt->if_conditional)) {
1016 __split_stmt(stmt->if_false);
1017 break;
1019 __split_whole_condition(stmt->if_conditional);
1020 __split_stmt(stmt->if_true);
1021 if (empty_statement(stmt->if_true) &&
1022 last_stmt_on_same_line() &&
1023 !get_macro_name(stmt->if_true->pos))
1024 sm_msg("warn: if();");
1025 __push_true_states();
1026 __use_false_states();
1027 __split_stmt(stmt->if_false);
1028 __merge_true_states();
1029 break;
1030 case STMT_ITERATOR:
1031 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1032 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1033 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1034 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1035 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1037 if (stmt->iterator_pre_condition)
1038 handle_pre_loop(stmt);
1039 else if (stmt->iterator_post_condition)
1040 handle_post_loop(stmt);
1041 else {
1042 // these are for(;;) type loops.
1043 handle_pre_loop(stmt);
1045 break;
1046 case STMT_SWITCH:
1047 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1048 expr_set_parent_stmt(stmt->switch_expression, stmt);
1050 if (get_value(stmt->switch_expression, &sval)) {
1051 split_known_switch(stmt, sval);
1052 break;
1054 __split_expr(stmt->switch_expression);
1055 push_expression(&switch_expr_stack, stmt->switch_expression);
1056 __save_switch_states(top_expression(switch_expr_stack));
1057 nullify_path();
1058 __push_default();
1059 __push_breaks();
1060 __split_stmt(stmt->switch_statement);
1061 if (!__pop_default())
1062 fake_an_empty_default(stmt->pos);
1063 __discard_switches();
1064 __merge_breaks();
1065 pop_expression(&switch_expr_stack);
1066 break;
1067 case STMT_CASE:
1068 split_case(stmt);
1069 break;
1070 case STMT_LABEL:
1071 __split_label_stmt(stmt);
1072 __split_stmt(stmt->label_statement);
1073 break;
1074 case STMT_GOTO:
1075 expr_set_parent_stmt(stmt->goto_expression, stmt);
1077 __split_expr(stmt->goto_expression);
1078 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1079 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1080 __process_breaks();
1081 } else if (!strcmp(stmt->goto_label->ident->name,
1082 "continue")) {
1083 __process_continues();
1085 } else if (stmt->goto_label &&
1086 stmt->goto_label->type == SYM_LABEL &&
1087 stmt->goto_label->ident) {
1088 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1090 nullify_path();
1091 if (is_last_stmt(stmt))
1092 handle_backward_goto(stmt);
1093 break;
1094 case STMT_NONE:
1095 break;
1096 case STMT_ASM:
1097 expr_set_parent_stmt(stmt->asm_string, stmt);
1099 find_asm_gotos(stmt);
1100 __pass_to_client(stmt, ASM_HOOK);
1101 __split_expr(stmt->asm_string);
1102 split_asm_constraints(stmt->asm_outputs);
1103 split_asm_constraints(stmt->asm_inputs);
1104 split_asm_constraints(stmt->asm_clobbers);
1105 break;
1106 case STMT_CONTEXT:
1107 break;
1108 case STMT_RANGE:
1109 __split_expr(stmt->range_expression);
1110 __split_expr(stmt->range_low);
1111 __split_expr(stmt->range_high);
1112 break;
1114 __pass_to_client(stmt, STMT_HOOK_AFTER);
1115 out:
1116 __process_post_op_stack();
1119 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1121 struct expression *expr;
1123 FOR_EACH_PTR(expr_list, expr) {
1124 expr_set_parent_expr(expr, parent);
1125 __split_expr(expr);
1126 __process_post_op_stack();
1127 } END_FOR_EACH_PTR(expr);
1130 static void split_sym(struct symbol *sym)
1132 if (!sym)
1133 return;
1134 if (!(sym->namespace & NS_SYMBOL))
1135 return;
1137 __split_stmt(sym->stmt);
1138 __split_expr(sym->array_size);
1139 split_symlist(sym->arguments);
1140 split_symlist(sym->symbol_list);
1141 __split_stmt(sym->inline_stmt);
1142 split_symlist(sym->inline_symbol_list);
1145 static void split_symlist(struct symbol_list *sym_list)
1147 struct symbol *sym;
1149 FOR_EACH_PTR(sym_list, sym) {
1150 split_sym(sym);
1151 } END_FOR_EACH_PTR(sym);
1154 typedef void (fake_cb)(struct expression *expr);
1156 static int member_to_number(struct expression *expr, struct ident *member)
1158 struct symbol *type, *tmp;
1159 char *name;
1160 int i;
1162 if (!member)
1163 return -1;
1164 name = member->name;
1166 type = get_type(expr);
1167 if (!type || type->type != SYM_STRUCT)
1168 return -1;
1170 i = -1;
1171 FOR_EACH_PTR(type->symbol_list, tmp) {
1172 i++;
1173 if (!tmp->ident)
1174 continue;
1175 if (strcmp(name, tmp->ident->name) == 0)
1176 return i;
1177 } END_FOR_EACH_PTR(tmp);
1178 return -1;
1181 static struct ident *number_to_member(struct expression *expr, int num)
1183 struct symbol *type, *member;
1184 int i = 0;
1186 type = get_type(expr);
1187 if (!type || type->type != SYM_STRUCT)
1188 return NULL;
1190 FOR_EACH_PTR(type->symbol_list, member) {
1191 if (i == num)
1192 return member->ident;
1193 i++;
1194 } END_FOR_EACH_PTR(member);
1195 return NULL;
1198 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1200 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1202 struct expression *edge_member, *assign;
1203 struct symbol *base = get_real_base_type(member);
1204 struct symbol *tmp;
1206 if (member->ident)
1207 expr = member_expression(expr, '.', member->ident);
1209 FOR_EACH_PTR(base->symbol_list, tmp) {
1210 struct symbol *type;
1212 type = get_real_base_type(tmp);
1213 if (!type)
1214 continue;
1216 edge_member = member_expression(expr, '.', tmp->ident);
1217 if (get_state_expr(SMATCH_EXTRA, edge_member))
1218 continue;
1220 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1221 set_inner_struct_members(expr, tmp);
1222 continue;
1225 if (!tmp->ident)
1226 continue;
1228 assign = assign_expression(edge_member, zero_expr());
1229 __split_expr(assign);
1230 } END_FOR_EACH_PTR(tmp);
1235 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1237 struct symbol *tmp;
1238 struct expression *member = NULL;
1239 struct expression *assign;
1240 int op = '*';
1242 if (expr->type == EXPR_PREOP && expr->op == '&') {
1243 expr = strip_expr(expr->unop);
1244 op = '.';
1247 FOR_EACH_PTR(type->symbol_list, tmp) {
1248 type = get_real_base_type(tmp);
1249 if (!type)
1250 continue;
1252 if (tmp->ident) {
1253 member = member_expression(expr, op, tmp->ident);
1254 if (get_state_expr(SMATCH_EXTRA, member))
1255 continue;
1258 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1259 set_inner_struct_members(expr, tmp);
1260 continue;
1262 if (type->type == SYM_ARRAY)
1263 continue;
1264 if (!tmp->ident)
1265 continue;
1267 assign = assign_expression(member, zero_expr());
1268 __split_expr(assign);
1269 } END_FOR_EACH_PTR(tmp);
1272 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1274 struct expression *deref, *assign, *tmp, *right;
1275 struct symbol *struct_type, *type;
1276 struct ident *member;
1277 int member_idx;
1279 struct_type = get_type(symbol);
1280 if (!struct_type ||
1281 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1282 return;
1285 * We're parsing an initializer that could look something like this:
1286 * struct foo foo = {
1287 * 42,
1288 * .whatever.xxx = 11,
1289 * .zzz = 12,
1290 * };
1292 * So what we have here is a list with 42, .whatever, and .zzz. We need
1293 * to break it up into left and right sides of the assignments.
1296 member_idx = 0;
1297 FOR_EACH_PTR(members, tmp) {
1298 deref = NULL;
1299 if (tmp->type == EXPR_IDENTIFIER) {
1300 member_idx = member_to_number(symbol, tmp->expr_ident);
1301 while (tmp->type == EXPR_IDENTIFIER) {
1302 member = tmp->expr_ident;
1303 tmp = tmp->ident_expression;
1304 if (deref)
1305 deref = member_expression(deref, '.', member);
1306 else
1307 deref = member_expression(symbol, '.', member);
1309 } else {
1310 member = number_to_member(symbol, member_idx);
1311 deref = member_expression(symbol, '.', member);
1313 right = tmp;
1314 member_idx++;
1315 if (right->type == EXPR_INITIALIZER) {
1316 type = get_type(deref);
1317 if (type && type->type == SYM_ARRAY)
1318 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1319 else
1320 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1321 } else {
1322 assign = assign_expression(deref, right);
1323 fake_cb(assign);
1325 } END_FOR_EACH_PTR(tmp);
1327 set_unset_to_zero(struct_type, symbol);
1330 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1332 fake_member_assigns_helper(symbol_expression(sym),
1333 sym->initializer->expr_list, fake_cb);
1336 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1338 struct expression *offset, *binop, *assign, *tmp;
1339 struct symbol *type;
1340 int idx;
1342 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1343 return;
1345 idx = 0;
1346 FOR_EACH_PTR(expr_list, tmp) {
1347 if (tmp->type == EXPR_INDEX) {
1348 if (tmp->idx_from != tmp->idx_to)
1349 return;
1350 idx = tmp->idx_from;
1351 if (!tmp->idx_expression)
1352 goto next;
1353 tmp = tmp->idx_expression;
1355 offset = value_expr(idx);
1356 binop = array_element_expression(array, offset);
1357 if (tmp->type == EXPR_INITIALIZER) {
1358 type = get_type(binop);
1359 if (type && type->type == SYM_ARRAY)
1360 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1361 else
1362 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1363 } else {
1364 assign = assign_expression(binop, tmp);
1365 fake_cb(assign);
1367 next:
1368 idx++;
1369 } END_FOR_EACH_PTR(tmp);
1372 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1374 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1377 static void fake_assign_expr(struct symbol *sym)
1379 struct expression *assign, *symbol;
1381 symbol = symbol_expression(sym);
1382 assign = assign_expression(symbol, sym->initializer);
1383 __split_expr(assign);
1386 static void call_split_expr(struct expression *expr)
1388 __split_expr(expr);
1391 static void do_initializer_stuff(struct symbol *sym)
1393 if (!sym->initializer)
1394 return;
1396 if (sym->initializer->type == EXPR_INITIALIZER) {
1397 if (get_real_base_type(sym)->type == SYM_ARRAY)
1398 fake_element_assigns(sym, call_split_expr);
1399 else
1400 fake_member_assigns(sym, call_split_expr);
1401 } else {
1402 fake_assign_expr(sym);
1406 static void split_declaration(struct symbol_list *sym_list)
1408 struct symbol *sym;
1410 FOR_EACH_PTR(sym_list, sym) {
1411 __pass_to_client(sym, DECLARATION_HOOK);
1412 do_initializer_stuff(sym);
1413 split_sym(sym);
1414 } END_FOR_EACH_PTR(sym);
1417 static void call_global_assign_hooks(struct expression *assign)
1419 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1422 static void fake_global_assign(struct symbol *sym)
1424 struct expression *assign, *symbol;
1426 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1427 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1428 fake_element_assigns(sym, call_global_assign_hooks);
1429 } else if (sym->initializer) {
1430 symbol = symbol_expression(sym);
1431 assign = assign_expression(symbol, sym->initializer);
1432 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1433 } else {
1434 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1436 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1437 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1438 fake_member_assigns(sym, call_global_assign_hooks);
1439 } else if (sym->initializer) {
1440 symbol = symbol_expression(sym);
1441 assign = assign_expression(symbol, sym->initializer);
1442 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1443 } else {
1444 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1446 } else {
1447 symbol = symbol_expression(sym);
1448 if (sym->initializer)
1449 assign = assign_expression(symbol, sym->initializer);
1450 else
1451 assign = assign_expression(symbol, zero_expr());
1452 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1456 static void start_function_definition(struct symbol *sym)
1458 __in_function_def = 1;
1459 __pass_to_client(sym, FUNC_DEF_HOOK);
1460 __in_function_def = 0;
1461 __pass_to_client(sym, AFTER_DEF_HOOK);
1465 static void split_function(struct symbol *sym)
1467 struct symbol *base_type = get_base_type(sym);
1469 if (!base_type->stmt && !base_type->inline_stmt)
1470 return;
1472 gettimeofday(&fn_start_time, NULL);
1473 cur_func_sym = sym;
1474 if (sym->ident)
1475 cur_func = sym->ident->name;
1476 set_position(sym->pos);
1477 loop_count = 0;
1478 last_goto_statement_handled = 0;
1479 sm_debug("new function: %s\n", cur_func);
1480 __stree_id = 0;
1481 if (option_two_passes) {
1482 __unnullify_path();
1483 loop_num = 0;
1484 final_pass = 0;
1485 start_function_definition(sym);
1486 __split_stmt(base_type->stmt);
1487 __split_stmt(base_type->inline_stmt);
1488 nullify_path();
1490 __unnullify_path();
1491 loop_num = 0;
1492 final_pass = 1;
1493 start_function_definition(sym);
1494 __split_stmt(base_type->stmt);
1495 __split_stmt(base_type->inline_stmt);
1496 __pass_to_client(sym, END_FUNC_HOOK);
1497 if (need_delayed_scope_hooks())
1498 __call_scope_hooks();
1499 __pass_to_client(sym, AFTER_FUNC_HOOK);
1501 clear_all_states();
1502 cur_func_sym = NULL;
1503 cur_func = NULL;
1504 free_data_info_allocs();
1505 free_expression_stack(&switch_expr_stack);
1506 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1507 __bail_on_rest_of_function = 0;
1510 static void parse_inline(struct expression *call)
1512 struct symbol *base_type;
1513 int loop_num_bak = loop_num;
1514 int loop_count_bak = loop_count;
1515 int final_pass_bak = final_pass;
1516 char *cur_func_bak = cur_func;
1517 struct statement_list *big_statement_stack_bak = big_statement_stack;
1518 struct expression_list *big_expression_stack_bak = big_expression_stack;
1519 struct expression_list *big_condition_stack_bak = big_condition_stack;
1520 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1521 struct symbol *cur_func_sym_bak = cur_func_sym;
1523 __pass_to_client(call, INLINE_FN_START);
1524 final_pass = 0; /* don't print anything */
1525 __inline_fn = call;
1527 base_type = get_base_type(call->fn->symbol);
1528 cur_func_sym = call->fn->symbol;
1529 if (call->fn->symbol->ident)
1530 cur_func = call->fn->symbol->ident->name;
1531 else
1532 cur_func = NULL;
1533 set_position(call->fn->symbol->pos);
1535 save_all_states();
1536 big_statement_stack = NULL;
1537 big_expression_stack = NULL;
1538 big_condition_stack = NULL;
1539 switch_expr_stack = NULL;
1541 sm_debug("inline function: %s\n", cur_func);
1542 __unnullify_path();
1543 loop_num = 0;
1544 start_function_definition(call->fn->symbol);
1545 __split_stmt(base_type->stmt);
1546 __split_stmt(base_type->inline_stmt);
1547 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1548 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1550 free_expression_stack(&switch_expr_stack);
1551 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1552 nullify_path();
1553 free_goto_stack();
1555 loop_num = loop_num_bak;
1556 loop_count = loop_count_bak;
1557 final_pass = final_pass_bak;
1558 cur_func_sym = cur_func_sym_bak;
1559 cur_func = cur_func_bak;
1560 big_statement_stack = big_statement_stack_bak;
1561 big_expression_stack = big_expression_stack_bak;
1562 big_condition_stack = big_condition_stack_bak;
1563 switch_expr_stack = switch_expr_stack_bak;
1565 restore_all_states();
1566 set_position(call->pos);
1567 __inline_fn = NULL;
1568 __pass_to_client(call, INLINE_FN_END);
1571 static struct symbol_list *inlines_called;
1572 static void add_inline_function(struct symbol *sym)
1574 static struct symbol_list *already_added;
1575 struct symbol *tmp;
1577 FOR_EACH_PTR(already_added, tmp) {
1578 if (tmp == sym)
1579 return;
1580 } END_FOR_EACH_PTR(tmp);
1582 add_ptr_list(&already_added, sym);
1583 add_ptr_list(&inlines_called, sym);
1586 static void process_inlines(void)
1588 struct symbol *tmp;
1590 FOR_EACH_PTR(inlines_called, tmp) {
1591 split_function(tmp);
1592 } END_FOR_EACH_PTR(tmp);
1593 free_ptr_list(&inlines_called);
1596 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1598 struct symbol *sym;
1600 FOR_EACH_PTR_REVERSE(big_list, sym) {
1601 if (!sym->scope)
1602 continue;
1603 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1604 return sym;
1605 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1606 return sym;
1607 } END_FOR_EACH_PTR_REVERSE(sym);
1609 return NULL;
1612 static void split_inlines_in_scope(struct symbol *sym)
1614 struct symbol *base;
1615 struct symbol_list *scope_list;
1616 int stream;
1618 scope_list = sym->scope->symbols;
1619 stream = sym->pos.stream;
1621 /* find the last static symbol in the file */
1622 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1623 if (sym->pos.stream != stream)
1624 continue;
1625 if (sym->type != SYM_NODE)
1626 continue;
1627 base = get_base_type(sym);
1628 if (!base)
1629 continue;
1630 if (base->type != SYM_FN)
1631 continue;
1632 if (!base->inline_stmt)
1633 continue;
1634 add_inline_function(sym);
1635 } END_FOR_EACH_PTR_REVERSE(sym);
1637 process_inlines();
1640 static void split_inlines(struct symbol_list *sym_list)
1642 struct symbol *sym;
1644 sym = get_last_scoped_symbol(sym_list, 0);
1645 if (sym)
1646 split_inlines_in_scope(sym);
1647 sym = get_last_scoped_symbol(sym_list, 1);
1648 if (sym)
1649 split_inlines_in_scope(sym);
1652 static struct stree *clone_estates_perm(struct stree *orig)
1654 struct stree *ret = NULL;
1655 struct sm_state *tmp;
1657 FOR_EACH_SM(orig, tmp) {
1658 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1659 } END_FOR_EACH_SM(tmp);
1661 return ret;
1664 struct position last_pos;
1665 static void split_functions(struct symbol_list *sym_list)
1667 struct symbol *sym;
1669 __unnullify_path();
1670 FOR_EACH_PTR(sym_list, sym) {
1671 set_position(sym->pos);
1672 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1673 __pass_to_client(sym, BASE_HOOK);
1674 fake_global_assign(sym);
1676 } END_FOR_EACH_PTR(sym);
1677 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1678 nullify_path();
1680 FOR_EACH_PTR(sym_list, sym) {
1681 set_position(sym->pos);
1682 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1683 split_function(sym);
1684 process_inlines();
1686 last_pos = sym->pos;
1687 } END_FOR_EACH_PTR(sym);
1688 split_inlines(sym_list);
1689 __pass_to_client(sym_list, END_FILE_HOOK);
1692 static int final_before_fake;
1693 void init_fake_env(void)
1695 if (!in_fake_env)
1696 final_before_fake = final_pass;
1697 in_fake_env++;
1698 __push_fake_cur_stree();
1699 final_pass = 0;
1702 void end_fake_env(void)
1704 __pop_fake_cur_stree();
1705 in_fake_env--;
1706 if (!in_fake_env)
1707 final_pass = final_before_fake;
1710 void smatch(int argc, char **argv)
1712 struct string_list *filelist = NULL;
1713 struct symbol_list *sym_list;
1714 struct timeval stop, start;
1716 gettimeofday(&start, NULL);
1718 if (argc < 2) {
1719 printf("Usage: smatch [--debug] <filename.c>\n");
1720 exit(1);
1722 sparse_initialize(argc, argv, &filelist);
1723 set_valid_ptr_max();
1724 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1725 if (option_file_output) {
1726 char buf[256];
1728 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1729 sm_outfd = fopen(buf, "w");
1730 if (!sm_outfd) {
1731 printf("Error: Cannot open %s\n", base_file);
1732 exit(1);
1735 sym_list = sparse_keep_tokens(base_file);
1736 split_functions(sym_list);
1737 } END_FOR_EACH_PTR_NOTAG(base_file);
1739 gettimeofday(&stop, NULL);
1741 set_position(last_pos);
1742 if (option_time)
1743 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);