2 * sparse/check_string_ret.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
16 int __smatch_lineno
= 0;
21 static char *filename
;
22 static char *cur_func
;
23 static int line_func_start
;
24 static unsigned int path_id
= 0;
25 static unsigned int path_id_next
= 1;
27 char *get_filename() { return filename
; }
28 char *get_function() { return cur_func
; }
29 int get_lineno() { return __smatch_lineno
; }
30 int get_func_pos() { return __smatch_lineno
- line_func_start
; }
32 static void split_statements(struct statement
*stmt
);
33 static void split_symlist(struct symbol_list
*sym_list
);
34 static void split_expr_list(struct expression_list
*expr_list
);
35 static void split_expr(struct expression
*expr
);
37 unsigned int get_path_id()
42 static unsigned int split_path_id()
46 path_id
= path_id_next
++;
50 static void restore_path_id(int old_id
)
56 static int is_logical_and(struct expression
*expr
)
58 /* If you have if (!(a && b)) smatch translates that to
59 * if (!a || !b). Logically those are the same.
62 if ((!__negate
&& expr
->op
== SPECIAL_LOGICAL_AND
) ||
63 (__negate
&& expr
->op
== SPECIAL_LOGICAL_OR
))
68 static int is_logical_or(struct expression
*expr
)
70 if ((!__negate
&& expr
->op
== SPECIAL_LOGICAL_OR
) ||
71 (__negate
&& expr
->op
== SPECIAL_LOGICAL_AND
))
76 static void inc_ands_ors(struct expression
*expr
)
78 if (is_logical_and(expr
))
80 else if (is_logical_or(expr
))
84 static void dec_ands_ors(struct expression
*expr
)
86 if (is_logical_and(expr
))
88 else if (is_logical_or(expr
))
92 static void special_kernel_macros(struct expression
*expr
)
95 struct expression
*tmp
;
97 tmp
= first_ptr_list((struct ptr_list
*) expr
->args
);
98 if (tmp
->op
== '!' && tmp
->unop
->op
== '!'
99 && tmp
->unop
->unop
->op
== '(') {
100 split_conditions(tmp
->unop
->unop
->unop
);
102 __pass_to_client(expr
, CONDITION_HOOK
);
110 static int is_zero(struct expression
*expr
)
112 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 0)
115 return is_zero(expr
->unop
);
116 if (expr
->type
== EXPR_CAST
)
117 return is_zero(expr
->cast_expression
);
121 static int handle_zero_comparisons(struct expression
*expr
)
123 struct expression
*tmp
= NULL
;
125 // if left is zero or right is zero
126 if (is_zero(expr
->left
))
128 else if (is_zero(expr
->right
))
133 // "if (foo != 0)" is the same as "if (foo)"
134 if (expr
->op
== SPECIAL_NOTEQUAL
) {
135 split_conditions(tmp
);
139 // "if (foo == 0)" is the same as "if (!foo)"
140 if (expr
->op
== SPECIAL_EQUAL
) {
141 __negate
= (__negate
+ 1)%2;
142 split_conditions(tmp
);
143 __negate
= (__negate
+ 1)%2;
150 void split_conditions(struct expression
*expr
)
153 * If you have if ((a || a = foo()), we know a is
154 * non-null on the true state because 'a' is checked in both and
155 * groups. __ors_reached helps with that. Unfortunately
156 * smatch doesn't handle stuff like if (a && a = foo())
157 * because it never occured to me that people would do that...
160 static int __ors_reached
;
162 SM_DEBUG("%d in split_conditions type=%d\n", get_lineno(), expr
->type
);
164 if (expr
->type
== EXPR_COMPARE
)
165 if (handle_zero_comparisons(expr
))
168 if (expr
->type
== EXPR_LOGICAL
) {
169 unsigned int path_orig
;
172 path_orig
= split_path_id();
173 __split_false_states_mini();
175 split_conditions(expr
->left
);
177 if (is_logical_and(expr
)) {
179 __pop_false_states_mini();
180 split_conditions(expr
->right
);
181 } else if (is_logical_or(expr
)) {
182 if (!__ors_reached
) {
189 __use_false_states_mini();
190 split_conditions(expr
->right
);
194 if (__ands
+ __ors
== 0) {
200 restore_path_id(path_orig
);
202 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '!') {
203 __negate
= (__negate
+ 1)%2;
204 split_conditions(expr
->unop
);
205 __negate
= (__negate
+ 1)%2;
207 } else if (expr
->type
== EXPR_PREOP
&& expr
->op
== '(') {
208 split_conditions(expr
->unop
);
209 } else if (expr
->type
== EXPR_CALL
) {
211 if (expr
->fn
->type
!= EXPR_SYMBOL
||
212 strcmp("__builtin_expect", expr
->fn
->symbol_name
->name
)) {
213 __pass_to_client(expr
, CONDITION_HOOK
);
217 special_kernel_macros(expr
);
219 __pass_to_client(expr
, CONDITION_HOOK
);
224 static void split_whole_condition(struct expression
*expr
)
227 split_conditions(expr
);
228 SM_DEBUG("%d __ands = %d __ors = %d __negate = %d\n", get_lineno(),
229 __ands
, __ors
, __negate
);
232 static void split_expr(struct expression
*expr
)
237 __smatch_lineno
= expr
->pos
.line
;
238 __pass_to_client(expr
, EXPR_HOOK
);
240 // printf("%d Debug expr_type %d\n", get_lineno(), expr->type);
242 switch (expr
->type
) {
246 __pass_to_client(expr
, DEREF_HOOK
);
247 split_expr(expr
->unop
);
250 split_statements(expr
->statement
);
256 case EXPR_ASSIGNMENT
:
257 if (expr
->type
== EXPR_ASSIGNMENT
)
258 __pass_to_client(expr
, ASSIGNMENT_HOOK
);
259 split_expr(expr
->left
);
260 split_expr(expr
->right
);
261 if (expr
->type
== EXPR_ASSIGNMENT
)
262 __pass_to_client(expr
, ASSIGNMENT_AFTER_HOOK
);
265 __pass_to_client(expr
, DEREF_HOOK
);
266 split_expr(expr
->deref
);
269 split_expr(expr
->base
);
273 split_expr(expr
->cast_expression
);
275 case EXPR_CONDITIONAL
:
277 __split_true_false_paths();
278 __pass_to_client(expr
->conditional
, WHOLE_CONDITION_HOOK
);
279 split_whole_condition(expr
->conditional
);
281 split_expr(expr
->cond_true
);
282 __use_false_states();
283 split_expr(expr
->cond_false
);
284 __merge_true_states();
287 __pass_to_client(expr
, FUNCTION_CALL_HOOK
);
288 split_expr(expr
->fn
);
289 split_expr_list(expr
->args
);
290 __pass_to_client(expr
, FUNCTION_CALL_AFTER_HOOK
);
292 if (expr
->fn
->type
== EXPR_SYMBOL
&&
293 !strcmp(expr
->fn
->symbol_name
->name
, "panic"))
297 case EXPR_INITIALIZER
:
298 split_expr_list(expr
->expr_list
);
300 case EXPR_IDENTIFIER
:
301 split_expr(expr
->ident_expression
);
304 split_expr(expr
->idx_expression
);
307 split_expr(expr
->init_expr
);
314 static int is_forever_loop(struct statement
*stmt
)
317 struct expression
*expr
;
319 expr
= stmt
->iterator_pre_condition
;
321 expr
= stmt
->iterator_post_condition
;
323 // this is a for(;;) loop...
327 if (expr
->type
== EXPR_VALUE
&& expr
->value
== 1) {
335 * Pre Loops are while and for loops.
338 static void handle_pre_loop(struct statement
*stmt
)
340 unsigned int path_orig
;
342 split_statements(stmt
->iterator_pre_statement
);
344 __split_true_false_paths();
347 if (stmt
->iterator_pre_condition
) {
348 __pass_to_client(stmt
->iterator_pre_condition
, WHOLE_CONDITION_HOOK
);
349 split_whole_condition(stmt
->iterator_pre_condition
);
351 path_orig
= split_path_id();
354 split_statements(stmt
->iterator_statement
);
355 split_statements(stmt
->iterator_post_statement
);
356 if (is_forever_loop(stmt
)) {
358 __pop_false_states();
361 __merge_false_states();
365 restore_path_id(path_orig
);
369 * Post loops are do {} while();
371 static void handle_post_loop(struct statement
*stmt
)
375 split_statements(stmt
->iterator_statement
);
377 __split_true_false_paths();
378 __pass_to_client(stmt
->iterator_post_condition
, WHOLE_CONDITION_HOOK
);
379 split_whole_condition(stmt
->iterator_post_condition
);
380 /* It would prossibly be cleaner to make this all one function */
382 __use_false_states();
385 if (is_forever_loop(stmt
)) {
394 static void split_statements(struct statement
*stmt
)
396 unsigned int path_orig
;
401 __smatch_lineno
= stmt
->pos
.line
;
402 __pass_to_client(stmt
, STMT_HOOK
);
404 switch (stmt
->type
) {
405 case STMT_DECLARATION
:
406 __pass_declarations_to_client(stmt
->declaration
);
407 split_symlist(stmt
->declaration
);
410 __pass_to_client(stmt
, RETURN_HOOK
);
411 split_expr(stmt
->ret_value
);
414 case STMT_EXPRESSION
:
415 split_expr(stmt
->expression
);
417 case STMT_COMPOUND
: {
419 FOR_EACH_PTR(stmt
->stmts
, s
) {
421 } END_FOR_EACH_PTR(s
);
425 __split_true_false_paths();
426 __pass_to_client(stmt
->if_conditional
, WHOLE_CONDITION_HOOK
);
427 split_whole_condition(stmt
->if_conditional
);
428 path_orig
= split_path_id();
430 split_statements(stmt
->if_true
);
432 __use_false_states();
433 split_statements(stmt
->if_false
);
434 __merge_true_states();
435 restore_path_id(path_orig
);
438 if (stmt
->iterator_pre_condition
)
439 handle_pre_loop(stmt
);
440 else if (stmt
->iterator_post_condition
)
441 handle_post_loop(stmt
);
443 // these are for(;;) type loops.
444 handle_pre_loop(stmt
);
448 split_expr(stmt
->switch_expression
);
449 __save_switch_states();
453 path_orig
= get_path_id();
454 split_statements(stmt
->switch_statement
);
455 if (!__pop_default())
459 restore_path_id(path_orig
);
464 if (!stmt
->case_expression
)
466 split_expr(stmt
->case_expression
);
467 split_expr(stmt
->case_to
);
468 split_statements(stmt
->case_statement
);
472 stmt
->label
->type
== SYM_LABEL
&&
473 stmt
->label
->ident
) {
474 __merge_gotos(stmt
->label
->ident
->name
);
476 split_statements(stmt
->label_statement
);
479 split_expr(stmt
->goto_expression
);
480 if (stmt
->goto_label
&& stmt
->goto_label
->type
== SYM_NODE
) {
481 if (!strcmp(stmt
->goto_label
->ident
->name
, "break")) {
483 } else if (!strcmp(stmt
->goto_label
->ident
->name
,
485 __process_continues();
487 } else if (stmt
->goto_label
&&
488 stmt
->goto_label
->type
== SYM_LABEL
&&
489 stmt
->goto_label
->ident
) {
490 __save_gotos(stmt
->goto_label
->ident
->name
);
497 split_expr(stmt
->asm_string
);
498 //split_expr(stmt->asm_outputs);
499 //split_expr(stmt->asm_inputs);
500 //split_expr(stmt->asm_clobbers);
505 split_expr(stmt
->range_expression
);
506 split_expr(stmt
->range_low
);
507 split_expr(stmt
->range_high
);
512 static void split_expr_list(struct expression_list
*expr_list
)
514 struct expression
*expr
;
515 FOR_EACH_PTR(expr_list
, expr
) {
517 } END_FOR_EACH_PTR(expr
);
521 static void split_sym(struct symbol
*sym
)
525 if (!(sym
->namespace & NS_SYMBOL
))
528 __pass_to_client(sym
, SYM_HOOK
);
529 split_statements(sym
->stmt
);
530 split_expr(sym
->array_size
);
531 split_symlist(sym
->arguments
);
532 split_symlist(sym
->symbol_list
);
533 split_statements(sym
->inline_stmt
);
534 split_symlist(sym
->inline_symbol_list
);
535 split_expr(sym
->initializer
);
538 static void split_symlist(struct symbol_list
*sym_list
)
542 FOR_EACH_PTR(sym_list
, sym
) {
544 } END_FOR_EACH_PTR(sym
);
547 static void split_functions(struct symbol_list
*sym_list
)
551 FOR_EACH_PTR(sym_list
, sym
) {
552 struct symbol
*base_type
;
553 base_type
= get_base_type(sym
);
554 if (sym
->type
== SYM_NODE
&& base_type
->type
== SYM_FN
) {
556 line_func_start
= base_type
->stmt
->pos
.line
;
558 cur_func
= sym
->ident
->name
;
559 __smatch_lineno
= sym
->pos
.line
;
560 __pass_to_client(sym
, FUNC_DEF_HOOK
);
561 split_statements(base_type
->stmt
);
562 __pass_to_client(sym
, END_FUNC_HOOK
);
568 __pass_to_client(sym
, BASE_HOOK
);
570 } END_FOR_EACH_PTR(sym
);
573 void smatch (int argc
, char **argv
)
576 struct string_list
*filelist
= NULL
;
577 struct symbol_list
*sym_list
;
580 printf("Usage: smatch <filename.c>\n");
583 sparse_initialize(argc
, argv
, &filelist
);
584 FOR_EACH_PTR_NOTAG(filelist
, filename
) {
585 sym_list
= __sparse(filename
);
586 split_functions(sym_list
);
587 } END_FOR_EACH_PTR_NOTAG(filename
);