*new* readl_infinite_loops: check for hotplug bugs which could cause hangs
[smatch.git] / smatch_flow.c
blobbfe06ad58b5bc90de1ceb4fa6e39469f25dc18da
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 __in_fake_assign;
21 int final_pass;
22 int __inline_call;
23 struct expression *__inline_fn;
25 static int __smatch_lineno = 0;
27 static char *base_file;
28 static const char *filename;
29 static char *pathname;
30 static char *full_filename;
31 static char *cur_func;
32 static int loop_count;
33 int __expr_stmt_count;
34 int __in_function_def;
35 static struct expression_list *switch_expr_stack = NULL;
36 static struct expression_list *post_op_stack = NULL;
38 struct expression_list *big_expression_stack;
39 struct statement_list *big_statement_stack;
40 int __in_pre_condition = 0;
41 int __bail_on_rest_of_function = 0;
42 char *get_function(void) { return cur_func; }
43 int get_lineno(void) { return __smatch_lineno; }
44 int inside_loop(void) { return !!loop_count; }
45 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
46 int in_expression_statement(void) { return !!__expr_stmt_count; }
48 static void split_symlist(struct symbol_list *sym_list);
49 static void split_declaration(struct symbol_list *sym_list);
50 static void split_expr_list(struct expression_list *expr_list);
51 static void add_inline_function(struct symbol *sym);
52 static void parse_inline(struct expression *expr);
54 int option_assume_loops = 0;
55 int option_known_conditions = 0;
56 int option_two_passes = 0;
57 struct symbol *cur_func_sym = NULL;
59 int outside_of_function(void)
61 return cur_func_sym == NULL;
64 const char *get_filename(void)
66 if (option_info)
67 return base_file;
68 if (option_full_path)
69 return full_filename;
70 return filename;
73 const char *get_base_file(void)
75 return base_file;
78 static void set_position(struct position pos)
80 int len;
81 static int prev_stream = -1;
83 if (pos.stream == 0 && pos.line == 0)
84 return;
86 __smatch_lineno = pos.line;
88 if (pos.stream == prev_stream)
89 return;
91 filename = stream_name(pos.stream);
93 free(full_filename);
94 pathname = getcwd(NULL, 0);
95 if (pathname) {
96 len = strlen(pathname) + 1 + strlen(filename) + 1;
97 full_filename = malloc(len);
98 snprintf(full_filename, len, "%s/%s", pathname, filename);
99 } else {
100 full_filename = alloc_string(filename);
102 free(pathname);
105 static int is_inline_func(struct expression *expr)
107 if (expr->type != EXPR_SYMBOL || !expr->symbol)
108 return 0;
109 if (expr->symbol->ctype.modifiers & MOD_INLINE)
110 return 1;
111 return 0;
114 static int is_noreturn_func(struct expression *expr)
116 if (expr->type != EXPR_SYMBOL || !expr->symbol)
117 return 0;
118 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
119 return 1;
120 return 0;
123 int inlinable(struct expression *expr)
125 struct symbol *sym;
127 if (__inline_fn) /* don't nest */
128 return 0;
130 if (expr->type != EXPR_SYMBOL || !expr->symbol)
131 return 0;
132 if (is_no_inline_function(expr->symbol->ident->name))
133 return 0;
134 sym = get_base_type(expr->symbol);
135 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
136 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
137 return 1;
138 return 0;
140 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
141 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
142 return 1;
143 return 0;
145 return 0;
148 void __process_post_op_stack(void)
150 struct expression *expr;
152 FOR_EACH_PTR(post_op_stack, expr) {
153 __pass_to_client(expr, OP_HOOK);
154 } END_FOR_EACH_PTR(expr);
156 __free_ptr_list((struct ptr_list **)&post_op_stack);
159 void __split_expr(struct expression *expr)
161 if (!expr)
162 return;
164 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
166 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
167 return;
168 if (__in_fake_assign >= 4) /* don't allow too much nesting */
169 return;
171 push_expression(&big_expression_stack, expr);
172 set_position(expr->pos);
173 __pass_to_client(expr, EXPR_HOOK);
175 switch (expr->type) {
176 case EXPR_PREOP:
177 if (expr->op == '*')
178 __pass_to_client(expr, DEREF_HOOK);
179 __split_expr(expr->unop);
180 __pass_to_client(expr, OP_HOOK);
181 break;
182 case EXPR_POSTOP:
183 __split_expr(expr->unop);
184 push_expression(&post_op_stack, expr);
185 break;
186 case EXPR_STATEMENT:
187 __expr_stmt_count++;
188 __split_stmt(expr->statement);
189 __expr_stmt_count--;
190 break;
191 case EXPR_LOGICAL:
192 case EXPR_COMPARE:
193 __pass_to_client(expr, LOGIC_HOOK);
194 __handle_logic(expr);
195 break;
196 case EXPR_BINOP:
197 __pass_to_client(expr, BINOP_HOOK);
198 case EXPR_COMMA:
199 __split_expr(expr->left);
200 __process_post_op_stack();
201 __split_expr(expr->right);
202 break;
203 case EXPR_ASSIGNMENT: {
204 struct expression *tmp;
206 if (!expr->right)
207 break;
209 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
211 /* foo = !bar() */
212 if (__handle_condition_assigns(expr))
213 break;
214 /* foo = (x < 5 ? foo : 5); */
215 if (__handle_select_assigns(expr))
216 break;
217 /* foo = ({frob(); frob(); frob(); 1;}) */
218 if (__handle_expr_statement_assigns(expr))
219 break;
221 __split_expr(expr->right);
222 if (outside_of_function())
223 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
224 else
225 __pass_to_client(expr, ASSIGNMENT_HOOK);
227 __fake_struct_member_assignments(expr);
229 tmp = strip_expr(expr->right);
230 if (tmp->type == EXPR_CALL)
231 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
232 if (get_macro_name(tmp->pos) &&
233 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
234 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
235 __split_expr(expr->left);
236 break;
238 case EXPR_DEREF:
239 __pass_to_client(expr, DEREF_HOOK);
240 __split_expr(expr->deref);
241 break;
242 case EXPR_SLICE:
243 __split_expr(expr->base);
244 break;
245 case EXPR_CAST:
246 case EXPR_FORCE_CAST:
247 __pass_to_client(expr, CAST_HOOK);
248 __split_expr(expr->cast_expression);
249 break;
250 case EXPR_SIZEOF:
251 if (expr->cast_expression)
252 __pass_to_client(strip_parens(expr->cast_expression),
253 SIZEOF_HOOK);
254 break;
255 case EXPR_OFFSETOF:
256 case EXPR_ALIGNOF:
257 evaluate_expression(expr);
258 break;
259 case EXPR_CONDITIONAL:
260 case EXPR_SELECT:
261 if (known_condition_true(expr->conditional)) {
262 __split_expr(expr->cond_true);
263 break;
265 if (known_condition_false(expr->conditional)) {
266 __split_expr(expr->cond_false);
267 break;
269 __pass_to_client(expr, SELECT_HOOK);
270 __split_whole_condition(expr->conditional);
271 __split_expr(expr->cond_true);
272 __push_true_states();
273 __use_false_states();
274 __split_expr(expr->cond_false);
275 __merge_true_states();
276 break;
277 case EXPR_CALL:
278 if (sym_name_is("__builtin_constant_p", expr->fn))
279 break;
280 split_expr_list(expr->args);
281 __split_expr(expr->fn);
282 if (is_inline_func(expr->fn))
283 add_inline_function(expr->fn->symbol);
284 if (inlinable(expr->fn))
285 __inline_call = 1;
286 __process_post_op_stack();
287 __pass_to_client(expr, FUNCTION_CALL_HOOK);
288 __inline_call = 0;
289 if (inlinable(expr->fn)) {
290 parse_inline(expr);
292 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
293 if (is_noreturn_func(expr->fn))
294 nullify_path();
295 break;
296 case EXPR_INITIALIZER:
297 split_expr_list(expr->expr_list);
298 break;
299 case EXPR_IDENTIFIER:
300 __split_expr(expr->ident_expression);
301 break;
302 case EXPR_INDEX:
303 __split_expr(expr->idx_expression);
304 break;
305 case EXPR_POS:
306 __split_expr(expr->init_expr);
307 break;
308 case EXPR_SYMBOL:
309 __pass_to_client(expr, SYM_HOOK);
310 break;
311 case EXPR_STRING:
312 __pass_to_client(expr, STRING_HOOK);
313 break;
314 default:
315 break;
317 pop_expression(&big_expression_stack);
320 static int is_forever_loop(struct statement *stmt)
322 struct expression *expr;
324 expr = strip_expr(stmt->iterator_pre_condition);
325 if (!expr)
326 expr = stmt->iterator_post_condition;
327 if (!expr) {
328 /* this is a for(;;) loop... */
329 return 1;
332 if (expr->type == EXPR_VALUE && expr->value == 1)
333 return 1;
335 return 0;
338 static int loop_num;
339 static char *get_loop_name(int num)
341 char buf[256];
343 snprintf(buf, 255, "-loop%d", num);
344 buf[255] = '\0';
345 return alloc_sname(buf);
349 * Pre Loops are while and for loops.
351 static void handle_pre_loop(struct statement *stmt)
353 int once_through; /* we go through the loop at least once */
354 struct sm_state *extra_sm = NULL;
355 int unchanged = 0;
356 char *loop_name;
357 struct state_list *slist = NULL;
358 struct sm_state *sm = NULL;
360 loop_name = get_loop_name(loop_num);
361 loop_num++;
363 __split_stmt(stmt->iterator_pre_statement);
365 once_through = implied_condition_true(stmt->iterator_pre_condition);
367 loop_count++;
368 __push_continues();
369 __push_breaks();
371 __merge_gotos(loop_name);
373 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
374 __in_pre_condition++;
375 __pass_to_client(stmt, PRELOOP_HOOK);
376 __split_whole_condition(stmt->iterator_pre_condition);
377 __in_pre_condition--;
378 FOR_EACH_PTR(slist, sm) {
379 set_state(sm->owner, sm->name, sm->sym, sm->state);
380 } END_FOR_EACH_PTR(sm);
381 free_slist(&slist);
382 if (extra_sm)
383 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
385 if (option_assume_loops)
386 once_through = 1;
388 __split_stmt(stmt->iterator_statement);
389 __warn_on_silly_pre_loops();
390 if (is_forever_loop(stmt)) {
391 struct state_list *slist;
393 __save_gotos(loop_name);
395 __push_fake_cur_slist();
396 __split_stmt(stmt->iterator_post_statement);
397 slist = __pop_fake_cur_slist();
399 __discard_continues();
400 __discard_false_states();
401 __use_breaks();
403 if (!__path_is_null())
404 __merge_slist_into_cur(slist);
405 free_slist(&slist);
406 } else {
407 __merge_continues();
408 unchanged = __iterator_unchanged(extra_sm);
409 __split_stmt(stmt->iterator_post_statement);
410 __save_gotos(loop_name);
411 __split_whole_condition(stmt->iterator_pre_condition);
412 nullify_path();
413 __merge_false_states();
414 if (once_through)
415 __discard_false_states();
416 else
417 __merge_false_states();
419 if (extra_sm && unchanged)
420 __extra_pre_loop_hook_after(extra_sm,
421 stmt->iterator_post_statement,
422 stmt->iterator_pre_condition);
423 __merge_breaks();
425 loop_count--;
429 * Post loops are do {} while();
431 static void handle_post_loop(struct statement *stmt)
433 char *loop_name;
435 loop_name = get_loop_name(loop_num);
436 loop_num++;
437 loop_count++;
439 __push_continues();
440 __push_breaks();
441 __merge_gotos(loop_name);
442 __split_stmt(stmt->iterator_statement);
443 __merge_continues();
444 if (!is_zero(stmt->iterator_post_condition))
445 __save_gotos(loop_name);
447 if (is_forever_loop(stmt)) {
448 __use_breaks();
449 } else {
450 __split_whole_condition(stmt->iterator_post_condition);
451 __use_false_states();
452 __merge_breaks();
454 loop_count--;
457 static int empty_statement(struct statement *stmt)
459 if (!stmt)
460 return 0;
461 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
462 return 1;
463 return 0;
466 static int last_stmt_on_same_line()
468 struct statement *stmt;
469 int i = 0;
471 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
472 if (!i++)
473 continue;
474 if (stmt->pos.line == get_lineno())
475 return 1;
476 return 0;
477 } END_FOR_EACH_PTR_REVERSE(stmt);
478 return 0;
481 static struct statement *last_stmt;
482 static int is_last_stmt(struct statement *stmt)
484 if (stmt == last_stmt)
485 return 1;
486 return 0;
489 static void print_unreached_initializers(struct symbol_list *sym_list)
491 struct symbol *sym;
493 FOR_EACH_PTR(sym_list, sym) {
494 if (sym->initializer)
495 sm_msg("info: '%s' is not actually initialized (unreached code).",
496 (sym->ident ? sym->ident->name : "this variable"));
497 } END_FOR_EACH_PTR(sym);
500 static void print_unreached(struct statement *stmt)
502 static int print = 1;
504 if (__inline_fn)
505 return;
507 if (!__path_is_null()) {
508 print = 1;
509 return;
511 if (!print)
512 return;
514 switch (stmt->type) {
515 case STMT_COMPOUND: /* after a switch before a case stmt */
516 case STMT_RANGE:
517 case STMT_CASE:
518 case STMT_LABEL:
519 return;
520 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
521 print_unreached_initializers(stmt->declaration);
522 return;
523 case STMT_RETURN: /* gcc complains if you don't have a return statement */
524 if (is_last_stmt(stmt))
525 return;
526 break;
527 case STMT_GOTO:
528 /* people put extra breaks inside switch statements */
529 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE &&
530 strcmp(stmt->goto_label->ident->name, "break") == 0)
531 return;
532 break;
533 default:
534 break;
536 if (empty_statement(stmt))
537 return;
538 if (!option_spammy)
539 return;
540 sm_msg("info: ignoring unreachable code.");
541 print = 0;
544 static void split_asm_constraints(struct expression_list *expr_list)
546 struct expression *expr;
547 int state = 0;
549 FOR_EACH_PTR(expr_list, expr) {
550 switch (state) {
551 case 0: /* identifier */
552 case 1: /* constraint */
553 state++;
554 continue;
555 case 2: /* expression */
556 state = 0;
557 __split_expr(expr);
558 continue;
560 } END_FOR_EACH_PTR(expr);
563 static int is_case_val(struct statement *stmt, sval_t sval)
565 sval_t case_sval;
567 if (stmt->type != STMT_CASE)
568 return 0;
569 if (!stmt->case_expression) {
570 __set_default();
571 return 1;
573 if (!get_value(stmt->case_expression, &case_sval))
574 return 0;
575 if (case_sval.value == sval.value)
576 return 1;
577 return 0;
580 static void split_known_switch(struct statement *stmt, sval_t sval)
582 struct statement *tmp;
584 __split_expr(stmt->switch_expression);
586 push_expression(&switch_expr_stack, stmt->switch_expression);
587 __save_switch_states(top_expression(switch_expr_stack));
588 nullify_path();
589 __push_default();
590 __push_breaks();
592 stmt = stmt->switch_statement;
594 if (!last_stmt)
595 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
597 __push_scope_hooks();
598 FOR_EACH_PTR(stmt->stmts, tmp) {
599 __smatch_lineno = tmp->pos.line;
600 if (is_case_val(tmp, sval)) {
601 __merge_switches(top_expression(switch_expr_stack),
602 stmt->case_expression);
603 __pass_case_to_client(top_expression(switch_expr_stack),
604 stmt->case_expression);
606 if (__path_is_null())
607 continue;
608 __split_stmt(tmp);
609 if (__path_is_null()) {
610 __set_default();
611 goto out;
613 } END_FOR_EACH_PTR(tmp);
614 out:
615 __call_scope_hooks();
616 if (!__pop_default())
617 __merge_switches(top_expression(switch_expr_stack),
618 NULL);
619 __discard_switches();
620 __merge_breaks();
621 pop_expression(&switch_expr_stack);
624 void __split_stmt(struct statement *stmt)
626 sval_t sval;
628 if (!stmt)
629 goto out;
631 if (out_of_memory() || __bail_on_rest_of_function) {
632 static char *printed = NULL;
634 if (printed != cur_func)
635 sm_msg("Function too hairy. Giving up.");
636 final_pass = 0; /* turn off sm_msg() from here */
637 printed = cur_func;
638 return;
641 add_ptr_list(&big_statement_stack, stmt);
642 free_expression_stack(&big_expression_stack);
643 set_position(stmt->pos);
644 print_unreached(stmt);
645 __pass_to_client(stmt, STMT_HOOK);
647 switch (stmt->type) {
648 case STMT_DECLARATION:
649 split_declaration(stmt->declaration);
650 break;
651 case STMT_RETURN:
652 __split_expr(stmt->ret_value);
653 __pass_to_client(stmt->ret_value, RETURN_HOOK);
654 nullify_path();
655 break;
656 case STMT_EXPRESSION:
657 __split_expr(stmt->expression);
658 break;
659 case STMT_COMPOUND: {
660 struct statement *tmp;
662 if (!last_stmt)
663 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
664 __push_scope_hooks();
665 FOR_EACH_PTR(stmt->stmts, tmp) {
666 __split_stmt(tmp);
667 } END_FOR_EACH_PTR(tmp);
668 __call_scope_hooks();
669 break;
671 case STMT_IF:
672 if (known_condition_true(stmt->if_conditional)) {
673 __split_stmt(stmt->if_true);
674 break;
676 if (known_condition_false(stmt->if_conditional)) {
677 __split_stmt(stmt->if_false);
678 break;
680 if (option_known_conditions &&
681 implied_condition_true(stmt->if_conditional)) {
682 sm_info("this condition is true.");
683 __split_stmt(stmt->if_true);
684 break;
686 if (option_known_conditions &&
687 implied_condition_false(stmt->if_conditional)) {
688 sm_info("this condition is false.");
689 __split_stmt(stmt->if_false);
690 break;
692 __split_whole_condition(stmt->if_conditional);
693 __split_stmt(stmt->if_true);
694 if (empty_statement(stmt->if_true) &&
695 last_stmt_on_same_line() &&
696 !get_macro_name(stmt->if_true->pos))
697 sm_msg("warn: if();");
698 __push_true_states();
699 __use_false_states();
700 __split_stmt(stmt->if_false);
701 __merge_true_states();
702 break;
703 case STMT_ITERATOR:
704 if (stmt->iterator_pre_condition)
705 handle_pre_loop(stmt);
706 else if (stmt->iterator_post_condition)
707 handle_post_loop(stmt);
708 else {
709 // these are for(;;) type loops.
710 handle_pre_loop(stmt);
712 break;
713 case STMT_SWITCH:
714 if (get_value(stmt->switch_expression, &sval)) {
715 split_known_switch(stmt, sval);
716 break;
718 __split_expr(stmt->switch_expression);
719 push_expression(&switch_expr_stack, stmt->switch_expression);
720 __save_switch_states(top_expression(switch_expr_stack));
721 nullify_path();
722 __push_default();
723 __push_breaks();
724 __split_stmt(stmt->switch_statement);
725 if (!__pop_default())
726 __merge_switches(top_expression(switch_expr_stack),
727 NULL);
728 __discard_switches();
729 __merge_breaks();
730 pop_expression(&switch_expr_stack);
731 break;
732 case STMT_CASE:
733 __merge_switches(top_expression(switch_expr_stack),
734 stmt->case_expression);
735 __pass_case_to_client(top_expression(switch_expr_stack),
736 stmt->case_expression);
737 if (!stmt->case_expression)
738 __set_default();
739 __split_expr(stmt->case_expression);
740 __split_expr(stmt->case_to);
741 __split_stmt(stmt->case_statement);
742 break;
743 case STMT_LABEL:
744 if (stmt->label_identifier &&
745 stmt->label_identifier->type == SYM_LABEL &&
746 stmt->label_identifier->ident) {
747 loop_count = 1000000;
748 __merge_gotos(stmt->label_identifier->ident->name);
750 __split_stmt(stmt->label_statement);
751 break;
752 case STMT_GOTO:
753 __split_expr(stmt->goto_expression);
754 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
755 if (!strcmp(stmt->goto_label->ident->name, "break")) {
756 __process_breaks();
757 } else if (!strcmp(stmt->goto_label->ident->name,
758 "continue")) {
759 __process_continues();
761 } else if (stmt->goto_label &&
762 stmt->goto_label->type == SYM_LABEL &&
763 stmt->goto_label->ident) {
764 __save_gotos(stmt->goto_label->ident->name);
766 nullify_path();
767 break;
768 case STMT_NONE:
769 break;
770 case STMT_ASM:
771 __pass_to_client(stmt, ASM_HOOK);
772 __split_expr(stmt->asm_string);
773 split_asm_constraints(stmt->asm_outputs);
774 split_asm_constraints(stmt->asm_inputs);
775 split_asm_constraints(stmt->asm_clobbers);
776 break;
777 case STMT_CONTEXT:
778 break;
779 case STMT_RANGE:
780 __split_expr(stmt->range_expression);
781 __split_expr(stmt->range_low);
782 __split_expr(stmt->range_high);
783 break;
785 __pass_to_client(stmt, STMT_HOOK_AFTER);
786 out:
787 __process_post_op_stack();
790 static void split_expr_list(struct expression_list *expr_list)
792 struct expression *expr;
794 FOR_EACH_PTR(expr_list, expr) {
795 __split_expr(expr);
796 __process_post_op_stack();
797 } END_FOR_EACH_PTR(expr);
800 static void split_sym(struct symbol *sym)
802 if (!sym)
803 return;
804 if (!(sym->namespace & NS_SYMBOL))
805 return;
807 __split_stmt(sym->stmt);
808 __split_expr(sym->array_size);
809 split_symlist(sym->arguments);
810 split_symlist(sym->symbol_list);
811 __split_stmt(sym->inline_stmt);
812 split_symlist(sym->inline_symbol_list);
815 static void split_symlist(struct symbol_list *sym_list)
817 struct symbol *sym;
819 FOR_EACH_PTR(sym_list, sym) {
820 split_sym(sym);
821 } END_FOR_EACH_PTR(sym);
824 typedef void (fake_cb)(struct expression *expr);
826 static int member_to_number(struct expression *expr, struct ident *member)
828 struct symbol *type, *tmp;
829 char *name;
830 int i;
832 if (!member)
833 return -1;
834 name = member->name;
836 type = get_type(expr);
837 if (!type || type->type != SYM_STRUCT)
838 return -1;
840 i = -1;
841 FOR_EACH_PTR(type->symbol_list, tmp) {
842 i++;
843 if (!tmp->ident)
844 continue;
845 if (strcmp(name, tmp->ident->name) == 0)
846 return i;
847 } END_FOR_EACH_PTR(tmp);
848 return -1;
851 static struct ident *number_to_member(struct expression *expr, int num)
853 struct symbol *type, *member;
854 int i = 0;
856 type = get_type(expr);
857 if (!type || type->type != SYM_STRUCT)
858 return NULL;
860 FOR_EACH_PTR(type->symbol_list, member) {
861 if (i == num)
862 return member->ident;
863 i++;
864 } END_FOR_EACH_PTR(member);
865 return NULL;
868 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
870 struct member_set {
871 struct ident *ident;
872 int set;
875 static struct member_set *alloc_member_set(struct symbol *type)
877 struct member_set *member_set;
878 struct symbol *member;
879 int member_count;
880 int member_idx;
882 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
883 member_set = malloc(member_count * sizeof(*member_set));
884 member_idx = 0;
885 FOR_EACH_PTR(type->symbol_list, member) {
886 member_set[member_idx].ident = member->ident;
887 member_set[member_idx].set = 0;
888 member_idx++;
889 } END_FOR_EACH_PTR(member);
891 return member_set;
894 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
896 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
897 int i;
899 for (i = 0; i < member_count; i++) {
900 if (member_set[i].ident == ident) {
901 member_set[i].set = 1;
902 return;
905 // crap. this is buggy.
906 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
909 static void set_unset_to_zero(struct expression *symbol, struct symbol *type, struct member_set *member_set)
911 struct expression *deref, *assign;
912 struct symbol *member, *member_type;
913 int member_idx;
915 member_idx = 0;
916 FOR_EACH_PTR(type->symbol_list, member) {
917 if (!member->ident || member_set[member_idx].set) {
918 member_idx++;
919 continue;
921 member_type = get_real_base_type(member);
922 if (!member_type || member_type->type == SYM_ARRAY) {
923 member_idx++;
924 continue;
926 /* TODO: this should be handled recursively and not ignored */
927 if (member_type->type == SYM_STRUCT || member_type->type == SYM_UNION) {
928 member_idx++;
929 continue;
931 deref = member_expression(symbol, '.', member->ident);
932 assign = assign_expression(deref, zero_expr());
933 __split_expr(assign);
934 member_idx++;
935 } END_FOR_EACH_PTR(member);
939 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
941 struct expression *deref, *assign, *tmp;
942 struct symbol *struct_type, *type;
943 struct ident *member;
944 int member_idx;
945 struct member_set *member_set;
947 struct_type = get_type(symbol);
948 if (!struct_type ||
949 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
950 return;
952 member_set = alloc_member_set(struct_type);
954 member_idx = 0;
955 FOR_EACH_PTR(members, tmp) {
956 member = number_to_member(symbol, member_idx);
957 while (tmp->type == EXPR_IDENTIFIER) {
958 member = tmp->expr_ident;
959 member_idx = member_to_number(symbol, member);
960 tmp = tmp->ident_expression;
962 mark_member_as_set(struct_type, member_set, member);
963 member_idx++;
964 deref = member_expression(symbol, '.', member);
965 if (tmp->type == EXPR_INITIALIZER) {
966 type = get_type(deref);
967 if (type && type->type == SYM_ARRAY)
968 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
969 else
970 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
971 } else {
972 assign = assign_expression(deref, tmp);
973 fake_cb(assign);
975 } END_FOR_EACH_PTR(tmp);
977 set_unset_to_zero(symbol, struct_type, member_set);
980 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
982 fake_member_assigns_helper(symbol_expression(sym),
983 sym->initializer->expr_list, fake_cb);
986 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
988 struct expression *offset, *binop, *assign, *tmp;
989 struct symbol *type;
990 int idx;
992 idx = 0;
993 FOR_EACH_PTR(expr_list, tmp) {
994 if (tmp->type == EXPR_INDEX) {
995 if (tmp->idx_from != tmp->idx_to)
996 return;
997 idx = tmp->idx_from;
998 if (!tmp->idx_expression)
999 goto next;
1000 tmp = tmp->idx_expression;
1002 offset = value_expr(idx);
1003 binop = array_element_expression(array, offset);
1004 if (tmp->type == EXPR_INITIALIZER) {
1005 type = get_type(binop);
1006 if (type && type->type == SYM_ARRAY)
1007 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1008 else
1009 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1010 } else {
1011 assign = assign_expression(binop, tmp);
1012 fake_cb(assign);
1014 next:
1015 idx++;
1016 } END_FOR_EACH_PTR(tmp);
1019 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1021 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1024 static void fake_assign_expr(struct symbol *sym)
1026 struct expression *assign, *symbol;
1028 symbol = symbol_expression(sym);
1029 assign = assign_expression(symbol, sym->initializer);
1030 __split_expr(assign);
1033 static void call_split_expr(struct expression *expr)
1035 __split_expr(expr);
1038 static void do_initializer_stuff(struct symbol *sym)
1040 if (!sym->initializer)
1041 return;
1043 if (sym->initializer->type == EXPR_INITIALIZER) {
1044 if (get_real_base_type(sym)->type == SYM_ARRAY)
1045 fake_element_assigns(sym, call_split_expr);
1046 else
1047 fake_member_assigns(sym, call_split_expr);
1048 } else {
1049 fake_assign_expr(sym);
1053 static void split_declaration(struct symbol_list *sym_list)
1055 struct symbol *sym;
1057 FOR_EACH_PTR(sym_list, sym) {
1058 __pass_to_client(sym, DECLARATION_HOOK);
1059 do_initializer_stuff(sym);
1060 split_sym(sym);
1061 } END_FOR_EACH_PTR(sym);
1064 static void call_global_assign_hooks(struct expression *assign)
1066 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1069 static void fake_global_assign(struct symbol *sym)
1071 struct expression *assign, *symbol;
1073 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1074 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1075 fake_element_assigns(sym, call_global_assign_hooks);
1076 } else if (sym->initializer) {
1077 symbol = symbol_expression(sym);
1078 assign = assign_expression(symbol, sym->initializer);
1079 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1080 } else {
1081 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1083 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1084 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1085 fake_member_assigns(sym, call_global_assign_hooks);
1086 } else if (sym->initializer) {
1087 symbol = symbol_expression(sym);
1088 assign = assign_expression(symbol, sym->initializer);
1089 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1090 } else {
1091 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1093 } else {
1094 symbol = symbol_expression(sym);
1095 if (sym->initializer)
1096 assign = assign_expression(symbol, sym->initializer);
1097 else
1098 assign = assign_expression(symbol, zero_expr());
1099 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1103 static void start_function_definition(struct symbol *sym)
1105 __in_function_def = 1;
1106 __pass_to_client(sym, FUNC_DEF_HOOK);
1107 __in_function_def = 0;
1108 __pass_to_client(sym, AFTER_DEF_HOOK);
1112 static void split_function(struct symbol *sym)
1114 struct symbol *base_type = get_base_type(sym);
1116 cur_func_sym = sym;
1117 if (sym->ident)
1118 cur_func = sym->ident->name;
1119 __smatch_lineno = sym->pos.line;
1120 last_stmt = NULL;
1121 loop_count = 0;
1122 sm_debug("new function: %s\n", cur_func);
1123 __slist_id = 0;
1124 if (option_two_passes) {
1125 __unnullify_path();
1126 loop_num = 0;
1127 final_pass = 0;
1128 start_function_definition(sym);
1129 __split_stmt(base_type->stmt);
1130 __split_stmt(base_type->inline_stmt);
1131 nullify_path();
1133 __unnullify_path();
1134 loop_num = 0;
1135 start_function_definition(sym);
1136 __split_stmt(base_type->stmt);
1137 __split_stmt(base_type->inline_stmt);
1138 __pass_to_client(sym, END_FUNC_HOOK);
1139 __pass_to_client(sym, AFTER_FUNC_HOOK);
1140 cur_func_sym = NULL;
1141 cur_func = NULL;
1142 clear_all_states();
1143 free_data_info_allocs();
1144 free_expression_stack(&switch_expr_stack);
1145 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1146 __bail_on_rest_of_function = 0;
1149 static void parse_inline(struct expression *call)
1151 struct symbol *base_type;
1152 int loop_num_bak = loop_num;
1153 int final_pass_bak = final_pass;
1154 char *cur_func_bak = cur_func;
1155 struct statement_list *big_statement_stack_bak = big_statement_stack;
1156 struct expression_list *big_expression_stack_bak = big_expression_stack;
1157 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1158 struct symbol *cur_func_sym_bak = cur_func_sym;
1160 __pass_to_client(call, INLINE_FN_START);
1161 final_pass = 0; /* don't print anything */
1162 __inline_fn = call;
1164 base_type = get_base_type(call->fn->symbol);
1165 cur_func_sym = call->fn->symbol;
1166 if (call->fn->symbol->ident)
1167 cur_func = call->fn->symbol->ident->name;
1168 else
1169 cur_func = NULL;
1170 set_position(call->fn->symbol->pos);
1172 save_all_states();
1173 nullify_all_states();
1174 big_statement_stack = NULL;
1175 big_expression_stack = NULL;
1176 switch_expr_stack = NULL;
1178 sm_debug("inline function: %s\n", cur_func);
1179 __unnullify_path();
1180 loop_num = 0;
1181 start_function_definition(call->fn->symbol);
1182 __split_stmt(base_type->stmt);
1183 __split_stmt(base_type->inline_stmt);
1184 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1185 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1187 free_expression_stack(&switch_expr_stack);
1188 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1189 nullify_path();
1191 loop_num = loop_num_bak;
1192 final_pass = final_pass_bak;
1193 cur_func_sym = cur_func_sym_bak;
1194 cur_func = cur_func_bak;
1195 big_statement_stack = big_statement_stack_bak;
1196 big_expression_stack = big_expression_stack_bak;
1197 switch_expr_stack = switch_expr_stack_bak;
1199 restore_all_states();
1200 set_position(call->pos);
1201 __inline_fn = NULL;
1202 __pass_to_client(call, INLINE_FN_END);
1205 static struct symbol_list *inlines_called;
1206 static void add_inline_function(struct symbol *sym)
1208 static struct symbol_list *already_added;
1209 struct symbol *tmp;
1211 FOR_EACH_PTR(already_added, tmp) {
1212 if (tmp == sym)
1213 return;
1214 } END_FOR_EACH_PTR(tmp);
1216 add_ptr_list(&already_added, sym);
1217 add_ptr_list(&inlines_called, sym);
1220 static void process_inlines()
1222 struct symbol *tmp;
1224 FOR_EACH_PTR(inlines_called, tmp) {
1225 split_function(tmp);
1226 } END_FOR_EACH_PTR(tmp);
1227 free_ptr_list(&inlines_called);
1230 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1232 struct symbol *sym;
1234 FOR_EACH_PTR_REVERSE(big_list, sym) {
1235 if (!sym->scope)
1236 continue;
1237 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1238 return sym;
1239 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1240 return sym;
1241 } END_FOR_EACH_PTR_REVERSE(sym);
1243 return NULL;
1246 static void split_inlines_in_scope(struct symbol *sym)
1248 struct symbol *base;
1249 struct symbol_list *scope_list;
1250 int stream;
1252 scope_list = sym->scope->symbols;
1253 stream = sym->pos.stream;
1255 /* find the last static symbol in the file */
1256 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1257 if (sym->pos.stream != stream)
1258 continue;
1259 if (sym->type != SYM_NODE)
1260 continue;
1261 base = get_base_type(sym);
1262 if (!base)
1263 continue;
1264 if (base->type != SYM_FN)
1265 continue;
1266 if (!base->inline_stmt)
1267 continue;
1268 add_inline_function(sym);
1269 } END_FOR_EACH_PTR_REVERSE(sym);
1271 process_inlines();
1274 static void split_inlines(struct symbol_list *sym_list)
1276 struct symbol *sym;
1278 sym = get_last_scoped_symbol(sym_list, 0);
1279 if (sym)
1280 split_inlines_in_scope(sym);
1281 sym = get_last_scoped_symbol(sym_list, 1);
1282 if (sym)
1283 split_inlines_in_scope(sym);
1286 static void split_functions(struct symbol_list *sym_list)
1288 struct symbol *sym;
1290 FOR_EACH_PTR(sym_list, sym) {
1291 set_position(sym->pos);
1292 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1293 split_function(sym);
1294 process_inlines();
1295 } else {
1296 __pass_to_client(sym, BASE_HOOK);
1297 fake_global_assign(sym);
1299 } END_FOR_EACH_PTR(sym);
1300 split_inlines(sym_list);
1301 __pass_to_client(sym_list, END_FILE_HOOK);
1304 void smatch(int argc, char **argv)
1307 struct string_list *filelist = NULL;
1308 struct symbol_list *sym_list;
1310 if (argc < 2) {
1311 printf("Usage: smatch [--debug] <filename.c>\n");
1312 exit(1);
1314 sparse_initialize(argc, argv, &filelist);
1315 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1316 if (option_file_output) {
1317 char buf[256];
1319 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1320 sm_outfd = fopen(buf, "w");
1321 if (!sm_outfd) {
1322 printf("Error: Cannot open %s\n", base_file);
1323 exit(1);
1326 sym_list = sparse_keep_tokens(base_file);
1327 split_functions(sym_list);
1328 } END_FOR_EACH_PTR_NOTAG(base_file);