smatch: remove --known_conditions option
[smatch.git] / smatch_flow.c
bloba9605bb17b57c79eb7d04afa7b4e317e54f606e6
1 /*
2 * Copyright (C) 2006,2008 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #define _GNU_SOURCE 1
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "token.h"
22 #include "scope.h"
23 #include "smatch.h"
24 #include "smatch_expression_stacks.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 int __in_fake_assign;
29 int final_pass;
30 int __inline_call;
31 struct expression *__inline_fn;
33 static int __smatch_lineno = 0;
35 static char *base_file;
36 static const char *filename;
37 static char *pathname;
38 static char *full_filename;
39 static char *cur_func;
40 static unsigned int loop_count;
41 static int last_goto_statement_handled;
42 int __expr_stmt_count;
43 int __in_function_def;
44 static struct expression_list *switch_expr_stack = NULL;
45 static struct expression_list *post_op_stack = NULL;
47 struct expression_list *big_expression_stack;
48 struct statement_list *big_statement_stack;
49 struct statement *__prev_stmt;
50 struct statement *__cur_stmt;
51 struct statement *__next_stmt;
52 int __in_pre_condition = 0;
53 int __bail_on_rest_of_function = 0;
54 static struct timeval fn_start_time;
55 char *get_function(void) { return cur_func; }
56 int get_lineno(void) { return __smatch_lineno; }
57 int inside_loop(void) { return !!loop_count; }
58 int definitely_inside_loop(void) { return !!(loop_count & ~0x80000000); }
59 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
60 int in_expression_statement(void) { return !!__expr_stmt_count; }
62 static void split_symlist(struct symbol_list *sym_list);
63 static void split_declaration(struct symbol_list *sym_list);
64 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
65 static void add_inline_function(struct symbol *sym);
66 static void parse_inline(struct expression *expr);
68 int option_assume_loops = 0;
69 int option_two_passes = 0;
70 struct symbol *cur_func_sym = NULL;
71 struct stree *global_states;
73 long long valid_ptr_min = 4096;
74 long long valid_ptr_max = 2117777777;
75 sval_t valid_ptr_min_sval = {
76 .type = &ptr_ctype,
77 {.value = 4096},
79 sval_t valid_ptr_max_sval = {
80 .type = &ptr_ctype,
81 {.value = LONG_MAX - 100000},
84 static void set_valid_ptr_max(void)
86 if (type_bits(&ptr_ctype) == 32)
87 valid_ptr_max = 2117777777;
88 else if (type_bits(&ptr_ctype) == 64)
89 valid_ptr_max = 2117777777777777777LL;
91 valid_ptr_max_sval.value = valid_ptr_max;
94 int outside_of_function(void)
96 return cur_func_sym == NULL;
99 const char *get_filename(void)
101 if (option_info)
102 return base_file;
103 if (option_full_path)
104 return full_filename;
105 return filename;
108 const char *get_base_file(void)
110 return base_file;
113 static void set_position(struct position pos)
115 int len;
116 static int prev_stream = -1;
118 if (pos.stream == 0 && pos.line == 0)
119 return;
121 __smatch_lineno = pos.line;
123 if (pos.stream == prev_stream)
124 return;
126 filename = stream_name(pos.stream);
128 free(full_filename);
129 pathname = getcwd(NULL, 0);
130 if (pathname) {
131 len = strlen(pathname) + 1 + strlen(filename) + 1;
132 full_filename = malloc(len);
133 snprintf(full_filename, len, "%s/%s", pathname, filename);
134 } else {
135 full_filename = alloc_string(filename);
137 free(pathname);
140 static void set_parent(struct expression *expr, struct expression *parent)
142 if (!expr)
143 return;
144 expr->parent = parent;
147 static void set_parent_stmt(struct statement *stmt, struct statement *parent)
149 if (!stmt)
150 return;
151 stmt->parent = parent;
154 int is_assigned_call(struct expression *expr)
156 struct expression *tmp;
158 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
159 if (tmp->type == EXPR_ASSIGNMENT && tmp->op == '=' &&
160 strip_expr(tmp->right) == expr)
161 return 1;
162 if (tmp->pos.line < expr->pos.line)
163 return 0;
164 } END_FOR_EACH_PTR_REVERSE(tmp);
165 return 0;
168 static int is_inline_func(struct expression *expr)
170 if (expr->type != EXPR_SYMBOL || !expr->symbol)
171 return 0;
172 if (expr->symbol->ctype.modifiers & MOD_INLINE)
173 return 1;
174 return 0;
177 static int is_noreturn_func(struct expression *expr)
179 if (expr->type != EXPR_SYMBOL || !expr->symbol)
180 return 0;
181 if (expr->symbol->ctype.modifiers & MOD_NORETURN)
182 return 1;
183 return 0;
186 int inlinable(struct expression *expr)
188 struct symbol *sym;
189 struct statement *last_stmt = NULL;
191 if (__inline_fn) /* don't nest */
192 return 0;
194 if (expr->type != EXPR_SYMBOL || !expr->symbol)
195 return 0;
196 if (is_no_inline_function(expr->symbol->ident->name))
197 return 0;
198 sym = get_base_type(expr->symbol);
199 if (sym->stmt && sym->stmt->type == STMT_COMPOUND) {
200 if (ptr_list_size((struct ptr_list *)sym->stmt->stmts) > 10)
201 return 0;
202 if (sym->stmt->type != STMT_COMPOUND)
203 return 0;
204 last_stmt = last_ptr_list((struct ptr_list *)sym->stmt->stmts);
206 if (sym->inline_stmt && sym->inline_stmt->type == STMT_COMPOUND) {
207 if (ptr_list_size((struct ptr_list *)sym->inline_stmt->stmts) > 10)
208 return 0;
209 if (sym->inline_stmt->type != STMT_COMPOUND)
210 return 0;
211 last_stmt = last_ptr_list((struct ptr_list *)sym->inline_stmt->stmts);
214 if (!last_stmt)
215 return 0;
217 /* the magic numbers in this function are pulled out of my bum. */
218 if (last_stmt->pos.line > sym->pos.line + 20)
219 return 0;
221 return 1;
224 void __process_post_op_stack(void)
226 struct expression *expr;
228 FOR_EACH_PTR(post_op_stack, expr) {
229 __pass_to_client(expr, OP_HOOK);
230 } END_FOR_EACH_PTR(expr);
232 __free_ptr_list((struct ptr_list **)&post_op_stack);
235 static int handle_comma_assigns(struct expression *expr)
237 struct expression *right;
238 struct expression *assign;
240 right = strip_expr(expr->right);
241 if (right->type != EXPR_COMMA)
242 return 0;
244 __split_expr(right->left);
245 __process_post_op_stack();
247 assign = assign_expression(expr->left, right->right);
248 __split_expr(assign);
250 return 1;
253 static int prev_expression_is_getting_address(struct expression *expr)
255 struct expression *parent;
257 do {
258 parent = expr->parent;
260 if (!parent)
261 return 0;
262 if (parent->type == EXPR_PREOP && parent->op == '&')
263 return 1;
264 if (parent->type == EXPR_PREOP && parent->op == '(')
265 goto next;
266 if (parent->type == EXPR_DEREF && parent->op == '.')
267 goto next;
269 return 0;
270 next:
271 expr = parent;
272 } while (1);
275 void __split_expr(struct expression *expr)
277 if (!expr)
278 return;
280 // sm_msg(" Debug expr_type %d %s", expr->type, show_special(expr->op));
282 if (__in_fake_assign && expr->type != EXPR_ASSIGNMENT)
283 return;
284 if (__in_fake_assign >= 4) /* don't allow too much nesting */
285 return;
287 push_expression(&big_expression_stack, expr);
288 set_position(expr->pos);
289 __pass_to_client(expr, EXPR_HOOK);
291 switch (expr->type) {
292 case EXPR_PREOP:
293 set_parent(expr->unop, expr);
295 if (expr->op == '*' &&
296 !prev_expression_is_getting_address(expr))
297 __pass_to_client(expr, DEREF_HOOK);
298 __split_expr(expr->unop);
299 __pass_to_client(expr, OP_HOOK);
300 break;
301 case EXPR_POSTOP:
302 set_parent(expr->unop, expr);
304 __split_expr(expr->unop);
305 push_expression(&post_op_stack, expr);
306 break;
307 case EXPR_STATEMENT:
308 __expr_stmt_count++;
309 __split_stmt(expr->statement);
310 __expr_stmt_count--;
311 break;
312 case EXPR_LOGICAL:
313 case EXPR_COMPARE:
314 set_parent(expr->left, expr);
315 set_parent(expr->right, expr);
317 __pass_to_client(expr, LOGIC_HOOK);
318 __handle_logic(expr);
319 break;
320 case EXPR_BINOP:
321 set_parent(expr->left, expr);
322 set_parent(expr->right, expr);
324 __pass_to_client(expr, BINOP_HOOK);
325 case EXPR_COMMA:
326 set_parent(expr->left, expr);
327 set_parent(expr->right, expr);
329 __split_expr(expr->left);
330 __process_post_op_stack();
331 __split_expr(expr->right);
332 break;
333 case EXPR_ASSIGNMENT: {
334 struct expression *tmp;
336 set_parent(expr->left, expr);
337 set_parent(expr->right, expr);
339 if (!expr->right)
340 break;
342 __pass_to_client(expr, RAW_ASSIGNMENT_HOOK);
344 /* foo = !bar() */
345 if (__handle_condition_assigns(expr))
346 break;
347 /* foo = (x < 5 ? foo : 5); */
348 if (__handle_select_assigns(expr))
349 break;
350 /* foo = ({frob(); frob(); frob(); 1;}) */
351 if (__handle_expr_statement_assigns(expr))
352 break;
353 /* foo = (3, 4); */
354 if (handle_comma_assigns(expr))
355 break;
357 __split_expr(expr->right);
358 if (outside_of_function())
359 __pass_to_client(expr, GLOBAL_ASSIGNMENT_HOOK);
360 else
361 __pass_to_client(expr, ASSIGNMENT_HOOK);
363 __fake_struct_member_assignments(expr);
365 tmp = strip_expr(expr->right);
366 if (expr->op == '=' && tmp->type == EXPR_CALL) {
367 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
368 if (!is_fake_call(tmp))
369 __pass_to_client(tmp, FUNCTION_CALL_HOOK_AFTER);
371 if (get_macro_name(tmp->pos) &&
372 get_macro_name(expr->pos) != get_macro_name(tmp->pos))
373 __pass_to_client(expr, MACRO_ASSIGNMENT_HOOK);
374 __split_expr(expr->left);
375 break;
377 case EXPR_DEREF:
378 set_parent(expr->deref, expr);
380 __pass_to_client(expr, DEREF_HOOK);
381 __split_expr(expr->deref);
382 break;
383 case EXPR_SLICE:
384 set_parent(expr->base, expr);
386 __split_expr(expr->base);
387 break;
388 case EXPR_CAST:
389 case EXPR_FORCE_CAST:
390 set_parent(expr->cast_expression, expr);
392 __pass_to_client(expr, CAST_HOOK);
393 __split_expr(expr->cast_expression);
394 break;
395 case EXPR_SIZEOF:
396 if (expr->cast_expression)
397 __pass_to_client(strip_parens(expr->cast_expression),
398 SIZEOF_HOOK);
399 break;
400 case EXPR_OFFSETOF:
401 case EXPR_ALIGNOF:
402 evaluate_expression(expr);
403 break;
404 case EXPR_CONDITIONAL:
405 case EXPR_SELECT:
406 set_parent(expr->conditional, expr);
407 set_parent(expr->cond_true, expr);
408 set_parent(expr->cond_false, expr);
410 if (known_condition_true(expr->conditional)) {
411 __split_expr(expr->cond_true);
412 break;
414 if (known_condition_false(expr->conditional)) {
415 __split_expr(expr->cond_false);
416 break;
418 __pass_to_client(expr, SELECT_HOOK);
419 __split_whole_condition(expr->conditional);
420 __split_expr(expr->cond_true);
421 __push_true_states();
422 __use_false_states();
423 __split_expr(expr->cond_false);
424 __merge_true_states();
425 break;
426 case EXPR_CALL:
427 set_parent(expr->fn, expr);
429 if (sym_name_is("__builtin_constant_p", expr->fn))
430 break;
431 split_expr_list(expr->args, expr);
432 __split_expr(expr->fn);
433 if (is_inline_func(expr->fn))
434 add_inline_function(expr->fn->symbol);
435 if (inlinable(expr->fn))
436 __inline_call = 1;
437 __process_post_op_stack();
438 __pass_to_client(expr, FUNCTION_CALL_HOOK);
439 __inline_call = 0;
440 if (inlinable(expr->fn)) {
441 parse_inline(expr);
443 __pass_to_client(expr, CALL_HOOK_AFTER_INLINE);
444 if (!is_assigned_call(expr))
445 __pass_to_client(expr, FUNCTION_CALL_HOOK_AFTER);
446 if (is_noreturn_func(expr->fn))
447 nullify_path();
448 break;
449 case EXPR_INITIALIZER:
450 split_expr_list(expr->expr_list, expr);
451 break;
452 case EXPR_IDENTIFIER:
453 set_parent(expr->ident_expression, expr);
454 __split_expr(expr->ident_expression);
455 break;
456 case EXPR_INDEX:
457 set_parent(expr->idx_expression, expr);
458 __split_expr(expr->idx_expression);
459 break;
460 case EXPR_POS:
461 set_parent(expr->init_expr, expr);
462 __split_expr(expr->init_expr);
463 break;
464 case EXPR_SYMBOL:
465 __pass_to_client(expr, SYM_HOOK);
466 break;
467 case EXPR_STRING:
468 __pass_to_client(expr, STRING_HOOK);
469 break;
470 default:
471 break;
473 pop_expression(&big_expression_stack);
476 static int is_forever_loop(struct statement *stmt)
478 struct expression *expr;
480 expr = strip_expr(stmt->iterator_pre_condition);
481 if (!expr)
482 expr = stmt->iterator_post_condition;
483 if (!expr) {
484 /* this is a for(;;) loop... */
485 return 1;
488 if (expr->type == EXPR_VALUE && expr->value == 1)
489 return 1;
491 return 0;
494 static int loop_num;
495 static char *get_loop_name(int num)
497 char buf[256];
499 snprintf(buf, 255, "-loop%d", num);
500 buf[255] = '\0';
501 return alloc_sname(buf);
505 * Pre Loops are while and for loops.
507 static void handle_pre_loop(struct statement *stmt)
509 int once_through; /* we go through the loop at least once */
510 struct sm_state *extra_sm = NULL;
511 int unchanged = 0;
512 char *loop_name;
513 struct stree *stree = NULL;
514 struct sm_state *sm = NULL;
516 loop_name = get_loop_name(loop_num);
517 loop_num++;
519 __split_stmt(stmt->iterator_pre_statement);
520 __prev_stmt = stmt->iterator_pre_statement;
522 once_through = implied_condition_true(stmt->iterator_pre_condition);
524 loop_count++;
525 __push_continues();
526 __push_breaks();
528 __merge_gotos(loop_name);
530 extra_sm = __extra_handle_canonical_loops(stmt, &stree);
531 __in_pre_condition++;
532 __pass_to_client(stmt, PRELOOP_HOOK);
533 __split_whole_condition(stmt->iterator_pre_condition);
534 __in_pre_condition--;
535 FOR_EACH_SM(stree, sm) {
536 set_state(sm->owner, sm->name, sm->sym, sm->state);
537 } END_FOR_EACH_SM(sm);
538 free_stree(&stree);
539 if (extra_sm)
540 extra_sm = get_sm_state(extra_sm->owner, extra_sm->name, extra_sm->sym);
542 if (option_assume_loops)
543 once_through = 1;
545 __split_stmt(stmt->iterator_statement);
546 if (is_forever_loop(stmt)) {
547 __merge_continues();
548 __save_gotos(loop_name);
550 __push_fake_cur_stree();
551 __split_stmt(stmt->iterator_post_statement);
552 stree = __pop_fake_cur_stree();
554 __discard_false_states();
555 __use_breaks();
557 if (!__path_is_null())
558 __merge_stree_into_cur(stree);
559 free_stree(&stree);
560 } else {
561 __merge_continues();
562 unchanged = __iterator_unchanged(extra_sm);
563 __split_stmt(stmt->iterator_post_statement);
564 __prev_stmt = stmt->iterator_post_statement;
565 __cur_stmt = stmt;
567 __save_gotos(loop_name);
568 __in_pre_condition++;
569 __split_whole_condition(stmt->iterator_pre_condition);
570 __in_pre_condition--;
571 nullify_path();
572 __merge_false_states();
573 if (once_through)
574 __discard_false_states();
575 else
576 __merge_false_states();
578 if (extra_sm && unchanged)
579 __extra_pre_loop_hook_after(extra_sm,
580 stmt->iterator_post_statement,
581 stmt->iterator_pre_condition);
582 __merge_breaks();
584 loop_count--;
588 * Post loops are do {} while();
590 static void handle_post_loop(struct statement *stmt)
592 char *loop_name;
594 loop_name = get_loop_name(loop_num);
595 loop_num++;
596 loop_count++;
598 __push_continues();
599 __push_breaks();
600 __merge_gotos(loop_name);
601 __split_stmt(stmt->iterator_statement);
602 __merge_continues();
603 if (!is_zero(stmt->iterator_post_condition))
604 __save_gotos(loop_name);
606 if (is_forever_loop(stmt)) {
607 __use_breaks();
608 } else {
609 __split_whole_condition(stmt->iterator_post_condition);
610 __use_false_states();
611 __merge_breaks();
613 loop_count--;
616 static int empty_statement(struct statement *stmt)
618 if (!stmt)
619 return 0;
620 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
621 return 1;
622 return 0;
625 static int last_stmt_on_same_line(void)
627 struct statement *stmt;
628 int i = 0;
630 FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
631 if (!i++)
632 continue;
633 if (stmt->pos.line == get_lineno())
634 return 1;
635 return 0;
636 } END_FOR_EACH_PTR_REVERSE(stmt);
637 return 0;
640 static void split_asm_constraints(struct expression_list *expr_list)
642 struct expression *expr;
643 int state = 0;
645 FOR_EACH_PTR(expr_list, expr) {
646 switch (state) {
647 case 0: /* identifier */
648 case 1: /* constraint */
649 state++;
650 continue;
651 case 2: /* expression */
652 state = 0;
653 __split_expr(expr);
654 continue;
656 } END_FOR_EACH_PTR(expr);
659 static int is_case_val(struct statement *stmt, sval_t sval)
661 sval_t case_sval;
663 if (stmt->type != STMT_CASE)
664 return 0;
665 if (!stmt->case_expression) {
666 __set_default();
667 return 1;
669 if (!get_value(stmt->case_expression, &case_sval))
670 return 0;
671 if (case_sval.value == sval.value)
672 return 1;
673 return 0;
676 static void split_known_switch(struct statement *stmt, sval_t sval)
678 struct statement *tmp;
680 __split_expr(stmt->switch_expression);
682 push_expression(&switch_expr_stack, stmt->switch_expression);
683 __save_switch_states(top_expression(switch_expr_stack));
684 nullify_path();
685 __push_default();
686 __push_breaks();
688 stmt = stmt->switch_statement;
690 __push_scope_hooks();
691 FOR_EACH_PTR(stmt->stmts, tmp) {
692 __smatch_lineno = tmp->pos.line;
693 if (is_case_val(tmp, sval)) {
694 __merge_switches(top_expression(switch_expr_stack),
695 stmt->case_expression, stmt->case_to);
696 __pass_case_to_client(top_expression(switch_expr_stack),
697 stmt->case_expression);
699 if (__path_is_null())
700 continue;
701 __split_stmt(tmp);
702 if (__path_is_null()) {
703 __set_default();
704 goto out;
706 } END_FOR_EACH_PTR(tmp);
707 out:
708 __call_scope_hooks();
709 if (!__pop_default())
710 __merge_switches(top_expression(switch_expr_stack),
711 NULL, NULL);
712 __discard_switches();
713 __merge_breaks();
714 pop_expression(&switch_expr_stack);
717 static int taking_too_long(void)
719 int ms;
721 ms = ms_since(&fn_start_time);
722 if (ms > 1000 * 60 * 5) /* five minutes */
723 return 1;
724 return 0;
727 static int is_last_stmt(struct statement *cur_stmt)
729 struct symbol *fn = get_base_type(cur_func_sym);
730 struct statement *stmt;
732 if (!fn)
733 return 0;
734 stmt = fn->stmt;
735 if (!stmt)
736 stmt = fn->inline_stmt;
737 if (!stmt || stmt->type != STMT_COMPOUND)
738 return 0;
739 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
740 if (stmt && stmt->type == STMT_LABEL)
741 stmt = stmt->label_statement;
742 if (stmt == cur_stmt)
743 return 1;
744 return 0;
747 static void handle_backward_goto(struct statement *goto_stmt)
749 const char *goto_name, *label_name;
750 struct statement *func_stmt;
751 struct symbol *base_type = get_base_type(cur_func_sym);
752 struct statement *tmp;
753 int found = 0;
755 if (!option_info)
756 return;
757 if (last_goto_statement_handled)
758 return;
759 last_goto_statement_handled = 1;
761 if (!goto_stmt->goto_label ||
762 goto_stmt->goto_label->type != SYM_LABEL ||
763 !goto_stmt->goto_label->ident)
764 return;
765 goto_name = goto_stmt->goto_label->ident->name;
767 func_stmt = base_type->stmt;
768 if (!func_stmt)
769 func_stmt = base_type->inline_stmt;
770 if (!func_stmt)
771 return;
772 if (func_stmt->type != STMT_COMPOUND)
773 return;
775 FOR_EACH_PTR(func_stmt->stmts, tmp) {
776 if (!found) {
777 if (tmp->type != STMT_LABEL)
778 continue;
779 if (!tmp->label_identifier ||
780 tmp->label_identifier->type != SYM_LABEL ||
781 !tmp->label_identifier->ident)
782 continue;
783 label_name = tmp->label_identifier->ident->name;
784 if (strcmp(goto_name, label_name) != 0)
785 continue;
786 found = 1;
788 __split_stmt(tmp);
789 } END_FOR_EACH_PTR(tmp);
792 static void fake_a_return(void)
794 struct symbol *return_type;
796 nullify_path();
797 __unnullify_path();
799 return_type = get_real_base_type(cur_func_sym);
800 return_type = get_real_base_type(return_type);
801 if (return_type != &void_ctype) {
802 __pass_to_client(unknown_value_expression(NULL), RETURN_HOOK);
803 nullify_path();
806 __pass_to_client(cur_func_sym, END_FUNC_HOOK);
807 __pass_to_client(cur_func_sym, AFTER_FUNC_HOOK);
810 static void split_compound(struct statement *stmt)
812 struct statement *prev = NULL;
813 struct statement *cur = NULL;
814 struct statement *next;
816 __push_scope_hooks();
818 FOR_EACH_PTR(stmt->stmts, next) {
819 /* just set them all ahead of time */
820 set_parent_stmt(next, stmt);
822 if (cur) {
823 __prev_stmt = prev;
824 __next_stmt = next;
825 __cur_stmt = cur;
826 __split_stmt(cur);
828 prev = cur;
829 cur = next;
830 } END_FOR_EACH_PTR(next);
831 if (cur) {
832 __prev_stmt = prev;
833 __cur_stmt = cur;
834 __next_stmt = NULL;
835 __split_stmt(cur);
838 __call_scope_hooks();
841 void __split_stmt(struct statement *stmt)
843 sval_t sval;
845 if (!stmt)
846 goto out;
848 if (__bail_on_rest_of_function || out_of_memory() || taking_too_long()) {
849 static char *printed = NULL;
851 __bail_on_rest_of_function = 1;
852 if (printed != cur_func)
853 sm_msg("Function too hairy. Giving up.");
854 fake_a_return();
855 final_pass = 0; /* turn off sm_msg() from here */
856 printed = cur_func;
857 return;
860 add_ptr_list(&big_statement_stack, stmt);
861 free_expression_stack(&big_expression_stack);
862 set_position(stmt->pos);
863 __pass_to_client(stmt, STMT_HOOK);
865 switch (stmt->type) {
866 case STMT_DECLARATION:
867 split_declaration(stmt->declaration);
868 break;
869 case STMT_RETURN:
870 __split_expr(stmt->ret_value);
871 __pass_to_client(stmt->ret_value, RETURN_HOOK);
872 __process_post_op_stack();
873 nullify_path();
874 break;
875 case STMT_EXPRESSION:
876 __split_expr(stmt->expression);
877 break;
878 case STMT_COMPOUND:
879 split_compound(stmt);
880 break;
881 case STMT_IF:
882 set_parent_stmt(stmt->if_true, stmt);
883 set_parent_stmt(stmt->if_false, stmt);
885 if (known_condition_true(stmt->if_conditional)) {
886 __split_stmt(stmt->if_true);
887 break;
889 if (known_condition_false(stmt->if_conditional)) {
890 __split_stmt(stmt->if_false);
891 break;
893 __split_whole_condition(stmt->if_conditional);
894 __split_stmt(stmt->if_true);
895 if (empty_statement(stmt->if_true) &&
896 last_stmt_on_same_line() &&
897 !get_macro_name(stmt->if_true->pos))
898 sm_msg("warn: if();");
899 __push_true_states();
900 __use_false_states();
901 __split_stmt(stmt->if_false);
902 __merge_true_states();
903 break;
904 case STMT_ITERATOR:
905 set_parent_stmt(stmt->iterator_pre_statement, stmt);
906 set_parent_stmt(stmt->iterator_statement, stmt);
907 set_parent_stmt(stmt->iterator_post_statement, stmt);
909 if (stmt->iterator_pre_condition)
910 handle_pre_loop(stmt);
911 else if (stmt->iterator_post_condition)
912 handle_post_loop(stmt);
913 else {
914 // these are for(;;) type loops.
915 handle_pre_loop(stmt);
917 break;
918 case STMT_SWITCH:
919 set_parent_stmt(stmt->switch_statement, stmt);
921 if (get_value(stmt->switch_expression, &sval)) {
922 split_known_switch(stmt, sval);
923 break;
925 __split_expr(stmt->switch_expression);
926 push_expression(&switch_expr_stack, stmt->switch_expression);
927 __save_switch_states(top_expression(switch_expr_stack));
928 nullify_path();
929 __push_default();
930 __push_breaks();
931 __split_stmt(stmt->switch_statement);
932 if (!__pop_default())
933 __merge_switches(top_expression(switch_expr_stack),
934 NULL, NULL);
935 __discard_switches();
936 __merge_breaks();
937 pop_expression(&switch_expr_stack);
938 break;
939 case STMT_CASE:
940 __merge_switches(top_expression(switch_expr_stack),
941 stmt->case_expression, stmt->case_to);
942 __pass_case_to_client(top_expression(switch_expr_stack),
943 stmt->case_expression);
944 if (!stmt->case_expression)
945 __set_default();
946 __split_expr(stmt->case_expression);
947 __split_expr(stmt->case_to);
948 __split_stmt(stmt->case_statement);
949 break;
950 case STMT_LABEL:
951 if (stmt->label_identifier &&
952 stmt->label_identifier->type == SYM_LABEL &&
953 stmt->label_identifier->ident) {
954 loop_count |= 0x80000000;
955 __merge_gotos(stmt->label_identifier->ident->name);
957 __split_stmt(stmt->label_statement);
958 break;
959 case STMT_GOTO:
960 __split_expr(stmt->goto_expression);
961 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
962 if (!strcmp(stmt->goto_label->ident->name, "break")) {
963 __process_breaks();
964 } else if (!strcmp(stmt->goto_label->ident->name,
965 "continue")) {
966 __process_continues();
968 } else if (stmt->goto_label &&
969 stmt->goto_label->type == SYM_LABEL &&
970 stmt->goto_label->ident) {
971 __save_gotos(stmt->goto_label->ident->name);
973 nullify_path();
974 if (is_last_stmt(stmt))
975 handle_backward_goto(stmt);
976 break;
977 case STMT_NONE:
978 break;
979 case STMT_ASM:
980 __pass_to_client(stmt, ASM_HOOK);
981 __split_expr(stmt->asm_string);
982 split_asm_constraints(stmt->asm_outputs);
983 split_asm_constraints(stmt->asm_inputs);
984 split_asm_constraints(stmt->asm_clobbers);
985 break;
986 case STMT_CONTEXT:
987 break;
988 case STMT_RANGE:
989 __split_expr(stmt->range_expression);
990 __split_expr(stmt->range_low);
991 __split_expr(stmt->range_high);
992 break;
994 __pass_to_client(stmt, STMT_HOOK_AFTER);
995 out:
996 __process_post_op_stack();
999 static void split_expr_list(struct expression_list *expr_list, struct expression *parent)
1001 struct expression *expr;
1003 FOR_EACH_PTR(expr_list, expr) {
1004 set_parent(expr, parent);
1005 __split_expr(expr);
1006 __process_post_op_stack();
1007 } END_FOR_EACH_PTR(expr);
1010 static void split_sym(struct symbol *sym)
1012 if (!sym)
1013 return;
1014 if (!(sym->namespace & NS_SYMBOL))
1015 return;
1017 __split_stmt(sym->stmt);
1018 __split_expr(sym->array_size);
1019 split_symlist(sym->arguments);
1020 split_symlist(sym->symbol_list);
1021 __split_stmt(sym->inline_stmt);
1022 split_symlist(sym->inline_symbol_list);
1025 static void split_symlist(struct symbol_list *sym_list)
1027 struct symbol *sym;
1029 FOR_EACH_PTR(sym_list, sym) {
1030 split_sym(sym);
1031 } END_FOR_EACH_PTR(sym);
1034 typedef void (fake_cb)(struct expression *expr);
1036 static int member_to_number(struct expression *expr, struct ident *member)
1038 struct symbol *type, *tmp;
1039 char *name;
1040 int i;
1042 if (!member)
1043 return -1;
1044 name = member->name;
1046 type = get_type(expr);
1047 if (!type || type->type != SYM_STRUCT)
1048 return -1;
1050 i = -1;
1051 FOR_EACH_PTR(type->symbol_list, tmp) {
1052 i++;
1053 if (!tmp->ident)
1054 continue;
1055 if (strcmp(name, tmp->ident->name) == 0)
1056 return i;
1057 } END_FOR_EACH_PTR(tmp);
1058 return -1;
1061 static struct ident *number_to_member(struct expression *expr, int num)
1063 struct symbol *type, *member;
1064 int i = 0;
1066 type = get_type(expr);
1067 if (!type || type->type != SYM_STRUCT)
1068 return NULL;
1070 FOR_EACH_PTR(type->symbol_list, member) {
1071 if (i == num)
1072 return member->ident;
1073 i++;
1074 } END_FOR_EACH_PTR(member);
1075 return NULL;
1078 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb);
1080 struct member_set {
1081 struct ident *ident;
1082 int set;
1085 static struct member_set *alloc_member_set(struct symbol *type)
1087 struct member_set *member_set;
1088 struct symbol *member;
1089 int member_count;
1090 int member_idx;
1092 member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1093 member_set = malloc(member_count * sizeof(*member_set));
1094 member_idx = 0;
1095 FOR_EACH_PTR(type->symbol_list, member) {
1096 member_set[member_idx].ident = member->ident;
1097 member_set[member_idx].set = 0;
1098 member_idx++;
1099 } END_FOR_EACH_PTR(member);
1101 return member_set;
1104 static void mark_member_as_set(struct symbol *type, struct member_set *member_set, struct ident *ident)
1106 int member_count = ptr_list_size((struct ptr_list *)type->symbol_list);
1107 int i;
1109 for (i = 0; i < member_count; i++) {
1110 if (member_set[i].ident == ident) {
1111 member_set[i].set = 1;
1112 return;
1115 // crap. this is buggy.
1116 // sm_msg("internal smatch error in initializer %s.%s", type->ident->name, ident->name);
1119 static void set_inner_struct_members(struct expression *expr, struct symbol *member)
1121 struct expression *edge_member, *assign;
1122 struct symbol *base = get_real_base_type(member);
1123 struct symbol *tmp;
1125 if (member->ident)
1126 expr = member_expression(expr, '.', member->ident);
1128 FOR_EACH_PTR(base->symbol_list, tmp) {
1129 struct symbol *type;
1131 type = get_real_base_type(tmp);
1132 if (!type)
1133 continue;
1135 if (tmp->ident) {
1136 edge_member = member_expression(expr, '.', tmp->ident);
1137 if (get_state_expr(SMATCH_EXTRA, edge_member))
1138 continue;
1141 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1142 set_inner_struct_members(expr, tmp);
1143 continue;
1146 if (!tmp->ident)
1147 continue;
1149 assign = assign_expression(edge_member, zero_expr());
1150 __split_expr(assign);
1151 } END_FOR_EACH_PTR(tmp);
1156 static void set_unset_to_zero(struct symbol *type, struct expression *expr)
1158 struct symbol *tmp;
1159 struct expression *member, *assign;
1160 int op = '*';
1162 if (expr->type == EXPR_PREOP && expr->op == '&') {
1163 expr = strip_expr(expr->unop);
1164 op = '.';
1167 FOR_EACH_PTR(type->symbol_list, tmp) {
1168 type = get_real_base_type(tmp);
1169 if (!type)
1170 continue;
1172 if (tmp->ident) {
1173 member = member_expression(expr, op, tmp->ident);
1174 if (get_state_expr(SMATCH_EXTRA, member))
1175 continue;
1178 if (type->type == SYM_UNION || type->type == SYM_STRUCT) {
1179 set_inner_struct_members(expr, tmp);
1180 continue;
1182 if (type->type == SYM_ARRAY)
1183 continue;
1184 if (!tmp->ident)
1185 continue;
1187 assign = assign_expression(member, zero_expr());
1188 __split_expr(assign);
1189 } END_FOR_EACH_PTR(tmp);
1192 static void fake_member_assigns_helper(struct expression *symbol, struct expression_list *members, fake_cb *fake_cb)
1194 struct expression *deref, *assign, *tmp;
1195 struct symbol *struct_type, *type;
1196 struct ident *member;
1197 int member_idx;
1198 struct member_set *member_set;
1200 struct_type = get_type(symbol);
1201 if (!struct_type ||
1202 (struct_type->type != SYM_STRUCT && struct_type->type != SYM_UNION))
1203 return;
1205 member_set = alloc_member_set(struct_type);
1207 member_idx = 0;
1208 FOR_EACH_PTR(members, tmp) {
1209 member = number_to_member(symbol, member_idx);
1210 while (tmp->type == EXPR_IDENTIFIER) {
1211 member = tmp->expr_ident;
1212 member_idx = member_to_number(symbol, member);
1213 tmp = tmp->ident_expression;
1215 mark_member_as_set(struct_type, member_set, member);
1216 member_idx++;
1217 deref = member_expression(symbol, '.', member);
1218 if (tmp->type == EXPR_INITIALIZER) {
1219 type = get_type(deref);
1220 if (type && type->type == SYM_ARRAY)
1221 fake_element_assigns_helper(deref, tmp->expr_list, fake_cb);
1222 else
1223 fake_member_assigns_helper(deref, tmp->expr_list, fake_cb);
1224 } else {
1225 assign = assign_expression(deref, tmp);
1226 fake_cb(assign);
1228 } END_FOR_EACH_PTR(tmp);
1230 set_unset_to_zero(struct_type, symbol);
1233 static void fake_member_assigns(struct symbol *sym, fake_cb *fake_cb)
1235 fake_member_assigns_helper(symbol_expression(sym),
1236 sym->initializer->expr_list, fake_cb);
1239 static void fake_element_assigns_helper(struct expression *array, struct expression_list *expr_list, fake_cb *fake_cb)
1241 struct expression *offset, *binop, *assign, *tmp;
1242 struct symbol *type;
1243 int idx;
1245 if (ptr_list_size((struct ptr_list *)expr_list) > 1000)
1246 return;
1248 idx = 0;
1249 FOR_EACH_PTR(expr_list, tmp) {
1250 if (tmp->type == EXPR_INDEX) {
1251 if (tmp->idx_from != tmp->idx_to)
1252 return;
1253 idx = tmp->idx_from;
1254 if (!tmp->idx_expression)
1255 goto next;
1256 tmp = tmp->idx_expression;
1258 offset = value_expr(idx);
1259 binop = array_element_expression(array, offset);
1260 if (tmp->type == EXPR_INITIALIZER) {
1261 type = get_type(binop);
1262 if (type && type->type == SYM_ARRAY)
1263 fake_element_assigns_helper(binop, tmp->expr_list, fake_cb);
1264 else
1265 fake_member_assigns_helper(binop, tmp->expr_list, fake_cb);
1266 } else {
1267 assign = assign_expression(binop, tmp);
1268 fake_cb(assign);
1270 next:
1271 idx++;
1272 } END_FOR_EACH_PTR(tmp);
1275 static void fake_element_assigns(struct symbol *sym, fake_cb *fake_cb)
1277 fake_element_assigns_helper(symbol_expression(sym), sym->initializer->expr_list, fake_cb);
1280 static void fake_assign_expr(struct symbol *sym)
1282 struct expression *assign, *symbol;
1284 symbol = symbol_expression(sym);
1285 assign = assign_expression(symbol, sym->initializer);
1286 __split_expr(assign);
1289 static void call_split_expr(struct expression *expr)
1291 __split_expr(expr);
1294 static void do_initializer_stuff(struct symbol *sym)
1296 if (!sym->initializer)
1297 return;
1299 if (sym->initializer->type == EXPR_INITIALIZER) {
1300 if (get_real_base_type(sym)->type == SYM_ARRAY)
1301 fake_element_assigns(sym, call_split_expr);
1302 else
1303 fake_member_assigns(sym, call_split_expr);
1304 } else {
1305 fake_assign_expr(sym);
1309 static void split_declaration(struct symbol_list *sym_list)
1311 struct symbol *sym;
1313 FOR_EACH_PTR(sym_list, sym) {
1314 __pass_to_client(sym, DECLARATION_HOOK);
1315 do_initializer_stuff(sym);
1316 split_sym(sym);
1317 } END_FOR_EACH_PTR(sym);
1320 static void call_global_assign_hooks(struct expression *assign)
1322 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1325 static void fake_global_assign(struct symbol *sym)
1327 struct expression *assign, *symbol;
1329 if (get_real_base_type(sym)->type == SYM_ARRAY) {
1330 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1331 fake_element_assigns(sym, call_global_assign_hooks);
1332 } else if (sym->initializer) {
1333 symbol = symbol_expression(sym);
1334 assign = assign_expression(symbol, sym->initializer);
1335 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1336 } else {
1337 fake_element_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1339 } else if (get_real_base_type(sym)->type == SYM_STRUCT) {
1340 if (sym->initializer && sym->initializer->type == EXPR_INITIALIZER) {
1341 fake_member_assigns(sym, call_global_assign_hooks);
1342 } else if (sym->initializer) {
1343 symbol = symbol_expression(sym);
1344 assign = assign_expression(symbol, sym->initializer);
1345 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1346 } else {
1347 fake_member_assigns_helper(symbol_expression(sym), NULL, call_global_assign_hooks);
1349 } else {
1350 symbol = symbol_expression(sym);
1351 if (sym->initializer)
1352 assign = assign_expression(symbol, sym->initializer);
1353 else
1354 assign = assign_expression(symbol, zero_expr());
1355 __pass_to_client(assign, GLOBAL_ASSIGNMENT_HOOK);
1359 static void start_function_definition(struct symbol *sym)
1361 __in_function_def = 1;
1362 __pass_to_client(sym, FUNC_DEF_HOOK);
1363 __in_function_def = 0;
1364 __pass_to_client(sym, AFTER_DEF_HOOK);
1368 static void split_function(struct symbol *sym)
1370 struct symbol *base_type = get_base_type(sym);
1372 if (!base_type->stmt && !base_type->inline_stmt)
1373 return;
1375 gettimeofday(&fn_start_time, NULL);
1376 cur_func_sym = sym;
1377 if (sym->ident)
1378 cur_func = sym->ident->name;
1379 __smatch_lineno = sym->pos.line;
1380 loop_count = 0;
1381 last_goto_statement_handled = 0;
1382 sm_debug("new function: %s\n", cur_func);
1383 __stree_id = 0;
1384 if (option_two_passes) {
1385 __unnullify_path();
1386 loop_num = 0;
1387 final_pass = 0;
1388 start_function_definition(sym);
1389 __split_stmt(base_type->stmt);
1390 __split_stmt(base_type->inline_stmt);
1391 nullify_path();
1393 __unnullify_path();
1394 loop_num = 0;
1395 final_pass = 1;
1396 start_function_definition(sym);
1397 __split_stmt(base_type->stmt);
1398 __split_stmt(base_type->inline_stmt);
1399 __pass_to_client(sym, END_FUNC_HOOK);
1400 __pass_to_client(sym, AFTER_FUNC_HOOK);
1402 clear_all_states();
1403 cur_func_sym = NULL;
1404 cur_func = NULL;
1405 free_data_info_allocs();
1406 free_expression_stack(&switch_expr_stack);
1407 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1408 __bail_on_rest_of_function = 0;
1411 static void parse_inline(struct expression *call)
1413 struct symbol *base_type;
1414 int loop_num_bak = loop_num;
1415 int final_pass_bak = final_pass;
1416 char *cur_func_bak = cur_func;
1417 struct statement_list *big_statement_stack_bak = big_statement_stack;
1418 struct expression_list *big_expression_stack_bak = big_expression_stack;
1419 struct expression_list *switch_expr_stack_bak = switch_expr_stack;
1420 struct symbol *cur_func_sym_bak = cur_func_sym;
1422 __pass_to_client(call, INLINE_FN_START);
1423 final_pass = 0; /* don't print anything */
1424 __inline_fn = call;
1426 base_type = get_base_type(call->fn->symbol);
1427 cur_func_sym = call->fn->symbol;
1428 if (call->fn->symbol->ident)
1429 cur_func = call->fn->symbol->ident->name;
1430 else
1431 cur_func = NULL;
1432 set_position(call->fn->symbol->pos);
1434 save_all_states();
1435 big_statement_stack = NULL;
1436 big_expression_stack = NULL;
1437 switch_expr_stack = NULL;
1439 sm_debug("inline function: %s\n", cur_func);
1440 __unnullify_path();
1441 loop_num = 0;
1442 start_function_definition(call->fn->symbol);
1443 __split_stmt(base_type->stmt);
1444 __split_stmt(base_type->inline_stmt);
1445 __pass_to_client(call->fn->symbol, END_FUNC_HOOK);
1446 __pass_to_client(call->fn->symbol, AFTER_FUNC_HOOK);
1448 free_expression_stack(&switch_expr_stack);
1449 __free_ptr_list((struct ptr_list **)&big_statement_stack);
1450 nullify_path();
1451 free_goto_stack();
1453 loop_num = loop_num_bak;
1454 final_pass = final_pass_bak;
1455 cur_func_sym = cur_func_sym_bak;
1456 cur_func = cur_func_bak;
1457 big_statement_stack = big_statement_stack_bak;
1458 big_expression_stack = big_expression_stack_bak;
1459 switch_expr_stack = switch_expr_stack_bak;
1461 restore_all_states();
1462 set_position(call->pos);
1463 __inline_fn = NULL;
1464 __pass_to_client(call, INLINE_FN_END);
1467 static struct symbol_list *inlines_called;
1468 static void add_inline_function(struct symbol *sym)
1470 static struct symbol_list *already_added;
1471 struct symbol *tmp;
1473 FOR_EACH_PTR(already_added, tmp) {
1474 if (tmp == sym)
1475 return;
1476 } END_FOR_EACH_PTR(tmp);
1478 add_ptr_list(&already_added, sym);
1479 add_ptr_list(&inlines_called, sym);
1482 static void process_inlines(void)
1484 struct symbol *tmp;
1486 FOR_EACH_PTR(inlines_called, tmp) {
1487 split_function(tmp);
1488 } END_FOR_EACH_PTR(tmp);
1489 free_ptr_list(&inlines_called);
1492 static struct symbol *get_last_scoped_symbol(struct symbol_list *big_list, int use_static)
1494 struct symbol *sym;
1496 FOR_EACH_PTR_REVERSE(big_list, sym) {
1497 if (!sym->scope)
1498 continue;
1499 if (use_static && sym->ctype.modifiers & MOD_STATIC)
1500 return sym;
1501 if (!use_static && !(sym->ctype.modifiers & MOD_STATIC))
1502 return sym;
1503 } END_FOR_EACH_PTR_REVERSE(sym);
1505 return NULL;
1508 static void split_inlines_in_scope(struct symbol *sym)
1510 struct symbol *base;
1511 struct symbol_list *scope_list;
1512 int stream;
1514 scope_list = sym->scope->symbols;
1515 stream = sym->pos.stream;
1517 /* find the last static symbol in the file */
1518 FOR_EACH_PTR_REVERSE(scope_list, sym) {
1519 if (sym->pos.stream != stream)
1520 continue;
1521 if (sym->type != SYM_NODE)
1522 continue;
1523 base = get_base_type(sym);
1524 if (!base)
1525 continue;
1526 if (base->type != SYM_FN)
1527 continue;
1528 if (!base->inline_stmt)
1529 continue;
1530 add_inline_function(sym);
1531 } END_FOR_EACH_PTR_REVERSE(sym);
1533 process_inlines();
1536 static void split_inlines(struct symbol_list *sym_list)
1538 struct symbol *sym;
1540 sym = get_last_scoped_symbol(sym_list, 0);
1541 if (sym)
1542 split_inlines_in_scope(sym);
1543 sym = get_last_scoped_symbol(sym_list, 1);
1544 if (sym)
1545 split_inlines_in_scope(sym);
1548 static struct stree *clone_estates_perm(struct stree *orig)
1550 struct stree *ret = NULL;
1551 struct sm_state *tmp;
1553 FOR_EACH_SM(orig, tmp) {
1554 set_state_stree_perm(&ret, tmp->owner, tmp->name, tmp->sym, clone_estate_perm(tmp->state));
1555 } END_FOR_EACH_SM(tmp);
1557 return ret;
1560 static void split_functions(struct symbol_list *sym_list)
1562 struct symbol *sym;
1564 __unnullify_path();
1565 FOR_EACH_PTR(sym_list, sym) {
1566 set_position(sym->pos);
1567 if (sym->type != SYM_NODE || get_base_type(sym)->type != SYM_FN) {
1568 __pass_to_client(sym, BASE_HOOK);
1569 fake_global_assign(sym);
1571 } END_FOR_EACH_PTR(sym);
1572 global_states = clone_estates_perm(get_all_states_stree(SMATCH_EXTRA));
1573 nullify_path();
1575 FOR_EACH_PTR(sym_list, sym) {
1576 set_position(sym->pos);
1577 if (sym->type == SYM_NODE && get_base_type(sym)->type == SYM_FN) {
1578 split_function(sym);
1579 process_inlines();
1581 } END_FOR_EACH_PTR(sym);
1582 split_inlines(sym_list);
1583 __pass_to_client(sym_list, END_FILE_HOOK);
1586 void smatch(int argc, char **argv)
1589 struct string_list *filelist = NULL;
1590 struct symbol_list *sym_list;
1592 if (argc < 2) {
1593 printf("Usage: smatch [--debug] <filename.c>\n");
1594 exit(1);
1596 sparse_initialize(argc, argv, &filelist);
1597 set_valid_ptr_max();
1598 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1599 if (option_file_output) {
1600 char buf[256];
1602 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1603 sm_outfd = fopen(buf, "w");
1604 if (!sm_outfd) {
1605 printf("Error: Cannot open %s\n", base_file);
1606 exit(1);
1609 sym_list = sparse_keep_tokens(base_file);
1610 split_functions(sym_list);
1611 } END_FOR_EACH_PTR_NOTAG(base_file);