implied: fix a bug handling parameter implications
[smatch.git] / smatch_flow.c
blob6f32f382060afd24d784cd4dac4800c51e5c5708
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 if (expr->statement && !expr->statement->parent) {
332 set_parent_stmt(expr->statement,
333 last_ptr_list((struct ptr_list *)big_statement_stack));
335 __split_stmt(expr->statement);
336 __expr_stmt_count--;
337 break;
338 case EXPR_LOGICAL:
339 case EXPR_COMPARE:
340 set_parent_expr(expr->left, expr);
341 set_parent_expr(expr->right, expr);
343 __pass_to_client(expr, LOGIC_HOOK);
344 __handle_logic(expr);
345 break;
346 case EXPR_BINOP:
347 set_parent_expr(expr->left, expr);
348 set_parent_expr(expr->right, expr);
350 __pass_to_client(expr, BINOP_HOOK);
351 case EXPR_COMMA:
352 set_parent_expr(expr->left, expr);
353 set_parent_expr(expr->right, expr);
355 __split_expr(expr->left);
356 __process_post_op_stack();
357 __split_expr(expr->right);
358 break;
359 case EXPR_ASSIGNMENT: {
360 struct expression *right;
362 set_parent_expr(expr->left, expr);
363 set_parent_expr(expr->right, expr);
365 if (!expr->right)
366 break;
368 right = strip_expr(expr->right);
370 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
372 /* foo = !bar() */
373 if (__handle_condition_assigns(expr))
374 break;
375 /* foo = (x < 5 ? foo : 5); */
376 if (__handle_select_assigns(expr))
377 break;
378 /* foo = ({frob(); frob(); frob(); 1;}) */
379 if (__handle_expr_statement_assigns(expr))
380 break;
381 /* foo = (3, 4); */
382 if (handle_comma_assigns(expr))
383 break;
384 if (handle_postop_assigns(expr))
385 break;
387 __split_expr(expr->right);
388 if (outside_of_function())
389 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
390 else
391 __pass_to_client(expr, ASSIGNMENT_HOOK);
393 __fake_struct_member_assignments(expr);
395 if (expr->op == '=' && right->type == EXPR_CALL)
396 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
398 if (get_macro_name(right->pos) &&
399 get_macro_name(expr->pos) != get_macro_name(right->pos))
400 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
402 __split_expr(expr->left);
403 break;
405 case EXPR_DEREF:
406 set_parent_expr(expr->deref, expr);
408 __pass_to_client(expr, DEREF_HOOK);
409 __split_expr(expr->deref);
410 break;
411 case EXPR_SLICE:
412 set_parent_expr(expr->base, expr);
414 __split_expr(expr->base);
415 break;
416 case EXPR_CAST:
417 case EXPR_FORCE_CAST:
418 set_parent_expr(expr->cast_expression, expr);
420 __pass_to_client(expr, CAST_HOOK);
421 __split_expr(expr->cast_expression);
422 break;
423 case EXPR_SIZEOF:
424 if (expr->cast_expression)
425 __pass_to_client(strip_parens(expr->cast_expression),
426 SIZEOF_HOOK);
427 break;
428 case EXPR_OFFSETOF:
429 case EXPR_ALIGNOF:
430 evaluate_expression(expr);
431 break;
432 case EXPR_CONDITIONAL:
433 case EXPR_SELECT:
434 set_parent_expr(expr->conditional, expr);
435 set_parent_expr(expr->cond_true, expr);
436 set_parent_expr(expr->cond_false, expr);
438 if (known_condition_true(expr->conditional)) {
439 __split_expr(expr->cond_true);
440 break;
442 if (known_condition_false(expr->conditional)) {
443 __split_expr(expr->cond_false);
444 break;
446 __pass_to_client(expr, SELECT_HOOK);
447 __split_whole_condition(expr->conditional);
448 __split_expr(expr->cond_true);
449 __push_true_states();
450 __use_false_states();
451 __split_expr(expr->cond_false);
452 __merge_true_states();
453 break;
454 case EXPR_CALL:
455 set_parent_expr(expr->fn, expr);
457 if (sym_name_is("__builtin_constant_p", expr->fn))
458 break;
459 split_expr_list(expr->args, expr);
460 __split_expr(expr->fn);
461 if (is_inline_func(expr->fn))
462 add_inline_function(expr->fn->symbol);
463 if (inlinable(expr->fn))
464 __inline_call = 1;
465 __process_post_op_stack();
466 __pass_to_client(expr, FUNCTION_CALL_HOOK);
467 __inline_call = 0;
468 if (inlinable(expr->fn)) {
469 parse_inline(expr);
471 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
472 if (is_noreturn_func(expr->fn))
473 nullify_path();
474 break;
475 case EXPR_INITIALIZER:
476 split_expr_list(expr->expr_list, expr);
477 break;
478 case EXPR_IDENTIFIER:
479 set_parent_expr(expr->ident_expression, expr);
480 __split_expr(expr->ident_expression);
481 break;
482 case EXPR_INDEX:
483 set_parent_expr(expr->idx_expression, expr);
484 __split_expr(expr->idx_expression);
485 break;
486 case EXPR_POS:
487 set_parent_expr(expr->init_expr, expr);
488 __split_expr(expr->init_expr);
489 break;
490 case EXPR_SYMBOL:
491 __pass_to_client(expr, SYM_HOOK);
492 break;
493 case EXPR_STRING:
494 __pass_to_client(expr, STRING_HOOK);
495 break;
496 default:
497 break;
499 pop_expression(&big_expression_stack);
502 static int is_forever_loop(struct statement *stmt)
504 struct expression *expr;
505 sval_t sval;
507 expr = strip_expr(stmt->iterator_pre_condition);
508 if (!expr)
509 expr = stmt->iterator_post_condition;
510 if (!expr) {
511 /* this is a for(;;) loop... */
512 return 1;
515 if (get_value(expr, &sval) && sval.value != 0)
516 return 1;
518 return 0;
521 static int loop_num;
522 static char *get_loop_name(int num)
524 char buf[256];
526 snprintf(buf, 255, "-loop%d", num);
527 buf[255] = '\0';
528 return alloc_sname(buf);
532 * Pre Loops are while and for loops.
534 static void handle_pre_loop(struct statement *stmt)
536 int once_through; /* we go through the loop at least once */
537 struct sm_state *extra_sm = NULL;
538 int unchanged = 0;
539 char *loop_name;
540 struct stree *stree = NULL;
541 struct sm_state *sm = NULL;
543 loop_name = get_loop_name(loop_num);
544 loop_num++;
546 __split_stmt(stmt->iterator_pre_statement);
547 __prev_stmt = stmt->iterator_pre_statement;
549 once_through = implied_condition_true(stmt->iterator_pre_condition);
551 loop_count++;
552 __push_continues();
553 __push_breaks();
555 __merge_gotos(loop_name, NULL);
557 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
558 __in_pre_condition++;
559 __pass_to_client(stmt, PRELOOP_HOOK);
560 __split_whole_condition(stmt->iterator_pre_condition);
561 __in_pre_condition--;
562 FOR_EACH_SM(stree, sm) {
563 set_state(sm->owner, sm->name, sm->sym, sm->state);
564 } END_FOR_EACH_SM(sm);
565 free_stree(&stree);
566 if (extra_sm)
567 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
569 if (option_assume_loops)
570 once_through = 1;
572 __split_stmt(stmt->iterator_statement);
573 if (is_forever_loop(stmt)) {
574 __merge_continues();
575 __save_gotos(loop_name, NULL);
577 __push_fake_cur_stree();
578 __split_stmt(stmt->iterator_post_statement);
579 stree = __pop_fake_cur_stree();
581 __discard_false_states();
582 __use_breaks();
584 if (!__path_is_null())
585 __merge_stree_into_cur(stree);
586 free_stree(&stree);
587 } else {
588 __merge_continues();
589 unchanged = __iterator_unchanged(extra_sm);
590 __split_stmt(stmt->iterator_post_statement);
591 __prev_stmt = stmt->iterator_post_statement;
592 __cur_stmt = stmt;
594 __save_gotos(loop_name, NULL);
595 __in_pre_condition++;
596 __split_whole_condition(stmt->iterator_pre_condition);
597 __in_pre_condition--;
598 nullify_path();
599 __merge_false_states();
600 if (once_through)
601 __discard_false_states();
602 else
603 __merge_false_states();
605 if (extra_sm && unchanged)
606 __extra_pre_loop_hook_after(extra_sm,
607 stmt->iterator_post_statement,
608 stmt->iterator_pre_condition);
609 __merge_breaks();
611 loop_count--;
615 * Post loops are do {} while();
617 static void handle_post_loop(struct statement *stmt)
619 char *loop_name;
621 loop_name = get_loop_name(loop_num);
622 loop_num++;
623 loop_count++;
625 __push_continues();
626 __push_breaks();
627 __merge_gotos(loop_name, NULL);
628 __split_stmt(stmt->iterator_statement);
629 __merge_continues();
630 if (!is_zero(stmt->iterator_post_condition))
631 __save_gotos(loop_name, NULL);
633 if (is_forever_loop(stmt)) {
634 __use_breaks();
635 } else {
636 __split_whole_condition(stmt->iterator_post_condition);
637 __use_false_states();
638 __merge_breaks();
640 loop_count--;
643 static int empty_statement(struct statement *stmt)
645 if (!stmt)
646 return 0;
647 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
648 return 1;
649 return 0;
652 static int last_stmt_on_same_line(void)
654 struct statement *stmt;
655 int i = 0;
657 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
658 if (!i++)
659 continue;
660 if (stmt->pos.line == get_lineno())
661 return 1;
662 return 0;
663 } END_FOR_EACH_PTR_REVERSE(stmt);
664 return 0;
667 static void split_asm_constraints(struct expression_list *expr_list)
669 struct expression *expr;
670 int state = 0;
672 FOR_EACH_PTR(expr_list, expr) {
673 switch (state) {
674 case 0: /* identifier */
675 case 1: /* constraint */
676 state++;
677 continue;
678 case 2: /* expression */
679 state = 0;
680 __split_expr(expr);
681 continue;
683 } END_FOR_EACH_PTR(expr);
686 static int is_case_val(struct statement *stmt, sval_t sval)
688 sval_t case_sval;
690 if (stmt->type != STMT_CASE)
691 return 0;
692 if (!stmt->case_expression) {
693 __set_default();
694 return 1;
696 if (!get_value(stmt->case_expression, &case_sval))
697 return 0;
698 if (case_sval.value == sval.value)
699 return 1;
700 return 0;
703 static struct range_list *get_case_rl(struct expression *switch_expr,
704 struct expression *case_expr,
705 struct expression *case_to)
707 sval_t start, end;
708 struct range_list *rl = NULL;
709 struct symbol *switch_type;
711 switch_type = get_type(switch_expr);
712 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
713 start = sval_cast(switch_type, start);
714 end = sval_cast(switch_type, end);
715 add_range(&rl, start, end);
716 } else if (get_value(case_expr, &start)) {
717 start = sval_cast(switch_type, start);
718 add_range(&rl, start, start);
721 return rl;
724 static void split_known_switch(struct statement *stmt, sval_t sval)
726 struct statement *tmp;
727 struct range_list *rl;
729 __split_expr(stmt->switch_expression);
730 sval = sval_cast(get_type(stmt->switch_expression), sval);
732 push_expression(&switch_expr_stack, stmt->switch_expression);
733 __save_switch_states(top_expression(switch_expr_stack));
734 nullify_path();
735 __push_default();
736 __push_breaks();
738 stmt = stmt->switch_statement;
740 __push_scope_hooks();
741 FOR_EACH_PTR(stmt->stmts, tmp) {
742 __smatch_lineno = tmp->pos.line;
743 if (is_case_val(tmp, sval)) {
744 rl = alloc_rl(sval, sval);
745 __merge_switches(top_expression(switch_expr_stack), rl);
746 __pass_case_to_client(top_expression(switch_expr_stack), rl);
748 if (__path_is_null())
749 continue;
750 __split_stmt(tmp);
751 if (__path_is_null()) {
752 __set_default();
753 goto out;
755 } END_FOR_EACH_PTR(tmp);
756 out:
757 __call_scope_hooks();
758 if (!__pop_default())
759 __merge_switches(top_expression(switch_expr_stack), NULL);
760 __discard_switches();
761 __merge_breaks();
762 pop_expression(&switch_expr_stack);
765 static void split_case(struct statement *stmt)
767 struct range_list *rl = NULL;
769 rl = get_case_rl(top_expression(switch_expr_stack),
770 stmt->case_expression, stmt->case_to);
771 while (stmt->case_statement->type == STMT_CASE) {
772 struct range_list *tmp;
774 tmp = get_case_rl(top_expression(switch_expr_stack),
775 stmt->case_statement->case_expression,
776 stmt->case_statement->case_to);
777 if (!tmp)
778 break;
779 rl = rl_union(rl, tmp);
780 if (!stmt->case_expression)
781 __set_default();
782 stmt = stmt->case_statement;
785 __merge_switches(top_expression(switch_expr_stack), rl);
787 if (!stmt->case_expression)
788 __set_default();
789 __split_stmt(stmt->case_statement);
792 static int taking_too_long(void)
794 int ms;
796 ms = ms_since(&fn_start_time);
797 if (ms > 1000 * 60 * 5) /* five minutes */
798 return 1;
799 return 0;
802 static int is_last_stmt(struct statement *cur_stmt)
804 struct symbol *fn = get_base_type(cur_func_sym);
805 struct statement *stmt;
807 if (!fn)
808 return 0;
809 stmt = fn->stmt;
810 if (!stmt)
811 stmt = fn->inline_stmt;
812 if (!stmt || stmt->type != STMT_COMPOUND)
813 return 0;
814 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
815 if (stmt && stmt->type == STMT_LABEL)
816 stmt = stmt->label_statement;
817 if (stmt == cur_stmt)
818 return 1;
819 return 0;
822 static void handle_backward_goto(struct statement *goto_stmt)
824 const char *goto_name, *label_name;
825 struct statement *func_stmt;
826 struct symbol *base_type = get_base_type(cur_func_sym);
827 struct statement *tmp;
828 int found = 0;
830 if (!option_info)
831 return;
832 if (last_goto_statement_handled)
833 return;
834 last_goto_statement_handled = 1;
836 if (!goto_stmt->goto_label ||
837 goto_stmt->goto_label->type != SYM_LABEL ||
838 !goto_stmt->goto_label->ident)
839 return;
840 goto_name = goto_stmt->goto_label->ident->name;
842 func_stmt = base_type->stmt;
843 if (!func_stmt)
844 func_stmt = base_type->inline_stmt;
845 if (!func_stmt)
846 return;
847 if (func_stmt->type != STMT_COMPOUND)
848 return;
850 FOR_EACH_PTR(func_stmt->stmts, tmp) {
851 if (!found) {
852 if (tmp->type != STMT_LABEL)
853 continue;
854 if (!tmp->label_identifier ||
855 tmp->label_identifier->type != SYM_LABEL ||
856 !tmp->label_identifier->ident)
857 continue;
858 label_name = tmp->label_identifier->ident->name;
859 if (strcmp(goto_name, label_name) != 0)
860 continue;
861 found = 1;
863 __split_stmt(tmp);
864 } END_FOR_EACH_PTR(tmp);
867 static void fake_a_return(void)
869 struct symbol *return_type;
871 nullify_path();
872 __unnullify_path();
874 return_type = get_real_base_type(cur_func_sym);
875 return_type = get_real_base_type(return_type);
876 if (return_type != &void_ctype) {
877 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
878 nullify_path();
882 static void fake_an_empty_default(struct position pos)
884 static struct statement none = {};
886 none.pos = pos;
887 none.type = STMT_NONE;
888 __merge_switches(top_expression(switch_expr_stack), NULL);
889 __split_stmt(&none);
892 static void split_compound(struct statement *stmt)
894 struct statement *prev = NULL;
895 struct statement *cur = NULL;
896 struct statement *next;
898 __push_scope_hooks();
900 FOR_EACH_PTR(stmt->stmts, next) {
901 /* just set them all ahead of time */
902 set_parent_stmt(next, stmt);
904 if (cur) {
905 __prev_stmt = prev;
906 __next_stmt = next;
907 __cur_stmt = cur;
908 __split_stmt(cur);
910 prev = cur;
911 cur = next;
912 } END_FOR_EACH_PTR(next);
913 if (cur) {
914 __prev_stmt = prev;
915 __cur_stmt = cur;
916 __next_stmt = NULL;
917 __split_stmt(cur);
921 * For function scope, then delay calling the scope hooks until the
922 * end of function hooks can run. I'm not positive this is the right
923 * thing...
925 if (!is_last_stmt(cur))
926 __call_scope_hooks();
930 * This is a hack, work around for detecting empty functions.
932 static int need_delayed_scope_hooks(void)
934 struct symbol *fn = get_base_type(cur_func_sym);
935 struct statement *stmt;
937 if (!fn)
938 return 0;
939 stmt = fn->stmt;
940 if (!stmt)
941 stmt = fn->inline_stmt;
942 if (stmt && stmt->type == STMT_COMPOUND)
943 return 1;
944 return 0;
947 void __split_label_stmt(struct statement *stmt)
949 if (stmt->label_identifier &&
950 stmt->label_identifier->type == SYM_LABEL &&
951 stmt->label_identifier->ident) {
952 loop_count |= 0x80000000;
953 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
957 static void find_asm_gotos(struct statement *stmt)
959 struct symbol *sym;
961 FOR_EACH_PTR(stmt->asm_labels, sym) {
962 __save_gotos(sym->ident->name, sym);
963 } END_FOR_EACH_PTR(sym);
966 void __split_stmt(struct statement *stmt)
968 sval_t sval;
970 if (!stmt)
971 goto out;
973 if (__bail_on_rest_of_function)
974 return;
976 if (out_of_memory() || taking_too_long()) {
978 __bail_on_rest_of_function = 1;
979 final_pass = 1;
980 sm_msg("Function too hairy. Giving up.");
981 fake_a_return();
982 final_pass = 0; /* turn off sm_msg() from here */
983 return;
986 add_ptr_list(&big_statement_stack, stmt);
987 free_expression_stack(&big_expression_stack);
988 set_position(stmt->pos);
989 __pass_to_client(stmt, STMT_HOOK);
991 switch (stmt->type) {
992 case STMT_DECLARATION:
993 split_declaration(stmt->declaration);
994 break;
995 case STMT_RETURN:
996 __split_expr(stmt->ret_value);
997 __pass_to_client(stmt->ret_value, RETURN_HOOK);
998 __process_post_op_stack();
999 nullify_path();
1000 break;
1001 case STMT_EXPRESSION:
1002 __split_expr(stmt->expression);
1003 break;
1004 case STMT_COMPOUND:
1005 split_compound(stmt);
1006 break;
1007 case STMT_IF:
1008 set_parent_stmt(stmt->if_true, stmt);
1009 set_parent_stmt(stmt->if_false, 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 set_parent_stmt(stmt->iterator_pre_statement, stmt);
1032 set_parent_stmt(stmt->iterator_statement, stmt);
1033 set_parent_stmt(stmt->iterator_post_statement, stmt);
1035 if (stmt->iterator_pre_condition)
1036 handle_pre_loop(stmt);
1037 else if (stmt->iterator_post_condition)
1038 handle_post_loop(stmt);
1039 else {
1040 // these are for(;;) type loops.
1041 handle_pre_loop(stmt);
1043 break;
1044 case STMT_SWITCH:
1045 set_parent_stmt(stmt->switch_statement, stmt);
1047 if (get_value(stmt->switch_expression, &sval)) {
1048 split_known_switch(stmt, sval);
1049 break;
1051 __split_expr(stmt->switch_expression);
1052 push_expression(&switch_expr_stack, stmt->switch_expression);
1053 __save_switch_states(top_expression(switch_expr_stack));
1054 nullify_path();
1055 __push_default();
1056 __push_breaks();
1057 __split_stmt(stmt->switch_statement);
1058 if (!__pop_default())
1059 fake_an_empty_default(stmt->pos);
1060 __discard_switches();
1061 __merge_breaks();
1062 pop_expression(&switch_expr_stack);
1063 break;
1064 case STMT_CASE:
1065 split_case(stmt);
1066 break;
1067 case STMT_LABEL:
1068 __split_label_stmt(stmt);
1069 __split_stmt(stmt->label_statement);
1070 break;
1071 case STMT_GOTO:
1072 __split_expr(stmt->goto_expression);
1073 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1074 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1075 __process_breaks();
1076 } else if (!strcmp(stmt->goto_label->ident->name,
1077 "continue")) {
1078 __process_continues();
1080 } else if (stmt->goto_label &&
1081 stmt->goto_label->type == SYM_LABEL &&
1082 stmt->goto_label->ident) {
1083 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1085 nullify_path();
1086 if (is_last_stmt(stmt))
1087 handle_backward_goto(stmt);
1088 break;
1089 case STMT_NONE:
1090 break;
1091 case STMT_ASM:
1092 find_asm_gotos(stmt);
1093 __pass_to_client(stmt, ASM_HOOK);
1094 __split_expr(stmt->asm_string);
1095 split_asm_constraints(stmt->asm_outputs);
1096 split_asm_constraints(stmt->asm_inputs);
1097 split_asm_constraints(stmt->asm_clobbers);
1098 break;
1099 case STMT_CONTEXT:
1100 break;
1101 case STMT_RANGE:
1102 __split_expr(stmt->range_expression);
1103 __split_expr(stmt->range_low);
1104 __split_expr(stmt->range_high);
1105 break;
1107 __pass_to_client(stmt, STMT_HOOK_AFTER);
1108 out:
1109 __process_post_op_stack();
1112 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1114 struct expression *expr;
1116 FOR_EACH_PTR(expr_list, expr) {
1117 set_parent_expr(expr, parent);
1118 __split_expr(expr);
1119 __process_post_op_stack();
1120 } END_FOR_EACH_PTR(expr);
1123 static void split_sym(struct symbol *sym)
1125 if (!sym)
1126 return;
1127 if (!(sym->namespace & NS_SYMBOL))
1128 return;
1130 __split_stmt(sym->stmt);
1131 __split_expr(sym->array_size);
1132 split_symlist(sym->arguments);
1133 split_symlist(sym->symbol_list);
1134 __split_stmt(sym->inline_stmt);
1135 split_symlist(sym->inline_symbol_list);
1138 static void split_symlist(struct symbol_list *sym_list)
1140 struct symbol *sym;
1142 FOR_EACH_PTR(sym_list, sym) {
1143 split_sym(sym);
1144 } END_FOR_EACH_PTR(sym);
1147 typedef void (fake_cb)(struct expression *expr);
1149 static int member_to_number(struct expression *expr, struct ident *member)
1151 struct symbol *type, *tmp;
1152 char *name;
1153 int i;
1155 if (!member)
1156 return -1;
1157 name = member->name;
1159 type = get_type(expr);
1160 if (!type || type->type != SYM_STRUCT)
1161 return -1;
1163 i = -1;
1164 FOR_EACH_PTR(type->symbol_list, tmp) {
1165 i++;
1166 if (!tmp->ident)
1167 continue;
1168 if (strcmp(name, tmp->ident->name) == 0)
1169 return i;
1170 } END_FOR_EACH_PTR(tmp);
1171 return -1;
1174 static struct ident *number_to_member(struct expression *expr, int num)
1176 struct symbol *type, *member;
1177 int i = 0;
1179 type = get_type(expr);
1180 if (!type || type->type != SYM_STRUCT)
1181 return NULL;
1183 FOR_EACH_PTR(type->symbol_list, member) {
1184 if (i == num)
1185 return member->ident;
1186 i++;
1187 } END_FOR_EACH_PTR(member);
1188 return NULL;
1191 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1193 struct member_set {
1194 struct ident *ident;
1195 int set;
1198 static struct member_set *alloc_member_set(struct symbol *type)
1200 struct member_set *member_set;
1201 struct symbol *member;
1202 int member_count;
1203 int member_idx;
1205 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1206 member_set = malloc(member_count * sizeof(*member_set));
1207 member_idx = 0;
1208 FOR_EACH_PTR(type->symbol_list, member) {
1209 member_set[member_idx].ident = member->ident;
1210 member_set[member_idx].set = 0;
1211 member_idx++;
1212 } END_FOR_EACH_PTR(member);
1214 return member_set;
1217 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1219 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1220 int i;
1222 for (i = 0; i < member_count; i++) {
1223 if (member_set[i].ident == ident) {
1224 member_set[i].set = 1;
1225 return;
1228 // crap. this is buggy.
1229 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1232 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1234 struct expression *edge_member, *assign;
1235 struct symbol *base = get_real_base_type(member);
1236 struct symbol *tmp;
1238 if (member->ident)
1239 expr = member_expression(expr, '.', member->ident);
1241 FOR_EACH_PTR(base->symbol_list, tmp) {
1242 struct symbol *type;
1244 type = get_real_base_type(tmp);
1245 if (!type)
1246 continue;
1248 edge_member = member_expression(expr, '.', tmp->ident);
1249 if (get_state_expr(SMATCH_EXTRA, edge_member))
1250 continue;
1252 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1253 set_inner_struct_members(expr, tmp);
1254 continue;
1257 if (!tmp->ident)
1258 continue;
1260 assign = assign_expression(edge_member, zero_expr());
1261 __split_expr(assign);
1262 } END_FOR_EACH_PTR(tmp);
1267 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1269 struct symbol *tmp;
1270 struct expression *member = NULL;
1271 struct expression *assign;
1272 int op = '*';
1274 if (expr->type == EXPR_PREOP && expr->op == '&') {
1275 expr = strip_expr(expr->unop);
1276 op = '.';
1279 FOR_EACH_PTR(type->symbol_list, tmp) {
1280 type = get_real_base_type(tmp);
1281 if (!type)
1282 continue;
1284 if (tmp->ident) {
1285 member = member_expression(expr, op, tmp->ident);
1286 if (get_state_expr(SMATCH_EXTRA, member))
1287 continue;
1290 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1291 set_inner_struct_members(expr, tmp);
1292 continue;
1294 if (type->type == SYM_ARRAY)
1295 continue;
1296 if (!tmp->ident)
1297 continue;
1299 assign = assign_expression(member, zero_expr());
1300 __split_expr(assign);
1301 } END_FOR_EACH_PTR(tmp);
1304 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1306 struct expression *deref, *assign, *tmp;
1307 struct symbol *struct_type, *type;
1308 struct ident *member;
1309 int member_idx;
1310 struct member_set *member_set;
1312 struct_type = get_type(symbol);
1313 if (!struct_type ||
1314 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1315 return;
1317 member_set = alloc_member_set(struct_type);
1319 member_idx = 0;
1320 FOR_EACH_PTR(members, tmp) {
1321 member = number_to_member(symbol, member_idx);
1322 while (tmp->type == EXPR_IDENTIFIER) {
1323 member = tmp->expr_ident;
1324 member_idx = member_to_number(symbol, member);
1325 tmp = tmp->ident_expression;
1327 mark_member_as_set(struct_type, member_set, member);
1328 member_idx++;
1329 deref = member_expression(symbol, '.', member);
1330 if (tmp->type == EXPR_INITIALIZER) {
1331 type = get_type(deref);
1332 if (type && type->type == SYM_ARRAY)
1333 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1334 else
1335 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1336 } else {
1337 assign = assign_expression(deref, tmp);
1338 fake_cb(assign);
1340 } END_FOR_EACH_PTR(tmp);
1342 set_unset_to_zero(struct_type, symbol);
1345 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1347 fake_member_assigns_helper(symbol_expression(sym),
1348 sym->initializer->expr_list, fake_cb);
1351 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1353 struct expression *offset, *binop, *assign, *tmp;
1354 struct symbol *type;
1355 int idx;
1357 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1358 return;
1360 idx = 0;
1361 FOR_EACH_PTR(expr_list, tmp) {
1362 if (tmp->type == EXPR_INDEX) {
1363 if (tmp->idx_from != tmp->idx_to)
1364 return;
1365 idx = tmp->idx_from;
1366 if (!tmp->idx_expression)
1367 goto next;
1368 tmp = tmp->idx_expression;
1370 offset = value_expr(idx);
1371 binop = array_element_expression(array, offset);
1372 if (tmp->type == EXPR_INITIALIZER) {
1373 type = get_type(binop);
1374 if (type && type->type == SYM_ARRAY)
1375 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1376 else
1377 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1378 } else {
1379 assign = assign_expression(binop, tmp);
1380 fake_cb(assign);
1382 next:
1383 idx++;
1384 } END_FOR_EACH_PTR(tmp);
1387 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1389 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1392 static void fake_assign_expr(struct symbol *sym)
1394 struct expression *assign, *symbol;
1396 symbol = symbol_expression(sym);
1397 assign = assign_expression(symbol, sym->initializer);
1398 __split_expr(assign);
1401 static void call_split_expr(struct expression *expr)
1403 __split_expr(expr);
1406 static void do_initializer_stuff(struct symbol *sym)
1408 if (!sym->initializer)
1409 return;
1411 if (sym->initializer->type == EXPR_INITIALIZER) {
1412 if (get_real_base_type(sym)->type == SYM_ARRAY)
1413 fake_element_assigns(sym, call_split_expr);
1414 else
1415 fake_member_assigns(sym, call_split_expr);
1416 } else {
1417 fake_assign_expr(sym);
1421 static void split_declaration(struct symbol_list *sym_list)
1423 struct symbol *sym;
1425 FOR_EACH_PTR(sym_list, sym) {
1426 __pass_to_client(sym, DECLARATION_HOOK);
1427 do_initializer_stuff(sym);
1428 split_sym(sym);
1429 } END_FOR_EACH_PTR(sym);
1432 static void call_global_assign_hooks(struct expression *assign)
1434 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1437 static void fake_global_assign(struct symbol *sym)
1439 struct expression *assign, *symbol;
1441 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1442 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1443 fake_element_assigns(sym, call_global_assign_hooks);
1444 } else if (sym->initializer) {
1445 symbol = symbol_expression(sym);
1446 assign = assign_expression(symbol, sym->initializer);
1447 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1448 } else {
1449 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1451 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1452 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1453 fake_member_assigns(sym, call_global_assign_hooks);
1454 } else if (sym->initializer) {
1455 symbol = symbol_expression(sym);
1456 assign = assign_expression(symbol, sym->initializer);
1457 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1458 } else {
1459 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1461 } else {
1462 symbol = symbol_expression(sym);
1463 if (sym->initializer)
1464 assign = assign_expression(symbol, sym->initializer);
1465 else
1466 assign = assign_expression(symbol, zero_expr());
1467 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1471 static void start_function_definition(struct symbol *sym)
1473 __in_function_def = 1;
1474 __pass_to_client(sym, FUNC_DEF_HOOK);
1475 __in_function_def = 0;
1476 __pass_to_client(sym, AFTER_DEF_HOOK);
1480 static void split_function(struct symbol *sym)
1482 struct symbol *base_type = get_base_type(sym);
1484 if (!base_type->stmt && !base_type->inline_stmt)
1485 return;
1487 gettimeofday(&fn_start_time, NULL);
1488 cur_func_sym = sym;
1489 if (sym->ident)
1490 cur_func = sym->ident->name;
1491 set_position(sym->pos);
1492 loop_count = 0;
1493 last_goto_statement_handled = 0;
1494 sm_debug("new function: %s\n", cur_func);
1495 __stree_id = 0;
1496 if (option_two_passes) {
1497 __unnullify_path();
1498 loop_num = 0;
1499 final_pass = 0;
1500 start_function_definition(sym);
1501 __split_stmt(base_type->stmt);
1502 __split_stmt(base_type->inline_stmt);
1503 nullify_path();
1505 __unnullify_path();
1506 loop_num = 0;
1507 final_pass = 1;
1508 start_function_definition(sym);
1509 __split_stmt(base_type->stmt);
1510 __split_stmt(base_type->inline_stmt);
1511 __pass_to_client(sym, END_FUNC_HOOK);
1512 if (need_delayed_scope_hooks())
1513 __call_scope_hooks();
1514 __pass_to_client(sym, AFTER_FUNC_HOOK);
1516 clear_all_states();
1517 cur_func_sym = NULL;
1518 cur_func = NULL;
1519 free_data_info_allocs();
1520 free_expression_stack(&switch_expr_stack);
1521 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1522 __bail_on_rest_of_function = 0;
1525 static void parse_inline(struct expression *call)
1527 struct symbol *base_type;
1528 int loop_num_bak = loop_num;
1529 int loop_count_bak = loop_count;
1530 int final_pass_bak = final_pass;
1531 char *cur_func_bak = cur_func;
1532 struct statement_list *big_statement_stack_bak = big_statement_stack;
1533 struct expression_list *big_expression_stack_bak = big_expression_stack;
1534 struct expression_list *big_condition_stack_bak = big_condition_stack;
1535 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1536 struct symbol *cur_func_sym_bak = cur_func_sym;
1538 __pass_to_client(call, INLINE_FN_START);
1539 final_pass = 0; /* don't print anything */
1540 __inline_fn = call;
1542 base_type = get_base_type(call->fn->symbol);
1543 cur_func_sym = call->fn->symbol;
1544 if (call->fn->symbol->ident)
1545 cur_func = call->fn->symbol->ident->name;
1546 else
1547 cur_func = NULL;
1548 set_position(call->fn->symbol->pos);
1550 save_all_states();
1551 big_statement_stack = NULL;
1552 big_expression_stack = NULL;
1553 big_condition_stack = NULL;
1554 switch_expr_stack = NULL;
1556 sm_debug("inline function: %s\n", cur_func);
1557 __unnullify_path();
1558 loop_num = 0;
1559 start_function_definition(call->fn->symbol);
1560 __split_stmt(base_type->stmt);
1561 __split_stmt(base_type->inline_stmt);
1562 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1563 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1565 free_expression_stack(&switch_expr_stack);
1566 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1567 nullify_path();
1568 free_goto_stack();
1570 loop_num = loop_num_bak;
1571 loop_count = loop_count_bak;
1572 final_pass = final_pass_bak;
1573 cur_func_sym = cur_func_sym_bak;
1574 cur_func = cur_func_bak;
1575 big_statement_stack = big_statement_stack_bak;
1576 big_expression_stack = big_expression_stack_bak;
1577 big_condition_stack = big_condition_stack_bak;
1578 switch_expr_stack = switch_expr_stack_bak;
1580 restore_all_states();
1581 set_position(call->pos);
1582 __inline_fn = NULL;
1583 __pass_to_client(call, INLINE_FN_END);
1586 static struct symbol_list *inlines_called;
1587 static void add_inline_function(struct symbol *sym)
1589 static struct symbol_list *already_added;
1590 struct symbol *tmp;
1592 FOR_EACH_PTR(already_added, tmp) {
1593 if (tmp == sym)
1594 return;
1595 } END_FOR_EACH_PTR(tmp);
1597 add_ptr_list(&already_added, sym);
1598 add_ptr_list(&inlines_called, sym);
1601 static void process_inlines(void)
1603 struct symbol *tmp;
1605 FOR_EACH_PTR(inlines_called, tmp) {
1606 split_function(tmp);
1607 } END_FOR_EACH_PTR(tmp);
1608 free_ptr_list(&inlines_called);
1611 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1613 struct symbol *sym;
1615 FOR_EACH_PTR_REVERSE(big_list, sym) {
1616 if (!sym->scope)
1617 continue;
1618 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1619 return sym;
1620 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1621 return sym;
1622 } END_FOR_EACH_PTR_REVERSE(sym);
1624 return NULL;
1627 static void split_inlines_in_scope(struct symbol *sym)
1629 struct symbol *base;
1630 struct symbol_list *scope_list;
1631 int stream;
1633 scope_list = sym->scope->symbols;
1634 stream = sym->pos.stream;
1636 /* find the last static symbol in the file */
1637 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1638 if (sym->pos.stream != stream)
1639 continue;
1640 if (sym->type != SYM_NODE)
1641 continue;
1642 base = get_base_type(sym);
1643 if (!base)
1644 continue;
1645 if (base->type != SYM_FN)
1646 continue;
1647 if (!base->inline_stmt)
1648 continue;
1649 add_inline_function(sym);
1650 } END_FOR_EACH_PTR_REVERSE(sym);
1652 process_inlines();
1655 static void split_inlines(struct symbol_list *sym_list)
1657 struct symbol *sym;
1659 sym = get_last_scoped_symbol(sym_list, 0);
1660 if (sym)
1661 split_inlines_in_scope(sym);
1662 sym = get_last_scoped_symbol(sym_list, 1);
1663 if (sym)
1664 split_inlines_in_scope(sym);
1667 static struct stree *clone_estates_perm(struct stree *orig)
1669 struct stree *ret = NULL;
1670 struct sm_state *tmp;
1672 FOR_EACH_SM(orig, tmp) {
1673 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1674 } END_FOR_EACH_SM(tmp);
1676 return ret;
1679 struct position last_pos;
1680 static void split_functions(struct symbol_list *sym_list)
1682 struct symbol *sym;
1684 __unnullify_path();
1685 FOR_EACH_PTR(sym_list, sym) {
1686 set_position(sym->pos);
1687 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1688 __pass_to_client(sym, BASE_HOOK);
1689 fake_global_assign(sym);
1691 } END_FOR_EACH_PTR(sym);
1692 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1693 nullify_path();
1695 FOR_EACH_PTR(sym_list, sym) {
1696 set_position(sym->pos);
1697 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1698 split_function(sym);
1699 process_inlines();
1701 last_pos = sym->pos;
1702 } END_FOR_EACH_PTR(sym);
1703 split_inlines(sym_list);
1704 __pass_to_client(sym_list, END_FILE_HOOK);
1707 void smatch(int argc, char **argv)
1709 struct string_list *filelist = NULL;
1710 struct symbol_list *sym_list;
1711 struct timeval stop, start;
1713 gettimeofday(&start, NULL);
1715 if (argc < 2) {
1716 printf("Usage: smatch [--debug] <filename.c>\n");
1717 exit(1);
1719 sparse_initialize(argc, argv, &filelist);
1720 set_valid_ptr_max();
1721 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1722 if (option_file_output) {
1723 char buf[256];
1725 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1726 sm_outfd = fopen(buf, "w");
1727 if (!sm_outfd) {
1728 printf("Error: Cannot open %s\n", base_file);
1729 exit(1);
1732 sym_list = sparse_keep_tokens(base_file);
1733 split_functions(sym_list);
1734 } END_FOR_EACH_PTR_NOTAG(base_file);
1736 gettimeofday(&stop, NULL);
1738 set_position(last_pos);
1739 if (option_time)
1740 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);