0d5c5708eee048fc273d0127edafcf1b40860438
[smatch.git] / smatch_flow.c
blob0d5c5708eee048fc273d0127edafcf1b40860438
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 (__inline_fn)
485 return;
487 if (!__path_is_null()) {
488 print = 1;
489 return;
491 if (!print)
492 return;
494 switch (stmt->type) {
495 case STMT_COMPOUND: /* after a switch before a case stmt */
496 case STMT_RANGE:
497 case STMT_CASE:
498 case STMT_LABEL:
499 return;
500 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
501 print_unreached_initializers(stmt->declaration);
502 return;
503 case STMT_RETURN: /* gcc complains if you don't have a return statement */
504 if (is_last_stmt(stmt))
505 return;
506 break;
507 case STMT_GOTO:
508 if (!option_spammy)
509 return;
510 break;
511 default:
512 break;
514 if (!option_spammy && empty_statement(stmt))
515 return;
516 sm_msg("info: ignoring unreachable code.");
517 print = 0;
520 static void split_asm_constraints(struct expression_list *expr_list)
522 struct expression *expr;
523 int state = 0;
525 FOR_EACH_PTR(expr_list, expr) {
526 switch (state) {
527 case 0: /* identifier */
528 case 1: /* constraint */
529 state++;
530 continue;
531 case 2: /* expression */
532 state = 0;
533 __split_expr(expr);
534 continue;
536 } END_FOR_EACH_PTR(expr);
539 static int is_case_val(struct statement *stmt, sval_t sval)
541 sval_t case_sval;
543 if (stmt->type != STMT_CASE)
544 return 0;
545 if (!stmt->case_expression) {
546 __set_default();
547 return 1;
549 if (!get_value(stmt->case_expression, &case_sval))
550 return 0;
551 if (case_sval.value == sval.value)
552 return 1;
553 return 0;
556 static void split_known_switch(struct statement *stmt, sval_t sval)
558 struct statement *tmp;
560 __split_expr(stmt->switch_expression);
562 push_expression(&switch_expr_stack, stmt->switch_expression);
563 __save_switch_states(top_expression(switch_expr_stack));
564 nullify_path();
565 __push_default();
566 __push_breaks();
568 stmt = stmt->switch_statement;
570 if (!last_stmt)
571 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
573 __push_scope_hooks();
574 FOR_EACH_PTR(stmt->stmts, tmp) {
575 __smatch_lineno = tmp->pos.line;
576 if (is_case_val(tmp, sval)) {
577 __merge_switches(top_expression(switch_expr_stack),
578 stmt->case_expression);
579 __pass_case_to_client(top_expression(switch_expr_stack),
580 stmt->case_expression);
582 if (__path_is_null())
583 continue;
584 __split_stmt(tmp);
585 if (__path_is_null()) {
586 __set_default();
587 goto out;
589 } END_FOR_EACH_PTR(tmp);
590 out:
591 __call_scope_hooks();
592 if (!__pop_default())
593 __merge_switches(top_expression(switch_expr_stack),
594 NULL);
595 __discard_switches();
596 __merge_breaks();
597 pop_expression(&switch_expr_stack);
600 void __split_stmt(struct statement *stmt)
602 sval_t sval;
604 if (!stmt)
605 goto out;
607 if (out_of_memory() || __bail_on_rest_of_function) {
608 static char *printed = NULL;
610 if (printed != cur_func)
611 sm_msg("Function too hairy. Giving up.");
612 final_pass = 0; /* turn off sm_msg() from here */
613 printed = cur_func;
614 return;
617 add_ptr_list(&big_statement_stack, stmt);
618 free_expression_stack(&big_expression_stack);
619 set_position(stmt->pos);
620 print_unreached(stmt);
621 __pass_to_client(stmt, STMT_HOOK);
623 switch (stmt->type) {
624 case STMT_DECLARATION:
625 split_declaration(stmt->declaration);
626 break;
627 case STMT_RETURN:
628 __split_expr(stmt->ret_value);
629 __pass_to_client(stmt->ret_value, RETURN_HOOK);
630 nullify_path();
631 break;
632 case STMT_EXPRESSION:
633 __split_expr(stmt->expression);
634 break;
635 case STMT_COMPOUND: {
636 struct statement *tmp;
638 if (!last_stmt)
639 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
640 __push_scope_hooks();
641 FOR_EACH_PTR(stmt->stmts, tmp) {
642 __split_stmt(tmp);
643 } END_FOR_EACH_PTR(tmp);
644 __call_scope_hooks();
645 break;
647 case STMT_IF:
648 if (known_condition_true(stmt->if_conditional)) {
649 __split_stmt(stmt->if_true);
650 break;
652 if (known_condition_false(stmt->if_conditional)) {
653 __split_stmt(stmt->if_false);
654 break;
656 if (option_known_conditions &&
657 implied_condition_true(stmt->if_conditional)) {
658 sm_info("this condition is true.");
659 __split_stmt(stmt->if_true);
660 break;
662 if (option_known_conditions &&
663 implied_condition_false(stmt->if_conditional)) {
664 sm_info("this condition is false.");
665 __split_stmt(stmt->if_false);
666 break;
668 __split_whole_condition(stmt->if_conditional);
669 __split_stmt(stmt->if_true);
670 if (empty_statement(stmt->if_true) &&
671 last_stmt_on_same_line() &&
672 !get_macro_name(stmt->if_true->pos))
673 sm_msg("warn: if();");
674 __push_true_states();
675 __use_false_states();
676 __split_stmt(stmt->if_false);
677 __merge_true_states();
678 break;
679 case STMT_ITERATOR:
680 if (stmt->iterator_pre_condition)
681 handle_pre_loop(stmt);
682 else if (stmt->iterator_post_condition)
683 handle_post_loop(stmt);
684 else {
685 // these are for(;;) type loops.
686 handle_pre_loop(stmt);
688 break;
689 case STMT_SWITCH:
690 if (get_value(stmt->switch_expression, &sval)) {
691 split_known_switch(stmt, sval);
692 break;
694 __split_expr(stmt->switch_expression);
695 push_expression(&switch_expr_stack, stmt->switch_expression);
696 __save_switch_states(top_expression(switch_expr_stack));
697 nullify_path();
698 __push_default();
699 __push_breaks();
700 __split_stmt(stmt->switch_statement);
701 if (!__pop_default())
702 __merge_switches(top_expression(switch_expr_stack),
703 NULL);
704 __discard_switches();
705 __merge_breaks();
706 pop_expression(&switch_expr_stack);
707 break;
708 case STMT_CASE:
709 __merge_switches(top_expression(switch_expr_stack),
710 stmt->case_expression);
711 __pass_case_to_client(top_expression(switch_expr_stack),
712 stmt->case_expression);
713 if (!stmt->case_expression)
714 __set_default();
715 __split_expr(stmt->case_expression);
716 __split_expr(stmt->case_to);
717 __split_stmt(stmt->case_statement);
718 break;
719 case STMT_LABEL:
720 if (stmt->label_identifier &&
721 stmt->label_identifier->type == SYM_LABEL &&
722 stmt->label_identifier->ident) {
723 loop_count = 1000000;
724 __merge_gotos(stmt->label_identifier->ident->name);
726 __split_stmt(stmt->label_statement);
727 break;
728 case STMT_GOTO:
729 __split_expr(stmt->goto_expression);
730 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
731 if (!strcmp(stmt->goto_label->ident->name, "break")) {
732 __process_breaks();
733 } else if (!strcmp(stmt->goto_label->ident->name,
734 "continue")) {
735 __process_continues();
737 } else if (stmt->goto_label &&
738 stmt->goto_label->type == SYM_LABEL &&
739 stmt->goto_label->ident) {
740 __save_gotos(stmt->goto_label->ident->name);
742 nullify_path();
743 break;
744 case STMT_NONE:
745 break;
746 case STMT_ASM:
747 __pass_to_client(stmt, ASM_HOOK);
748 __split_expr(stmt->asm_string);
749 split_asm_constraints(stmt->asm_outputs);
750 split_asm_constraints(stmt->asm_inputs);
751 split_asm_constraints(stmt->asm_clobbers);
752 break;
753 case STMT_CONTEXT:
754 break;
755 case STMT_RANGE:
756 __split_expr(stmt->range_expression);
757 __split_expr(stmt->range_low);
758 __split_expr(stmt->range_high);
759 break;
761 out:
762 __process_post_op_stack();
765 static void split_expr_list(struct expression_list *expr_list)
767 struct expression *expr;
769 FOR_EACH_PTR(expr_list, expr) {
770 __split_expr(expr);
771 __process_post_op_stack();
772 } END_FOR_EACH_PTR(expr);
775 static void split_sym(struct symbol *sym)
777 if (!sym)
778 return;
779 if (!(sym->namespace & NS_SYMBOL))
780 return;
782 __split_stmt(sym->stmt);
783 __split_expr(sym->array_size);
784 split_symlist(sym->arguments);
785 split_symlist(sym->symbol_list);
786 __split_stmt(sym->inline_stmt);
787 split_symlist(sym->inline_symbol_list);
790 static void split_symlist(struct symbol_list *sym_list)
792 struct symbol *sym;
794 FOR_EACH_PTR(sym_list, sym) {
795 split_sym(sym);
796 } END_FOR_EACH_PTR(sym);
799 typedef void (fake_cb)(struct expression *expr);
801 static int member_to_number(struct expression *expr, struct ident *member)
803 struct symbol *type, *tmp;
804 char *name;
805 int i;
807 if (!member)
808 return -1;
809 name = member->name;
811 type = get_type(expr);
812 if (!type || type->type != SYM_STRUCT)
813 return -1;
815 i = -1;
816 FOR_EACH_PTR(type->symbol_list, tmp) {
817 i++;
818 if (!tmp->ident)
819 continue;
820 if (strcmp(name, tmp->ident->name) == 0)
821 return i;
822 } END_FOR_EACH_PTR(tmp);
823 return -1;
826 static struct ident *number_to_member(struct expression *expr, int num)
828 struct symbol *type, *member;
829 int i = 0;
831 type = get_type(expr);
832 if (!type || type->type != SYM_STRUCT)
833 return NULL;
835 FOR_EACH_PTR(type->symbol_list, member) {
836 if (i == num)
837 return member->ident;
838 i++;
839 } END_FOR_EACH_PTR(member);
840 return NULL;
843 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
845 static void set_members_to_zero(struct expression *symbol)
847 struct expression *deref, *assign;
848 struct symbol *type;
849 struct symbol *member, *member_type;
851 type = get_type(symbol);
852 if (!type)
853 return;
855 FOR_EACH_PTR(type->symbol_list, member) {
856 if (!member->ident)
857 continue;
858 member_type = get_real_base_type(member);
859 if (!member_type || member_type->type == SYM_ARRAY)
860 continue;
861 deref = member_expression(symbol, '.', member->ident);
862 assign = assign_expression(deref, zero_expr());
863 __split_expr(assign);
864 } END_FOR_EACH_PTR(member);
868 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
870 struct expression *deref, *assign, *tmp;
871 struct symbol *type;
872 struct ident *member;
873 int member_idx = 0;
875 type = get_type(symbol);
876 if (!type || (type->type != SYM_STRUCT && type->type != SYM_UNION))
877 return;
879 set_members_to_zero(symbol);
881 FOR_EACH_PTR(members, tmp) {
882 member = number_to_member(symbol, member_idx);
883 while (tmp->type == EXPR_IDENTIFIER) {
884 member = tmp->expr_ident;
885 member_idx = member_to_number(symbol, member);
886 tmp = tmp->ident_expression;
888 member_idx++;
889 deref = member_expression(symbol, '.', member);
890 if (tmp->type == EXPR_INITIALIZER) {
891 type = get_type(deref);
892 if (type && type->type == SYM_ARRAY)
893 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
894 else
895 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
896 } else {
897 assign = assign_expression(deref, tmp);
898 fake_cb(assign);
900 } END_FOR_EACH_PTR(tmp);
903 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
905 fake_member_assigns_helper(symbol_expression(sym),
906 sym->initializer->expr_list, fake_cb);
909 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
911 struct expression *offset, *binop, *assign, *tmp;
912 struct symbol *type;
913 int idx;
915 idx = 0;
916 FOR_EACH_PTR(expr_list, tmp) {
917 if (tmp->type == EXPR_INDEX) {
918 if (tmp->idx_from != tmp->idx_to)
919 return;
920 idx = tmp->idx_from;
921 if (!tmp->idx_expression)
922 goto next;
923 tmp = tmp->idx_expression;
925 offset = value_expr(idx);
926 binop = array_element_expression(array, offset);
927 if (tmp->type == EXPR_INITIALIZER) {
928 type = get_type(binop);
929 if (type && type->type == SYM_ARRAY)
930 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
931 else
932 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
933 } else {
934 assign = assign_expression(binop, tmp);
935 fake_cb(assign);
937 next:
938 idx++;
939 } END_FOR_EACH_PTR(tmp);
942 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
944 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
947 static void fake_assign_expr(struct symbol *sym)
949 struct expression *assign, *symbol;
951 symbol = symbol_expression(sym);
952 assign = assign_expression(symbol, sym->initializer);
953 __split_expr(assign);
956 static void call_split_expr(struct expression *expr)
958 __split_expr(expr);
961 static void do_initializer_stuff(struct symbol *sym)
963 if (!sym->initializer)
964 return;
966 if (sym->initializer->type == EXPR_INITIALIZER) {
967 if (get_real_base_type(sym)->type == SYM_ARRAY)
968 fake_element_assigns(sym, call_split_expr);
969 else
970 fake_member_assigns(sym, call_split_expr);
971 } else {
972 fake_assign_expr(sym);
976 static void split_declaration(struct symbol_list *sym_list)
978 struct symbol *sym;
980 FOR_EACH_PTR(sym_list, sym) {
981 __pass_to_client(sym, DECLARATION_HOOK);
982 do_initializer_stuff(sym);
983 split_sym(sym);
984 } END_FOR_EACH_PTR(sym);
987 static void call_global_assign_hooks(struct expression *assign)
989 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
992 static void fake_global_assign(struct symbol *sym)
994 struct expression *assign, *symbol;
996 if (!sym->initializer)
997 return;
998 if (sym->initializer->type == EXPR_INITIALIZER) {
999 if (get_real_base_type(sym)->type == SYM_ARRAY)
1000 fake_element_assigns(sym, call_global_assign_hooks);
1001 else
1002 fake_member_assigns(sym, call_global_assign_hooks);
1003 } else {
1004 symbol = symbol_expression(sym);
1005 assign = assign_expression(symbol, sym->initializer);
1006 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1010 static void split_function(struct symbol *sym)
1012 struct symbol *base_type = get_base_type(sym);
1014 cur_func_sym = sym;
1015 if (sym->ident)
1016 cur_func = sym->ident->name;
1017 __smatch_lineno = sym->pos.line;
1018 last_stmt = NULL;
1019 loop_count = 0;
1020 sm_debug("new function: %s\n", cur_func);
1021 __slist_id = 0;
1022 if (option_two_passes) {
1023 __unnullify_path();
1024 loop_num = 0;
1025 final_pass = 0;
1026 __pass_to_client(sym, FUNC_DEF_HOOK);
1027 __pass_to_client(sym, AFTER_DEF_HOOK);
1028 __split_stmt(base_type->stmt);
1029 __split_stmt(base_type->inline_stmt);
1030 nullify_path();
1032 __unnullify_path();
1033 loop_num = 0;
1034 final_pass = 1;
1035 __pass_to_client(sym, FUNC_DEF_HOOK);
1036 __pass_to_client(sym, AFTER_DEF_HOOK);
1037 __split_stmt(base_type->stmt);
1038 __split_stmt(base_type->inline_stmt);
1039 __pass_to_client(sym, END_FUNC_HOOK);
1040 __pass_to_client(sym, AFTER_FUNC_HOOK);
1041 cur_func = NULL;
1042 clear_all_states();
1043 free_data_info_allocs();
1044 free_expression_stack(&switch_expr_stack);
1045 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1046 __bail_on_rest_of_function = 0;
1049 static void parse_inline(struct expression *call)
1051 struct symbol *base_type;
1052 int loop_num_bak = loop_num;
1053 int final_pass_bak = final_pass;
1054 char *cur_func_bak = cur_func;
1055 struct statement_list *big_statement_stack_bak = big_statement_stack;
1056 struct expression_list *big_expression_stack_bak = big_expression_stack;
1057 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1058 struct symbol *cur_func_sym_bak = cur_func_sym;
1060 __pass_to_client(call, INLINE_FN_START);
1061 final_pass = 0; /* don't print anything */
1062 __inline_fn = call;
1064 base_type = get_base_type(call->fn->symbol);
1065 cur_func_sym = call->fn->symbol;
1066 if (call->fn->symbol->ident)
1067 cur_func = call->fn->symbol->ident->name;
1068 else
1069 cur_func = NULL;
1070 set_position(call->fn->symbol->pos);
1072 save_all_states();
1073 nullify_all_states();
1074 big_statement_stack = NULL;
1075 big_expression_stack = NULL;
1076 switch_expr_stack = NULL;
1078 sm_debug("inline function: %s\n", cur_func);
1079 __unnullify_path();
1080 loop_num = 0;
1081 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
1082 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
1083 __split_stmt(base_type->stmt);
1084 __split_stmt(base_type->inline_stmt);
1085 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1086 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1088 free_expression_stack(&switch_expr_stack);
1089 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1090 nullify_path();
1092 loop_num = loop_num_bak;
1093 final_pass = final_pass_bak;
1094 cur_func_sym = cur_func_sym_bak;
1095 cur_func = cur_func_bak;
1096 big_statement_stack = big_statement_stack_bak;
1097 big_expression_stack = big_expression_stack_bak;
1098 switch_expr_stack = switch_expr_stack_bak;
1100 restore_all_states();
1101 set_position(call->pos);
1102 __inline_fn = NULL;
1103 __pass_to_client(call, INLINE_FN_END);
1106 static struct symbol_list *inlines_called;
1107 static void add_inline_function(struct symbol *sym)
1109 static struct symbol_list *already_added;
1110 struct symbol *tmp;
1112 FOR_EACH_PTR(already_added, tmp) {
1113 if (tmp == sym)
1114 return;
1115 } END_FOR_EACH_PTR(tmp);
1117 add_ptr_list(&already_added, sym);
1118 add_ptr_list(&inlines_called, sym);
1121 static void process_inlines()
1123 struct symbol *tmp;
1125 FOR_EACH_PTR(inlines_called, tmp) {
1126 split_function(tmp);
1127 } END_FOR_EACH_PTR(tmp);
1128 free_ptr_list(&inlines_called);
1131 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1133 struct symbol *sym;
1135 FOR_EACH_PTR_REVERSE(big_list, sym) {
1136 if (!sym->scope)
1137 continue;
1138 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1139 return sym;
1140 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1141 return sym;
1142 } END_FOR_EACH_PTR_REVERSE(sym);
1144 return NULL;
1147 static void split_inlines_in_scope(struct symbol *sym)
1149 struct symbol *base;
1150 struct symbol_list *scope_list;
1151 int stream;
1153 scope_list = sym->scope->symbols;
1154 stream = sym->pos.stream;
1156 /* find the last static symbol in the file */
1157 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1158 if (sym->pos.stream != stream)
1159 continue;
1160 if (sym->type != SYM_NODE)
1161 continue;
1162 base = get_base_type(sym);
1163 if (!base)
1164 continue;
1165 if (base->type != SYM_FN)
1166 continue;
1167 if (!base->inline_stmt)
1168 continue;
1169 add_inline_function(sym);
1170 } END_FOR_EACH_PTR_REVERSE(sym);
1172 process_inlines();
1175 static void split_inlines(struct symbol_list *sym_list)
1177 struct symbol *sym;
1179 sym = get_last_scoped_symbol(sym_list, 0);
1180 if (sym)
1181 split_inlines_in_scope(sym);
1182 sym = get_last_scoped_symbol(sym_list, 1);
1183 if (sym)
1184 split_inlines_in_scope(sym);
1187 static void split_functions(struct symbol_list *sym_list)
1189 struct symbol *sym;
1191 FOR_EACH_PTR(sym_list, sym) {
1192 set_position(sym->pos);
1193 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1194 split_function(sym);
1195 process_inlines();
1196 } else {
1197 __pass_to_client(sym, BASE_HOOK);
1198 fake_global_assign(sym);
1200 } END_FOR_EACH_PTR(sym);
1201 split_inlines(sym_list);
1202 __pass_to_client(sym_list, END_FILE_HOOK);
1205 void smatch(int argc, char **argv)
1208 struct string_list *filelist = NULL;
1209 struct symbol_list *sym_list;
1211 if (argc < 2) {
1212 printf("Usage: smatch [--debug] <filename.c>\n");
1213 exit(1);
1215 sparse_initialize(argc, argv, &filelist);
1216 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1217 if (option_file_output) {
1218 char buf[256];
1220 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1221 sm_outfd = fopen(buf, "w");
1222 if (!sm_outfd) {
1223 printf("Error: Cannot open %s\n", base_file);
1224 exit(1);
1227 sym_list = sparse_keep_tokens(base_file);
1228 split_functions(sym_list);
1229 } END_FOR_EACH_PTR_NOTAG(base_file);