2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * Miscellaneous helper functions.
26 #include "smatch_extra.h"
30 char *alloc_string(const char *str
)
36 tmp
= malloc(strlen(str
) + 1);
41 void free_string(char *str
)
46 void remove_parens(char *str
)
51 while (*src
!= '\0') {
52 if (*src
== '(' || *src
== ')') {
61 struct smatch_state
*alloc_state_num(int num
)
63 struct smatch_state
*state
;
64 static char buff
[256];
66 state
= __alloc_smatch_state(0);
67 snprintf(buff
, 255, "%d", num
);
69 state
->name
= alloc_string(buff
);
70 state
->data
= INT_PTR(num
);
74 void append(char *dest
, const char *data
, int buff_len
)
76 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
80 * If you have "foo(a, b, 1);" then use
81 * get_argument_from_call_expr(expr, 0) to return the expression for
82 * a. Yes, it does start counting from 0.
84 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
87 struct expression
*expr
;
93 FOR_EACH_PTR(args
, expr
) {
97 } END_FOR_EACH_PTR(expr
);
101 static struct expression
*get_array_expr(struct expression
*expr
)
105 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
108 type
= get_type(expr
->left
);
109 if (!type
|| type
->type
!= SYM_ARRAY
)
114 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
115 struct expression
*expr
, int len
,
116 int *complicated
, int no_parens
)
118 switch (expr
->type
) {
120 struct expression
*deref
;
126 struct expression
*unop
= strip_expr(deref
->unop
);
128 if (unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
136 __get_variable_from_expr(sym_ptr
, buf
, deref
, len
, complicated
, no_parens
);
139 append(buf
, "->", len
);
141 append(buf
, ".", len
);
144 append(buf
, expr
->member
->name
, len
);
146 append(buf
, "unknown_member", len
);
151 if (expr
->symbol_name
)
152 append(buf
, expr
->symbol_name
->name
, len
);
156 *sym_ptr
= expr
->symbol
;
162 if (get_expression_statement(expr
)) {
167 if (expr
->op
== '(') {
169 append(buf
, "(", len
);
170 } else if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
171 tmp
= show_special(expr
->op
);
172 append(buf
, tmp
, len
);
174 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
175 len
, complicated
, no_parens
);
177 if (expr
->op
== '(' && !no_parens
)
178 append(buf
, ")", len
);
180 if (expr
->op
== SPECIAL_DECREMENT
||
181 expr
->op
== SPECIAL_INCREMENT
)
189 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
190 len
, complicated
, no_parens
);
191 tmp
= show_special(expr
->op
);
192 append(buf
, tmp
, len
);
194 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
198 case EXPR_ASSIGNMENT
:
203 struct expression
*array_expr
;
206 array_expr
= get_array_expr(expr
);
208 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
, no_parens
);
209 append(buf
, "[", len
);
211 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
, complicated
, no_parens
);
212 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
213 append(buf
, tmp
, len
);
215 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
, complicated
, no_parens
);
217 append(buf
, "]", len
);
224 snprintf(tmp
, 25, "%lld", expr
->value
);
225 append(buf
, tmp
, len
);
229 append(buf
, "\"", len
);
231 append(buf
, expr
->string
->data
, len
);
232 append(buf
, "\"", len
);
235 struct expression
*tmp
;
239 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
, complicated
, no_parens
);
240 append(buf
, "(", len
);
242 FOR_EACH_PTR(expr
->args
, tmp
) {
244 append(buf
, ", ", len
);
245 __get_variable_from_expr(NULL
, buf
, tmp
, len
, complicated
, no_parens
);
246 } END_FOR_EACH_PTR(tmp
);
247 append(buf
, ")", len
);
251 __get_variable_from_expr(sym_ptr
, buf
,
252 expr
->cast_expression
, len
,
253 complicated
, no_parens
);
259 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
260 size
= type_bytes(get_base_type(expr
->cast_type
));
261 snprintf(tmp
, 25, "%d", size
);
262 append(buf
, tmp
, len
);
266 case EXPR_IDENTIFIER
:
268 if (expr
->expr_ident
)
269 append(buf
, expr
->expr_ident
->name
, len
);
273 //printf("unknown type = %d\n", expr->type);
279 * This is returns a stylized "c looking" representation of the
282 * It uses the same buffer every time so you have to save the result
283 * yourself if you want to keep it.
287 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
289 static char var_name
[VAR_LEN
];
298 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
301 return alloc_string(var_name
);
306 char *expr_to_str(struct expression
*expr
)
308 return expr_to_str_sym(expr
, NULL
);
312 * get_variable_from_expr_simple() only returns simple variables.
313 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
314 * then it returns NULL.
316 char *expr_to_var_sym(struct expression
*expr
,
317 struct symbol
**sym_ptr
)
319 static char var_name
[VAR_LEN
];
328 expr
= strip_expr(expr
);
329 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
337 return alloc_string(var_name
);
340 char *expr_to_var(struct expression
*expr
)
342 return expr_to_var_sym(expr
, NULL
);
345 int sym_name_is(const char *name
, struct expression
*expr
)
349 if (expr
->type
!= EXPR_SYMBOL
)
351 if (!strcmp(expr
->symbol_name
->name
, name
))
356 int is_zero(struct expression
*expr
)
360 if (get_value(expr
, &sval
) && sval
.value
== 0)
365 int is_array(struct expression
*expr
)
367 expr
= strip_expr(expr
);
368 if (!expr
|| expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
376 struct expression
*get_array_name(struct expression
*expr
)
380 return strip_expr(expr
->unop
->left
);
383 struct expression
*get_array_offset(struct expression
*expr
)
387 return expr
->unop
->right
;
390 const char *show_state(struct smatch_state
*state
)
397 struct statement
*get_expression_statement(struct expression
*expr
)
399 /* What are those things called? if (({....; ret;})) { ...*/
401 if (expr
->type
!= EXPR_PREOP
)
405 if (expr
->unop
->type
!= EXPR_STATEMENT
)
407 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
409 return expr
->unop
->statement
;
412 struct expression
*strip_parens(struct expression
*expr
)
417 if (expr
->type
== EXPR_PREOP
) {
418 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
419 expr
->unop
->statement
->type
== STMT_COMPOUND
)
422 return strip_parens(expr
->unop
);
427 struct expression
*strip_expr(struct expression
*expr
)
432 switch (expr
->type
) {
433 case EXPR_FORCE_CAST
:
435 return strip_expr(expr
->cast_expression
);
437 struct expression
*unop
;
439 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
440 expr
->unop
->statement
->type
== STMT_COMPOUND
)
443 unop
= strip_expr(expr
->unop
);
445 if (expr
->op
== '*' &&
446 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
447 struct symbol
*type
= get_type(unop
->unop
);
449 if (type
&& type
->type
== SYM_ARRAY
)
451 return strip_expr(unop
->unop
);
459 case EXPR_CONDITIONAL
:
460 if (known_condition_true(expr
->conditional
)) {
462 return strip_expr(expr
->cond_true
);
463 return strip_expr(expr
->conditional
);
465 if (known_condition_false(expr
->conditional
))
466 return strip_expr(expr
->cond_false
);
469 if (sym_name_is("__builtin_expect", expr
->fn
)) {
470 expr
= get_argument_from_call_expr(expr
->args
, 0);
471 return strip_expr(expr
);
478 static void delete_state_tracker(struct tracker
*t
)
480 delete_state(t
->owner
, t
->name
, t
->sym
);
484 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
488 t
= alloc_tracker(my_id
, name
, sym
);
489 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
492 int is_error_return(struct expression
*expr
)
494 struct symbol
*cur_func
= cur_func_sym
;
499 if (cur_func
->type
!= SYM_NODE
)
501 cur_func
= get_base_type(cur_func
);
502 if (cur_func
->type
!= SYM_FN
)
504 cur_func
= get_base_type(cur_func
);
505 if (cur_func
== &void_ctype
)
507 if (!get_implied_value(expr
, &sval
))
511 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
516 int getting_address(void)
518 struct expression
*tmp
;
522 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
525 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
527 if (tmp
->op
== '.' && !dot_ops
++)
532 } END_FOR_EACH_PTR_REVERSE(tmp
);
536 char *get_member_name(struct expression
*expr
)
541 expr
= strip_expr(expr
);
542 if (expr
->type
!= EXPR_DEREF
)
544 sym
= get_type(expr
->deref
);
547 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
548 if (sym
->type
== SYM_UNION
) {
551 FOR_EACH_PTR_REVERSE(sym
->symbol_list
, tmp
) {
553 const char *member_name
;
556 member_name
= expr
->member
->name
;
558 member_name
= "unknown_member";
559 snprintf(buf
, sizeof(buf
), "(union hack %s)->%s", tmp
->ident
->name
, member_name
);
560 return alloc_string(buf
);
562 } END_FOR_EACH_PTR_REVERSE(tmp
);
565 if (!sym
->ident
|| !expr
->member
)
567 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
568 return alloc_string(buf
);
571 int cmp_pos(struct position pos1
, struct position pos2
)
573 /* the stream position is ... */
574 if (pos1
.stream
> pos2
.stream
)
576 if (pos1
.stream
< pos2
.stream
)
579 if (pos1
.line
< pos2
.line
)
581 if (pos1
.line
> pos2
.line
)
584 if (pos1
.pos
< pos2
.pos
)
586 if (pos1
.pos
> pos2
.pos
)
592 int positions_eq(struct position pos1
, struct position pos2
)
594 if (pos1
.line
!= pos2
.line
)
596 if (pos1
.pos
!= pos2
.pos
)
598 if (pos1
.stream
!= pos2
.stream
)
603 struct statement
*get_current_statement(void)
605 struct statement
*prev
, *tmp
;
607 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
609 if (!prev
|| !get_macro_name(prev
->pos
))
612 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
613 if (positions_eq(tmp
->pos
, prev
->pos
))
615 if (prev
->pos
.line
> tmp
->pos
.line
)
618 } END_FOR_EACH_PTR_REVERSE(tmp
);
622 struct statement
*get_prev_statement(void)
624 struct statement
*tmp
;
628 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
631 } END_FOR_EACH_PTR_REVERSE(tmp
);
635 int get_param_num_from_sym(struct symbol
*sym
)
644 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
648 } END_FOR_EACH_PTR(tmp
);
652 int get_param_num(struct expression
*expr
)
659 name
= expr_to_var_sym(expr
, &sym
);
663 return get_param_num_from_sym(sym
);
666 int ms_since(struct timeval
*start
)
671 gettimeofday(&end
, NULL
);
672 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
673 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
677 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
682 if (parent_is_null_var_sym(name
, sym
) ||
683 parent_is_free_var_sym(name
, sym
))
688 int parent_is_gone(struct expression
*expr
)
694 expr
= strip_expr(expr
);
695 var
= expr_to_var_sym(expr
, &sym
);
698 ret
= parent_is_gone_var_sym(var
, sym
);