Add a list of noreturn type functions that nullify the path.
[smatch.git] / smatch_flow.c
blob7b83967c0f0712296e7e70ddbddc99e992224e68
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 return;
100 case EXPR_INITIALIZER:
101 split_expr_list(expr->expr_list);
102 return;
103 case EXPR_IDENTIFIER:
104 __split_expr(expr->ident_expression);
105 return;
106 case EXPR_INDEX:
107 __split_expr(expr->idx_expression);
108 return;
109 case EXPR_POS:
110 __split_expr(expr->init_expr);
111 return;
112 default:
113 return;
117 static int is_forever_loop(struct statement *stmt)
120 struct expression *expr;
122 expr = strip_expr(stmt->iterator_pre_condition);
123 if (!expr)
124 expr = stmt->iterator_post_condition;
125 if (!expr) {
126 /* this is a for(;;) loop... */
127 return 1;
130 if (expr->type == EXPR_VALUE && expr->value == 1) {
131 return 1;
134 return 0;
138 * Pre Loops are while and for loops.
141 static void handle_pre_loop(struct statement *stmt)
143 int once_through; /* we go through the loop at least once */
145 __split_statements(stmt->iterator_pre_statement);
147 once_through = known_condition_true(stmt->iterator_pre_condition);
148 if (option_assume_loops)
149 once_through = 1;
151 __push_continues();
152 __push_breaks();
154 __split_whole_condition(stmt->iterator_pre_condition);
156 __split_statements(stmt->iterator_statement);
157 __warn_on_silly_pre_loops();
158 if (is_forever_loop(stmt)) {
159 __pop_false_only_stack();
160 /* forever loops don't have an iterator_post_statement */
161 __pop_continues();
162 __pop_false_states();
163 __use_breaks();
164 } else if (once_through) {
165 __merge_continues();
166 __split_statements(stmt->iterator_post_statement);
167 __pop_false_states();
168 __use_false_only_stack();
169 __merge_breaks();
170 } else {
171 __merge_continues();
172 __split_statements(stmt->iterator_post_statement);
173 __merge_false_states();
174 __use_false_only_stack();
175 __merge_breaks();
180 * Post loops are do {} while();
182 static void handle_post_loop(struct statement *stmt)
184 __push_continues();
185 __push_breaks();
186 __split_statements(stmt->iterator_statement);
187 if (is_forever_loop(stmt)) {
188 __pop_continues();
189 __use_breaks();
191 } else {
192 __merge_continues();
193 __split_whole_condition(stmt->iterator_post_condition);
194 __use_false_states();
195 __merge_breaks();
197 __pop_false_only_stack();
200 static void print_unreached(struct statement *stmt)
204 * GCC insists on a return statement even where it is never
205 * reached. Also BUG() sometimes is a forever loop and
206 * sometimes not so people put code after a BUG(). There
207 * are way to many false positives.
209 #ifdef KERNEL
210 return;
211 #endif
212 if (__path_is_null()) {
213 switch(stmt->type) {
214 case STMT_COMPOUND: /* after a switch before a case stmt */
215 case STMT_CASE:
216 case STMT_LABEL:
217 case STMT_DECLARATION: /* switch(x) { int a; case foo: ... */
218 break;
219 default:
220 smatch_msg("unreachable code. %d", stmt->type);
225 void __split_statements(struct statement *stmt)
227 if (!stmt)
228 return;
230 if (__get_allocations() > MAXSMSTATES) {
231 static char *printed = NULL;
233 if (printed != cur_func)
234 smatch_msg("Function too big. Giving up.");
235 printed = cur_func;
236 return;
239 __smatch_lineno = stmt->pos.line;
240 print_unreached(stmt);
241 __pass_to_client(stmt, STMT_HOOK);
243 switch (stmt->type) {
244 case STMT_DECLARATION:
245 __pass_declarations_to_client(stmt->declaration);
246 split_symlist(stmt->declaration);
247 return;
248 case STMT_RETURN:
249 __split_expr(stmt->ret_value);
250 __pass_to_client(stmt, RETURN_HOOK);
251 nullify_path();
252 return;
253 case STMT_EXPRESSION:
254 __split_expr(stmt->expression);
255 return;
256 case STMT_COMPOUND: {
257 struct statement *s;
258 FOR_EACH_PTR(stmt->stmts, s) {
259 __split_statements(s);
260 } END_FOR_EACH_PTR(s);
261 return;
263 case STMT_IF:
264 if (option_known_conditions &&
265 known_condition_true(stmt->if_conditional)) {
266 smatch_msg("info: this condition is true.");
267 __split_statements(stmt->if_true);
268 return;
270 if (option_known_conditions &&
271 known_condition_false(stmt->if_conditional)) {
272 smatch_msg("info: this condition is false.");
273 __split_statements(stmt->if_false);
274 return;
276 __split_whole_condition(stmt->if_conditional);
277 __split_statements(stmt->if_true);
278 __push_true_states();
279 __use_false_states();
280 __split_statements(stmt->if_false);
281 __merge_true_states();
282 __pop_false_only_stack();
283 return;
284 case STMT_ITERATOR:
285 if (stmt->iterator_pre_condition)
286 handle_pre_loop(stmt);
287 else if (stmt->iterator_post_condition)
288 handle_post_loop(stmt);
289 else {
290 // these are for(;;) type loops.
291 handle_pre_loop(stmt);
293 return;
294 case STMT_SWITCH:
295 __split_expr(stmt->switch_expression);
296 __save_switch_states();
297 __push_default();
298 __push_breaks();
299 __split_statements(stmt->switch_statement);
300 if (!__pop_default())
301 __merge_switches();
302 __pop_switches();
303 __merge_breaks();
304 return;
305 case STMT_CASE:
306 __merge_switches();
307 if (!stmt->case_expression)
308 __set_default();
309 __split_expr(stmt->case_expression);
310 __split_expr(stmt->case_to);
311 __split_statements(stmt->case_statement);
312 return;
313 case STMT_LABEL:
314 if (stmt->label &&
315 stmt->label->type == SYM_LABEL &&
316 stmt->label->ident) {
317 __merge_gotos(stmt->label->ident->name);
319 __split_statements(stmt->label_statement);
320 return;
321 case STMT_GOTO:
322 __split_expr(stmt->goto_expression);
323 if (stmt->goto_label && stmt->goto_label->type == SYM_NODE) {
324 if (!strcmp(stmt->goto_label->ident->name, "break")) {
325 __process_breaks();
326 } else if (!strcmp(stmt->goto_label->ident->name,
327 "continue")) {
328 __process_continues();
330 } else if (stmt->goto_label &&
331 stmt->goto_label->type == SYM_LABEL &&
332 stmt->goto_label->ident) {
333 __save_gotos(stmt->goto_label->ident->name);
335 nullify_path();
336 return;
337 case STMT_NONE:
338 return;
339 case STMT_ASM:
340 __split_expr(stmt->asm_string);
341 //__split_expr(stmt->asm_outputs);
342 //__split_expr(stmt->asm_inputs);
343 //__split_expr(stmt->asm_clobbers);
344 return;
345 case STMT_CONTEXT:
346 return;
347 case STMT_RANGE:
348 __split_expr(stmt->range_expression);
349 __split_expr(stmt->range_low);
350 __split_expr(stmt->range_high);
351 return;
355 static void split_expr_list(struct expression_list *expr_list)
357 struct expression *expr;
358 FOR_EACH_PTR(expr_list, expr) {
359 __split_expr(expr);
360 } END_FOR_EACH_PTR(expr);
364 static void split_sym(struct symbol *sym)
366 if (!sym)
367 return;
368 if (!(sym->namespace & NS_SYMBOL))
369 return;
371 __pass_to_client(sym, SYM_HOOK);
372 __split_statements(sym->stmt);
373 __split_expr(sym->array_size);
374 split_symlist(sym->arguments);
375 split_symlist(sym->symbol_list);
376 __split_statements(sym->inline_stmt);
377 split_symlist(sym->inline_symbol_list);
378 __split_expr(sym->initializer);
381 static void split_symlist(struct symbol_list *sym_list)
383 struct symbol *sym;
385 FOR_EACH_PTR(sym_list, sym) {
386 split_sym(sym);
387 } END_FOR_EACH_PTR(sym);
390 static void split_functions(struct symbol_list *sym_list)
392 struct symbol *sym;
394 FOR_EACH_PTR(sym_list, sym) {
395 struct symbol *base_type;
396 base_type = get_base_type(sym);
397 if (sym->type == SYM_NODE && base_type->type == SYM_FN) {
398 if (base_type->stmt)
399 line_func_start = base_type->stmt->pos.line;
400 if (sym->ident)
401 cur_func = sym->ident->name;
402 __smatch_lineno = sym->pos.line;
403 SM_DEBUG("new function: %s\n", cur_func);
404 __unnullify_path();
405 __pass_to_client(sym, FUNC_DEF_HOOK);
406 __split_statements(base_type->stmt);
407 __pass_to_client(sym, END_FUNC_HOOK);
408 cur_func = NULL;
409 line_func_start = 0;
410 clear_all_states();
412 } else {
413 __pass_to_client(sym, BASE_HOOK);
415 } END_FOR_EACH_PTR(sym);
416 __pass_to_client_no_data(END_FILE_HOOK);
419 void smatch (int argc, char **argv)
422 struct string_list *filelist = NULL;
423 struct symbol_list *sym_list;
425 if (argc < 2) {
426 printf("Usage: smatch [--debug] <filename.c>\n");
427 exit(1);
429 sparse_initialize(argc, argv, &filelist);
430 FOR_EACH_PTR_NOTAG(filelist, filename) {
431 sym_list = __sparse(filename);
432 split_functions(sym_list);
433 } END_FOR_EACH_PTR_NOTAG(filename);