2 * sparse/smatch_helper.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * Miscellaneous helper functions.
22 char *alloc_string(const char *str
)
28 tmp
= malloc(strlen(str
) + 1);
33 void free_string(char *str
)
38 static void append(char *dest
, const char *data
, int buff_len
)
40 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
44 * If you have "foo(a, b, 1);" then use
45 * get_argument_from_call_expr(expr, 0) to return the expression for
46 * a. Yes, it does start counting from 0.
48 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
51 struct expression
*expr
;
57 FOR_EACH_PTR(args
, expr
) {
61 } END_FOR_EACH_PTR(expr
);
65 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
66 struct expression
*expr
, int len
,
69 struct expression
*tmp
;
77 __get_variable_from_expr(sym_ptr
, buf
, tmp
, len
, complicated
);
81 append(buf
, "->", len
);
83 append(buf
, ".", len
);
85 append(buf
, expr
->member
->name
, len
);
89 if (expr
->symbol_name
)
90 append(buf
, expr
->symbol_name
->name
, len
);
94 *sym_ptr
= expr
->symbol
;
100 if (get_block_thing(expr
)) {
105 tmp
= show_special(expr
->op
);
106 append(buf
, tmp
, len
);
107 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
110 if (expr
->op
== '(') {
111 append(buf
, ")", len
);
114 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
122 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
124 tmp
= show_special(expr
->op
);
125 append(buf
, tmp
, len
);
127 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
135 append(buf
, "(", len
);
136 __get_variable_from_expr(NULL
, buf
, expr
->left
, len
,
138 tmp
= show_special(expr
->op
);
139 append(buf
, tmp
, len
);
140 __get_variable_from_expr(sym_ptr
, buf
, expr
->right
,
142 append(buf
, ")", len
);
148 snprintf(tmp
, 25, "%lld", expr
->value
);
149 append(buf
, tmp
, len
);
153 append(buf
, expr
->string
->data
, len
);
156 struct expression
*tmp
;
160 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
162 append(buf
, "(", len
);
164 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
166 append(buf
, ", ", len
);
167 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
169 } END_FOR_EACH_PTR_REVERSE(tmp
);
170 append(buf
, ")", len
);
174 __get_variable_from_expr(sym_ptr
, buf
,
175 expr
->cast_expression
, len
,
182 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
183 size
= (get_base_type(expr
->cast_type
))->bit_size
;
184 snprintf(tmp
, 25, "%d", size
);
185 append(buf
, tmp
, len
);
191 //printf("unknown type = %d\n", expr->type);
198 * This is returns a stylized "c looking" representation of the
201 * It uses the same buffer every time so you have to save the result
202 * yourself if you want to keep it.
206 char *get_variable_from_expr_complex(struct expression
*expr
, struct symbol
**sym_ptr
)
208 static char var_name
[VAR_LEN
];
217 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
220 return alloc_string(var_name
);
226 * get_variable_from_expr_simple() only returns simple variables.
227 * If it's a complicated variable like a->foo instead of just 'a'
228 * then it returns NULL.
231 char *get_variable_from_expr(struct expression
*expr
,
232 struct symbol
**sym_ptr
)
234 static char var_name
[VAR_LEN
];
243 expr
= strip_expr(expr
);
244 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
252 return alloc_string(var_name
);
255 struct symbol
*get_ptr_type_ptr(struct symbol
*sym
)
261 if (sym
->type
!= SYM_NODE
)
263 sym
= get_base_type(sym
);
264 if (sym
->type
!= SYM_PTR
)
266 sym
= get_base_type(sym
);
270 static struct symbol
*get_struct_sym(struct expression
*expr
)
272 struct symbol
*base_type
;
273 struct symbol
*parent_struct
;
276 if (expr
->type
!= EXPR_PREOP
)
280 if (expr
->type
== EXPR_DEREF
) {
281 parent_struct
= get_struct_sym(expr
->deref
);
285 FOR_EACH_PTR(parent_struct
->symbol_list
, tmp
) {
286 if (tmp
->ident
== expr
->member
)
288 } END_FOR_EACH_PTR(tmp
);
289 if (!tmp
|| tmp
->ident
!= expr
->member
)
291 base_type
= get_base_type(tmp
);
292 } else if (expr
->type
== EXPR_SYMBOL
) {
293 base_type
= get_base_type(expr
->symbol
);
297 if (base_type
->type
!= SYM_PTR
)
299 base_type
= get_base_type(base_type
);
300 if (base_type
->type
!= SYM_STRUCT
&& base_type
->type
!= SYM_UNION
)
305 struct symbol
*get_deref_type(struct expression
*expr
)
307 struct ident
*member
= expr
->member
;
308 struct symbol
*struct_sym
;
311 struct_sym
= get_struct_sym(expr
->deref
);
312 if (!struct_sym
|| (struct_sym
->type
!= SYM_STRUCT
313 && struct_sym
->type
!= SYM_UNION
))
315 FOR_EACH_PTR(struct_sym
->symbol_list
, tmp
) {
316 if (tmp
->ident
== member
)
317 return get_ptr_type_ptr(tmp
);
318 } END_FOR_EACH_PTR(tmp
);
322 struct symbol
*get_ptr_type(struct expression
*expr
)
324 struct symbol
*ptr_type
= NULL
;
328 if (expr
->type
== EXPR_DEREF
)
329 ptr_type
= get_deref_type(expr
);
330 if (expr
->type
== EXPR_SYMBOL
)
331 ptr_type
= get_ptr_type_ptr(expr
->symbol
);
335 int sym_name_is(const char *name
, struct expression
*expr
)
339 if (expr
->type
!= EXPR_SYMBOL
)
341 if (!strcmp(expr
->symbol_name
->name
, name
))
349 static int _get_value(struct expression
*expr
, int *discard
, int *undefined
, int implied
)
352 long long ret
= BOGUS
;
365 expr
= strip_expr(expr
);
372 if (expr
->op
== '-') {
373 ret
= - _get_value(expr
->unop
, discard
, undefined
, implied
);
382 left
= _get_value(expr
->left
, discard
, undefined
, implied
);
383 right
= _get_value(expr
->right
, discard
, undefined
, implied
);
384 if (expr
->op
== '*') {
386 } else if (expr
->op
== '/') {
388 } else if (expr
->op
== '+') {
390 } else if (expr
->op
== '-') {
392 } else if (expr
->op
== '|') {
394 } else if (expr
->op
== '&') {
396 } else if (expr
->op
== SPECIAL_RIGHTSHIFT
) {
398 } else if (expr
->op
== SPECIAL_LEFTSHIFT
) {
408 ret
= get_expression_value(expr
);
412 if (!get_implied_single_val(expr
, &ret
)) {
428 /* returns 1 if it can get a value literal or else returns 0 */
429 int get_value(struct expression
*expr
, long long *val
)
433 *val
= _get_value(expr
, NULL
, &undefined
, NOTIMPLIED
);
439 int get_implied_value(struct expression
*expr
, long long *val
)
443 *val
= _get_value(expr
, NULL
, &undefined
, IMPLIED
);
447 int is_zero(struct expression
*expr
)
451 if (get_value(expr
, &val
) && val
== 0)
456 int is_array(struct expression
*expr
)
458 expr
= strip_expr(expr
);
459 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
467 struct expression
*get_array_name(struct expression
*expr
)
471 return expr
->unop
->left
;
474 struct expression
*get_array_offset(struct expression
*expr
)
478 return expr
->unop
->right
;
481 const char *show_state(struct smatch_state
*state
)
488 struct statement
*get_block_thing(struct expression
*expr
)
490 /* What are those things called? if (({....; ret;})) { ...*/
492 if (expr
->type
!= EXPR_PREOP
)
496 if (expr
->unop
->type
!= EXPR_STATEMENT
)
498 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
500 return expr
->unop
->statement
;
503 struct expression
*strip_expr(struct expression
*expr
)
510 return strip_expr(expr
->cast_expression
);
512 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
513 expr
->unop
->statement
->type
== STMT_COMPOUND
)
516 return strip_expr(expr
->unop
);
521 static void delete_state_tracker(struct tracker
*t
)
523 delete_state(t
->owner
, t
->name
, t
->sym
);
527 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
531 t
= alloc_tracker(my_id
, name
, sym
);
532 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
535 int is_error_return(struct expression
*expr
)
537 struct symbol
*cur_func
= cur_func_sym
;
542 if (cur_func
->type
!= SYM_NODE
)
544 cur_func
= get_base_type(cur_func
);
545 if (cur_func
->type
!= SYM_FN
)
547 cur_func
= get_base_type(cur_func
);
548 if (cur_func
== &void_ctype
)
550 if (!get_value(expr
, &val
))
554 if (cur_func
->type
== SYM_PTR
&& val
== 0)