flow: expressions: don't try updating to uninitialized position
[smatch.git] / smatch_flow.c
blob10c1c766f2792cee03bf9d6bf31b480b7b1a7ee2
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 const char *get_filename(void)
59 if (option_info)
60 return base_file;
61 if (option_full_path)
62 return full_filename;
63 return filename;
66 const char *get_base_file(void)
68 return base_file;
71 static void set_position(struct position pos)
73 int len;
74 static int prev_stream = -1;
76 if (pos.stream == 0 && pos.line == 0)
77 return;
79 __smatch_lineno = pos.line;
81 if (pos.stream == prev_stream)
82 return;
84 filename = stream_name(pos.stream);
86 free(full_filename);
87 pathname = getcwd(NULL, 0);
88 if (pathname) {
89 len = strlen(pathname) + 1 + strlen(filename) + 1;
90 full_filename = malloc(len);
91 snprintf(full_filename, len, "%s/%s", pathname, filename);
92 } else {
93 full_filename = alloc_string(filename);
95 free(pathname);
98 static int is_inline_func(struct expression *expr)
100 if (expr->type != EXPR_SYMBOL || !expr->symbol)
101 return 0;
102 if (expr->symbol->ctype.modifiers & MOD_INLINE)
103 return 1;
104 return 0;
107 static int is_noreturn_func(struct expression *expr)
109 if (expr->type != EXPR_SYMBOL || !expr->symbol)
110 return 0;
111 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
112 return 1;
113 return 0;
116 int inlinable(struct expression *expr)
118 struct symbol *sym;
120 if (__inline_fn) /* don't nest */
121 return 0;
123 if (expr->type != EXPR_SYMBOL || !expr->symbol)
124 return 0;
125 sym = get_base_type(expr->symbol);
126 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
127 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
128 return 1;
129 return 0;
131 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
132 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
133 return 1;
134 return 0;
136 return 0;
139 void __process_post_op_stack(void)
141 struct expression *expr;
143 FOR_EACH_PTR(post_op_stack, expr) {
144 __pass_to_client(expr, OP_HOOK);
145 } END_FOR_EACH_PTR(expr);
147 __free_ptr_list((struct ptr_list **)&post_op_stack);
150 void __split_expr(struct expression *expr)
152 if (!expr)
153 return;
155 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
157 push_expression(&big_expression_stack, expr);
158 set_position(expr->pos);
159 __pass_to_client(expr, EXPR_HOOK);
161 switch (expr->type) {
162 case EXPR_PREOP:
163 if (expr->op == '*')
164 __pass_to_client(expr, DEREF_HOOK);
165 __split_expr(expr->unop);
166 __pass_to_client(expr, OP_HOOK);
167 break;
168 case EXPR_POSTOP:
169 __split_expr(expr->unop);
170 push_expression(&post_op_stack, expr);
171 break;
172 case EXPR_STATEMENT:
173 __expr_stmt_count++;
174 __split_stmt(expr->statement);
175 __expr_stmt_count--;
176 break;
177 case EXPR_LOGICAL:
178 case EXPR_COMPARE:
179 __pass_to_client(expr, LOGIC_HOOK);
180 __handle_logic(expr);
181 break;
182 case EXPR_BINOP:
183 __pass_to_client(expr, BINOP_HOOK);
184 case EXPR_COMMA:
185 __split_expr(expr->left);
186 __process_post_op_stack();
187 __split_expr(expr->right);
188 break;
189 case EXPR_ASSIGNMENT: {
190 struct expression *tmp;
192 if (!expr->right)
193 break;
195 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
197 /* foo = !bar() */
198 if (__handle_condition_assigns(expr))
199 break;
200 /* foo = (x < 5 ? foo : 5); */
201 if (__handle_select_assigns(expr))
202 break;
203 /* foo = ({frob(); frob(); frob(); 1;}) */
204 if (__handle_expr_statement_assigns(expr))
205 break;
207 __split_expr(expr->right);
208 __pass_to_client(expr, ASSIGNMENT_HOOK);
209 tmp = strip_expr(expr->right);
210 if (tmp->type == EXPR_CALL)
211 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
212 if (get_macro_name(tmp->pos) &&
213 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
214 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
215 __split_expr(expr->left);
216 break;
218 case EXPR_DEREF:
219 __pass_to_client(expr, DEREF_HOOK);
220 __split_expr(expr->deref);
221 break;
222 case EXPR_SLICE:
223 __split_expr(expr->base);
224 break;
225 case EXPR_CAST:
226 case EXPR_FORCE_CAST:
227 __pass_to_client(expr, CAST_HOOK);
228 __split_expr(expr->cast_expression);
229 break;
230 case EXPR_SIZEOF:
231 if (expr->cast_expression)
232 __pass_to_client(strip_parens(expr->cast_expression),
233 SIZEOF_HOOK);
234 break;
235 case EXPR_OFFSETOF:
236 case EXPR_ALIGNOF:
237 evaluate_expression(expr);
238 break;
239 case EXPR_CONDITIONAL:
240 case EXPR_SELECT:
241 if (known_condition_true(expr->conditional)) {
242 __split_expr(expr->cond_true);
243 break;
245 if (known_condition_false(expr->conditional)) {
246 __split_expr(expr->cond_false);
247 break;
249 __pass_to_client(expr, SELECT_HOOK);
250 __split_whole_condition(expr->conditional);
251 __split_expr(expr->cond_true);
252 __push_true_states();
253 __use_false_states();
254 __split_expr(expr->cond_false);
255 __merge_true_states();
256 break;
257 case EXPR_CALL:
258 if (sym_name_is("__builtin_constant_p", expr->fn))
259 break;
260 split_expr_list(expr->args);
261 __split_expr(expr->fn);
262 if (is_inline_func(expr->fn))
263 add_inline_function(expr->fn->symbol);
264 if (inlinable(expr->fn))
265 __inline_call = 1;
266 __process_post_op_stack();
267 __pass_to_client(expr, FUNCTION_CALL_HOOK);
268 __inline_call = 0;
269 if (inlinable(expr->fn)) {
270 parse_inline(expr);
272 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
273 if (is_noreturn_func(expr->fn))
274 nullify_path();
275 break;
276 case EXPR_INITIALIZER:
277 split_expr_list(expr->expr_list);
278 break;
279 case EXPR_IDENTIFIER:
280 __split_expr(expr->ident_expression);
281 break;
282 case EXPR_INDEX:
283 __split_expr(expr->idx_expression);
284 break;
285 case EXPR_POS:
286 __split_expr(expr->init_expr);
287 break;
288 case EXPR_SYMBOL:
289 __pass_to_client(expr, SYM_HOOK);
290 break;
291 case EXPR_STRING:
292 __pass_to_client(expr, STRING_HOOK);
293 break;
294 default:
295 break;
297 pop_expression(&big_expression_stack);
300 static int is_forever_loop(struct statement *stmt)
302 struct expression *expr;
304 expr = strip_expr(stmt->iterator_pre_condition);
305 if (!expr)
306 expr = stmt->iterator_post_condition;
307 if (!expr) {
308 /* this is a for(;;) loop... */
309 return 1;
312 if (expr->type == EXPR_VALUE && expr->value == 1)
313 return 1;
315 return 0;
318 static int loop_num;
319 static char *get_loop_name(int num)
321 char buf[256];
323 snprintf(buf, 255, "-loop%d", num);
324 buf[255] = '\0';
325 return alloc_sname(buf);
329 * Pre Loops are while and for loops.
331 static void handle_pre_loop(struct statement *stmt)
333 int once_through; /* we go through the loop at least once */
334 struct sm_state *extra_sm = NULL;
335 int unchanged = 0;
336 char *loop_name;
337 struct state_list *slist = NULL;
338 struct sm_state *sm = NULL;
340 loop_name = get_loop_name(loop_num);
341 loop_num++;
343 __split_stmt(stmt->iterator_pre_statement);
345 once_through = implied_condition_true(stmt->iterator_pre_condition);
347 loop_count++;
348 __push_continues();
349 __push_breaks();
351 __merge_gotos(loop_name);
353 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
354 __in_pre_condition++;
355 __pass_to_client(stmt, PRELOOP_HOOK);
356 __split_whole_condition(stmt->iterator_pre_condition);
357 __in_pre_condition--;
358 FOR_EACH_PTR(slist, sm) {
359 set_state(sm->owner, sm->name, sm->sym, sm->state);
360 } END_FOR_EACH_PTR(sm);
361 free_slist(&slist);
362 if (extra_sm)
363 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
365 if (option_assume_loops)
366 once_through = 1;
368 __split_stmt(stmt->iterator_statement);
369 __warn_on_silly_pre_loops();
370 if (is_forever_loop(stmt)) {
371 struct state_list *slist;
373 __save_gotos(loop_name);
375 __push_fake_cur_slist();
376 __split_stmt(stmt->iterator_post_statement);
377 slist = __pop_fake_cur_slist();
379 __discard_continues();
380 __discard_false_states();
381 __use_breaks();
383 if (!__path_is_null())
384 __merge_slist_into_cur(slist);
385 free_slist(&slist);
386 } else {
387 __merge_continues();
388 unchanged = __iterator_unchanged(extra_sm);
389 __split_stmt(stmt->iterator_post_statement);
390 __save_gotos(loop_name);
391 __split_whole_condition(stmt->iterator_pre_condition);
392 nullify_path();
393 __merge_false_states();
394 if (once_through)
395 __discard_false_states();
396 else
397 __merge_false_states();
399 if (extra_sm && unchanged)
400 __extra_pre_loop_hook_after(extra_sm,
401 stmt->iterator_post_statement,
402 stmt->iterator_pre_condition);
403 __merge_breaks();
405 loop_count--;
409 * Post loops are do {} while();
411 static void handle_post_loop(struct statement *stmt)
413 char *loop_name;
415 loop_name = get_loop_name(loop_num);
416 loop_num++;
417 loop_count++;
419 __push_continues();
420 __push_breaks();
421 __merge_gotos(loop_name);
422 __split_stmt(stmt->iterator_statement);
423 __merge_continues();
424 if (!is_zero(stmt->iterator_post_condition))
425 __save_gotos(loop_name);
427 if (is_forever_loop(stmt)) {
428 __use_breaks();
429 } else {
430 __split_whole_condition(stmt->iterator_post_condition);
431 __use_false_states();
432 __merge_breaks();
434 loop_count--;
437 static int empty_statement(struct statement *stmt)
439 if (!stmt)
440 return 0;
441 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
442 return 1;
443 return 0;
446 static int last_stmt_on_same_line()
448 struct statement *stmt;
449 int i = 0;
451 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
452 if (!i++)
453 continue;
454 if (stmt->pos.line == get_lineno())
455 return 1;
456 return 0;
457 } END_FOR_EACH_PTR_REVERSE(stmt);
458 return 0;
461 static struct statement *last_stmt;
462 static int is_last_stmt(struct statement *stmt)
464 if (stmt == last_stmt)
465 return 1;
466 return 0;
469 static void print_unreached_initializers(struct symbol_list *sym_list)
471 struct symbol *sym;
473 FOR_EACH_PTR(sym_list, sym) {
474 if (sym->initializer)
475 sm_msg("info: '%s' is not actually initialized (unreached code).",
476 (sym->ident ? sym->ident->name : "this variable"));
477 } END_FOR_EACH_PTR(sym);
480 static void print_unreached(struct statement *stmt)
482 static int print = 1;
484 if (!__path_is_null()) {
485 print = 1;
486 return;
488 if (!print)
489 return;
491 switch (stmt->type) {
492 case STMT_COMPOUND: /* after a switch before a case stmt */
493 case STMT_RANGE:
494 case STMT_CASE:
495 case STMT_LABEL:
496 return;
497 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
498 print_unreached_initializers(stmt->declaration);
499 return;
500 case STMT_RETURN: /* gcc complains if you don't have a return statement */
501 if (is_last_stmt(stmt))
502 return;
503 break;
504 case STMT_GOTO:
505 if (!option_spammy)
506 return;
507 break;
508 default:
509 break;
511 if (!option_spammy && empty_statement(stmt))
512 return;
513 sm_msg("info: ignoring unreachable code.");
514 print = 0;
517 static void split_asm_constraints(struct expression_list *expr_list)
519 struct expression *expr;
520 int state = 0;
522 FOR_EACH_PTR(expr_list, expr) {
523 switch (state) {
524 case 0: /* identifier */
525 case 1: /* constraint */
526 state++;
527 continue;
528 case 2: /* expression */
529 state = 0;
530 __split_expr(expr);
531 continue;
533 } END_FOR_EACH_PTR(expr);
536 static int is_case_val(struct statement *stmt, sval_t sval)
538 sval_t case_sval;
540 if (stmt->type != STMT_CASE)
541 return 0;
542 if (!stmt->case_expression) {
543 __set_default();
544 return 1;
546 if (!get_value(stmt->case_expression, &case_sval))
547 return 0;
548 if (case_sval.value == sval.value)
549 return 1;
550 return 0;
553 static void split_known_switch(struct statement *stmt, sval_t sval)
555 struct statement *tmp;
557 __split_expr(stmt->switch_expression);
559 push_expression(&switch_expr_stack, stmt->switch_expression);
560 __save_switch_states(top_expression(switch_expr_stack));
561 nullify_path();
562 __push_default();
563 __push_breaks();
565 stmt = stmt->switch_statement;
567 if (!last_stmt)
568 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
570 __push_scope_hooks();
571 FOR_EACH_PTR(stmt->stmts, tmp) {
572 __smatch_lineno = tmp->pos.line;
573 if (is_case_val(tmp, sval)) {
574 __merge_switches(top_expression(switch_expr_stack),
575 stmt->case_expression);
576 __pass_case_to_client(top_expression(switch_expr_stack),
577 stmt->case_expression);
579 if (__path_is_null())
580 continue;
581 __split_stmt(tmp);
582 if (__path_is_null()) {
583 __set_default();
584 goto out;
586 } END_FOR_EACH_PTR(tmp);
587 out:
588 __call_scope_hooks();
589 if (!__pop_default())
590 __merge_switches(top_expression(switch_expr_stack),
591 NULL);
592 __discard_switches();
593 __merge_breaks();
594 pop_expression(&switch_expr_stack);
597 void __split_stmt(struct statement *stmt)
599 sval_t sval;
601 if (!stmt)
602 goto out;
604 if (out_of_memory() || __bail_on_rest_of_function) {
605 static char *printed = NULL;
607 if (printed != cur_func)
608 sm_msg("Function too hairy. Giving up.");
609 final_pass = 0; /* turn off sm_msg() from here */
610 printed = cur_func;
611 return;
614 add_ptr_list(&big_statement_stack, stmt);
615 free_expression_stack(&big_expression_stack);
616 set_position(stmt->pos);
617 print_unreached(stmt);
618 __pass_to_client(stmt, STMT_HOOK);
620 switch (stmt->type) {
621 case STMT_DECLARATION:
622 split_declaration(stmt->declaration);
623 break;
624 case STMT_RETURN:
625 __split_expr(stmt->ret_value);
626 __pass_to_client(stmt->ret_value, RETURN_HOOK);
627 nullify_path();
628 break;
629 case STMT_EXPRESSION:
630 __split_expr(stmt->expression);
631 break;
632 case STMT_COMPOUND: {
633 struct statement *tmp;
635 if (!last_stmt)
636 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
637 __push_scope_hooks();
638 FOR_EACH_PTR(stmt->stmts, tmp) {
639 __split_stmt(tmp);
640 } END_FOR_EACH_PTR(tmp);
641 __call_scope_hooks();
642 break;
644 case STMT_IF:
645 if (known_condition_true(stmt->if_conditional)) {
646 __split_stmt(stmt->if_true);
647 break;
649 if (known_condition_false(stmt->if_conditional)) {
650 __split_stmt(stmt->if_false);
651 break;
653 if (option_known_conditions &&
654 implied_condition_true(stmt->if_conditional)) {
655 sm_info("this condition is true.");
656 __split_stmt(stmt->if_true);
657 break;
659 if (option_known_conditions &&
660 implied_condition_false(stmt->if_conditional)) {
661 sm_info("this condition is false.");
662 __split_stmt(stmt->if_false);
663 break;
665 __split_whole_condition(stmt->if_conditional);
666 __split_stmt(stmt->if_true);
667 if (empty_statement(stmt->if_true) &&
668 last_stmt_on_same_line() &&
669 !get_macro_name(stmt->if_true->pos))
670 sm_msg("warn: if();");
671 __push_true_states();
672 __use_false_states();
673 __split_stmt(stmt->if_false);
674 __merge_true_states();
675 break;
676 case STMT_ITERATOR:
677 if (stmt->iterator_pre_condition)
678 handle_pre_loop(stmt);
679 else if (stmt->iterator_post_condition)
680 handle_post_loop(stmt);
681 else {
682 // these are for(;;) type loops.
683 handle_pre_loop(stmt);
685 break;
686 case STMT_SWITCH:
687 if (get_value(stmt->switch_expression, &sval)) {
688 split_known_switch(stmt, sval);
689 break;
691 __split_expr(stmt->switch_expression);
692 push_expression(&switch_expr_stack, stmt->switch_expression);
693 __save_switch_states(top_expression(switch_expr_stack));
694 nullify_path();
695 __push_default();
696 __push_breaks();
697 __split_stmt(stmt->switch_statement);
698 if (!__pop_default())
699 __merge_switches(top_expression(switch_expr_stack),
700 NULL);
701 __discard_switches();
702 __merge_breaks();
703 pop_expression(&switch_expr_stack);
704 break;
705 case STMT_CASE:
706 __merge_switches(top_expression(switch_expr_stack),
707 stmt->case_expression);
708 __pass_case_to_client(top_expression(switch_expr_stack),
709 stmt->case_expression);
710 if (!stmt->case_expression)
711 __set_default();
712 __split_expr(stmt->case_expression);
713 __split_expr(stmt->case_to);
714 __split_stmt(stmt->case_statement);
715 break;
716 case STMT_LABEL:
717 if (stmt->label_identifier &&
718 stmt->label_identifier->type == SYM_LABEL &&
719 stmt->label_identifier->ident) {
720 loop_count = 1000000;
721 __merge_gotos(stmt->label_identifier->ident->name);
723 __split_stmt(stmt->label_statement);
724 break;
725 case STMT_GOTO:
726 __split_expr(stmt->goto_expression);
727 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
728 if (!strcmp(stmt->goto_label->ident->name, "break")) {
729 __process_breaks();
730 } else if (!strcmp(stmt->goto_label->ident->name,
731 "continue")) {
732 __process_continues();
734 } else if (stmt->goto_label &&
735 stmt->goto_label->type == SYM_LABEL &&
736 stmt->goto_label->ident) {
737 __save_gotos(stmt->goto_label->ident->name);
739 nullify_path();
740 break;
741 case STMT_NONE:
742 break;
743 case STMT_ASM:
744 __pass_to_client(stmt, ASM_HOOK);
745 __split_expr(stmt->asm_string);
746 split_asm_constraints(stmt->asm_outputs);
747 split_asm_constraints(stmt->asm_inputs);
748 split_asm_constraints(stmt->asm_clobbers);
749 break;
750 case STMT_CONTEXT:
751 break;
752 case STMT_RANGE:
753 __split_expr(stmt->range_expression);
754 __split_expr(stmt->range_low);
755 __split_expr(stmt->range_high);
756 break;
758 out:
759 __process_post_op_stack();
762 static void split_expr_list(struct expression_list *expr_list)
764 struct expression *expr;
766 FOR_EACH_PTR(expr_list, expr) {
767 __split_expr(expr);
768 __process_post_op_stack();
769 } END_FOR_EACH_PTR(expr);
772 static void split_sym(struct symbol *sym)
774 if (!sym)
775 return;
776 if (!(sym->namespace & NS_SYMBOL))
777 return;
779 __split_stmt(sym->stmt);
780 __split_expr(sym->array_size);
781 split_symlist(sym->arguments);
782 split_symlist(sym->symbol_list);
783 __split_stmt(sym->inline_stmt);
784 split_symlist(sym->inline_symbol_list);
787 static void split_symlist(struct symbol_list *sym_list)
789 struct symbol *sym;
791 FOR_EACH_PTR(sym_list, sym) {
792 split_sym(sym);
793 } END_FOR_EACH_PTR(sym);
796 static void fake_member_assigns(struct symbol *sym)
798 struct expression *symbol, *deref, *assign, *tmp;
800 symbol = symbol_expression(sym);
801 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
802 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
803 continue;
804 deref = member_expression(symbol, '.', tmp->expr_ident);
805 assign = assign_expression(deref, tmp->ident_expression);
806 __split_expr(assign);
807 } END_FOR_EACH_PTR(tmp);
810 static void fake_assign_expr(struct symbol *sym)
812 struct expression *assign, *symbol;
814 symbol = symbol_expression(sym);
815 assign = assign_expression(symbol, sym->initializer);
816 __split_expr(assign);
819 static void do_initializer_stuff(struct symbol *sym)
821 if (!sym->initializer)
822 return;
823 if (sym->initializer->type == EXPR_INITIALIZER)
824 fake_member_assigns(sym);
825 else
826 fake_assign_expr(sym);
829 static void split_declaration(struct symbol_list *sym_list)
831 struct symbol *sym;
833 FOR_EACH_PTR(sym_list, sym) {
834 __pass_to_client(sym, DECLARATION_HOOK);
835 do_initializer_stuff(sym);
836 split_sym(sym);
837 } END_FOR_EACH_PTR(sym);
840 static void fake_global_assign(struct symbol *sym)
842 struct expression *assign, *symbol;
844 if (!sym->initializer)
845 return;
846 if (sym->initializer->type == EXPR_INITIALIZER) {
847 struct expression *deref, *tmp;
849 symbol = symbol_expression(sym);
850 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
851 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
852 continue;
853 deref = member_expression(symbol, '.', tmp->expr_ident);
854 assign = assign_expression(deref, tmp->ident_expression);
855 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
856 } END_FOR_EACH_PTR(tmp);
857 } else {
858 symbol = symbol_expression(sym);
859 assign = assign_expression(symbol, sym->initializer);
860 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
864 static void split_function(struct symbol *sym)
866 struct symbol *base_type = get_base_type(sym);
868 cur_func_sym = sym;
869 if (sym->ident)
870 cur_func = sym->ident->name;
871 __smatch_lineno = sym->pos.line;
872 last_stmt = NULL;
873 loop_count = 0;
874 sm_debug("new function: %s\n", cur_func);
875 __slist_id = 0;
876 if (option_two_passes) {
877 __unnullify_path();
878 loop_num = 0;
879 final_pass = 0;
880 __pass_to_client(sym, FUNC_DEF_HOOK);
881 __pass_to_client(sym, AFTER_DEF_HOOK);
882 __split_stmt(base_type->stmt);
883 __split_stmt(base_type->inline_stmt);
884 nullify_path();
886 __unnullify_path();
887 loop_num = 0;
888 final_pass = 1;
889 __pass_to_client(sym, FUNC_DEF_HOOK);
890 __pass_to_client(sym, AFTER_DEF_HOOK);
891 __split_stmt(base_type->stmt);
892 __split_stmt(base_type->inline_stmt);
893 __pass_to_client(sym, END_FUNC_HOOK);
894 cur_func = NULL;
895 clear_all_states();
896 free_data_info_allocs();
897 free_expression_stack(&switch_expr_stack);
898 __free_ptr_list((struct ptr_list **)&big_statement_stack);
899 __bail_on_rest_of_function = 0;
902 static void parse_inline(struct expression *call)
904 struct symbol *base_type;
905 int loop_num_bak = loop_num;
906 int final_pass_bak = final_pass;
907 char *cur_func_bak = cur_func;
908 struct statement_list *big_statement_stack_bak = big_statement_stack;
909 struct expression_list *big_expression_stack_bak = big_expression_stack;
910 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
911 struct symbol *cur_func_sym_bak = cur_func_sym;
913 __pass_to_client(call, INLINE_FN_START);
914 final_pass = 0; /* don't print anything */
915 __inline_fn = call;
917 base_type = get_base_type(call->fn->symbol);
918 cur_func_sym = call->fn->symbol;
919 if (call->fn->symbol->ident)
920 cur_func = call->fn->symbol->ident->name;
921 else
922 cur_func = NULL;
923 set_position(call->fn->symbol->pos);
925 save_all_states();
926 nullify_all_states();
927 big_statement_stack = NULL;
928 big_expression_stack = NULL;
929 switch_expr_stack = NULL;
931 sm_debug("inline function: %s\n", cur_func);
932 __unnullify_path();
933 loop_num = 0;
934 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
935 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
936 __split_stmt(base_type->stmt);
937 __split_stmt(base_type->inline_stmt);
938 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
940 free_expression_stack(&switch_expr_stack);
941 __free_ptr_list((struct ptr_list **)&big_statement_stack);
942 nullify_path();
944 loop_num = loop_num_bak;
945 final_pass = final_pass_bak;
946 cur_func_sym = cur_func_sym_bak;
947 cur_func = cur_func_bak;
948 big_statement_stack = big_statement_stack_bak;
949 big_expression_stack = big_expression_stack_bak;
950 switch_expr_stack = switch_expr_stack_bak;
952 restore_all_states();
953 set_position(call->pos);
954 __inline_fn = NULL;
955 __pass_to_client(call, INLINE_FN_END);
958 static struct symbol_list *inlines_called;
959 static void add_inline_function(struct symbol *sym)
961 static struct symbol_list *already_added;
962 struct symbol *tmp;
964 FOR_EACH_PTR(already_added, tmp) {
965 if (tmp == sym)
966 return;
967 } END_FOR_EACH_PTR(tmp);
969 add_ptr_list(&already_added, sym);
970 add_ptr_list(&inlines_called, sym);
973 static void process_inlines()
975 struct symbol *tmp;
977 FOR_EACH_PTR(inlines_called, tmp) {
978 split_function(tmp);
979 } END_FOR_EACH_PTR(tmp);
980 free_ptr_list(&inlines_called);
983 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list)
985 struct symbol *sym;
987 FOR_EACH_PTR_REVERSE(big_list, sym) {
988 if (!sym->scope)
989 continue;
990 return sym;
991 } END_FOR_EACH_PTR_REVERSE(sym);
993 return NULL;
996 static void split_inlines(struct symbol_list *sym_list)
998 struct symbol *sym, *base;
999 struct symbol_list *scope_list;
1000 int stream;
1002 sym = get_last_scoped_symbol(sym_list);
1003 if (!sym)
1004 return;
1005 scope_list = sym->scope->symbols;
1006 stream = sym->pos.stream;
1008 /* find the last static symbol in the file */
1009 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1010 if (sym->pos.stream != stream)
1011 continue;
1012 if (sym->type != SYM_NODE)
1013 continue;
1014 base = get_base_type(sym);
1015 if (!base)
1016 continue;
1017 if (base->type != SYM_FN)
1018 continue;
1019 if (!base->inline_stmt)
1020 continue;
1021 add_inline_function(sym);
1022 } END_FOR_EACH_PTR_REVERSE(sym);
1024 process_inlines();
1027 static void split_functions(struct symbol_list *sym_list)
1029 struct symbol *sym;
1031 FOR_EACH_PTR(sym_list, sym) {
1032 set_position(sym->pos);
1033 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1034 split_function(sym);
1035 process_inlines();
1036 } else {
1037 __pass_to_client(sym, BASE_HOOK);
1038 fake_global_assign(sym);
1040 } END_FOR_EACH_PTR(sym);
1041 split_inlines(sym_list);
1042 __pass_to_client(sym_list, END_FILE_HOOK);
1045 void smatch(int argc, char **argv)
1048 struct string_list *filelist = NULL;
1049 struct symbol_list *sym_list;
1051 if (argc < 2) {
1052 printf("Usage: smatch [--debug] <filename.c>\n");
1053 exit(1);
1055 sparse_initialize(argc, argv, &filelist);
1056 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1057 if (option_file_output) {
1058 char buf[256];
1060 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1061 sm_outfd = fopen(buf, "w");
1062 if (!sm_outfd) {
1063 printf("Error: Cannot open %s\n", base_file);
1064 exit(1);
1067 sym_list = sparse_keep_tokens(base_file);
1068 split_functions(sym_list);
1069 } END_FOR_EACH_PTR_NOTAG(base_file);