debug: add __smatch_member_name()
[smatch.git] / smatch_flow.c
blob89fc4b815f78233e2d1d21150b8fe1a205643eae
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 int __in_function_def;
34 static struct expression_list *switch_expr_stack = NULL;
35 static struct expression_list *post_op_stack = NULL;
37 struct expression_list *big_expression_stack;
38 struct statement_list *big_statement_stack;
39 int __in_pre_condition = 0;
40 int __bail_on_rest_of_function = 0;
41 char *get_function(void) { return cur_func; }
42 int get_lineno(void) { return __smatch_lineno; }
43 int inside_loop(void) { return !!loop_count; }
44 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
45 int in_expression_statement(void) { return !!__expr_stmt_count; }
47 static void split_symlist(struct symbol_list *sym_list);
48 static void split_declaration(struct symbol_list *sym_list);
49 static void split_expr_list(struct expression_list *expr_list);
50 static void add_inline_function(struct symbol *sym);
51 static void parse_inline(struct expression *expr);
53 int option_assume_loops = 0;
54 int option_known_conditions = 0;
55 int option_two_passes = 0;
56 struct symbol *cur_func_sym = NULL;
58 int outside_of_function(void)
60 return cur_func_sym == NULL;
63 const char *get_filename(void)
65 if (option_info)
66 return base_file;
67 if (option_full_path)
68 return full_filename;
69 return filename;
72 const char *get_base_file(void)
74 return base_file;
77 static void set_position(struct position pos)
79 int len;
80 static int prev_stream = -1;
82 if (pos.stream == 0 && pos.line == 0)
83 return;
85 __smatch_lineno = pos.line;
87 if (pos.stream == prev_stream)
88 return;
90 filename = stream_name(pos.stream);
92 free(full_filename);
93 pathname = getcwd(NULL, 0);
94 if (pathname) {
95 len = strlen(pathname) + 1 + strlen(filename) + 1;
96 full_filename = malloc(len);
97 snprintf(full_filename, len, "%s/%s", pathname, filename);
98 } else {
99 full_filename = alloc_string(filename);
101 free(pathname);
104 static int is_inline_func(struct expression *expr)
106 if (expr->type != EXPR_SYMBOL || !expr->symbol)
107 return 0;
108 if (expr->symbol->ctype.modifiers & MOD_INLINE)
109 return 1;
110 return 0;
113 static int is_noreturn_func(struct expression *expr)
115 if (expr->type != EXPR_SYMBOL || !expr->symbol)
116 return 0;
117 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
118 return 1;
119 return 0;
122 int inlinable(struct expression *expr)
124 struct symbol *sym;
126 if (__inline_fn) /* don't nest */
127 return 0;
129 if (expr->type != EXPR_SYMBOL || !expr->symbol)
130 return 0;
131 if (is_no_inline_function(expr->symbol->ident->name))
132 return 0;
133 sym = get_base_type(expr->symbol);
134 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
135 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
136 return 1;
137 return 0;
139 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
140 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
141 return 1;
142 return 0;
144 return 0;
147 void __process_post_op_stack(void)
149 struct expression *expr;
151 FOR_EACH_PTR(post_op_stack, expr) {
152 __pass_to_client(expr, OP_HOOK);
153 } END_FOR_EACH_PTR(expr);
155 __free_ptr_list((struct ptr_list **)&post_op_stack);
158 void __split_expr(struct expression *expr)
160 if (!expr)
161 return;
163 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
165 push_expression(&big_expression_stack, expr);
166 set_position(expr->pos);
167 __pass_to_client(expr, EXPR_HOOK);
169 switch (expr->type) {
170 case EXPR_PREOP:
171 if (expr->op == '*')
172 __pass_to_client(expr, DEREF_HOOK);
173 __split_expr(expr->unop);
174 __pass_to_client(expr, OP_HOOK);
175 break;
176 case EXPR_POSTOP:
177 __split_expr(expr->unop);
178 push_expression(&post_op_stack, expr);
179 break;
180 case EXPR_STATEMENT:
181 __expr_stmt_count++;
182 __split_stmt(expr->statement);
183 __expr_stmt_count--;
184 break;
185 case EXPR_LOGICAL:
186 case EXPR_COMPARE:
187 __pass_to_client(expr, LOGIC_HOOK);
188 __handle_logic(expr);
189 break;
190 case EXPR_BINOP:
191 __pass_to_client(expr, BINOP_HOOK);
192 case EXPR_COMMA:
193 __split_expr(expr->left);
194 __process_post_op_stack();
195 __split_expr(expr->right);
196 break;
197 case EXPR_ASSIGNMENT: {
198 struct expression *tmp;
200 if (!expr->right)
201 break;
203 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
205 /* foo = !bar() */
206 if (__handle_condition_assigns(expr))
207 break;
208 /* foo = (x < 5 ? foo : 5); */
209 if (__handle_select_assigns(expr))
210 break;
211 /* foo = ({frob(); frob(); frob(); 1;}) */
212 if (__handle_expr_statement_assigns(expr))
213 break;
215 __split_expr(expr->right);
216 if (outside_of_function())
217 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
218 else
219 __pass_to_client(expr, ASSIGNMENT_HOOK);
220 tmp = strip_expr(expr->right);
221 if (tmp->type == EXPR_CALL)
222 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
223 if (get_macro_name(tmp->pos) &&
224 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
225 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
226 __split_expr(expr->left);
227 break;
229 case EXPR_DEREF:
230 __pass_to_client(expr, DEREF_HOOK);
231 __split_expr(expr->deref);
232 break;
233 case EXPR_SLICE:
234 __split_expr(expr->base);
235 break;
236 case EXPR_CAST:
237 case EXPR_FORCE_CAST:
238 __pass_to_client(expr, CAST_HOOK);
239 __split_expr(expr->cast_expression);
240 break;
241 case EXPR_SIZEOF:
242 if (expr->cast_expression)
243 __pass_to_client(strip_parens(expr->cast_expression),
244 SIZEOF_HOOK);
245 break;
246 case EXPR_OFFSETOF:
247 case EXPR_ALIGNOF:
248 evaluate_expression(expr);
249 break;
250 case EXPR_CONDITIONAL:
251 case EXPR_SELECT:
252 if (known_condition_true(expr->conditional)) {
253 __split_expr(expr->cond_true);
254 break;
256 if (known_condition_false(expr->conditional)) {
257 __split_expr(expr->cond_false);
258 break;
260 __pass_to_client(expr, SELECT_HOOK);
261 __split_whole_condition(expr->conditional);
262 __split_expr(expr->cond_true);
263 __push_true_states();
264 __use_false_states();
265 __split_expr(expr->cond_false);
266 __merge_true_states();
267 break;
268 case EXPR_CALL:
269 if (sym_name_is("__builtin_constant_p", expr->fn))
270 break;
271 split_expr_list(expr->args);
272 __split_expr(expr->fn);
273 if (is_inline_func(expr->fn))
274 add_inline_function(expr->fn->symbol);
275 if (inlinable(expr->fn))
276 __inline_call = 1;
277 __process_post_op_stack();
278 __pass_to_client(expr, FUNCTION_CALL_HOOK);
279 __inline_call = 0;
280 if (inlinable(expr->fn)) {
281 parse_inline(expr);
283 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
284 if (is_noreturn_func(expr->fn))
285 nullify_path();
286 break;
287 case EXPR_INITIALIZER:
288 split_expr_list(expr->expr_list);
289 break;
290 case EXPR_IDENTIFIER:
291 __split_expr(expr->ident_expression);
292 break;
293 case EXPR_INDEX:
294 __split_expr(expr->idx_expression);
295 break;
296 case EXPR_POS:
297 __split_expr(expr->init_expr);
298 break;
299 case EXPR_SYMBOL:
300 __pass_to_client(expr, SYM_HOOK);
301 break;
302 case EXPR_STRING:
303 __pass_to_client(expr, STRING_HOOK);
304 break;
305 default:
306 break;
308 pop_expression(&big_expression_stack);
311 static int is_forever_loop(struct statement *stmt)
313 struct expression *expr;
315 expr = strip_expr(stmt->iterator_pre_condition);
316 if (!expr)
317 expr = stmt->iterator_post_condition;
318 if (!expr) {
319 /* this is a for(;;) loop... */
320 return 1;
323 if (expr->type == EXPR_VALUE && expr->value == 1)
324 return 1;
326 return 0;
329 static int loop_num;
330 static char *get_loop_name(int num)
332 char buf[256];
334 snprintf(buf, 255, "-loop%d", num);
335 buf[255] = '\0';
336 return alloc_sname(buf);
340 * Pre Loops are while and for loops.
342 static void handle_pre_loop(struct statement *stmt)
344 int once_through; /* we go through the loop at least once */
345 struct sm_state *extra_sm = NULL;
346 int unchanged = 0;
347 char *loop_name;
348 struct state_list *slist = NULL;
349 struct sm_state *sm = NULL;
351 loop_name = get_loop_name(loop_num);
352 loop_num++;
354 __split_stmt(stmt->iterator_pre_statement);
356 once_through = implied_condition_true(stmt->iterator_pre_condition);
358 loop_count++;
359 __push_continues();
360 __push_breaks();
362 __merge_gotos(loop_name);
364 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
365 __in_pre_condition++;
366 __pass_to_client(stmt, PRELOOP_HOOK);
367 __split_whole_condition(stmt->iterator_pre_condition);
368 __in_pre_condition--;
369 FOR_EACH_PTR(slist, sm) {
370 set_state(sm->owner, sm->name, sm->sym, sm->state);
371 } END_FOR_EACH_PTR(sm);
372 free_slist(&slist);
373 if (extra_sm)
374 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
376 if (option_assume_loops)
377 once_through = 1;
379 __split_stmt(stmt->iterator_statement);
380 __warn_on_silly_pre_loops();
381 if (is_forever_loop(stmt)) {
382 struct state_list *slist;
384 __save_gotos(loop_name);
386 __push_fake_cur_slist();
387 __split_stmt(stmt->iterator_post_statement);
388 slist = __pop_fake_cur_slist();
390 __discard_continues();
391 __discard_false_states();
392 __use_breaks();
394 if (!__path_is_null())
395 __merge_slist_into_cur(slist);
396 free_slist(&slist);
397 } else {
398 __merge_continues();
399 unchanged = __iterator_unchanged(extra_sm);
400 __split_stmt(stmt->iterator_post_statement);
401 __save_gotos(loop_name);
402 __split_whole_condition(stmt->iterator_pre_condition);
403 nullify_path();
404 __merge_false_states();
405 if (once_through)
406 __discard_false_states();
407 else
408 __merge_false_states();
410 if (extra_sm && unchanged)
411 __extra_pre_loop_hook_after(extra_sm,
412 stmt->iterator_post_statement,
413 stmt->iterator_pre_condition);
414 __merge_breaks();
416 loop_count--;
420 * Post loops are do {} while();
422 static void handle_post_loop(struct statement *stmt)
424 char *loop_name;
426 loop_name = get_loop_name(loop_num);
427 loop_num++;
428 loop_count++;
430 __push_continues();
431 __push_breaks();
432 __merge_gotos(loop_name);
433 __split_stmt(stmt->iterator_statement);
434 __merge_continues();
435 if (!is_zero(stmt->iterator_post_condition))
436 __save_gotos(loop_name);
438 if (is_forever_loop(stmt)) {
439 __use_breaks();
440 } else {
441 __split_whole_condition(stmt->iterator_post_condition);
442 __use_false_states();
443 __merge_breaks();
445 loop_count--;
448 static int empty_statement(struct statement *stmt)
450 if (!stmt)
451 return 0;
452 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
453 return 1;
454 return 0;
457 static int last_stmt_on_same_line()
459 struct statement *stmt;
460 int i = 0;
462 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
463 if (!i++)
464 continue;
465 if (stmt->pos.line == get_lineno())
466 return 1;
467 return 0;
468 } END_FOR_EACH_PTR_REVERSE(stmt);
469 return 0;
472 static struct statement *last_stmt;
473 static int is_last_stmt(struct statement *stmt)
475 if (stmt == last_stmt)
476 return 1;
477 return 0;
480 static void print_unreached_initializers(struct symbol_list *sym_list)
482 struct symbol *sym;
484 FOR_EACH_PTR(sym_list, sym) {
485 if (sym->initializer)
486 sm_msg("info: '%s' is not actually initialized (unreached code).",
487 (sym->ident ? sym->ident->name : "this variable"));
488 } END_FOR_EACH_PTR(sym);
491 static void print_unreached(struct statement *stmt)
493 static int print = 1;
495 if (__inline_fn)
496 return;
498 if (!__path_is_null()) {
499 print = 1;
500 return;
502 if (!print)
503 return;
505 switch (stmt->type) {
506 case STMT_COMPOUND: /* after a switch before a case stmt */
507 case STMT_RANGE:
508 case STMT_CASE:
509 case STMT_LABEL:
510 return;
511 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
512 print_unreached_initializers(stmt->declaration);
513 return;
514 case STMT_RETURN: /* gcc complains if you don't have a return statement */
515 if (is_last_stmt(stmt))
516 return;
517 break;
518 case STMT_GOTO:
519 if (!option_spammy)
520 return;
521 break;
522 default:
523 break;
525 if (!option_spammy && empty_statement(stmt))
526 return;
527 sm_msg("info: ignoring unreachable code.");
528 print = 0;
531 static void split_asm_constraints(struct expression_list *expr_list)
533 struct expression *expr;
534 int state = 0;
536 FOR_EACH_PTR(expr_list, expr) {
537 switch (state) {
538 case 0: /* identifier */
539 case 1: /* constraint */
540 state++;
541 continue;
542 case 2: /* expression */
543 state = 0;
544 __split_expr(expr);
545 continue;
547 } END_FOR_EACH_PTR(expr);
550 static int is_case_val(struct statement *stmt, sval_t sval)
552 sval_t case_sval;
554 if (stmt->type != STMT_CASE)
555 return 0;
556 if (!stmt->case_expression) {
557 __set_default();
558 return 1;
560 if (!get_value(stmt->case_expression, &case_sval))
561 return 0;
562 if (case_sval.value == sval.value)
563 return 1;
564 return 0;
567 static void split_known_switch(struct statement *stmt, sval_t sval)
569 struct statement *tmp;
571 __split_expr(stmt->switch_expression);
573 push_expression(&switch_expr_stack, stmt->switch_expression);
574 __save_switch_states(top_expression(switch_expr_stack));
575 nullify_path();
576 __push_default();
577 __push_breaks();
579 stmt = stmt->switch_statement;
581 if (!last_stmt)
582 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
584 __push_scope_hooks();
585 FOR_EACH_PTR(stmt->stmts, tmp) {
586 __smatch_lineno = tmp->pos.line;
587 if (is_case_val(tmp, sval)) {
588 __merge_switches(top_expression(switch_expr_stack),
589 stmt->case_expression);
590 __pass_case_to_client(top_expression(switch_expr_stack),
591 stmt->case_expression);
593 if (__path_is_null())
594 continue;
595 __split_stmt(tmp);
596 if (__path_is_null()) {
597 __set_default();
598 goto out;
600 } END_FOR_EACH_PTR(tmp);
601 out:
602 __call_scope_hooks();
603 if (!__pop_default())
604 __merge_switches(top_expression(switch_expr_stack),
605 NULL);
606 __discard_switches();
607 __merge_breaks();
608 pop_expression(&switch_expr_stack);
611 void __split_stmt(struct statement *stmt)
613 sval_t sval;
615 if (!stmt)
616 goto out;
618 if (out_of_memory() || __bail_on_rest_of_function) {
619 static char *printed = NULL;
621 if (printed != cur_func)
622 sm_msg("Function too hairy. Giving up.");
623 final_pass = 0; /* turn off sm_msg() from here */
624 printed = cur_func;
625 return;
628 add_ptr_list(&big_statement_stack, stmt);
629 free_expression_stack(&big_expression_stack);
630 set_position(stmt->pos);
631 print_unreached(stmt);
632 __pass_to_client(stmt, STMT_HOOK);
634 switch (stmt->type) {
635 case STMT_DECLARATION:
636 split_declaration(stmt->declaration);
637 break;
638 case STMT_RETURN:
639 __split_expr(stmt->ret_value);
640 __pass_to_client(stmt->ret_value, RETURN_HOOK);
641 nullify_path();
642 break;
643 case STMT_EXPRESSION:
644 __split_expr(stmt->expression);
645 break;
646 case STMT_COMPOUND: {
647 struct statement *tmp;
649 if (!last_stmt)
650 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
651 __push_scope_hooks();
652 FOR_EACH_PTR(stmt->stmts, tmp) {
653 __split_stmt(tmp);
654 } END_FOR_EACH_PTR(tmp);
655 __call_scope_hooks();
656 break;
658 case STMT_IF:
659 if (known_condition_true(stmt->if_conditional)) {
660 __split_stmt(stmt->if_true);
661 break;
663 if (known_condition_false(stmt->if_conditional)) {
664 __split_stmt(stmt->if_false);
665 break;
667 if (option_known_conditions &&
668 implied_condition_true(stmt->if_conditional)) {
669 sm_info("this condition is true.");
670 __split_stmt(stmt->if_true);
671 break;
673 if (option_known_conditions &&
674 implied_condition_false(stmt->if_conditional)) {
675 sm_info("this condition is false.");
676 __split_stmt(stmt->if_false);
677 break;
679 __split_whole_condition(stmt->if_conditional);
680 __split_stmt(stmt->if_true);
681 if (empty_statement(stmt->if_true) &&
682 last_stmt_on_same_line() &&
683 !get_macro_name(stmt->if_true->pos))
684 sm_msg("warn: if();");
685 __push_true_states();
686 __use_false_states();
687 __split_stmt(stmt->if_false);
688 __merge_true_states();
689 break;
690 case STMT_ITERATOR:
691 if (stmt->iterator_pre_condition)
692 handle_pre_loop(stmt);
693 else if (stmt->iterator_post_condition)
694 handle_post_loop(stmt);
695 else {
696 // these are for(;;) type loops.
697 handle_pre_loop(stmt);
699 break;
700 case STMT_SWITCH:
701 if (get_value(stmt->switch_expression, &sval)) {
702 split_known_switch(stmt, sval);
703 break;
705 __split_expr(stmt->switch_expression);
706 push_expression(&switch_expr_stack, stmt->switch_expression);
707 __save_switch_states(top_expression(switch_expr_stack));
708 nullify_path();
709 __push_default();
710 __push_breaks();
711 __split_stmt(stmt->switch_statement);
712 if (!__pop_default())
713 __merge_switches(top_expression(switch_expr_stack),
714 NULL);
715 __discard_switches();
716 __merge_breaks();
717 pop_expression(&switch_expr_stack);
718 break;
719 case STMT_CASE:
720 __merge_switches(top_expression(switch_expr_stack),
721 stmt->case_expression);
722 __pass_case_to_client(top_expression(switch_expr_stack),
723 stmt->case_expression);
724 if (!stmt->case_expression)
725 __set_default();
726 __split_expr(stmt->case_expression);
727 __split_expr(stmt->case_to);
728 __split_stmt(stmt->case_statement);
729 break;
730 case STMT_LABEL:
731 if (stmt->label_identifier &&
732 stmt->label_identifier->type == SYM_LABEL &&
733 stmt->label_identifier->ident) {
734 loop_count = 1000000;
735 __merge_gotos(stmt->label_identifier->ident->name);
737 __split_stmt(stmt->label_statement);
738 break;
739 case STMT_GOTO:
740 __split_expr(stmt->goto_expression);
741 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
742 if (!strcmp(stmt->goto_label->ident->name, "break")) {
743 __process_breaks();
744 } else if (!strcmp(stmt->goto_label->ident->name,
745 "continue")) {
746 __process_continues();
748 } else if (stmt->goto_label &&
749 stmt->goto_label->type == SYM_LABEL &&
750 stmt->goto_label->ident) {
751 __save_gotos(stmt->goto_label->ident->name);
753 nullify_path();
754 break;
755 case STMT_NONE:
756 break;
757 case STMT_ASM:
758 __pass_to_client(stmt, ASM_HOOK);
759 __split_expr(stmt->asm_string);
760 split_asm_constraints(stmt->asm_outputs);
761 split_asm_constraints(stmt->asm_inputs);
762 split_asm_constraints(stmt->asm_clobbers);
763 break;
764 case STMT_CONTEXT:
765 break;
766 case STMT_RANGE:
767 __split_expr(stmt->range_expression);
768 __split_expr(stmt->range_low);
769 __split_expr(stmt->range_high);
770 break;
772 out:
773 __process_post_op_stack();
776 static void split_expr_list(struct expression_list *expr_list)
778 struct expression *expr;
780 FOR_EACH_PTR(expr_list, expr) {
781 __split_expr(expr);
782 __process_post_op_stack();
783 } END_FOR_EACH_PTR(expr);
786 static void split_sym(struct symbol *sym)
788 if (!sym)
789 return;
790 if (!(sym->namespace & NS_SYMBOL))
791 return;
793 __split_stmt(sym->stmt);
794 __split_expr(sym->array_size);
795 split_symlist(sym->arguments);
796 split_symlist(sym->symbol_list);
797 __split_stmt(sym->inline_stmt);
798 split_symlist(sym->inline_symbol_list);
801 static void split_symlist(struct symbol_list *sym_list)
803 struct symbol *sym;
805 FOR_EACH_PTR(sym_list, sym) {
806 split_sym(sym);
807 } END_FOR_EACH_PTR(sym);
810 typedef void (fake_cb)(struct expression *expr);
812 static int member_to_number(struct expression *expr, struct ident *member)
814 struct symbol *type, *tmp;
815 char *name;
816 int i;
818 if (!member)
819 return -1;
820 name = member->name;
822 type = get_type(expr);
823 if (!type || type->type != SYM_STRUCT)
824 return -1;
826 i = -1;
827 FOR_EACH_PTR(type->symbol_list, tmp) {
828 i++;
829 if (!tmp->ident)
830 continue;
831 if (strcmp(name, tmp->ident->name) == 0)
832 return i;
833 } END_FOR_EACH_PTR(tmp);
834 return -1;
837 static struct ident *number_to_member(struct expression *expr, int num)
839 struct symbol *type, *member;
840 int i = 0;
842 type = get_type(expr);
843 if (!type || type->type != SYM_STRUCT)
844 return NULL;
846 FOR_EACH_PTR(type->symbol_list, member) {
847 if (i == num)
848 return member->ident;
849 i++;
850 } END_FOR_EACH_PTR(member);
851 return NULL;
854 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
856 struct member_set {
857 struct ident *ident;
858 int set;
861 static struct member_set *alloc_member_set(struct symbol *type)
863 struct member_set *member_set;
864 struct symbol *member;
865 int member_count;
866 int member_idx;
868 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
869 member_set = malloc(member_count * sizeof(*member_set));
870 member_idx = 0;
871 FOR_EACH_PTR(type->symbol_list, member) {
872 member_set[member_idx].ident = member->ident;
873 member_set[member_idx].set = 0;
874 member_idx++;
875 } END_FOR_EACH_PTR(member);
877 return member_set;
880 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
882 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
883 int i;
885 for (i = 0; i < member_count; i++) {
886 if (member_set[i].ident == ident) {
887 member_set[i].set = 1;
888 return;
891 // crap. this is buggy.
892 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
895 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
897 struct expression *deref, *assign;
898 struct symbol *member, *member_type;
899 int member_idx;
901 member_idx = 0;
902 FOR_EACH_PTR(type->symbol_list, member) {
903 if (!member->ident || member_set[member_idx].set) {
904 member_idx++;
905 continue;
907 member_type = get_real_base_type(member);
908 if (!member_type || member_type->type == SYM_ARRAY)
909 continue;
910 /* TODO: this should be handled recursively and not ignored */
911 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION)
912 continue;
913 deref = member_expression(symbol, '.', member->ident);
914 assign = assign_expression(deref, zero_expr());
915 __split_expr(assign);
916 member_idx++;
917 } END_FOR_EACH_PTR(member);
921 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
923 struct expression *deref, *assign, *tmp;
924 struct symbol *struct_type, *type;
925 struct ident *member;
926 int member_idx;
927 struct member_set *member_set;
929 struct_type = get_type(symbol);
930 if (!struct_type ||
931 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
932 return;
934 member_set = alloc_member_set(struct_type);
936 member_idx = 0;
937 FOR_EACH_PTR(members, tmp) {
938 member = number_to_member(symbol, member_idx);
939 while (tmp->type == EXPR_IDENTIFIER) {
940 member = tmp->expr_ident;
941 member_idx = member_to_number(symbol, member);
942 tmp = tmp->ident_expression;
944 mark_member_as_set(struct_type, member_set, member);
945 member_idx++;
946 deref = member_expression(symbol, '.', member);
947 if (tmp->type == EXPR_INITIALIZER) {
948 type = get_type(deref);
949 if (type && type->type == SYM_ARRAY)
950 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
951 else
952 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
953 } else {
954 assign = assign_expression(deref, tmp);
955 fake_cb(assign);
957 } END_FOR_EACH_PTR(tmp);
959 set_unset_to_zero(symbol, struct_type, member_set);
962 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
964 fake_member_assigns_helper(symbol_expression(sym),
965 sym->initializer->expr_list, fake_cb);
968 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
970 struct expression *offset, *binop, *assign, *tmp;
971 struct symbol *type;
972 int idx;
974 idx = 0;
975 FOR_EACH_PTR(expr_list, tmp) {
976 if (tmp->type == EXPR_INDEX) {
977 if (tmp->idx_from != tmp->idx_to)
978 return;
979 idx = tmp->idx_from;
980 if (!tmp->idx_expression)
981 goto next;
982 tmp = tmp->idx_expression;
984 offset = value_expr(idx);
985 binop = array_element_expression(array, offset);
986 if (tmp->type == EXPR_INITIALIZER) {
987 type = get_type(binop);
988 if (type && type->type == SYM_ARRAY)
989 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
990 else
991 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
992 } else {
993 assign = assign_expression(binop, tmp);
994 fake_cb(assign);
996 next:
997 idx++;
998 } END_FOR_EACH_PTR(tmp);
1001 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1003 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1006 static void fake_assign_expr(struct symbol *sym)
1008 struct expression *assign, *symbol;
1010 symbol = symbol_expression(sym);
1011 assign = assign_expression(symbol, sym->initializer);
1012 __split_expr(assign);
1015 static void call_split_expr(struct expression *expr)
1017 __split_expr(expr);
1020 static void do_initializer_stuff(struct symbol *sym)
1022 if (!sym->initializer)
1023 return;
1025 if (sym->initializer->type == EXPR_INITIALIZER) {
1026 if (get_real_base_type(sym)->type == SYM_ARRAY)
1027 fake_element_assigns(sym, call_split_expr);
1028 else
1029 fake_member_assigns(sym, call_split_expr);
1030 } else {
1031 fake_assign_expr(sym);
1035 static void split_declaration(struct symbol_list *sym_list)
1037 struct symbol *sym;
1039 FOR_EACH_PTR(sym_list, sym) {
1040 __pass_to_client(sym, DECLARATION_HOOK);
1041 do_initializer_stuff(sym);
1042 split_sym(sym);
1043 } END_FOR_EACH_PTR(sym);
1046 static void call_global_assign_hooks(struct expression *assign)
1048 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1051 static void fake_global_assign(struct symbol *sym)
1053 struct expression *assign, *symbol;
1055 if (!sym->initializer)
1056 return;
1057 if (sym->initializer->type == EXPR_INITIALIZER) {
1058 if (get_real_base_type(sym)->type == SYM_ARRAY)
1059 fake_element_assigns(sym, call_global_assign_hooks);
1060 else
1061 fake_member_assigns(sym, call_global_assign_hooks);
1062 } else {
1063 symbol = symbol_expression(sym);
1064 assign = assign_expression(symbol, sym->initializer);
1065 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1069 static void start_function_definition(struct symbol *sym)
1071 __in_function_def = 1;
1072 __pass_to_client(sym, FUNC_DEF_HOOK);
1073 __in_function_def = 0;
1074 __pass_to_client(sym, AFTER_DEF_HOOK);
1078 static void split_function(struct symbol *sym)
1080 struct symbol *base_type = get_base_type(sym);
1082 cur_func_sym = sym;
1083 if (sym->ident)
1084 cur_func = sym->ident->name;
1085 __smatch_lineno = sym->pos.line;
1086 last_stmt = NULL;
1087 loop_count = 0;
1088 sm_debug("new function: %s\n", cur_func);
1089 __slist_id = 0;
1090 if (option_two_passes) {
1091 __unnullify_path();
1092 loop_num = 0;
1093 final_pass = 0;
1094 start_function_definition(sym);
1095 __split_stmt(base_type->stmt);
1096 __split_stmt(base_type->inline_stmt);
1097 nullify_path();
1099 __unnullify_path();
1100 loop_num = 0;
1101 start_function_definition(sym);
1102 __split_stmt(base_type->stmt);
1103 __split_stmt(base_type->inline_stmt);
1104 __pass_to_client(sym, END_FUNC_HOOK);
1105 __pass_to_client(sym, AFTER_FUNC_HOOK);
1106 cur_func_sym = NULL;
1107 cur_func = NULL;
1108 clear_all_states();
1109 free_data_info_allocs();
1110 free_expression_stack(&switch_expr_stack);
1111 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1112 __bail_on_rest_of_function = 0;
1115 static void parse_inline(struct expression *call)
1117 struct symbol *base_type;
1118 int loop_num_bak = loop_num;
1119 int final_pass_bak = final_pass;
1120 char *cur_func_bak = cur_func;
1121 struct statement_list *big_statement_stack_bak = big_statement_stack;
1122 struct expression_list *big_expression_stack_bak = big_expression_stack;
1123 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1124 struct symbol *cur_func_sym_bak = cur_func_sym;
1126 __pass_to_client(call, INLINE_FN_START);
1127 final_pass = 0; /* don't print anything */
1128 __inline_fn = call;
1130 base_type = get_base_type(call->fn->symbol);
1131 cur_func_sym = call->fn->symbol;
1132 if (call->fn->symbol->ident)
1133 cur_func = call->fn->symbol->ident->name;
1134 else
1135 cur_func = NULL;
1136 set_position(call->fn->symbol->pos);
1138 save_all_states();
1139 nullify_all_states();
1140 big_statement_stack = NULL;
1141 big_expression_stack = NULL;
1142 switch_expr_stack = NULL;
1144 sm_debug("inline function: %s\n", cur_func);
1145 __unnullify_path();
1146 loop_num = 0;
1147 start_function_definition(call->fn->symbol);
1148 __split_stmt(base_type->stmt);
1149 __split_stmt(base_type->inline_stmt);
1150 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1151 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1153 free_expression_stack(&switch_expr_stack);
1154 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1155 nullify_path();
1157 loop_num = loop_num_bak;
1158 final_pass = final_pass_bak;
1159 cur_func_sym = cur_func_sym_bak;
1160 cur_func = cur_func_bak;
1161 big_statement_stack = big_statement_stack_bak;
1162 big_expression_stack = big_expression_stack_bak;
1163 switch_expr_stack = switch_expr_stack_bak;
1165 restore_all_states();
1166 set_position(call->pos);
1167 __inline_fn = NULL;
1168 __pass_to_client(call, INLINE_FN_END);
1171 static struct symbol_list *inlines_called;
1172 static void add_inline_function(struct symbol *sym)
1174 static struct symbol_list *already_added;
1175 struct symbol *tmp;
1177 FOR_EACH_PTR(already_added, tmp) {
1178 if (tmp == sym)
1179 return;
1180 } END_FOR_EACH_PTR(tmp);
1182 add_ptr_list(&already_added, sym);
1183 add_ptr_list(&inlines_called, sym);
1186 static void process_inlines()
1188 struct symbol *tmp;
1190 FOR_EACH_PTR(inlines_called, tmp) {
1191 split_function(tmp);
1192 } END_FOR_EACH_PTR(tmp);
1193 free_ptr_list(&inlines_called);
1196 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1198 struct symbol *sym;
1200 FOR_EACH_PTR_REVERSE(big_list, sym) {
1201 if (!sym->scope)
1202 continue;
1203 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1204 return sym;
1205 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1206 return sym;
1207 } END_FOR_EACH_PTR_REVERSE(sym);
1209 return NULL;
1212 static void split_inlines_in_scope(struct symbol *sym)
1214 struct symbol *base;
1215 struct symbol_list *scope_list;
1216 int stream;
1218 scope_list = sym->scope->symbols;
1219 stream = sym->pos.stream;
1221 /* find the last static symbol in the file */
1222 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1223 if (sym->pos.stream != stream)
1224 continue;
1225 if (sym->type != SYM_NODE)
1226 continue;
1227 base = get_base_type(sym);
1228 if (!base)
1229 continue;
1230 if (base->type != SYM_FN)
1231 continue;
1232 if (!base->inline_stmt)
1233 continue;
1234 add_inline_function(sym);
1235 } END_FOR_EACH_PTR_REVERSE(sym);
1237 process_inlines();
1240 static void split_inlines(struct symbol_list *sym_list)
1242 struct symbol *sym;
1244 sym = get_last_scoped_symbol(sym_list, 0);
1245 if (sym)
1246 split_inlines_in_scope(sym);
1247 sym = get_last_scoped_symbol(sym_list, 1);
1248 if (sym)
1249 split_inlines_in_scope(sym);
1252 static void split_functions(struct symbol_list *sym_list)
1254 struct symbol *sym;
1256 FOR_EACH_PTR(sym_list, sym) {
1257 set_position(sym->pos);
1258 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1259 split_function(sym);
1260 process_inlines();
1261 } else {
1262 __pass_to_client(sym, BASE_HOOK);
1263 fake_global_assign(sym);
1265 } END_FOR_EACH_PTR(sym);
1266 split_inlines(sym_list);
1267 __pass_to_client(sym_list, END_FILE_HOOK);
1270 void smatch(int argc, char **argv)
1273 struct string_list *filelist = NULL;
1274 struct symbol_list *sym_list;
1276 if (argc < 2) {
1277 printf("Usage: smatch [--debug] <filename.c>\n");
1278 exit(1);
1280 sparse_initialize(argc, argv, &filelist);
1281 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1282 if (option_file_output) {
1283 char buf[256];
1285 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1286 sm_outfd = fopen(buf, "w");
1287 if (!sm_outfd) {
1288 printf("Error: Cannot open %s\n", base_file);
1289 exit(1);
1292 sym_list = sparse_keep_tokens(base_file);
1293 split_functions(sym_list);
1294 } END_FOR_EACH_PTR_NOTAG(base_file);