parse: add a big hack to track packed structs
[smatch.git] / smatch_flow.c
blob791b4da7010644978e07bcb6a1c7e6933f849ba4
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 typedef void (fake_cb)(struct expression *expr);
798 static int member_to_number(struct expression *expr, struct ident *member)
800 struct symbol *type, *tmp;
801 char *name;
802 int i;
804 if (!member)
805 return -1;
806 name = member->name;
808 type = get_type(expr);
809 if (!type || type->type != SYM_STRUCT)
810 return -1;
812 i = -1;
813 FOR_EACH_PTR(type->symbol_list, tmp) {
814 i++;
815 if (!tmp->ident)
816 continue;
817 if (strcmp(name, tmp->ident->name) == 0)
818 return i;
819 } END_FOR_EACH_PTR(tmp);
820 return -1;
823 static struct ident *number_to_member(struct expression *expr, int num)
825 struct symbol *type, *member;
826 int i = 0;
828 type = get_type(expr);
829 if (!type || type->type != SYM_STRUCT)
830 return NULL;
832 FOR_EACH_PTR(type->symbol_list, member) {
833 if (i == num)
834 return member->ident;
835 i++;
836 } END_FOR_EACH_PTR(member);
837 return NULL;
840 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
842 struct expression *deref, *assign, *tmp;
843 struct symbol *type;
844 struct ident *member;
845 int member_idx = 0;
847 type = get_type(symbol);
848 if (!type || type->type != SYM_STRUCT)
849 return;
851 FOR_EACH_PTR(members, tmp) {
852 if (tmp->type == EXPR_IDENTIFIER) {
853 member = tmp->expr_ident;
854 member_idx = member_to_number(symbol, member);
855 tmp = tmp->ident_expression;
856 } else {
857 member = number_to_member(symbol, member_idx);
859 member_idx++;
860 deref = member_expression(symbol, '.', member);
861 if (tmp->type == EXPR_INITIALIZER) {
862 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
863 } else {
864 assign = assign_expression(deref, tmp);
865 fake_cb(assign);
867 } END_FOR_EACH_PTR(tmp);
870 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
872 fake_member_assigns_helper(symbol_expression(sym),
873 sym->initializer->expr_list, fake_cb);
876 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
878 struct expression *array, *offset, *binop, *assign, *tmp;
879 int idx;
881 array = symbol_expression(sym);
882 idx = 0;
883 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
884 if (tmp->type == EXPR_INDEX) {
885 if (tmp->idx_from != tmp->idx_to)
886 return;
887 idx = tmp->idx_from;
888 if (!tmp->idx_expression)
889 goto next;
890 tmp = tmp->idx_expression;
892 offset = value_expr(idx);
893 binop = array_element_expression(array, offset);
894 if (tmp->type == EXPR_INITIALIZER) {
895 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
896 } else {
897 assign = assign_expression(binop, tmp);
898 fake_cb(assign);
900 next:
901 idx++;
902 } END_FOR_EACH_PTR(tmp);
905 static void fake_assign_expr(struct symbol *sym)
907 struct expression *assign, *symbol;
909 symbol = symbol_expression(sym);
910 assign = assign_expression(symbol, sym->initializer);
911 __split_expr(assign);
914 static void call_split_expr(struct expression *expr)
916 __split_expr(expr);
919 static void do_initializer_stuff(struct symbol *sym)
921 if (!sym->initializer)
922 return;
924 if (sym->initializer->type == EXPR_INITIALIZER) {
925 if (get_real_base_type(sym)->type == SYM_ARRAY)
926 fake_element_assigns(sym, call_split_expr);
927 else
928 fake_member_assigns(sym, call_split_expr);
929 } else {
930 fake_assign_expr(sym);
934 static void split_declaration(struct symbol_list *sym_list)
936 struct symbol *sym;
938 FOR_EACH_PTR(sym_list, sym) {
939 __pass_to_client(sym, DECLARATION_HOOK);
940 do_initializer_stuff(sym);
941 split_sym(sym);
942 } END_FOR_EACH_PTR(sym);
945 static void call_global_assign_hooks(struct expression *assign)
947 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
950 static void fake_global_assign(struct symbol *sym)
952 struct expression *assign, *symbol;
954 if (!sym->initializer)
955 return;
956 if (sym->initializer->type == EXPR_INITIALIZER) {
957 if (get_real_base_type(sym)->type == SYM_ARRAY)
958 fake_element_assigns(sym, call_global_assign_hooks);
959 else
960 fake_member_assigns(sym, call_global_assign_hooks);
961 } else {
962 symbol = symbol_expression(sym);
963 assign = assign_expression(symbol, sym->initializer);
964 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
968 static void split_function(struct symbol *sym)
970 struct symbol *base_type = get_base_type(sym);
972 cur_func_sym = sym;
973 if (sym->ident)
974 cur_func = sym->ident->name;
975 __smatch_lineno = sym->pos.line;
976 last_stmt = NULL;
977 loop_count = 0;
978 sm_debug("new function: %s\n", cur_func);
979 __slist_id = 0;
980 if (option_two_passes) {
981 __unnullify_path();
982 loop_num = 0;
983 final_pass = 0;
984 __pass_to_client(sym, FUNC_DEF_HOOK);
985 __pass_to_client(sym, AFTER_DEF_HOOK);
986 __split_stmt(base_type->stmt);
987 __split_stmt(base_type->inline_stmt);
988 nullify_path();
990 __unnullify_path();
991 loop_num = 0;
992 final_pass = 1;
993 __pass_to_client(sym, FUNC_DEF_HOOK);
994 __pass_to_client(sym, AFTER_DEF_HOOK);
995 __split_stmt(base_type->stmt);
996 __split_stmt(base_type->inline_stmt);
997 __pass_to_client(sym, END_FUNC_HOOK);
998 cur_func = NULL;
999 clear_all_states();
1000 free_data_info_allocs();
1001 free_expression_stack(&switch_expr_stack);
1002 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1003 __bail_on_rest_of_function = 0;
1006 static void parse_inline(struct expression *call)
1008 struct symbol *base_type;
1009 int loop_num_bak = loop_num;
1010 int final_pass_bak = final_pass;
1011 char *cur_func_bak = cur_func;
1012 struct statement_list *big_statement_stack_bak = big_statement_stack;
1013 struct expression_list *big_expression_stack_bak = big_expression_stack;
1014 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1015 struct symbol *cur_func_sym_bak = cur_func_sym;
1017 __pass_to_client(call, INLINE_FN_START);
1018 final_pass = 0; /* don't print anything */
1019 __inline_fn = call;
1021 base_type = get_base_type(call->fn->symbol);
1022 cur_func_sym = call->fn->symbol;
1023 if (call->fn->symbol->ident)
1024 cur_func = call->fn->symbol->ident->name;
1025 else
1026 cur_func = NULL;
1027 set_position(call->fn->symbol->pos);
1029 save_all_states();
1030 nullify_all_states();
1031 big_statement_stack = NULL;
1032 big_expression_stack = NULL;
1033 switch_expr_stack = NULL;
1035 sm_debug("inline function: %s\n", cur_func);
1036 __unnullify_path();
1037 loop_num = 0;
1038 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
1039 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
1040 __split_stmt(base_type->stmt);
1041 __split_stmt(base_type->inline_stmt);
1042 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1044 free_expression_stack(&switch_expr_stack);
1045 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1046 nullify_path();
1048 loop_num = loop_num_bak;
1049 final_pass = final_pass_bak;
1050 cur_func_sym = cur_func_sym_bak;
1051 cur_func = cur_func_bak;
1052 big_statement_stack = big_statement_stack_bak;
1053 big_expression_stack = big_expression_stack_bak;
1054 switch_expr_stack = switch_expr_stack_bak;
1056 restore_all_states();
1057 set_position(call->pos);
1058 __inline_fn = NULL;
1059 __pass_to_client(call, INLINE_FN_END);
1062 static struct symbol_list *inlines_called;
1063 static void add_inline_function(struct symbol *sym)
1065 static struct symbol_list *already_added;
1066 struct symbol *tmp;
1068 FOR_EACH_PTR(already_added, tmp) {
1069 if (tmp == sym)
1070 return;
1071 } END_FOR_EACH_PTR(tmp);
1073 add_ptr_list(&already_added, sym);
1074 add_ptr_list(&inlines_called, sym);
1077 static void process_inlines()
1079 struct symbol *tmp;
1081 FOR_EACH_PTR(inlines_called, tmp) {
1082 split_function(tmp);
1083 } END_FOR_EACH_PTR(tmp);
1084 free_ptr_list(&inlines_called);
1087 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list)
1089 struct symbol *sym;
1091 FOR_EACH_PTR_REVERSE(big_list, sym) {
1092 if (!sym->scope)
1093 continue;
1094 return sym;
1095 } END_FOR_EACH_PTR_REVERSE(sym);
1097 return NULL;
1100 static void split_inlines(struct symbol_list *sym_list)
1102 struct symbol *sym, *base;
1103 struct symbol_list *scope_list;
1104 int stream;
1106 sym = get_last_scoped_symbol(sym_list);
1107 if (!sym)
1108 return;
1109 scope_list = sym->scope->symbols;
1110 stream = sym->pos.stream;
1112 /* find the last static symbol in the file */
1113 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1114 if (sym->pos.stream != stream)
1115 continue;
1116 if (sym->type != SYM_NODE)
1117 continue;
1118 base = get_base_type(sym);
1119 if (!base)
1120 continue;
1121 if (base->type != SYM_FN)
1122 continue;
1123 if (!base->inline_stmt)
1124 continue;
1125 add_inline_function(sym);
1126 } END_FOR_EACH_PTR_REVERSE(sym);
1128 process_inlines();
1131 static void split_functions(struct symbol_list *sym_list)
1133 struct symbol *sym;
1135 FOR_EACH_PTR(sym_list, sym) {
1136 set_position(sym->pos);
1137 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1138 split_function(sym);
1139 process_inlines();
1140 } else {
1141 __pass_to_client(sym, BASE_HOOK);
1142 fake_global_assign(sym);
1144 } END_FOR_EACH_PTR(sym);
1145 split_inlines(sym_list);
1146 __pass_to_client(sym_list, END_FILE_HOOK);
1149 void smatch(int argc, char **argv)
1152 struct string_list *filelist = NULL;
1153 struct symbol_list *sym_list;
1155 if (argc < 2) {
1156 printf("Usage: smatch [--debug] <filename.c>\n");
1157 exit(1);
1159 sparse_initialize(argc, argv, &filelist);
1160 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1161 if (option_file_output) {
1162 char buf[256];
1164 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1165 sm_outfd = fopen(buf, "w");
1166 if (!sm_outfd) {
1167 printf("Error: Cannot open %s\n", base_file);
1168 exit(1);
1171 sym_list = sparse_keep_tokens(base_file);
1172 split_functions(sym_list);
1173 } END_FOR_EACH_PTR_NOTAG(base_file);