implied cleanup: rename left => lr
[smatch.git] / smatch_flow.c
blob5f93193c6be5055a9d7c118c4af725eb1bee75aa
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" // just for sname.
19 int final_pass;
21 static int __smatch_lineno = 0;
23 static char *filename;
24 static char *pathname;
25 static char *full_filename;
26 static char *cur_func;
27 static int line_func_start;
28 static struct expression_list *switch_expr_stack = NULL;
29 struct expression_list *big_expression_stack;
31 char *get_function(void) { return cur_func; }
32 int get_lineno(void) { return __smatch_lineno; }
33 int get_func_pos(void) { return __smatch_lineno - line_func_start; }
35 static void split_symlist(struct symbol_list *sym_list);
36 static void split_declaration(struct symbol_list *sym_list);
37 static void split_expr_list(struct expression_list *expr_list);
39 int option_assume_loops = 0;
40 int option_known_conditions = 0;
41 int option_two_passes = 0;
42 struct symbol *cur_func_sym = NULL;
44 char *get_filename(void)
46 if (option_full_path)
47 return full_filename;
48 return filename;
51 void __split_expr(struct expression *expr)
53 if (!expr)
54 return;
56 // printf("%d Debug expr_type %d %s\n", get_lineno(), expr->type, show_special(expr->op));
58 push_expression(&big_expression_stack, expr);
59 __smatch_lineno = expr->pos.line;
60 __pass_to_client(expr, EXPR_HOOK);
62 switch (expr->type) {
63 case EXPR_PREOP:
64 if (expr->op == '*')
65 __pass_to_client(expr, DEREF_HOOK);
66 case EXPR_POSTOP:
67 __pass_to_client(expr, OP_HOOK);
68 __split_expr(expr->unop);
69 return;
70 case EXPR_STATEMENT:
71 __split_statements(expr->statement);
72 return;
73 case EXPR_LOGICAL:
74 __split_whole_condition(expr);
75 __push_true_states();
76 __use_false_states();
77 __merge_true_states();
78 return;
79 case EXPR_BINOP:
80 __pass_to_client(expr, BINOP_HOOK);
81 case EXPR_COMMA:
82 case EXPR_COMPARE:
83 __split_expr(expr->left);
84 __split_expr(expr->right);
85 return;
86 case EXPR_ASSIGNMENT: {
87 struct expression *tmp;
89 __split_expr(expr->right);
90 __pass_to_client(expr, ASSIGNMENT_HOOK);
91 tmp = strip_expr(expr->right);
92 if (tmp->type == EXPR_CALL)
93 __pass_to_client(expr, CALL_ASSIGNMENT_HOOK);
94 __split_expr(expr->left);
95 return;
97 case EXPR_DEREF:
98 __pass_to_client(expr, DEREF_HOOK);
99 __split_expr(expr->deref);
100 return;
101 case EXPR_SLICE:
102 __split_expr(expr->base);
103 return;
104 case EXPR_CAST:
105 case EXPR_FORCE_CAST:
106 __split_expr(expr->cast_expression);
107 return;
108 case EXPR_SIZEOF:
109 /* there isn't anything to pass a client from inside a sizeof() */
110 return;
111 case EXPR_CONDITIONAL:
112 case EXPR_SELECT:
113 __split_whole_condition(expr->conditional);
114 __split_expr(expr->cond_true);
115 __push_true_states();
116 __use_false_states();
117 __split_expr(expr->cond_false);
118 __merge_true_states();
119 return;
120 case EXPR_CALL:
121 split_expr_list(expr->args);
122 __split_expr(expr->fn);
123 __pass_to_client(expr, FUNCTION_CALL_HOOK);
124 return;
125 case EXPR_INITIALIZER:
126 split_expr_list(expr->expr_list);
127 return;
128 case EXPR_IDENTIFIER:
129 __split_expr(expr->ident_expression);
130 return;
131 case EXPR_INDEX:
132 __split_expr(expr->idx_expression);
133 return;
134 case EXPR_POS:
135 __split_expr(expr->init_expr);
136 return;
137 case EXPR_SYMBOL:
138 __pass_to_client(expr, SYM_HOOK);
139 return;
140 default:
141 return;
145 static int is_forever_loop(struct statement *stmt)
148 struct expression *expr;
150 expr = strip_expr(stmt->iterator_pre_condition);
151 if (!expr)
152 expr = stmt->iterator_post_condition;
153 if (!expr) {
154 /* this is a for(;;) loop... */
155 return 1;
158 if (expr->type == EXPR_VALUE && expr->value == 1) {
159 return 1;
162 return 0;
165 static int loop_num;
166 static char *get_loop_name(int num)
168 char buf[256];
170 snprintf(buf, 255, "-loop%d", num);
171 buf[255] = '\0';
172 return alloc_sname(buf);;
176 * Pre Loops are while and for loops.
179 static void handle_pre_loop(struct statement *stmt)
181 int once_through; /* we go through the loop at least once */
182 struct sm_state *extra_state = NULL;
183 int unchanged = 0;
184 char *loop_name;
186 loop_name = get_loop_name(loop_num);
187 loop_num++;
189 __split_statements(stmt->iterator_pre_statement);
191 once_through = implied_condition_true(stmt->iterator_pre_condition);
193 __push_continues();
194 __push_breaks();
196 __merge_gotos(loop_name);
197 __split_whole_condition(stmt->iterator_pre_condition);
198 if (once_through)
199 extra_state = __extra_pre_loop_hook_before(stmt->iterator_pre_statement);
200 if (option_assume_loops)
201 once_through = 1;
202 __split_statements(stmt->iterator_statement);
204 __warn_on_silly_pre_loops();
205 if (is_forever_loop(stmt)) {
206 __save_gotos(loop_name);
207 /* forever loops don't have an iterator_post_statement */
208 __pop_continues();
209 __pop_false_states();
210 __use_breaks();
211 } else if (once_through) {
212 __merge_continues();
213 if (extra_state)
214 unchanged = __iterator_unchanged(extra_state, stmt->iterator_post_statement);
215 __split_statements(stmt->iterator_post_statement);
216 __save_gotos(loop_name);
217 __split_whole_condition(stmt->iterator_pre_condition);
218 nullify_path();
219 __merge_false_states();
220 if (extra_state && unchanged)
221 __extra_pre_loop_hook_after(extra_state,
222 stmt->iterator_post_statement, stmt->iterator_pre_condition);
223 __pop_false_states();
224 __merge_breaks();
225 } else {
226 __merge_continues();
227 __split_statements(stmt->iterator_post_statement);
228 __save_gotos(loop_name);
229 __split_whole_condition(stmt->iterator_pre_condition);
230 nullify_path();
231 __merge_false_states();
232 __merge_false_states();
233 __merge_breaks();
238 * Post loops are do {} while();
240 static void handle_post_loop(struct statement *stmt)
242 char *loop_name;
244 loop_name = get_loop_name(loop_num);
245 loop_num++;
247 __push_continues();
248 __push_breaks();
249 __merge_gotos(loop_name);
250 __split_statements(stmt->iterator_statement);
251 __merge_continues();
252 if (!is_zero(stmt->iterator_post_condition))
253 __save_gotos(loop_name);
255 if (is_forever_loop(stmt)) {
256 __use_breaks();
257 } else {
258 __split_whole_condition(stmt->iterator_post_condition);
259 __use_false_states();
260 __merge_breaks();
264 static void print_unreached(struct statement *stmt)
268 * GCC insists on a return statement even where it is never
269 * reached. Also BUG() sometimes is a forever loop and
270 * sometimes not so people put code after a BUG(). There
271 * are way to many false positives.
273 return;
275 if (__path_is_null()) {
276 switch(stmt->type) {
277 case STMT_COMPOUND: /* after a switch before a case stmt */
278 case STMT_CASE:
279 case STMT_LABEL:
280 case STMT_DECLARATION: /* switch(x) { int a; case foo: ... */
281 break;
282 default:
283 sm_msg("unreachable code. %d", stmt->type);
288 static int empty_statement(struct statement *stmt)
290 if (!stmt)
291 return 0;
292 if (stmt->type == STMT_EXPRESSION && !stmt->expression)
293 return 1;
294 return 0;
297 void __split_statements(struct statement *stmt)
299 if (!stmt)
300 return;
302 if (out_of_memory()) {
303 static char *printed = NULL;
305 if (printed != cur_func)
306 sm_msg("Function too big. Giving up.");
307 printed = cur_func;
308 return;
311 free_expression_stack(&big_expression_stack);
312 __smatch_lineno = stmt->pos.line;
313 print_unreached(stmt);
314 __pass_to_client(stmt, STMT_HOOK);
316 switch (stmt->type) {
317 case STMT_DECLARATION:
318 split_declaration(stmt->declaration);
319 return;
320 case STMT_RETURN:
321 __split_expr(stmt->ret_value);
322 __pass_to_client(stmt->ret_value, RETURN_HOOK);
323 nullify_path();
324 return;
325 case STMT_EXPRESSION:
326 __split_expr(stmt->expression);
327 return;
328 case STMT_COMPOUND: {
329 struct statement *s;
330 __push_scope_hooks();
331 FOR_EACH_PTR(stmt->stmts, s) {
332 __split_statements(s);
333 } END_FOR_EACH_PTR(s);
334 __call_scope_hooks();
335 return;
337 case STMT_IF:
338 if (known_condition_true(stmt->if_conditional)) {
339 __split_statements(stmt->if_true);
340 return;
342 if (known_condition_false(stmt->if_conditional)) {
343 __split_statements(stmt->if_false);
344 return;
346 if (option_known_conditions &&
347 implied_condition_true(stmt->if_conditional)) {
348 sm_info("this condition is true.");
349 __split_statements(stmt->if_true);
350 return;
352 if (option_known_conditions &&
353 implied_condition_false(stmt->if_conditional)) {
354 sm_info("this condition is false.");
355 __split_statements(stmt->if_false);
356 return;
358 __split_whole_condition(stmt->if_conditional);
359 __split_statements(stmt->if_true);
360 if (empty_statement(stmt->if_true))
361 sm_msg("warn: if();");
362 __push_true_states();
363 __use_false_states();
364 __split_statements(stmt->if_false);
365 __merge_true_states();
366 return;
367 case STMT_ITERATOR:
368 if (stmt->iterator_pre_condition)
369 handle_pre_loop(stmt);
370 else if (stmt->iterator_post_condition)
371 handle_post_loop(stmt);
372 else {
373 // these are for(;;) type loops.
374 handle_pre_loop(stmt);
376 return;
377 case STMT_SWITCH:
378 __split_expr(stmt->switch_expression);
379 push_expression(&switch_expr_stack, stmt->switch_expression);
380 __save_switch_states(top_expression(switch_expr_stack));
381 nullify_path();
382 __push_default();
383 __push_breaks();
384 __split_statements(stmt->switch_statement);
385 if (!__pop_default())
386 __merge_switches(top_expression(switch_expr_stack),
387 NULL);
388 __pop_switches();
389 __merge_breaks();
390 pop_expression(&switch_expr_stack);
391 return;
392 case STMT_CASE:
393 __merge_switches(top_expression(switch_expr_stack),
394 stmt->case_expression);
395 __pass_case_to_client(top_expression(switch_expr_stack),
396 stmt->case_expression);
397 if (!stmt->case_expression)
398 __set_default();
399 __split_expr(stmt->case_expression);
400 __split_expr(stmt->case_to);
401 __split_statements(stmt->case_statement);
402 return;
403 case STMT_LABEL:
404 if (stmt->label &&
405 stmt->label->type == SYM_LABEL &&
406 stmt->label->ident) {
407 __merge_gotos(stmt->label->ident->name);
409 __split_statements(stmt->label_statement);
410 return;
411 case STMT_GOTO:
412 __split_expr(stmt->goto_expression);
413 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
414 if (!strcmp(stmt->goto_label->ident->name, "break")) {
415 __process_breaks();
416 } else if (!strcmp(stmt->goto_label->ident->name,
417 "continue")) {
418 __process_continues();
420 } else if (stmt->goto_label &&
421 stmt->goto_label->type == SYM_LABEL &&
422 stmt->goto_label->ident) {
423 __save_gotos(stmt->goto_label->ident->name);
425 nullify_path();
426 return;
427 case STMT_NONE:
428 return;
429 case STMT_ASM:
430 __split_expr(stmt->asm_string);
431 //__split_expr(stmt->asm_outputs);
432 //__split_expr(stmt->asm_inputs);
433 //__split_expr(stmt->asm_clobbers);
434 return;
435 case STMT_CONTEXT:
436 return;
437 case STMT_RANGE:
438 __split_expr(stmt->range_expression);
439 __split_expr(stmt->range_low);
440 __split_expr(stmt->range_high);
441 return;
445 static void split_expr_list(struct expression_list *expr_list)
447 struct expression *expr;
448 FOR_EACH_PTR(expr_list, expr) {
449 __split_expr(expr);
450 } END_FOR_EACH_PTR(expr);
454 static void split_sym(struct symbol *sym)
456 if (!sym)
457 return;
458 if (!(sym->namespace & NS_SYMBOL))
459 return;
461 __split_statements(sym->stmt);
462 __split_expr(sym->array_size);
463 split_symlist(sym->arguments);
464 split_symlist(sym->symbol_list);
465 __split_statements(sym->inline_stmt);
466 split_symlist(sym->inline_symbol_list);
467 __split_expr(sym->initializer);
470 static void split_symlist(struct symbol_list *sym_list)
472 struct symbol *sym;
474 FOR_EACH_PTR(sym_list, sym) {
475 split_sym(sym);
476 } END_FOR_EACH_PTR(sym);
479 static struct expression *fake_assign_expr(struct symbol *sym)
481 struct expression *e_assign, *e_symbol;
483 e_assign = alloc_expression(sym->initializer->pos, EXPR_ASSIGNMENT);
484 e_symbol = alloc_expression(sym->initializer->pos, EXPR_SYMBOL);
485 e_assign->op = (int)'=';
486 e_symbol->symbol = sym;
487 e_symbol->symbol_name = sym->ident;
488 e_assign->left = e_symbol;
489 e_assign->right = sym->initializer;
490 return e_assign;
493 static void split_declaration(struct symbol_list *sym_list)
495 struct symbol *sym;
496 struct expression *assign, *tmp;
498 FOR_EACH_PTR(sym_list, sym) {
499 __pass_to_client(sym, DECLARATION_HOOK);
500 __split_expr(sym->initializer);
501 if(sym->initializer) {
502 assign = fake_assign_expr(sym);
503 __pass_to_client(assign, ASSIGNMENT_HOOK);
504 tmp = strip_expr(assign->right);
505 if (tmp->type == EXPR_CALL)
506 __pass_to_client(assign, CALL_ASSIGNMENT_HOOK);
508 split_sym(sym);
509 } END_FOR_EACH_PTR(sym);
512 static void split_functions(struct symbol_list *sym_list)
514 struct symbol *sym;
516 FOR_EACH_PTR(sym_list, sym) {
517 struct symbol *base_type;
518 base_type = get_base_type(sym);
519 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
520 cur_func_sym = sym;
521 if (base_type->stmt)
522 line_func_start = base_type->stmt->pos.line;
523 if (sym->ident)
524 cur_func = sym->ident->name;
525 __smatch_lineno = sym->pos.line;
526 sm_debug("new function: %s\n", cur_func);
527 if (option_two_passes) {
528 __unnullify_path();
529 loop_num = 0;
530 final_pass = 0;
531 __pass_to_client(sym, FUNC_DEF_HOOK);
532 __split_statements(base_type->stmt);
533 nullify_path();
535 __unnullify_path();
536 loop_num = 0;
537 final_pass = 1;
538 __pass_to_client(sym, FUNC_DEF_HOOK);
539 __split_statements(base_type->stmt);
540 __pass_to_client(sym, END_FUNC_HOOK);
541 cur_func = NULL;
542 line_func_start = 0;
543 clear_all_states();
544 free_data_info_allocs();
545 free_expression_stack(&switch_expr_stack);
546 } else {
547 __pass_to_client(sym, BASE_HOOK);
549 } END_FOR_EACH_PTR(sym);
550 __pass_to_client_no_data(END_FILE_HOOK);
553 void smatch (int argc, char **argv)
556 struct string_list *filelist = NULL;
557 struct symbol_list *sym_list;
559 if (argc < 2) {
560 printf("Usage: smatch [--debug] <filename.c>\n");
561 exit(1);
563 sparse_initialize(argc, argv, &filelist);
564 FOR_EACH_PTR_NOTAG(filelist, filename) {
565 int len;
567 pathname = get_current_dir_name();
568 if (pathname) {
569 len = strlen(pathname) + 1 + strlen(filename) + 1;
570 full_filename = malloc(len);
571 snprintf(full_filename, len, "%s/%s", pathname, filename);
572 } else {
573 full_filename = filename;
576 sym_list = __sparse(filename);
577 split_functions(sym_list);
579 if (pathname) {
580 free(full_filename);
581 free(pathname);
583 } END_FOR_EACH_PTR_NOTAG(filename);