2 * sparse/smatch_helper.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
18 char *alloc_string(const char *str
)
24 tmp
= malloc(strlen(str
) + 1);
29 void free_string(char *str
)
34 static void append(char *dest
, const char *data
, int buff_len
)
36 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
40 * If you have "foo(a, b, 1);" then use
41 * get_argument_from_call_expr(expr, 0) to return the expression for
42 * a. Yes, it does start counting from 0.
44 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
47 struct expression
*expr
;
53 FOR_EACH_PTR(args
, expr
) {
57 } END_FOR_EACH_PTR(expr
);
61 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
62 struct expression
*expr
, int len
,
65 struct expression
*tmp
;
70 if (!strcmp(show_special(tmp
->op
), "*")) {
73 __get_variable_from_expr(sym_ptr
, buf
, tmp
, len
, complicated
);
76 if (!strcmp(show_special(tmp
->op
), "*")) {
77 append(buf
, "->", len
);
79 append(buf
, ".", len
);
81 append(buf
, expr
->member
->name
, len
);
85 if (expr
->symbol_name
)
86 append(buf
, expr
->symbol_name
->name
, len
);
90 *sym_ptr
= expr
->symbol
;
96 if (get_block_thing(expr
)) {
102 tmp
= show_special(expr
->op
);
103 append(buf
, tmp
, len
);
104 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
108 append(buf
, ")", len
);
111 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
119 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
121 tmp
= show_special(expr
->op
);
122 append(buf
, tmp
, len
);
124 if ((!strcmp(tmp
, "--")) || (!strcmp(tmp
, "++")))
133 append(buf
, "(", len
);
134 __get_variable_from_expr(NULL
, buf
, expr
->left
, len
,
136 tmp
= show_special(expr
->op
);
137 append(buf
, tmp
, len
);
138 __get_variable_from_expr(sym_ptr
, buf
, expr
->right
,
140 append(buf
, ")", len
);
146 snprintf(tmp
, 25, "%lld", expr
->value
);
147 append(buf
, tmp
, len
);
151 append(buf
, expr
->string
->data
, len
);
154 struct expression
*tmp
;
158 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
160 append(buf
, "(", len
);
162 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
164 append(buf
, ", ", len
);
165 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
167 } END_FOR_EACH_PTR_REVERSE(tmp
);
168 append(buf
, ")", len
);
172 __get_variable_from_expr(sym_ptr
, buf
,
173 expr
->cast_expression
, len
,
180 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
181 size
= (get_base_type(expr
->cast_type
))->bit_size
;
182 snprintf(tmp
, 25, "%d", size
);
183 append(buf
, tmp
, len
);
189 //printf("unknown type = %d\n", expr->type);
196 * This is returns a stylized "c looking" representation of the
199 * It uses the same buffer every time so you have to save the result
200 * yourself if you want to keep it.
204 char *get_variable_from_expr_complex(struct expression
*expr
, struct symbol
**sym_ptr
)
206 static char var_name
[VAR_LEN
];
215 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
218 return alloc_string(var_name
);
224 * get_variable_from_expr_simple() only returns simple variables.
225 * If it's a complicated variable like a->foo instead of just 'a'
226 * then it returns NULL.
229 char *get_variable_from_expr(struct expression
*expr
,
230 struct symbol
**sym_ptr
)
232 static char var_name
[VAR_LEN
];
241 expr
= strip_expr(expr
);
242 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
250 return alloc_string(var_name
);
253 struct symbol
*get_ptr_type_ptr(struct symbol
*sym
)
259 if (sym
->type
!= SYM_NODE
)
261 sym
= get_base_type(sym
);
262 if (sym
->type
!= SYM_PTR
)
264 sym
= get_base_type(sym
);
268 static struct symbol
*get_struct_sym(struct expression
*expr
)
270 struct symbol
*base_type
;
271 struct symbol
*parent_struct
;
274 if (expr
->type
!= EXPR_PREOP
)
278 if (expr
->type
== EXPR_DEREF
) {
279 parent_struct
= get_struct_sym(expr
->deref
);
283 FOR_EACH_PTR(parent_struct
->symbol_list
, tmp
) {
284 if (tmp
->ident
== expr
->member
)
286 } END_FOR_EACH_PTR(tmp
);
287 if (!tmp
|| tmp
->ident
!= expr
->member
)
289 base_type
= get_base_type(tmp
);
290 } else if (expr
->type
== EXPR_SYMBOL
) {
291 base_type
= get_base_type(expr
->symbol
);
295 if (base_type
->type
!= SYM_PTR
)
297 base_type
= get_base_type(base_type
);
298 if (base_type
->type
!= SYM_STRUCT
&& base_type
->type
!= SYM_UNION
)
303 struct symbol
*get_deref_type(struct expression
*expr
)
305 struct ident
*member
= expr
->member
;
306 struct symbol
*struct_sym
;
309 struct_sym
= get_struct_sym(expr
->deref
);
310 if (!struct_sym
|| (struct_sym
->type
!= SYM_STRUCT
311 && struct_sym
->type
!= SYM_UNION
))
313 FOR_EACH_PTR(struct_sym
->symbol_list
, tmp
) {
314 if (tmp
->ident
== member
)
315 return get_ptr_type_ptr(tmp
);
316 } END_FOR_EACH_PTR(tmp
);
320 struct symbol
*get_ptr_type(struct expression
*expr
)
322 struct symbol
*ptr_type
= NULL
;
326 if (expr
->type
== EXPR_DEREF
)
327 ptr_type
= get_deref_type(expr
);
328 if (expr
->type
== EXPR_SYMBOL
)
329 ptr_type
= get_ptr_type_ptr(expr
->symbol
);
333 int sym_name_is(const char *name
, struct expression
*expr
)
337 if (expr
->type
!= EXPR_SYMBOL
)
339 if (!strcmp(expr
->symbol_name
->name
, name
))
347 static int _get_value(struct expression
*expr
, int *discard
, int implied
)
359 expr
= strip_expr(expr
);
366 if (!strcmp("-", show_special(expr
->op
)))
367 ret
= - _get_value(expr
->unop
, discard
, implied
);
374 if (!show_special(expr
->op
)) {
378 left
= _get_value(expr
->left
, discard
, implied
);
379 right
= _get_value(expr
->right
, discard
, implied
);
380 if (!strcmp("*", show_special(expr
->op
))) {
382 } else if (!strcmp("/", show_special(expr
->op
))) {
384 } else if (!strcmp("+", show_special(expr
->op
))) {
386 } else if (!strcmp("-", show_special(expr
->op
))) {
388 } else if (!strcmp("|", show_special(expr
->op
))) {
390 } else if (!strcmp("&", show_special(expr
->op
))) {
392 } else if (!strcmp(">>", show_special(expr
->op
))) {
394 } else if (!strcmp("<<", show_special(expr
->op
))) {
403 ret
= get_expression_value(expr
);
407 ret
= get_implied_single_val(expr
);
408 if (ret
== UNDEFINED
)
419 /* returns UNDEFINED on error */
420 int get_value(struct expression
*expr
)
422 return _get_value(expr
, NULL
, NOTIMPLIED
);
425 int get_implied_value(struct expression
*expr
)
427 return _get_value(expr
, NULL
, IMPLIED
);
430 int is_zero(struct expression
*expr
)
432 if (get_value(expr
) == 0)
437 int is_array(struct expression
*expr
)
439 expr
= strip_expr(expr
);
440 if (expr
->type
!= EXPR_PREOP
|| strcmp(show_special(expr
->op
), "*"))
443 if (strcmp(show_special(expr
->op
), "+"))
448 struct expression
*get_array_name(struct expression
*expr
)
452 return expr
->unop
->left
;
455 struct expression
*get_array_offset(struct expression
*expr
)
459 return expr
->unop
->right
;
462 const char *show_state(struct smatch_state
*state
)
469 struct statement
*get_block_thing(struct expression
*expr
)
471 /* What are those things called? if (({....; ret;})) { ...*/
473 if (expr
->type
!= EXPR_PREOP
)
477 if (expr
->unop
->type
!= EXPR_STATEMENT
)
479 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
481 return expr
->unop
->statement
;
484 struct expression
*strip_expr(struct expression
*expr
)
491 return strip_expr(expr
->cast_expression
);
493 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
494 expr
->unop
->statement
->type
== STMT_COMPOUND
)
497 return strip_expr(expr
->unop
);
502 static void delete_state_tracker(struct tracker
*t
)
504 delete_state(t
->owner
, t
->name
, t
->sym
);
508 void scoped_state(const char *name
, int my_id
, struct symbol
*sym
)
512 t
= alloc_tracker(my_id
, name
, sym
);
513 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);