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 void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
79 struct expression
*expr
, int len
,
82 struct expression
*tmp
;
90 __get_variable_from_expr(sym_ptr
, buf
, tmp
, len
, complicated
);
94 append(buf
, "->", len
);
96 append(buf
, ".", len
);
98 append(buf
, expr
->member
->name
, len
);
102 if (expr
->symbol_name
)
103 append(buf
, expr
->symbol_name
->name
, len
);
107 *sym_ptr
= expr
->symbol
;
113 if (get_block_thing(expr
)) {
118 tmp
= show_special(expr
->op
);
119 append(buf
, tmp
, len
);
120 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
123 if (expr
->op
== '(') {
124 append(buf
, ")", len
);
127 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
135 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
137 tmp
= show_special(expr
->op
);
138 append(buf
, tmp
, len
);
140 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
148 append(buf
, "(", len
);
149 __get_variable_from_expr(NULL
, buf
, expr
->left
, len
,
151 tmp
= show_special(expr
->op
);
152 append(buf
, tmp
, len
);
153 __get_variable_from_expr(sym_ptr
, buf
, expr
->right
,
155 append(buf
, ")", len
);
161 snprintf(tmp
, 25, "%lld", expr
->value
);
162 append(buf
, tmp
, len
);
166 append(buf
, expr
->string
->data
, len
);
169 struct expression
*tmp
;
173 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
175 append(buf
, "(", len
);
177 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
179 append(buf
, ", ", len
);
180 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
182 } END_FOR_EACH_PTR_REVERSE(tmp
);
183 append(buf
, ")", len
);
187 __get_variable_from_expr(sym_ptr
, buf
,
188 expr
->cast_expression
, len
,
195 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
196 size
= (get_base_type(expr
->cast_type
))->bit_size
;
197 snprintf(tmp
, 25, "%d", size
);
198 append(buf
, tmp
, len
);
204 //printf("unknown type = %d\n", expr->type);
211 * This is returns a stylized "c looking" representation of the
214 * It uses the same buffer every time so you have to save the result
215 * yourself if you want to keep it.
219 char *get_variable_from_expr_complex(struct expression
*expr
, struct symbol
**sym_ptr
)
221 static char var_name
[VAR_LEN
];
230 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
233 return alloc_string(var_name
);
239 * get_variable_from_expr_simple() only returns simple variables.
240 * If it's a complicated variable like a->foo instead of just 'a'
241 * then it returns NULL.
244 char *get_variable_from_expr(struct expression
*expr
,
245 struct symbol
**sym_ptr
)
247 static char var_name
[VAR_LEN
];
256 expr
= strip_expr(expr
);
257 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
265 return alloc_string(var_name
);
268 int sym_name_is(const char *name
, struct expression
*expr
)
272 if (expr
->type
!= EXPR_SYMBOL
)
274 if (!strcmp(expr
->symbol_name
->name
, name
))
283 static int _get_value(struct expression
*expr
, int *discard
, int *undefined
, int implied
)
286 long long ret
= BOGUS
;
299 expr
= strip_expr(expr
);
306 if (expr
->op
== '-') {
307 ret
= - _get_value(expr
->unop
, discard
, undefined
, implied
);
316 left
= _get_value(expr
->left
, discard
, undefined
, implied
);
317 right
= _get_value(expr
->right
, discard
, undefined
, implied
);
318 if (expr
->op
== '*') {
320 } else if (expr
->op
== '/') {
322 } else if (expr
->op
== '+') {
324 } else if (expr
->op
== '-') {
326 } else if (expr
->op
== '|') {
328 } else if (expr
->op
== '&') {
330 } else if (expr
->op
== SPECIAL_RIGHTSHIFT
) {
332 } else if (expr
->op
== SPECIAL_LEFTSHIFT
) {
342 ret
= get_expression_value(expr
);
345 if (implied
== IMPLIED
) {
346 if (!get_implied_single_val(expr
, &ret
)) {
350 } else if (implied
== FUZZYMAX
) {
351 if (!get_implied_single_fuzzy_max(expr
, &ret
)) {
367 /* returns 1 if it can get a value literal or else returns 0 */
368 int get_value(struct expression
*expr
, long long *val
)
372 *val
= _get_value(expr
, NULL
, &undefined
, NOTIMPLIED
);
378 int get_implied_value(struct expression
*expr
, long long *val
)
382 *val
= _get_value(expr
, NULL
, &undefined
, IMPLIED
);
386 int get_fuzzy_max(struct expression
*expr
, long long *val
)
390 *val
= _get_value(expr
, NULL
, &undefined
, FUZZYMAX
);
394 int is_zero(struct expression
*expr
)
398 if (get_value(expr
, &val
) && val
== 0)
403 int is_array(struct expression
*expr
)
405 expr
= strip_expr(expr
);
406 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
414 struct expression
*get_array_name(struct expression
*expr
)
418 return expr
->unop
->left
;
421 struct expression
*get_array_offset(struct expression
*expr
)
425 return expr
->unop
->right
;
428 const char *show_state(struct smatch_state
*state
)
435 struct statement
*get_block_thing(struct expression
*expr
)
437 /* What are those things called? if (({....; ret;})) { ...*/
439 if (expr
->type
!= EXPR_PREOP
)
443 if (expr
->unop
->type
!= EXPR_STATEMENT
)
445 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
447 return expr
->unop
->statement
;
450 struct expression
*strip_expr(struct expression
*expr
)
457 return strip_expr(expr
->cast_expression
);
459 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
460 expr
->unop
->statement
->type
== STMT_COMPOUND
)
463 return strip_expr(expr
->unop
);
468 static void delete_state_tracker(struct tracker
*t
)
470 delete_state(t
->owner
, t
->name
, t
->sym
);
474 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
478 t
= alloc_tracker(my_id
, name
, sym
);
479 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
482 int is_error_return(struct expression
*expr
)
484 struct symbol
*cur_func
= cur_func_sym
;
489 if (cur_func
->type
!= SYM_NODE
)
491 cur_func
= get_base_type(cur_func
);
492 if (cur_func
->type
!= SYM_FN
)
494 cur_func
= get_base_type(cur_func
);
495 if (cur_func
== &void_ctype
)
497 if (!get_value(expr
, &val
))
501 if (cur_func
->type
== SYM_PTR
&& val
== 0)
506 int getting_address()
508 struct expression
*tmp
;
512 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
517 if (tmp
->op
== '.' && !dot_ops
++)
522 } END_FOR_EACH_PTR_REVERSE(tmp
);