flow: introduce outside_of_function() to fix global initializer handling
[smatch.git] / smatch_flow.c
blobd97c036a835de11d669c80e209c86622d51c3d47
1 /*
2 * sparse/smatch_flow.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #define _GNU_SOURCE 1
11 #include <unistd.h>
12 #include <stdio.h>
13 #include "token.h"
14 #include "scope.h"
15 #include "smatch.h"
16 #include "smatch_expression_stacks.h"
17 #include "smatch_extra.h"
18 #include "smatch_slist.h"
20 int final_pass;
21 int __inline_call;
22 struct expression *__inline_fn;
24 static int __smatch_lineno = 0;
26 static char *base_file;
27 static const char *filename;
28 static char *pathname;
29 static char *full_filename;
30 static char *cur_func;
31 static int loop_count;
32 int __expr_stmt_count;
33 static struct expression_list *switch_expr_stack = NULL;
34 static struct expression_list *post_op_stack = NULL;
36 struct expression_list *big_expression_stack;
37 struct statement_list *big_statement_stack;
38 int __in_pre_condition = 0;
39 int __bail_on_rest_of_function = 0;
40 char *get_function(void) { return cur_func; }
41 int get_lineno(void) { return __smatch_lineno; }
42 int inside_loop(void) { return !!loop_count; }
43 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
44 int in_expression_statement(void) { return !!__expr_stmt_count; }
46 static void split_symlist(struct symbol_list *sym_list);
47 static void split_declaration(struct symbol_list *sym_list);
48 static void split_expr_list(struct expression_list *expr_list);
49 static void add_inline_function(struct symbol *sym);
50 static void parse_inline(struct expression *expr);
52 int option_assume_loops = 0;
53 int option_known_conditions = 0;
54 int option_two_passes = 0;
55 struct symbol *cur_func_sym = NULL;
57 int outside_of_function(void)
59 return cur_func_sym == NULL;
62 const char *get_filename(void)
64 if (option_info)
65 return base_file;
66 if (option_full_path)
67 return full_filename;
68 return filename;
71 const char *get_base_file(void)
73 return base_file;
76 static void set_position(struct position pos)
78 int len;
79 static int prev_stream = -1;
81 if (pos.stream == 0 && pos.line == 0)
82 return;
84 __smatch_lineno = pos.line;
86 if (pos.stream == prev_stream)
87 return;
89 filename = stream_name(pos.stream);
91 free(full_filename);
92 pathname = getcwd(NULL, 0);
93 if (pathname) {
94 len = strlen(pathname) + 1 + strlen(filename) + 1;
95 full_filename = malloc(len);
96 snprintf(full_filename, len, "%s/%s", pathname, filename);
97 } else {
98 full_filename = alloc_string(filename);
100 free(pathname);
103 static int is_inline_func(struct expression *expr)
105 if (expr->type != EXPR_SYMBOL || !expr->symbol)
106 return 0;
107 if (expr->symbol->ctype.modifiers & MOD_INLINE)
108 return 1;
109 return 0;
112 static int is_noreturn_func(struct expression *expr)
114 if (expr->type != EXPR_SYMBOL || !expr->symbol)
115 return 0;
116 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
117 return 1;
118 return 0;
121 int inlinable(struct expression *expr)
123 struct symbol *sym;
125 if (__inline_fn) /* don't nest */
126 return 0;
128 if (expr->type != EXPR_SYMBOL || !expr->symbol)
129 return 0;
130 sym = get_base_type(expr->symbol);
131 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
132 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
133 return 1;
134 return 0;
136 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
137 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
138 return 1;
139 return 0;
141 return 0;
144 void __process_post_op_stack(void)
146 struct expression *expr;
148 FOR_EACH_PTR(post_op_stack, expr) {
149 __pass_to_client(expr, OP_HOOK);
150 } END_FOR_EACH_PTR(expr);
152 __free_ptr_list((struct ptr_list **)&post_op_stack);
155 void __split_expr(struct expression *expr)
157 if (!expr)
158 return;
160 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
162 push_expression(&big_expression_stack, expr);
163 set_position(expr->pos);
164 __pass_to_client(expr, EXPR_HOOK);
166 switch (expr->type) {
167 case EXPR_PREOP:
168 if (expr->op == '*')
169 __pass_to_client(expr, DEREF_HOOK);
170 __split_expr(expr->unop);
171 __pass_to_client(expr, OP_HOOK);
172 break;
173 case EXPR_POSTOP:
174 __split_expr(expr->unop);
175 push_expression(&post_op_stack, expr);
176 break;
177 case EXPR_STATEMENT:
178 __expr_stmt_count++;
179 __split_stmt(expr->statement);
180 __expr_stmt_count--;
181 break;
182 case EXPR_LOGICAL:
183 case EXPR_COMPARE:
184 __pass_to_client(expr, LOGIC_HOOK);
185 __handle_logic(expr);
186 break;
187 case EXPR_BINOP:
188 __pass_to_client(expr, BINOP_HOOK);
189 case EXPR_COMMA:
190 __split_expr(expr->left);
191 __process_post_op_stack();
192 __split_expr(expr->right);
193 break;
194 case EXPR_ASSIGNMENT: {
195 struct expression *tmp;
197 if (!expr->right)
198 break;
200 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
202 /* foo = !bar() */
203 if (__handle_condition_assigns(expr))
204 break;
205 /* foo = (x < 5 ? foo : 5); */
206 if (__handle_select_assigns(expr))
207 break;
208 /* foo = ({frob(); frob(); frob(); 1;}) */
209 if (__handle_expr_statement_assigns(expr))
210 break;
212 __split_expr(expr->right);
213 if (outside_of_function())
214 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
215 else
216 __pass_to_client(expr, ASSIGNMENT_HOOK);
217 tmp = strip_expr(expr->right);
218 if (tmp->type == EXPR_CALL)
219 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
220 if (get_macro_name(tmp->pos) &&
221 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
222 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
223 __split_expr(expr->left);
224 break;
226 case EXPR_DEREF:
227 __pass_to_client(expr, DEREF_HOOK);
228 __split_expr(expr->deref);
229 break;
230 case EXPR_SLICE:
231 __split_expr(expr->base);
232 break;
233 case EXPR_CAST:
234 case EXPR_FORCE_CAST:
235 __pass_to_client(expr, CAST_HOOK);
236 __split_expr(expr->cast_expression);
237 break;
238 case EXPR_SIZEOF:
239 if (expr->cast_expression)
240 __pass_to_client(strip_parens(expr->cast_expression),
241 SIZEOF_HOOK);
242 break;
243 case EXPR_OFFSETOF:
244 case EXPR_ALIGNOF:
245 evaluate_expression(expr);
246 break;
247 case EXPR_CONDITIONAL:
248 case EXPR_SELECT:
249 if (known_condition_true(expr->conditional)) {
250 __split_expr(expr->cond_true);
251 break;
253 if (known_condition_false(expr->conditional)) {
254 __split_expr(expr->cond_false);
255 break;
257 __pass_to_client(expr, SELECT_HOOK);
258 __split_whole_condition(expr->conditional);
259 __split_expr(expr->cond_true);
260 __push_true_states();
261 __use_false_states();
262 __split_expr(expr->cond_false);
263 __merge_true_states();
264 break;
265 case EXPR_CALL:
266 if (sym_name_is("__builtin_constant_p", expr->fn))
267 break;
268 split_expr_list(expr->args);
269 __split_expr(expr->fn);
270 if (is_inline_func(expr->fn))
271 add_inline_function(expr->fn->symbol);
272 if (inlinable(expr->fn))
273 __inline_call = 1;
274 __process_post_op_stack();
275 __pass_to_client(expr, FUNCTION_CALL_HOOK);
276 __inline_call = 0;
277 if (inlinable(expr->fn)) {
278 parse_inline(expr);
280 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
281 if (is_noreturn_func(expr->fn))
282 nullify_path();
283 break;
284 case EXPR_INITIALIZER:
285 split_expr_list(expr->expr_list);
286 break;
287 case EXPR_IDENTIFIER:
288 __split_expr(expr->ident_expression);
289 break;
290 case EXPR_INDEX:
291 __split_expr(expr->idx_expression);
292 break;
293 case EXPR_POS:
294 __split_expr(expr->init_expr);
295 break;
296 case EXPR_SYMBOL:
297 __pass_to_client(expr, SYM_HOOK);
298 break;
299 case EXPR_STRING:
300 __pass_to_client(expr, STRING_HOOK);
301 break;
302 default:
303 break;
305 pop_expression(&big_expression_stack);
308 static int is_forever_loop(struct statement *stmt)
310 struct expression *expr;
312 expr = strip_expr(stmt->iterator_pre_condition);
313 if (!expr)
314 expr = stmt->iterator_post_condition;
315 if (!expr) {
316 /* this is a for(;;) loop... */
317 return 1;
320 if (expr->type == EXPR_VALUE && expr->value == 1)
321 return 1;
323 return 0;
326 static int loop_num;
327 static char *get_loop_name(int num)
329 char buf[256];
331 snprintf(buf, 255, "-loop%d", num);
332 buf[255] = '\0';
333 return alloc_sname(buf);
337 * Pre Loops are while and for loops.
339 static void handle_pre_loop(struct statement *stmt)
341 int once_through; /* we go through the loop at least once */
342 struct sm_state *extra_sm = NULL;
343 int unchanged = 0;
344 char *loop_name;
345 struct state_list *slist = NULL;
346 struct sm_state *sm = NULL;
348 loop_name = get_loop_name(loop_num);
349 loop_num++;
351 __split_stmt(stmt->iterator_pre_statement);
353 once_through = implied_condition_true(stmt->iterator_pre_condition);
355 loop_count++;
356 __push_continues();
357 __push_breaks();
359 __merge_gotos(loop_name);
361 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
362 __in_pre_condition++;
363 __pass_to_client(stmt, PRELOOP_HOOK);
364 __split_whole_condition(stmt->iterator_pre_condition);
365 __in_pre_condition--;
366 FOR_EACH_PTR(slist, sm) {
367 set_state(sm->owner, sm->name, sm->sym, sm->state);
368 } END_FOR_EACH_PTR(sm);
369 free_slist(&slist);
370 if (extra_sm)
371 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
373 if (option_assume_loops)
374 once_through = 1;
376 __split_stmt(stmt->iterator_statement);
377 __warn_on_silly_pre_loops();
378 if (is_forever_loop(stmt)) {
379 struct state_list *slist;
381 __save_gotos(loop_name);
383 __push_fake_cur_slist();
384 __split_stmt(stmt->iterator_post_statement);
385 slist = __pop_fake_cur_slist();
387 __discard_continues();
388 __discard_false_states();
389 __use_breaks();
391 if (!__path_is_null())
392 __merge_slist_into_cur(slist);
393 free_slist(&slist);
394 } else {
395 __merge_continues();
396 unchanged = __iterator_unchanged(extra_sm);
397 __split_stmt(stmt->iterator_post_statement);
398 __save_gotos(loop_name);
399 __split_whole_condition(stmt->iterator_pre_condition);
400 nullify_path();
401 __merge_false_states();
402 if (once_through)
403 __discard_false_states();
404 else
405 __merge_false_states();
407 if (extra_sm && unchanged)
408 __extra_pre_loop_hook_after(extra_sm,
409 stmt->iterator_post_statement,
410 stmt->iterator_pre_condition);
411 __merge_breaks();
413 loop_count--;
417 * Post loops are do {} while();
419 static void handle_post_loop(struct statement *stmt)
421 char *loop_name;
423 loop_name = get_loop_name(loop_num);
424 loop_num++;
425 loop_count++;
427 __push_continues();
428 __push_breaks();
429 __merge_gotos(loop_name);
430 __split_stmt(stmt->iterator_statement);
431 __merge_continues();
432 if (!is_zero(stmt->iterator_post_condition))
433 __save_gotos(loop_name);
435 if (is_forever_loop(stmt)) {
436 __use_breaks();
437 } else {
438 __split_whole_condition(stmt->iterator_post_condition);
439 __use_false_states();
440 __merge_breaks();
442 loop_count--;
445 static int empty_statement(struct statement *stmt)
447 if (!stmt)
448 return 0;
449 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
450 return 1;
451 return 0;
454 static int last_stmt_on_same_line()
456 struct statement *stmt;
457 int i = 0;
459 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
460 if (!i++)
461 continue;
462 if (stmt->pos.line == get_lineno())
463 return 1;
464 return 0;
465 } END_FOR_EACH_PTR_REVERSE(stmt);
466 return 0;
469 static struct statement *last_stmt;
470 static int is_last_stmt(struct statement *stmt)
472 if (stmt == last_stmt)
473 return 1;
474 return 0;
477 static void print_unreached_initializers(struct symbol_list *sym_list)
479 struct symbol *sym;
481 FOR_EACH_PTR(sym_list, sym) {
482 if (sym->initializer)
483 sm_msg("info: '%s' is not actually initialized (unreached code).",
484 (sym->ident ? sym->ident->name : "this variable"));
485 } END_FOR_EACH_PTR(sym);
488 static void print_unreached(struct statement *stmt)
490 static int print = 1;
492 if (__inline_fn)
493 return;
495 if (!__path_is_null()) {
496 print = 1;
497 return;
499 if (!print)
500 return;
502 switch (stmt->type) {
503 case STMT_COMPOUND: /* after a switch before a case stmt */
504 case STMT_RANGE:
505 case STMT_CASE:
506 case STMT_LABEL:
507 return;
508 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
509 print_unreached_initializers(stmt->declaration);
510 return;
511 case STMT_RETURN: /* gcc complains if you don't have a return statement */
512 if (is_last_stmt(stmt))
513 return;
514 break;
515 case STMT_GOTO:
516 if (!option_spammy)
517 return;
518 break;
519 default:
520 break;
522 if (!option_spammy && empty_statement(stmt))
523 return;
524 sm_msg("info: ignoring unreachable code.");
525 print = 0;
528 static void split_asm_constraints(struct expression_list *expr_list)
530 struct expression *expr;
531 int state = 0;
533 FOR_EACH_PTR(expr_list, expr) {
534 switch (state) {
535 case 0: /* identifier */
536 case 1: /* constraint */
537 state++;
538 continue;
539 case 2: /* expression */
540 state = 0;
541 __split_expr(expr);
542 continue;
544 } END_FOR_EACH_PTR(expr);
547 static int is_case_val(struct statement *stmt, sval_t sval)
549 sval_t case_sval;
551 if (stmt->type != STMT_CASE)
552 return 0;
553 if (!stmt->case_expression) {
554 __set_default();
555 return 1;
557 if (!get_value(stmt->case_expression, &case_sval))
558 return 0;
559 if (case_sval.value == sval.value)
560 return 1;
561 return 0;
564 static void split_known_switch(struct statement *stmt, sval_t sval)
566 struct statement *tmp;
568 __split_expr(stmt->switch_expression);
570 push_expression(&switch_expr_stack, stmt->switch_expression);
571 __save_switch_states(top_expression(switch_expr_stack));
572 nullify_path();
573 __push_default();
574 __push_breaks();
576 stmt = stmt->switch_statement;
578 if (!last_stmt)
579 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
581 __push_scope_hooks();
582 FOR_EACH_PTR(stmt->stmts, tmp) {
583 __smatch_lineno = tmp->pos.line;
584 if (is_case_val(tmp, sval)) {
585 __merge_switches(top_expression(switch_expr_stack),
586 stmt->case_expression);
587 __pass_case_to_client(top_expression(switch_expr_stack),
588 stmt->case_expression);
590 if (__path_is_null())
591 continue;
592 __split_stmt(tmp);
593 if (__path_is_null()) {
594 __set_default();
595 goto out;
597 } END_FOR_EACH_PTR(tmp);
598 out:
599 __call_scope_hooks();
600 if (!__pop_default())
601 __merge_switches(top_expression(switch_expr_stack),
602 NULL);
603 __discard_switches();
604 __merge_breaks();
605 pop_expression(&switch_expr_stack);
608 void __split_stmt(struct statement *stmt)
610 sval_t sval;
612 if (!stmt)
613 goto out;
615 if (out_of_memory() || __bail_on_rest_of_function) {
616 static char *printed = NULL;
618 if (printed != cur_func)
619 sm_msg("Function too hairy. Giving up.");
620 final_pass = 0; /* turn off sm_msg() from here */
621 printed = cur_func;
622 return;
625 add_ptr_list(&big_statement_stack, stmt);
626 free_expression_stack(&big_expression_stack);
627 set_position(stmt->pos);
628 print_unreached(stmt);
629 __pass_to_client(stmt, STMT_HOOK);
631 switch (stmt->type) {
632 case STMT_DECLARATION:
633 split_declaration(stmt->declaration);
634 break;
635 case STMT_RETURN:
636 __split_expr(stmt->ret_value);
637 __pass_to_client(stmt->ret_value, RETURN_HOOK);
638 nullify_path();
639 break;
640 case STMT_EXPRESSION:
641 __split_expr(stmt->expression);
642 break;
643 case STMT_COMPOUND: {
644 struct statement *tmp;
646 if (!last_stmt)
647 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
648 __push_scope_hooks();
649 FOR_EACH_PTR(stmt->stmts, tmp) {
650 __split_stmt(tmp);
651 } END_FOR_EACH_PTR(tmp);
652 __call_scope_hooks();
653 break;
655 case STMT_IF:
656 if (known_condition_true(stmt->if_conditional)) {
657 __split_stmt(stmt->if_true);
658 break;
660 if (known_condition_false(stmt->if_conditional)) {
661 __split_stmt(stmt->if_false);
662 break;
664 if (option_known_conditions &&
665 implied_condition_true(stmt->if_conditional)) {
666 sm_info("this condition is true.");
667 __split_stmt(stmt->if_true);
668 break;
670 if (option_known_conditions &&
671 implied_condition_false(stmt->if_conditional)) {
672 sm_info("this condition is false.");
673 __split_stmt(stmt->if_false);
674 break;
676 __split_whole_condition(stmt->if_conditional);
677 __split_stmt(stmt->if_true);
678 if (empty_statement(stmt->if_true) &&
679 last_stmt_on_same_line() &&
680 !get_macro_name(stmt->if_true->pos))
681 sm_msg("warn: if();");
682 __push_true_states();
683 __use_false_states();
684 __split_stmt(stmt->if_false);
685 __merge_true_states();
686 break;
687 case STMT_ITERATOR:
688 if (stmt->iterator_pre_condition)
689 handle_pre_loop(stmt);
690 else if (stmt->iterator_post_condition)
691 handle_post_loop(stmt);
692 else {
693 // these are for(;;) type loops.
694 handle_pre_loop(stmt);
696 break;
697 case STMT_SWITCH:
698 if (get_value(stmt->switch_expression, &sval)) {
699 split_known_switch(stmt, sval);
700 break;
702 __split_expr(stmt->switch_expression);
703 push_expression(&switch_expr_stack, stmt->switch_expression);
704 __save_switch_states(top_expression(switch_expr_stack));
705 nullify_path();
706 __push_default();
707 __push_breaks();
708 __split_stmt(stmt->switch_statement);
709 if (!__pop_default())
710 __merge_switches(top_expression(switch_expr_stack),
711 NULL);
712 __discard_switches();
713 __merge_breaks();
714 pop_expression(&switch_expr_stack);
715 break;
716 case STMT_CASE:
717 __merge_switches(top_expression(switch_expr_stack),
718 stmt->case_expression);
719 __pass_case_to_client(top_expression(switch_expr_stack),
720 stmt->case_expression);
721 if (!stmt->case_expression)
722 __set_default();
723 __split_expr(stmt->case_expression);
724 __split_expr(stmt->case_to);
725 __split_stmt(stmt->case_statement);
726 break;
727 case STMT_LABEL:
728 if (stmt->label_identifier &&
729 stmt->label_identifier->type == SYM_LABEL &&
730 stmt->label_identifier->ident) {
731 loop_count = 1000000;
732 __merge_gotos(stmt->label_identifier->ident->name);
734 __split_stmt(stmt->label_statement);
735 break;
736 case STMT_GOTO:
737 __split_expr(stmt->goto_expression);
738 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
739 if (!strcmp(stmt->goto_label->ident->name, "break")) {
740 __process_breaks();
741 } else if (!strcmp(stmt->goto_label->ident->name,
742 "continue")) {
743 __process_continues();
745 } else if (stmt->goto_label &&
746 stmt->goto_label->type == SYM_LABEL &&
747 stmt->goto_label->ident) {
748 __save_gotos(stmt->goto_label->ident->name);
750 nullify_path();
751 break;
752 case STMT_NONE:
753 break;
754 case STMT_ASM:
755 __pass_to_client(stmt, ASM_HOOK);
756 __split_expr(stmt->asm_string);
757 split_asm_constraints(stmt->asm_outputs);
758 split_asm_constraints(stmt->asm_inputs);
759 split_asm_constraints(stmt->asm_clobbers);
760 break;
761 case STMT_CONTEXT:
762 break;
763 case STMT_RANGE:
764 __split_expr(stmt->range_expression);
765 __split_expr(stmt->range_low);
766 __split_expr(stmt->range_high);
767 break;
769 out:
770 __process_post_op_stack();
773 static void split_expr_list(struct expression_list *expr_list)
775 struct expression *expr;
777 FOR_EACH_PTR(expr_list, expr) {
778 __split_expr(expr);
779 __process_post_op_stack();
780 } END_FOR_EACH_PTR(expr);
783 static void split_sym(struct symbol *sym)
785 if (!sym)
786 return;
787 if (!(sym->namespace & NS_SYMBOL))
788 return;
790 __split_stmt(sym->stmt);
791 __split_expr(sym->array_size);
792 split_symlist(sym->arguments);
793 split_symlist(sym->symbol_list);
794 __split_stmt(sym->inline_stmt);
795 split_symlist(sym->inline_symbol_list);
798 static void split_symlist(struct symbol_list *sym_list)
800 struct symbol *sym;
802 FOR_EACH_PTR(sym_list, sym) {
803 split_sym(sym);
804 } END_FOR_EACH_PTR(sym);
807 typedef void (fake_cb)(struct expression *expr);
809 static int member_to_number(struct expression *expr, struct ident *member)
811 struct symbol *type, *tmp;
812 char *name;
813 int i;
815 if (!member)
816 return -1;
817 name = member->name;
819 type = get_type(expr);
820 if (!type || type->type != SYM_STRUCT)
821 return -1;
823 i = -1;
824 FOR_EACH_PTR(type->symbol_list, tmp) {
825 i++;
826 if (!tmp->ident)
827 continue;
828 if (strcmp(name, tmp->ident->name) == 0)
829 return i;
830 } END_FOR_EACH_PTR(tmp);
831 return -1;
834 static struct ident *number_to_member(struct expression *expr, int num)
836 struct symbol *type, *member;
837 int i = 0;
839 type = get_type(expr);
840 if (!type || type->type != SYM_STRUCT)
841 return NULL;
843 FOR_EACH_PTR(type->symbol_list, member) {
844 if (i == num)
845 return member->ident;
846 i++;
847 } END_FOR_EACH_PTR(member);
848 return NULL;
851 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
853 static void set_members_to_zero(struct expression *symbol)
855 struct expression *deref, *assign;
856 struct symbol *type;
857 struct symbol *member, *member_type;
859 type = get_type(symbol);
860 if (!type)
861 return;
863 FOR_EACH_PTR(type->symbol_list, member) {
864 if (!member->ident)
865 continue;
866 member_type = get_real_base_type(member);
867 if (!member_type || member_type->type == SYM_ARRAY)
868 continue;
869 deref = member_expression(symbol, '.', member->ident);
870 assign = assign_expression(deref, zero_expr());
871 __split_expr(assign);
872 } END_FOR_EACH_PTR(member);
876 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
878 struct expression *deref, *assign, *tmp;
879 struct symbol *type;
880 struct ident *member;
881 int member_idx = 0;
883 type = get_type(symbol);
884 if (!type || (type->type != SYM_STRUCT && type->type != SYM_UNION))
885 return;
887 set_members_to_zero(symbol);
889 FOR_EACH_PTR(members, tmp) {
890 member = number_to_member(symbol, member_idx);
891 while (tmp->type == EXPR_IDENTIFIER) {
892 member = tmp->expr_ident;
893 member_idx = member_to_number(symbol, member);
894 tmp = tmp->ident_expression;
896 member_idx++;
897 deref = member_expression(symbol, '.', member);
898 if (tmp->type == EXPR_INITIALIZER) {
899 type = get_type(deref);
900 if (type && type->type == SYM_ARRAY)
901 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
902 else
903 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
904 } else {
905 assign = assign_expression(deref, tmp);
906 fake_cb(assign);
908 } END_FOR_EACH_PTR(tmp);
911 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
913 fake_member_assigns_helper(symbol_expression(sym),
914 sym->initializer->expr_list, fake_cb);
917 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
919 struct expression *offset, *binop, *assign, *tmp;
920 struct symbol *type;
921 int idx;
923 idx = 0;
924 FOR_EACH_PTR(expr_list, tmp) {
925 if (tmp->type == EXPR_INDEX) {
926 if (tmp->idx_from != tmp->idx_to)
927 return;
928 idx = tmp->idx_from;
929 if (!tmp->idx_expression)
930 goto next;
931 tmp = tmp->idx_expression;
933 offset = value_expr(idx);
934 binop = array_element_expression(array, offset);
935 if (tmp->type == EXPR_INITIALIZER) {
936 type = get_type(binop);
937 if (type && type->type == SYM_ARRAY)
938 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
939 else
940 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
941 } else {
942 assign = assign_expression(binop, tmp);
943 fake_cb(assign);
945 next:
946 idx++;
947 } END_FOR_EACH_PTR(tmp);
950 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
952 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
955 static void fake_assign_expr(struct symbol *sym)
957 struct expression *assign, *symbol;
959 symbol = symbol_expression(sym);
960 assign = assign_expression(symbol, sym->initializer);
961 __split_expr(assign);
964 static void call_split_expr(struct expression *expr)
966 __split_expr(expr);
969 static void do_initializer_stuff(struct symbol *sym)
971 if (!sym->initializer)
972 return;
974 if (sym->initializer->type == EXPR_INITIALIZER) {
975 if (get_real_base_type(sym)->type == SYM_ARRAY)
976 fake_element_assigns(sym, call_split_expr);
977 else
978 fake_member_assigns(sym, call_split_expr);
979 } else {
980 fake_assign_expr(sym);
984 static void split_declaration(struct symbol_list *sym_list)
986 struct symbol *sym;
988 FOR_EACH_PTR(sym_list, sym) {
989 __pass_to_client(sym, DECLARATION_HOOK);
990 do_initializer_stuff(sym);
991 split_sym(sym);
992 } END_FOR_EACH_PTR(sym);
995 static void call_global_assign_hooks(struct expression *assign)
997 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1000 static void fake_global_assign(struct symbol *sym)
1002 struct expression *assign, *symbol;
1004 if (!sym->initializer)
1005 return;
1006 if (sym->initializer->type == EXPR_INITIALIZER) {
1007 if (get_real_base_type(sym)->type == SYM_ARRAY)
1008 fake_element_assigns(sym, call_global_assign_hooks);
1009 else
1010 fake_member_assigns(sym, call_global_assign_hooks);
1011 } else {
1012 symbol = symbol_expression(sym);
1013 assign = assign_expression(symbol, sym->initializer);
1014 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1018 static void split_function(struct symbol *sym)
1020 struct symbol *base_type = get_base_type(sym);
1022 cur_func_sym = sym;
1023 if (sym->ident)
1024 cur_func = sym->ident->name;
1025 __smatch_lineno = sym->pos.line;
1026 last_stmt = NULL;
1027 loop_count = 0;
1028 sm_debug("new function: %s\n", cur_func);
1029 __slist_id = 0;
1030 if (option_two_passes) {
1031 __unnullify_path();
1032 loop_num = 0;
1033 final_pass = 0;
1034 __pass_to_client(sym, FUNC_DEF_HOOK);
1035 __pass_to_client(sym, AFTER_DEF_HOOK);
1036 __split_stmt(base_type->stmt);
1037 __split_stmt(base_type->inline_stmt);
1038 nullify_path();
1040 __unnullify_path();
1041 loop_num = 0;
1042 final_pass = 1;
1043 __pass_to_client(sym, FUNC_DEF_HOOK);
1044 __pass_to_client(sym, AFTER_DEF_HOOK);
1045 __split_stmt(base_type->stmt);
1046 __split_stmt(base_type->inline_stmt);
1047 __pass_to_client(sym, END_FUNC_HOOK);
1048 __pass_to_client(sym, AFTER_FUNC_HOOK);
1049 cur_func_sym = NULL;
1050 cur_func = NULL;
1051 clear_all_states();
1052 free_data_info_allocs();
1053 free_expression_stack(&switch_expr_stack);
1054 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1055 __bail_on_rest_of_function = 0;
1058 static void parse_inline(struct expression *call)
1060 struct symbol *base_type;
1061 int loop_num_bak = loop_num;
1062 int final_pass_bak = final_pass;
1063 char *cur_func_bak = cur_func;
1064 struct statement_list *big_statement_stack_bak = big_statement_stack;
1065 struct expression_list *big_expression_stack_bak = big_expression_stack;
1066 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1067 struct symbol *cur_func_sym_bak = cur_func_sym;
1069 __pass_to_client(call, INLINE_FN_START);
1070 final_pass = 0; /* don't print anything */
1071 __inline_fn = call;
1073 base_type = get_base_type(call->fn->symbol);
1074 cur_func_sym = call->fn->symbol;
1075 if (call->fn->symbol->ident)
1076 cur_func = call->fn->symbol->ident->name;
1077 else
1078 cur_func = NULL;
1079 set_position(call->fn->symbol->pos);
1081 save_all_states();
1082 nullify_all_states();
1083 big_statement_stack = NULL;
1084 big_expression_stack = NULL;
1085 switch_expr_stack = NULL;
1087 sm_debug("inline function: %s\n", cur_func);
1088 __unnullify_path();
1089 loop_num = 0;
1090 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
1091 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
1092 __split_stmt(base_type->stmt);
1093 __split_stmt(base_type->inline_stmt);
1094 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1095 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1097 free_expression_stack(&switch_expr_stack);
1098 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1099 nullify_path();
1101 loop_num = loop_num_bak;
1102 final_pass = final_pass_bak;
1103 cur_func_sym = cur_func_sym_bak;
1104 cur_func = cur_func_bak;
1105 big_statement_stack = big_statement_stack_bak;
1106 big_expression_stack = big_expression_stack_bak;
1107 switch_expr_stack = switch_expr_stack_bak;
1109 restore_all_states();
1110 set_position(call->pos);
1111 __inline_fn = NULL;
1112 __pass_to_client(call, INLINE_FN_END);
1115 static struct symbol_list *inlines_called;
1116 static void add_inline_function(struct symbol *sym)
1118 static struct symbol_list *already_added;
1119 struct symbol *tmp;
1121 FOR_EACH_PTR(already_added, tmp) {
1122 if (tmp == sym)
1123 return;
1124 } END_FOR_EACH_PTR(tmp);
1126 add_ptr_list(&already_added, sym);
1127 add_ptr_list(&inlines_called, sym);
1130 static void process_inlines()
1132 struct symbol *tmp;
1134 FOR_EACH_PTR(inlines_called, tmp) {
1135 split_function(tmp);
1136 } END_FOR_EACH_PTR(tmp);
1137 free_ptr_list(&inlines_called);
1140 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1142 struct symbol *sym;
1144 FOR_EACH_PTR_REVERSE(big_list, sym) {
1145 if (!sym->scope)
1146 continue;
1147 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1148 return sym;
1149 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1150 return sym;
1151 } END_FOR_EACH_PTR_REVERSE(sym);
1153 return NULL;
1156 static void split_inlines_in_scope(struct symbol *sym)
1158 struct symbol *base;
1159 struct symbol_list *scope_list;
1160 int stream;
1162 scope_list = sym->scope->symbols;
1163 stream = sym->pos.stream;
1165 /* find the last static symbol in the file */
1166 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1167 if (sym->pos.stream != stream)
1168 continue;
1169 if (sym->type != SYM_NODE)
1170 continue;
1171 base = get_base_type(sym);
1172 if (!base)
1173 continue;
1174 if (base->type != SYM_FN)
1175 continue;
1176 if (!base->inline_stmt)
1177 continue;
1178 add_inline_function(sym);
1179 } END_FOR_EACH_PTR_REVERSE(sym);
1181 process_inlines();
1184 static void split_inlines(struct symbol_list *sym_list)
1186 struct symbol *sym;
1188 sym = get_last_scoped_symbol(sym_list, 0);
1189 if (sym)
1190 split_inlines_in_scope(sym);
1191 sym = get_last_scoped_symbol(sym_list, 1);
1192 if (sym)
1193 split_inlines_in_scope(sym);
1196 static void split_functions(struct symbol_list *sym_list)
1198 struct symbol *sym;
1200 FOR_EACH_PTR(sym_list, sym) {
1201 set_position(sym->pos);
1202 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1203 split_function(sym);
1204 process_inlines();
1205 } else {
1206 __pass_to_client(sym, BASE_HOOK);
1207 fake_global_assign(sym);
1209 } END_FOR_EACH_PTR(sym);
1210 split_inlines(sym_list);
1211 __pass_to_client(sym_list, END_FILE_HOOK);
1214 void smatch(int argc, char **argv)
1217 struct string_list *filelist = NULL;
1218 struct symbol_list *sym_list;
1220 if (argc < 2) {
1221 printf("Usage: smatch [--debug] <filename.c>\n");
1222 exit(1);
1224 sparse_initialize(argc, argv, &filelist);
1225 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1226 if (option_file_output) {
1227 char buf[256];
1229 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1230 sm_outfd = fopen(buf, "w");
1231 if (!sm_outfd) {
1232 printf("Error: Cannot open %s\n", base_file);
1233 exit(1);
1236 sym_list = sparse_keep_tokens(base_file);
1237 split_functions(sym_list);
1238 } END_FOR_EACH_PTR_NOTAG(base_file);