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 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
,
107 int *complicated
, int no_parens
)
109 struct expression
*tmp
;
111 switch (expr
->type
) {
117 __get_variable_from_expr(sym_ptr
, buf
, tmp
, len
, complicated
, no_parens
);
121 append(buf
, "->", len
);
123 append(buf
, ".", len
);
125 if (expr
->member
&& expr
->member
->name
)
126 append(buf
, expr
->member
->name
, len
);
128 append(buf
, "unknown_member", len
);
132 if (expr
->symbol_name
)
133 append(buf
, expr
->symbol_name
->name
, len
);
137 *sym_ptr
= expr
->symbol
;
143 if (get_expression_statement(expr
)) {
148 if (expr
->op
== '(') {
150 append(buf
, "(", len
);
151 } else if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
152 tmp
= show_special(expr
->op
);
153 append(buf
, tmp
, len
);
155 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
156 len
, complicated
, no_parens
);
158 if (expr
->op
== '(' && !no_parens
)
159 append(buf
, ")", len
);
161 if (expr
->op
== SPECIAL_DECREMENT
||
162 expr
->op
== SPECIAL_INCREMENT
||
171 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
172 len
, complicated
, no_parens
);
173 tmp
= show_special(expr
->op
);
174 append(buf
, tmp
, len
);
176 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
180 case EXPR_ASSIGNMENT
:
185 struct expression
*array_expr
;
188 array_expr
= get_array_expr(expr
);
190 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
, no_parens
);
191 append(buf
, "[", len
);
193 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
, complicated
, no_parens
);
194 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
195 append(buf
, tmp
, len
);
197 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
, complicated
, no_parens
);
199 append(buf
, "]", len
);
206 snprintf(tmp
, 25, "%lld", expr
->value
);
207 append(buf
, tmp
, len
);
211 append(buf
, "\"", len
);
212 append(buf
, expr
->string
->data
, len
);
213 append(buf
, "\"", len
);
216 struct expression
*tmp
;
220 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
, complicated
, no_parens
);
221 append(buf
, "(", len
);
223 FOR_EACH_PTR(expr
->args
, tmp
) {
225 append(buf
, ", ", len
);
226 __get_variable_from_expr(NULL
, buf
, tmp
, len
, complicated
, no_parens
);
227 } END_FOR_EACH_PTR(tmp
);
228 append(buf
, ")", len
);
232 __get_variable_from_expr(sym_ptr
, buf
,
233 expr
->cast_expression
, len
,
234 complicated
, no_parens
);
240 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
241 size
= (get_base_type(expr
->cast_type
))->bit_size
;
242 snprintf(tmp
, 25, "%d", bits_to_bytes(size
));
243 append(buf
, tmp
, len
);
247 case EXPR_IDENTIFIER
:
249 if (expr
->expr_ident
&& expr
->expr_ident
->name
)
250 append(buf
, expr
->expr_ident
->name
, len
);
254 //printf("unknown type = %d\n", expr->type);
260 * This is returns a stylized "c looking" representation of the
263 * It uses the same buffer every time so you have to save the result
264 * yourself if you want to keep it.
268 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
270 static char var_name
[VAR_LEN
];
279 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
282 return alloc_string(var_name
);
287 char *expr_to_str(struct expression
*expr
)
289 return expr_to_str_sym(expr
, NULL
);
293 * get_variable_from_expr_simple() only returns simple variables.
294 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
295 * then it returns NULL.
297 char *expr_to_var_sym(struct expression
*expr
,
298 struct symbol
**sym_ptr
)
300 static char var_name
[VAR_LEN
];
309 expr
= strip_expr(expr
);
310 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
318 return alloc_string(var_name
);
321 char *expr_to_var(struct expression
*expr
)
323 return expr_to_var_sym(expr
, NULL
);
326 int sym_name_is(const char *name
, struct expression
*expr
)
330 if (expr
->type
!= EXPR_SYMBOL
)
332 if (!strcmp(expr
->symbol_name
->name
, name
))
337 int is_zero(struct expression
*expr
)
341 if (get_value(expr
, &sval
) && sval
.value
== 0)
346 int is_array(struct expression
*expr
)
348 expr
= strip_expr(expr
);
349 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
357 struct expression
*get_array_name(struct expression
*expr
)
361 return strip_expr(expr
->unop
->left
);
364 struct expression
*get_array_offset(struct expression
*expr
)
368 return expr
->unop
->right
;
371 const char *show_state(struct smatch_state
*state
)
378 struct statement
*get_expression_statement(struct expression
*expr
)
380 /* What are those things called? if (({....; ret;})) { ...*/
382 if (expr
->type
!= EXPR_PREOP
)
386 if (expr
->unop
->type
!= EXPR_STATEMENT
)
388 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
390 return expr
->unop
->statement
;
393 struct expression
*strip_parens(struct expression
*expr
)
398 if (expr
->type
== EXPR_PREOP
) {
399 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
400 expr
->unop
->statement
->type
== STMT_COMPOUND
)
403 return strip_parens(expr
->unop
);
408 struct expression
*strip_expr(struct expression
*expr
)
413 switch (expr
->type
) {
414 case EXPR_FORCE_CAST
:
416 return strip_expr(expr
->cast_expression
);
418 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
419 expr
->unop
->statement
->type
== STMT_COMPOUND
)
422 return strip_expr(expr
->unop
);
427 static void delete_state_tracker(struct tracker
*t
)
429 delete_state(t
->owner
, t
->name
, t
->sym
);
433 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
437 t
= alloc_tracker(my_id
, name
, sym
);
438 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
441 int is_error_return(struct expression
*expr
)
443 struct symbol
*cur_func
= cur_func_sym
;
448 if (cur_func
->type
!= SYM_NODE
)
450 cur_func
= get_base_type(cur_func
);
451 if (cur_func
->type
!= SYM_FN
)
453 cur_func
= get_base_type(cur_func
);
454 if (cur_func
== &void_ctype
)
456 if (!get_implied_value(expr
, &sval
))
460 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
465 int getting_address(void)
467 struct expression
*tmp
;
471 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
474 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
476 if (tmp
->op
== '.' && !dot_ops
++)
481 } END_FOR_EACH_PTR_REVERSE(tmp
);
485 char *get_member_name(struct expression
*expr
)
490 expr
= strip_expr(expr
);
491 if (expr
->type
!= EXPR_DEREF
)
493 sym
= get_type(expr
->deref
);
496 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
497 if (sym
->type
== SYM_UNION
) {
500 FOR_EACH_PTR_REVERSE(sym
->symbol_list
, tmp
) {
502 const char *member_name
;
505 member_name
= expr
->member
->name
;
507 member_name
= "unknown_member";
508 snprintf(buf
, sizeof(buf
), "(union hack %s)->%s", tmp
->ident
->name
, member_name
);
509 return alloc_string(buf
);
511 } END_FOR_EACH_PTR_REVERSE(tmp
);
516 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
517 return alloc_string(buf
);
520 int positions_eq(struct position pos1
, struct position pos2
)
522 if (pos1
.line
!= pos2
.line
)
524 if (pos1
.pos
!= pos2
.pos
)
526 if (pos1
.stream
!= pos2
.stream
)
531 struct statement
*get_current_statement(void)
533 struct statement
*prev
, *tmp
;
535 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
537 if (!prev
|| !get_macro_name(prev
->pos
))
540 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
541 if (positions_eq(tmp
->pos
, prev
->pos
))
543 if (prev
->pos
.line
> tmp
->pos
.line
)
546 } END_FOR_EACH_PTR_REVERSE(tmp
);
550 int get_param_num_from_sym(struct symbol
*sym
)
559 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
563 } END_FOR_EACH_PTR(tmp
);
567 int ms_since(struct timeval
*start
)
572 gettimeofday(&end
, NULL
);
573 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
574 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;