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 struct symbol
*expr_to_sym(struct expression
*expr
)
350 name
= expr_to_var_sym(expr
, &sym
);
355 int sym_name_is(const char *name
, struct expression
*expr
)
359 if (expr
->type
!= EXPR_SYMBOL
)
361 if (!strcmp(expr
->symbol_name
->name
, name
))
366 int is_zero(struct expression
*expr
)
370 if (get_value(expr
, &sval
) && sval
.value
== 0)
375 int is_array(struct expression
*expr
)
379 expr
= strip_expr(expr
);
383 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
384 expr
= strip_expr(expr
->unop
);
385 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
389 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
392 type
= get_type(expr
->left
);
393 if (!type
|| type
->type
!= SYM_ARRAY
)
399 struct expression
*get_array_base(struct expression
*expr
)
403 expr
= strip_expr(expr
);
404 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
405 expr
= strip_expr(expr
->unop
);
406 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
408 return strip_parens(expr
->left
);
411 struct expression
*get_array_offset(struct expression
*expr
)
415 expr
= strip_expr(expr
);
416 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
417 expr
= strip_expr(expr
->unop
);
418 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
420 return strip_expr(expr
->right
);
423 const char *show_state(struct smatch_state
*state
)
430 struct statement
*get_expression_statement(struct expression
*expr
)
432 /* What are those things called? if (({....; ret;})) { ...*/
434 if (expr
->type
!= EXPR_PREOP
)
438 if (expr
->unop
->type
!= EXPR_STATEMENT
)
440 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
442 return expr
->unop
->statement
;
445 struct expression
*strip_parens(struct expression
*expr
)
450 if (expr
->type
== EXPR_PREOP
) {
451 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
452 expr
->unop
->statement
->type
== STMT_COMPOUND
)
455 return strip_parens(expr
->unop
);
460 struct expression
*strip_expr(struct expression
*expr
)
465 switch (expr
->type
) {
466 case EXPR_FORCE_CAST
:
468 return strip_expr(expr
->cast_expression
);
470 struct expression
*unop
;
472 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
473 expr
->unop
->statement
->type
== STMT_COMPOUND
)
476 unop
= strip_expr(expr
->unop
);
478 if (expr
->op
== '*' &&
479 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
480 struct symbol
*type
= get_type(unop
->unop
);
482 if (type
&& type
->type
== SYM_ARRAY
)
484 return strip_expr(unop
->unop
);
492 case EXPR_CONDITIONAL
:
493 if (known_condition_true(expr
->conditional
)) {
495 return strip_expr(expr
->cond_true
);
496 return strip_expr(expr
->conditional
);
498 if (known_condition_false(expr
->conditional
))
499 return strip_expr(expr
->cond_false
);
502 if (sym_name_is("__builtin_expect", expr
->fn
)) {
503 expr
= get_argument_from_call_expr(expr
->args
, 0);
504 return strip_expr(expr
);
511 static void delete_state_tracker(struct tracker
*t
)
513 delete_state(t
->owner
, t
->name
, t
->sym
);
517 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
521 t
= alloc_tracker(my_id
, name
, sym
);
522 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
525 int is_error_return(struct expression
*expr
)
527 struct symbol
*cur_func
= cur_func_sym
;
532 if (cur_func
->type
!= SYM_NODE
)
534 cur_func
= get_base_type(cur_func
);
535 if (cur_func
->type
!= SYM_FN
)
537 cur_func
= get_base_type(cur_func
);
538 if (cur_func
== &void_ctype
)
540 if (!get_implied_value(expr
, &sval
))
544 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
549 int getting_address(void)
551 struct expression
*tmp
;
555 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
558 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
560 if (tmp
->op
== '.' && !dot_ops
++)
565 } END_FOR_EACH_PTR_REVERSE(tmp
);
569 char *get_member_name(struct expression
*expr
)
574 expr
= strip_expr(expr
);
575 if (expr
->type
!= EXPR_DEREF
)
580 sym
= get_type(expr
->deref
);
583 if (sym
->type
== SYM_UNION
) {
584 sym
= expr_to_sym(expr
->deref
);
585 sym
= get_real_base_type(sym
);
586 if (sym
&& sym
->type
== SYM_PTR
)
587 sym
= get_real_base_type(sym
);
588 if (!sym
|| !sym
->ident
) {
589 snprintf(buf
, sizeof(buf
), "(union hack)->%s", expr
->member
->name
);
590 return alloc_string(buf
);
595 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
596 return alloc_string(buf
);
599 int cmp_pos(struct position pos1
, struct position pos2
)
601 /* the stream position is ... */
602 if (pos1
.stream
> pos2
.stream
)
604 if (pos1
.stream
< pos2
.stream
)
607 if (pos1
.line
< pos2
.line
)
609 if (pos1
.line
> pos2
.line
)
612 if (pos1
.pos
< pos2
.pos
)
614 if (pos1
.pos
> pos2
.pos
)
620 int positions_eq(struct position pos1
, struct position pos2
)
622 if (pos1
.line
!= pos2
.line
)
624 if (pos1
.pos
!= pos2
.pos
)
626 if (pos1
.stream
!= pos2
.stream
)
631 struct statement
*get_current_statement(void)
633 struct statement
*prev
, *tmp
;
635 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
637 if (!prev
|| !get_macro_name(prev
->pos
))
640 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
641 if (positions_eq(tmp
->pos
, prev
->pos
))
643 if (prev
->pos
.line
> tmp
->pos
.line
)
646 } END_FOR_EACH_PTR_REVERSE(tmp
);
650 struct statement
*get_prev_statement(void)
652 struct statement
*tmp
;
656 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
659 } END_FOR_EACH_PTR_REVERSE(tmp
);
663 int get_param_num_from_sym(struct symbol
*sym
)
672 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
676 } END_FOR_EACH_PTR(tmp
);
680 int get_param_num(struct expression
*expr
)
687 name
= expr_to_var_sym(expr
, &sym
);
691 return get_param_num_from_sym(sym
);
694 int ms_since(struct timeval
*start
)
699 gettimeofday(&end
, NULL
);
700 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
701 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
705 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
710 if (parent_is_null_var_sym(name
, sym
) ||
711 parent_is_free_var_sym(name
, sym
))
716 int parent_is_gone(struct expression
*expr
)
722 expr
= strip_expr(expr
);
723 var
= expr_to_var_sym(expr
, &sym
);
726 ret
= parent_is_gone_var_sym(var
, sym
);
732 int invert_op(int op
)
743 case SPECIAL_LEFTSHIFT
:
744 return SPECIAL_RIGHTSHIFT
;
745 case SPECIAL_RIGHTSHIFT
:
746 return SPECIAL_LEFTSHIFT
;