db/fixup_kernel.sh: bitmap_allocate_region doesn't return 1
[smatch.git] / smatch_flow.c
blob61465aedba49a78b308e11a4d80f23f7bc77df27
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 int __expr_stmt_count;
42 int __in_function_def;
43 static struct expression_list *switch_expr_stack = NULL;
44 static struct expression_list *post_op_stack = NULL;
46 struct expression_list *big_expression_stack;
47 struct statement_list *big_statement_stack;
48 int __in_pre_condition = 0;
49 int __bail_on_rest_of_function = 0;
50 static struct timeval fn_start_time;
51 char *get_function(void) { return cur_func; }
52 int get_lineno(void) { return __smatch_lineno; }
53 int inside_loop(void) { return !!loop_count; }
54 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
55 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
56 int in_expression_statement(void) { return !!__expr_stmt_count; }
58 static void split_symlist(struct symbol_list *sym_list);
59 static void split_declaration(struct symbol_list *sym_list);
60 static void split_expr_list(struct expression_list *expr_list);
61 static void add_inline_function(struct symbol *sym);
62 static void parse_inline(struct expression *expr);
64 int option_assume_loops = 0;
65 int option_known_conditions = 0;
66 int option_two_passes = 0;
67 struct symbol *cur_func_sym = NULL;
68 struct stree *global_states;
70 int outside_of_function(void)
72 return cur_func_sym == NULL;
75 const char *get_filename(void)
77 if (option_info)
78 return base_file;
79 if (option_full_path)
80 return full_filename;
81 return filename;
84 const char *get_base_file(void)
86 return base_file;
89 static void set_position(struct position pos)
91 int len;
92 static int prev_stream = -1;
94 if (pos.stream == 0 && pos.line == 0)
95 return;
97 __smatch_lineno = pos.line;
99 if (pos.stream == prev_stream)
100 return;
102 filename = stream_name(pos.stream);
104 free(full_filename);
105 pathname = getcwd(NULL, 0);
106 if (pathname) {
107 len = strlen(pathname) + 1 + strlen(filename) + 1;
108 full_filename = malloc(len);
109 snprintf(full_filename, len, "%s/%s", pathname, filename);
110 } else {
111 full_filename = alloc_string(filename);
113 free(pathname);
116 static int is_inline_func(struct expression *expr)
118 if (expr->type != EXPR_SYMBOL || !expr->symbol)
119 return 0;
120 if (expr->symbol->ctype.modifiers & MOD_INLINE)
121 return 1;
122 return 0;
125 static int is_noreturn_func(struct expression *expr)
127 if (expr->type != EXPR_SYMBOL || !expr->symbol)
128 return 0;
129 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
130 return 1;
131 return 0;
134 int inlinable(struct expression *expr)
136 struct symbol *sym;
138 if (__inline_fn) /* don't nest */
139 return 0;
141 if (expr->type != EXPR_SYMBOL || !expr->symbol)
142 return 0;
143 if (is_no_inline_function(expr->symbol->ident->name))
144 return 0;
145 sym = get_base_type(expr->symbol);
146 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
147 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
148 return 1;
149 return 0;
151 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
152 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
153 return 1;
154 return 0;
156 return 0;
159 void __process_post_op_stack(void)
161 struct expression *expr;
163 FOR_EACH_PTR(post_op_stack, expr) {
164 __pass_to_client(expr, OP_HOOK);
165 } END_FOR_EACH_PTR(expr);
167 __free_ptr_list((struct ptr_list **)&post_op_stack);
170 void __split_expr(struct expression *expr)
172 if (!expr)
173 return;
175 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
177 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
178 return;
179 if (__in_fake_assign >= 4) /* don't allow too much nesting */
180 return;
182 push_expression(&big_expression_stack, expr);
183 set_position(expr->pos);
184 __pass_to_client(expr, EXPR_HOOK);
186 switch (expr->type) {
187 case EXPR_PREOP:
188 if (expr->op == '*')
189 __pass_to_client(expr, DEREF_HOOK);
190 __split_expr(expr->unop);
191 __pass_to_client(expr, OP_HOOK);
192 break;
193 case EXPR_POSTOP:
194 __split_expr(expr->unop);
195 push_expression(&post_op_stack, expr);
196 break;
197 case EXPR_STATEMENT:
198 __expr_stmt_count++;
199 __split_stmt(expr->statement);
200 __expr_stmt_count--;
201 break;
202 case EXPR_LOGICAL:
203 case EXPR_COMPARE:
204 __pass_to_client(expr, LOGIC_HOOK);
205 __handle_logic(expr);
206 break;
207 case EXPR_BINOP:
208 __pass_to_client(expr, BINOP_HOOK);
209 case EXPR_COMMA:
210 __split_expr(expr->left);
211 __process_post_op_stack();
212 __split_expr(expr->right);
213 break;
214 case EXPR_ASSIGNMENT: {
215 struct expression *tmp;
217 if (!expr->right)
218 break;
220 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
222 /* foo = !bar() */
223 if (__handle_condition_assigns(expr))
224 break;
225 /* foo = (x < 5 ? foo : 5); */
226 if (__handle_select_assigns(expr))
227 break;
228 /* foo = ({frob(); frob(); frob(); 1;}) */
229 if (__handle_expr_statement_assigns(expr))
230 break;
232 __split_expr(expr->right);
233 if (outside_of_function())
234 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
235 else
236 __pass_to_client(expr, ASSIGNMENT_HOOK);
238 __fake_struct_member_assignments(expr);
240 tmp = strip_expr(expr->right);
241 if (tmp->type == EXPR_CALL)
242 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
243 if (get_macro_name(tmp->pos) &&
244 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
245 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
246 __split_expr(expr->left);
247 break;
249 case EXPR_DEREF:
250 __pass_to_client(expr, DEREF_HOOK);
251 __split_expr(expr->deref);
252 break;
253 case EXPR_SLICE:
254 __split_expr(expr->base);
255 break;
256 case EXPR_CAST:
257 case EXPR_FORCE_CAST:
258 __pass_to_client(expr, CAST_HOOK);
259 __split_expr(expr->cast_expression);
260 break;
261 case EXPR_SIZEOF:
262 if (expr->cast_expression)
263 __pass_to_client(strip_parens(expr->cast_expression),
264 SIZEOF_HOOK);
265 break;
266 case EXPR_OFFSETOF:
267 case EXPR_ALIGNOF:
268 evaluate_expression(expr);
269 break;
270 case EXPR_CONDITIONAL:
271 case EXPR_SELECT:
272 if (known_condition_true(expr->conditional)) {
273 __split_expr(expr->cond_true);
274 break;
276 if (known_condition_false(expr->conditional)) {
277 __split_expr(expr->cond_false);
278 break;
280 __pass_to_client(expr, SELECT_HOOK);
281 __split_whole_condition(expr->conditional);
282 __split_expr(expr->cond_true);
283 __push_true_states();
284 __use_false_states();
285 __split_expr(expr->cond_false);
286 __merge_true_states();
287 break;
288 case EXPR_CALL:
289 if (sym_name_is("__builtin_constant_p", expr->fn))
290 break;
291 split_expr_list(expr->args);
292 __split_expr(expr->fn);
293 if (is_inline_func(expr->fn))
294 add_inline_function(expr->fn->symbol);
295 if (inlinable(expr->fn))
296 __inline_call = 1;
297 __process_post_op_stack();
298 __pass_to_client(expr, FUNCTION_CALL_HOOK);
299 __inline_call = 0;
300 if (inlinable(expr->fn)) {
301 parse_inline(expr);
303 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
304 if (is_noreturn_func(expr->fn))
305 nullify_path();
306 break;
307 case EXPR_INITIALIZER:
308 split_expr_list(expr->expr_list);
309 break;
310 case EXPR_IDENTIFIER:
311 __split_expr(expr->ident_expression);
312 break;
313 case EXPR_INDEX:
314 __split_expr(expr->idx_expression);
315 break;
316 case EXPR_POS:
317 __split_expr(expr->init_expr);
318 break;
319 case EXPR_SYMBOL:
320 __pass_to_client(expr, SYM_HOOK);
321 break;
322 case EXPR_STRING:
323 __pass_to_client(expr, STRING_HOOK);
324 break;
325 default:
326 break;
328 pop_expression(&big_expression_stack);
331 static int is_forever_loop(struct statement *stmt)
333 struct expression *expr;
335 expr = strip_expr(stmt->iterator_pre_condition);
336 if (!expr)
337 expr = stmt->iterator_post_condition;
338 if (!expr) {
339 /* this is a for(;;) loop... */
340 return 1;
343 if (expr->type == EXPR_VALUE && expr->value == 1)
344 return 1;
346 return 0;
349 static int loop_num;
350 static char *get_loop_name(int num)
352 char buf[256];
354 snprintf(buf, 255, "-loop%d", num);
355 buf[255] = '\0';
356 return alloc_sname(buf);
360 * Pre Loops are while and for loops.
362 static void handle_pre_loop(struct statement *stmt)
364 int once_through; /* we go through the loop at least once */
365 struct sm_state *extra_sm = NULL;
366 int unchanged = 0;
367 char *loop_name;
368 struct stree *stree = NULL;
369 struct sm_state *sm = NULL;
371 loop_name = get_loop_name(loop_num);
372 loop_num++;
374 __split_stmt(stmt->iterator_pre_statement);
376 once_through = implied_condition_true(stmt->iterator_pre_condition);
378 loop_count++;
379 __push_continues();
380 __push_breaks();
382 __merge_gotos(loop_name);
384 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
385 __in_pre_condition++;
386 __pass_to_client(stmt, PRELOOP_HOOK);
387 __split_whole_condition(stmt->iterator_pre_condition);
388 __in_pre_condition--;
389 FOR_EACH_SM(stree, sm) {
390 set_state(sm->owner, sm->name, sm->sym, sm->state);
391 } END_FOR_EACH_SM(sm);
392 free_stree(&stree);
393 if (extra_sm)
394 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
396 if (option_assume_loops)
397 once_through = 1;
399 __split_stmt(stmt->iterator_statement);
400 __warn_on_silly_pre_loops();
401 if (is_forever_loop(stmt)) {
402 __merge_continues();
403 __save_gotos(loop_name);
405 __push_fake_cur_stree();
406 __split_stmt(stmt->iterator_post_statement);
407 stree = __pop_fake_cur_stree();
409 __discard_false_states();
410 __use_breaks();
412 if (!__path_is_null())
413 __merge_stree_into_cur(stree);
414 free_stree(&stree);
415 } else {
416 __merge_continues();
417 unchanged = __iterator_unchanged(extra_sm);
418 __split_stmt(stmt->iterator_post_statement);
419 __save_gotos(loop_name);
420 __in_pre_condition++;
421 __split_whole_condition(stmt->iterator_pre_condition);
422 __in_pre_condition--;
423 nullify_path();
424 __merge_false_states();
425 if (once_through)
426 __discard_false_states();
427 else
428 __merge_false_states();
430 if (extra_sm && unchanged)
431 __extra_pre_loop_hook_after(extra_sm,
432 stmt->iterator_post_statement,
433 stmt->iterator_pre_condition);
434 __merge_breaks();
436 loop_count--;
440 * Post loops are do {} while();
442 static void handle_post_loop(struct statement *stmt)
444 char *loop_name;
446 loop_name = get_loop_name(loop_num);
447 loop_num++;
448 loop_count++;
450 __push_continues();
451 __push_breaks();
452 __merge_gotos(loop_name);
453 __split_stmt(stmt->iterator_statement);
454 __merge_continues();
455 if (!is_zero(stmt->iterator_post_condition))
456 __save_gotos(loop_name);
458 if (is_forever_loop(stmt)) {
459 __use_breaks();
460 } else {
461 __split_whole_condition(stmt->iterator_post_condition);
462 __use_false_states();
463 __merge_breaks();
465 loop_count--;
468 static int empty_statement(struct statement *stmt)
470 if (!stmt)
471 return 0;
472 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
473 return 1;
474 return 0;
477 static int last_stmt_on_same_line(void)
479 struct statement *stmt;
480 int i = 0;
482 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
483 if (!i++)
484 continue;
485 if (stmt->pos.line == get_lineno())
486 return 1;
487 return 0;
488 } END_FOR_EACH_PTR_REVERSE(stmt);
489 return 0;
492 static void split_asm_constraints(struct expression_list *expr_list)
494 struct expression *expr;
495 int state = 0;
497 FOR_EACH_PTR(expr_list, expr) {
498 switch (state) {
499 case 0: /* identifier */
500 case 1: /* constraint */
501 state++;
502 continue;
503 case 2: /* expression */
504 state = 0;
505 __split_expr(expr);
506 continue;
508 } END_FOR_EACH_PTR(expr);
511 static int is_case_val(struct statement *stmt, sval_t sval)
513 sval_t case_sval;
515 if (stmt->type != STMT_CASE)
516 return 0;
517 if (!stmt->case_expression) {
518 __set_default();
519 return 1;
521 if (!get_value(stmt->case_expression, &case_sval))
522 return 0;
523 if (case_sval.value == sval.value)
524 return 1;
525 return 0;
528 static void split_known_switch(struct statement *stmt, sval_t sval)
530 struct statement *tmp;
532 __split_expr(stmt->switch_expression);
534 push_expression(&switch_expr_stack, stmt->switch_expression);
535 __save_switch_states(top_expression(switch_expr_stack));
536 nullify_path();
537 __push_default();
538 __push_breaks();
540 stmt = stmt->switch_statement;
542 __push_scope_hooks();
543 FOR_EACH_PTR(stmt->stmts, tmp) {
544 __smatch_lineno = tmp->pos.line;
545 if (is_case_val(tmp, sval)) {
546 __merge_switches(top_expression(switch_expr_stack),
547 stmt->case_expression);
548 __pass_case_to_client(top_expression(switch_expr_stack),
549 stmt->case_expression);
551 if (__path_is_null())
552 continue;
553 __split_stmt(tmp);
554 if (__path_is_null()) {
555 __set_default();
556 goto out;
558 } END_FOR_EACH_PTR(tmp);
559 out:
560 __call_scope_hooks();
561 if (!__pop_default())
562 __merge_switches(top_expression(switch_expr_stack),
563 NULL);
564 __discard_switches();
565 __merge_breaks();
566 pop_expression(&switch_expr_stack);
569 static int taking_too_long(void)
571 int ms;
573 ms = ms_since(&fn_start_time);
574 if (ms > 1000 * 60 * 5) /* five minutes */
575 return 1;
576 return 0;
579 void __split_stmt(struct statement *stmt)
581 sval_t sval;
583 if (!stmt)
584 goto out;
586 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
587 static char *printed = NULL;
589 __bail_on_rest_of_function = 1;
590 if (printed != cur_func)
591 sm_msg("Function too hairy. Giving up.");
592 final_pass = 0; /* turn off sm_msg() from here */
593 printed = cur_func;
594 return;
597 add_ptr_list(&big_statement_stack, stmt);
598 free_expression_stack(&big_expression_stack);
599 set_position(stmt->pos);
600 __pass_to_client(stmt, STMT_HOOK);
602 switch (stmt->type) {
603 case STMT_DECLARATION:
604 split_declaration(stmt->declaration);
605 break;
606 case STMT_RETURN:
607 __split_expr(stmt->ret_value);
608 __pass_to_client(stmt->ret_value, RETURN_HOOK);
609 __process_post_op_stack();
610 nullify_path();
611 break;
612 case STMT_EXPRESSION:
613 __split_expr(stmt->expression);
614 break;
615 case STMT_COMPOUND: {
616 struct statement *tmp;
618 __push_scope_hooks();
619 FOR_EACH_PTR(stmt->stmts, tmp) {
620 __split_stmt(tmp);
621 } END_FOR_EACH_PTR(tmp);
622 __call_scope_hooks();
623 break;
625 case STMT_IF:
626 if (known_condition_true(stmt->if_conditional)) {
627 __split_stmt(stmt->if_true);
628 break;
630 if (known_condition_false(stmt->if_conditional)) {
631 __split_stmt(stmt->if_false);
632 break;
634 if (option_known_conditions &&
635 implied_condition_true(stmt->if_conditional)) {
636 sm_info("this condition is true.");
637 __split_stmt(stmt->if_true);
638 break;
640 if (option_known_conditions &&
641 implied_condition_false(stmt->if_conditional)) {
642 sm_info("this condition is false.");
643 __split_stmt(stmt->if_false);
644 break;
646 __split_whole_condition(stmt->if_conditional);
647 __split_stmt(stmt->if_true);
648 if (empty_statement(stmt->if_true) &&
649 last_stmt_on_same_line() &&
650 !get_macro_name(stmt->if_true->pos))
651 sm_msg("warn: if();");
652 __push_true_states();
653 __use_false_states();
654 __split_stmt(stmt->if_false);
655 __merge_true_states();
656 break;
657 case STMT_ITERATOR:
658 if (stmt->iterator_pre_condition)
659 handle_pre_loop(stmt);
660 else if (stmt->iterator_post_condition)
661 handle_post_loop(stmt);
662 else {
663 // these are for(;;) type loops.
664 handle_pre_loop(stmt);
666 break;
667 case STMT_SWITCH:
668 if (get_value(stmt->switch_expression, &sval)) {
669 split_known_switch(stmt, sval);
670 break;
672 __split_expr(stmt->switch_expression);
673 push_expression(&switch_expr_stack, stmt->switch_expression);
674 __save_switch_states(top_expression(switch_expr_stack));
675 nullify_path();
676 __push_default();
677 __push_breaks();
678 __split_stmt(stmt->switch_statement);
679 if (!__pop_default())
680 __merge_switches(top_expression(switch_expr_stack),
681 NULL);
682 __discard_switches();
683 __merge_breaks();
684 pop_expression(&switch_expr_stack);
685 break;
686 case STMT_CASE:
687 __merge_switches(top_expression(switch_expr_stack),
688 stmt->case_expression);
689 __pass_case_to_client(top_expression(switch_expr_stack),
690 stmt->case_expression);
691 if (!stmt->case_expression)
692 __set_default();
693 __split_expr(stmt->case_expression);
694 __split_expr(stmt->case_to);
695 __split_stmt(stmt->case_statement);
696 break;
697 case STMT_LABEL:
698 if (stmt->label_identifier &&
699 stmt->label_identifier->type == SYM_LABEL &&
700 stmt->label_identifier->ident) {
701 loop_count |= 0x80000000;
702 __merge_gotos(stmt->label_identifier->ident->name);
704 __split_stmt(stmt->label_statement);
705 break;
706 case STMT_GOTO:
707 __split_expr(stmt->goto_expression);
708 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
709 if (!strcmp(stmt->goto_label->ident->name, "break")) {
710 __process_breaks();
711 } else if (!strcmp(stmt->goto_label->ident->name,
712 "continue")) {
713 __process_continues();
715 } else if (stmt->goto_label &&
716 stmt->goto_label->type == SYM_LABEL &&
717 stmt->goto_label->ident) {
718 __save_gotos(stmt->goto_label->ident->name);
720 nullify_path();
721 break;
722 case STMT_NONE:
723 break;
724 case STMT_ASM:
725 __pass_to_client(stmt, ASM_HOOK);
726 __split_expr(stmt->asm_string);
727 split_asm_constraints(stmt->asm_outputs);
728 split_asm_constraints(stmt->asm_inputs);
729 split_asm_constraints(stmt->asm_clobbers);
730 break;
731 case STMT_CONTEXT:
732 break;
733 case STMT_RANGE:
734 __split_expr(stmt->range_expression);
735 __split_expr(stmt->range_low);
736 __split_expr(stmt->range_high);
737 break;
739 __pass_to_client(stmt, STMT_HOOK_AFTER);
740 out:
741 __process_post_op_stack();
744 static void split_expr_list(struct expression_list *expr_list)
746 struct expression *expr;
748 FOR_EACH_PTR(expr_list, expr) {
749 __split_expr(expr);
750 __process_post_op_stack();
751 } END_FOR_EACH_PTR(expr);
754 static void split_sym(struct symbol *sym)
756 if (!sym)
757 return;
758 if (!(sym->namespace & NS_SYMBOL))
759 return;
761 __split_stmt(sym->stmt);
762 __split_expr(sym->array_size);
763 split_symlist(sym->arguments);
764 split_symlist(sym->symbol_list);
765 __split_stmt(sym->inline_stmt);
766 split_symlist(sym->inline_symbol_list);
769 static void split_symlist(struct symbol_list *sym_list)
771 struct symbol *sym;
773 FOR_EACH_PTR(sym_list, sym) {
774 split_sym(sym);
775 } END_FOR_EACH_PTR(sym);
778 typedef void (fake_cb)(struct expression *expr);
780 static int member_to_number(struct expression *expr, struct ident *member)
782 struct symbol *type, *tmp;
783 char *name;
784 int i;
786 if (!member)
787 return -1;
788 name = member->name;
790 type = get_type(expr);
791 if (!type || type->type != SYM_STRUCT)
792 return -1;
794 i = -1;
795 FOR_EACH_PTR(type->symbol_list, tmp) {
796 i++;
797 if (!tmp->ident)
798 continue;
799 if (strcmp(name, tmp->ident->name) == 0)
800 return i;
801 } END_FOR_EACH_PTR(tmp);
802 return -1;
805 static struct ident *number_to_member(struct expression *expr, int num)
807 struct symbol *type, *member;
808 int i = 0;
810 type = get_type(expr);
811 if (!type || type->type != SYM_STRUCT)
812 return NULL;
814 FOR_EACH_PTR(type->symbol_list, member) {
815 if (i == num)
816 return member->ident;
817 i++;
818 } END_FOR_EACH_PTR(member);
819 return NULL;
822 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
824 struct member_set {
825 struct ident *ident;
826 int set;
829 static struct member_set *alloc_member_set(struct symbol *type)
831 struct member_set *member_set;
832 struct symbol *member;
833 int member_count;
834 int member_idx;
836 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
837 member_set = malloc(member_count * sizeof(*member_set));
838 member_idx = 0;
839 FOR_EACH_PTR(type->symbol_list, member) {
840 member_set[member_idx].ident = member->ident;
841 member_set[member_idx].set = 0;
842 member_idx++;
843 } END_FOR_EACH_PTR(member);
845 return member_set;
848 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
850 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
851 int i;
853 for (i = 0; i < member_count; i++) {
854 if (member_set[i].ident == ident) {
855 member_set[i].set = 1;
856 return;
859 // crap. this is buggy.
860 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
863 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
865 struct expression *deref, *assign;
866 struct symbol *member, *member_type;
867 int member_idx;
869 member_idx = 0;
870 FOR_EACH_PTR(type->symbol_list, member) {
871 if (!member->ident || member_set[member_idx].set) {
872 member_idx++;
873 continue;
875 member_type = get_real_base_type(member);
876 if (!member_type || member_type->type == SYM_ARRAY) {
877 member_idx++;
878 continue;
880 /* TODO: this should be handled recursively and not ignored */
881 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION) {
882 member_idx++;
883 continue;
885 deref = member_expression(symbol, '.', member->ident);
886 assign = assign_expression(deref, zero_expr());
887 __split_expr(assign);
888 member_idx++;
889 } END_FOR_EACH_PTR(member);
893 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
895 struct expression *deref, *assign, *tmp;
896 struct symbol *struct_type, *type;
897 struct ident *member;
898 int member_idx;
899 struct member_set *member_set;
901 struct_type = get_type(symbol);
902 if (!struct_type ||
903 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
904 return;
906 member_set = alloc_member_set(struct_type);
908 member_idx = 0;
909 FOR_EACH_PTR(members, tmp) {
910 member = number_to_member(symbol, member_idx);
911 while (tmp->type == EXPR_IDENTIFIER) {
912 member = tmp->expr_ident;
913 member_idx = member_to_number(symbol, member);
914 tmp = tmp->ident_expression;
916 mark_member_as_set(struct_type, member_set, member);
917 member_idx++;
918 deref = member_expression(symbol, '.', member);
919 if (tmp->type == EXPR_INITIALIZER) {
920 type = get_type(deref);
921 if (type && type->type == SYM_ARRAY)
922 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
923 else
924 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
925 } else {
926 assign = assign_expression(deref, tmp);
927 fake_cb(assign);
929 } END_FOR_EACH_PTR(tmp);
931 set_unset_to_zero(symbol, struct_type, member_set);
934 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
936 fake_member_assigns_helper(symbol_expression(sym),
937 sym->initializer->expr_list, fake_cb);
940 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
942 struct expression *offset, *binop, *assign, *tmp;
943 struct symbol *type;
944 int idx;
946 idx = 0;
947 FOR_EACH_PTR(expr_list, tmp) {
948 if (tmp->type == EXPR_INDEX) {
949 if (tmp->idx_from != tmp->idx_to)
950 return;
951 idx = tmp->idx_from;
952 if (!tmp->idx_expression)
953 goto next;
954 tmp = tmp->idx_expression;
956 offset = value_expr(idx);
957 binop = array_element_expression(array, offset);
958 if (tmp->type == EXPR_INITIALIZER) {
959 type = get_type(binop);
960 if (type && type->type == SYM_ARRAY)
961 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
962 else
963 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
964 } else {
965 assign = assign_expression(binop, tmp);
966 fake_cb(assign);
968 next:
969 idx++;
970 } END_FOR_EACH_PTR(tmp);
973 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
975 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
978 static void fake_assign_expr(struct symbol *sym)
980 struct expression *assign, *symbol;
982 symbol = symbol_expression(sym);
983 assign = assign_expression(symbol, sym->initializer);
984 __split_expr(assign);
987 static void call_split_expr(struct expression *expr)
989 __split_expr(expr);
992 static void do_initializer_stuff(struct symbol *sym)
994 if (!sym->initializer)
995 return;
997 if (sym->initializer->type == EXPR_INITIALIZER) {
998 if (get_real_base_type(sym)->type == SYM_ARRAY)
999 fake_element_assigns(sym, call_split_expr);
1000 else
1001 fake_member_assigns(sym, call_split_expr);
1002 } else {
1003 fake_assign_expr(sym);
1007 static void split_declaration(struct symbol_list *sym_list)
1009 struct symbol *sym;
1011 FOR_EACH_PTR(sym_list, sym) {
1012 __pass_to_client(sym, DECLARATION_HOOK);
1013 do_initializer_stuff(sym);
1014 split_sym(sym);
1015 } END_FOR_EACH_PTR(sym);
1018 static void call_global_assign_hooks(struct expression *assign)
1020 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1023 static void fake_global_assign(struct symbol *sym)
1025 struct expression *assign, *symbol;
1027 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1028 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1029 fake_element_assigns(sym, call_global_assign_hooks);
1030 } else if (sym->initializer) {
1031 symbol = symbol_expression(sym);
1032 assign = assign_expression(symbol, sym->initializer);
1033 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1034 } else {
1035 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1037 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1038 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1039 fake_member_assigns(sym, call_global_assign_hooks);
1040 } else if (sym->initializer) {
1041 symbol = symbol_expression(sym);
1042 assign = assign_expression(symbol, sym->initializer);
1043 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1044 } else {
1045 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1047 } else {
1048 symbol = symbol_expression(sym);
1049 if (sym->initializer)
1050 assign = assign_expression(symbol, sym->initializer);
1051 else
1052 assign = assign_expression(symbol, zero_expr());
1053 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1057 static void start_function_definition(struct symbol *sym)
1059 __in_function_def = 1;
1060 __pass_to_client(sym, FUNC_DEF_HOOK);
1061 __in_function_def = 0;
1062 __pass_to_client(sym, AFTER_DEF_HOOK);
1066 static void split_function(struct symbol *sym)
1068 struct symbol *base_type = get_base_type(sym);
1070 gettimeofday(&fn_start_time, NULL);
1071 cur_func_sym = sym;
1072 if (sym->ident)
1073 cur_func = sym->ident->name;
1074 __smatch_lineno = sym->pos.line;
1075 loop_count = 0;
1076 sm_debug("new function: %s\n", cur_func);
1077 __stree_id = 0;
1078 if (option_two_passes) {
1079 __unnullify_path();
1080 loop_num = 0;
1081 final_pass = 0;
1082 start_function_definition(sym);
1083 __split_stmt(base_type->stmt);
1084 __split_stmt(base_type->inline_stmt);
1085 nullify_path();
1087 __unnullify_path();
1088 loop_num = 0;
1089 final_pass = 1;
1090 start_function_definition(sym);
1091 __split_stmt(base_type->stmt);
1092 __split_stmt(base_type->inline_stmt);
1093 __pass_to_client(sym, END_FUNC_HOOK);
1094 __pass_to_client(sym, AFTER_FUNC_HOOK);
1096 cur_func_sym = NULL;
1097 cur_func = NULL;
1098 clear_all_states();
1099 free_data_info_allocs();
1100 free_expression_stack(&switch_expr_stack);
1101 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1102 __bail_on_rest_of_function = 0;
1105 static void parse_inline(struct expression *call)
1107 struct symbol *base_type;
1108 int loop_num_bak = loop_num;
1109 int final_pass_bak = final_pass;
1110 char *cur_func_bak = cur_func;
1111 struct statement_list *big_statement_stack_bak = big_statement_stack;
1112 struct expression_list *big_expression_stack_bak = big_expression_stack;
1113 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1114 struct symbol *cur_func_sym_bak = cur_func_sym;
1116 __pass_to_client(call, INLINE_FN_START);
1117 final_pass = 0; /* don't print anything */
1118 __inline_fn = call;
1120 base_type = get_base_type(call->fn->symbol);
1121 cur_func_sym = call->fn->symbol;
1122 if (call->fn->symbol->ident)
1123 cur_func = call->fn->symbol->ident->name;
1124 else
1125 cur_func = NULL;
1126 set_position(call->fn->symbol->pos);
1128 save_all_states();
1129 big_statement_stack = NULL;
1130 big_expression_stack = NULL;
1131 switch_expr_stack = NULL;
1133 sm_debug("inline function: %s\n", cur_func);
1134 __unnullify_path();
1135 loop_num = 0;
1136 start_function_definition(call->fn->symbol);
1137 __split_stmt(base_type->stmt);
1138 __split_stmt(base_type->inline_stmt);
1139 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1140 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1142 free_expression_stack(&switch_expr_stack);
1143 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1144 nullify_path();
1145 free_goto_stack();
1147 loop_num = loop_num_bak;
1148 final_pass = final_pass_bak;
1149 cur_func_sym = cur_func_sym_bak;
1150 cur_func = cur_func_bak;
1151 big_statement_stack = big_statement_stack_bak;
1152 big_expression_stack = big_expression_stack_bak;
1153 switch_expr_stack = switch_expr_stack_bak;
1155 restore_all_states();
1156 set_position(call->pos);
1157 __inline_fn = NULL;
1158 __pass_to_client(call, INLINE_FN_END);
1161 static struct symbol_list *inlines_called;
1162 static void add_inline_function(struct symbol *sym)
1164 static struct symbol_list *already_added;
1165 struct symbol *tmp;
1167 FOR_EACH_PTR(already_added, tmp) {
1168 if (tmp == sym)
1169 return;
1170 } END_FOR_EACH_PTR(tmp);
1172 add_ptr_list(&already_added, sym);
1173 add_ptr_list(&inlines_called, sym);
1176 static void process_inlines(void)
1178 struct symbol *tmp;
1180 FOR_EACH_PTR(inlines_called, tmp) {
1181 split_function(tmp);
1182 } END_FOR_EACH_PTR(tmp);
1183 free_ptr_list(&inlines_called);
1186 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1188 struct symbol *sym;
1190 FOR_EACH_PTR_REVERSE(big_list, sym) {
1191 if (!sym->scope)
1192 continue;
1193 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1194 return sym;
1195 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1196 return sym;
1197 } END_FOR_EACH_PTR_REVERSE(sym);
1199 return NULL;
1202 static void split_inlines_in_scope(struct symbol *sym)
1204 struct symbol *base;
1205 struct symbol_list *scope_list;
1206 int stream;
1208 scope_list = sym->scope->symbols;
1209 stream = sym->pos.stream;
1211 /* find the last static symbol in the file */
1212 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1213 if (sym->pos.stream != stream)
1214 continue;
1215 if (sym->type != SYM_NODE)
1216 continue;
1217 base = get_base_type(sym);
1218 if (!base)
1219 continue;
1220 if (base->type != SYM_FN)
1221 continue;
1222 if (!base->inline_stmt)
1223 continue;
1224 add_inline_function(sym);
1225 } END_FOR_EACH_PTR_REVERSE(sym);
1227 process_inlines();
1230 static void split_inlines(struct symbol_list *sym_list)
1232 struct symbol *sym;
1234 sym = get_last_scoped_symbol(sym_list, 0);
1235 if (sym)
1236 split_inlines_in_scope(sym);
1237 sym = get_last_scoped_symbol(sym_list, 1);
1238 if (sym)
1239 split_inlines_in_scope(sym);
1242 static struct stree *clone_estates_perm(struct stree *orig)
1244 struct stree *ret = NULL;
1245 struct sm_state *tmp;
1247 FOR_EACH_SM(orig, tmp) {
1248 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1249 } END_FOR_EACH_SM(tmp);
1251 return ret;
1254 static void split_functions(struct symbol_list *sym_list)
1256 struct symbol *sym;
1258 __unnullify_path();
1259 FOR_EACH_PTR(sym_list, sym) {
1260 set_position(sym->pos);
1261 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1262 __pass_to_client(sym, BASE_HOOK);
1263 fake_global_assign(sym);
1265 } END_FOR_EACH_PTR(sym);
1266 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1267 nullify_path();
1269 FOR_EACH_PTR(sym_list, sym) {
1270 set_position(sym->pos);
1271 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1272 split_function(sym);
1273 process_inlines();
1275 } END_FOR_EACH_PTR(sym);
1276 split_inlines(sym_list);
1277 __pass_to_client(sym_list, END_FILE_HOOK);
1280 void smatch(int argc, char **argv)
1283 struct string_list *filelist = NULL;
1284 struct symbol_list *sym_list;
1286 if (argc < 2) {
1287 printf("Usage: smatch [--debug] <filename.c>\n");
1288 exit(1);
1290 sparse_initialize(argc, argv, &filelist);
1291 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1292 if (option_file_output) {
1293 char buf[256];
1295 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1296 sm_outfd = fopen(buf, "w");
1297 if (!sm_outfd) {
1298 printf("Error: Cannot open %s\n", base_file);
1299 exit(1);
1302 sym_list = sparse_keep_tokens(base_file);
1303 split_functions(sym_list);
1304 } END_FOR_EACH_PTR_NOTAG(base_file);