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 struct smatch_state
*alloc_state_num(int num
)
40 struct smatch_state
*state
;
41 static char buff
[256];
43 state
= __alloc_smatch_state(0);
44 snprintf(buff
, 255, "%d", num
);
46 state
->name
= alloc_string(buff
);
47 state
->data
= (void *)num
;
51 static void append(char *dest
, const char *data
, int buff_len
)
53 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
57 * If you have "foo(a, b, 1);" then use
58 * get_argument_from_call_expr(expr, 0) to return the expression for
59 * a. Yes, it does start counting from 0.
61 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
64 struct expression
*expr
;
70 FOR_EACH_PTR(args
, expr
) {
74 } END_FOR_EACH_PTR(expr
);
78 static struct expression
*get_array_expr(struct expression
*expr
)
82 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
85 type
= get_type(expr
->left
);
86 if (!type
|| type
->type
!= SYM_ARRAY
)
91 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
92 struct expression
*expr
, int len
,
95 struct expression
*tmp
;
100 if (tmp
->op
== '*') {
103 __get_variable_from_expr(sym_ptr
, buf
, tmp
, len
, complicated
);
106 if (tmp
->op
== '*') {
107 append(buf
, "->", len
);
109 append(buf
, ".", len
);
111 append(buf
, expr
->member
->name
, len
);
115 if (expr
->symbol_name
)
116 append(buf
, expr
->symbol_name
->name
, len
);
120 *sym_ptr
= expr
->symbol
;
126 if (get_block_thing(expr
)) {
131 if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
132 tmp
= show_special(expr
->op
);
133 append(buf
, tmp
, len
);
135 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
138 if (expr
->op
== '(') {
139 append(buf
, ")", len
);
142 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
150 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
152 tmp
= show_special(expr
->op
);
153 append(buf
, tmp
, len
);
155 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
161 struct expression
*array_expr
;
164 array_expr
= get_array_expr(expr
);
166 __get_variable_from_expr(NULL
, buf
, array_expr
, len
, complicated
);
167 append(buf
, "[", len
);
169 append(buf
, "(", len
);
170 __get_variable_from_expr(NULL
, buf
, expr
->left
, len
,
172 tmp
= show_special(expr
->op
);
173 append(buf
, tmp
, len
);
175 __get_variable_from_expr(sym_ptr
, buf
, expr
->right
,
178 append(buf
, "]", len
);
180 append(buf
, ")", len
);
186 snprintf(tmp
, 25, "%lld", expr
->value
);
187 append(buf
, tmp
, len
);
191 append(buf
, expr
->string
->data
, len
);
194 struct expression
*tmp
;
198 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
200 append(buf
, "(", len
);
202 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
204 append(buf
, ", ", len
);
205 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
207 } END_FOR_EACH_PTR_REVERSE(tmp
);
208 append(buf
, ")", len
);
212 __get_variable_from_expr(sym_ptr
, buf
,
213 expr
->cast_expression
, len
,
220 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
221 size
= (get_base_type(expr
->cast_type
))->bit_size
;
222 snprintf(tmp
, 25, "%d", size
);
223 append(buf
, tmp
, len
);
229 //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
))
308 static long long _get_value(struct expression
*expr
, int *discard
, int *undefined
, int implied
)
311 long long ret
= BOGUS
;
324 expr
= strip_parens(expr
);
331 if (expr
->op
== '-') {
332 ret
= - _get_value(expr
->unop
, discard
, undefined
, implied
);
339 case EXPR_FORCE_CAST
:
340 case EXPR_IMPLIED_CAST
:
342 struct symbol
*type
= get_base_type(expr
->cast_type
);
344 ret
= _get_value(expr
->cast_expression
, discard
, undefined
, implied
);
345 switch (type
->bit_size
) {
347 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
348 ret
= (int)(unsigned char) ret
;
350 ret
= (int)(char) ret
;
353 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
354 ret
= (int)(unsigned short) ret
;
356 ret
= (int)(short) ret
;
359 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
360 ret
= (int)(unsigned int) ret
;
362 ret
= (int)(int) ret
;
370 left
= _get_value(expr
->left
, discard
, undefined
, implied
);
371 right
= _get_value(expr
->right
, discard
, undefined
, implied
);
372 if (expr
->op
== '*') {
374 } else if (expr
->op
== '/') {
376 } else if (expr
->op
== '+') {
378 } else if (expr
->op
== '-') {
380 } else if (expr
->op
== '|') {
382 } else if (expr
->op
== '&') {
384 } else if (expr
->op
== SPECIAL_RIGHTSHIFT
) {
386 } else if (expr
->op
== SPECIAL_LEFTSHIFT
) {
396 ret
= get_expression_value(expr
);
399 if (implied
== IMPLIED
) {
400 if (!get_implied_single_val(expr
, &ret
)) {
404 } else if (implied
== FUZZYMAX
) {
405 if (!get_implied_single_fuzzy_max(expr
, &ret
)) {
421 /* returns 1 if it can get a value literal or else returns 0 */
422 int get_value(struct expression
*expr
, long long *val
)
426 *val
= _get_value(expr
, NULL
, &undefined
, NOTIMPLIED
);
432 int get_implied_value(struct expression
*expr
, long long *val
)
436 *val
= _get_value(expr
, NULL
, &undefined
, IMPLIED
);
440 int get_fuzzy_max(struct expression
*expr
, long long *val
)
444 *val
= _get_value(expr
, NULL
, &undefined
, FUZZYMAX
);
448 int is_zero(struct expression
*expr
)
452 if (get_value(expr
, &val
) && val
== 0)
457 int is_array(struct expression
*expr
)
459 expr
= strip_expr(expr
);
460 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
468 struct expression
*get_array_name(struct expression
*expr
)
472 return strip_expr(expr
->unop
->left
);
475 struct expression
*get_array_offset(struct expression
*expr
)
479 return expr
->unop
->right
;
482 const char *show_state(struct smatch_state
*state
)
489 struct statement
*get_block_thing(struct expression
*expr
)
491 /* What are those things called? if (({....; ret;})) { ...*/
493 if (expr
->type
!= EXPR_PREOP
)
497 if (expr
->unop
->type
!= EXPR_STATEMENT
)
499 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
501 return expr
->unop
->statement
;
504 struct expression
*strip_parens(struct expression
*expr
)
509 if (expr
->type
== EXPR_PREOP
) {
510 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
511 expr
->unop
->statement
->type
== STMT_COMPOUND
)
514 return strip_parens(expr
->unop
);
519 struct expression
*strip_expr(struct expression
*expr
)
524 switch (expr
->type
) {
526 return strip_expr(expr
->cast_expression
);
528 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
529 expr
->unop
->statement
->type
== STMT_COMPOUND
)
532 return strip_expr(expr
->unop
);
537 static void delete_state_tracker(struct tracker
*t
)
539 delete_state(t
->owner
, t
->name
, t
->sym
);
543 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
547 t
= alloc_tracker(my_id
, name
, sym
);
548 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
551 int is_error_return(struct expression
*expr
)
553 struct symbol
*cur_func
= cur_func_sym
;
558 if (cur_func
->type
!= SYM_NODE
)
560 cur_func
= get_base_type(cur_func
);
561 if (cur_func
->type
!= SYM_FN
)
563 cur_func
= get_base_type(cur_func
);
564 if (cur_func
== &void_ctype
)
566 if (!get_value(expr
, &val
))
570 if (cur_func
->type
== SYM_PTR
&& val
== 0)
575 int getting_address(void)
577 struct expression
*tmp
;
581 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
584 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
586 if (tmp
->op
== '.' && !dot_ops
++)
591 } END_FOR_EACH_PTR_REVERSE(tmp
);