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
= INT_PTR(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
);
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_expression_statement(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
,
138 append(buf
, ")", len
);
140 if (expr
->op
== SPECIAL_DECREMENT
||
141 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
)
163 struct expression
*array_expr
;
166 array_expr
= get_array_expr(expr
);
168 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
);
169 append(buf
, "[", len
);
171 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
,
173 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
174 append(buf
, tmp
, len
);
176 __get_variable_from_expr(NULL
, buf
, expr
->right
,
179 append(buf
, "]", len
);
186 snprintf(tmp
, 25, "%lld", expr
->value
);
187 append(buf
, tmp
, len
);
191 append(buf
, "\"", len
);
192 append(buf
, expr
->string
->data
, len
);
193 append(buf
, "\"", len
);
196 struct expression
*tmp
;
200 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
,
202 append(buf
, "(", len
);
204 FOR_EACH_PTR_REVERSE(expr
->args
, tmp
) {
206 append(buf
, ", ", len
);
207 __get_variable_from_expr(NULL
, buf
, tmp
, len
,
209 } END_FOR_EACH_PTR_REVERSE(tmp
);
210 append(buf
, ")", len
);
214 __get_variable_from_expr(sym_ptr
, buf
,
215 expr
->cast_expression
, len
,
222 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
223 size
= (get_base_type(expr
->cast_type
))->bit_size
;
224 snprintf(tmp
, 25, "%d", bits_to_bytes(size
));
225 append(buf
, tmp
, len
);
231 //printf("unknown type = %d\n", expr->type);
237 * This is returns a stylized "c looking" representation of the
240 * It uses the same buffer every time so you have to save the result
241 * yourself if you want to keep it.
245 char *get_variable_from_expr_complex(struct expression
*expr
, struct symbol
**sym_ptr
)
247 static char var_name
[VAR_LEN
];
256 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
259 return alloc_string(var_name
);
265 * get_variable_from_expr_simple() only returns simple variables.
266 * If it's a complicated variable like a->foo instead of just 'a'
267 * then it returns NULL.
270 char *get_variable_from_expr(struct expression
*expr
,
271 struct symbol
**sym_ptr
)
273 static char var_name
[VAR_LEN
];
282 expr
= strip_expr(expr
);
283 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
291 return alloc_string(var_name
);
294 int sym_name_is(const char *name
, struct expression
*expr
)
298 if (expr
->type
!= EXPR_SYMBOL
)
300 if (!strcmp(expr
->symbol_name
->name
, name
))
305 int is_zero(struct expression
*expr
)
309 if (get_value_sval(expr
, &sval
) && sval
.value
== 0)
314 int is_array(struct expression
*expr
)
316 expr
= strip_expr(expr
);
317 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
325 struct expression
*get_array_name(struct expression
*expr
)
329 return strip_expr(expr
->unop
->left
);
332 struct expression
*get_array_offset(struct expression
*expr
)
336 return expr
->unop
->right
;
339 const char *show_state(struct smatch_state
*state
)
346 struct statement
*get_expression_statement(struct expression
*expr
)
348 /* What are those things called? if (({....; ret;})) { ...*/
350 if (expr
->type
!= EXPR_PREOP
)
354 if (expr
->unop
->type
!= EXPR_STATEMENT
)
356 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
358 return expr
->unop
->statement
;
361 struct expression
*strip_parens(struct expression
*expr
)
366 if (expr
->type
== EXPR_PREOP
) {
367 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
368 expr
->unop
->statement
->type
== STMT_COMPOUND
)
371 return strip_parens(expr
->unop
);
376 struct expression
*strip_expr(struct expression
*expr
)
381 switch (expr
->type
) {
382 case EXPR_FORCE_CAST
:
384 return strip_expr(expr
->cast_expression
);
386 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
387 expr
->unop
->statement
->type
== STMT_COMPOUND
)
390 return strip_expr(expr
->unop
);
395 static void delete_state_tracker(struct tracker
*t
)
397 delete_state(t
->owner
, t
->name
, t
->sym
);
401 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
405 t
= alloc_tracker(my_id
, name
, sym
);
406 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
409 int is_error_return(struct expression
*expr
)
411 struct symbol
*cur_func
= cur_func_sym
;
416 if (cur_func
->type
!= SYM_NODE
)
418 cur_func
= get_base_type(cur_func
);
419 if (cur_func
->type
!= SYM_FN
)
421 cur_func
= get_base_type(cur_func
);
422 if (cur_func
== &void_ctype
)
424 if (!get_value_sval(expr
, &sval
))
426 if (sval_cmp_val(sval
, 0) < 0)
428 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
433 int getting_address(void)
435 struct expression
*tmp
;
439 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
442 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
444 if (tmp
->op
== '.' && !dot_ops
++)
449 } END_FOR_EACH_PTR_REVERSE(tmp
);
453 char *get_member_name(struct expression
*expr
)
458 if (expr
->type
!= EXPR_DEREF
)
460 sym
= get_type(expr
->deref
);
461 if (!sym
|| !sym
->ident
)
463 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
464 return alloc_string(buf
);
467 char *get_fnptr_name(struct expression
*expr
)
469 if (expr
->type
== EXPR_SYMBOL
)
470 return get_variable_from_expr(expr
, NULL
);
471 return get_member_name(expr
);
474 int positions_eq(struct position pos1
, struct position pos2
)
476 if (pos1
.line
!= pos2
.line
)
478 if (pos1
.pos
!= pos2
.pos
)
480 if (pos1
.stream
!= pos2
.stream
)
485 struct statement
*get_current_statement(void)
487 struct statement
*prev
, *tmp
;
489 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
491 if (!prev
|| !get_macro_name(prev
->pos
))
494 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
495 if (positions_eq(tmp
->pos
, prev
->pos
))
497 if (prev
->pos
.line
> tmp
->pos
.line
)
500 } END_FOR_EACH_PTR_REVERSE(tmp
);