handle known conditions better
[smatch.git] / smatch_flow.c
blob8072c246a8f560678d77797c5cb79ee2783b64a9
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 #include <stdio.h>
11 #include "token.h"
12 #include "smatch.h"
14 int __smatch_lineno = 0;
16 static char *filename;
17 static char *cur_func;
18 static int line_func_start;
20 char *get_filename() { return filename; }
21 char *get_function() { return cur_func; }
22 int get_lineno() { return __smatch_lineno; }
23 int get_func_pos() { return __smatch_lineno - line_func_start; }
25 static void split_symlist(struct symbol_list *sym_list);
26 static void split_expr_list(struct expression_list *expr_list);
28 unsigned int __get_allocations();
30 int option_assume_loops = 0;
32 void __split_expr(struct expression *expr)
34 if (!expr)
35 return;
37 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
39 __smatch_lineno = expr->pos.line;
40 __pass_to_client(expr, EXPR_HOOK);
42 switch (expr->type) {
43 case EXPR_PREOP:
44 case EXPR_POSTOP:
45 __pass_to_client(expr, OP_HOOK);
46 __split_expr(expr->unop);
47 return;
48 case EXPR_STATEMENT:
49 __split_statements(expr->statement);
50 return;
51 case EXPR_LOGICAL:
52 __split_whole_condition(expr);
53 __push_true_states();
54 __use_false_states();
55 __merge_true_states();
56 __pop_false_only_stack();
57 return;
59 return;
60 case EXPR_BINOP:
61 case EXPR_COMMA:
62 case EXPR_COMPARE:
63 case EXPR_ASSIGNMENT:
64 if (expr->type == EXPR_ASSIGNMENT)
65 __pass_to_client(expr, ASSIGNMENT_HOOK);
66 __split_expr(expr->left);
67 __split_expr(expr->right);
68 if (expr->type == EXPR_ASSIGNMENT)
69 __pass_to_client(expr, ASSIGNMENT_AFTER_HOOK);
70 return;
71 case EXPR_DEREF:
72 __pass_to_client(expr, DEREF_HOOK);
73 __split_expr(expr->deref);
74 return;
75 case EXPR_SLICE:
76 __split_expr(expr->base);
77 return;
78 case EXPR_CAST:
79 __split_expr(expr->cast_expression);
80 return;
81 case EXPR_SIZEOF:
82 /* there isn't anything to pass a client from inside a sizeof() */
83 return;
84 case EXPR_CONDITIONAL:
85 case EXPR_SELECT:
86 __split_whole_condition(expr->conditional);
87 __split_expr(expr->cond_true);
88 __push_true_states();
89 __use_false_states();
90 __split_expr(expr->cond_false);
91 __merge_true_states();
92 __pop_false_only_stack();
93 return;
94 case EXPR_CALL:
95 __pass_to_client(expr, FUNCTION_CALL_HOOK);
96 __split_expr(expr->fn);
97 split_expr_list(expr->args);
98 __pass_to_client(expr, FUNCTION_CALL_AFTER_HOOK);
99 #ifdef KERNEL
100 if (expr->fn->type == EXPR_SYMBOL &&
101 !strcmp(expr->fn->symbol_name->name, "panic"))
102 nullify_path();
103 #endif
104 return;
105 case EXPR_INITIALIZER:
106 split_expr_list(expr->expr_list);
107 return;
108 case EXPR_IDENTIFIER:
109 __split_expr(expr->ident_expression);
110 return;
111 case EXPR_INDEX:
112 __split_expr(expr->idx_expression);
113 return;
114 case EXPR_POS:
115 __split_expr(expr->init_expr);
116 return;
117 default:
118 return;
122 static int is_forever_loop(struct statement *stmt)
125 struct expression *expr;
127 expr = strip_expr(stmt->iterator_pre_condition);
128 if (!expr)
129 expr = stmt->iterator_post_condition;
130 if (!expr) {
131 /* this is a for(;;) loop... */
132 return 1;
135 if (expr->type == EXPR_VALUE && expr->value == 1) {
136 return 1;
139 return 0;
143 * Pre Loops are while and for loops.
146 static void handle_pre_loop(struct statement *stmt)
148 int once_through; /* we go through the loop at least once */
150 __split_statements(stmt->iterator_pre_statement);
152 once_through = known_condition_true(stmt->iterator_pre_condition);
153 if (option_assume_loops)
154 once_through = 1;
156 __push_continues();
157 __push_breaks();
159 __split_whole_condition(stmt->iterator_pre_condition);
161 __split_statements(stmt->iterator_statement);
162 __warn_on_silly_pre_loops();
163 if (is_forever_loop(stmt)) {
164 __pop_false_only_stack();
165 /* forever loops don't have an iterator_post_statement */
166 __pop_continues();
167 __pop_false_states();
168 __use_breaks();
169 } else if (once_through) {
170 __merge_continues();
171 __split_statements(stmt->iterator_post_statement);
172 __pop_false_states();
173 __use_false_only_stack();
174 __merge_breaks();
175 } else {
176 __merge_continues();
177 __split_statements(stmt->iterator_post_statement);
178 __merge_false_states();
179 __use_false_only_stack();
180 __merge_breaks();
185 * Post loops are do {} while();
187 static void handle_post_loop(struct statement *stmt)
189 __push_continues();
190 __push_breaks();
191 __split_statements(stmt->iterator_statement);
192 if (is_forever_loop(stmt)) {
193 __pop_continues();
194 __use_breaks();
196 } else {
197 __merge_continues();
198 __split_whole_condition(stmt->iterator_post_condition);
199 __use_false_states();
200 __merge_breaks();
202 __pop_false_only_stack();
205 static void print_unreached(struct statement *stmt)
209 * GCC insists on a return statement even where it is never
210 * reached. Also BUG() sometimes is a forever loop and
211 * sometimes not so people put code after a BUG(). There
212 * are way to many false positives.
214 #ifdef KERNEL
215 return;
216 #endif
217 if (__path_is_null()) {
218 switch(stmt->type) {
219 case STMT_COMPOUND: /* after a switch before a case stmt */
220 case STMT_CASE:
221 case STMT_LABEL:
222 case STMT_DECLARATION: /* switch(x) { int a; case foo: ... */
223 break;
224 default:
225 smatch_msg("unreachable code. %d", stmt->type);
230 void __split_statements(struct statement *stmt)
232 if (!stmt)
233 return;
235 if (__get_allocations() > MAXSMSTATES) {
236 static char *printed = NULL;
238 if (printed != cur_func)
239 smatch_msg("Function too big. Giving up.");
240 printed = cur_func;
241 return;
244 __smatch_lineno = stmt->pos.line;
245 print_unreached(stmt);
246 __pass_to_client(stmt, STMT_HOOK);
248 switch (stmt->type) {
249 case STMT_DECLARATION:
250 __pass_declarations_to_client(stmt->declaration);
251 split_symlist(stmt->declaration);
252 return;
253 case STMT_RETURN:
254 __split_expr(stmt->ret_value);
255 __pass_to_client(stmt, RETURN_HOOK);
256 nullify_path();
257 return;
258 case STMT_EXPRESSION:
259 __split_expr(stmt->expression);
260 return;
261 case STMT_COMPOUND: {
262 struct statement *s;
263 FOR_EACH_PTR(stmt->stmts, s) {
264 __split_statements(s);
265 } END_FOR_EACH_PTR(s);
266 return;
268 case STMT_IF:
269 if (known_condition_true(stmt->if_conditional)) {
270 smatch_msg("info: this condition is true.");
271 __split_statements(stmt->if_true);
272 return;
274 if (known_condition_false(stmt->if_conditional)) {
275 smatch_msg("info: this condition is false.");
276 __split_statements(stmt->if_false);
277 return;
279 __split_whole_condition(stmt->if_conditional);
280 __split_statements(stmt->if_true);
281 __push_true_states();
282 __use_false_states();
283 __split_statements(stmt->if_false);
284 __merge_true_states();
285 __pop_false_only_stack();
286 return;
287 case STMT_ITERATOR:
288 if (stmt->iterator_pre_condition)
289 handle_pre_loop(stmt);
290 else if (stmt->iterator_post_condition)
291 handle_post_loop(stmt);
292 else {
293 // these are for(;;) type loops.
294 handle_pre_loop(stmt);
296 return;
297 case STMT_SWITCH:
298 __split_expr(stmt->switch_expression);
299 __save_switch_states();
300 __push_default();
301 __push_breaks();
302 __split_statements(stmt->switch_statement);
303 if (!__pop_default())
304 __merge_switches();
305 __pop_switches();
306 __merge_breaks();
307 return;
308 case STMT_CASE:
309 __merge_switches();
310 if (!stmt->case_expression)
311 __set_default();
312 __split_expr(stmt->case_expression);
313 __split_expr(stmt->case_to);
314 __split_statements(stmt->case_statement);
315 return;
316 case STMT_LABEL:
317 if (stmt->label &&
318 stmt->label->type == SYM_LABEL &&
319 stmt->label->ident) {
320 __merge_gotos(stmt->label->ident->name);
322 __split_statements(stmt->label_statement);
323 return;
324 case STMT_GOTO:
325 __split_expr(stmt->goto_expression);
326 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
327 if (!strcmp(stmt->goto_label->ident->name, "break")) {
328 __process_breaks();
329 } else if (!strcmp(stmt->goto_label->ident->name,
330 "continue")) {
331 __process_continues();
333 } else if (stmt->goto_label &&
334 stmt->goto_label->type == SYM_LABEL &&
335 stmt->goto_label->ident) {
336 __save_gotos(stmt->goto_label->ident->name);
338 nullify_path();
339 return;
340 case STMT_NONE:
341 return;
342 case STMT_ASM:
343 __split_expr(stmt->asm_string);
344 //__split_expr(stmt->asm_outputs);
345 //__split_expr(stmt->asm_inputs);
346 //__split_expr(stmt->asm_clobbers);
347 return;
348 case STMT_CONTEXT:
349 return;
350 case STMT_RANGE:
351 __split_expr(stmt->range_expression);
352 __split_expr(stmt->range_low);
353 __split_expr(stmt->range_high);
354 return;
358 static void split_expr_list(struct expression_list *expr_list)
360 struct expression *expr;
361 FOR_EACH_PTR(expr_list, expr) {
362 __split_expr(expr);
363 } END_FOR_EACH_PTR(expr);
367 static void split_sym(struct symbol *sym)
369 if (!sym)
370 return;
371 if (!(sym->namespace & NS_SYMBOL))
372 return;
374 __pass_to_client(sym, SYM_HOOK);
375 __split_statements(sym->stmt);
376 __split_expr(sym->array_size);
377 split_symlist(sym->arguments);
378 split_symlist(sym->symbol_list);
379 __split_statements(sym->inline_stmt);
380 split_symlist(sym->inline_symbol_list);
381 __split_expr(sym->initializer);
384 static void split_symlist(struct symbol_list *sym_list)
386 struct symbol *sym;
388 FOR_EACH_PTR(sym_list, sym) {
389 split_sym(sym);
390 } END_FOR_EACH_PTR(sym);
393 static void split_functions(struct symbol_list *sym_list)
395 struct symbol *sym;
397 FOR_EACH_PTR(sym_list, sym) {
398 struct symbol *base_type;
399 base_type = get_base_type(sym);
400 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
401 if (base_type->stmt)
402 line_func_start = base_type->stmt->pos.line;
403 if (sym->ident)
404 cur_func = sym->ident->name;
405 __smatch_lineno = sym->pos.line;
406 SM_DEBUG("new function: %s\n", cur_func);
407 __unnullify_path();
408 __pass_to_client(sym, FUNC_DEF_HOOK);
409 __split_statements(base_type->stmt);
410 __pass_to_client(sym, END_FUNC_HOOK);
411 cur_func = NULL;
412 line_func_start = 0;
413 clear_all_states();
415 } else {
416 __pass_to_client(sym, BASE_HOOK);
418 } END_FOR_EACH_PTR(sym);
419 __pass_to_client_no_data(END_FILE_HOOK);
422 void smatch (int argc, char **argv)
425 struct string_list *filelist = NULL;
426 struct symbol_list *sym_list;
428 if (argc < 2) {
429 printf("Usage: smatch [--debug] <filename.c>\n");
430 exit(1);
432 sparse_initialize(argc, argv, &filelist);
433 FOR_EACH_PTR_NOTAG(filelist, filename) {
434 sym_list = __sparse(filename);
435 split_functions(sym_list);
436 } END_FOR_EACH_PTR_NOTAG(filename);