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 void remove_parens(char *str
)
42 while (*src
!= '\0') {
43 if (*src
== '(' || *src
== ')') {
52 struct smatch_state
*alloc_state_num(int num
)
54 struct smatch_state
*state
;
55 static char buff
[256];
57 state
= __alloc_smatch_state(0);
58 snprintf(buff
, 255, "%d", num
);
60 state
->name
= alloc_string(buff
);
61 state
->data
= INT_PTR(num
);
65 static void append(char *dest
, const char *data
, int buff_len
)
67 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
71 * If you have "foo(a, b, 1);" then use
72 * get_argument_from_call_expr(expr, 0) to return the expression for
73 * a. Yes, it does start counting from 0.
75 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
78 struct expression
*expr
;
84 FOR_EACH_PTR(args
, expr
) {
88 } END_FOR_EACH_PTR(expr
);
92 static struct expression
*get_array_expr(struct expression
*expr
)
96 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
99 type
= get_type(expr
->left
);
100 if (!type
|| type
->type
!= SYM_ARRAY
)
105 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
106 struct expression
*expr
, int len
,
109 struct expression
*tmp
;
111 switch (expr
->type
) {
117 __get_variable_from_expr(sym_ptr
, buf
, tmp
, len
, complicated
);
121 append(buf
, "->", len
);
123 append(buf
, ".", len
);
125 append(buf
, expr
->member
->name
, len
);
129 if (expr
->symbol_name
)
130 append(buf
, expr
->symbol_name
->name
, len
);
134 *sym_ptr
= expr
->symbol
;
140 if (get_expression_statement(expr
)) {
145 if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
146 tmp
= show_special(expr
->op
);
147 append(buf
, tmp
, len
);
149 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
153 append(buf
, ")", len
);
155 if (expr
->op
== SPECIAL_DECREMENT
||
156 expr
->op
== SPECIAL_INCREMENT
||
165 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
167 tmp
= show_special(expr
->op
);
168 append(buf
, tmp
, len
);
170 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
174 case EXPR_ASSIGNMENT
:
179 struct expression
*array_expr
;
182 array_expr
= get_array_expr(expr
);
184 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
);
185 append(buf
, "[", len
);
187 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
,
189 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
190 append(buf
, tmp
, len
);
192 __get_variable_from_expr(NULL
, buf
, expr
->right
,
195 append(buf
, "]", len
);
202 snprintf(tmp
, 25, "%lld", expr
->value
);
203 append(buf
, tmp
, len
);
207 append(buf
, "\"", len
);
208 append(buf
, expr
->string
->data
, len
);
209 append(buf
, "\"", len
);
212 struct expression
*tmp
;
216 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
218 append(buf
, "(", len
);
220 FOR_EACH_PTR(expr
->args
, tmp
) {
222 append(buf
, ", ", len
);
223 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
225 } END_FOR_EACH_PTR(tmp
);
226 append(buf
, ")", len
);
230 __get_variable_from_expr(sym_ptr
, buf
,
231 expr
->cast_expression
, len
,
238 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
239 size
= (get_base_type(expr
->cast_type
))->bit_size
;
240 snprintf(tmp
, 25, "%d", bits_to_bytes(size
));
241 append(buf
, tmp
, len
);
247 //printf("unknown type = %d\n", expr->type);
253 * This is returns a stylized "c looking" representation of the
256 * It uses the same buffer every time so you have to save the result
257 * yourself if you want to keep it.
261 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
263 static char var_name
[VAR_LEN
];
272 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
275 return alloc_string(var_name
);
280 char *expr_to_str(struct expression
*expr
)
282 return expr_to_str_sym(expr
, NULL
);
286 * get_variable_from_expr_simple() only returns simple variables.
287 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
288 * then it returns NULL.
290 char *expr_to_var_sym(struct expression
*expr
,
291 struct symbol
**sym_ptr
)
293 static char var_name
[VAR_LEN
];
302 expr
= strip_expr(expr
);
303 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
311 remove_parens(var_name
);
312 return alloc_string(var_name
);
315 char *expr_to_var(struct expression
*expr
)
317 return expr_to_var_sym(expr
, NULL
);
320 int sym_name_is(const char *name
, struct expression
*expr
)
324 if (expr
->type
!= EXPR_SYMBOL
)
326 if (!strcmp(expr
->symbol_name
->name
, name
))
331 int is_zero(struct expression
*expr
)
335 if (get_value(expr
, &sval
) && sval
.value
== 0)
340 int is_array(struct expression
*expr
)
342 expr
= strip_expr(expr
);
343 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
351 struct expression
*get_array_name(struct expression
*expr
)
355 return strip_expr(expr
->unop
->left
);
358 struct expression
*get_array_offset(struct expression
*expr
)
362 return expr
->unop
->right
;
365 const char *show_state(struct smatch_state
*state
)
372 struct statement
*get_expression_statement(struct expression
*expr
)
374 /* What are those things called? if (({....; ret;})) { ...*/
376 if (expr
->type
!= EXPR_PREOP
)
380 if (expr
->unop
->type
!= EXPR_STATEMENT
)
382 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
384 return expr
->unop
->statement
;
387 struct expression
*strip_parens(struct expression
*expr
)
392 if (expr
->type
== EXPR_PREOP
) {
393 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
394 expr
->unop
->statement
->type
== STMT_COMPOUND
)
397 return strip_parens(expr
->unop
);
402 struct expression
*strip_expr(struct expression
*expr
)
407 switch (expr
->type
) {
408 case EXPR_FORCE_CAST
:
410 return strip_expr(expr
->cast_expression
);
412 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
413 expr
->unop
->statement
->type
== STMT_COMPOUND
)
416 return strip_expr(expr
->unop
);
421 static void delete_state_tracker(struct tracker
*t
)
423 delete_state(t
->owner
, t
->name
, t
->sym
);
427 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
431 t
= alloc_tracker(my_id
, name
, sym
);
432 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
435 int is_error_return(struct expression
*expr
)
437 struct symbol
*cur_func
= cur_func_sym
;
442 if (cur_func
->type
!= SYM_NODE
)
444 cur_func
= get_base_type(cur_func
);
445 if (cur_func
->type
!= SYM_FN
)
447 cur_func
= get_base_type(cur_func
);
448 if (cur_func
== &void_ctype
)
450 if (!get_value(expr
, &sval
))
452 if (sval_cmp_val(sval
, 0) < 0)
454 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
459 int getting_address(void)
461 struct expression
*tmp
;
465 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
468 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
470 if (tmp
->op
== '.' && !dot_ops
++)
475 } END_FOR_EACH_PTR_REVERSE(tmp
);
479 char *get_member_name(struct expression
*expr
)
484 expr
= strip_expr(expr
);
485 if (expr
->type
!= EXPR_DEREF
)
487 sym
= get_type(expr
->deref
);
488 if (!sym
|| !sym
->ident
)
490 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
491 return alloc_string(buf
);
494 int positions_eq(struct position pos1
, struct position pos2
)
496 if (pos1
.line
!= pos2
.line
)
498 if (pos1
.pos
!= pos2
.pos
)
500 if (pos1
.stream
!= pos2
.stream
)
505 struct statement
*get_current_statement(void)
507 struct statement
*prev
, *tmp
;
509 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
511 if (!prev
|| !get_macro_name(prev
->pos
))
514 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
515 if (positions_eq(tmp
->pos
, prev
->pos
))
517 if (prev
->pos
.line
> tmp
->pos
.line
)
520 } END_FOR_EACH_PTR_REVERSE(tmp
);
524 int get_param_num_from_sym(struct symbol
*sym
)
533 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
537 } END_FOR_EACH_PTR(tmp
);