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"
27 #include "smatch_slist.h"
31 char *alloc_string(const char *str
)
37 tmp
= malloc(strlen(str
) + 1);
42 void free_string(char *str
)
47 void remove_parens(char *str
)
52 while (*src
!= '\0') {
53 if (*src
== '(' || *src
== ')') {
62 struct smatch_state
*alloc_state_num(int num
)
64 struct smatch_state
*state
;
65 static char buff
[256];
67 state
= __alloc_smatch_state(0);
68 snprintf(buff
, 255, "%d", num
);
70 state
->name
= alloc_string(buff
);
71 state
->data
= INT_PTR(num
);
75 struct smatch_state
*alloc_state_str(const char *name
)
77 struct smatch_state
*state
;
79 state
= __alloc_smatch_state(0);
80 state
->name
= alloc_string(name
);
84 struct smatch_state
*alloc_state_expr(struct expression
*expr
)
86 struct smatch_state
*state
;
89 state
= __alloc_smatch_state(0);
90 expr
= strip_expr(expr
);
91 name
= expr_to_str(expr
);
92 state
->name
= alloc_sname(name
);
98 void append(char *dest
, const char *data
, int buff_len
)
100 strncat(dest
, data
, buff_len
- strlen(dest
) - 1);
104 * If you have "foo(a, b, 1);" then use
105 * get_argument_from_call_expr(expr, 0) to return the expression for
106 * a. Yes, it does start counting from 0.
108 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
111 struct expression
*expr
;
117 FOR_EACH_PTR(args
, expr
) {
121 } END_FOR_EACH_PTR(expr
);
125 static struct expression
*get_array_expr(struct expression
*expr
)
129 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
132 type
= get_type(expr
->left
);
133 if (!type
|| type
->type
!= SYM_ARRAY
)
138 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
139 struct expression
*expr
, int len
,
140 int *complicated
, int no_parens
)
142 switch (expr
->type
) {
144 struct expression
*deref
;
150 struct expression
*unop
= strip_expr(deref
->unop
);
152 if (unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
157 if (!is_pointer(deref
))
162 __get_variable_from_expr(sym_ptr
, buf
, deref
, len
, complicated
, no_parens
);
165 append(buf
, "->", len
);
167 append(buf
, ".", len
);
170 append(buf
, expr
->member
->name
, len
);
172 append(buf
, "unknown_member", len
);
177 if (expr
->symbol_name
)
178 append(buf
, expr
->symbol_name
->name
, len
);
182 *sym_ptr
= expr
->symbol
;
188 if (get_expression_statement(expr
)) {
193 if (expr
->op
== '(') {
194 if (!no_parens
&& expr
->unop
->type
!= EXPR_SYMBOL
)
195 append(buf
, "(", len
);
196 } else if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
197 tmp
= show_special(expr
->op
);
198 append(buf
, tmp
, len
);
200 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
201 len
, complicated
, no_parens
);
203 if (expr
->op
== '(' && !no_parens
&& expr
->unop
->type
!= EXPR_SYMBOL
)
204 append(buf
, ")", len
);
206 if (expr
->op
== SPECIAL_DECREMENT
||
207 expr
->op
== SPECIAL_INCREMENT
)
215 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
216 len
, complicated
, no_parens
);
217 tmp
= show_special(expr
->op
);
218 append(buf
, tmp
, len
);
220 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
224 case EXPR_ASSIGNMENT
:
229 struct expression
*array_expr
;
232 array_expr
= get_array_expr(expr
);
234 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
, no_parens
);
235 append(buf
, "[", len
);
237 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
, complicated
, no_parens
);
238 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
239 append(buf
, tmp
, len
);
241 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
, complicated
, no_parens
);
243 append(buf
, "]", len
);
250 snprintf(tmp
, 25, "%lld", expr
->value
);
251 append(buf
, tmp
, len
);
255 append(buf
, "\"", len
);
257 append(buf
, expr
->string
->data
, len
);
258 append(buf
, "\"", len
);
261 struct expression
*tmp
;
265 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
, complicated
, no_parens
);
266 append(buf
, "(", len
);
268 FOR_EACH_PTR(expr
->args
, tmp
) {
270 append(buf
, ", ", len
);
271 __get_variable_from_expr(NULL
, buf
, tmp
, len
, complicated
, no_parens
);
272 } END_FOR_EACH_PTR(tmp
);
273 append(buf
, ")", len
);
277 case EXPR_FORCE_CAST
:
278 __get_variable_from_expr(sym_ptr
, buf
,
279 expr
->cast_expression
, len
,
280 complicated
, no_parens
);
286 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
287 size
= type_bytes(get_base_type(expr
->cast_type
));
288 snprintf(tmp
, 25, "%d", size
);
289 append(buf
, tmp
, len
);
293 case EXPR_IDENTIFIER
:
295 if (expr
->expr_ident
)
296 append(buf
, expr
->expr_ident
->name
, len
);
300 //printf("unknown type = %d\n", expr->type);
306 * This is returns a stylized "c looking" representation of the
309 * It uses the same buffer every time so you have to save the result
310 * yourself if you want to keep it.
314 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
316 static char var_name
[VAR_LEN
];
325 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
328 return alloc_string(var_name
);
333 char *expr_to_str(struct expression
*expr
)
335 return expr_to_str_sym(expr
, NULL
);
339 * get_variable_from_expr_simple() only returns simple variables.
340 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
341 * then it returns NULL.
343 char *expr_to_var_sym(struct expression
*expr
,
344 struct symbol
**sym_ptr
)
346 static char var_name
[VAR_LEN
];
355 expr
= strip_expr(expr
);
356 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
364 return alloc_string(var_name
);
367 char *expr_to_var(struct expression
*expr
)
369 return expr_to_var_sym(expr
, NULL
);
372 struct symbol
*expr_to_sym(struct expression
*expr
)
377 name
= expr_to_var_sym(expr
, &sym
);
382 int get_complication_score(struct expression
*expr
)
386 expr
= strip_expr(expr
);
389 * Don't forget to keep get_complication_score() and store_all_links()
394 switch (expr
->type
) {
399 score
+= get_complication_score(expr
->left
);
400 score
+= get_complication_score(expr
->right
);
403 if (is_local_variable(expr
))
408 return score
+ get_complication_score(expr
->unop
);
411 return score
+ get_complication_score(expr
->deref
);
419 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
430 expr
= strip_parens(expr
);
434 name
= expr_to_var_sym(expr
, &tmp
);
439 *vsl
= expr_to_vsl(expr
);
444 score
= get_complication_score(expr
);
445 if (score
<= 0 || score
> 2)
449 *vsl
= expr_to_vsl(expr
);
454 return expr_to_str(expr
);
457 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
459 return expr_to_chunk_helper(expr
, sym
, NULL
);
462 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
464 return expr_to_chunk_helper(expr
, sym
, vsl
);
467 int sym_name_is(const char *name
, struct expression
*expr
)
471 if (expr
->type
!= EXPR_SYMBOL
)
473 if (!strcmp(expr
->symbol_name
->name
, name
))
478 int is_zero(struct expression
*expr
)
482 if (get_value(expr
, &sval
) && sval
.value
== 0)
487 int is_array(struct expression
*expr
)
491 expr
= strip_expr(expr
);
495 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
496 expr
= strip_expr(expr
->unop
);
497 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
501 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
504 type
= get_type(expr
->left
);
505 if (!type
|| type
->type
!= SYM_ARRAY
)
511 struct expression
*get_array_base(struct expression
*expr
)
515 expr
= strip_expr(expr
);
516 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
517 expr
= strip_expr(expr
->unop
);
518 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
520 return strip_parens(expr
->left
);
523 struct expression
*get_array_offset(struct expression
*expr
)
527 expr
= strip_expr(expr
);
528 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
529 expr
= strip_expr(expr
->unop
);
530 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
532 return strip_parens(expr
->right
);
535 const char *show_state(struct smatch_state
*state
)
542 struct statement
*get_expression_statement(struct expression
*expr
)
544 /* What are those things called? if (({....; ret;})) { ...*/
546 if (expr
->type
!= EXPR_PREOP
)
550 if (expr
->unop
->type
!= EXPR_STATEMENT
)
552 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
554 return expr
->unop
->statement
;
557 struct expression
*strip_parens(struct expression
*expr
)
562 if (expr
->type
== EXPR_PREOP
) {
563 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
564 expr
->unop
->statement
->type
== STMT_COMPOUND
)
567 return strip_parens(expr
->unop
);
572 struct expression
*strip_expr(struct expression
*expr
)
577 switch (expr
->type
) {
578 case EXPR_FORCE_CAST
:
580 return strip_expr(expr
->cast_expression
);
582 struct expression
*unop
;
584 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
585 expr
->unop
->statement
->type
== STMT_COMPOUND
)
588 unop
= strip_expr(expr
->unop
);
590 if (expr
->op
== '*' &&
591 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
592 struct symbol
*type
= get_type(unop
->unop
);
594 if (type
&& type
->type
== SYM_ARRAY
)
596 return strip_expr(unop
->unop
);
604 case EXPR_CONDITIONAL
:
605 if (known_condition_true(expr
->conditional
)) {
607 return strip_expr(expr
->cond_true
);
608 return strip_expr(expr
->conditional
);
610 if (known_condition_false(expr
->conditional
))
611 return strip_expr(expr
->cond_false
);
614 if (sym_name_is("__builtin_expect", expr
->fn
)) {
615 expr
= get_argument_from_call_expr(expr
->args
, 0);
616 return strip_expr(expr
);
623 static void delete_state_tracker(struct tracker
*t
)
625 delete_state(t
->owner
, t
->name
, t
->sym
);
629 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
633 t
= alloc_tracker(my_id
, name
, sym
);
634 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
637 int is_error_return(struct expression
*expr
)
639 struct symbol
*cur_func
= cur_func_sym
;
644 if (cur_func
->type
!= SYM_NODE
)
646 cur_func
= get_base_type(cur_func
);
647 if (cur_func
->type
!= SYM_FN
)
649 cur_func
= get_base_type(cur_func
);
650 if (cur_func
== &void_ctype
)
652 if (!get_implied_value(expr
, &sval
))
656 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
661 int getting_address(void)
663 struct expression
*tmp
;
667 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
670 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
672 if (tmp
->op
== '.' && !dot_ops
++)
677 } END_FOR_EACH_PTR_REVERSE(tmp
);
681 char *get_member_name(struct expression
*expr
)
686 expr
= strip_expr(expr
);
687 if (expr
->type
!= EXPR_DEREF
)
692 sym
= get_type(expr
->deref
);
695 if (sym
->type
== SYM_UNION
) {
696 sym
= expr_to_sym(expr
->deref
);
697 sym
= get_real_base_type(sym
);
698 if (sym
&& sym
->type
== SYM_PTR
)
699 sym
= get_real_base_type(sym
);
700 if (!sym
|| !sym
->ident
) {
701 snprintf(buf
, sizeof(buf
), "(union hack)->%s", expr
->member
->name
);
702 return alloc_string(buf
);
707 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
708 return alloc_string(buf
);
711 int cmp_pos(struct position pos1
, struct position pos2
)
713 /* the stream position is ... */
714 if (pos1
.stream
> pos2
.stream
)
716 if (pos1
.stream
< pos2
.stream
)
719 if (pos1
.line
< pos2
.line
)
721 if (pos1
.line
> pos2
.line
)
724 if (pos1
.pos
< pos2
.pos
)
726 if (pos1
.pos
> pos2
.pos
)
732 int positions_eq(struct position pos1
, struct position pos2
)
734 if (pos1
.line
!= pos2
.line
)
736 if (pos1
.pos
!= pos2
.pos
)
738 if (pos1
.stream
!= pos2
.stream
)
743 struct statement
*get_current_statement(void)
745 struct statement
*prev
, *tmp
;
747 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
749 if (!prev
|| !get_macro_name(prev
->pos
))
752 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
753 if (positions_eq(tmp
->pos
, prev
->pos
))
755 if (prev
->pos
.line
> tmp
->pos
.line
)
758 } END_FOR_EACH_PTR_REVERSE(tmp
);
762 struct statement
*get_prev_statement(void)
764 struct statement
*tmp
;
768 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
771 } END_FOR_EACH_PTR_REVERSE(tmp
);
775 int get_param_num_from_sym(struct symbol
*sym
)
784 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
788 } END_FOR_EACH_PTR(tmp
);
792 int get_param_num(struct expression
*expr
)
799 name
= expr_to_var_sym(expr
, &sym
);
803 return get_param_num_from_sym(sym
);
806 int ms_since(struct timeval
*start
)
811 gettimeofday(&end
, NULL
);
812 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
813 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
817 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
822 if (parent_is_null_var_sym(name
, sym
) ||
823 parent_is_free_var_sym(name
, sym
))
828 int parent_is_gone(struct expression
*expr
)
834 expr
= strip_expr(expr
);
835 var
= expr_to_var_sym(expr
, &sym
);
838 ret
= parent_is_gone_var_sym(var
, sym
);
844 int invert_op(int op
)
855 case SPECIAL_LEFTSHIFT
:
856 return SPECIAL_RIGHTSHIFT
;
857 case SPECIAL_RIGHTSHIFT
:
858 return SPECIAL_LEFTSHIFT
;
863 int expr_equiv(struct expression
*one
, struct expression
*two
)
865 struct symbol
*one_sym
, *two_sym
;
866 char *one_name
= NULL
;
867 char *two_name
= NULL
;
872 if (one
->type
!= two
->type
)
875 one_name
= expr_to_str_sym(one
, &one_sym
);
876 if (!one_name
|| !one_sym
)
878 two_name
= expr_to_str_sym(two
, &two_sym
);
879 if (!two_name
|| !two_sym
)
881 if (one_sym
!= two_sym
)
883 if (strcmp(one_name
, two_name
) == 0)
886 free_string(one_name
);
887 free_string(two_name
);
891 void push_int(struct int_stack
**stack
, int num
)
896 * Just put the int on directly instead of a pointer to the int.
897 * Shift it to the left because Sparse uses the last two bits.
898 * This is sort of a dirty hack, yes.
901 munged
= INT_PTR(num
<< 2);
903 add_ptr_list(stack
, munged
);
906 int pop_int(struct int_stack
**stack
)
910 num
= last_ptr_list((struct ptr_list
*)*stack
);
911 delete_ptr_list_last((struct ptr_list
**)stack
);
913 return PTR_INT(num
) >> 2;