db/fixup_kernel.sh: ignore do_loop_readv_writev()
[smatch.git] / smatch_flow.c
blob250528f4b3a9702cc380f6daa77400cddf6b175c
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 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
404 __split_expr(expr->left);
405 break;
407 case EXPR_DEREF:
408 set_parent_expr(expr->deref, expr);
410 __pass_to_client(expr, DEREF_HOOK);
411 __split_expr(expr->deref);
412 break;
413 case EXPR_SLICE:
414 set_parent_expr(expr->base, expr);
416 __split_expr(expr->base);
417 break;
418 case EXPR_CAST:
419 case EXPR_FORCE_CAST:
420 set_parent_expr(expr->cast_expression, expr);
422 __pass_to_client(expr, CAST_HOOK);
423 __split_expr(expr->cast_expression);
424 break;
425 case EXPR_SIZEOF:
426 if (expr->cast_expression)
427 __pass_to_client(strip_parens(expr->cast_expression),
428 SIZEOF_HOOK);
429 break;
430 case EXPR_OFFSETOF:
431 case EXPR_ALIGNOF:
432 evaluate_expression(expr);
433 break;
434 case EXPR_CONDITIONAL:
435 case EXPR_SELECT:
436 set_parent_expr(expr->conditional, expr);
437 set_parent_expr(expr->cond_true, expr);
438 set_parent_expr(expr->cond_false, expr);
440 if (known_condition_true(expr->conditional)) {
441 __split_expr(expr->cond_true);
442 break;
444 if (known_condition_false(expr->conditional)) {
445 __split_expr(expr->cond_false);
446 break;
448 __pass_to_client(expr, SELECT_HOOK);
449 __split_whole_condition(expr->conditional);
450 __split_expr(expr->cond_true);
451 __push_true_states();
452 __use_false_states();
453 __split_expr(expr->cond_false);
454 __merge_true_states();
455 break;
456 case EXPR_CALL:
457 set_parent_expr(expr->fn, expr);
459 if (sym_name_is("__builtin_constant_p", expr->fn))
460 break;
461 split_expr_list(expr->args, expr);
462 __split_expr(expr->fn);
463 if (is_inline_func(expr->fn))
464 add_inline_function(expr->fn->symbol);
465 if (inlinable(expr->fn))
466 __inline_call = 1;
467 __process_post_op_stack();
468 __pass_to_client(expr, FUNCTION_CALL_HOOK);
469 __inline_call = 0;
470 if (inlinable(expr->fn)) {
471 parse_inline(expr);
473 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
474 if (is_noreturn_func(expr->fn))
475 nullify_path();
476 break;
477 case EXPR_INITIALIZER:
478 split_expr_list(expr->expr_list, expr);
479 break;
480 case EXPR_IDENTIFIER:
481 set_parent_expr(expr->ident_expression, expr);
482 __split_expr(expr->ident_expression);
483 break;
484 case EXPR_INDEX:
485 set_parent_expr(expr->idx_expression, expr);
486 __split_expr(expr->idx_expression);
487 break;
488 case EXPR_POS:
489 set_parent_expr(expr->init_expr, expr);
490 __split_expr(expr->init_expr);
491 break;
492 case EXPR_SYMBOL:
493 __pass_to_client(expr, SYM_HOOK);
494 break;
495 case EXPR_STRING:
496 __pass_to_client(expr, STRING_HOOK);
497 break;
498 default:
499 break;
501 pop_expression(&big_expression_stack);
504 static int is_forever_loop(struct statement *stmt)
506 struct expression *expr;
507 sval_t sval;
509 expr = strip_expr(stmt->iterator_pre_condition);
510 if (!expr)
511 expr = stmt->iterator_post_condition;
512 if (!expr) {
513 /* this is a for(;;) loop... */
514 return 1;
517 if (get_value(expr, &sval) && sval.value != 0)
518 return 1;
520 return 0;
523 static int loop_num;
524 static char *get_loop_name(int num)
526 char buf[256];
528 snprintf(buf, 255, "-loop%d", num);
529 buf[255] = '\0';
530 return alloc_sname(buf);
534 * Pre Loops are while and for loops.
536 static void handle_pre_loop(struct statement *stmt)
538 int once_through; /* we go through the loop at least once */
539 struct sm_state *extra_sm = NULL;
540 int unchanged = 0;
541 char *loop_name;
542 struct stree *stree = NULL;
543 struct sm_state *sm = NULL;
545 loop_name = get_loop_name(loop_num);
546 loop_num++;
548 __split_stmt(stmt->iterator_pre_statement);
549 __prev_stmt = stmt->iterator_pre_statement;
551 once_through = implied_condition_true(stmt->iterator_pre_condition);
553 loop_count++;
554 __push_continues();
555 __push_breaks();
557 __merge_gotos(loop_name, NULL);
559 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
560 __in_pre_condition++;
561 __pass_to_client(stmt, PRELOOP_HOOK);
562 __split_whole_condition(stmt->iterator_pre_condition);
563 __in_pre_condition--;
564 FOR_EACH_SM(stree, sm) {
565 set_state(sm->owner, sm->name, sm->sym, sm->state);
566 } END_FOR_EACH_SM(sm);
567 free_stree(&stree);
568 if (extra_sm)
569 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
571 if (option_assume_loops)
572 once_through = 1;
574 __split_stmt(stmt->iterator_statement);
575 if (is_forever_loop(stmt)) {
576 __merge_continues();
577 __save_gotos(loop_name, NULL);
579 __push_fake_cur_stree();
580 __split_stmt(stmt->iterator_post_statement);
581 stree = __pop_fake_cur_stree();
583 __discard_false_states();
584 __use_breaks();
586 if (!__path_is_null())
587 __merge_stree_into_cur(stree);
588 free_stree(&stree);
589 } else {
590 __merge_continues();
591 unchanged = __iterator_unchanged(extra_sm);
592 __split_stmt(stmt->iterator_post_statement);
593 __prev_stmt = stmt->iterator_post_statement;
594 __cur_stmt = stmt;
596 __save_gotos(loop_name, NULL);
597 __in_pre_condition++;
598 __split_whole_condition(stmt->iterator_pre_condition);
599 __in_pre_condition--;
600 nullify_path();
601 __merge_false_states();
602 if (once_through)
603 __discard_false_states();
604 else
605 __merge_false_states();
607 if (extra_sm && unchanged)
608 __extra_pre_loop_hook_after(extra_sm,
609 stmt->iterator_post_statement,
610 stmt->iterator_pre_condition);
611 __merge_breaks();
613 loop_count--;
617 * Post loops are do {} while();
619 static void handle_post_loop(struct statement *stmt)
621 char *loop_name;
623 loop_name = get_loop_name(loop_num);
624 loop_num++;
625 loop_count++;
627 __push_continues();
628 __push_breaks();
629 __merge_gotos(loop_name, NULL);
630 __split_stmt(stmt->iterator_statement);
631 __merge_continues();
632 if (!is_zero(stmt->iterator_post_condition))
633 __save_gotos(loop_name, NULL);
635 if (is_forever_loop(stmt)) {
636 __use_breaks();
637 } else {
638 __split_whole_condition(stmt->iterator_post_condition);
639 __use_false_states();
640 __merge_breaks();
642 loop_count--;
645 static int empty_statement(struct statement *stmt)
647 if (!stmt)
648 return 0;
649 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
650 return 1;
651 return 0;
654 static int last_stmt_on_same_line(void)
656 struct statement *stmt;
657 int i = 0;
659 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
660 if (!i++)
661 continue;
662 if (stmt->pos.line == get_lineno())
663 return 1;
664 return 0;
665 } END_FOR_EACH_PTR_REVERSE(stmt);
666 return 0;
669 static void split_asm_constraints(struct expression_list *expr_list)
671 struct expression *expr;
672 int state = 0;
674 FOR_EACH_PTR(expr_list, expr) {
675 switch (state) {
676 case 0: /* identifier */
677 case 1: /* constraint */
678 state++;
679 continue;
680 case 2: /* expression */
681 state = 0;
682 __split_expr(expr);
683 continue;
685 } END_FOR_EACH_PTR(expr);
688 static int is_case_val(struct statement *stmt, sval_t sval)
690 sval_t case_sval;
692 if (stmt->type != STMT_CASE)
693 return 0;
694 if (!stmt->case_expression) {
695 __set_default();
696 return 1;
698 if (!get_value(stmt->case_expression, &case_sval))
699 return 0;
700 if (case_sval.value == sval.value)
701 return 1;
702 return 0;
705 static struct range_list *get_case_rl(struct expression *switch_expr,
706 struct expression *case_expr,
707 struct expression *case_to)
709 sval_t start, end;
710 struct range_list *rl = NULL;
711 struct symbol *switch_type;
713 switch_type = get_type(switch_expr);
714 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
715 start = sval_cast(switch_type, start);
716 end = sval_cast(switch_type, end);
717 add_range(&rl, start, end);
718 } else if (get_value(case_expr, &start)) {
719 start = sval_cast(switch_type, start);
720 add_range(&rl, start, start);
723 return rl;
726 static void split_known_switch(struct statement *stmt, sval_t sval)
728 struct statement *tmp;
729 struct range_list *rl;
731 __split_expr(stmt->switch_expression);
732 sval = sval_cast(get_type(stmt->switch_expression), sval);
734 push_expression(&switch_expr_stack, stmt->switch_expression);
735 __save_switch_states(top_expression(switch_expr_stack));
736 nullify_path();
737 __push_default();
738 __push_breaks();
740 stmt = stmt->switch_statement;
742 __push_scope_hooks();
743 FOR_EACH_PTR(stmt->stmts, tmp) {
744 __smatch_lineno = tmp->pos.line;
745 if (is_case_val(tmp, sval)) {
746 rl = alloc_rl(sval, sval);
747 __merge_switches(top_expression(switch_expr_stack), rl);
748 __pass_case_to_client(top_expression(switch_expr_stack), rl);
750 if (__path_is_null())
751 continue;
752 __split_stmt(tmp);
753 if (__path_is_null()) {
754 __set_default();
755 goto out;
757 } END_FOR_EACH_PTR(tmp);
758 out:
759 __call_scope_hooks();
760 if (!__pop_default())
761 __merge_switches(top_expression(switch_expr_stack), NULL);
762 __discard_switches();
763 __merge_breaks();
764 pop_expression(&switch_expr_stack);
767 static void split_case(struct statement *stmt)
769 struct range_list *rl = NULL;
771 rl = get_case_rl(top_expression(switch_expr_stack),
772 stmt->case_expression, stmt->case_to);
773 while (stmt->case_statement->type == STMT_CASE) {
774 struct range_list *tmp;
776 tmp = get_case_rl(top_expression(switch_expr_stack),
777 stmt->case_statement->case_expression,
778 stmt->case_statement->case_to);
779 if (!tmp)
780 break;
781 rl = rl_union(rl, tmp);
782 if (!stmt->case_expression)
783 __set_default();
784 stmt = stmt->case_statement;
787 __merge_switches(top_expression(switch_expr_stack), rl);
789 if (!stmt->case_expression)
790 __set_default();
791 __split_stmt(stmt->case_statement);
794 static int taking_too_long(void)
796 int ms;
798 ms = ms_since(&fn_start_time);
799 if (ms > 1000 * 60 * 5) /* five minutes */
800 return 1;
801 return 0;
804 static int is_last_stmt(struct statement *cur_stmt)
806 struct symbol *fn = get_base_type(cur_func_sym);
807 struct statement *stmt;
809 if (!fn)
810 return 0;
811 stmt = fn->stmt;
812 if (!stmt)
813 stmt = fn->inline_stmt;
814 if (!stmt || stmt->type != STMT_COMPOUND)
815 return 0;
816 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
817 if (stmt && stmt->type == STMT_LABEL)
818 stmt = stmt->label_statement;
819 if (stmt == cur_stmt)
820 return 1;
821 return 0;
824 static void handle_backward_goto(struct statement *goto_stmt)
826 const char *goto_name, *label_name;
827 struct statement *func_stmt;
828 struct symbol *base_type = get_base_type(cur_func_sym);
829 struct statement *tmp;
830 int found = 0;
832 if (!option_info)
833 return;
834 if (last_goto_statement_handled)
835 return;
836 last_goto_statement_handled = 1;
838 if (!goto_stmt->goto_label ||
839 goto_stmt->goto_label->type != SYM_LABEL ||
840 !goto_stmt->goto_label->ident)
841 return;
842 goto_name = goto_stmt->goto_label->ident->name;
844 func_stmt = base_type->stmt;
845 if (!func_stmt)
846 func_stmt = base_type->inline_stmt;
847 if (!func_stmt)
848 return;
849 if (func_stmt->type != STMT_COMPOUND)
850 return;
852 FOR_EACH_PTR(func_stmt->stmts, tmp) {
853 if (!found) {
854 if (tmp->type != STMT_LABEL)
855 continue;
856 if (!tmp->label_identifier ||
857 tmp->label_identifier->type != SYM_LABEL ||
858 !tmp->label_identifier->ident)
859 continue;
860 label_name = tmp->label_identifier->ident->name;
861 if (strcmp(goto_name, label_name) != 0)
862 continue;
863 found = 1;
865 __split_stmt(tmp);
866 } END_FOR_EACH_PTR(tmp);
869 static void fake_a_return(void)
871 struct symbol *return_type;
873 nullify_path();
874 __unnullify_path();
876 return_type = get_real_base_type(cur_func_sym);
877 return_type = get_real_base_type(return_type);
878 if (return_type != &void_ctype) {
879 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
880 nullify_path();
884 static void fake_an_empty_default(struct position pos)
886 static struct statement none = {};
888 none.pos = pos;
889 none.type = STMT_NONE;
890 __merge_switches(top_expression(switch_expr_stack), NULL);
891 __split_stmt(&none);
894 static void split_compound(struct statement *stmt)
896 struct statement *prev = NULL;
897 struct statement *cur = NULL;
898 struct statement *next;
900 __push_scope_hooks();
902 FOR_EACH_PTR(stmt->stmts, next) {
903 /* just set them all ahead of time */
904 set_parent_stmt(next, stmt);
906 if (cur) {
907 __prev_stmt = prev;
908 __next_stmt = next;
909 __cur_stmt = cur;
910 __split_stmt(cur);
912 prev = cur;
913 cur = next;
914 } END_FOR_EACH_PTR(next);
915 if (cur) {
916 __prev_stmt = prev;
917 __cur_stmt = cur;
918 __next_stmt = NULL;
919 __split_stmt(cur);
923 * For function scope, then delay calling the scope hooks until the
924 * end of function hooks can run. I'm not positive this is the right
925 * thing...
927 if (!is_last_stmt(cur))
928 __call_scope_hooks();
932 * This is a hack, work around for detecting empty functions.
934 static int need_delayed_scope_hooks(void)
936 struct symbol *fn = get_base_type(cur_func_sym);
937 struct statement *stmt;
939 if (!fn)
940 return 0;
941 stmt = fn->stmt;
942 if (!stmt)
943 stmt = fn->inline_stmt;
944 if (stmt && stmt->type == STMT_COMPOUND)
945 return 1;
946 return 0;
949 void __split_label_stmt(struct statement *stmt)
951 if (stmt->label_identifier &&
952 stmt->label_identifier->type == SYM_LABEL &&
953 stmt->label_identifier->ident) {
954 loop_count |= 0x80000000;
955 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
959 static void find_asm_gotos(struct statement *stmt)
961 struct symbol *sym;
963 FOR_EACH_PTR(stmt->asm_labels, sym) {
964 __save_gotos(sym->ident->name, sym);
965 } END_FOR_EACH_PTR(sym);
968 void __split_stmt(struct statement *stmt)
970 sval_t sval;
972 if (!stmt)
973 goto out;
975 if (__bail_on_rest_of_function)
976 return;
978 if (out_of_memory() || taking_too_long()) {
980 __bail_on_rest_of_function = 1;
981 final_pass = 1;
982 sm_msg("Function too hairy. Giving up.");
983 fake_a_return();
984 final_pass = 0; /* turn off sm_msg() from here */
985 return;
988 add_ptr_list(&big_statement_stack, stmt);
989 free_expression_stack(&big_expression_stack);
990 set_position(stmt->pos);
991 __pass_to_client(stmt, STMT_HOOK);
993 switch (stmt->type) {
994 case STMT_DECLARATION:
995 split_declaration(stmt->declaration);
996 break;
997 case STMT_RETURN:
998 __split_expr(stmt->ret_value);
999 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1000 __process_post_op_stack();
1001 nullify_path();
1002 break;
1003 case STMT_EXPRESSION:
1004 __split_expr(stmt->expression);
1005 break;
1006 case STMT_COMPOUND:
1007 split_compound(stmt);
1008 break;
1009 case STMT_IF:
1010 set_parent_stmt(stmt->if_true, stmt);
1011 set_parent_stmt(stmt->if_false, stmt);
1013 if (known_condition_true(stmt->if_conditional)) {
1014 __split_stmt(stmt->if_true);
1015 break;
1017 if (known_condition_false(stmt->if_conditional)) {
1018 __split_stmt(stmt->if_false);
1019 break;
1021 __split_whole_condition(stmt->if_conditional);
1022 __split_stmt(stmt->if_true);
1023 if (empty_statement(stmt->if_true) &&
1024 last_stmt_on_same_line() &&
1025 !get_macro_name(stmt->if_true->pos))
1026 sm_msg("warn: if();");
1027 __push_true_states();
1028 __use_false_states();
1029 __split_stmt(stmt->if_false);
1030 __merge_true_states();
1031 break;
1032 case STMT_ITERATOR:
1033 set_parent_stmt(stmt->iterator_pre_statement, stmt);
1034 set_parent_stmt(stmt->iterator_statement, stmt);
1035 set_parent_stmt(stmt->iterator_post_statement, 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 set_parent_stmt(stmt->switch_statement, stmt);
1049 if (get_value(stmt->switch_expression, &sval)) {
1050 split_known_switch(stmt, sval);
1051 break;
1053 __split_expr(stmt->switch_expression);
1054 push_expression(&switch_expr_stack, stmt->switch_expression);
1055 __save_switch_states(top_expression(switch_expr_stack));
1056 nullify_path();
1057 __push_default();
1058 __push_breaks();
1059 __split_stmt(stmt->switch_statement);
1060 if (!__pop_default())
1061 fake_an_empty_default(stmt->pos);
1062 __discard_switches();
1063 __merge_breaks();
1064 pop_expression(&switch_expr_stack);
1065 break;
1066 case STMT_CASE:
1067 split_case(stmt);
1068 break;
1069 case STMT_LABEL:
1070 __split_label_stmt(stmt);
1071 __split_stmt(stmt->label_statement);
1072 break;
1073 case STMT_GOTO:
1074 __split_expr(stmt->goto_expression);
1075 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1076 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1077 __process_breaks();
1078 } else if (!strcmp(stmt->goto_label->ident->name,
1079 "continue")) {
1080 __process_continues();
1082 } else if (stmt->goto_label &&
1083 stmt->goto_label->type == SYM_LABEL &&
1084 stmt->goto_label->ident) {
1085 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1087 nullify_path();
1088 if (is_last_stmt(stmt))
1089 handle_backward_goto(stmt);
1090 break;
1091 case STMT_NONE:
1092 break;
1093 case STMT_ASM:
1094 find_asm_gotos(stmt);
1095 __pass_to_client(stmt, ASM_HOOK);
1096 __split_expr(stmt->asm_string);
1097 split_asm_constraints(stmt->asm_outputs);
1098 split_asm_constraints(stmt->asm_inputs);
1099 split_asm_constraints(stmt->asm_clobbers);
1100 break;
1101 case STMT_CONTEXT:
1102 break;
1103 case STMT_RANGE:
1104 __split_expr(stmt->range_expression);
1105 __split_expr(stmt->range_low);
1106 __split_expr(stmt->range_high);
1107 break;
1109 __pass_to_client(stmt, STMT_HOOK_AFTER);
1110 out:
1111 __process_post_op_stack();
1114 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1116 struct expression *expr;
1118 FOR_EACH_PTR(expr_list, expr) {
1119 set_parent_expr(expr, parent);
1120 __split_expr(expr);
1121 __process_post_op_stack();
1122 } END_FOR_EACH_PTR(expr);
1125 static void split_sym(struct symbol *sym)
1127 if (!sym)
1128 return;
1129 if (!(sym->namespace & NS_SYMBOL))
1130 return;
1132 __split_stmt(sym->stmt);
1133 __split_expr(sym->array_size);
1134 split_symlist(sym->arguments);
1135 split_symlist(sym->symbol_list);
1136 __split_stmt(sym->inline_stmt);
1137 split_symlist(sym->inline_symbol_list);
1140 static void split_symlist(struct symbol_list *sym_list)
1142 struct symbol *sym;
1144 FOR_EACH_PTR(sym_list, sym) {
1145 split_sym(sym);
1146 } END_FOR_EACH_PTR(sym);
1149 typedef void (fake_cb)(struct expression *expr);
1151 static int member_to_number(struct expression *expr, struct ident *member)
1153 struct symbol *type, *tmp;
1154 char *name;
1155 int i;
1157 if (!member)
1158 return -1;
1159 name = member->name;
1161 type = get_type(expr);
1162 if (!type || type->type != SYM_STRUCT)
1163 return -1;
1165 i = -1;
1166 FOR_EACH_PTR(type->symbol_list, tmp) {
1167 i++;
1168 if (!tmp->ident)
1169 continue;
1170 if (strcmp(name, tmp->ident->name) == 0)
1171 return i;
1172 } END_FOR_EACH_PTR(tmp);
1173 return -1;
1176 static struct ident *number_to_member(struct expression *expr, int num)
1178 struct symbol *type, *member;
1179 int i = 0;
1181 type = get_type(expr);
1182 if (!type || type->type != SYM_STRUCT)
1183 return NULL;
1185 FOR_EACH_PTR(type->symbol_list, member) {
1186 if (i == num)
1187 return member->ident;
1188 i++;
1189 } END_FOR_EACH_PTR(member);
1190 return NULL;
1193 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1195 struct member_set {
1196 struct ident *ident;
1197 int set;
1200 static struct member_set *alloc_member_set(struct symbol *type)
1202 struct member_set *member_set;
1203 struct symbol *member;
1204 int member_count;
1205 int member_idx;
1207 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1208 member_set = malloc(member_count * sizeof(*member_set));
1209 member_idx = 0;
1210 FOR_EACH_PTR(type->symbol_list, member) {
1211 member_set[member_idx].ident = member->ident;
1212 member_set[member_idx].set = 0;
1213 member_idx++;
1214 } END_FOR_EACH_PTR(member);
1216 return member_set;
1219 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1221 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1222 int i;
1224 for (i = 0; i < member_count; i++) {
1225 if (member_set[i].ident == ident) {
1226 member_set[i].set = 1;
1227 return;
1230 // crap. this is buggy.
1231 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1234 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1236 struct expression *edge_member, *assign;
1237 struct symbol *base = get_real_base_type(member);
1238 struct symbol *tmp;
1240 if (member->ident)
1241 expr = member_expression(expr, '.', member->ident);
1243 FOR_EACH_PTR(base->symbol_list, tmp) {
1244 struct symbol *type;
1246 type = get_real_base_type(tmp);
1247 if (!type)
1248 continue;
1250 edge_member = member_expression(expr, '.', tmp->ident);
1251 if (get_state_expr(SMATCH_EXTRA, edge_member))
1252 continue;
1254 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1255 set_inner_struct_members(expr, tmp);
1256 continue;
1259 if (!tmp->ident)
1260 continue;
1262 assign = assign_expression(edge_member, zero_expr());
1263 __split_expr(assign);
1264 } END_FOR_EACH_PTR(tmp);
1269 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1271 struct symbol *tmp;
1272 struct expression *member = NULL;
1273 struct expression *assign;
1274 int op = '*';
1276 if (expr->type == EXPR_PREOP && expr->op == '&') {
1277 expr = strip_expr(expr->unop);
1278 op = '.';
1281 FOR_EACH_PTR(type->symbol_list, tmp) {
1282 type = get_real_base_type(tmp);
1283 if (!type)
1284 continue;
1286 if (tmp->ident) {
1287 member = member_expression(expr, op, tmp->ident);
1288 if (get_state_expr(SMATCH_EXTRA, member))
1289 continue;
1292 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1293 set_inner_struct_members(expr, tmp);
1294 continue;
1296 if (type->type == SYM_ARRAY)
1297 continue;
1298 if (!tmp->ident)
1299 continue;
1301 assign = assign_expression(member, zero_expr());
1302 __split_expr(assign);
1303 } END_FOR_EACH_PTR(tmp);
1306 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1308 struct expression *deref, *assign, *tmp;
1309 struct symbol *struct_type, *type;
1310 struct ident *member;
1311 int member_idx;
1312 struct member_set *member_set;
1314 struct_type = get_type(symbol);
1315 if (!struct_type ||
1316 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1317 return;
1319 member_set = alloc_member_set(struct_type);
1321 member_idx = 0;
1322 FOR_EACH_PTR(members, tmp) {
1323 member = number_to_member(symbol, member_idx);
1324 while (tmp->type == EXPR_IDENTIFIER) {
1325 member = tmp->expr_ident;
1326 member_idx = member_to_number(symbol, member);
1327 tmp = tmp->ident_expression;
1329 mark_member_as_set(struct_type, member_set, member);
1330 member_idx++;
1331 deref = member_expression(symbol, '.', member);
1332 if (tmp->type == EXPR_INITIALIZER) {
1333 type = get_type(deref);
1334 if (type && type->type == SYM_ARRAY)
1335 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1336 else
1337 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1338 } else {
1339 assign = assign_expression(deref, tmp);
1340 fake_cb(assign);
1342 } END_FOR_EACH_PTR(tmp);
1344 set_unset_to_zero(struct_type, symbol);
1347 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1349 fake_member_assigns_helper(symbol_expression(sym),
1350 sym->initializer->expr_list, fake_cb);
1353 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1355 struct expression *offset, *binop, *assign, *tmp;
1356 struct symbol *type;
1357 int idx;
1359 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1360 return;
1362 idx = 0;
1363 FOR_EACH_PTR(expr_list, tmp) {
1364 if (tmp->type == EXPR_INDEX) {
1365 if (tmp->idx_from != tmp->idx_to)
1366 return;
1367 idx = tmp->idx_from;
1368 if (!tmp->idx_expression)
1369 goto next;
1370 tmp = tmp->idx_expression;
1372 offset = value_expr(idx);
1373 binop = array_element_expression(array, offset);
1374 if (tmp->type == EXPR_INITIALIZER) {
1375 type = get_type(binop);
1376 if (type && type->type == SYM_ARRAY)
1377 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1378 else
1379 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1380 } else {
1381 assign = assign_expression(binop, tmp);
1382 fake_cb(assign);
1384 next:
1385 idx++;
1386 } END_FOR_EACH_PTR(tmp);
1389 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1391 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1394 static void fake_assign_expr(struct symbol *sym)
1396 struct expression *assign, *symbol;
1398 symbol = symbol_expression(sym);
1399 assign = assign_expression(symbol, sym->initializer);
1400 __split_expr(assign);
1403 static void call_split_expr(struct expression *expr)
1405 __split_expr(expr);
1408 static void do_initializer_stuff(struct symbol *sym)
1410 if (!sym->initializer)
1411 return;
1413 if (sym->initializer->type == EXPR_INITIALIZER) {
1414 if (get_real_base_type(sym)->type == SYM_ARRAY)
1415 fake_element_assigns(sym, call_split_expr);
1416 else
1417 fake_member_assigns(sym, call_split_expr);
1418 } else {
1419 fake_assign_expr(sym);
1423 static void split_declaration(struct symbol_list *sym_list)
1425 struct symbol *sym;
1427 FOR_EACH_PTR(sym_list, sym) {
1428 __pass_to_client(sym, DECLARATION_HOOK);
1429 do_initializer_stuff(sym);
1430 split_sym(sym);
1431 } END_FOR_EACH_PTR(sym);
1434 static void call_global_assign_hooks(struct expression *assign)
1436 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1439 static void fake_global_assign(struct symbol *sym)
1441 struct expression *assign, *symbol;
1443 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1444 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1445 fake_element_assigns(sym, call_global_assign_hooks);
1446 } else if (sym->initializer) {
1447 symbol = symbol_expression(sym);
1448 assign = assign_expression(symbol, sym->initializer);
1449 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1450 } else {
1451 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1453 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1454 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1455 fake_member_assigns(sym, call_global_assign_hooks);
1456 } else if (sym->initializer) {
1457 symbol = symbol_expression(sym);
1458 assign = assign_expression(symbol, sym->initializer);
1459 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1460 } else {
1461 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1463 } else {
1464 symbol = symbol_expression(sym);
1465 if (sym->initializer)
1466 assign = assign_expression(symbol, sym->initializer);
1467 else
1468 assign = assign_expression(symbol, zero_expr());
1469 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1473 static void start_function_definition(struct symbol *sym)
1475 __in_function_def = 1;
1476 __pass_to_client(sym, FUNC_DEF_HOOK);
1477 __in_function_def = 0;
1478 __pass_to_client(sym, AFTER_DEF_HOOK);
1482 static void split_function(struct symbol *sym)
1484 struct symbol *base_type = get_base_type(sym);
1486 if (!base_type->stmt && !base_type->inline_stmt)
1487 return;
1489 gettimeofday(&fn_start_time, NULL);
1490 cur_func_sym = sym;
1491 if (sym->ident)
1492 cur_func = sym->ident->name;
1493 set_position(sym->pos);
1494 loop_count = 0;
1495 last_goto_statement_handled = 0;
1496 sm_debug("new function: %s\n", cur_func);
1497 __stree_id = 0;
1498 if (option_two_passes) {
1499 __unnullify_path();
1500 loop_num = 0;
1501 final_pass = 0;
1502 start_function_definition(sym);
1503 __split_stmt(base_type->stmt);
1504 __split_stmt(base_type->inline_stmt);
1505 nullify_path();
1507 __unnullify_path();
1508 loop_num = 0;
1509 final_pass = 1;
1510 start_function_definition(sym);
1511 __split_stmt(base_type->stmt);
1512 __split_stmt(base_type->inline_stmt);
1513 __pass_to_client(sym, END_FUNC_HOOK);
1514 if (need_delayed_scope_hooks())
1515 __call_scope_hooks();
1516 __pass_to_client(sym, AFTER_FUNC_HOOK);
1518 clear_all_states();
1519 cur_func_sym = NULL;
1520 cur_func = NULL;
1521 free_data_info_allocs();
1522 free_expression_stack(&switch_expr_stack);
1523 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1524 __bail_on_rest_of_function = 0;
1527 static void parse_inline(struct expression *call)
1529 struct symbol *base_type;
1530 int loop_num_bak = loop_num;
1531 int loop_count_bak = loop_count;
1532 int final_pass_bak = final_pass;
1533 char *cur_func_bak = cur_func;
1534 struct statement_list *big_statement_stack_bak = big_statement_stack;
1535 struct expression_list *big_expression_stack_bak = big_expression_stack;
1536 struct expression_list *big_condition_stack_bak = big_condition_stack;
1537 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1538 struct symbol *cur_func_sym_bak = cur_func_sym;
1540 __pass_to_client(call, INLINE_FN_START);
1541 final_pass = 0; /* don't print anything */
1542 __inline_fn = call;
1544 base_type = get_base_type(call->fn->symbol);
1545 cur_func_sym = call->fn->symbol;
1546 if (call->fn->symbol->ident)
1547 cur_func = call->fn->symbol->ident->name;
1548 else
1549 cur_func = NULL;
1550 set_position(call->fn->symbol->pos);
1552 save_all_states();
1553 big_statement_stack = NULL;
1554 big_expression_stack = NULL;
1555 big_condition_stack = NULL;
1556 switch_expr_stack = NULL;
1558 sm_debug("inline function: %s\n", cur_func);
1559 __unnullify_path();
1560 loop_num = 0;
1561 start_function_definition(call->fn->symbol);
1562 __split_stmt(base_type->stmt);
1563 __split_stmt(base_type->inline_stmt);
1564 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1565 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1567 free_expression_stack(&switch_expr_stack);
1568 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1569 nullify_path();
1570 free_goto_stack();
1572 loop_num = loop_num_bak;
1573 loop_count = loop_count_bak;
1574 final_pass = final_pass_bak;
1575 cur_func_sym = cur_func_sym_bak;
1576 cur_func = cur_func_bak;
1577 big_statement_stack = big_statement_stack_bak;
1578 big_expression_stack = big_expression_stack_bak;
1579 big_condition_stack = big_condition_stack_bak;
1580 switch_expr_stack = switch_expr_stack_bak;
1582 restore_all_states();
1583 set_position(call->pos);
1584 __inline_fn = NULL;
1585 __pass_to_client(call, INLINE_FN_END);
1588 static struct symbol_list *inlines_called;
1589 static void add_inline_function(struct symbol *sym)
1591 static struct symbol_list *already_added;
1592 struct symbol *tmp;
1594 FOR_EACH_PTR(already_added, tmp) {
1595 if (tmp == sym)
1596 return;
1597 } END_FOR_EACH_PTR(tmp);
1599 add_ptr_list(&already_added, sym);
1600 add_ptr_list(&inlines_called, sym);
1603 static void process_inlines(void)
1605 struct symbol *tmp;
1607 FOR_EACH_PTR(inlines_called, tmp) {
1608 split_function(tmp);
1609 } END_FOR_EACH_PTR(tmp);
1610 free_ptr_list(&inlines_called);
1613 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1615 struct symbol *sym;
1617 FOR_EACH_PTR_REVERSE(big_list, sym) {
1618 if (!sym->scope)
1619 continue;
1620 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1621 return sym;
1622 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1623 return sym;
1624 } END_FOR_EACH_PTR_REVERSE(sym);
1626 return NULL;
1629 static void split_inlines_in_scope(struct symbol *sym)
1631 struct symbol *base;
1632 struct symbol_list *scope_list;
1633 int stream;
1635 scope_list = sym->scope->symbols;
1636 stream = sym->pos.stream;
1638 /* find the last static symbol in the file */
1639 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1640 if (sym->pos.stream != stream)
1641 continue;
1642 if (sym->type != SYM_NODE)
1643 continue;
1644 base = get_base_type(sym);
1645 if (!base)
1646 continue;
1647 if (base->type != SYM_FN)
1648 continue;
1649 if (!base->inline_stmt)
1650 continue;
1651 add_inline_function(sym);
1652 } END_FOR_EACH_PTR_REVERSE(sym);
1654 process_inlines();
1657 static void split_inlines(struct symbol_list *sym_list)
1659 struct symbol *sym;
1661 sym = get_last_scoped_symbol(sym_list, 0);
1662 if (sym)
1663 split_inlines_in_scope(sym);
1664 sym = get_last_scoped_symbol(sym_list, 1);
1665 if (sym)
1666 split_inlines_in_scope(sym);
1669 static struct stree *clone_estates_perm(struct stree *orig)
1671 struct stree *ret = NULL;
1672 struct sm_state *tmp;
1674 FOR_EACH_SM(orig, tmp) {
1675 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1676 } END_FOR_EACH_SM(tmp);
1678 return ret;
1681 struct position last_pos;
1682 static void split_functions(struct symbol_list *sym_list)
1684 struct symbol *sym;
1686 __unnullify_path();
1687 FOR_EACH_PTR(sym_list, sym) {
1688 set_position(sym->pos);
1689 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1690 __pass_to_client(sym, BASE_HOOK);
1691 fake_global_assign(sym);
1693 } END_FOR_EACH_PTR(sym);
1694 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1695 nullify_path();
1697 FOR_EACH_PTR(sym_list, sym) {
1698 set_position(sym->pos);
1699 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1700 split_function(sym);
1701 process_inlines();
1703 last_pos = sym->pos;
1704 } END_FOR_EACH_PTR(sym);
1705 split_inlines(sym_list);
1706 __pass_to_client(sym_list, END_FILE_HOOK);
1709 void smatch(int argc, char **argv)
1711 struct string_list *filelist = NULL;
1712 struct symbol_list *sym_list;
1713 struct timeval stop, start;
1715 gettimeofday(&start, NULL);
1717 if (argc < 2) {
1718 printf("Usage: smatch [--debug] <filename.c>\n");
1719 exit(1);
1721 sparse_initialize(argc, argv, &filelist);
1722 set_valid_ptr_max();
1723 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1724 if (option_file_output) {
1725 char buf[256];
1727 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1728 sm_outfd = fopen(buf, "w");
1729 if (!sm_outfd) {
1730 printf("Error: Cannot open %s\n", base_file);
1731 exit(1);
1734 sym_list = sparse_keep_tokens(base_file);
1735 split_functions(sym_list);
1736 } END_FOR_EACH_PTR_NOTAG(base_file);
1738 gettimeofday(&stop, NULL);
1740 set_position(last_pos);
1741 if (option_time)
1742 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);