4 * Copyright (C) 2006,2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 #include "smatch_expression_stacks.h"
15 static int __smatch_lineno
= 0;
17 static char *filename
;
18 static char *cur_func
;
19 static int line_func_start
;
20 static struct expression_stack
*switch_expr_stack
= NULL
;
22 char *get_filename(void) { return filename
; }
23 char *get_function(void) { return cur_func
; }
24 int get_lineno(void) { return __smatch_lineno
; }
25 int get_func_pos(void) { return __smatch_lineno
- line_func_start
; }
27 static void split_symlist(struct symbol_list
*sym_list
);
28 static void split_declaration(struct symbol_list
*sym_list
);
29 static void split_expr_list(struct expression_list
*expr_list
);
31 unsigned int __get_allocations();
33 int option_assume_loops
= 0;
34 int option_known_conditions
= 0;
36 void __split_expr(struct expression
*expr
)
41 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
43 __smatch_lineno
= expr
->pos
.line
;
44 __pass_to_client(expr
, EXPR_HOOK
);
49 __pass_to_client(expr
, OP_HOOK
);
50 __split_expr(expr
->unop
);
53 __split_statements(expr
->statement
);
56 __split_whole_condition(expr
);
59 __merge_true_states();
60 __pop_false_only_stack();
67 __split_expr(expr
->left
);
68 __split_expr(expr
->right
);
70 case EXPR_ASSIGNMENT
: {
71 struct expression
*tmp
;
73 __split_expr(expr
->right
);
74 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
75 tmp
= strip_expr(expr
->right
);
76 if (tmp
->type
== EXPR_CALL
)
77 __pass_to_client(expr
, CALL_ASSIGNMENT_HOOK
);
78 __split_expr(expr
->left
);
82 __pass_to_client(expr
, DEREF_HOOK
);
83 __split_expr(expr
->deref
);
86 __split_expr(expr
->base
);
89 __split_expr(expr
->cast_expression
);
92 /* there isn't anything to pass a client from inside a sizeof() */
94 case EXPR_CONDITIONAL
:
96 __split_whole_condition(expr
->conditional
);
97 __split_expr(expr
->cond_true
);
100 __split_expr(expr
->cond_false
);
101 __merge_true_states();
102 __pop_false_only_stack();
105 split_expr_list(expr
->args
);
106 __split_expr(expr
->fn
);
107 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
109 case EXPR_INITIALIZER
:
110 split_expr_list(expr
->expr_list
);
112 case EXPR_IDENTIFIER
:
113 __split_expr(expr
->ident_expression
);
116 __split_expr(expr
->idx_expression
);
119 __split_expr(expr
->init_expr
);
126 static int is_forever_loop(struct statement
*stmt
)
129 struct expression
*expr
;
131 expr
= strip_expr(stmt
->iterator_pre_condition
);
133 expr
= stmt
->iterator_post_condition
;
135 /* this is a for(;;) loop... */
139 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
147 * Pre Loops are while and for loops.
150 static void handle_pre_loop(struct statement
*stmt
)
152 int once_through
; /* we go through the loop at least once */
154 __split_statements(stmt
->iterator_pre_statement
);
156 once_through
= known_condition_true(stmt
->iterator_pre_condition
);
157 if (option_assume_loops
)
163 __split_whole_condition(stmt
->iterator_pre_condition
);
165 __split_statements(stmt
->iterator_statement
);
166 __warn_on_silly_pre_loops();
167 if (is_forever_loop(stmt
)) {
168 __pop_false_only_stack();
169 /* forever loops don't have an iterator_post_statement */
171 __pop_false_states();
173 } else if (once_through
) {
175 __split_statements(stmt
->iterator_post_statement
);
176 __pop_false_states();
177 __use_false_only_stack();
181 __split_statements(stmt
->iterator_post_statement
);
182 __merge_false_states();
183 __use_false_only_stack();
189 * Post loops are do {} while();
191 static void handle_post_loop(struct statement
*stmt
)
195 __split_statements(stmt
->iterator_statement
);
196 if (is_forever_loop(stmt
)) {
202 __split_whole_condition(stmt
->iterator_post_condition
);
203 __use_false_states();
206 __pop_false_only_stack();
209 static void print_unreached(struct statement
*stmt
)
213 * GCC insists on a return statement even where it is never
214 * reached. Also BUG() sometimes is a forever loop and
215 * sometimes not so people put code after a BUG(). There
216 * are way to many false positives.
221 if (__path_is_null()) {
223 case STMT_COMPOUND
: /* after a switch before a case stmt */
226 case STMT_DECLARATION
: /* switch(x) { int a; case foo: ... */
229 smatch_msg("unreachable code. %d", stmt
->type
);
234 void __split_statements(struct statement
*stmt
)
239 if (__get_allocations() > MAXSMSTATES
) {
240 static char *printed
= NULL
;
242 if (printed
!= cur_func
)
243 smatch_msg("Function too big. Giving up.");
248 __smatch_lineno
= stmt
->pos
.line
;
249 print_unreached(stmt
);
250 __pass_to_client(stmt
, STMT_HOOK
);
252 switch (stmt
->type
) {
253 case STMT_DECLARATION
:
254 split_declaration(stmt
->declaration
);
257 __split_expr(stmt
->ret_value
);
258 __pass_to_client(stmt
, RETURN_HOOK
);
261 case STMT_EXPRESSION
:
262 __split_expr(stmt
->expression
);
264 case STMT_COMPOUND
: {
266 FOR_EACH_PTR(stmt
->stmts
, s
) {
267 __split_statements(s
);
268 } END_FOR_EACH_PTR(s
);
272 if (option_known_conditions
&&
273 known_condition_true(stmt
->if_conditional
)) {
274 smatch_msg("info: this condition is true.");
275 __split_statements(stmt
->if_true
);
278 if (option_known_conditions
&&
279 known_condition_false(stmt
->if_conditional
)) {
280 smatch_msg("info: this condition is false.");
281 __split_statements(stmt
->if_false
);
284 __split_whole_condition(stmt
->if_conditional
);
285 __split_statements(stmt
->if_true
);
286 __push_true_states();
287 __use_false_states();
288 __split_statements(stmt
->if_false
);
289 __merge_true_states();
290 __pop_false_only_stack();
293 if (stmt
->iterator_pre_condition
)
294 handle_pre_loop(stmt
);
295 else if (stmt
->iterator_post_condition
)
296 handle_post_loop(stmt
);
298 // these are for(;;) type loops.
299 handle_pre_loop(stmt
);
303 __split_expr(stmt
->switch_expression
);
304 push_expression(&switch_expr_stack
, stmt
->switch_expression
);
305 __save_switch_states();
308 __split_statements(stmt
->switch_statement
);
309 if (!__pop_default())
313 pop_expression(&switch_expr_stack
);
317 __pass_case_to_client(top_expression(switch_expr_stack
),
318 stmt
->case_expression
);
319 if (!stmt
->case_expression
)
321 __split_expr(stmt
->case_expression
);
322 __split_expr(stmt
->case_to
);
323 __split_statements(stmt
->case_statement
);
327 stmt
->label
->type
== SYM_LABEL
&&
328 stmt
->label
->ident
) {
329 __merge_gotos(stmt
->label
->ident
->name
);
331 __split_statements(stmt
->label_statement
);
334 __split_expr(stmt
->goto_expression
);
335 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
336 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
338 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
340 __process_continues();
342 } else if (stmt
->goto_label
&&
343 stmt
->goto_label
->type
== SYM_LABEL
&&
344 stmt
->goto_label
->ident
) {
345 __save_gotos(stmt
->goto_label
->ident
->name
);
352 __split_expr(stmt
->asm_string
);
353 //__split_expr(stmt->asm_outputs);
354 //__split_expr(stmt->asm_inputs);
355 //__split_expr(stmt->asm_clobbers);
360 __split_expr(stmt
->range_expression
);
361 __split_expr(stmt
->range_low
);
362 __split_expr(stmt
->range_high
);
367 static void split_expr_list(struct expression_list
*expr_list
)
369 struct expression
*expr
;
370 FOR_EACH_PTR(expr_list
, expr
) {
372 } END_FOR_EACH_PTR(expr
);
376 static void split_sym(struct symbol
*sym
)
380 if (!(sym
->namespace & NS_SYMBOL
))
383 __pass_to_client(sym
, SYM_HOOK
);
384 __split_statements(sym
->stmt
);
385 __split_expr(sym
->array_size
);
386 split_symlist(sym
->arguments
);
387 split_symlist(sym
->symbol_list
);
388 __split_statements(sym
->inline_stmt
);
389 split_symlist(sym
->inline_symbol_list
);
390 __split_expr(sym
->initializer
);
393 static void split_symlist(struct symbol_list
*sym_list
)
397 FOR_EACH_PTR(sym_list
, sym
) {
399 } END_FOR_EACH_PTR(sym
);
402 static void split_declaration(struct symbol_list
*sym_list
)
406 FOR_EACH_PTR(sym_list
, sym
) {
407 __pass_to_client(sym
, DECLARATION_HOOK
);
408 __split_expr(sym
->initializer
);
409 if(sym
->initializer
&& sym
->initializer
->type
== EXPR_CALL
) {
410 __match_initializer_call(sym
);
413 } END_FOR_EACH_PTR(sym
);
416 static void split_functions(struct symbol_list
*sym_list
)
420 FOR_EACH_PTR(sym_list
, sym
) {
421 struct symbol
*base_type
;
422 base_type
= get_base_type(sym
);
423 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
425 line_func_start
= base_type
->stmt
->pos
.line
;
427 cur_func
= sym
->ident
->name
;
428 __smatch_lineno
= sym
->pos
.line
;
429 SM_DEBUG("new function: %s\n", cur_func
);
431 __pass_to_client(sym
, FUNC_DEF_HOOK
);
432 __split_statements(base_type
->stmt
);
433 __pass_to_client(sym
, END_FUNC_HOOK
);
437 free_expression_stack(&switch_expr_stack
);
439 __pass_to_client(sym
, BASE_HOOK
);
441 } END_FOR_EACH_PTR(sym
);
442 __pass_to_client_no_data(END_FILE_HOOK
);
445 void smatch (int argc
, char **argv
)
448 struct string_list
*filelist
= NULL
;
449 struct symbol_list
*sym_list
;
452 printf("Usage: smatch [--debug] <filename.c>\n");
455 sparse_initialize(argc
, argv
, &filelist
);
456 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
457 sym_list
= __sparse(filename
);
458 split_functions(sym_list
);
459 } END_FOR_EACH_PTR_NOTAG(filename
);