ranges: in cast_rl() sometimes it's better to just give up
[smatch.git] / smatch_flow.c
blob8d41cc63134d5ff6d770fc36efa76bff2f63449e
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 "smatch.h"
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h"
19 int final_pass;
20 int __inline_call;
21 struct expression *__inline_fn;
23 static int __smatch_lineno = 0;
25 static char *base_file;
26 static const char *filename;
27 static char *pathname;
28 static char *full_filename;
29 static char *cur_func;
30 static int loop_count;
31 int __expr_stmt_count;
32 static struct expression_list *switch_expr_stack = NULL;
33 static struct expression_list *post_op_stack = NULL;
35 struct expression_list *big_expression_stack;
36 struct statement_list *big_statement_stack;
37 int __in_pre_condition = 0;
38 int __bail_on_rest_of_function = 0;
39 char *get_function(void) { return cur_func; }
40 int get_lineno(void) { return __smatch_lineno; }
41 int inside_loop(void) { return !!loop_count; }
42 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
43 int in_expression_statement(void) { return !!__expr_stmt_count; }
45 static void split_symlist(struct symbol_list *sym_list);
46 static void split_declaration(struct symbol_list *sym_list);
47 static void split_expr_list(struct expression_list *expr_list);
48 static void add_inline_function(struct symbol *sym);
49 static void parse_inline(struct expression *expr);
51 int option_assume_loops = 0;
52 int option_known_conditions = 0;
53 int option_two_passes = 0;
54 struct symbol *cur_func_sym = NULL;
56 const char *get_filename(void)
58 if (option_info)
59 return base_file;
60 if (option_full_path)
61 return full_filename;
62 return filename;
65 const char *get_base_file(void)
67 return base_file;
70 static void set_position(struct position pos)
72 int len;
73 static int prev_stream = -1;
75 __smatch_lineno = pos.line;
77 if (pos.stream == prev_stream)
78 return;
80 filename = stream_name(pos.stream);
82 free(full_filename);
83 pathname = getcwd(NULL, 0);
84 if (pathname) {
85 len = strlen(pathname) + 1 + strlen(filename) + 1;
86 full_filename = malloc(len);
87 snprintf(full_filename, len, "%s/%s", pathname, filename);
88 } else {
89 full_filename = alloc_string(filename);
91 free(pathname);
94 static int is_inline_func(struct expression *expr)
96 if (expr->type != EXPR_SYMBOL || !expr->symbol)
97 return 0;
98 if (expr->symbol->ctype.modifiers & MOD_INLINE)
99 return 1;
100 return 0;
103 static int is_noreturn_func(struct expression *expr)
105 if (expr->type != EXPR_SYMBOL || !expr->symbol)
106 return 0;
107 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
108 return 1;
109 return 0;
112 int inlinable(struct expression *expr)
114 struct symbol *sym;
116 if (__inline_fn) /* don't nest */
117 return 0;
119 if (expr->type != EXPR_SYMBOL || !expr->symbol)
120 return 0;
121 sym = get_base_type(expr->symbol);
122 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
123 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
124 return 1;
125 return 0;
127 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
128 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
129 return 1;
130 return 0;
132 return 0;
135 void __process_post_op_stack(void)
137 struct expression *expr;
139 FOR_EACH_PTR(post_op_stack, expr) {
140 __pass_to_client(expr, OP_HOOK);
141 } END_FOR_EACH_PTR(expr);
143 __free_ptr_list((struct ptr_list **)&post_op_stack);
146 void __split_expr(struct expression *expr)
148 if (!expr)
149 return;
151 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
153 push_expression(&big_expression_stack, expr);
154 set_position(expr->pos);
155 __pass_to_client(expr, EXPR_HOOK);
157 switch (expr->type) {
158 case EXPR_PREOP:
159 if (expr->op == '*')
160 __pass_to_client(expr, DEREF_HOOK);
161 __split_expr(expr->unop);
162 __pass_to_client(expr, OP_HOOK);
163 break;
164 case EXPR_POSTOP:
165 __split_expr(expr->unop);
166 push_expression(&post_op_stack, expr);
167 break;
168 case EXPR_STATEMENT:
169 __expr_stmt_count++;
170 __split_stmt(expr->statement);
171 __expr_stmt_count--;
172 break;
173 case EXPR_LOGICAL:
174 case EXPR_COMPARE:
175 __pass_to_client(expr, LOGIC_HOOK);
176 __handle_logic(expr);
177 break;
178 case EXPR_BINOP:
179 __pass_to_client(expr, BINOP_HOOK);
180 case EXPR_COMMA:
181 __split_expr(expr->left);
182 __process_post_op_stack();
183 __split_expr(expr->right);
184 break;
185 case EXPR_ASSIGNMENT: {
186 struct expression *tmp;
188 if (!expr->right)
189 break;
191 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
193 /* foo = !bar() */
194 if (__handle_condition_assigns(expr))
195 break;
196 /* foo = (x < 5 ? foo : 5); */
197 if (__handle_select_assigns(expr))
198 break;
199 /* foo = ({frob(); frob(); frob(); 1;}) */
200 if (__handle_expr_statement_assigns(expr))
201 break;
203 __split_expr(expr->right);
204 __pass_to_client(expr, ASSIGNMENT_HOOK);
205 tmp = strip_expr(expr->right);
206 if (tmp->type == EXPR_CALL)
207 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
208 if (get_macro_name(tmp->pos) &&
209 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
210 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
211 __split_expr(expr->left);
212 break;
214 case EXPR_DEREF:
215 __pass_to_client(expr, DEREF_HOOK);
216 __split_expr(expr->deref);
217 break;
218 case EXPR_SLICE:
219 __split_expr(expr->base);
220 break;
221 case EXPR_CAST:
222 case EXPR_FORCE_CAST:
223 __pass_to_client(expr, CAST_HOOK);
224 __split_expr(expr->cast_expression);
225 break;
226 case EXPR_SIZEOF:
227 if (expr->cast_expression)
228 __pass_to_client(strip_parens(expr->cast_expression),
229 SIZEOF_HOOK);
230 break;
231 case EXPR_OFFSETOF:
232 case EXPR_ALIGNOF:
233 evaluate_expression(expr);
234 break;
235 case EXPR_CONDITIONAL:
236 case EXPR_SELECT:
237 if (known_condition_true(expr->conditional)) {
238 __split_expr(expr->cond_true);
239 break;
241 if (known_condition_false(expr->conditional)) {
242 __split_expr(expr->cond_false);
243 break;
245 __pass_to_client(expr, SELECT_HOOK);
246 __split_whole_condition(expr->conditional);
247 __split_expr(expr->cond_true);
248 __push_true_states();
249 __use_false_states();
250 __split_expr(expr->cond_false);
251 __merge_true_states();
252 break;
253 case EXPR_CALL:
254 if (sym_name_is("__builtin_constant_p", expr->fn))
255 break;
256 split_expr_list(expr->args);
257 __split_expr(expr->fn);
258 if (is_inline_func(expr->fn))
259 add_inline_function(expr->fn->symbol);
260 if (inlinable(expr->fn))
261 __inline_call = 1;
262 __process_post_op_stack();
263 __pass_to_client(expr, FUNCTION_CALL_HOOK);
264 __inline_call = 0;
265 if (inlinable(expr->fn)) {
266 parse_inline(expr);
268 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
269 if (is_noreturn_func(expr->fn))
270 nullify_path();
271 break;
272 case EXPR_INITIALIZER:
273 split_expr_list(expr->expr_list);
274 break;
275 case EXPR_IDENTIFIER:
276 __split_expr(expr->ident_expression);
277 break;
278 case EXPR_INDEX:
279 __split_expr(expr->idx_expression);
280 break;
281 case EXPR_POS:
282 __split_expr(expr->init_expr);
283 break;
284 case EXPR_SYMBOL:
285 __pass_to_client(expr, SYM_HOOK);
286 break;
287 case EXPR_STRING:
288 __pass_to_client(expr, STRING_HOOK);
289 break;
290 default:
291 break;
293 pop_expression(&big_expression_stack);
296 static int is_forever_loop(struct statement *stmt)
298 struct expression *expr;
300 expr = strip_expr(stmt->iterator_pre_condition);
301 if (!expr)
302 expr = stmt->iterator_post_condition;
303 if (!expr) {
304 /* this is a for(;;) loop... */
305 return 1;
308 if (expr->type == EXPR_VALUE && expr->value == 1)
309 return 1;
311 return 0;
314 static int loop_num;
315 static char *get_loop_name(int num)
317 char buf[256];
319 snprintf(buf, 255, "-loop%d", num);
320 buf[255] = '\0';
321 return alloc_sname(buf);
325 * Pre Loops are while and for loops.
327 static void handle_pre_loop(struct statement *stmt)
329 int once_through; /* we go through the loop at least once */
330 struct sm_state *extra_sm = NULL;
331 int unchanged = 0;
332 char *loop_name;
333 struct state_list *slist = NULL;
334 struct sm_state *sm = NULL;
336 loop_name = get_loop_name(loop_num);
337 loop_num++;
339 __split_stmt(stmt->iterator_pre_statement);
341 once_through = implied_condition_true(stmt->iterator_pre_condition);
343 loop_count++;
344 __push_continues();
345 __push_breaks();
347 __merge_gotos(loop_name);
349 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
350 __in_pre_condition++;
351 __pass_to_client(stmt, PRELOOP_HOOK);
352 __split_whole_condition(stmt->iterator_pre_condition);
353 __in_pre_condition--;
354 FOR_EACH_PTR(slist, sm) {
355 set_state(sm->owner, sm->name, sm->sym, sm->state);
356 } END_FOR_EACH_PTR(sm);
357 free_slist(&slist);
358 if (extra_sm)
359 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
361 if (option_assume_loops)
362 once_through = 1;
364 __split_stmt(stmt->iterator_statement);
365 __warn_on_silly_pre_loops();
366 if (is_forever_loop(stmt)) {
367 struct state_list *slist;
369 __save_gotos(loop_name);
371 __push_fake_cur_slist();
372 __split_stmt(stmt->iterator_post_statement);
373 slist = __pop_fake_cur_slist();
375 __discard_continues();
376 __discard_false_states();
377 __use_breaks();
379 if (!__path_is_null())
380 __merge_slist_into_cur(slist);
381 free_slist(&slist);
382 } else {
383 __merge_continues();
384 unchanged = __iterator_unchanged(extra_sm);
385 __split_stmt(stmt->iterator_post_statement);
386 __save_gotos(loop_name);
387 __split_whole_condition(stmt->iterator_pre_condition);
388 nullify_path();
389 __merge_false_states();
390 if (once_through)
391 __discard_false_states();
392 else
393 __merge_false_states();
395 if (extra_sm && unchanged)
396 __extra_pre_loop_hook_after(extra_sm,
397 stmt->iterator_post_statement,
398 stmt->iterator_pre_condition);
399 __merge_breaks();
401 loop_count--;
405 * Post loops are do {} while();
407 static void handle_post_loop(struct statement *stmt)
409 char *loop_name;
411 loop_name = get_loop_name(loop_num);
412 loop_num++;
413 loop_count++;
415 __push_continues();
416 __push_breaks();
417 __merge_gotos(loop_name);
418 __split_stmt(stmt->iterator_statement);
419 __merge_continues();
420 if (!is_zero(stmt->iterator_post_condition))
421 __save_gotos(loop_name);
423 if (is_forever_loop(stmt)) {
424 __use_breaks();
425 } else {
426 __split_whole_condition(stmt->iterator_post_condition);
427 __use_false_states();
428 __merge_breaks();
430 loop_count--;
433 static int empty_statement(struct statement *stmt)
435 if (!stmt)
436 return 0;
437 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
438 return 1;
439 return 0;
442 static int last_stmt_on_same_line()
444 struct statement *stmt;
445 int i = 0;
447 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
448 if (!i++)
449 continue;
450 if (stmt->pos.line == get_lineno())
451 return 1;
452 return 0;
453 } END_FOR_EACH_PTR_REVERSE(stmt);
454 return 0;
457 static struct statement *last_stmt;
458 static int is_last_stmt(struct statement *stmt)
460 if (stmt == last_stmt)
461 return 1;
462 return 0;
465 static void print_unreached_initializers(struct symbol_list *sym_list)
467 struct symbol *sym;
469 FOR_EACH_PTR(sym_list, sym) {
470 if (sym->initializer)
471 sm_msg("info: '%s' is not actually initialized (unreached code).",
472 (sym->ident ? sym->ident->name : "this variable"));
473 } END_FOR_EACH_PTR(sym);
476 static void print_unreached(struct statement *stmt)
478 static int print = 1;
480 if (!__path_is_null()) {
481 print = 1;
482 return;
484 if (!print)
485 return;
487 switch (stmt->type) {
488 case STMT_COMPOUND: /* after a switch before a case stmt */
489 case STMT_RANGE:
490 case STMT_CASE:
491 case STMT_LABEL:
492 return;
493 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
494 print_unreached_initializers(stmt->declaration);
495 return;
496 case STMT_RETURN: /* gcc complains if you don't have a return statement */
497 if (is_last_stmt(stmt))
498 return;
499 break;
500 case STMT_GOTO:
501 if (!option_spammy)
502 return;
503 break;
504 default:
505 break;
507 if (!option_spammy && empty_statement(stmt))
508 return;
509 sm_msg("info: ignoring unreachable code.");
510 print = 0;
513 static void split_asm_constraints(struct expression_list *expr_list)
515 struct expression *expr;
516 int state = 0;
518 FOR_EACH_PTR(expr_list, expr) {
519 switch (state) {
520 case 0: /* identifier */
521 case 1: /* constraint */
522 state++;
523 continue;
524 case 2: /* expression */
525 state = 0;
526 __split_expr(expr);
527 continue;
529 } END_FOR_EACH_PTR(expr);
532 static int is_case_val(struct statement *stmt, sval_t sval)
534 sval_t case_sval;
536 if (stmt->type != STMT_CASE)
537 return 0;
538 if (!stmt->case_expression) {
539 __set_default();
540 return 1;
542 if (!get_value(stmt->case_expression, &case_sval))
543 return 0;
544 if (case_sval.value == sval.value)
545 return 1;
546 return 0;
549 static void split_known_switch(struct statement *stmt, sval_t sval)
551 struct statement *tmp;
553 __split_expr(stmt->switch_expression);
555 push_expression(&switch_expr_stack, stmt->switch_expression);
556 __save_switch_states(top_expression(switch_expr_stack));
557 nullify_path();
558 __push_default();
559 __push_breaks();
561 stmt = stmt->switch_statement;
563 if (!last_stmt)
564 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
566 __push_scope_hooks();
567 FOR_EACH_PTR(stmt->stmts, tmp) {
568 __smatch_lineno = tmp->pos.line;
569 if (is_case_val(tmp, sval)) {
570 __merge_switches(top_expression(switch_expr_stack),
571 stmt->case_expression);
572 __pass_case_to_client(top_expression(switch_expr_stack),
573 stmt->case_expression);
575 if (__path_is_null())
576 continue;
577 __split_stmt(tmp);
578 if (__path_is_null()) {
579 __set_default();
580 goto out;
582 } END_FOR_EACH_PTR(tmp);
583 out:
584 __call_scope_hooks();
585 if (!__pop_default())
586 __merge_switches(top_expression(switch_expr_stack),
587 NULL);
588 __discard_switches();
589 __merge_breaks();
590 pop_expression(&switch_expr_stack);
593 void __split_stmt(struct statement *stmt)
595 sval_t sval;
597 if (!stmt)
598 goto out;
600 if (out_of_memory() || __bail_on_rest_of_function) {
601 static char *printed = NULL;
603 if (printed != cur_func)
604 sm_msg("Function too hairy. Giving up.");
605 final_pass = 0; /* turn off sm_msg() from here */
606 printed = cur_func;
607 return;
610 add_ptr_list(&big_statement_stack, stmt);
611 free_expression_stack(&big_expression_stack);
612 set_position(stmt->pos);
613 print_unreached(stmt);
614 __pass_to_client(stmt, STMT_HOOK);
616 switch (stmt->type) {
617 case STMT_DECLARATION:
618 split_declaration(stmt->declaration);
619 break;
620 case STMT_RETURN:
621 __split_expr(stmt->ret_value);
622 __pass_to_client(stmt->ret_value, RETURN_HOOK);
623 nullify_path();
624 break;
625 case STMT_EXPRESSION:
626 __split_expr(stmt->expression);
627 break;
628 case STMT_COMPOUND: {
629 struct statement *tmp;
631 if (!last_stmt)
632 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
633 __push_scope_hooks();
634 FOR_EACH_PTR(stmt->stmts, tmp) {
635 __split_stmt(tmp);
636 } END_FOR_EACH_PTR(tmp);
637 __call_scope_hooks();
638 break;
640 case STMT_IF:
641 if (known_condition_true(stmt->if_conditional)) {
642 __split_stmt(stmt->if_true);
643 break;
645 if (known_condition_false(stmt->if_conditional)) {
646 __split_stmt(stmt->if_false);
647 break;
649 if (option_known_conditions &&
650 implied_condition_true(stmt->if_conditional)) {
651 sm_info("this condition is true.");
652 __split_stmt(stmt->if_true);
653 break;
655 if (option_known_conditions &&
656 implied_condition_false(stmt->if_conditional)) {
657 sm_info("this condition is false.");
658 __split_stmt(stmt->if_false);
659 break;
661 __split_whole_condition(stmt->if_conditional);
662 __split_stmt(stmt->if_true);
663 if (empty_statement(stmt->if_true) &&
664 last_stmt_on_same_line() &&
665 !get_macro_name(stmt->if_true->pos))
666 sm_msg("warn: if();");
667 __push_true_states();
668 __use_false_states();
669 __split_stmt(stmt->if_false);
670 __merge_true_states();
671 break;
672 case STMT_ITERATOR:
673 if (stmt->iterator_pre_condition)
674 handle_pre_loop(stmt);
675 else if (stmt->iterator_post_condition)
676 handle_post_loop(stmt);
677 else {
678 // these are for(;;) type loops.
679 handle_pre_loop(stmt);
681 break;
682 case STMT_SWITCH:
683 if (get_value(stmt->switch_expression, &sval)) {
684 split_known_switch(stmt, sval);
685 break;
687 __split_expr(stmt->switch_expression);
688 push_expression(&switch_expr_stack, stmt->switch_expression);
689 __save_switch_states(top_expression(switch_expr_stack));
690 nullify_path();
691 __push_default();
692 __push_breaks();
693 __split_stmt(stmt->switch_statement);
694 if (!__pop_default())
695 __merge_switches(top_expression(switch_expr_stack),
696 NULL);
697 __discard_switches();
698 __merge_breaks();
699 pop_expression(&switch_expr_stack);
700 break;
701 case STMT_CASE:
702 __merge_switches(top_expression(switch_expr_stack),
703 stmt->case_expression);
704 __pass_case_to_client(top_expression(switch_expr_stack),
705 stmt->case_expression);
706 if (!stmt->case_expression)
707 __set_default();
708 __split_expr(stmt->case_expression);
709 __split_expr(stmt->case_to);
710 __split_stmt(stmt->case_statement);
711 break;
712 case STMT_LABEL:
713 if (stmt->label_identifier &&
714 stmt->label_identifier->type == SYM_LABEL &&
715 stmt->label_identifier->ident) {
716 loop_count = 1000000;
717 __merge_gotos(stmt->label_identifier->ident->name);
719 __split_stmt(stmt->label_statement);
720 break;
721 case STMT_GOTO:
722 __split_expr(stmt->goto_expression);
723 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
724 if (!strcmp(stmt->goto_label->ident->name, "break")) {
725 __process_breaks();
726 } else if (!strcmp(stmt->goto_label->ident->name,
727 "continue")) {
728 __process_continues();
730 } else if (stmt->goto_label &&
731 stmt->goto_label->type == SYM_LABEL &&
732 stmt->goto_label->ident) {
733 __save_gotos(stmt->goto_label->ident->name);
735 nullify_path();
736 break;
737 case STMT_NONE:
738 break;
739 case STMT_ASM:
740 __pass_to_client(stmt, ASM_HOOK);
741 __split_expr(stmt->asm_string);
742 split_asm_constraints(stmt->asm_outputs);
743 split_asm_constraints(stmt->asm_inputs);
744 split_asm_constraints(stmt->asm_clobbers);
745 break;
746 case STMT_CONTEXT:
747 break;
748 case STMT_RANGE:
749 __split_expr(stmt->range_expression);
750 __split_expr(stmt->range_low);
751 __split_expr(stmt->range_high);
752 break;
754 out:
755 __process_post_op_stack();
758 static void split_expr_list(struct expression_list *expr_list)
760 struct expression *expr;
762 FOR_EACH_PTR(expr_list, expr) {
763 __split_expr(expr);
764 __process_post_op_stack();
765 } END_FOR_EACH_PTR(expr);
768 static void split_sym(struct symbol *sym)
770 if (!sym)
771 return;
772 if (!(sym->namespace & NS_SYMBOL))
773 return;
775 __split_stmt(sym->stmt);
776 __split_expr(sym->array_size);
777 split_symlist(sym->arguments);
778 split_symlist(sym->symbol_list);
779 __split_stmt(sym->inline_stmt);
780 split_symlist(sym->inline_symbol_list);
783 static void split_symlist(struct symbol_list *sym_list)
785 struct symbol *sym;
787 FOR_EACH_PTR(sym_list, sym) {
788 split_sym(sym);
789 } END_FOR_EACH_PTR(sym);
792 static void fake_member_assigns(struct symbol *sym)
794 struct expression *symbol, *deref, *assign, *tmp;
796 symbol = symbol_expression(sym);
797 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
798 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
799 continue;
800 deref = member_expression(symbol, '.', tmp->expr_ident);
801 assign = assign_expression(deref, tmp->ident_expression);
802 __split_expr(assign);
803 } END_FOR_EACH_PTR(tmp);
806 static void fake_assign_expr(struct symbol *sym)
808 struct expression *assign, *symbol;
810 symbol = symbol_expression(sym);
811 assign = assign_expression(symbol, sym->initializer);
812 __split_expr(assign);
815 static void do_initializer_stuff(struct symbol *sym)
817 if (!sym->initializer)
818 return;
819 if (sym->initializer->type == EXPR_INITIALIZER)
820 fake_member_assigns(sym);
821 else
822 fake_assign_expr(sym);
825 static void split_declaration(struct symbol_list *sym_list)
827 struct symbol *sym;
829 FOR_EACH_PTR(sym_list, sym) {
830 __pass_to_client(sym, DECLARATION_HOOK);
831 do_initializer_stuff(sym);
832 split_sym(sym);
833 } END_FOR_EACH_PTR(sym);
836 static void fake_global_assign(struct symbol *sym)
838 struct expression *assign, *symbol;
840 if (!sym->initializer)
841 return;
842 if (sym->initializer->type == EXPR_INITIALIZER) {
843 struct expression *deref, *tmp;
845 symbol = symbol_expression(sym);
846 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
847 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
848 continue;
849 deref = member_expression(symbol, '.', tmp->expr_ident);
850 assign = assign_expression(deref, tmp->ident_expression);
851 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
852 } END_FOR_EACH_PTR(tmp);
853 } else {
854 symbol = symbol_expression(sym);
855 assign = assign_expression(symbol, sym->initializer);
856 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
860 static void split_function(struct symbol *sym)
862 struct symbol *base_type = get_base_type(sym);
864 cur_func_sym = sym;
865 if (sym->ident)
866 cur_func = sym->ident->name;
867 __smatch_lineno = sym->pos.line;
868 last_stmt = NULL;
869 loop_count = 0;
870 sm_debug("new function: %s\n", cur_func);
871 __slist_id = 0;
872 if (option_two_passes) {
873 __unnullify_path();
874 loop_num = 0;
875 final_pass = 0;
876 __pass_to_client(sym, FUNC_DEF_HOOK);
877 __pass_to_client(sym, AFTER_DEF_HOOK);
878 __split_stmt(base_type->stmt);
879 __split_stmt(base_type->inline_stmt);
880 nullify_path();
882 __unnullify_path();
883 loop_num = 0;
884 final_pass = 1;
885 __pass_to_client(sym, FUNC_DEF_HOOK);
886 __pass_to_client(sym, AFTER_DEF_HOOK);
887 __split_stmt(base_type->stmt);
888 __split_stmt(base_type->inline_stmt);
889 __pass_to_client(sym, END_FUNC_HOOK);
890 cur_func = NULL;
891 clear_all_states();
892 free_data_info_allocs();
893 free_expression_stack(&switch_expr_stack);
894 __free_ptr_list((struct ptr_list **)&big_statement_stack);
895 __bail_on_rest_of_function = 0;
898 static void parse_inline(struct expression *call)
900 struct symbol *base_type;
901 int loop_num_bak = loop_num;
902 int final_pass_bak = final_pass;
903 char *cur_func_bak = cur_func;
904 struct statement_list *big_statement_stack_bak = big_statement_stack;
905 struct expression_list *big_expression_stack_bak = big_expression_stack;
906 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
907 struct symbol *cur_func_sym_bak = cur_func_sym;
909 __pass_to_client(call, INLINE_FN_START);
910 final_pass = 0; /* don't print anything */
911 __inline_fn = call;
913 base_type = get_base_type(call->fn->symbol);
914 cur_func_sym = call->fn->symbol;
915 if (call->fn->symbol->ident)
916 cur_func = call->fn->symbol->ident->name;
917 else
918 cur_func = NULL;
919 set_position(call->fn->symbol->pos);
921 save_all_states();
922 nullify_all_states();
923 big_statement_stack = NULL;
924 big_expression_stack = NULL;
925 switch_expr_stack = NULL;
927 sm_debug("inline function: %s\n", cur_func);
928 __unnullify_path();
929 loop_num = 0;
930 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
931 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
932 __split_stmt(base_type->stmt);
933 __split_stmt(base_type->inline_stmt);
934 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
936 free_expression_stack(&switch_expr_stack);
937 __free_ptr_list((struct ptr_list **)&big_statement_stack);
938 nullify_path();
940 loop_num = loop_num_bak;
941 final_pass = final_pass_bak;
942 cur_func_sym = cur_func_sym_bak;
943 cur_func = cur_func_bak;
944 big_statement_stack = big_statement_stack_bak;
945 big_expression_stack = big_expression_stack_bak;
946 switch_expr_stack = switch_expr_stack_bak;
948 restore_all_states();
949 set_position(call->pos);
950 __inline_fn = NULL;
951 __pass_to_client(call, INLINE_FN_END);
954 static struct symbol_list *inlines_called;
955 static void add_inline_function(struct symbol *sym)
957 static struct symbol_list *already_added;
958 struct symbol *tmp;
960 FOR_EACH_PTR(already_added, tmp) {
961 if (tmp == sym)
962 return;
963 } END_FOR_EACH_PTR(tmp);
965 add_ptr_list(&already_added, sym);
966 add_ptr_list(&inlines_called, sym);
969 static void process_inlines()
971 struct symbol *tmp;
973 FOR_EACH_PTR(inlines_called, tmp) {
974 split_function(tmp);
975 } END_FOR_EACH_PTR(tmp);
976 free_ptr_list(&inlines_called);
979 static void split_functions(struct symbol_list *sym_list)
981 struct symbol *sym;
983 FOR_EACH_PTR(sym_list, sym) {
984 set_position(sym->pos);
985 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
986 split_function(sym);
987 process_inlines();
988 } else {
989 __pass_to_client(sym, BASE_HOOK);
990 fake_global_assign(sym);
992 } END_FOR_EACH_PTR(sym);
993 __pass_to_client_no_data(END_FILE_HOOK);
996 void smatch(int argc, char **argv)
999 struct string_list *filelist = NULL;
1000 struct symbol_list *sym_list;
1002 if (argc < 2) {
1003 printf("Usage: smatch [--debug] <filename.c>\n");
1004 exit(1);
1006 sparse_initialize(argc, argv, &filelist);
1007 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1008 if (option_file_output) {
1009 char buf[256];
1011 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1012 sm_outfd = fopen(buf, "w");
1013 if (!sm_outfd) {
1014 printf("Error: Cannot open %s\n", base_file);
1015 exit(1);
1018 sym_list = sparse_keep_tokens(base_file);
1019 split_functions(sym_list);
1020 } END_FOR_EACH_PTR_NOTAG(base_file);