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
)
145 /* can't happen on valid code */
150 switch (expr
->type
) {
152 struct expression
*deref
;
158 struct expression
*unop
= strip_expr(deref
->unop
);
160 if (unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
165 if (!is_pointer(deref
))
170 __get_variable_from_expr(sym_ptr
, buf
, deref
, len
, complicated
, no_parens
);
173 append(buf
, "->", len
);
175 append(buf
, ".", len
);
178 append(buf
, expr
->member
->name
, len
);
180 append(buf
, "unknown_member", len
);
185 if (expr
->symbol_name
)
186 append(buf
, expr
->symbol_name
->name
, len
);
190 *sym_ptr
= expr
->symbol
;
196 if (get_expression_statement(expr
)) {
201 if (expr
->op
== '(') {
202 if (!no_parens
&& expr
->unop
->type
!= EXPR_SYMBOL
)
203 append(buf
, "(", len
);
204 } else if (expr
->op
!= '*' || !get_array_expr(expr
->unop
)) {
205 tmp
= show_special(expr
->op
);
206 append(buf
, tmp
, len
);
208 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
209 len
, complicated
, no_parens
);
211 if (expr
->op
== '(' && !no_parens
&& expr
->unop
->type
!= EXPR_SYMBOL
)
212 append(buf
, ")", len
);
214 if (expr
->op
== SPECIAL_DECREMENT
||
215 expr
->op
== SPECIAL_INCREMENT
)
223 __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
,
224 len
, complicated
, no_parens
);
225 tmp
= show_special(expr
->op
);
226 append(buf
, tmp
, len
);
228 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
232 case EXPR_ASSIGNMENT
:
237 struct expression
*array_expr
;
240 array_expr
= get_array_expr(expr
);
242 __get_variable_from_expr(sym_ptr
, buf
, array_expr
, len
, complicated
, no_parens
);
243 append(buf
, "[", len
);
245 __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, len
, complicated
, no_parens
);
246 snprintf(tmp
, sizeof(tmp
), " %s ", show_special(expr
->op
));
247 append(buf
, tmp
, len
);
249 __get_variable_from_expr(NULL
, buf
, expr
->right
, len
, complicated
, no_parens
);
251 append(buf
, "]", len
);
258 snprintf(tmp
, 25, "%lld", expr
->value
);
259 append(buf
, tmp
, len
);
263 append(buf
, "\"", len
);
265 append(buf
, expr
->string
->data
, len
);
266 append(buf
, "\"", len
);
269 struct expression
*tmp
;
273 __get_variable_from_expr(NULL
, buf
, expr
->fn
, len
, complicated
, no_parens
);
274 append(buf
, "(", len
);
276 FOR_EACH_PTR(expr
->args
, tmp
) {
278 append(buf
, ", ", len
);
279 __get_variable_from_expr(NULL
, buf
, tmp
, len
, complicated
, no_parens
);
280 } END_FOR_EACH_PTR(tmp
);
281 append(buf
, ")", len
);
285 case EXPR_FORCE_CAST
:
286 __get_variable_from_expr(sym_ptr
, buf
,
287 expr
->cast_expression
, len
,
288 complicated
, no_parens
);
294 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
295 size
= type_bytes(get_base_type(expr
->cast_type
));
296 snprintf(tmp
, 25, "%d", size
);
297 append(buf
, tmp
, len
);
301 case EXPR_IDENTIFIER
:
303 if (expr
->expr_ident
)
304 append(buf
, expr
->expr_ident
->name
, len
);
308 //printf("unknown type = %d\n", expr->type);
314 * This is returns a stylized "c looking" representation of the
317 * It uses the same buffer every time so you have to save the result
318 * yourself if you want to keep it.
322 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
324 static char var_name
[VAR_LEN
];
333 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
336 return alloc_string(var_name
);
341 char *expr_to_str(struct expression
*expr
)
343 return expr_to_str_sym(expr
, NULL
);
347 * get_variable_from_expr_simple() only returns simple variables.
348 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
349 * then it returns NULL.
351 char *expr_to_var_sym(struct expression
*expr
,
352 struct symbol
**sym_ptr
)
354 static char var_name
[VAR_LEN
];
363 expr
= strip_expr(expr
);
364 __get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
372 return alloc_string(var_name
);
375 char *expr_to_var(struct expression
*expr
)
377 return expr_to_var_sym(expr
, NULL
);
380 struct symbol
*expr_to_sym(struct expression
*expr
)
385 name
= expr_to_var_sym(expr
, &sym
);
390 int get_complication_score(struct expression
*expr
)
394 expr
= strip_expr(expr
);
397 * Don't forget to keep get_complication_score() and store_all_links()
405 switch (expr
->type
) {
410 score
+= get_complication_score(expr
->left
);
411 score
+= get_complication_score(expr
->right
);
414 if (is_local_variable(expr
))
419 return score
+ get_complication_score(expr
->unop
);
422 return score
+ get_complication_score(expr
->deref
);
430 struct expression
*reorder_expr_alphabetically(struct expression
*expr
)
432 struct expression
*ret
;
435 if (expr
->type
!= EXPR_BINOP
)
437 if (expr
->op
!= '+' && expr
->op
!= '*')
440 left
= expr_to_var(expr
->left
);
441 right
= expr_to_var(expr
->right
);
445 if (strcmp(left
, right
) <= 0)
448 ret
= binop_expression(expr
->right
, expr
->op
, expr
->left
);
456 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
467 expr
= strip_parens(expr
);
471 name
= expr_to_var_sym(expr
, &tmp
);
476 *vsl
= expr_to_vsl(expr
);
481 score
= get_complication_score(expr
);
482 if (score
<= 0 || score
> 2)
486 *vsl
= expr_to_vsl(expr
);
491 expr
= reorder_expr_alphabetically(expr
);
493 return expr_to_str(expr
);
496 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
498 return expr_to_chunk_helper(expr
, sym
, NULL
);
501 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
503 return expr_to_chunk_helper(expr
, sym
, vsl
);
506 int sym_name_is(const char *name
, struct expression
*expr
)
510 if (expr
->type
!= EXPR_SYMBOL
)
512 if (!strcmp(expr
->symbol_name
->name
, name
))
517 int is_zero(struct expression
*expr
)
521 if (get_value(expr
, &sval
) && sval
.value
== 0)
526 int is_array(struct expression
*expr
)
530 expr
= strip_expr(expr
);
534 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
535 expr
= strip_expr(expr
->unop
);
538 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
542 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
545 type
= get_type(expr
->left
);
546 if (!type
|| type
->type
!= SYM_ARRAY
)
552 struct expression
*get_array_base(struct expression
*expr
)
556 expr
= strip_expr(expr
);
557 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
558 expr
= strip_expr(expr
->unop
);
559 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
561 return strip_parens(expr
->left
);
564 struct expression
*get_array_offset(struct expression
*expr
)
568 expr
= strip_expr(expr
);
569 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
570 expr
= strip_expr(expr
->unop
);
571 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
573 return strip_parens(expr
->right
);
576 const char *show_state(struct smatch_state
*state
)
583 struct statement
*get_expression_statement(struct expression
*expr
)
585 /* What are those things called? if (({....; ret;})) { ...*/
587 if (expr
->type
!= EXPR_PREOP
)
591 if (expr
->unop
->type
!= EXPR_STATEMENT
)
593 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
595 return expr
->unop
->statement
;
598 struct expression
*strip_parens(struct expression
*expr
)
603 if (expr
->type
== EXPR_PREOP
) {
604 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
605 expr
->unop
->statement
->type
== STMT_COMPOUND
)
608 return strip_parens(expr
->unop
);
613 struct expression
*strip_expr(struct expression
*expr
)
618 switch (expr
->type
) {
619 case EXPR_FORCE_CAST
:
621 return strip_expr(expr
->cast_expression
);
623 struct expression
*unop
;
625 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
626 expr
->unop
->statement
->type
== STMT_COMPOUND
)
629 unop
= strip_expr(expr
->unop
);
631 if (expr
->op
== '*' && unop
&&
632 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
633 struct symbol
*type
= get_type(unop
->unop
);
635 if (type
&& type
->type
== SYM_ARRAY
)
637 return strip_expr(unop
->unop
);
645 case EXPR_CONDITIONAL
:
646 if (known_condition_true(expr
->conditional
)) {
648 return strip_expr(expr
->cond_true
);
649 return strip_expr(expr
->conditional
);
651 if (known_condition_false(expr
->conditional
))
652 return strip_expr(expr
->cond_false
);
655 if (sym_name_is("__builtin_expect", expr
->fn
)) {
656 expr
= get_argument_from_call_expr(expr
->args
, 0);
657 return strip_expr(expr
);
664 static void delete_state_tracker(struct tracker
*t
)
666 delete_state(t
->owner
, t
->name
, t
->sym
);
670 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
674 t
= alloc_tracker(my_id
, name
, sym
);
675 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
678 int is_error_return(struct expression
*expr
)
680 struct symbol
*cur_func
= cur_func_sym
;
685 if (cur_func
->type
!= SYM_NODE
)
687 cur_func
= get_base_type(cur_func
);
688 if (cur_func
->type
!= SYM_FN
)
690 cur_func
= get_base_type(cur_func
);
691 if (cur_func
== &void_ctype
)
693 if (!get_implied_value(expr
, &sval
))
697 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
702 int getting_address(void)
704 struct expression
*tmp
;
708 FOR_EACH_PTR_REVERSE(big_expression_stack
, tmp
) {
711 if (tmp
->type
== EXPR_PREOP
&& tmp
->op
== '(')
713 if (tmp
->op
== '.' && !dot_ops
++)
718 } END_FOR_EACH_PTR_REVERSE(tmp
);
722 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
726 expr
= strip_expr(expr
);
727 if (expr
->type
!= EXPR_DEREF
)
732 sym
= get_type(expr
->deref
);
735 if (sym
->type
== SYM_UNION
)
740 *type
= sym
->ident
->name
;
741 *member
= expr
->member
->name
;
745 char *get_member_name(struct expression
*expr
)
750 expr
= strip_expr(expr
);
751 if (expr
->type
!= EXPR_DEREF
)
756 sym
= get_type(expr
->deref
);
759 if (sym
->type
== SYM_UNION
) {
760 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
761 sym
->ident
? sym
->ident
->name
: "anonymous",
763 return alloc_string(buf
);
767 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
768 return alloc_string(buf
);
771 int cmp_pos(struct position pos1
, struct position pos2
)
773 /* the stream position is ... */
774 if (pos1
.stream
> pos2
.stream
)
776 if (pos1
.stream
< pos2
.stream
)
779 if (pos1
.line
< pos2
.line
)
781 if (pos1
.line
> pos2
.line
)
784 if (pos1
.pos
< pos2
.pos
)
786 if (pos1
.pos
> pos2
.pos
)
792 int positions_eq(struct position pos1
, struct position pos2
)
794 if (pos1
.line
!= pos2
.line
)
796 if (pos1
.pos
!= pos2
.pos
)
798 if (pos1
.stream
!= pos2
.stream
)
803 struct statement
*get_current_statement(void)
805 struct statement
*prev
, *tmp
;
807 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
809 if (!prev
|| !get_macro_name(prev
->pos
))
812 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
813 if (positions_eq(tmp
->pos
, prev
->pos
))
815 if (prev
->pos
.line
> tmp
->pos
.line
)
818 } END_FOR_EACH_PTR_REVERSE(tmp
);
822 struct statement
*get_prev_statement(void)
824 struct statement
*tmp
;
828 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
831 } END_FOR_EACH_PTR_REVERSE(tmp
);
835 int get_param_num_from_sym(struct symbol
*sym
)
844 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, tmp
) {
848 } END_FOR_EACH_PTR(tmp
);
852 int get_param_num(struct expression
*expr
)
859 name
= expr_to_var_sym(expr
, &sym
);
863 return get_param_num_from_sym(sym
);
866 int ms_since(struct timeval
*start
)
871 gettimeofday(&end
, NULL
);
872 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
873 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
877 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
882 if (parent_is_null_var_sym(name
, sym
) ||
883 parent_is_free_var_sym(name
, sym
))
888 int parent_is_gone(struct expression
*expr
)
894 expr
= strip_expr(expr
);
895 var
= expr_to_var_sym(expr
, &sym
);
898 ret
= parent_is_gone_var_sym(var
, sym
);
904 int invert_op(int op
)
915 case SPECIAL_LEFTSHIFT
:
916 return SPECIAL_RIGHTSHIFT
;
917 case SPECIAL_RIGHTSHIFT
:
918 return SPECIAL_LEFTSHIFT
;
923 int expr_equiv(struct expression
*one
, struct expression
*two
)
925 struct symbol
*one_sym
, *two_sym
;
926 char *one_name
= NULL
;
927 char *two_name
= NULL
;
932 if (one
->type
!= two
->type
)
935 one_name
= expr_to_str_sym(one
, &one_sym
);
936 if (!one_name
|| !one_sym
)
938 two_name
= expr_to_str_sym(two
, &two_sym
);
939 if (!two_name
|| !two_sym
)
941 if (one_sym
!= two_sym
)
943 if (strcmp(one_name
, two_name
) == 0)
946 free_string(one_name
);
947 free_string(two_name
);
951 void push_int(struct int_stack
**stack
, int num
)
956 * Just put the int on directly instead of a pointer to the int.
957 * Shift it to the left because Sparse uses the last two bits.
958 * This is sort of a dirty hack, yes.
961 munged
= INT_PTR(num
<< 2);
963 add_ptr_list(stack
, munged
);
966 int pop_int(struct int_stack
**stack
)
970 num
= last_ptr_list((struct ptr_list
*)*stack
);
971 delete_ptr_list_last((struct ptr_list
**)stack
);
973 return PTR_INT(num
) >> 2;