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 struct smatch_state
*alloc_state_str(const char *name
)
76 struct smatch_state
*state
;
78 state
= __alloc_smatch_state(0);
79 state
->name
= alloc_string(name
);
83 void append(char *dest
, const char *data
, int buff_len
)
85 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
89 * If you have "foo(a, b, 1);" then use
90 * get_argument_from_call_expr(expr, 0) to return the expression for
91 * a. Yes, it does start counting from 0.
93 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
96 struct expression
*expr
;
102 FOR_EACH_PTR(args
, expr
) {
106 } END_FOR_EACH_PTR(expr
);
110 static struct expression
*get_array_expr(struct expression
*expr
)
114 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
117 type
= get_type(expr
->left
);
118 if (!type
|| type
->type
!= SYM_ARRAY
)
123 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
124 struct expression
*expr
, int len
,
125 int *complicated
, int no_parens
)
127 switch (expr
->type
) {
129 struct expression
*deref
;
135 struct expression
*unop
= strip_expr(deref
->unop
);
137 if (unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
145 __get_variable_from_expr(sym_ptr
, buf
, deref
, len
, complicated
, no_parens
);
148 append(buf
, "->", len
);
150 append(buf
, ".", len
);
153 append(buf
, expr
->member
->name
, len
);
155 append(buf
, "unknown_member", len
);
160 if (expr
->symbol_name
)
161 append(buf
, expr
->symbol_name
->name
, len
);
165 *sym_ptr
= expr
->symbol
;
171 if (get_expression_statement(expr
)) {
176 if (expr
->op
== '(') {
178 append(buf
, "(", len
);
179 } else if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
180 tmp
= show_special(expr
->op
);
181 append(buf
, tmp
, len
);
183 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
184 len
, complicated
, no_parens
);
186 if (expr
->op
== '(' && !no_parens
)
187 append(buf
, ")", len
);
189 if (expr
->op
== SPECIAL_DECREMENT
||
190 expr
->op
== SPECIAL_INCREMENT
)
198 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
199 len
, complicated
, no_parens
);
200 tmp
= show_special(expr
->op
);
201 append(buf
, tmp
, len
);
203 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
207 case EXPR_ASSIGNMENT
:
212 struct expression
*array_expr
;
215 array_expr
= get_array_expr(expr
);
217 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
, no_parens
);
218 append(buf
, "[", len
);
220 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
, complicated
, no_parens
);
221 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
222 append(buf
, tmp
, len
);
224 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
, complicated
, no_parens
);
226 append(buf
, "]", len
);
233 snprintf(tmp
, 25, "%lld", expr
->value
);
234 append(buf
, tmp
, len
);
238 append(buf
, "\"", len
);
240 append(buf
, expr
->string
->data
, len
);
241 append(buf
, "\"", len
);
244 struct expression
*tmp
;
248 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
, complicated
, no_parens
);
249 append(buf
, "(", len
);
251 FOR_EACH_PTR(expr
->args
, tmp
) {
253 append(buf
, ", ", len
);
254 __get_variable_from_expr(NULL
, buf
, tmp
, len
, complicated
, no_parens
);
255 } END_FOR_EACH_PTR(tmp
);
256 append(buf
, ")", len
);
260 case EXPR_FORCE_CAST
:
261 __get_variable_from_expr(sym_ptr
, buf
,
262 expr
->cast_expression
, len
,
263 complicated
, no_parens
);
269 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
270 size
= type_bytes(get_base_type(expr
->cast_type
));
271 snprintf(tmp
, 25, "%d", size
);
272 append(buf
, tmp
, len
);
276 case EXPR_IDENTIFIER
:
278 if (expr
->expr_ident
)
279 append(buf
, expr
->expr_ident
->name
, len
);
283 //printf("unknown type = %d\n", expr->type);
289 * This is returns a stylized "c looking" representation of the
292 * It uses the same buffer every time so you have to save the result
293 * yourself if you want to keep it.
297 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
299 static char var_name
[VAR_LEN
];
308 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
311 return alloc_string(var_name
);
316 char *expr_to_str(struct expression
*expr
)
318 return expr_to_str_sym(expr
, NULL
);
322 * get_variable_from_expr_simple() only returns simple variables.
323 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
324 * then it returns NULL.
326 char *expr_to_var_sym(struct expression
*expr
,
327 struct symbol
**sym_ptr
)
329 static char var_name
[VAR_LEN
];
338 expr
= strip_expr(expr
);
339 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
347 return alloc_string(var_name
);
350 char *expr_to_var(struct expression
*expr
)
352 return expr_to_var_sym(expr
, NULL
);
355 struct symbol
*expr_to_sym(struct expression
*expr
)
360 name
= expr_to_var_sym(expr
, &sym
);
365 int get_complication_score(struct expression
*expr
)
369 expr
= strip_expr(expr
);
372 * Don't forget to keep get_complication_score() and store_all_links()
377 switch (expr
->type
) {
382 score
+= get_complication_score(expr
->left
);
383 score
+= get_complication_score(expr
->right
);
386 if (is_local_variable(expr
))
391 return score
+ get_complication_score(expr
->unop
);
394 return score
+ get_complication_score(expr
->deref
);
402 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
413 expr
= strip_parens(expr
);
417 name
= expr_to_var_sym(expr
, &tmp
);
422 *vsl
= expr_to_vsl(expr
);
427 score
= get_complication_score(expr
);
428 if (score
<= 0 || score
> 2)
432 *vsl
= expr_to_vsl(expr
);
437 return expr_to_str(expr
);
440 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
442 return expr_to_chunk_helper(expr
, sym
, NULL
);
445 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
447 return expr_to_chunk_helper(expr
, sym
, vsl
);
450 int sym_name_is(const char *name
, struct expression
*expr
)
454 if (expr
->type
!= EXPR_SYMBOL
)
456 if (!strcmp(expr
->symbol_name
->name
, name
))
461 int is_zero(struct expression
*expr
)
465 if (get_value(expr
, &sval
) && sval
.value
== 0)
470 int is_array(struct expression
*expr
)
474 expr
= strip_expr(expr
);
478 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
479 expr
= strip_expr(expr
->unop
);
480 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
484 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
487 type
= get_type(expr
->left
);
488 if (!type
|| type
->type
!= SYM_ARRAY
)
494 struct expression
*get_array_base(struct expression
*expr
)
498 expr
= strip_expr(expr
);
499 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
500 expr
= strip_expr(expr
->unop
);
501 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
503 return strip_parens(expr
->left
);
506 struct expression
*get_array_offset(struct expression
*expr
)
510 expr
= strip_expr(expr
);
511 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
512 expr
= strip_expr(expr
->unop
);
513 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
515 return strip_parens(expr
->right
);
518 const char *show_state(struct smatch_state
*state
)
525 struct statement
*get_expression_statement(struct expression
*expr
)
527 /* What are those things called? if (({....; ret;})) { ...*/
529 if (expr
->type
!= EXPR_PREOP
)
533 if (expr
->unop
->type
!= EXPR_STATEMENT
)
535 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
537 return expr
->unop
->statement
;
540 struct expression
*strip_parens(struct expression
*expr
)
545 if (expr
->type
== EXPR_PREOP
) {
546 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
547 expr
->unop
->statement
->type
== STMT_COMPOUND
)
550 return strip_parens(expr
->unop
);
555 struct expression
*strip_expr(struct expression
*expr
)
560 switch (expr
->type
) {
561 case EXPR_FORCE_CAST
:
563 return strip_expr(expr
->cast_expression
);
565 struct expression
*unop
;
567 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
568 expr
->unop
->statement
->type
== STMT_COMPOUND
)
571 unop
= strip_expr(expr
->unop
);
573 if (expr
->op
== '*' &&
574 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
575 struct symbol
*type
= get_type(unop
->unop
);
577 if (type
&& type
->type
== SYM_ARRAY
)
579 return strip_expr(unop
->unop
);
587 case EXPR_CONDITIONAL
:
588 if (known_condition_true(expr
->conditional
)) {
590 return strip_expr(expr
->cond_true
);
591 return strip_expr(expr
->conditional
);
593 if (known_condition_false(expr
->conditional
))
594 return strip_expr(expr
->cond_false
);
597 if (sym_name_is("__builtin_expect", expr
->fn
)) {
598 expr
= get_argument_from_call_expr(expr
->args
, 0);
599 return strip_expr(expr
);
606 static void delete_state_tracker(struct tracker
*t
)
608 delete_state(t
->owner
, t
->name
, t
->sym
);
612 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
616 t
= alloc_tracker(my_id
, name
, sym
);
617 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
620 int is_error_return(struct expression
*expr
)
622 struct symbol
*cur_func
= cur_func_sym
;
627 if (cur_func
->type
!= SYM_NODE
)
629 cur_func
= get_base_type(cur_func
);
630 if (cur_func
->type
!= SYM_FN
)
632 cur_func
= get_base_type(cur_func
);
633 if (cur_func
== &void_ctype
)
635 if (!get_implied_value(expr
, &sval
))
639 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
644 int getting_address(void)
646 struct expression
*tmp
;
650 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
653 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
655 if (tmp
->op
== '.' && !dot_ops
++)
660 } END_FOR_EACH_PTR_REVERSE(tmp
);
664 char *get_member_name(struct expression
*expr
)
669 expr
= strip_expr(expr
);
670 if (expr
->type
!= EXPR_DEREF
)
675 sym
= get_type(expr
->deref
);
678 if (sym
->type
== SYM_UNION
) {
679 sym
= expr_to_sym(expr
->deref
);
680 sym
= get_real_base_type(sym
);
681 if (sym
&& sym
->type
== SYM_PTR
)
682 sym
= get_real_base_type(sym
);
683 if (!sym
|| !sym
->ident
) {
684 snprintf(buf
, sizeof(buf
), "(union hack)->%s", expr
->member
->name
);
685 return alloc_string(buf
);
690 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
691 return alloc_string(buf
);
694 int cmp_pos(struct position pos1
, struct position pos2
)
696 /* the stream position is ... */
697 if (pos1
.stream
> pos2
.stream
)
699 if (pos1
.stream
< pos2
.stream
)
702 if (pos1
.line
< pos2
.line
)
704 if (pos1
.line
> pos2
.line
)
707 if (pos1
.pos
< pos2
.pos
)
709 if (pos1
.pos
> pos2
.pos
)
715 int positions_eq(struct position pos1
, struct position pos2
)
717 if (pos1
.line
!= pos2
.line
)
719 if (pos1
.pos
!= pos2
.pos
)
721 if (pos1
.stream
!= pos2
.stream
)
726 struct statement
*get_current_statement(void)
728 struct statement
*prev
, *tmp
;
730 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
732 if (!prev
|| !get_macro_name(prev
->pos
))
735 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
736 if (positions_eq(tmp
->pos
, prev
->pos
))
738 if (prev
->pos
.line
> tmp
->pos
.line
)
741 } END_FOR_EACH_PTR_REVERSE(tmp
);
745 struct statement
*get_prev_statement(void)
747 struct statement
*tmp
;
751 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
754 } END_FOR_EACH_PTR_REVERSE(tmp
);
758 int get_param_num_from_sym(struct symbol
*sym
)
767 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
771 } END_FOR_EACH_PTR(tmp
);
775 int get_param_num(struct expression
*expr
)
782 name
= expr_to_var_sym(expr
, &sym
);
786 return get_param_num_from_sym(sym
);
789 int ms_since(struct timeval
*start
)
794 gettimeofday(&end
, NULL
);
795 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
796 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
800 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
805 if (parent_is_null_var_sym(name
, sym
) ||
806 parent_is_free_var_sym(name
, sym
))
811 int parent_is_gone(struct expression
*expr
)
817 expr
= strip_expr(expr
);
818 var
= expr_to_var_sym(expr
, &sym
);
821 ret
= parent_is_gone_var_sym(var
, sym
);
827 int invert_op(int op
)
838 case SPECIAL_LEFTSHIFT
:
839 return SPECIAL_RIGHTSHIFT
;
840 case SPECIAL_RIGHTSHIFT
:
841 return SPECIAL_LEFTSHIFT
;
846 int expr_equiv(struct expression
*one
, struct expression
*two
)
848 struct symbol
*one_sym
, *two_sym
;
849 char *one_name
= NULL
;
850 char *two_name
= NULL
;
855 if (one
->type
!= two
->type
)
858 one_name
= expr_to_str_sym(one
, &one_sym
);
859 if (!one_name
|| !one_sym
)
861 two_name
= expr_to_str_sym(two
, &two_sym
);
862 if (!two_name
|| !two_sym
)
864 if (one_sym
!= two_sym
)
866 if (strcmp(one_name
, two_name
) == 0)
869 free_string(one_name
);
870 free_string(two_name
);