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_parens(expr
);
306 if (expr
->op
== '-') {
307 ret
= - _get_value(expr
->unop
, discard
, undefined
, implied
);
314 case EXPR_FORCE_CAST
:
315 case EXPR_IMPLIED_CAST
:
317 struct symbol
*type
= get_base_type(expr
->cast_type
);
319 ret
= _get_value(expr
->cast_expression
, discard
, undefined
, implied
);
320 switch (type
->bit_size
) {
322 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
323 ret
= (int)(unsigned char) ret
;
325 ret
= (int)(char) ret
;
328 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
329 ret
= (int)(unsigned short) ret
;
331 ret
= (int)(short) ret
;
334 if (type
->ctype
.modifiers
& MOD_UNSIGNED
)
335 ret
= (int)(unsigned int) ret
;
337 ret
= (int)(int) ret
;
345 left
= _get_value(expr
->left
, discard
, undefined
, implied
);
346 right
= _get_value(expr
->right
, discard
, undefined
, implied
);
347 if (expr
->op
== '*') {
349 } else if (expr
->op
== '/') {
351 } else if (expr
->op
== '+') {
353 } else if (expr
->op
== '-') {
355 } else if (expr
->op
== '|') {
357 } else if (expr
->op
== '&') {
359 } else if (expr
->op
== SPECIAL_RIGHTSHIFT
) {
361 } else if (expr
->op
== SPECIAL_LEFTSHIFT
) {
371 ret
= get_expression_value(expr
);
374 if (implied
== IMPLIED
) {
375 if (!get_implied_single_val(expr
, &ret
)) {
379 } else if (implied
== FUZZYMAX
) {
380 if (!get_implied_single_fuzzy_max(expr
, &ret
)) {
396 /* returns 1 if it can get a value literal or else returns 0 */
397 int get_value(struct expression
*expr
, long long *val
)
401 *val
= _get_value(expr
, NULL
, &undefined
, NOTIMPLIED
);
407 int get_implied_value(struct expression
*expr
, long long *val
)
411 *val
= _get_value(expr
, NULL
, &undefined
, IMPLIED
);
415 int get_fuzzy_max(struct expression
*expr
, long long *val
)
419 *val
= _get_value(expr
, NULL
, &undefined
, FUZZYMAX
);
423 int is_zero(struct expression
*expr
)
427 if (get_value(expr
, &val
) && val
== 0)
432 int is_array(struct expression
*expr
)
434 expr
= strip_expr(expr
);
435 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
443 struct expression
*get_array_name(struct expression
*expr
)
447 return expr
->unop
->left
;
450 struct expression
*get_array_offset(struct expression
*expr
)
454 return expr
->unop
->right
;
457 const char *show_state(struct smatch_state
*state
)
464 struct statement
*get_block_thing(struct expression
*expr
)
466 /* What are those things called? if (({....; ret;})) { ...*/
468 if (expr
->type
!= EXPR_PREOP
)
472 if (expr
->unop
->type
!= EXPR_STATEMENT
)
474 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
476 return expr
->unop
->statement
;
479 struct expression
*strip_parens(struct expression
*expr
)
484 if (expr
->type
== EXPR_PREOP
) {
485 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
486 expr
->unop
->statement
->type
== STMT_COMPOUND
)
489 return strip_parens(expr
->unop
);
494 struct expression
*strip_expr(struct expression
*expr
)
499 switch (expr
->type
) {
501 return strip_expr(expr
->cast_expression
);
503 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
504 expr
->unop
->statement
->type
== STMT_COMPOUND
)
507 return strip_expr(expr
->unop
);
512 static void delete_state_tracker(struct tracker
*t
)
514 delete_state(t
->owner
, t
->name
, t
->sym
);
518 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
522 t
= alloc_tracker(my_id
, name
, sym
);
523 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
526 int is_error_return(struct expression
*expr
)
528 struct symbol
*cur_func
= cur_func_sym
;
533 if (cur_func
->type
!= SYM_NODE
)
535 cur_func
= get_base_type(cur_func
);
536 if (cur_func
->type
!= SYM_FN
)
538 cur_func
= get_base_type(cur_func
);
539 if (cur_func
== &void_ctype
)
541 if (!get_value(expr
, &val
))
545 if (cur_func
->type
== SYM_PTR
&& val
== 0)
550 int getting_address()
552 struct expression
*tmp
;
556 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
559 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
561 if (tmp
->op
== '.' && !dot_ops
++)
566 } END_FOR_EACH_PTR_REVERSE(tmp
);