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.
21 char *alloc_string(const char *str
)
27 tmp
= malloc(strlen(str
) + 1);
32 void free_string(char *str
)
37 struct smatch_state
*alloc_state_num(int num
)
39 struct smatch_state
*state
;
40 static char buff
[256];
42 state
= __alloc_smatch_state(0);
43 snprintf(buff
, 255, "%d", num
);
45 state
->name
= alloc_string(buff
);
46 state
->data
= (void *)num
;
50 static void append(char *dest
, const char *data
, int buff_len
)
52 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
56 * If you have "foo(a, b, 1);" then use
57 * get_argument_from_call_expr(expr, 0) to return the expression for
58 * a. Yes, it does start counting from 0.
60 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
63 struct expression
*expr
;
69 FOR_EACH_PTR(args
, expr
) {
73 } END_FOR_EACH_PTR(expr
);
77 static struct expression
*get_array_expr(struct expression
*expr
)
81 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
84 type
= get_type(expr
->left
);
85 if (!type
|| type
->type
!= SYM_ARRAY
)
90 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
91 struct expression
*expr
, int len
,
94 struct expression
*tmp
;
102 __get_variable_from_expr(sym_ptr
, buf
, tmp
, len
, complicated
);
105 if (tmp
->op
== '*') {
106 append(buf
, "->", len
);
108 append(buf
, ".", len
);
110 append(buf
, expr
->member
->name
, len
);
114 if (expr
->symbol_name
)
115 append(buf
, expr
->symbol_name
->name
, len
);
119 *sym_ptr
= expr
->symbol
;
125 if (get_block_thing(expr
)) {
130 if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
131 tmp
= show_special(expr
->op
);
132 append(buf
, tmp
, len
);
134 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
137 if (expr
->op
== '(') {
138 append(buf
, ")", len
);
141 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
149 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
151 tmp
= show_special(expr
->op
);
152 append(buf
, tmp
, len
);
154 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
160 struct expression
*array_expr
;
163 array_expr
= get_array_expr(expr
);
165 __get_variable_from_expr(NULL
, buf
, array_expr
, len
, complicated
);
166 append(buf
, "[", len
);
168 append(buf
, "(", len
);
169 __get_variable_from_expr(NULL
, buf
, expr
->left
, len
,
171 tmp
= show_special(expr
->op
);
172 append(buf
, tmp
, len
);
174 __get_variable_from_expr(sym_ptr
, buf
, expr
->right
,
177 append(buf
, "]", len
);
179 append(buf
, ")", len
);
185 snprintf(tmp
, 25, "%lld", expr
->value
);
186 append(buf
, tmp
, len
);
190 append(buf
, "\"", len
);
191 append(buf
, expr
->string
->data
, len
);
192 append(buf
, "\"", len
);
195 struct expression
*tmp
;
199 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
201 append(buf
, "(", len
);
203 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
205 append(buf
, ", ", len
);
206 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
208 } END_FOR_EACH_PTR_REVERSE(tmp
);
209 append(buf
, ")", len
);
213 __get_variable_from_expr(sym_ptr
, buf
,
214 expr
->cast_expression
, len
,
221 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
222 size
= (get_base_type(expr
->cast_type
))->bit_size
;
223 snprintf(tmp
, 25, "%d", size
);
224 append(buf
, tmp
, len
);
230 //printf("unknown type = %d\n", expr->type);
236 * This is returns a stylized "c looking" representation of the
239 * It uses the same buffer every time so you have to save the result
240 * yourself if you want to keep it.
244 char *get_variable_from_expr_complex(struct expression
*expr
, struct symbol
**sym_ptr
)
246 static char var_name
[VAR_LEN
];
255 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
258 return alloc_string(var_name
);
264 * get_variable_from_expr_simple() only returns simple variables.
265 * If it's a complicated variable like a->foo instead of just 'a'
266 * then it returns NULL.
269 char *get_variable_from_expr(struct expression
*expr
,
270 struct symbol
**sym_ptr
)
272 static char var_name
[VAR_LEN
];
281 expr
= strip_expr(expr
);
282 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
290 return alloc_string(var_name
);
293 int sym_name_is(const char *name
, struct expression
*expr
)
297 if (expr
->type
!= EXPR_SYMBOL
)
299 if (!strcmp(expr
->symbol_name
->name
, name
))
304 int is_zero(struct expression
*expr
)
308 if (get_value(expr
, &val
) && val
== 0)
313 int is_array(struct expression
*expr
)
315 expr
= strip_expr(expr
);
316 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
324 struct expression
*get_array_name(struct expression
*expr
)
328 return strip_expr(expr
->unop
->left
);
331 struct expression
*get_array_offset(struct expression
*expr
)
335 return expr
->unop
->right
;
338 const char *show_state(struct smatch_state
*state
)
345 struct statement
*get_block_thing(struct expression
*expr
)
347 /* What are those things called? if (({....; ret;})) { ...*/
349 if (expr
->type
!= EXPR_PREOP
)
353 if (expr
->unop
->type
!= EXPR_STATEMENT
)
355 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
357 return expr
->unop
->statement
;
360 struct expression
*strip_parens(struct expression
*expr
)
365 if (expr
->type
== EXPR_PREOP
) {
366 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
367 expr
->unop
->statement
->type
== STMT_COMPOUND
)
370 return strip_parens(expr
->unop
);
375 struct expression
*strip_expr(struct expression
*expr
)
380 switch (expr
->type
) {
381 case EXPR_FORCE_CAST
:
383 return strip_expr(expr
->cast_expression
);
385 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
386 expr
->unop
->statement
->type
== STMT_COMPOUND
)
389 return strip_expr(expr
->unop
);
394 static void delete_state_tracker(struct tracker
*t
)
396 delete_state(t
->owner
, t
->name
, t
->sym
);
400 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
404 t
= alloc_tracker(my_id
, name
, sym
);
405 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
408 int is_error_return(struct expression
*expr
)
410 struct symbol
*cur_func
= cur_func_sym
;
415 if (cur_func
->type
!= SYM_NODE
)
417 cur_func
= get_base_type(cur_func
);
418 if (cur_func
->type
!= SYM_FN
)
420 cur_func
= get_base_type(cur_func
);
421 if (cur_func
== &void_ctype
)
423 if (!get_value(expr
, &val
))
427 if (cur_func
->type
== SYM_PTR
&& val
== 0)
432 int getting_address(void)
434 struct expression
*tmp
;
438 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
441 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
443 if (tmp
->op
== '.' && !dot_ops
++)
448 } END_FOR_EACH_PTR_REVERSE(tmp
);