buf_size: handle dma_alloc_attrs()
[smatch.git] / smatch_flow.c
blob7e86c28b0f06ed44ee1bc2cc17c84d378d46176b
1 /*
2 * sparse/smatch_flow.c
4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #define _GNU_SOURCE 1
11 #include <unistd.h>
12 #include <stdio.h>
13 #include "token.h"
14 #include "smatch.h"
15 #include "smatch_expression_stacks.h"
16 #include "smatch_extra.h"
17 #include "smatch_slist.h"
19 int final_pass;
20 int __inline_call;
21 struct expression *__inline_fn;
23 static int __smatch_lineno = 0;
25 static char *base_file;
26 static const char *filename;
27 static char *pathname;
28 static char *full_filename;
29 static char *cur_func;
30 static int loop_count;
31 int __expr_stmt_count;
32 static struct expression_list *switch_expr_stack = NULL;
34 struct expression_list *big_expression_stack;
35 struct statement_list *big_statement_stack;
36 int __in_pre_condition = 0;
37 int __bail_on_rest_of_function = 0;
38 char *get_function(void) { return cur_func; }
39 int get_lineno(void) { return __smatch_lineno; }
40 int inside_loop(void) { return !!loop_count; }
41 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
42 int in_expression_statement(void) { return !!__expr_stmt_count; }
44 static void split_symlist(struct symbol_list *sym_list);
45 static void split_declaration(struct symbol_list *sym_list);
46 static void split_expr_list(struct expression_list *expr_list);
47 static void add_inline_function(struct symbol *sym);
48 static void parse_inline(struct expression *expr);
50 int option_assume_loops = 0;
51 int option_known_conditions = 0;
52 int option_two_passes = 0;
53 struct symbol *cur_func_sym = NULL;
55 const char *get_filename(void)
57 if (option_info)
58 return base_file;
59 if (option_full_path)
60 return full_filename;
61 return filename;
64 const char *get_base_file(void)
66 return base_file;
69 static void set_position(struct position pos)
71 int len;
72 static int prev_stream = -1;
74 __smatch_lineno = pos.line;
76 if (pos.stream == prev_stream)
77 return;
79 filename = stream_name(pos.stream);
81 free(full_filename);
82 pathname = getcwd(NULL, 0);
83 if (pathname) {
84 len = strlen(pathname) + 1 + strlen(filename) + 1;
85 full_filename = malloc(len);
86 snprintf(full_filename, len, "%s/%s", pathname, filename);
87 } else {
88 full_filename = alloc_string(filename);
90 free(pathname);
93 static int is_inline_func(struct expression *expr)
95 if (expr->type != EXPR_SYMBOL || !expr->symbol)
96 return 0;
97 if (expr->symbol->ctype.modifiers & MOD_INLINE)
98 return 1;
99 return 0;
102 static int is_noreturn_func(struct expression *expr)
104 if (expr->type != EXPR_SYMBOL || !expr->symbol)
105 return 0;
106 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
107 return 1;
108 return 0;
111 int inlinable(struct expression *expr)
113 struct symbol *sym;
115 if (__inline_fn) /* don't nest */
116 return 0;
118 if (expr->type != EXPR_SYMBOL || !expr->symbol)
119 return 0;
120 sym = get_base_type(expr->symbol);
121 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
122 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) <= 10)
123 return 1;
124 return 0;
126 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
127 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) <= 10)
128 return 1;
129 return 0;
131 return 0;
134 void __split_expr(struct expression *expr)
136 if (!expr)
137 return;
139 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
141 push_expression(&big_expression_stack, expr);
142 set_position(expr->pos);
143 __pass_to_client(expr, EXPR_HOOK);
145 switch (expr->type) {
146 case EXPR_PREOP:
147 if (expr->op == '*')
148 __pass_to_client(expr, DEREF_HOOK);
149 case EXPR_POSTOP:
150 __pass_to_client(expr, OP_HOOK);
151 __split_expr(expr->unop);
152 break;
153 case EXPR_STATEMENT:
154 __expr_stmt_count++;
155 __split_stmt(expr->statement);
156 __expr_stmt_count--;
157 break;
158 case EXPR_LOGICAL:
159 case EXPR_COMPARE:
160 __pass_to_client(expr, LOGIC_HOOK);
161 __handle_logic(expr);
162 break;
163 case EXPR_BINOP:
164 __pass_to_client(expr, BINOP_HOOK);
165 case EXPR_COMMA:
166 __split_expr(expr->left);
167 __split_expr(expr->right);
168 break;
169 case EXPR_ASSIGNMENT: {
170 struct expression *tmp;
172 if (!expr->right)
173 break;
175 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
177 /* foo = !bar() */
178 if (__handle_condition_assigns(expr))
179 break;
180 /* foo = (x < 5 ? foo : 5); */
181 if (__handle_select_assigns(expr))
182 break;
183 /* foo = ({frob(); frob(); frob(); 1;}) */
184 if (__handle_expr_statement_assigns(expr))
185 break;
187 __split_expr(expr->right);
188 __pass_to_client(expr, ASSIGNMENT_HOOK);
189 tmp = strip_expr(expr->right);
190 if (tmp->type == EXPR_CALL)
191 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
192 if (get_macro_name(tmp->pos) &&
193 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
194 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
195 __split_expr(expr->left);
196 break;
198 case EXPR_DEREF:
199 __pass_to_client(expr, DEREF_HOOK);
200 __split_expr(expr->deref);
201 break;
202 case EXPR_SLICE:
203 __split_expr(expr->base);
204 break;
205 case EXPR_CAST:
206 case EXPR_FORCE_CAST:
207 __pass_to_client(expr, CAST_HOOK);
208 __split_expr(expr->cast_expression);
209 break;
210 case EXPR_SIZEOF:
211 if (expr->cast_expression)
212 __pass_to_client(strip_parens(expr->cast_expression),
213 SIZEOF_HOOK);
214 break;
215 case EXPR_OFFSETOF:
216 case EXPR_ALIGNOF:
217 evaluate_expression(expr);
218 break;
219 case EXPR_CONDITIONAL:
220 case EXPR_SELECT:
221 if (known_condition_true(expr->conditional)) {
222 __split_expr(expr->cond_true);
223 break;
225 if (known_condition_false(expr->conditional)) {
226 __split_expr(expr->cond_false);
227 break;
229 __pass_to_client(expr, SELECT_HOOK);
230 __split_whole_condition(expr->conditional);
231 __split_expr(expr->cond_true);
232 __push_true_states();
233 __use_false_states();
234 __split_expr(expr->cond_false);
235 __merge_true_states();
236 break;
237 case EXPR_CALL:
238 if (sym_name_is("__builtin_constant_p", expr->fn))
239 break;
240 split_expr_list(expr->args);
241 __split_expr(expr->fn);
242 if (is_inline_func(expr->fn))
243 add_inline_function(expr->fn->symbol);
244 if (inlinable(expr->fn))
245 __inline_call = 1;
246 __pass_to_client(expr, FUNCTION_CALL_HOOK);
247 __inline_call = 0;
248 if (inlinable(expr->fn)) {
249 parse_inline(expr);
251 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
252 if (is_noreturn_func(expr->fn))
253 nullify_path();
254 break;
255 case EXPR_INITIALIZER:
256 split_expr_list(expr->expr_list);
257 break;
258 case EXPR_IDENTIFIER:
259 __split_expr(expr->ident_expression);
260 break;
261 case EXPR_INDEX:
262 __split_expr(expr->idx_expression);
263 break;
264 case EXPR_POS:
265 __split_expr(expr->init_expr);
266 break;
267 case EXPR_SYMBOL:
268 __pass_to_client(expr, SYM_HOOK);
269 break;
270 case EXPR_STRING:
271 __pass_to_client(expr, STRING_HOOK);
272 break;
273 default:
274 break;
276 pop_expression(&big_expression_stack);
279 static int is_forever_loop(struct statement *stmt)
281 struct expression *expr;
283 expr = strip_expr(stmt->iterator_pre_condition);
284 if (!expr)
285 expr = stmt->iterator_post_condition;
286 if (!expr) {
287 /* this is a for(;;) loop... */
288 return 1;
291 if (expr->type == EXPR_VALUE && expr->value == 1)
292 return 1;
294 return 0;
297 static int loop_num;
298 static char *get_loop_name(int num)
300 char buf[256];
302 snprintf(buf, 255, "-loop%d", num);
303 buf[255] = '\0';
304 return alloc_sname(buf);
308 * Pre Loops are while and for loops.
310 static void handle_pre_loop(struct statement *stmt)
312 int once_through; /* we go through the loop at least once */
313 struct sm_state *extra_sm = NULL;
314 int unchanged = 0;
315 char *loop_name;
316 struct state_list *slist = NULL;
317 struct sm_state *sm = NULL;
319 loop_name = get_loop_name(loop_num);
320 loop_num++;
322 __split_stmt(stmt->iterator_pre_statement);
324 once_through = implied_condition_true(stmt->iterator_pre_condition);
326 loop_count++;
327 __push_continues();
328 __push_breaks();
330 __merge_gotos(loop_name);
332 extra_sm = __extra_handle_canonical_loops(stmt, &slist);
333 __in_pre_condition++;
334 __pass_to_client(stmt, PRELOOP_HOOK);
335 __split_whole_condition(stmt->iterator_pre_condition);
336 __in_pre_condition--;
337 FOR_EACH_PTR(slist, sm) {
338 set_state(sm->owner, sm->name, sm->sym, sm->state);
339 } END_FOR_EACH_PTR(sm);
340 free_slist(&slist);
341 if (extra_sm)
342 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
344 if (option_assume_loops)
345 once_through = 1;
347 __split_stmt(stmt->iterator_statement);
348 __warn_on_silly_pre_loops();
349 if (is_forever_loop(stmt)) {
350 struct state_list *slist;
352 __save_gotos(loop_name);
354 __push_fake_cur_slist();
355 __split_stmt(stmt->iterator_post_statement);
356 slist = __pop_fake_cur_slist();
358 __discard_continues();
359 __discard_false_states();
360 __use_breaks();
362 if (!__path_is_null())
363 __merge_slist_into_cur(slist);
364 free_slist(&slist);
365 } else {
366 __merge_continues();
367 unchanged = __iterator_unchanged(extra_sm);
368 __split_stmt(stmt->iterator_post_statement);
369 __save_gotos(loop_name);
370 __split_whole_condition(stmt->iterator_pre_condition);
371 nullify_path();
372 __merge_false_states();
373 if (once_through)
374 __discard_false_states();
375 else
376 __merge_false_states();
378 if (extra_sm && unchanged)
379 __extra_pre_loop_hook_after(extra_sm,
380 stmt->iterator_post_statement,
381 stmt->iterator_pre_condition);
382 __merge_breaks();
384 loop_count--;
388 * Post loops are do {} while();
390 static void handle_post_loop(struct statement *stmt)
392 char *loop_name;
394 loop_name = get_loop_name(loop_num);
395 loop_num++;
396 loop_count++;
398 __push_continues();
399 __push_breaks();
400 __merge_gotos(loop_name);
401 __split_stmt(stmt->iterator_statement);
402 __merge_continues();
403 if (!is_zero(stmt->iterator_post_condition))
404 __save_gotos(loop_name);
406 if (is_forever_loop(stmt)) {
407 __use_breaks();
408 } else {
409 __split_whole_condition(stmt->iterator_post_condition);
410 __use_false_states();
411 __merge_breaks();
413 loop_count--;
416 static int empty_statement(struct statement *stmt)
418 if (!stmt)
419 return 0;
420 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
421 return 1;
422 return 0;
425 static int last_stmt_on_same_line()
427 struct statement *stmt;
428 int i = 0;
430 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
431 if (!i++)
432 continue;
433 if (stmt->pos.line == get_lineno())
434 return 1;
435 return 0;
436 } END_FOR_EACH_PTR_REVERSE(stmt);
437 return 0;
440 static struct statement *last_stmt;
441 static int is_last_stmt(struct statement *stmt)
443 if (stmt == last_stmt)
444 return 1;
445 return 0;
448 static void print_unreached_initializers(struct symbol_list *sym_list)
450 struct symbol *sym;
452 FOR_EACH_PTR(sym_list, sym) {
453 if (sym->initializer)
454 sm_msg("info: '%s' is not actually initialized (unreached code).",
455 (sym->ident ? sym->ident->name : "this variable"));
456 } END_FOR_EACH_PTR(sym);
459 static void print_unreached(struct statement *stmt)
461 static int print = 1;
463 if (!__path_is_null()) {
464 print = 1;
465 return;
467 if (!print)
468 return;
470 switch (stmt->type) {
471 case STMT_COMPOUND: /* after a switch before a case stmt */
472 case STMT_RANGE:
473 case STMT_CASE:
474 case STMT_LABEL:
475 return;
476 case STMT_DECLARATION: /* switch (x) { int a; case foo: ... */
477 print_unreached_initializers(stmt->declaration);
478 return;
479 case STMT_RETURN: /* gcc complains if you don't have a return statement */
480 if (is_last_stmt(stmt))
481 return;
482 break;
483 case STMT_GOTO:
484 if (!option_spammy)
485 return;
486 break;
487 default:
488 break;
490 if (!option_spammy && empty_statement(stmt))
491 return;
492 sm_msg("info: ignoring unreachable code.");
493 print = 0;
496 static void split_asm_constraints(struct expression_list *expr_list)
498 struct expression *expr;
499 int state = 0;
501 FOR_EACH_PTR(expr_list, expr) {
502 switch (state) {
503 case 0: /* identifier */
504 case 1: /* constraint */
505 state++;
506 continue;
507 case 2: /* expression */
508 state = 0;
509 __split_expr(expr);
510 continue;
512 } END_FOR_EACH_PTR(expr);
515 static int is_case_val(struct statement *stmt, sval_t sval)
517 sval_t case_sval;
519 if (stmt->type != STMT_CASE)
520 return 0;
521 if (!stmt->case_expression) {
522 __set_default();
523 return 1;
525 if (!get_value(stmt->case_expression, &case_sval))
526 return 0;
527 if (case_sval.value == sval.value)
528 return 1;
529 return 0;
532 static void split_known_switch(struct statement *stmt, sval_t sval)
534 struct statement *tmp;
536 __split_expr(stmt->switch_expression);
538 push_expression(&switch_expr_stack, stmt->switch_expression);
539 __save_switch_states(top_expression(switch_expr_stack));
540 nullify_path();
541 __push_default();
542 __push_breaks();
544 stmt = stmt->switch_statement;
546 if (!last_stmt)
547 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
549 __push_scope_hooks();
550 FOR_EACH_PTR(stmt->stmts, tmp) {
551 __smatch_lineno = tmp->pos.line;
552 if (is_case_val(tmp, sval)) {
553 __merge_switches(top_expression(switch_expr_stack),
554 stmt->case_expression);
555 __pass_case_to_client(top_expression(switch_expr_stack),
556 stmt->case_expression);
558 if (__path_is_null())
559 continue;
560 __split_stmt(tmp);
561 if (__path_is_null()) {
562 __set_default();
563 goto out;
565 } END_FOR_EACH_PTR(tmp);
566 out:
567 __call_scope_hooks();
568 if (!__pop_default())
569 __merge_switches(top_expression(switch_expr_stack),
570 NULL);
571 __discard_switches();
572 __merge_breaks();
573 pop_expression(&switch_expr_stack);
576 void __split_stmt(struct statement *stmt)
578 sval_t sval;
580 if (!stmt)
581 return;
583 if (out_of_memory() || __bail_on_rest_of_function) {
584 static char *printed = NULL;
586 if (printed != cur_func)
587 sm_msg("Function too hairy. Giving up.");
588 final_pass = 0; /* turn off sm_msg() from here */
589 printed = cur_func;
590 return;
593 add_ptr_list(&big_statement_stack, stmt);
594 free_expression_stack(&big_expression_stack);
595 set_position(stmt->pos);
596 print_unreached(stmt);
597 __pass_to_client(stmt, STMT_HOOK);
599 switch (stmt->type) {
600 case STMT_DECLARATION:
601 split_declaration(stmt->declaration);
602 return;
603 case STMT_RETURN:
604 __split_expr(stmt->ret_value);
605 __pass_to_client(stmt->ret_value, RETURN_HOOK);
606 nullify_path();
607 return;
608 case STMT_EXPRESSION:
609 __split_expr(stmt->expression);
610 return;
611 case STMT_COMPOUND: {
612 struct statement *tmp;
614 if (!last_stmt)
615 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
616 __push_scope_hooks();
617 FOR_EACH_PTR(stmt->stmts, tmp) {
618 __split_stmt(tmp);
619 } END_FOR_EACH_PTR(tmp);
620 __call_scope_hooks();
621 return;
623 case STMT_IF:
624 if (known_condition_true(stmt->if_conditional)) {
625 __split_stmt(stmt->if_true);
626 return;
628 if (known_condition_false(stmt->if_conditional)) {
629 __split_stmt(stmt->if_false);
630 return;
632 if (option_known_conditions &&
633 implied_condition_true(stmt->if_conditional)) {
634 sm_info("this condition is true.");
635 __split_stmt(stmt->if_true);
636 return;
638 if (option_known_conditions &&
639 implied_condition_false(stmt->if_conditional)) {
640 sm_info("this condition is false.");
641 __split_stmt(stmt->if_false);
642 return;
644 __split_whole_condition(stmt->if_conditional);
645 __split_stmt(stmt->if_true);
646 if (empty_statement(stmt->if_true) &&
647 last_stmt_on_same_line() &&
648 !get_macro_name(stmt->if_true->pos))
649 sm_msg("warn: if();");
650 __push_true_states();
651 __use_false_states();
652 __split_stmt(stmt->if_false);
653 __merge_true_states();
654 return;
655 case STMT_ITERATOR:
656 if (stmt->iterator_pre_condition)
657 handle_pre_loop(stmt);
658 else if (stmt->iterator_post_condition)
659 handle_post_loop(stmt);
660 else {
661 // these are for(;;) type loops.
662 handle_pre_loop(stmt);
664 return;
665 case STMT_SWITCH:
666 if (get_value(stmt->switch_expression, &sval)) {
667 split_known_switch(stmt, sval);
668 return;
670 __split_expr(stmt->switch_expression);
671 push_expression(&switch_expr_stack, stmt->switch_expression);
672 __save_switch_states(top_expression(switch_expr_stack));
673 nullify_path();
674 __push_default();
675 __push_breaks();
676 __split_stmt(stmt->switch_statement);
677 if (!__pop_default())
678 __merge_switches(top_expression(switch_expr_stack),
679 NULL);
680 __discard_switches();
681 __merge_breaks();
682 pop_expression(&switch_expr_stack);
683 return;
684 case STMT_CASE:
685 __merge_switches(top_expression(switch_expr_stack),
686 stmt->case_expression);
687 __pass_case_to_client(top_expression(switch_expr_stack),
688 stmt->case_expression);
689 if (!stmt->case_expression)
690 __set_default();
691 __split_expr(stmt->case_expression);
692 __split_expr(stmt->case_to);
693 __split_stmt(stmt->case_statement);
694 return;
695 case STMT_LABEL:
696 if (stmt->label_identifier &&
697 stmt->label_identifier->type == SYM_LABEL &&
698 stmt->label_identifier->ident) {
699 loop_count = 1000000;
700 __merge_gotos(stmt->label_identifier->ident->name);
702 __split_stmt(stmt->label_statement);
703 return;
704 case STMT_GOTO:
705 __split_expr(stmt->goto_expression);
706 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
707 if (!strcmp(stmt->goto_label->ident->name, "break")) {
708 __process_breaks();
709 } else if (!strcmp(stmt->goto_label->ident->name,
710 "continue")) {
711 __process_continues();
713 } else if (stmt->goto_label &&
714 stmt->goto_label->type == SYM_LABEL &&
715 stmt->goto_label->ident) {
716 __save_gotos(stmt->goto_label->ident->name);
718 nullify_path();
719 return;
720 case STMT_NONE:
721 return;
722 case STMT_ASM:
723 __pass_to_client(stmt, ASM_HOOK);
724 __split_expr(stmt->asm_string);
725 split_asm_constraints(stmt->asm_outputs);
726 split_asm_constraints(stmt->asm_inputs);
727 split_asm_constraints(stmt->asm_clobbers);
728 return;
729 case STMT_CONTEXT:
730 return;
731 case STMT_RANGE:
732 __split_expr(stmt->range_expression);
733 __split_expr(stmt->range_low);
734 __split_expr(stmt->range_high);
735 return;
739 static void split_expr_list(struct expression_list *expr_list)
741 struct expression *expr;
743 FOR_EACH_PTR(expr_list, expr) {
744 __split_expr(expr);
745 } END_FOR_EACH_PTR(expr);
748 static void split_sym(struct symbol *sym)
750 if (!sym)
751 return;
752 if (!(sym->namespace & NS_SYMBOL))
753 return;
755 __split_stmt(sym->stmt);
756 __split_expr(sym->array_size);
757 split_symlist(sym->arguments);
758 split_symlist(sym->symbol_list);
759 __split_stmt(sym->inline_stmt);
760 split_symlist(sym->inline_symbol_list);
763 static void split_symlist(struct symbol_list *sym_list)
765 struct symbol *sym;
767 FOR_EACH_PTR(sym_list, sym) {
768 split_sym(sym);
769 } END_FOR_EACH_PTR(sym);
772 static void fake_member_assigns(struct symbol *sym)
774 struct expression *symbol, *deref, *assign, *tmp;
776 symbol = symbol_expression(sym);
777 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
778 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
779 continue;
780 deref = member_expression(symbol, '.', tmp->expr_ident);
781 assign = assign_expression(deref, tmp->ident_expression);
782 __split_expr(assign);
783 } END_FOR_EACH_PTR(tmp);
786 static void fake_assign_expr(struct symbol *sym)
788 struct expression *assign, *symbol;
790 symbol = symbol_expression(sym);
791 assign = assign_expression(symbol, sym->initializer);
792 __split_expr(assign);
795 static void do_initializer_stuff(struct symbol *sym)
797 if (!sym->initializer)
798 return;
799 if (sym->initializer->type == EXPR_INITIALIZER)
800 fake_member_assigns(sym);
801 else
802 fake_assign_expr(sym);
805 static void split_declaration(struct symbol_list *sym_list)
807 struct symbol *sym;
809 FOR_EACH_PTR(sym_list, sym) {
810 __pass_to_client(sym, DECLARATION_HOOK);
811 do_initializer_stuff(sym);
812 split_sym(sym);
813 } END_FOR_EACH_PTR(sym);
816 static void fake_global_assign(struct symbol *sym)
818 struct expression *assign, *symbol;
820 if (!sym->initializer)
821 return;
822 if (sym->initializer->type == EXPR_INITIALIZER) {
823 struct expression *deref, *tmp;
825 symbol = symbol_expression(sym);
826 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
827 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
828 continue;
829 deref = member_expression(symbol, '.', tmp->expr_ident);
830 assign = assign_expression(deref, tmp->ident_expression);
831 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
832 } END_FOR_EACH_PTR(tmp);
833 } else {
834 symbol = symbol_expression(sym);
835 assign = assign_expression(symbol, sym->initializer);
836 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
840 static void split_function(struct symbol *sym)
842 struct symbol *base_type = get_base_type(sym);
844 cur_func_sym = sym;
845 if (sym->ident)
846 cur_func = sym->ident->name;
847 __smatch_lineno = sym->pos.line;
848 last_stmt = NULL;
849 loop_count = 0;
850 sm_debug("new function: %s\n", cur_func);
851 __slist_id = 0;
852 if (option_two_passes) {
853 __unnullify_path();
854 loop_num = 0;
855 final_pass = 0;
856 __pass_to_client(sym, FUNC_DEF_HOOK);
857 __pass_to_client(sym, AFTER_DEF_HOOK);
858 __split_stmt(base_type->stmt);
859 __split_stmt(base_type->inline_stmt);
860 nullify_path();
862 __unnullify_path();
863 loop_num = 0;
864 final_pass = 1;
865 __pass_to_client(sym, FUNC_DEF_HOOK);
866 __pass_to_client(sym, AFTER_DEF_HOOK);
867 __split_stmt(base_type->stmt);
868 __split_stmt(base_type->inline_stmt);
869 __pass_to_client(sym, END_FUNC_HOOK);
870 cur_func = NULL;
871 clear_all_states();
872 free_data_info_allocs();
873 free_expression_stack(&switch_expr_stack);
874 __free_ptr_list((struct ptr_list **)&big_statement_stack);
875 __bail_on_rest_of_function = 0;
878 static void parse_inline(struct expression *call)
880 struct symbol *base_type;
881 int loop_num_bak = loop_num;
882 int final_pass_bak = final_pass;
883 char *cur_func_bak = cur_func;
884 struct statement_list *big_statement_stack_bak = big_statement_stack;
885 struct expression_list *big_expression_stack_bak = big_expression_stack;
886 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
887 struct symbol *cur_func_sym_bak = cur_func_sym;
889 __pass_to_client(call, INLINE_FN_START);
890 final_pass = 0; /* don't print anything */
891 __inline_fn = call;
893 base_type = get_base_type(call->fn->symbol);
894 cur_func_sym = call->fn->symbol;
895 if (call->fn->symbol->ident)
896 cur_func = call->fn->symbol->ident->name;
897 else
898 cur_func = NULL;
899 set_position(call->fn->symbol->pos);
901 save_all_states();
902 nullify_all_states();
903 big_statement_stack = NULL;
904 big_expression_stack = NULL;
905 switch_expr_stack = NULL;
907 sm_debug("inline function: %s\n", cur_func);
908 __unnullify_path();
909 loop_num = 0;
910 __pass_to_client(call->fn->symbol, FUNC_DEF_HOOK);
911 __pass_to_client(call->fn->symbol, AFTER_DEF_HOOK);
912 __split_stmt(base_type->stmt);
913 __split_stmt(base_type->inline_stmt);
914 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
916 free_expression_stack(&switch_expr_stack);
917 __free_ptr_list((struct ptr_list **)&big_statement_stack);
918 nullify_path();
920 loop_num = loop_num_bak;
921 final_pass = final_pass_bak;
922 cur_func_sym = cur_func_sym_bak;
923 cur_func = cur_func_bak;
924 big_statement_stack = big_statement_stack_bak;
925 big_expression_stack = big_expression_stack_bak;
926 switch_expr_stack = switch_expr_stack_bak;
928 restore_all_states();
929 set_position(call->pos);
930 __inline_fn = NULL;
931 __pass_to_client(call, INLINE_FN_END);
934 static struct symbol_list *inlines_called;
935 static void add_inline_function(struct symbol *sym)
937 static struct symbol_list *already_added;
938 struct symbol *tmp;
940 FOR_EACH_PTR(already_added, tmp) {
941 if (tmp == sym)
942 return;
943 } END_FOR_EACH_PTR(tmp);
945 add_ptr_list(&already_added, sym);
946 add_ptr_list(&inlines_called, sym);
949 static void process_inlines()
951 struct symbol *tmp;
953 FOR_EACH_PTR(inlines_called, tmp) {
954 split_function(tmp);
955 } END_FOR_EACH_PTR(tmp);
956 free_ptr_list(&inlines_called);
959 static void split_functions(struct symbol_list *sym_list)
961 struct symbol *sym;
963 FOR_EACH_PTR(sym_list, sym) {
964 set_position(sym->pos);
965 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
966 split_function(sym);
967 process_inlines();
968 } else {
969 __pass_to_client(sym, BASE_HOOK);
970 fake_global_assign(sym);
972 } END_FOR_EACH_PTR(sym);
973 __pass_to_client_no_data(END_FILE_HOOK);
976 void smatch(int argc, char **argv)
979 struct string_list *filelist = NULL;
980 struct symbol_list *sym_list;
982 if (argc < 2) {
983 printf("Usage: smatch [--debug] <filename.c>\n");
984 exit(1);
986 sparse_initialize(argc, argv, &filelist);
987 FOR_EACH_PTR_NOTAG(filelist, base_file) {
988 if (option_file_output) {
989 char buf[256];
991 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
992 sm_outfd = fopen(buf, "w");
993 if (!sm_outfd) {
994 printf("Error: Cannot open %s\n", base_file);
995 exit(1);
998 sym_list = sparse_keep_tokens(base_file);
999 split_functions(sym_list);
1000 } END_FOR_EACH_PTR_NOTAG(base_file);