assumed_nonnull crap... temp fix.
[smatch.git] / smatch_flow.c
blobc0e61265ce894bf15a2efc91bc8c1ac675f6c3be
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 static int __smatch_lineno = 0;
16 static char *filename;
17 static char *cur_func;
18 static int line_func_start;
20 char *get_filename(void) { return filename; }
21 char *get_function(void) { return cur_func; }
22 int get_lineno(void) { return __smatch_lineno; }
23 int get_func_pos(void) { 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;
31 int option_known_conditions = 0;
33 void __split_expr(struct expression *expr)
35 if (!expr)
36 return;
38 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
40 __smatch_lineno = expr->pos.line;
41 __pass_to_client(expr, EXPR_HOOK);
43 switch (expr->type) {
44 case EXPR_PREOP:
45 case EXPR_POSTOP:
46 __pass_to_client(expr, OP_HOOK);
47 __split_expr(expr->unop);
48 return;
49 case EXPR_STATEMENT:
50 __split_statements(expr->statement);
51 return;
52 case EXPR_LOGICAL:
53 __split_whole_condition(expr);
54 __push_true_states();
55 __use_false_states();
56 __merge_true_states();
57 __pop_false_only_stack();
58 return;
60 return;
61 case EXPR_BINOP:
62 case EXPR_COMMA:
63 case EXPR_COMPARE:
64 __split_expr(expr->left);
65 __split_expr(expr->right);
66 return;
67 case EXPR_ASSIGNMENT:
68 __split_expr(expr->right);
69 __pass_to_client(expr, ASSIGNMENT_HOOK);
70 __split_expr(expr->left);
71 return;
72 case EXPR_DEREF:
73 __pass_to_client(expr, DEREF_HOOK);
74 __split_expr(expr->deref);
75 return;
76 case EXPR_SLICE:
77 __split_expr(expr->base);
78 return;
79 case EXPR_CAST:
80 __split_expr(expr->cast_expression);
81 return;
82 case EXPR_SIZEOF:
83 /* there isn't anything to pass a client from inside a sizeof() */
84 return;
85 case EXPR_CONDITIONAL:
86 case EXPR_SELECT:
87 __split_whole_condition(expr->conditional);
88 __split_expr(expr->cond_true);
89 __push_true_states();
90 __use_false_states();
91 __split_expr(expr->cond_false);
92 __merge_true_states();
93 __pop_false_only_stack();
94 return;
95 case EXPR_CALL:
96 split_expr_list(expr->args);
97 __split_expr(expr->fn);
98 __pass_to_client(expr, FUNCTION_CALL_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 (option_known_conditions &&
270 known_condition_true(stmt->if_conditional)) {
271 smatch_msg("info: this condition is true.");
272 __split_statements(stmt->if_true);
273 return;
275 if (option_known_conditions &&
276 known_condition_false(stmt->if_conditional)) {
277 smatch_msg("info: this condition is false.");
278 __split_statements(stmt->if_false);
279 return;
281 __split_whole_condition(stmt->if_conditional);
282 __split_statements(stmt->if_true);
283 __push_true_states();
284 __use_false_states();
285 __split_statements(stmt->if_false);
286 __merge_true_states();
287 __pop_false_only_stack();
288 return;
289 case STMT_ITERATOR:
290 if (stmt->iterator_pre_condition)
291 handle_pre_loop(stmt);
292 else if (stmt->iterator_post_condition)
293 handle_post_loop(stmt);
294 else {
295 // these are for(;;) type loops.
296 handle_pre_loop(stmt);
298 return;
299 case STMT_SWITCH:
300 __split_expr(stmt->switch_expression);
301 __save_switch_states();
302 __push_default();
303 __push_breaks();
304 __split_statements(stmt->switch_statement);
305 if (!__pop_default())
306 __merge_switches();
307 __pop_switches();
308 __merge_breaks();
309 return;
310 case STMT_CASE:
311 __merge_switches();
312 if (!stmt->case_expression)
313 __set_default();
314 __split_expr(stmt->case_expression);
315 __split_expr(stmt->case_to);
316 __split_statements(stmt->case_statement);
317 return;
318 case STMT_LABEL:
319 if (stmt->label &&
320 stmt->label->type == SYM_LABEL &&
321 stmt->label->ident) {
322 __merge_gotos(stmt->label->ident->name);
324 __split_statements(stmt->label_statement);
325 return;
326 case STMT_GOTO:
327 __split_expr(stmt->goto_expression);
328 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
329 if (!strcmp(stmt->goto_label->ident->name, "break")) {
330 __process_breaks();
331 } else if (!strcmp(stmt->goto_label->ident->name,
332 "continue")) {
333 __process_continues();
335 } else if (stmt->goto_label &&
336 stmt->goto_label->type == SYM_LABEL &&
337 stmt->goto_label->ident) {
338 __save_gotos(stmt->goto_label->ident->name);
340 nullify_path();
341 return;
342 case STMT_NONE:
343 return;
344 case STMT_ASM:
345 __split_expr(stmt->asm_string);
346 //__split_expr(stmt->asm_outputs);
347 //__split_expr(stmt->asm_inputs);
348 //__split_expr(stmt->asm_clobbers);
349 return;
350 case STMT_CONTEXT:
351 return;
352 case STMT_RANGE:
353 __split_expr(stmt->range_expression);
354 __split_expr(stmt->range_low);
355 __split_expr(stmt->range_high);
356 return;
360 static void split_expr_list(struct expression_list *expr_list)
362 struct expression *expr;
363 FOR_EACH_PTR(expr_list, expr) {
364 __split_expr(expr);
365 } END_FOR_EACH_PTR(expr);
369 static void split_sym(struct symbol *sym)
371 if (!sym)
372 return;
373 if (!(sym->namespace & NS_SYMBOL))
374 return;
376 __pass_to_client(sym, SYM_HOOK);
377 __split_statements(sym->stmt);
378 __split_expr(sym->array_size);
379 split_symlist(sym->arguments);
380 split_symlist(sym->symbol_list);
381 __split_statements(sym->inline_stmt);
382 split_symlist(sym->inline_symbol_list);
383 __split_expr(sym->initializer);
386 static void split_symlist(struct symbol_list *sym_list)
388 struct symbol *sym;
390 FOR_EACH_PTR(sym_list, sym) {
391 split_sym(sym);
392 } END_FOR_EACH_PTR(sym);
395 static void split_functions(struct symbol_list *sym_list)
397 struct symbol *sym;
399 FOR_EACH_PTR(sym_list, sym) {
400 struct symbol *base_type;
401 base_type = get_base_type(sym);
402 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
403 if (base_type->stmt)
404 line_func_start = base_type->stmt->pos.line;
405 if (sym->ident)
406 cur_func = sym->ident->name;
407 __smatch_lineno = sym->pos.line;
408 SM_DEBUG("new function: %s\n", cur_func);
409 __unnullify_path();
410 __pass_to_client(sym, FUNC_DEF_HOOK);
411 __split_statements(base_type->stmt);
412 __pass_to_client(sym, END_FUNC_HOOK);
413 cur_func = NULL;
414 line_func_start = 0;
415 clear_all_states();
417 } else {
418 __pass_to_client(sym, BASE_HOOK);
420 } END_FOR_EACH_PTR(sym);
421 __pass_to_client_no_data(END_FILE_HOOK);
424 void smatch (int argc, char **argv)
427 struct string_list *filelist = NULL;
428 struct symbol_list *sym_list;
430 if (argc < 2) {
431 printf("Usage: smatch [--debug] <filename.c>\n");
432 exit(1);
434 sparse_initialize(argc, argv, &filelist);
435 FOR_EACH_PTR_NOTAG(filelist, filename) {
436 sym_list = __sparse(filename);
437 split_functions(sym_list);
438 } END_FOR_EACH_PTR_NOTAG(filename);