hooks: add a FUNCTION_CALL_HOOK_AFTER hook
[smatch.git] / smatch_flow.c
blobd136c8885099681ae90ef1d8dfbfd1241ed1cf4d
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 int __in_pre_condition = 0;
50 int __bail_on_rest_of_function = 0;
51 static struct timeval fn_start_time;
52 char *get_function(void) { return cur_func; }
53 int get_lineno(void) { return __smatch_lineno; }
54 int inside_loop(void) { return !!loop_count; }
55 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
56 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
57 int in_expression_statement(void) { return !!__expr_stmt_count; }
59 static void split_symlist(struct symbol_list *sym_list);
60 static void split_declaration(struct symbol_list *sym_list);
61 static void split_expr_list(struct expression_list *expr_list);
62 static void add_inline_function(struct symbol *sym);
63 static void parse_inline(struct expression *expr);
65 int option_assume_loops = 0;
66 int option_known_conditions = 0;
67 int option_two_passes = 0;
68 struct symbol *cur_func_sym = NULL;
69 struct stree *global_states;
71 long long valid_ptr_min = 4096;
72 long long valid_ptr_max = 7777777777;
73 sval_t valid_ptr_min_sval = {
74 .type = &ptr_ctype,
75 {.value = 4096},
77 sval_t valid_ptr_max_sval = {
78 .type = &ptr_ctype,
79 {.value = LONG_MAX - 100000},
82 static void set_valid_ptr_max(void)
84 if (type_bits(&ptr_ctype) == 32)
85 valid_ptr_max = 7777777777;
86 else if (type_bits(&ptr_ctype) == 64)
87 valid_ptr_max = 7777777777777777777LL;
89 valid_ptr_max_sval.value = valid_ptr_max;
92 int outside_of_function(void)
94 return cur_func_sym == NULL;
97 const char *get_filename(void)
99 if (option_info)
100 return base_file;
101 if (option_full_path)
102 return full_filename;
103 return filename;
106 const char *get_base_file(void)
108 return base_file;
111 static void set_position(struct position pos)
113 int len;
114 static int prev_stream = -1;
116 if (pos.stream == 0 && pos.line == 0)
117 return;
119 __smatch_lineno = pos.line;
121 if (pos.stream == prev_stream)
122 return;
124 filename = stream_name(pos.stream);
126 free(full_filename);
127 pathname = getcwd(NULL, 0);
128 if (pathname) {
129 len = strlen(pathname) + 1 + strlen(filename) + 1;
130 full_filename = malloc(len);
131 snprintf(full_filename, len, "%s/%s", pathname, filename);
132 } else {
133 full_filename = alloc_string(filename);
135 free(pathname);
138 static int is_inline_func(struct expression *expr)
140 if (expr->type != EXPR_SYMBOL || !expr->symbol)
141 return 0;
142 if (expr->symbol->ctype.modifiers & MOD_INLINE)
143 return 1;
144 return 0;
147 static int is_noreturn_func(struct expression *expr)
149 if (expr->type != EXPR_SYMBOL || !expr->symbol)
150 return 0;
151 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
152 return 1;
153 return 0;
156 int inlinable(struct expression *expr)
158 struct symbol *sym;
160 if (__inline_fn) /* don't nest */
161 return 0;
163 if (expr->type != EXPR_SYMBOL || !expr->symbol)
164 return 0;
165 if (is_no_inline_function(expr->symbol->ident->name))
166 return 0;
167 sym = get_base_type(expr->symbol);
168 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
169 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
170 return 1;
171 return 0;
173 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
174 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
175 return 1;
176 return 0;
178 return 0;
181 void __process_post_op_stack(void)
183 struct expression *expr;
185 FOR_EACH_PTR(post_op_stack, expr) {
186 __pass_to_client(expr, OP_HOOK);
187 } END_FOR_EACH_PTR(expr);
189 __free_ptr_list((struct ptr_list **)&post_op_stack);
192 void __split_expr(struct expression *expr)
194 if (!expr)
195 return;
197 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
199 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
200 return;
201 if (__in_fake_assign >= 4) /* don't allow too much nesting */
202 return;
204 push_expression(&big_expression_stack, expr);
205 set_position(expr->pos);
206 __pass_to_client(expr, EXPR_HOOK);
208 switch (expr->type) {
209 case EXPR_PREOP:
210 if (expr->op == '*')
211 __pass_to_client(expr, DEREF_HOOK);
212 __split_expr(expr->unop);
213 __pass_to_client(expr, OP_HOOK);
214 break;
215 case EXPR_POSTOP:
216 __split_expr(expr->unop);
217 push_expression(&post_op_stack, expr);
218 break;
219 case EXPR_STATEMENT:
220 __expr_stmt_count++;
221 __split_stmt(expr->statement);
222 __expr_stmt_count--;
223 break;
224 case EXPR_LOGICAL:
225 case EXPR_COMPARE:
226 __pass_to_client(expr, LOGIC_HOOK);
227 __handle_logic(expr);
228 break;
229 case EXPR_BINOP:
230 __pass_to_client(expr, BINOP_HOOK);
231 case EXPR_COMMA:
232 __split_expr(expr->left);
233 __process_post_op_stack();
234 __split_expr(expr->right);
235 break;
236 case EXPR_ASSIGNMENT: {
237 struct expression *tmp;
239 if (!expr->right)
240 break;
242 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
244 /* foo = !bar() */
245 if (__handle_condition_assigns(expr))
246 break;
247 /* foo = (x < 5 ? foo : 5); */
248 if (__handle_select_assigns(expr))
249 break;
250 /* foo = ({frob(); frob(); frob(); 1;}) */
251 if (__handle_expr_statement_assigns(expr))
252 break;
254 __split_expr(expr->right);
255 if (outside_of_function())
256 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
257 else
258 __pass_to_client(expr, ASSIGNMENT_HOOK);
260 __fake_struct_member_assignments(expr);
262 tmp = strip_expr(expr->right);
263 if (tmp->type == EXPR_CALL)
264 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
265 if (get_macro_name(tmp->pos) &&
266 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
267 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
268 __split_expr(expr->left);
269 break;
271 case EXPR_DEREF:
272 __pass_to_client(expr, DEREF_HOOK);
273 __split_expr(expr->deref);
274 break;
275 case EXPR_SLICE:
276 __split_expr(expr->base);
277 break;
278 case EXPR_CAST:
279 case EXPR_FORCE_CAST:
280 __pass_to_client(expr, CAST_HOOK);
281 __split_expr(expr->cast_expression);
282 break;
283 case EXPR_SIZEOF:
284 if (expr->cast_expression)
285 __pass_to_client(strip_parens(expr->cast_expression),
286 SIZEOF_HOOK);
287 break;
288 case EXPR_OFFSETOF:
289 case EXPR_ALIGNOF:
290 evaluate_expression(expr);
291 break;
292 case EXPR_CONDITIONAL:
293 case EXPR_SELECT:
294 if (known_condition_true(expr->conditional)) {
295 __split_expr(expr->cond_true);
296 break;
298 if (known_condition_false(expr->conditional)) {
299 __split_expr(expr->cond_false);
300 break;
302 __pass_to_client(expr, SELECT_HOOK);
303 __split_whole_condition(expr->conditional);
304 __split_expr(expr->cond_true);
305 __push_true_states();
306 __use_false_states();
307 __split_expr(expr->cond_false);
308 __merge_true_states();
309 break;
310 case EXPR_CALL:
311 if (sym_name_is("__builtin_constant_p", expr->fn))
312 break;
313 split_expr_list(expr->args);
314 __split_expr(expr->fn);
315 if (is_inline_func(expr->fn))
316 add_inline_function(expr->fn->symbol);
317 if (inlinable(expr->fn))
318 __inline_call = 1;
319 __process_post_op_stack();
320 __pass_to_client(expr, FUNCTION_CALL_HOOK);
321 __inline_call = 0;
322 if (inlinable(expr->fn)) {
323 parse_inline(expr);
325 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
326 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER);
327 if (is_noreturn_func(expr->fn))
328 nullify_path();
329 break;
330 case EXPR_INITIALIZER:
331 split_expr_list(expr->expr_list);
332 break;
333 case EXPR_IDENTIFIER:
334 __split_expr(expr->ident_expression);
335 break;
336 case EXPR_INDEX:
337 __split_expr(expr->idx_expression);
338 break;
339 case EXPR_POS:
340 __split_expr(expr->init_expr);
341 break;
342 case EXPR_SYMBOL:
343 __pass_to_client(expr, SYM_HOOK);
344 break;
345 case EXPR_STRING:
346 __pass_to_client(expr, STRING_HOOK);
347 break;
348 default:
349 break;
351 pop_expression(&big_expression_stack);
354 static int is_forever_loop(struct statement *stmt)
356 struct expression *expr;
358 expr = strip_expr(stmt->iterator_pre_condition);
359 if (!expr)
360 expr = stmt->iterator_post_condition;
361 if (!expr) {
362 /* this is a for(;;) loop... */
363 return 1;
366 if (expr->type == EXPR_VALUE && expr->value == 1)
367 return 1;
369 return 0;
372 static int loop_num;
373 static char *get_loop_name(int num)
375 char buf[256];
377 snprintf(buf, 255, "-loop%d", num);
378 buf[255] = '\0';
379 return alloc_sname(buf);
383 * Pre Loops are while and for loops.
385 static void handle_pre_loop(struct statement *stmt)
387 int once_through; /* we go through the loop at least once */
388 struct sm_state *extra_sm = NULL;
389 int unchanged = 0;
390 char *loop_name;
391 struct stree *stree = NULL;
392 struct sm_state *sm = NULL;
394 loop_name = get_loop_name(loop_num);
395 loop_num++;
397 __split_stmt(stmt->iterator_pre_statement);
399 once_through = implied_condition_true(stmt->iterator_pre_condition);
401 loop_count++;
402 __push_continues();
403 __push_breaks();
405 __merge_gotos(loop_name);
407 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
408 __in_pre_condition++;
409 __pass_to_client(stmt, PRELOOP_HOOK);
410 __split_whole_condition(stmt->iterator_pre_condition);
411 __in_pre_condition--;
412 FOR_EACH_SM(stree, sm) {
413 set_state(sm->owner, sm->name, sm->sym, sm->state);
414 } END_FOR_EACH_SM(sm);
415 free_stree(&stree);
416 if (extra_sm)
417 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
419 if (option_assume_loops)
420 once_through = 1;
422 __split_stmt(stmt->iterator_statement);
423 __warn_on_silly_pre_loops();
424 if (is_forever_loop(stmt)) {
425 __merge_continues();
426 __save_gotos(loop_name);
428 __push_fake_cur_stree();
429 __split_stmt(stmt->iterator_post_statement);
430 stree = __pop_fake_cur_stree();
432 __discard_false_states();
433 __use_breaks();
435 if (!__path_is_null())
436 __merge_stree_into_cur(stree);
437 free_stree(&stree);
438 } else {
439 __merge_continues();
440 unchanged = __iterator_unchanged(extra_sm);
441 __split_stmt(stmt->iterator_post_statement);
442 __save_gotos(loop_name);
443 __in_pre_condition++;
444 __split_whole_condition(stmt->iterator_pre_condition);
445 __in_pre_condition--;
446 nullify_path();
447 __merge_false_states();
448 if (once_through)
449 __discard_false_states();
450 else
451 __merge_false_states();
453 if (extra_sm && unchanged)
454 __extra_pre_loop_hook_after(extra_sm,
455 stmt->iterator_post_statement,
456 stmt->iterator_pre_condition);
457 __merge_breaks();
459 loop_count--;
463 * Post loops are do {} while();
465 static void handle_post_loop(struct statement *stmt)
467 char *loop_name;
469 loop_name = get_loop_name(loop_num);
470 loop_num++;
471 loop_count++;
473 __push_continues();
474 __push_breaks();
475 __merge_gotos(loop_name);
476 __split_stmt(stmt->iterator_statement);
477 __merge_continues();
478 if (!is_zero(stmt->iterator_post_condition))
479 __save_gotos(loop_name);
481 if (is_forever_loop(stmt)) {
482 __use_breaks();
483 } else {
484 __split_whole_condition(stmt->iterator_post_condition);
485 __use_false_states();
486 __merge_breaks();
488 loop_count--;
491 static int empty_statement(struct statement *stmt)
493 if (!stmt)
494 return 0;
495 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
496 return 1;
497 return 0;
500 static int last_stmt_on_same_line(void)
502 struct statement *stmt;
503 int i = 0;
505 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
506 if (!i++)
507 continue;
508 if (stmt->pos.line == get_lineno())
509 return 1;
510 return 0;
511 } END_FOR_EACH_PTR_REVERSE(stmt);
512 return 0;
515 static void split_asm_constraints(struct expression_list *expr_list)
517 struct expression *expr;
518 int state = 0;
520 FOR_EACH_PTR(expr_list, expr) {
521 switch (state) {
522 case 0: /* identifier */
523 case 1: /* constraint */
524 state++;
525 continue;
526 case 2: /* expression */
527 state = 0;
528 __split_expr(expr);
529 continue;
531 } END_FOR_EACH_PTR(expr);
534 static int is_case_val(struct statement *stmt, sval_t sval)
536 sval_t case_sval;
538 if (stmt->type != STMT_CASE)
539 return 0;
540 if (!stmt->case_expression) {
541 __set_default();
542 return 1;
544 if (!get_value(stmt->case_expression, &case_sval))
545 return 0;
546 if (case_sval.value == sval.value)
547 return 1;
548 return 0;
551 static void split_known_switch(struct statement *stmt, sval_t sval)
553 struct statement *tmp;
555 __split_expr(stmt->switch_expression);
557 push_expression(&switch_expr_stack, stmt->switch_expression);
558 __save_switch_states(top_expression(switch_expr_stack));
559 nullify_path();
560 __push_default();
561 __push_breaks();
563 stmt = stmt->switch_statement;
565 __push_scope_hooks();
566 FOR_EACH_PTR(stmt->stmts, tmp) {
567 __smatch_lineno = tmp->pos.line;
568 if (is_case_val(tmp, sval)) {
569 __merge_switches(top_expression(switch_expr_stack),
570 stmt->case_expression);
571 __pass_case_to_client(top_expression(switch_expr_stack),
572 stmt->case_expression);
574 if (__path_is_null())
575 continue;
576 __split_stmt(tmp);
577 if (__path_is_null()) {
578 __set_default();
579 goto out;
581 } END_FOR_EACH_PTR(tmp);
582 out:
583 __call_scope_hooks();
584 if (!__pop_default())
585 __merge_switches(top_expression(switch_expr_stack),
586 NULL);
587 __discard_switches();
588 __merge_breaks();
589 pop_expression(&switch_expr_stack);
592 static int taking_too_long(void)
594 int ms;
596 ms = ms_since(&fn_start_time);
597 if (ms > 1000 * 60 * 5) /* five minutes */
598 return 1;
599 return 0;
602 static int is_last_stmt(struct statement *cur_stmt)
604 struct symbol *fn = get_base_type(cur_func_sym);
605 struct statement *stmt;
607 if (!fn)
608 return 0;
609 stmt = fn->stmt;
610 if (!stmt)
611 stmt = fn->inline_stmt;
612 if (!stmt || stmt->type != STMT_COMPOUND)
613 return 0;
614 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
615 if (stmt && stmt->type == STMT_LABEL)
616 stmt = stmt->label_statement;
617 if (stmt == cur_stmt)
618 return 1;
619 return 0;
622 static void handle_backward_goto(struct statement *goto_stmt)
624 const char *goto_name, *label_name;
625 struct statement *func_stmt;
626 struct symbol *base_type = get_base_type(cur_func_sym);
627 struct statement *tmp;
628 int found = 0;
630 if (!option_info)
631 return;
632 if (last_goto_statement_handled)
633 return;
634 last_goto_statement_handled = 1;
636 if (!goto_stmt->goto_label ||
637 goto_stmt->goto_label->type != SYM_LABEL ||
638 !goto_stmt->goto_label->ident)
639 return;
640 goto_name = goto_stmt->goto_label->ident->name;
642 func_stmt = base_type->stmt;
643 if (!func_stmt)
644 func_stmt = base_type->inline_stmt;
645 if (!func_stmt)
646 return;
647 if (func_stmt->type != STMT_COMPOUND)
648 return;
650 FOR_EACH_PTR(func_stmt->stmts, tmp) {
651 if (!found) {
652 if (tmp->type != STMT_LABEL)
653 continue;
654 if (!tmp->label_identifier ||
655 tmp->label_identifier->type != SYM_LABEL ||
656 !tmp->label_identifier->ident)
657 continue;
658 label_name = tmp->label_identifier->ident->name;
659 if (strcmp(goto_name, label_name) != 0)
660 continue;
661 found = 1;
663 __split_stmt(tmp);
664 } END_FOR_EACH_PTR(tmp);
667 static void fake_a_return(void)
669 struct symbol *return_type;
671 nullify_path();
672 __unnullify_path();
674 return_type = get_real_base_type(cur_func_sym);
675 return_type = get_real_base_type(return_type);
676 if (return_type != &void_ctype) {
677 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
678 nullify_path();
681 __pass_to_client(cur_func_sym, END_FUNC_HOOK);
682 __pass_to_client(cur_func_sym, AFTER_FUNC_HOOK);
685 void __split_stmt(struct statement *stmt)
687 sval_t sval;
689 if (!stmt)
690 goto out;
692 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
693 static char *printed = NULL;
695 __bail_on_rest_of_function = 1;
696 if (printed != cur_func)
697 sm_msg("Function too hairy. Giving up.");
698 fake_a_return();
699 final_pass = 0; /* turn off sm_msg() from here */
700 printed = cur_func;
701 return;
704 add_ptr_list(&big_statement_stack, stmt);
705 free_expression_stack(&big_expression_stack);
706 set_position(stmt->pos);
707 __pass_to_client(stmt, STMT_HOOK);
709 switch (stmt->type) {
710 case STMT_DECLARATION:
711 split_declaration(stmt->declaration);
712 break;
713 case STMT_RETURN:
714 __split_expr(stmt->ret_value);
715 __pass_to_client(stmt->ret_value, RETURN_HOOK);
716 __process_post_op_stack();
717 nullify_path();
718 break;
719 case STMT_EXPRESSION:
720 __split_expr(stmt->expression);
721 break;
722 case STMT_COMPOUND: {
723 struct statement *tmp;
725 __push_scope_hooks();
726 FOR_EACH_PTR(stmt->stmts, tmp) {
727 __split_stmt(tmp);
728 } END_FOR_EACH_PTR(tmp);
729 __call_scope_hooks();
730 break;
732 case STMT_IF:
733 if (known_condition_true(stmt->if_conditional)) {
734 __split_stmt(stmt->if_true);
735 break;
737 if (known_condition_false(stmt->if_conditional)) {
738 __split_stmt(stmt->if_false);
739 break;
741 if (option_known_conditions &&
742 implied_condition_true(stmt->if_conditional)) {
743 sm_info("this condition is true.");
744 __split_stmt(stmt->if_true);
745 break;
747 if (option_known_conditions &&
748 implied_condition_false(stmt->if_conditional)) {
749 sm_info("this condition is false.");
750 __split_stmt(stmt->if_false);
751 break;
753 __split_whole_condition(stmt->if_conditional);
754 __split_stmt(stmt->if_true);
755 if (empty_statement(stmt->if_true) &&
756 last_stmt_on_same_line() &&
757 !get_macro_name(stmt->if_true->pos))
758 sm_msg("warn: if();");
759 __push_true_states();
760 __use_false_states();
761 __split_stmt(stmt->if_false);
762 __merge_true_states();
763 break;
764 case STMT_ITERATOR:
765 if (stmt->iterator_pre_condition)
766 handle_pre_loop(stmt);
767 else if (stmt->iterator_post_condition)
768 handle_post_loop(stmt);
769 else {
770 // these are for(;;) type loops.
771 handle_pre_loop(stmt);
773 break;
774 case STMT_SWITCH:
775 if (get_value(stmt->switch_expression, &sval)) {
776 split_known_switch(stmt, sval);
777 break;
779 __split_expr(stmt->switch_expression);
780 push_expression(&switch_expr_stack, stmt->switch_expression);
781 __save_switch_states(top_expression(switch_expr_stack));
782 nullify_path();
783 __push_default();
784 __push_breaks();
785 __split_stmt(stmt->switch_statement);
786 if (!__pop_default())
787 __merge_switches(top_expression(switch_expr_stack),
788 NULL);
789 __discard_switches();
790 __merge_breaks();
791 pop_expression(&switch_expr_stack);
792 break;
793 case STMT_CASE:
794 __merge_switches(top_expression(switch_expr_stack),
795 stmt->case_expression);
796 __pass_case_to_client(top_expression(switch_expr_stack),
797 stmt->case_expression);
798 if (!stmt->case_expression)
799 __set_default();
800 __split_expr(stmt->case_expression);
801 __split_expr(stmt->case_to);
802 __split_stmt(stmt->case_statement);
803 break;
804 case STMT_LABEL:
805 if (stmt->label_identifier &&
806 stmt->label_identifier->type == SYM_LABEL &&
807 stmt->label_identifier->ident) {
808 loop_count |= 0x80000000;
809 __merge_gotos(stmt->label_identifier->ident->name);
811 __split_stmt(stmt->label_statement);
812 break;
813 case STMT_GOTO:
814 __split_expr(stmt->goto_expression);
815 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
816 if (!strcmp(stmt->goto_label->ident->name, "break")) {
817 __process_breaks();
818 } else if (!strcmp(stmt->goto_label->ident->name,
819 "continue")) {
820 __process_continues();
822 } else if (stmt->goto_label &&
823 stmt->goto_label->type == SYM_LABEL &&
824 stmt->goto_label->ident) {
825 __save_gotos(stmt->goto_label->ident->name);
827 nullify_path();
828 if (is_last_stmt(stmt))
829 handle_backward_goto(stmt);
830 break;
831 case STMT_NONE:
832 break;
833 case STMT_ASM:
834 __pass_to_client(stmt, ASM_HOOK);
835 __split_expr(stmt->asm_string);
836 split_asm_constraints(stmt->asm_outputs);
837 split_asm_constraints(stmt->asm_inputs);
838 split_asm_constraints(stmt->asm_clobbers);
839 break;
840 case STMT_CONTEXT:
841 break;
842 case STMT_RANGE:
843 __split_expr(stmt->range_expression);
844 __split_expr(stmt->range_low);
845 __split_expr(stmt->range_high);
846 break;
848 __pass_to_client(stmt, STMT_HOOK_AFTER);
849 out:
850 __process_post_op_stack();
853 static void split_expr_list(struct expression_list *expr_list)
855 struct expression *expr;
857 FOR_EACH_PTR(expr_list, expr) {
858 __split_expr(expr);
859 __process_post_op_stack();
860 } END_FOR_EACH_PTR(expr);
863 static void split_sym(struct symbol *sym)
865 if (!sym)
866 return;
867 if (!(sym->namespace & NS_SYMBOL))
868 return;
870 __split_stmt(sym->stmt);
871 __split_expr(sym->array_size);
872 split_symlist(sym->arguments);
873 split_symlist(sym->symbol_list);
874 __split_stmt(sym->inline_stmt);
875 split_symlist(sym->inline_symbol_list);
878 static void split_symlist(struct symbol_list *sym_list)
880 struct symbol *sym;
882 FOR_EACH_PTR(sym_list, sym) {
883 split_sym(sym);
884 } END_FOR_EACH_PTR(sym);
887 typedef void (fake_cb)(struct expression *expr);
889 static int member_to_number(struct expression *expr, struct ident *member)
891 struct symbol *type, *tmp;
892 char *name;
893 int i;
895 if (!member)
896 return -1;
897 name = member->name;
899 type = get_type(expr);
900 if (!type || type->type != SYM_STRUCT)
901 return -1;
903 i = -1;
904 FOR_EACH_PTR(type->symbol_list, tmp) {
905 i++;
906 if (!tmp->ident)
907 continue;
908 if (strcmp(name, tmp->ident->name) == 0)
909 return i;
910 } END_FOR_EACH_PTR(tmp);
911 return -1;
914 static struct ident *number_to_member(struct expression *expr, int num)
916 struct symbol *type, *member;
917 int i = 0;
919 type = get_type(expr);
920 if (!type || type->type != SYM_STRUCT)
921 return NULL;
923 FOR_EACH_PTR(type->symbol_list, member) {
924 if (i == num)
925 return member->ident;
926 i++;
927 } END_FOR_EACH_PTR(member);
928 return NULL;
931 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
933 struct member_set {
934 struct ident *ident;
935 int set;
938 static struct member_set *alloc_member_set(struct symbol *type)
940 struct member_set *member_set;
941 struct symbol *member;
942 int member_count;
943 int member_idx;
945 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
946 member_set = malloc(member_count * sizeof(*member_set));
947 member_idx = 0;
948 FOR_EACH_PTR(type->symbol_list, member) {
949 member_set[member_idx].ident = member->ident;
950 member_set[member_idx].set = 0;
951 member_idx++;
952 } END_FOR_EACH_PTR(member);
954 return member_set;
957 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
959 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
960 int i;
962 for (i = 0; i < member_count; i++) {
963 if (member_set[i].ident == ident) {
964 member_set[i].set = 1;
965 return;
968 // crap. this is buggy.
969 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
972 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
974 struct expression *deref, *assign;
975 struct symbol *member, *member_type;
976 int member_idx;
978 member_idx = 0;
979 FOR_EACH_PTR(type->symbol_list, member) {
980 if (!member->ident || member_set[member_idx].set) {
981 member_idx++;
982 continue;
984 member_type = get_real_base_type(member);
985 if (!member_type || member_type->type == SYM_ARRAY) {
986 member_idx++;
987 continue;
989 /* TODO: this should be handled recursively and not ignored */
990 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION) {
991 member_idx++;
992 continue;
994 deref = member_expression(symbol, '.', member->ident);
995 assign = assign_expression(deref, zero_expr());
996 __split_expr(assign);
997 member_idx++;
998 } END_FOR_EACH_PTR(member);
1002 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1004 struct expression *deref, *assign, *tmp;
1005 struct symbol *struct_type, *type;
1006 struct ident *member;
1007 int member_idx;
1008 struct member_set *member_set;
1010 struct_type = get_type(symbol);
1011 if (!struct_type ||
1012 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1013 return;
1015 member_set = alloc_member_set(struct_type);
1017 member_idx = 0;
1018 FOR_EACH_PTR(members, tmp) {
1019 member = number_to_member(symbol, member_idx);
1020 while (tmp->type == EXPR_IDENTIFIER) {
1021 member = tmp->expr_ident;
1022 member_idx = member_to_number(symbol, member);
1023 tmp = tmp->ident_expression;
1025 mark_member_as_set(struct_type, member_set, member);
1026 member_idx++;
1027 deref = member_expression(symbol, '.', member);
1028 if (tmp->type == EXPR_INITIALIZER) {
1029 type = get_type(deref);
1030 if (type && type->type == SYM_ARRAY)
1031 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1032 else
1033 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1034 } else {
1035 assign = assign_expression(deref, tmp);
1036 fake_cb(assign);
1038 } END_FOR_EACH_PTR(tmp);
1040 set_unset_to_zero(symbol, struct_type, member_set);
1043 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1045 fake_member_assigns_helper(symbol_expression(sym),
1046 sym->initializer->expr_list, fake_cb);
1049 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1051 struct expression *offset, *binop, *assign, *tmp;
1052 struct symbol *type;
1053 int idx;
1055 idx = 0;
1056 FOR_EACH_PTR(expr_list, tmp) {
1057 if (tmp->type == EXPR_INDEX) {
1058 if (tmp->idx_from != tmp->idx_to)
1059 return;
1060 idx = tmp->idx_from;
1061 if (!tmp->idx_expression)
1062 goto next;
1063 tmp = tmp->idx_expression;
1065 offset = value_expr(idx);
1066 binop = array_element_expression(array, offset);
1067 if (tmp->type == EXPR_INITIALIZER) {
1068 type = get_type(binop);
1069 if (type && type->type == SYM_ARRAY)
1070 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1071 else
1072 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1073 } else {
1074 assign = assign_expression(binop, tmp);
1075 fake_cb(assign);
1077 next:
1078 idx++;
1079 } END_FOR_EACH_PTR(tmp);
1082 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1084 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1087 static void fake_assign_expr(struct symbol *sym)
1089 struct expression *assign, *symbol;
1091 symbol = symbol_expression(sym);
1092 assign = assign_expression(symbol, sym->initializer);
1093 __split_expr(assign);
1096 static void call_split_expr(struct expression *expr)
1098 __split_expr(expr);
1101 static void do_initializer_stuff(struct symbol *sym)
1103 if (!sym->initializer)
1104 return;
1106 if (sym->initializer->type == EXPR_INITIALIZER) {
1107 if (get_real_base_type(sym)->type == SYM_ARRAY)
1108 fake_element_assigns(sym, call_split_expr);
1109 else
1110 fake_member_assigns(sym, call_split_expr);
1111 } else {
1112 fake_assign_expr(sym);
1116 static void split_declaration(struct symbol_list *sym_list)
1118 struct symbol *sym;
1120 FOR_EACH_PTR(sym_list, sym) {
1121 __pass_to_client(sym, DECLARATION_HOOK);
1122 do_initializer_stuff(sym);
1123 split_sym(sym);
1124 } END_FOR_EACH_PTR(sym);
1127 static void call_global_assign_hooks(struct expression *assign)
1129 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1132 static void fake_global_assign(struct symbol *sym)
1134 struct expression *assign, *symbol;
1136 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1137 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1138 fake_element_assigns(sym, call_global_assign_hooks);
1139 } else if (sym->initializer) {
1140 symbol = symbol_expression(sym);
1141 assign = assign_expression(symbol, sym->initializer);
1142 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1143 } else {
1144 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1146 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1147 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1148 fake_member_assigns(sym, call_global_assign_hooks);
1149 } else if (sym->initializer) {
1150 symbol = symbol_expression(sym);
1151 assign = assign_expression(symbol, sym->initializer);
1152 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1153 } else {
1154 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1156 } else {
1157 symbol = symbol_expression(sym);
1158 if (sym->initializer)
1159 assign = assign_expression(symbol, sym->initializer);
1160 else
1161 assign = assign_expression(symbol, zero_expr());
1162 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1166 static void start_function_definition(struct symbol *sym)
1168 __in_function_def = 1;
1169 __pass_to_client(sym, FUNC_DEF_HOOK);
1170 __in_function_def = 0;
1171 __pass_to_client(sym, AFTER_DEF_HOOK);
1175 static void split_function(struct symbol *sym)
1177 struct symbol *base_type = get_base_type(sym);
1179 if (!base_type->stmt && !base_type->inline_stmt)
1180 return;
1182 gettimeofday(&fn_start_time, NULL);
1183 cur_func_sym = sym;
1184 if (sym->ident)
1185 cur_func = sym->ident->name;
1186 __smatch_lineno = sym->pos.line;
1187 loop_count = 0;
1188 last_goto_statement_handled = 0;
1189 sm_debug("new function: %s\n", cur_func);
1190 __stree_id = 0;
1191 if (option_two_passes) {
1192 __unnullify_path();
1193 loop_num = 0;
1194 final_pass = 0;
1195 start_function_definition(sym);
1196 __split_stmt(base_type->stmt);
1197 __split_stmt(base_type->inline_stmt);
1198 nullify_path();
1200 __unnullify_path();
1201 loop_num = 0;
1202 final_pass = 1;
1203 start_function_definition(sym);
1204 __split_stmt(base_type->stmt);
1205 __split_stmt(base_type->inline_stmt);
1206 __pass_to_client(sym, END_FUNC_HOOK);
1207 __pass_to_client(sym, AFTER_FUNC_HOOK);
1209 cur_func_sym = NULL;
1210 cur_func = NULL;
1211 clear_all_states();
1212 free_data_info_allocs();
1213 free_expression_stack(&switch_expr_stack);
1214 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1215 __bail_on_rest_of_function = 0;
1218 static void parse_inline(struct expression *call)
1220 struct symbol *base_type;
1221 int loop_num_bak = loop_num;
1222 int final_pass_bak = final_pass;
1223 char *cur_func_bak = cur_func;
1224 struct statement_list *big_statement_stack_bak = big_statement_stack;
1225 struct expression_list *big_expression_stack_bak = big_expression_stack;
1226 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1227 struct symbol *cur_func_sym_bak = cur_func_sym;
1229 __pass_to_client(call, INLINE_FN_START);
1230 final_pass = 0; /* don't print anything */
1231 __inline_fn = call;
1233 base_type = get_base_type(call->fn->symbol);
1234 cur_func_sym = call->fn->symbol;
1235 if (call->fn->symbol->ident)
1236 cur_func = call->fn->symbol->ident->name;
1237 else
1238 cur_func = NULL;
1239 set_position(call->fn->symbol->pos);
1241 save_all_states();
1242 big_statement_stack = NULL;
1243 big_expression_stack = NULL;
1244 switch_expr_stack = NULL;
1246 sm_debug("inline function: %s\n", cur_func);
1247 __unnullify_path();
1248 loop_num = 0;
1249 start_function_definition(call->fn->symbol);
1250 __split_stmt(base_type->stmt);
1251 __split_stmt(base_type->inline_stmt);
1252 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1253 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1255 free_expression_stack(&switch_expr_stack);
1256 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1257 nullify_path();
1258 free_goto_stack();
1260 loop_num = loop_num_bak;
1261 final_pass = final_pass_bak;
1262 cur_func_sym = cur_func_sym_bak;
1263 cur_func = cur_func_bak;
1264 big_statement_stack = big_statement_stack_bak;
1265 big_expression_stack = big_expression_stack_bak;
1266 switch_expr_stack = switch_expr_stack_bak;
1268 restore_all_states();
1269 set_position(call->pos);
1270 __inline_fn = NULL;
1271 __pass_to_client(call, INLINE_FN_END);
1274 static struct symbol_list *inlines_called;
1275 static void add_inline_function(struct symbol *sym)
1277 static struct symbol_list *already_added;
1278 struct symbol *tmp;
1280 FOR_EACH_PTR(already_added, tmp) {
1281 if (tmp == sym)
1282 return;
1283 } END_FOR_EACH_PTR(tmp);
1285 add_ptr_list(&already_added, sym);
1286 add_ptr_list(&inlines_called, sym);
1289 static void process_inlines(void)
1291 struct symbol *tmp;
1293 FOR_EACH_PTR(inlines_called, tmp) {
1294 split_function(tmp);
1295 } END_FOR_EACH_PTR(tmp);
1296 free_ptr_list(&inlines_called);
1299 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1301 struct symbol *sym;
1303 FOR_EACH_PTR_REVERSE(big_list, sym) {
1304 if (!sym->scope)
1305 continue;
1306 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1307 return sym;
1308 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1309 return sym;
1310 } END_FOR_EACH_PTR_REVERSE(sym);
1312 return NULL;
1315 static void split_inlines_in_scope(struct symbol *sym)
1317 struct symbol *base;
1318 struct symbol_list *scope_list;
1319 int stream;
1321 scope_list = sym->scope->symbols;
1322 stream = sym->pos.stream;
1324 /* find the last static symbol in the file */
1325 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1326 if (sym->pos.stream != stream)
1327 continue;
1328 if (sym->type != SYM_NODE)
1329 continue;
1330 base = get_base_type(sym);
1331 if (!base)
1332 continue;
1333 if (base->type != SYM_FN)
1334 continue;
1335 if (!base->inline_stmt)
1336 continue;
1337 add_inline_function(sym);
1338 } END_FOR_EACH_PTR_REVERSE(sym);
1340 process_inlines();
1343 static void split_inlines(struct symbol_list *sym_list)
1345 struct symbol *sym;
1347 sym = get_last_scoped_symbol(sym_list, 0);
1348 if (sym)
1349 split_inlines_in_scope(sym);
1350 sym = get_last_scoped_symbol(sym_list, 1);
1351 if (sym)
1352 split_inlines_in_scope(sym);
1355 static struct stree *clone_estates_perm(struct stree *orig)
1357 struct stree *ret = NULL;
1358 struct sm_state *tmp;
1360 FOR_EACH_SM(orig, tmp) {
1361 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1362 } END_FOR_EACH_SM(tmp);
1364 return ret;
1367 static void split_functions(struct symbol_list *sym_list)
1369 struct symbol *sym;
1371 __unnullify_path();
1372 FOR_EACH_PTR(sym_list, sym) {
1373 set_position(sym->pos);
1374 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1375 __pass_to_client(sym, BASE_HOOK);
1376 fake_global_assign(sym);
1378 } END_FOR_EACH_PTR(sym);
1379 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1380 nullify_path();
1382 FOR_EACH_PTR(sym_list, sym) {
1383 set_position(sym->pos);
1384 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1385 split_function(sym);
1386 process_inlines();
1388 } END_FOR_EACH_PTR(sym);
1389 split_inlines(sym_list);
1390 __pass_to_client(sym_list, END_FILE_HOOK);
1393 void smatch(int argc, char **argv)
1396 struct string_list *filelist = NULL;
1397 struct symbol_list *sym_list;
1399 if (argc < 2) {
1400 printf("Usage: smatch [--debug] <filename.c>\n");
1401 exit(1);
1403 sparse_initialize(argc, argv, &filelist);
1404 set_valid_ptr_max();
1405 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1406 if (option_file_output) {
1407 char buf[256];
1409 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1410 sm_outfd = fopen(buf, "w");
1411 if (!sm_outfd) {
1412 printf("Error: Cannot open %s\n", base_file);
1413 exit(1);
1416 sym_list = sparse_keep_tokens(base_file);
1417 split_functions(sym_list);
1418 } END_FOR_EACH_PTR_NOTAG(base_file);