flow: pull parse_assignment() into its own function
[smatch.git] / smatch_flow.c
blob1aaf804ce4511005a5b26d5e3823ca7d5b4f70bc
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 __in_fake_struct_assign;
30 int __in_fake_var_assign;
31 int __fake_state_cnt;
32 int in_fake_env;
33 int final_pass;
34 int __inline_call;
35 struct expression *__inline_fn;
37 int __smatch_lineno = 0;
39 static char *base_file;
40 static const char *filename;
41 static char *pathname;
42 static char *full_filename;
43 static char *full_base_file;
44 static char *cur_func;
45 int base_file_stream;
46 static unsigned int loop_count;
47 static int last_goto_statement_handled;
48 int __expr_stmt_count;
49 int __in_function_def;
50 int __in_unmatched_hook;
51 static struct expression_list *switch_expr_stack = NULL;
52 static struct expression_list *post_op_stack = NULL;
54 static struct ptr_list *fn_data_list;
55 static struct ptr_list *backup;
57 struct expression_list *big_expression_stack;
58 struct statement_list *big_statement_stack;
59 struct statement *__prev_stmt;
60 struct statement *__cur_stmt;
61 struct statement *__next_stmt;
62 int __in_pre_condition = 0;
63 int __bail_on_rest_of_function = 0;
64 static struct timeval fn_start_time;
65 static struct timeval outer_fn_start_time;
66 char *get_function(void) { return cur_func; }
67 int get_lineno(void) { return __smatch_lineno; }
68 int inside_loop(void) { return !!loop_count; }
69 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
70 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
71 int in_expression_statement(void) { return !!__expr_stmt_count; }
73 static void split_symlist(struct symbol_list *sym_list);
74 static void split_declaration(struct symbol_list *sym_list);
75 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
76 static void split_args(struct expression *expr);
77 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr);
78 static void add_inline_function(struct symbol *sym);
79 static void parse_inline(struct expression *expr);
81 int option_assume_loops = 0;
82 int option_two_passes = 0;
83 struct symbol *cur_func_sym = NULL;
84 struct stree *global_states;
86 const unsigned long valid_ptr_min = 4096;
87 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
88 const sval_t valid_ptr_min_sval = {
89 .type = &ptr_ctype,
90 {.value = 4096},
92 sval_t valid_ptr_max_sval = {
93 .type = &ptr_ctype,
94 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
96 struct range_list *valid_ptr_rl;
98 void alloc_valid_ptr_rl(void)
100 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
101 valid_ptr_max_sval.value = valid_ptr_max;
103 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
104 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
105 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
108 int outside_of_function(void)
110 return cur_func_sym == NULL;
113 const char *get_filename(void)
115 if (option_info && option_full_path)
116 return full_base_file;
117 if (option_info)
118 return base_file;
119 if (option_full_path)
120 return full_filename;
121 return filename;
124 const char *get_base_file(void)
126 if (option_full_path)
127 return full_base_file;
128 return base_file;
131 unsigned long long get_file_id(void)
133 return str_to_llu_hash(get_filename());
136 unsigned long long get_base_file_id(void)
138 return str_to_llu_hash(get_base_file());
141 static void set_position(struct position pos)
143 int len;
144 static int prev_stream = -1;
146 if (in_fake_env)
147 return;
149 if (pos.stream == 0 && pos.line == 0)
150 return;
152 __smatch_lineno = pos.line;
154 if (pos.stream == prev_stream)
155 return;
157 filename = stream_name(pos.stream);
159 free(full_filename);
160 pathname = getcwd(NULL, 0);
161 if (pathname) {
162 len = strlen(pathname) + 1 + strlen(filename) + 1;
163 full_filename = malloc(len);
164 snprintf(full_filename, len, "%s/%s", pathname, filename);
165 } else {
166 full_filename = alloc_string(filename);
168 free(pathname);
171 int is_assigned_call(struct expression *expr)
173 struct expression *parent = expr_get_parent_expr(expr);
175 if (parent &&
176 parent->type == EXPR_ASSIGNMENT &&
177 parent->op == '=' &&
178 strip_expr(parent->right) == expr)
179 return 1;
181 return 0;
184 int is_fake_assigned_call(struct expression *expr)
186 struct expression *parent = expr_get_fake_parent_expr(expr);
188 if (parent &&
189 parent->type == EXPR_ASSIGNMENT &&
190 parent->op == '=' &&
191 strip_expr(parent->right) == expr)
192 return 1;
194 return 0;
197 static bool is_inline_func(struct expression *expr)
199 if (expr->type != EXPR_SYMBOL || !expr->symbol)
200 return false;
201 if (!expr->symbol->definition)
202 return false;
203 if (expr->symbol->definition->ctype.modifiers & MOD_INLINE)
204 return true;
206 return 0;
209 static int is_noreturn_func(struct expression *expr)
211 if (expr->type != EXPR_SYMBOL || !expr->symbol)
212 return 0;
215 * It's almost impossible for Smatch to handle __builtin_constant_p()
216 * the same way that GCC does so Smatch ends up making some functions
217 * as no return functions incorrectly.
220 if (option_project == PROJ_KERNEL && expr->symbol->ident &&
221 strstr(expr->symbol->ident->name, "__compiletime_assert"))
222 return 0;
224 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
225 return 1;
226 return 0;
229 static int save_func_time(void *_rl, int argc, char **argv, char **azColName)
231 unsigned long *rl = _rl;
233 *rl = strtoul(argv[0], NULL, 10);
234 return 0;
237 static int get_func_time(struct symbol *sym)
239 unsigned long time = 0;
241 run_sql(&save_func_time, &time,
242 "select key from return_implies where %s and type = %d;",
243 get_static_filter(sym), FUNC_TIME);
245 return time;
248 static int inline_budget = 20;
250 int inlinable(struct expression *expr)
252 struct symbol *sym;
253 struct statement *last_stmt = NULL;
255 if (__inline_fn) /* don't nest */
256 return 0;
258 if (expr->type != EXPR_SYMBOL || !expr->symbol)
259 return 0;
260 if (is_no_inline_function(expr->symbol->ident->name))
261 return 0;
262 sym = get_base_type(expr->symbol);
263 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
264 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
265 return 0;
266 if (sym->stmt->type != STMT_COMPOUND)
267 return 0;
268 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
270 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
271 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
272 return 0;
273 if (sym->inline_stmt->type != STMT_COMPOUND)
274 return 0;
275 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
278 if (!last_stmt)
279 return 0;
281 /* the magic numbers in this function are pulled out of my bum. */
282 if (last_stmt->pos.line > sym->pos.line + inline_budget)
283 return 0;
285 if (get_func_time(expr->symbol) >= 2)
286 return 0;
288 return 1;
291 void __process_post_op_stack(void)
293 struct expression *expr;
295 FOR_EACH_PTR(post_op_stack, expr) {
296 __pass_to_client(expr, OP_HOOK);
297 } END_FOR_EACH_PTR(expr);
299 __free_ptr_list((struct ptr_list **)&post_op_stack);
302 static int handle_comma_assigns(struct expression *expr)
304 struct expression *right;
305 struct expression *assign;
307 right = strip_expr(expr->right);
308 if (right->type != EXPR_COMMA)
309 return 0;
311 __split_expr(right->left);
312 __process_post_op_stack();
314 assign = assign_expression(expr->left, '=', right->right);
315 __split_expr(assign);
317 return 1;
320 /* This is to handle *p++ = foo; assignments */
321 static int handle_postop_assigns(struct expression *expr)
323 struct expression *left, *fake_left;
324 struct expression *assign;
326 left = strip_expr(expr->left);
327 if (left->type != EXPR_PREOP || left->op != '*')
328 return 0;
329 left = strip_expr(left->unop);
330 if (left->type != EXPR_POSTOP)
331 return 0;
333 fake_left = deref_expression(strip_expr(left->unop));
334 assign = assign_expression(fake_left, '=', expr->right);
336 __split_expr(assign);
337 __split_expr(expr->left);
339 return 1;
342 static int prev_expression_is_getting_address(struct expression *expr)
344 struct expression *parent;
346 do {
347 parent = expr_get_parent_expr(expr);
349 if (!parent)
350 return 0;
351 if (parent->type == EXPR_PREOP && parent->op == '&')
352 return 1;
353 if (parent->type == EXPR_PREOP && parent->op == '(')
354 goto next;
355 if (parent->type == EXPR_DEREF && parent->op == '.')
356 goto next;
357 /* Handle &foo->array[offset] */
358 if (parent->type == EXPR_BINOP && parent->op == '+') {
359 parent = expr_get_parent_expr(parent);
360 if (!parent)
361 return 0;
362 if (parent->type == EXPR_PREOP && parent->op == '*')
363 goto next;
366 return 0;
367 next:
368 expr = parent;
369 } while (1);
372 int __in_builtin_overflow_func;
373 static void handle_builtin_overflow_func(struct expression *expr)
375 struct expression *a, *b, *res, *assign;
376 int op;
378 if (sym_name_is("__builtin_add_overflow", expr->fn))
379 op = '+';
380 else if (sym_name_is("__builtin_sub_overflow", expr->fn))
381 op = '-';
382 else if (sym_name_is("__builtin_mul_overflow", expr->fn))
383 op = '*';
384 else
385 return;
387 a = get_argument_from_call_expr(expr->args, 0);
388 b = get_argument_from_call_expr(expr->args, 1);
389 res = get_argument_from_call_expr(expr->args, 2);
391 assign = assign_expression(deref_expression(res), '=', binop_expression(a, op, b));
393 __in_builtin_overflow_func++;
394 __split_expr(assign);
395 __in_builtin_overflow_func--;
398 static int handle__builtin_choose_expr(struct expression *expr)
400 struct expression *const_expr, *expr1, *expr2;
401 sval_t sval;
403 if (!sym_name_is("__builtin_choose_expr", expr->fn))
404 return 0;
406 const_expr = get_argument_from_call_expr(expr->args, 0);
407 expr1 = get_argument_from_call_expr(expr->args, 1);
408 expr2 = get_argument_from_call_expr(expr->args, 2);
410 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
411 return 0;
412 if (sval.value)
413 __split_expr(expr1);
414 else
415 __split_expr(expr2);
416 return 1;
419 static int handle__builtin_choose_expr_assigns(struct expression *expr)
421 struct expression *const_expr, *right, *expr1, *expr2, *fake;
422 sval_t sval;
424 right = strip_parens(expr->right);
425 if (right->type != EXPR_CALL)
426 return 0;
427 if (!sym_name_is("__builtin_choose_expr", right->fn))
428 return 0;
430 const_expr = get_argument_from_call_expr(right->args, 0);
431 expr1 = get_argument_from_call_expr(right->args, 1);
432 expr2 = get_argument_from_call_expr(right->args, 2);
434 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
435 return 0;
437 fake = assign_expression(expr->left, '=', sval.value ? expr1 : expr2);
438 __split_expr(fake);
439 return 1;
442 int is_condition_call(struct expression *expr)
444 struct expression *tmp;
446 FOR_EACH_PTR_REVERSE(big_condition_stack, tmp) {
447 if (expr == tmp || expr_get_parent_expr(expr) == tmp)
448 return 1;
449 if (tmp->pos.line < expr->pos.line)
450 return 0;
451 } END_FOR_EACH_PTR_REVERSE(tmp);
453 return 0;
456 static struct expression *expr_get_parent_no_parens(struct expression *expr)
458 do {
459 expr = expr_get_parent_expr(expr);
460 } while (expr &&
461 expr->type == EXPR_PREOP &&
462 expr->op == '(');
464 return expr;
467 static bool gen_fake_function_assign(struct expression *expr)
469 static struct expression *parsed;
470 struct expression *assign, *parent;
471 struct symbol *type;
472 char buf[64];
474 /* The rule is that every non-void function call has to be part of an
475 * assignment. TODO: Should we create a fake non-casted assignment
476 * for casted assignments? Also faked assigns for += assignments?
478 type = get_type(expr);
479 if (!type || type == &void_ctype)
480 return false;
482 parent = expr_get_parent_no_parens(expr);
483 if (parent && parent->type == EXPR_ASSIGNMENT)
484 return false;
486 parent = expr_get_fake_parent_expr(expr);
487 if (parent) {
488 struct expression *left = parent->left;
490 if (parent == parsed)
491 return false;
492 if (!left || left->type != EXPR_SYMBOL)
493 return false;
494 if (strncmp(left->symbol_name->name, "__fake_assign_", 14) != 0)
495 return false;
496 parsed = parent;
497 __split_expr(parent);
498 return true;
501 // TODO: faked_assign skipping conditions is a hack
502 if (is_condition_call(expr))
503 return false;
505 snprintf(buf, sizeof(buf), "__fake_assign_%p", expr);
506 assign = create_fake_assign(buf, get_type(expr), expr);
508 parsed = assign;
509 __split_expr(assign);
510 return true;
513 static void split_call(struct expression *expr)
515 if (gen_fake_function_assign(expr))
516 return;
518 expr_set_parent_expr(expr->fn, expr);
520 if (sym_name_is("__builtin_constant_p", expr->fn))
521 return;
522 if (handle__builtin_choose_expr(expr))
523 return;
524 __split_expr(expr->fn);
525 split_args(expr);
526 if (is_inline_func(expr->fn))
527 add_inline_function(expr->fn->symbol->definition);
528 if (inlinable(expr->fn))
529 __inline_call = 1;
530 __process_post_op_stack();
531 __pass_to_client(expr, FUNCTION_CALL_HOOK_BEFORE);
532 __pass_to_client(expr, FUNCTION_CALL_HOOK);
533 __inline_call = 0;
534 if (inlinable(expr->fn))
535 parse_inline(expr);
536 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
537 if (is_noreturn_func(expr->fn))
538 nullify_path();
539 if (!expr_get_parent_expr(expr))
540 __discard_fake_states(expr);
541 handle_builtin_overflow_func(expr);
544 void parse_assignment(struct expression *expr)
546 struct expression *right;
548 expr_set_parent_expr(expr->left, expr);
549 expr_set_parent_expr(expr->right, expr);
551 right = strip_expr(expr->right);
552 if (!right)
553 return;
555 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
557 /* foo = !bar() */
558 if (__handle_condition_assigns(expr))
559 goto after_assign;
560 /* foo = (x < 5 ? foo : 5); */
561 if (__handle_select_assigns(expr))
562 goto after_assign;
563 /* foo = ({frob(); frob(); frob(); 1;}) */
564 if (__handle_expr_statement_assigns(expr))
565 return; // FIXME: got after
566 /* foo = (3, 4); */
567 if (handle_comma_assigns(expr))
568 goto after_assign;
569 if (handle__builtin_choose_expr_assigns(expr))
570 goto after_assign;
571 if (handle_postop_assigns(expr))
572 return; /* no need to goto after_assign */
574 __split_expr(expr->right);
575 if (outside_of_function())
576 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
577 else
578 __pass_to_client(expr, ASSIGNMENT_HOOK);
581 // FIXME: the ordering of this is tricky
582 __fake_struct_member_assignments(expr);
584 /* Re-examine ->right for inlines. See the commit message */
585 right = strip_expr(expr->right);
586 if (expr->op == '=' && right->type == EXPR_CALL)
587 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
589 after_assign:
590 if (get_macro_name(right->pos) &&
591 get_macro_name(expr->pos) != get_macro_name(right->pos))
592 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
594 __pass_to_client(expr, ASSIGNMENT_HOOK_AFTER);
595 __split_expr(expr->left);
598 void __split_expr(struct expression *expr)
600 if (!expr)
601 return;
603 // if (local_debug)
604 // sm_msg("Debug expr_type %d %s expr = '%s'", expr->type, show_special(expr->op), expr_to_str(expr));
606 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
607 return;
608 if (__in_fake_assign >= 4) /* don't allow too much nesting */
609 return;
611 push_expression(&big_expression_stack, expr);
612 set_position(expr->pos);
613 __pass_to_client(expr, EXPR_HOOK);
615 switch (expr->type) {
616 case EXPR_PREOP:
617 expr_set_parent_expr(expr->unop, expr);
619 if (expr->op == '*' &&
620 !prev_expression_is_getting_address(expr))
621 __pass_to_client(expr, DEREF_HOOK);
622 __split_expr(expr->unop);
623 __pass_to_client(expr, OP_HOOK);
624 break;
625 case EXPR_POSTOP:
626 expr_set_parent_expr(expr->unop, expr);
628 __split_expr(expr->unop);
629 push_expression(&post_op_stack, expr);
630 break;
631 case EXPR_STATEMENT:
632 __expr_stmt_count++;
633 if (expr->statement && !expr->statement) {
634 stmt_set_parent_stmt(expr->statement,
635 last_ptr_list((struct ptr_list *)big_statement_stack));
637 __split_stmt(expr->statement);
638 __expr_stmt_count--;
639 break;
640 case EXPR_LOGICAL:
641 case EXPR_COMPARE:
642 expr_set_parent_expr(expr->left, expr);
643 expr_set_parent_expr(expr->right, expr);
645 __pass_to_client(expr, LOGIC_HOOK);
646 __handle_logic(expr);
647 break;
648 case EXPR_BINOP:
649 expr_set_parent_expr(expr->left, expr);
650 expr_set_parent_expr(expr->right, expr);
652 __pass_to_client(expr, BINOP_HOOK);
653 __split_expr(expr->left);
654 __split_expr(expr->right);
655 break;
656 case EXPR_COMMA:
657 expr_set_parent_expr(expr->left, expr);
658 expr_set_parent_expr(expr->right, expr);
660 __split_expr(expr->left);
661 __process_post_op_stack();
662 __split_expr(expr->right);
663 break;
664 case EXPR_ASSIGNMENT:
665 parse_assignment(expr);
666 break;
667 case EXPR_DEREF:
668 expr_set_parent_expr(expr->deref, expr);
670 __pass_to_client(expr, DEREF_HOOK);
671 __split_expr(expr->deref);
672 break;
673 case EXPR_SLICE:
674 expr_set_parent_expr(expr->base, expr);
676 __split_expr(expr->base);
677 break;
678 case EXPR_CAST:
679 case EXPR_FORCE_CAST:
680 expr_set_parent_expr(expr->cast_expression, expr);
682 __pass_to_client(expr, CAST_HOOK);
683 __split_expr(expr->cast_expression);
684 break;
685 case EXPR_SIZEOF:
686 if (expr->cast_expression)
687 __pass_to_client(strip_parens(expr->cast_expression),
688 SIZEOF_HOOK);
689 break;
690 case EXPR_OFFSETOF:
691 case EXPR_ALIGNOF:
692 break;
693 case EXPR_CONDITIONAL:
694 case EXPR_SELECT:
695 expr_set_parent_expr(expr->conditional, expr);
696 expr_set_parent_expr(expr->cond_true, expr);
697 expr_set_parent_expr(expr->cond_false, expr);
699 if (known_condition_true(expr->conditional)) {
700 __split_expr(expr->cond_true);
701 break;
703 if (known_condition_false(expr->conditional)) {
704 __split_expr(expr->cond_false);
705 break;
707 __pass_to_client(expr, SELECT_HOOK);
708 __split_whole_condition(expr->conditional);
709 __split_expr(expr->cond_true);
710 __push_true_states();
711 __use_false_states();
712 __split_expr(expr->cond_false);
713 __merge_true_states();
714 break;
715 case EXPR_CALL:
716 split_call(expr);
717 break;
718 case EXPR_INITIALIZER:
719 split_expr_list(expr->expr_list, expr);
720 break;
721 case EXPR_IDENTIFIER:
722 expr_set_parent_expr(expr->ident_expression, expr);
723 __split_expr(expr->ident_expression);
724 break;
725 case EXPR_INDEX:
726 expr_set_parent_expr(expr->idx_expression, expr);
727 __split_expr(expr->idx_expression);
728 break;
729 case EXPR_POS:
730 expr_set_parent_expr(expr->init_expr, expr);
731 __split_expr(expr->init_expr);
732 break;
733 case EXPR_SYMBOL:
734 __pass_to_client(expr, SYM_HOOK);
735 break;
736 case EXPR_STRING:
737 __pass_to_client(expr, STRING_HOOK);
738 break;
739 case EXPR_GENERIC: {
740 struct expression *tmp;
742 tmp = strip_Generic(expr);
743 if (tmp != expr)
744 __split_expr(tmp);
745 break;
747 default:
748 break;
750 __pass_to_client(expr, EXPR_HOOK_AFTER);
751 pop_expression(&big_expression_stack);
754 static int is_forever_loop(struct statement *stmt)
756 struct expression *expr;
757 sval_t sval;
759 expr = strip_expr(stmt->iterator_pre_condition);
760 if (!expr)
761 expr = stmt->iterator_post_condition;
762 if (!expr) {
763 /* this is a for(;;) loop... */
764 return 1;
767 if (get_value(expr, &sval) && sval.value != 0)
768 return 1;
770 return 0;
773 static int loop_num;
774 static char *get_loop_name(int num)
776 char buf[256];
778 snprintf(buf, 255, "-loop%d", num);
779 buf[255] = '\0';
780 return alloc_sname(buf);
784 * Pre Loops are while and for loops.
786 static void handle_pre_loop(struct statement *stmt)
788 int once_through; /* we go through the loop at least once */
789 struct sm_state *extra_sm = NULL;
790 int unchanged = 0;
791 char *loop_name;
792 struct stree *stree = NULL;
793 struct sm_state *sm = NULL;
795 loop_name = get_loop_name(loop_num);
796 loop_num++;
798 if (stmt->iterator_pre_statement) {
799 __split_stmt(stmt->iterator_pre_statement);
800 __prev_stmt = stmt->iterator_pre_statement;
803 once_through = implied_condition_true(stmt->iterator_pre_condition);
805 loop_count++;
806 __push_continues();
807 __push_breaks();
809 __merge_gotos(loop_name, NULL);
811 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
812 __in_pre_condition++;
813 __pass_to_client(stmt, PRELOOP_HOOK);
814 __split_whole_condition(stmt->iterator_pre_condition);
815 __in_pre_condition--;
816 FOR_EACH_SM(stree, sm) {
817 set_state(sm->owner, sm->name, sm->sym, sm->state);
818 } END_FOR_EACH_SM(sm);
819 free_stree(&stree);
820 if (extra_sm)
821 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
823 if (option_assume_loops)
824 once_through = 1;
826 __split_stmt(stmt->iterator_statement);
827 if (is_forever_loop(stmt)) {
828 __merge_continues();
829 __save_gotos(loop_name, NULL);
831 __push_fake_cur_stree();
832 __split_stmt(stmt->iterator_post_statement);
833 stree = __pop_fake_cur_stree();
835 __discard_false_states();
836 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
837 __use_breaks();
839 if (!__path_is_null())
840 __merge_stree_into_cur(stree);
841 free_stree(&stree);
842 } else {
843 __merge_continues();
844 unchanged = __iterator_unchanged(extra_sm);
845 __split_stmt(stmt->iterator_post_statement);
846 __prev_stmt = stmt->iterator_post_statement;
847 __cur_stmt = stmt;
849 __save_gotos(loop_name, NULL);
850 __in_pre_condition++;
851 __split_whole_condition(stmt->iterator_pre_condition);
852 __in_pre_condition--;
853 nullify_path();
854 __merge_false_states();
855 if (once_through)
856 __discard_false_states();
857 else
858 __merge_false_states();
860 if (extra_sm && unchanged)
861 __extra_pre_loop_hook_after(extra_sm,
862 stmt->iterator_post_statement,
863 stmt->iterator_pre_condition);
864 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
865 __merge_breaks();
867 loop_count--;
871 * Post loops are do {} while();
873 static void handle_post_loop(struct statement *stmt)
875 char *loop_name;
877 loop_name = get_loop_name(loop_num);
878 loop_num++;
879 loop_count++;
881 __pass_to_client(stmt, POSTLOOP_HOOK);
883 __push_continues();
884 __push_breaks();
885 __merge_gotos(loop_name, NULL);
886 __split_stmt(stmt->iterator_statement);
887 __merge_continues();
888 if (!expr_is_zero(stmt->iterator_post_condition))
889 __save_gotos(loop_name, NULL);
891 if (is_forever_loop(stmt)) {
892 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
893 __use_breaks();
894 } else {
895 __split_whole_condition(stmt->iterator_post_condition);
896 __use_false_states();
897 __pass_to_client(stmt, AFTER_LOOP_NO_BREAKS);
898 __merge_breaks();
900 loop_count--;
903 static int empty_statement(struct statement *stmt)
905 if (!stmt)
906 return 0;
907 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
908 return 1;
909 return 0;
912 static int last_stmt_on_same_line(void)
914 struct statement *stmt;
915 int i = 0;
917 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
918 if (!i++)
919 continue;
920 if (stmt->pos.line == get_lineno())
921 return 1;
922 return 0;
923 } END_FOR_EACH_PTR_REVERSE(stmt);
924 return 0;
927 static void split_asm_ops(struct asm_operand_list *ops)
929 struct asm_operand *op;
931 FOR_EACH_PTR(ops, op) {
932 __split_expr(op->expr);
933 } END_FOR_EACH_PTR(op);
936 static int is_case_val(struct statement *stmt, sval_t sval)
938 sval_t case_sval;
940 if (stmt->type != STMT_CASE)
941 return 0;
942 if (!stmt->case_expression) {
943 __set_default();
944 return 1;
946 if (!get_value(stmt->case_expression, &case_sval))
947 return 0;
948 if (case_sval.value == sval.value)
949 return 1;
950 return 0;
953 static struct range_list *get_case_rl(struct expression *switch_expr,
954 struct expression *case_expr,
955 struct expression *case_to)
957 sval_t start, end;
958 struct range_list *rl = NULL;
959 struct symbol *switch_type;
961 switch_type = get_type(switch_expr);
962 if (get_value(case_to, &end) && get_value(case_expr, &start)) {
963 start = sval_cast(switch_type, start);
964 end = sval_cast(switch_type, end);
965 add_range(&rl, start, end);
966 } else if (get_value(case_expr, &start)) {
967 start = sval_cast(switch_type, start);
968 add_range(&rl, start, start);
971 return rl;
974 static void split_known_switch(struct statement *stmt, sval_t sval)
976 struct statement *tmp;
977 struct range_list *rl;
979 __split_expr(stmt->switch_expression);
980 sval = sval_cast(get_type(stmt->switch_expression), sval);
982 push_expression(&switch_expr_stack, stmt->switch_expression);
983 __save_switch_states(top_expression(switch_expr_stack));
984 nullify_path();
985 __push_default();
986 __push_breaks();
988 stmt = stmt->switch_statement;
990 __push_scope_hooks();
991 FOR_EACH_PTR(stmt->stmts, tmp) {
992 __smatch_lineno = tmp->pos.line;
993 // FIXME: what if default comes before the known case statement?
994 if (is_case_val(tmp, sval)) {
995 rl = alloc_rl(sval, sval);
996 __merge_switches(top_expression(switch_expr_stack), rl);
997 __pass_case_to_client(top_expression(switch_expr_stack), rl);
998 stmt_set_parent_stmt(tmp->case_statement, tmp);
999 __split_stmt(tmp->case_statement);
1000 goto next;
1002 if (__path_is_null())
1003 continue;
1004 __split_stmt(tmp);
1005 next:
1006 if (__path_is_null()) {
1007 __set_default();
1008 goto out;
1010 } END_FOR_EACH_PTR(tmp);
1011 out:
1012 __call_scope_hooks();
1013 if (!__pop_default())
1014 __merge_switches(top_expression(switch_expr_stack), NULL);
1015 __discard_switches();
1016 __merge_breaks();
1017 pop_expression(&switch_expr_stack);
1020 static void split_case(struct statement *stmt)
1022 struct range_list *rl = NULL;
1024 expr_set_parent_stmt(stmt->case_expression, stmt);
1025 expr_set_parent_stmt(stmt->case_to, stmt);
1027 rl = get_case_rl(top_expression(switch_expr_stack),
1028 stmt->case_expression, stmt->case_to);
1029 while (stmt->case_statement->type == STMT_CASE) {
1030 struct range_list *tmp;
1032 tmp = get_case_rl(top_expression(switch_expr_stack),
1033 stmt->case_statement->case_expression,
1034 stmt->case_statement->case_to);
1035 if (!tmp)
1036 goto next;
1037 rl = rl_union(rl, tmp);
1038 if (!stmt->case_expression)
1039 __set_default();
1040 next:
1041 stmt = stmt->case_statement;
1044 __merge_switches(top_expression(switch_expr_stack), rl);
1046 if (!stmt->case_expression)
1047 __set_default();
1049 stmt_set_parent_stmt(stmt->case_statement, stmt);
1050 __split_stmt(stmt->case_statement);
1053 int time_parsing_function(void)
1055 return ms_since(&fn_start_time) / 1000;
1058 bool taking_too_long(void)
1060 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
1061 return 1;
1062 return 0;
1065 struct statement *get_last_stmt(void)
1067 struct symbol *fn;
1068 struct statement *stmt;
1070 fn = get_base_type(cur_func_sym);
1071 if (!fn)
1072 return NULL;
1073 stmt = fn->stmt;
1074 if (!stmt)
1075 stmt = fn->inline_stmt;
1076 if (!stmt || stmt->type != STMT_COMPOUND)
1077 return NULL;
1078 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1079 if (stmt && stmt->type == STMT_LABEL)
1080 stmt = stmt->label_statement;
1081 return stmt;
1084 int is_last_stmt(struct statement *cur_stmt)
1086 struct statement *last;
1088 last = get_last_stmt();
1089 if (last && last == cur_stmt)
1090 return 1;
1091 return 0;
1094 static void handle_backward_goto(struct statement *goto_stmt)
1096 const char *goto_name, *label_name;
1097 struct statement *func_stmt;
1098 struct symbol *base_type = get_base_type(cur_func_sym);
1099 struct statement *tmp;
1100 int found = 0;
1102 if (!option_info)
1103 return;
1104 if (last_goto_statement_handled)
1105 return;
1106 last_goto_statement_handled = 1;
1108 if (!goto_stmt->goto_label ||
1109 goto_stmt->goto_label->type != SYM_LABEL ||
1110 !goto_stmt->goto_label->ident)
1111 return;
1112 goto_name = goto_stmt->goto_label->ident->name;
1114 func_stmt = base_type->stmt;
1115 if (!func_stmt)
1116 func_stmt = base_type->inline_stmt;
1117 if (!func_stmt)
1118 return;
1119 if (func_stmt->type != STMT_COMPOUND)
1120 return;
1122 FOR_EACH_PTR(func_stmt->stmts, tmp) {
1123 if (!found) {
1124 if (tmp->type != STMT_LABEL)
1125 continue;
1126 if (!tmp->label_identifier ||
1127 tmp->label_identifier->type != SYM_LABEL ||
1128 !tmp->label_identifier->ident)
1129 continue;
1130 label_name = tmp->label_identifier->ident->name;
1131 if (strcmp(goto_name, label_name) != 0)
1132 continue;
1133 found = 1;
1135 __split_stmt(tmp);
1136 } END_FOR_EACH_PTR(tmp);
1139 static void fake_a_return(void)
1141 struct expression *ret = NULL;
1143 nullify_path();
1144 __unnullify_path();
1146 if (cur_func_return_type() != &void_ctype)
1147 ret = unknown_value_expression(NULL);
1149 __pass_to_client(ret, RETURN_HOOK);
1150 nullify_path();
1153 static void split_ret_value(struct expression *expr)
1155 struct symbol *type;
1157 if (!expr)
1158 return;
1160 type = get_real_base_type(cur_func_sym);
1161 type = get_real_base_type(type);
1162 expr = fake_a_variable_assign(type, NULL, expr, -1);
1164 __in_fake_var_assign++;
1165 __split_expr(expr);
1166 __in_fake_var_assign--;
1169 static void fake_an_empty_default(struct position pos)
1171 static struct statement none = {};
1173 none.pos = pos;
1174 none.type = STMT_NONE;
1175 __merge_switches(top_expression(switch_expr_stack), NULL);
1176 __split_stmt(&none);
1179 static void split_compound(struct statement *stmt)
1181 struct statement *prev = NULL;
1182 struct statement *cur = NULL;
1183 struct statement *next;
1185 __push_scope_hooks();
1187 FOR_EACH_PTR(stmt->stmts, next) {
1188 /* just set them all ahead of time */
1189 stmt_set_parent_stmt(next, stmt);
1191 if (cur) {
1192 __prev_stmt = prev;
1193 __next_stmt = next;
1194 __cur_stmt = cur;
1195 __split_stmt(cur);
1197 prev = cur;
1198 cur = next;
1199 } END_FOR_EACH_PTR(next);
1200 if (cur) {
1201 __prev_stmt = prev;
1202 __cur_stmt = cur;
1203 __next_stmt = NULL;
1204 __split_stmt(cur);
1208 * For function scope, then delay calling the scope hooks until the
1209 * end of function hooks can run. I'm not positive this is the right
1210 * thing...
1212 if (!is_last_stmt(cur))
1213 __call_scope_hooks();
1217 * This is a hack, work around for detecting empty functions.
1219 static int need_delayed_scope_hooks(void)
1221 struct symbol *fn = get_base_type(cur_func_sym);
1222 struct statement *stmt;
1224 if (!fn)
1225 return 0;
1226 stmt = fn->stmt;
1227 if (!stmt)
1228 stmt = fn->inline_stmt;
1229 if (stmt && stmt->type == STMT_COMPOUND)
1230 return 1;
1231 return 0;
1234 void __split_label_stmt(struct statement *stmt)
1236 if (stmt->label_identifier &&
1237 stmt->label_identifier->type == SYM_LABEL &&
1238 stmt->label_identifier->ident) {
1239 loop_count |= 0x0800000;
1240 __merge_gotos(stmt->label_identifier->ident->name, stmt->label_identifier);
1244 static void find_asm_gotos(struct statement *stmt)
1246 struct symbol *sym;
1248 FOR_EACH_PTR(stmt->asm_labels, sym) {
1249 __save_gotos(sym->ident->name, sym);
1250 } END_FOR_EACH_PTR(sym);
1253 void __split_stmt(struct statement *stmt)
1255 static int indent_cnt;
1256 sval_t sval;
1257 struct timeval start, stop;
1258 bool skip_after = false;
1260 gettimeofday(&start, NULL);
1262 if (!stmt)
1263 goto out;
1265 if (!__in_fake_assign)
1266 __silence_warnings_for_stmt = false;
1268 if (__bail_on_rest_of_function || is_skipped_function())
1269 return;
1271 if (out_of_memory() || taking_too_long()) {
1272 gettimeofday(&start, NULL);
1274 __bail_on_rest_of_function = 1;
1275 final_pass = 1;
1276 sm_perror("Function too hairy. Giving up. %lu seconds",
1277 start.tv_sec - fn_start_time.tv_sec);
1278 fake_a_return();
1279 final_pass = 0; /* turn off sm_msg() from here */
1280 return;
1283 indent_cnt++;
1285 add_ptr_list(&big_statement_stack, stmt);
1286 free_expression_stack(&big_expression_stack);
1287 set_position(stmt->pos);
1288 __pass_to_client(stmt, STMT_HOOK);
1290 switch (stmt->type) {
1291 case STMT_DECLARATION:
1292 split_declaration(stmt->declaration);
1293 break;
1294 case STMT_RETURN:
1295 expr_set_parent_stmt(stmt->ret_value, stmt);
1297 split_ret_value(stmt->ret_value);
1298 __pass_to_client(stmt->ret_value, RETURN_HOOK);
1299 __process_post_op_stack();
1300 nullify_path();
1301 break;
1302 case STMT_EXPRESSION:
1303 expr_set_parent_stmt(stmt->expression, stmt);
1304 expr_set_parent_stmt(stmt->context, stmt);
1306 __split_expr(stmt->expression);
1307 break;
1308 case STMT_COMPOUND:
1309 split_compound(stmt);
1310 break;
1311 case STMT_IF:
1312 stmt_set_parent_stmt(stmt->if_true, stmt);
1313 stmt_set_parent_stmt(stmt->if_false, stmt);
1314 expr_set_parent_stmt(stmt->if_conditional, stmt);
1316 if (known_condition_true(stmt->if_conditional)) {
1317 __split_stmt(stmt->if_true);
1318 break;
1320 if (known_condition_false(stmt->if_conditional)) {
1321 __split_stmt(stmt->if_false);
1322 break;
1324 __split_whole_condition(stmt->if_conditional);
1325 __split_stmt(stmt->if_true);
1326 if (empty_statement(stmt->if_true) &&
1327 last_stmt_on_same_line() &&
1328 !get_macro_name(stmt->if_true->pos))
1329 sm_warning("if();");
1330 __push_true_states();
1331 __use_false_states();
1332 __split_stmt(stmt->if_false);
1333 __merge_true_states();
1334 break;
1335 case STMT_ITERATOR:
1336 stmt_set_parent_stmt(stmt->iterator_pre_statement, stmt);
1337 stmt_set_parent_stmt(stmt->iterator_statement, stmt);
1338 stmt_set_parent_stmt(stmt->iterator_post_statement, stmt);
1339 expr_set_parent_stmt(stmt->iterator_pre_condition, stmt);
1340 expr_set_parent_stmt(stmt->iterator_post_condition, stmt);
1342 if (stmt->iterator_pre_condition)
1343 handle_pre_loop(stmt);
1344 else if (stmt->iterator_post_condition)
1345 handle_post_loop(stmt);
1346 else {
1347 // these are for(;;) type loops.
1348 handle_pre_loop(stmt);
1350 break;
1351 case STMT_SWITCH:
1352 stmt_set_parent_stmt(stmt->switch_statement, stmt);
1353 expr_set_parent_stmt(stmt->switch_expression, stmt);
1355 if (get_value(stmt->switch_expression, &sval)) {
1356 split_known_switch(stmt, sval);
1357 break;
1359 __split_expr(stmt->switch_expression);
1360 push_expression(&switch_expr_stack, stmt->switch_expression);
1361 __save_switch_states(top_expression(switch_expr_stack));
1362 nullify_path();
1363 __push_default();
1364 __push_breaks();
1365 __split_stmt(stmt->switch_statement);
1366 if (!__pop_default() && have_remaining_cases())
1367 fake_an_empty_default(stmt->pos);
1368 __discard_switches();
1369 __merge_breaks();
1370 pop_expression(&switch_expr_stack);
1371 break;
1372 case STMT_CASE:
1373 split_case(stmt);
1374 break;
1375 case STMT_LABEL:
1376 __split_label_stmt(stmt);
1377 __pass_to_client(stmt, STMT_HOOK_AFTER);
1378 skip_after = true;
1379 __split_stmt(stmt->label_statement);
1380 break;
1381 case STMT_GOTO:
1382 expr_set_parent_stmt(stmt->goto_expression, stmt);
1384 __split_expr(stmt->goto_expression);
1385 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
1386 if (!strcmp(stmt->goto_label->ident->name, "break")) {
1387 __process_breaks();
1388 } else if (!strcmp(stmt->goto_label->ident->name,
1389 "continue")) {
1390 __process_continues();
1392 } else if (stmt->goto_label &&
1393 stmt->goto_label->type == SYM_LABEL &&
1394 stmt->goto_label->ident) {
1395 __save_gotos(stmt->goto_label->ident->name, stmt->goto_label);
1397 nullify_path();
1398 if (is_last_stmt(stmt))
1399 handle_backward_goto(stmt);
1400 break;
1401 case STMT_NONE:
1402 break;
1403 case STMT_ASM:
1404 expr_set_parent_stmt(stmt->asm_string, stmt);
1406 find_asm_gotos(stmt);
1407 __pass_to_client(stmt, ASM_HOOK);
1408 __split_expr(stmt->asm_string);
1409 split_asm_ops(stmt->asm_outputs);
1410 split_asm_ops(stmt->asm_inputs);
1411 split_expr_list(stmt->asm_clobbers, NULL);
1412 break;
1413 case STMT_CONTEXT:
1414 break;
1415 case STMT_RANGE:
1416 __split_expr(stmt->range_expression);
1417 __split_expr(stmt->range_low);
1418 __split_expr(stmt->range_high);
1419 break;
1421 if (!skip_after)
1422 __pass_to_client(stmt, STMT_HOOK_AFTER);
1423 if (--indent_cnt == 1)
1424 __discard_fake_states(NULL);
1426 out:
1427 __process_post_op_stack();
1429 gettimeofday(&stop, NULL);
1430 if (option_time_stmt && stmt)
1431 sm_msg("stmt_time%s: %ld",
1432 stmt->type == STMT_COMPOUND ? "_block" : "",
1433 stop.tv_sec - start.tv_sec);
1436 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1438 struct expression *expr;
1440 FOR_EACH_PTR(expr_list, expr) {
1441 expr_set_parent_expr(expr, parent);
1442 __split_expr(expr);
1443 __process_post_op_stack();
1444 } END_FOR_EACH_PTR(expr);
1447 static bool cast_arg(struct symbol *type, struct expression *arg)
1449 struct symbol *orig;
1451 if (!type)
1452 return false;
1454 arg = strip_parens(arg);
1455 if (arg != strip_expr(arg))
1456 return true;
1458 orig = get_type(arg);
1459 if (!orig)
1460 return true;
1461 if (types_equiv(orig, type))
1462 return false;
1464 if (orig->type == SYM_ARRAY && type->type == SYM_PTR)
1465 return true;
1468 * I would have expected that we could just do use (orig == type) but I
1469 * guess for pointers we need to get the basetype to do that comparison.
1473 if (orig->type != SYM_PTR ||
1474 type->type != SYM_PTR) {
1475 if (type_fits(type, orig))
1476 return false;
1477 return true;
1479 orig = get_real_base_type(orig);
1480 type = get_real_base_type(type);
1481 if (orig == type)
1482 return false;
1484 return true;
1487 static struct expression *fake_a_variable_assign(struct symbol *type, struct expression *call, struct expression *expr, int nr)
1489 char buf[64];
1490 bool cast;
1492 if (!expr || !cur_func_sym)
1493 return NULL;
1495 if (expr->type == EXPR_ASSIGNMENT)
1496 return expr;
1498 /* for va_args then we don't know the type */
1499 if (!type)
1500 type = get_type(expr);
1502 cast = cast_arg(type, expr);
1504 * Using expr_to_sym() here is a hack. We want to say that we don't
1505 * need to assign frob(foo) or frob(foo->bar) if the types are right.
1506 * It turns out faking these assignments is way more expensive than I
1507 * would have imagined. I'm not sure why exactly.
1510 if (!cast) {
1512 * if the code is "return *p;" where "p" is a user pointer then
1513 * we want to create a fake assignment so that it sets the state
1514 * in check_kernel_user_data.c.
1517 if (expr->type != EXPR_PREOP &&
1518 expr->op != '*' && expr->op != '&' &&
1519 expr_to_sym(expr))
1520 return expr;
1523 if (nr == -1)
1524 snprintf(buf, sizeof(buf), "__fake_return_%p", expr);
1525 else
1526 snprintf(buf, sizeof(buf), "__fake_param_%p_%d", call, nr);
1528 return create_fake_assign(buf, type, expr);
1531 static void split_args(struct expression *expr)
1533 struct expression *arg, *tmp;
1534 struct symbol *type;
1535 int i;
1537 i = -1;
1538 FOR_EACH_PTR(expr->args, arg) {
1539 i++;
1540 expr_set_parent_expr(arg, expr);
1541 type = get_arg_type(expr->fn, i);
1542 tmp = fake_a_variable_assign(type, expr, arg, i);
1543 if (tmp != arg)
1544 __in_fake_var_assign++;
1545 __split_expr(tmp);
1546 if (tmp != arg)
1547 __in_fake_var_assign--;
1548 __process_post_op_stack();
1549 } END_FOR_EACH_PTR(arg);
1552 static void split_sym(struct symbol *sym)
1554 if (!sym)
1555 return;
1556 if (!(sym->namespace & NS_SYMBOL))
1557 return;
1559 __split_stmt(sym->stmt);
1560 __split_expr(sym->array_size);
1561 split_symlist(sym->arguments);
1562 split_symlist(sym->symbol_list);
1563 __split_stmt(sym->inline_stmt);
1564 split_symlist(sym->inline_symbol_list);
1567 static void split_symlist(struct symbol_list *sym_list)
1569 struct symbol *sym;
1571 FOR_EACH_PTR(sym_list, sym) {
1572 split_sym(sym);
1573 } END_FOR_EACH_PTR(sym);
1576 typedef void (fake_cb)(struct expression *expr);
1578 static int member_to_number(struct expression *expr, struct ident *member)
1580 struct symbol *type, *tmp;
1581 char *name;
1582 int i;
1584 if (!member)
1585 return -1;
1586 name = member->name;
1588 type = get_type(expr);
1589 if (!type || type->type != SYM_STRUCT)
1590 return -1;
1592 i = -1;
1593 FOR_EACH_PTR(type->symbol_list, tmp) {
1594 i++;
1595 if (!tmp->ident)
1596 continue;
1597 if (strcmp(name, tmp->ident->name) == 0)
1598 return i;
1599 } END_FOR_EACH_PTR(tmp);
1600 return -1;
1603 static struct ident *number_to_member(struct expression *expr, int num)
1605 struct symbol *type, *member;
1606 int i = 0;
1608 type = get_type(expr);
1609 if (!type || type->type != SYM_STRUCT)
1610 return NULL;
1612 FOR_EACH_PTR(type->symbol_list, member) {
1613 if (i == num)
1614 return member->ident;
1615 i++;
1616 } END_FOR_EACH_PTR(member);
1617 return NULL;
1620 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1622 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1624 struct expression *edge_member, *assign;
1625 struct symbol *base = get_real_base_type(member);
1626 struct symbol *tmp;
1628 if (member->ident)
1629 expr = member_expression(expr, '.', member->ident);
1631 FOR_EACH_PTR(base->symbol_list, tmp) {
1632 struct symbol *type;
1634 type = get_real_base_type(tmp);
1635 if (!type)
1636 continue;
1638 edge_member = member_expression(expr, '.', tmp->ident);
1639 if (get_extra_state(edge_member))
1640 continue;
1642 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1643 set_inner_struct_members(expr, tmp);
1644 continue;
1647 if (!tmp->ident)
1648 continue;
1650 assign = assign_expression(edge_member, '=', zero_expr());
1651 __split_expr(assign);
1652 } END_FOR_EACH_PTR(tmp);
1657 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1659 struct symbol *tmp;
1660 struct expression *member = NULL;
1661 struct expression *assign;
1663 FOR_EACH_PTR(type->symbol_list, tmp) {
1664 type = get_real_base_type(tmp);
1665 if (!type)
1666 continue;
1668 if (tmp->ident) {
1669 member = member_expression(expr, '.', tmp->ident);
1670 if (get_extra_state(member))
1671 continue;
1674 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1675 set_inner_struct_members(expr, tmp);
1676 continue;
1678 if (type->type == SYM_ARRAY)
1679 continue;
1680 if (!tmp->ident)
1681 continue;
1683 assign = assign_expression(member, '=', zero_expr());
1684 __split_expr(assign);
1685 } END_FOR_EACH_PTR(tmp);
1688 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1690 struct expression *deref, *assign, *tmp, *right;
1691 struct symbol *struct_type, *type;
1692 struct ident *member;
1693 int member_idx;
1695 struct_type = get_type(symbol);
1696 if (!struct_type ||
1697 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1698 return;
1701 * We're parsing an initializer that could look something like this:
1702 * struct foo foo = {
1703 * 42,
1704 * .whatever.xxx = 11,
1705 * .zzz = 12,
1706 * };
1708 * So what we have here is a list with 42, .whatever, and .zzz. We need
1709 * to break it up into left and right sides of the assignments.
1712 member_idx = 0;
1713 FOR_EACH_PTR(members, tmp) {
1714 deref = NULL;
1715 if (tmp->type == EXPR_IDENTIFIER) {
1716 member_idx = member_to_number(symbol, tmp->expr_ident);
1717 while (tmp->type == EXPR_IDENTIFIER) {
1718 member = tmp->expr_ident;
1719 tmp = tmp->ident_expression;
1720 if (deref)
1721 deref = member_expression(deref, '.', member);
1722 else
1723 deref = member_expression(symbol, '.', member);
1725 } else {
1726 member = number_to_member(symbol, member_idx);
1727 deref = member_expression(symbol, '.', member);
1729 right = tmp;
1730 member_idx++;
1731 if (right->type == EXPR_INITIALIZER) {
1732 type = get_type(deref);
1733 if (type && type->type == SYM_ARRAY)
1734 fake_element_assigns_helper(deref, right->expr_list, fake_cb);
1735 else
1736 fake_member_assigns_helper(deref, right->expr_list, fake_cb);
1737 } else {
1738 assign = assign_expression(deref, '=', right);
1739 fake_cb(assign);
1741 } END_FOR_EACH_PTR(tmp);
1743 set_unset_to_zero(struct_type, symbol);
1746 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1748 fake_member_assigns_helper(symbol_expression(sym),
1749 sym->initializer->expr_list, fake_cb);
1752 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1754 struct expression *offset, *binop, *assign, *tmp;
1755 struct symbol *type;
1756 int idx, max;
1758 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1759 return;
1761 max = 0;
1762 idx = 0;
1763 FOR_EACH_PTR(expr_list, tmp) {
1764 if (tmp->type == EXPR_INDEX) {
1765 if (tmp->idx_from != tmp->idx_to)
1766 return;
1767 idx = tmp->idx_from;
1768 if (idx > max)
1769 max = idx;
1770 if (!tmp->idx_expression)
1771 goto next;
1772 tmp = tmp->idx_expression;
1774 offset = value_expr(idx);
1775 binop = array_element_expression(array, offset);
1776 if (tmp->type == EXPR_INITIALIZER) {
1777 type = get_type(binop);
1778 if (type && type->type == SYM_ARRAY)
1779 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1780 else
1781 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1782 } else {
1783 assign = assign_expression(binop, '=', tmp);
1784 fake_cb(assign);
1786 next:
1787 idx++;
1788 if (idx > max)
1789 max = idx;
1790 } END_FOR_EACH_PTR(tmp);
1792 __call_array_initialized_hooks(array, max);
1795 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1797 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1800 static void fake_assign_expr(struct symbol *sym)
1802 struct expression *assign, *symbol;
1804 symbol = symbol_expression(sym);
1805 assign = assign_expression(symbol, '=', sym->initializer);
1806 __split_expr(assign);
1809 static void do_initializer_stuff(struct symbol *sym)
1811 if (!sym->initializer)
1812 return;
1814 if (sym->initializer->type == EXPR_INITIALIZER) {
1815 if (get_real_base_type(sym)->type == SYM_ARRAY)
1816 fake_element_assigns(sym, __split_expr);
1817 else
1818 fake_member_assigns(sym, __split_expr);
1819 } else {
1820 fake_assign_expr(sym);
1824 static void split_declaration(struct symbol_list *sym_list)
1826 struct symbol *sym;
1828 FOR_EACH_PTR(sym_list, sym) {
1829 __pass_to_client(sym, DECLARATION_HOOK);
1830 do_initializer_stuff(sym);
1831 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
1832 split_sym(sym);
1833 } END_FOR_EACH_PTR(sym);
1836 static void call_global_assign_hooks(struct expression *assign)
1838 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1841 static void fake_global_assign(struct symbol *sym)
1843 struct expression *assign, *symbol;
1845 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1846 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1847 fake_element_assigns(sym, call_global_assign_hooks);
1848 } else if (sym->initializer) {
1849 symbol = symbol_expression(sym);
1850 assign = assign_expression(symbol, '=', sym->initializer);
1851 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1852 } else {
1853 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1855 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1856 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1857 fake_member_assigns(sym, call_global_assign_hooks);
1858 } else if (sym->initializer) {
1859 symbol = symbol_expression(sym);
1860 assign = assign_expression(symbol, '=', sym->initializer);
1861 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1862 } else {
1863 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1865 } else {
1866 symbol = symbol_expression(sym);
1867 if (sym->initializer) {
1868 assign = assign_expression(symbol, '=', sym->initializer);
1869 __split_expr(assign);
1870 } else {
1871 assign = assign_expression(symbol, '=', zero_expr());
1873 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1877 static void start_function_definition(struct symbol *sym)
1879 __in_function_def = 1;
1880 __pass_to_client(sym, FUNC_DEF_HOOK);
1881 __in_function_def = 0;
1882 __pass_to_client(sym, AFTER_DEF_HOOK);
1886 void add_function_data(unsigned long *fn_data)
1888 __add_ptr_list(&fn_data_list, fn_data);
1891 static void clear_function_data(void)
1893 unsigned long *tmp;
1895 FOR_EACH_PTR(fn_data_list, tmp) {
1896 *tmp = 0;
1897 } END_FOR_EACH_PTR(tmp);
1900 static void record_func_time(void)
1902 struct timeval stop;
1903 int func_time;
1904 char buf[32];
1906 gettimeofday(&stop, NULL);
1907 func_time = stop.tv_sec - fn_start_time.tv_sec;
1908 snprintf(buf, sizeof(buf), "%d", func_time);
1909 sql_insert_return_implies(FUNC_TIME, 0, "", buf);
1910 if (option_time && func_time > 2) {
1911 final_pass++;
1912 sm_msg("func_time: %d", func_time);
1913 final_pass--;
1917 static void split_function(struct symbol *sym)
1919 struct symbol *base_type = get_base_type(sym);
1921 if (!base_type->stmt && !base_type->inline_stmt)
1922 return;
1924 gettimeofday(&outer_fn_start_time, NULL);
1925 gettimeofday(&fn_start_time, NULL);
1926 cur_func_sym = sym;
1927 if (sym->ident)
1928 cur_func = sym->ident->name;
1929 set_position(sym->pos);
1930 clear_function_data();
1931 loop_count = 0;
1932 last_goto_statement_handled = 0;
1933 sm_debug("new function: %s\n", cur_func);
1934 __stree_id = 0;
1935 if (option_two_passes) {
1936 __unnullify_path();
1937 loop_num = 0;
1938 final_pass = 0;
1939 start_function_definition(sym);
1940 __split_stmt(base_type->stmt);
1941 __split_stmt(base_type->inline_stmt);
1942 nullify_path();
1944 __unnullify_path();
1945 loop_num = 0;
1946 final_pass = 1;
1947 start_function_definition(sym);
1948 __split_stmt(base_type->stmt);
1949 __split_stmt(base_type->inline_stmt);
1950 if (!__path_is_null() &&
1951 cur_func_return_type() == &void_ctype &&
1952 !__bail_on_rest_of_function) {
1953 __pass_to_client(NULL, RETURN_HOOK);
1954 nullify_path();
1956 __pass_to_client(sym, END_FUNC_HOOK);
1957 if (need_delayed_scope_hooks())
1958 __call_scope_hooks();
1959 __pass_to_client(sym, AFTER_FUNC_HOOK);
1960 sym->parsed = true;
1962 clear_all_states();
1964 record_func_time();
1966 cur_func_sym = NULL;
1967 cur_func = NULL;
1968 free_data_info_allocs();
1969 free_expression_stack(&switch_expr_stack);
1970 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1971 __bail_on_rest_of_function = 0;
1974 static void save_flow_state(void)
1976 unsigned long *tmp;
1978 __add_ptr_list(&backup, INT_PTR(loop_num << 2));
1979 __add_ptr_list(&backup, INT_PTR(loop_count << 2));
1980 __add_ptr_list(&backup, INT_PTR(final_pass << 2));
1982 __add_ptr_list(&backup, big_statement_stack);
1983 __add_ptr_list(&backup, big_expression_stack);
1984 __add_ptr_list(&backup, big_condition_stack);
1985 __add_ptr_list(&backup, switch_expr_stack);
1987 __add_ptr_list(&backup, cur_func_sym);
1989 __add_ptr_list(&backup, __prev_stmt);
1990 __add_ptr_list(&backup, __cur_stmt);
1991 __add_ptr_list(&backup, __next_stmt);
1993 FOR_EACH_PTR(fn_data_list, tmp) {
1994 __add_ptr_list(&backup, (void *)*tmp);
1995 } END_FOR_EACH_PTR(tmp);
1998 static void *pop_backup(void)
2000 void *ret;
2002 ret = last_ptr_list(backup);
2003 delete_ptr_list_last(&backup);
2004 return ret;
2007 static void restore_flow_state(void)
2009 unsigned long *tmp;
2011 FOR_EACH_PTR_REVERSE(fn_data_list, tmp) {
2012 *tmp = (unsigned long)pop_backup();
2013 } END_FOR_EACH_PTR_REVERSE(tmp);
2015 __next_stmt = pop_backup();
2016 __cur_stmt = pop_backup();
2017 __prev_stmt = pop_backup();
2019 cur_func_sym = pop_backup();
2020 switch_expr_stack = pop_backup();
2021 big_condition_stack = pop_backup();
2022 big_expression_stack = pop_backup();
2023 big_statement_stack = pop_backup();
2024 final_pass = PTR_INT(pop_backup()) >> 2;
2025 loop_count = PTR_INT(pop_backup()) >> 2;
2026 loop_num = PTR_INT(pop_backup()) >> 2;
2029 static void parse_inline(struct expression *call)
2031 struct symbol *base_type;
2032 char *cur_func_bak = cur_func; /* not aligned correctly for backup */
2033 struct timeval time_backup = fn_start_time;
2034 struct expression *orig_inline = __inline_fn;
2035 int orig_budget;
2037 if (out_of_memory() || taking_too_long())
2038 return;
2040 save_flow_state();
2042 __pass_to_client(call, INLINE_FN_START);
2043 final_pass = 0; /* don't print anything */
2044 __inline_fn = call;
2045 orig_budget = inline_budget;
2046 inline_budget = inline_budget - 5;
2048 base_type = get_base_type(call->fn->symbol);
2049 cur_func_sym = call->fn->symbol;
2050 if (call->fn->symbol->ident)
2051 cur_func = call->fn->symbol->ident->name;
2052 else
2053 cur_func = NULL;
2054 set_position(call->fn->symbol->pos);
2056 save_all_states();
2057 big_statement_stack = NULL;
2058 big_expression_stack = NULL;
2059 big_condition_stack = NULL;
2060 switch_expr_stack = NULL;
2062 sm_debug("inline function: %s\n", cur_func);
2063 __unnullify_path();
2064 clear_function_data();
2065 loop_num = 0;
2066 loop_count = 0;
2067 start_function_definition(call->fn->symbol);
2068 __split_stmt(base_type->stmt);
2069 __split_stmt(base_type->inline_stmt);
2070 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
2071 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
2072 call->fn->symbol->parsed = true;
2074 free_expression_stack(&switch_expr_stack);
2075 __free_ptr_list((struct ptr_list **)&big_statement_stack);
2076 nullify_path();
2077 free_goto_stack();
2079 restore_flow_state();
2080 fn_start_time = time_backup;
2081 cur_func = cur_func_bak;
2083 restore_all_states();
2084 set_position(call->pos);
2085 __inline_fn = orig_inline;
2086 inline_budget = orig_budget;
2087 __pass_to_client(call, INLINE_FN_END);
2090 static struct symbol_list *inlines_called;
2091 static void add_inline_function(struct symbol *sym)
2093 static struct symbol_list *already_added;
2094 struct symbol *tmp;
2096 FOR_EACH_PTR(already_added, tmp) {
2097 if (tmp == sym)
2098 return;
2099 } END_FOR_EACH_PTR(tmp);
2101 add_ptr_list(&already_added, sym);
2102 add_ptr_list(&inlines_called, sym);
2105 static void process_inlines(void)
2107 struct symbol *tmp;
2109 FOR_EACH_PTR(inlines_called, tmp) {
2110 split_function(tmp);
2111 } END_FOR_EACH_PTR(tmp);
2112 free_ptr_list(&inlines_called);
2115 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
2117 struct symbol *sym;
2119 FOR_EACH_PTR_REVERSE(big_list, sym) {
2120 if (!sym->scope)
2121 continue;
2122 if (use_static && sym->ctype.modifiers & MOD_STATIC)
2123 return sym;
2124 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
2125 return sym;
2126 } END_FOR_EACH_PTR_REVERSE(sym);
2128 return NULL;
2131 static bool interesting_function(struct symbol *sym)
2133 static int prev_stream = -1;
2134 static bool prev_answer;
2135 const char *filename;
2136 int len;
2138 if (!(sym->ctype.modifiers & MOD_INLINE))
2139 return true;
2141 if (sym->pos.stream == prev_stream)
2142 return prev_answer;
2144 prev_stream = sym->pos.stream;
2145 prev_answer = false;
2147 filename = stream_name(sym->pos.stream);
2148 len = strlen(filename);
2149 if (len > 0 && filename[len - 1] == 'c')
2150 prev_answer = true;
2151 return prev_answer;
2154 static void split_inlines_in_scope(struct symbol *sym)
2156 struct symbol *base;
2157 struct symbol_list *scope_list;
2158 int stream;
2160 scope_list = sym->scope->symbols;
2161 stream = sym->pos.stream;
2163 /* find the last static symbol in the file */
2164 FOR_EACH_PTR_REVERSE(scope_list, sym) {
2165 if (sym->pos.stream != stream)
2166 continue;
2167 if (sym->type != SYM_NODE)
2168 continue;
2169 base = get_base_type(sym);
2170 if (!base)
2171 continue;
2172 if (base->type != SYM_FN)
2173 continue;
2174 if (!base->inline_stmt)
2175 continue;
2176 if (!interesting_function(sym))
2177 continue;
2178 add_inline_function(sym);
2179 } END_FOR_EACH_PTR_REVERSE(sym);
2181 process_inlines();
2184 static void split_inlines(struct symbol_list *sym_list)
2186 struct symbol *sym;
2188 sym = get_last_scoped_symbol(sym_list, 0);
2189 if (sym)
2190 split_inlines_in_scope(sym);
2191 sym = get_last_scoped_symbol(sym_list, 1);
2192 if (sym)
2193 split_inlines_in_scope(sym);
2196 static struct stree *clone_estates_perm(struct stree *orig)
2198 struct stree *ret = NULL;
2199 struct sm_state *tmp;
2201 FOR_EACH_SM(orig, tmp) {
2202 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
2203 } END_FOR_EACH_SM(tmp);
2205 return ret;
2208 struct position last_pos;
2209 static void split_c_file_functions(struct symbol_list *sym_list)
2211 struct symbol *sym;
2213 __unnullify_path();
2214 FOR_EACH_PTR(sym_list, sym) {
2215 set_position(sym->pos);
2216 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
2217 __pass_to_client(sym, BASE_HOOK);
2218 fake_global_assign(sym);
2219 __pass_to_client(sym, DECLARATION_HOOK_AFTER);
2221 } END_FOR_EACH_PTR(sym);
2222 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
2223 nullify_path();
2225 FOR_EACH_PTR(sym_list, sym) {
2226 set_position(sym->pos);
2227 last_pos = sym->pos;
2228 if (!interesting_function(sym))
2229 continue;
2230 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
2231 split_function(sym);
2232 process_inlines();
2234 last_pos = sym->pos;
2235 } END_FOR_EACH_PTR(sym);
2236 split_inlines(sym_list);
2237 __pass_to_client(sym_list, END_FILE_HOOK);
2240 static int final_before_fake;
2241 void init_fake_env(void)
2243 if (!in_fake_env)
2244 final_before_fake = final_pass;
2245 in_fake_env++;
2246 __push_fake_cur_stree();
2247 final_pass = 0;
2250 void end_fake_env(void)
2252 __free_fake_cur_stree();
2253 in_fake_env--;
2254 if (!in_fake_env)
2255 final_pass = final_before_fake;
2258 static void open_output_files(char *base_file)
2260 char buf[256];
2262 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
2263 sm_outfd = fopen(buf, "w");
2264 if (!sm_outfd)
2265 sm_fatal("Cannot open %s", buf);
2267 if (!option_info)
2268 return;
2270 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
2271 sql_outfd = fopen(buf, "w");
2272 if (!sql_outfd)
2273 sm_fatal("Error: Cannot open %s", buf);
2275 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
2276 caller_info_fd = fopen(buf, "w");
2277 if (!caller_info_fd)
2278 sm_fatal("Error: Cannot open %s", buf);
2281 void smatch(struct string_list *filelist)
2283 struct symbol_list *sym_list;
2284 struct timeval stop, start;
2285 char *path;
2286 int len;
2288 gettimeofday(&start, NULL);
2290 FOR_EACH_PTR_NOTAG(filelist, base_file) {
2291 path = getcwd(NULL, 0);
2292 free(full_base_file);
2293 if (path) {
2294 len = strlen(path) + 1 + strlen(base_file) + 1;
2295 full_base_file = malloc(len);
2296 snprintf(full_base_file, len, "%s/%s", path, base_file);
2297 } else {
2298 full_base_file = alloc_string(base_file);
2300 if (option_file_output)
2301 open_output_files(base_file);
2302 base_file_stream = input_stream_nr;
2303 sym_list = sparse_keep_tokens(base_file);
2304 split_c_file_functions(sym_list);
2305 } END_FOR_EACH_PTR_NOTAG(base_file);
2307 gettimeofday(&stop, NULL);
2309 set_position(last_pos);
2310 final_pass = 1;
2311 if (option_time)
2312 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
2313 if (option_mem)
2314 sm_msg("mem: %luKb", get_max_memory());