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
)
127 struct expression
*parent
;
130 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
133 type
= get_type(expr
->left
);
136 if (type
->type
== SYM_ARRAY
)
138 if (type
->type
!= SYM_PTR
)
141 parent
= expr_get_parent_expr(expr
);
142 if (!parent
) /* Sometimes we haven't set up the ->parent yet. FIXME!! */
144 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
150 static void __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
151 struct expression
*expr
, int len
,
152 int *complicated
, int no_parens
)
157 /* can't happen on valid code */
162 switch (expr
->type
) {
164 struct expression
*deref
;
170 struct expression
*unop
= strip_expr(deref
->unop
);
172 if (unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
177 if (!is_pointer(deref
))
182 __get_variable_from_expr(sym_ptr
, buf
, deref
, len
, complicated
, no_parens
);
185 append(buf
, "->", len
);
187 append(buf
, ".", len
);
190 append(buf
, expr
->member
->name
, len
);
192 append(buf
, "unknown_member", len
);
197 if (expr
->symbol_name
)
198 append(buf
, expr
->symbol_name
->name
, len
);
202 *sym_ptr
= expr
->symbol
;
208 if (get_expression_statement(expr
)) {
213 if (expr
->op
== '(') {
214 if (!no_parens
&& expr
->unop
->type
!= EXPR_SYMBOL
)
215 append(buf
, "(", len
);
216 } else if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
217 tmp
= show_special(expr
->op
);
218 append(buf
, tmp
, len
);
220 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
221 len
, complicated
, no_parens
);
223 if (expr
->op
== '(' && !no_parens
&& expr
->unop
->type
!= EXPR_SYMBOL
)
224 append(buf
, ")", len
);
226 if (expr
->op
== SPECIAL_DECREMENT
||
227 expr
->op
== SPECIAL_INCREMENT
)
235 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
236 len
, complicated
, no_parens
);
237 tmp
= show_special(expr
->op
);
238 append(buf
, tmp
, len
);
240 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
244 case EXPR_ASSIGNMENT
:
249 struct expression
*array_expr
;
252 array_expr
= get_array_expr(expr
);
254 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
, no_parens
);
255 append(buf
, "[", len
);
257 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
, complicated
, no_parens
);
258 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
259 append(buf
, tmp
, len
);
261 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
, complicated
, no_parens
);
263 append(buf
, "]", len
);
270 snprintf(tmp
, 25, "%lld", expr
->value
);
271 append(buf
, tmp
, len
);
275 append(buf
, "\"", len
);
277 append(buf
, expr
->string
->data
, len
);
278 append(buf
, "\"", len
);
281 struct expression
*tmp
;
285 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
, complicated
, no_parens
);
286 append(buf
, "(", len
);
288 FOR_EACH_PTR(expr
->args
, tmp
) {
290 append(buf
, ", ", len
);
291 __get_variable_from_expr(NULL
, buf
, tmp
, len
, complicated
, no_parens
);
292 } END_FOR_EACH_PTR(tmp
);
293 append(buf
, ")", len
);
297 case EXPR_FORCE_CAST
:
298 __get_variable_from_expr(sym_ptr
, buf
,
299 expr
->cast_expression
, len
,
300 complicated
, no_parens
);
307 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
308 size
= type_bytes(get_base_type(expr
->cast_type
));
309 snprintf(tmp
, 25, "%d", size
);
310 append(buf
, tmp
, len
);
311 } else if (get_value(expr
, &sval
)) {
312 snprintf(tmp
, 25, "%s", sval_to_str(sval
));
313 append(buf
, tmp
, len
);
317 case EXPR_IDENTIFIER
:
319 if (expr
->expr_ident
)
320 append(buf
, expr
->expr_ident
->name
, len
);
324 //printf("unknown type = %d\n", expr->type);
329 struct expr_str_cache_results
{
330 struct expression
*expr
;
337 static void get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
338 struct expression
*expr
, int len
,
339 int *complicated
, int no_parens
)
341 static struct expr_str_cache_results cached
[8];
342 struct symbol
*tmp_sym
= NULL
;
346 for (i
= 0; i
< ARRAY_SIZE(cached
); i
++) {
347 if (expr
== cached
[i
].expr
&&
348 no_parens
== cached
[i
].no_parens
) {
349 strncpy(buf
, cached
[i
].str
, len
);
351 *sym_ptr
= cached
[i
].sym
;
352 *complicated
= cached
[i
].complicated
;
357 __get_variable_from_expr(&tmp_sym
, buf
, expr
, len
, complicated
, no_parens
);
361 cached
[idx
].expr
= expr
;
362 cached
[idx
].no_parens
= no_parens
;
363 strncpy(cached
[idx
].str
, buf
, VAR_LEN
);
364 cached
[idx
].sym
= tmp_sym
;
365 cached
[idx
].complicated
= *complicated
;
367 idx
= (idx
+ 1) % ARRAY_SIZE(cached
);
371 * This is returns a stylized "c looking" representation of the
374 * It uses the same buffer every time so you have to save the result
375 * yourself if you want to keep it.
379 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
381 static char var_name
[VAR_LEN
];
390 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
393 return alloc_string(var_name
);
398 char *expr_to_str(struct expression
*expr
)
400 return expr_to_str_sym(expr
, NULL
);
404 * get_variable_from_expr_simple() only returns simple variables.
405 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
406 * then it returns NULL.
408 char *expr_to_var_sym(struct expression
*expr
,
409 struct symbol
**sym_ptr
)
411 static char var_name
[VAR_LEN
];
420 expr
= strip_expr(expr
);
421 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
429 return alloc_string(var_name
);
432 char *expr_to_var(struct expression
*expr
)
434 return expr_to_var_sym(expr
, NULL
);
437 struct symbol
*expr_to_sym(struct expression
*expr
)
442 name
= expr_to_var_sym(expr
, &sym
);
447 int get_complication_score(struct expression
*expr
)
451 expr
= strip_expr(expr
);
454 * Don't forget to keep get_complication_score() and store_all_links()
462 switch (expr
->type
) {
467 score
+= get_complication_score(expr
->left
);
468 score
+= get_complication_score(expr
->right
);
471 if (is_local_variable(expr
))
476 return score
+ get_complication_score(expr
->unop
);
479 return score
+ get_complication_score(expr
->deref
);
487 struct expression
*reorder_expr_alphabetically(struct expression
*expr
)
489 struct expression
*ret
;
492 if (expr
->type
!= EXPR_BINOP
)
494 if (expr
->op
!= '+' && expr
->op
!= '*')
497 left
= expr_to_var(expr
->left
);
498 right
= expr_to_var(expr
->right
);
502 if (strcmp(left
, right
) <= 0)
505 ret
= binop_expression(expr
->right
, expr
->op
, expr
->left
);
513 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
524 expr
= strip_parens(expr
);
528 name
= expr_to_var_sym(expr
, &tmp
);
533 *vsl
= expr_to_vsl(expr
);
538 score
= get_complication_score(expr
);
539 if (score
<= 0 || score
> 2)
543 *vsl
= expr_to_vsl(expr
);
548 expr
= reorder_expr_alphabetically(expr
);
550 return expr_to_str(expr
);
553 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
555 return expr_to_chunk_helper(expr
, sym
, NULL
);
558 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
560 return expr_to_chunk_helper(expr
, sym
, vsl
);
563 int sym_name_is(const char *name
, struct expression
*expr
)
567 if (expr
->type
!= EXPR_SYMBOL
)
569 if (!strcmp(expr
->symbol_name
->name
, name
))
574 int is_zero(struct expression
*expr
)
578 if (get_value(expr
, &sval
) && sval
.value
== 0)
583 int is_array(struct expression
*expr
)
587 expr
= strip_expr(expr
);
591 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
592 expr
= strip_expr(expr
->unop
);
595 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
599 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
602 type
= get_type(expr
->left
);
603 if (!type
|| type
->type
!= SYM_ARRAY
)
609 struct expression
*get_array_base(struct expression
*expr
)
613 expr
= strip_expr(expr
);
614 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
615 expr
= strip_expr(expr
->unop
);
616 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
618 return strip_parens(expr
->left
);
621 struct expression
*get_array_offset(struct expression
*expr
)
625 expr
= strip_expr(expr
);
626 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
627 expr
= strip_expr(expr
->unop
);
628 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
630 return strip_parens(expr
->right
);
633 const char *show_state(struct smatch_state
*state
)
640 struct statement
*get_expression_statement(struct expression
*expr
)
642 /* What are those things called? if (({....; ret;})) { ...*/
644 if (expr
->type
!= EXPR_PREOP
)
650 if (expr
->unop
->type
!= EXPR_STATEMENT
)
652 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
654 return expr
->unop
->statement
;
657 struct expression
*strip_parens(struct expression
*expr
)
662 if (expr
->type
== EXPR_PREOP
) {
664 return expr
; /* parsing invalid code */
666 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
667 expr
->unop
->statement
->type
== STMT_COMPOUND
)
670 return strip_parens(expr
->unop
);
675 struct expression
*strip_expr(struct expression
*expr
)
680 switch (expr
->type
) {
681 case EXPR_FORCE_CAST
:
683 return strip_expr(expr
->cast_expression
);
685 struct expression
*unop
;
687 if (!expr
->unop
) /* parsing invalid code */
690 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
691 expr
->unop
->statement
->type
== STMT_COMPOUND
)
694 unop
= strip_expr(expr
->unop
);
696 if (expr
->op
== '*' && unop
&&
697 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
698 struct symbol
*type
= get_type(unop
->unop
);
700 if (type
&& type
->type
== SYM_ARRAY
)
702 return strip_expr(unop
->unop
);
710 case EXPR_CONDITIONAL
:
711 if (known_condition_true(expr
->conditional
)) {
713 return strip_expr(expr
->cond_true
);
714 return strip_expr(expr
->conditional
);
716 if (known_condition_false(expr
->conditional
))
717 return strip_expr(expr
->cond_false
);
720 if (sym_name_is("__builtin_expect", expr
->fn
)) {
721 expr
= get_argument_from_call_expr(expr
->args
, 0);
722 return strip_expr(expr
);
729 static void delete_state_tracker(struct tracker
*t
)
731 delete_state(t
->owner
, t
->name
, t
->sym
);
735 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
739 t
= alloc_tracker(my_id
, name
, sym
);
740 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
743 int is_error_return(struct expression
*expr
)
745 struct symbol
*cur_func
= cur_func_sym
;
750 if (cur_func
->type
!= SYM_NODE
)
752 cur_func
= get_base_type(cur_func
);
753 if (cur_func
->type
!= SYM_FN
)
755 cur_func
= get_base_type(cur_func
);
756 if (cur_func
== &void_ctype
)
758 if (!get_implied_value(expr
, &sval
))
762 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
767 int getting_address(void)
769 struct expression
*tmp
;
773 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
776 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
778 if (tmp
->op
== '.' && !dot_ops
++)
783 } END_FOR_EACH_PTR_REVERSE(tmp
);
787 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
791 expr
= strip_expr(expr
);
792 if (expr
->type
!= EXPR_DEREF
)
797 sym
= get_type(expr
->deref
);
800 if (sym
->type
== SYM_UNION
)
805 *type
= sym
->ident
->name
;
806 *member
= expr
->member
->name
;
810 char *get_member_name(struct expression
*expr
)
815 expr
= strip_expr(expr
);
816 if (!expr
|| expr
->type
!= EXPR_DEREF
)
821 sym
= get_type(expr
->deref
);
824 if (sym
->type
== SYM_UNION
) {
825 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
826 sym
->ident
? sym
->ident
->name
: "anonymous",
828 return alloc_string(buf
);
832 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
833 return alloc_string(buf
);
836 int cmp_pos(struct position pos1
, struct position pos2
)
838 /* the stream position is ... */
839 if (pos1
.stream
> pos2
.stream
)
841 if (pos1
.stream
< pos2
.stream
)
844 if (pos1
.line
< pos2
.line
)
846 if (pos1
.line
> pos2
.line
)
849 if (pos1
.pos
< pos2
.pos
)
851 if (pos1
.pos
> pos2
.pos
)
857 int positions_eq(struct position pos1
, struct position pos2
)
859 if (pos1
.line
!= pos2
.line
)
861 if (pos1
.pos
!= pos2
.pos
)
863 if (pos1
.stream
!= pos2
.stream
)
868 struct statement
*get_current_statement(void)
870 struct statement
*prev
, *tmp
;
872 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
874 if (!prev
|| !get_macro_name(prev
->pos
))
877 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
878 if (positions_eq(tmp
->pos
, prev
->pos
))
880 if (prev
->pos
.line
> tmp
->pos
.line
)
883 } END_FOR_EACH_PTR_REVERSE(tmp
);
887 struct statement
*get_prev_statement(void)
889 struct statement
*tmp
;
893 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
896 } END_FOR_EACH_PTR_REVERSE(tmp
);
900 int get_param_num_from_sym(struct symbol
*sym
)
909 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
913 } END_FOR_EACH_PTR(tmp
);
917 int get_param_num(struct expression
*expr
)
924 name
= expr_to_var_sym(expr
, &sym
);
928 return get_param_num_from_sym(sym
);
931 int ms_since(struct timeval
*start
)
936 gettimeofday(&end
, NULL
);
937 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
938 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
942 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
947 if (parent_is_null_var_sym(name
, sym
) ||
948 parent_is_free_var_sym(name
, sym
))
953 int parent_is_gone(struct expression
*expr
)
959 expr
= strip_expr(expr
);
960 var
= expr_to_var_sym(expr
, &sym
);
963 ret
= parent_is_gone_var_sym(var
, sym
);
969 int invert_op(int op
)
980 case SPECIAL_LEFTSHIFT
:
981 return SPECIAL_RIGHTSHIFT
;
982 case SPECIAL_RIGHTSHIFT
:
983 return SPECIAL_LEFTSHIFT
;
988 int expr_equiv(struct expression
*one
, struct expression
*two
)
990 struct symbol
*one_sym
= NULL
;
991 struct symbol
*two_sym
= NULL
;
992 char *one_name
= NULL
;
993 char *two_name
= NULL
;
998 if (one
->type
!= two
->type
)
1001 one_name
= expr_to_str_sym(one
, &one_sym
);
1004 two_name
= expr_to_str_sym(two
, &two_sym
);
1007 if (one_sym
!= two_sym
)
1010 * This is a terrible hack because expr_to_str() sometimes gives up in
1011 * the middle and just returns what it has. If you see a () you know
1012 * the string is bogus.
1014 if (strstr(one_name
, "()"))
1016 if (strcmp(one_name
, two_name
) == 0)
1019 free_string(one_name
);
1020 free_string(two_name
);
1024 void push_int(struct int_stack
**stack
, int num
)
1029 * Just put the int on directly instead of a pointer to the int.
1030 * Shift it to the left because Sparse uses the last two bits.
1031 * This is sort of a dirty hack, yes.
1034 munged
= INT_PTR(num
<< 2);
1036 add_ptr_list(stack
, munged
);
1039 int pop_int(struct int_stack
**stack
)
1043 num
= last_ptr_list((struct ptr_list
*)*stack
);
1044 delete_ptr_list_last((struct ptr_list
**)stack
);
1046 return PTR_INT(num
) >> 2;