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 static struct expression
*strip_expr_helper(struct expression
*expr
, bool set_parent
, bool cast
, int *nest
);
33 char *alloc_string(const char *str
)
39 tmp
= malloc(strlen(str
) + 1);
44 char *alloc_string_newline(const char *str
)
52 tmp
= malloc(len
+ 2);
53 snprintf(tmp
, len
+ 2, "%s\n", str
);
57 void free_string(char *str
)
62 void remove_parens(char *str
)
67 while (*src
!= '\0') {
68 if (*src
== '(' || *src
== ')') {
77 struct smatch_state
*alloc_state_num(int num
)
79 struct smatch_state
*state
;
80 static char buff
[256];
82 state
= __alloc_smatch_state(0);
83 snprintf(buff
, 255, "%d", num
);
85 state
->name
= alloc_string(buff
);
86 state
->data
= INT_PTR(num
);
90 struct smatch_state
*alloc_state_str(const char *name
)
92 struct smatch_state
*state
;
94 state
= __alloc_smatch_state(0);
95 state
->name
= alloc_string(name
);
99 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
)
101 if (!s1
->name
|| !s2
->name
)
103 if (strcmp(s1
->name
, s2
->name
) == 0)
108 struct smatch_state
*alloc_state_expr(struct expression
*expr
)
110 struct smatch_state
*state
;
113 expr
= strip_expr(expr
);
114 name
= expr_to_str(expr
);
118 state
= __alloc_smatch_state(0);
119 state
->name
= alloc_sname(name
);
125 static int FORMAT_ATTR(4) append(char *dest
, int off
, int len
, const char *fmt
, ...)
130 if (len
<= 0 || off
< 0 || off
>= len
- 1)
134 n
= vsnprintf(dest
+ off
, len
- off
, fmt
, args
);
137 if (n
> len
- off
- 1)
138 return len
- off
- 1;
142 struct expression
*get_assigned_call(struct expression
*expr
)
144 while (expr
&& expr
->type
== EXPR_ASSIGNMENT
)
145 expr
= strip_expr(expr
->right
);
146 if (!expr
|| expr
->type
!= EXPR_CALL
)
152 * If you have "foo(a, b, 1);" then use
153 * get_argument_from_call_expr(expr, 0) to return the expression for
154 * a. Yes, it does start counting from 0.
156 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
159 struct expression
*expr
;
165 FOR_EACH_PTR(args
, expr
) {
169 } END_FOR_EACH_PTR(expr
);
173 struct expression
*get_array_expr(struct expression
*expr
)
175 struct expression
*parent
;
178 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
181 type
= get_type(expr
->left
);
184 if (type
->type
== SYM_ARRAY
)
186 if (type
->type
!= SYM_PTR
)
189 parent
= expr_get_parent_expr(expr
);
190 if (!parent
) /* Sometimes we haven't set up the ->parent yet. FIXME!! */
192 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
198 static struct expression
*strip_star_address(struct expression
*expr
)
200 struct expression
*unop
;
202 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
204 unop
= strip_parens(expr
->unop
);
205 if (unop
->type
!= EXPR_PREOP
|| unop
->op
!= '&')
211 static struct expression
*strip_parens_symbol(struct expression
*expr
)
213 struct expression
*unop
;
215 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '(')
218 * This should probably be strip_parens() but expr_to_str() doesn't
219 * print casts so we may as well strip those too. In other words,
220 * instead of fixing the code to print the cast, it's easier to just
221 * write even more code that relies on the bug. ;)
223 unop
= strip_expr(expr
->unop
);
224 if (unop
->type
!= EXPR_SYMBOL
)
229 static int __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
230 struct expression
*expr
, int off
, int len
,
236 /* can't happen on valid code */
241 expr
= strip_star_address(expr
);
242 expr
= strip_parens_symbol(expr
);
244 switch (expr
->type
) {
246 struct expression
*deref
;
252 /* this is pure guess work and nonsense programming */
253 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*') {
258 off
+= __get_variable_from_expr(sym_ptr
, buf
, deref
, off
, len
, complicated
);
261 off
+= append(buf
, off
, len
, "->");
263 off
+= append(buf
, off
, len
, ".");
266 off
+= append(buf
, off
, len
, "%s", expr
->member
->name
);
268 off
+= append(buf
, off
, len
, "unknown_member");
269 return off
- orig_off
;
272 if (expr
->symbol_name
)
273 off
+= append(buf
, off
, len
, "%s", expr
->symbol_name
->name
);
277 *sym_ptr
= expr
->symbol
;
279 return off
- orig_off
;
283 if (get_expression_statement(expr
)) {
287 if (expr
->op
== SPECIAL_DECREMENT
||
288 expr
->op
== SPECIAL_INCREMENT
)
291 if (expr
->op
== '*' && get_array_expr(expr
->unop
))
294 tmp
= show_special(expr
->op
);
296 off
+= append(buf
, off
, len
, "%s", tmp
);
297 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
,
300 off
+= append(buf
, off
, len
, ")");
302 return off
- orig_off
;
305 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
, len
,
307 off
+= append(buf
, off
, len
, "%s", show_special(expr
->op
));
309 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
311 return off
- orig_off
;
313 case EXPR_ASSIGNMENT
:
318 struct expression
*array_expr
;
321 array_expr
= get_array_expr(expr
);
323 off
+= __get_variable_from_expr(sym_ptr
, buf
, array_expr
, off
, len
, complicated
);
324 off
+= append(buf
, off
, len
, "[");
326 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, off
, len
, complicated
);
327 off
+= append(buf
, off
, len
, "%s%s ",
328 (expr
->type
== EXPR_COMMA
) ? "" : " ",
329 show_special(expr
->op
));
331 off
+= __get_variable_from_expr(NULL
, buf
, expr
->right
, off
, len
, complicated
);
333 off
+= append(buf
, off
, len
, "]");
334 return off
- orig_off
;
340 if (!get_value(expr
, &sval
))
342 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
343 return off
- orig_off
;
349 if (!get_value(expr
, &sval
))
352 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
353 return off
- orig_off
;
356 off
+= append(buf
, off
, len
, "\"");
358 off
+= append(buf
, off
, len
, "%s", expr
->string
->data
);
359 off
+= append(buf
, off
, len
, "\"");
360 return off
- orig_off
;
362 struct expression
*tmp
;
366 off
+= __get_variable_from_expr(NULL
, buf
, expr
->fn
, off
, len
, complicated
);
367 off
+= append(buf
, off
, len
, "(");
369 FOR_EACH_PTR(expr
->args
, tmp
) {
371 off
+= append(buf
, off
, len
, ", ");
372 off
+= __get_variable_from_expr(NULL
, buf
, tmp
, off
, len
, complicated
);
373 } END_FOR_EACH_PTR(tmp
);
374 off
+= append(buf
, off
, len
, ")");
375 return off
- orig_off
;
378 case EXPR_FORCE_CAST
:
379 return __get_variable_from_expr(sym_ptr
, buf
,
380 expr
->cast_expression
, off
, len
,
386 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
387 size
= type_bytes(get_base_type(expr
->cast_type
));
388 off
+= append(buf
, off
, len
, "%d", size
);
389 } else if (get_value(expr
, &sval
)) {
390 off
+= append(buf
, off
, len
, "%s", sval_to_str(sval
));
392 return off
- orig_off
;
394 case EXPR_IDENTIFIER
:
396 if (expr
->expr_ident
)
397 off
+= append(buf
, off
, len
, "%s", expr
->expr_ident
->name
);
398 return off
- orig_off
;
400 case EXPR_CONDITIONAL
:
402 off
+= append(buf
, off
, len
, "(");
403 off
+= __get_variable_from_expr(NULL
, buf
, expr
->conditional
, off
, len
, complicated
);
404 off
+= append(buf
, off
, len
, ") ?");
406 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_true
, off
, len
, complicated
);
407 off
+= append(buf
, off
, len
, ":");
408 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_false
, off
, len
, complicated
);
409 return off
- orig_off
;
411 off
+= append(buf
, off
, len
, "$expr_%p(%d)", expr
, expr
->type
);
412 return off
- orig_off
;
416 struct expr_str_cache_results
{
417 struct expression
*expr
;
423 static void get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
424 struct expression
*expr
, int len
,
427 static struct expr_str_cache_results cached
[8];
428 struct symbol
*tmp_sym
= NULL
;
432 for (i
= 0; i
< ARRAY_SIZE(cached
); i
++) {
433 if (expr
== cached
[i
].expr
) {
434 strncpy(buf
, cached
[i
].str
, len
);
436 *sym_ptr
= cached
[i
].sym
;
437 *complicated
= cached
[i
].complicated
;
442 __get_variable_from_expr(&tmp_sym
, buf
, expr
, 0, len
, complicated
);
446 if (expr
->smatch_flags
& Tmp
)
449 cached
[idx
].expr
= expr
;
450 strncpy(cached
[idx
].str
, buf
, VAR_LEN
);
451 cached
[idx
].sym
= tmp_sym
;
452 cached
[idx
].complicated
= *complicated
;
454 idx
= (idx
+ 1) % ARRAY_SIZE(cached
);
458 * This is returns a stylized "c looking" representation of the
461 * It uses the same buffer every time so you have to save the result
462 * yourself if you want to keep it.
466 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
468 static char var_name
[VAR_LEN
];
477 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
480 return alloc_string(var_name
);
485 char *expr_to_str(struct expression
*expr
)
487 return expr_to_str_sym(expr
, NULL
);
491 * get_variable_from_expr_simple() only returns simple variables.
492 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
493 * then it returns NULL.
495 char *expr_to_var_sym(struct expression
*expr
,
496 struct symbol
**sym_ptr
)
498 static char var_name
[VAR_LEN
];
507 expr
= strip_expr(expr
);
508 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
516 return alloc_string(var_name
);
519 char *expr_to_var(struct expression
*expr
)
521 return expr_to_var_sym(expr
, NULL
);
524 struct symbol
*expr_to_sym(struct expression
*expr
)
529 name
= expr_to_var_sym(expr
, &sym
);
534 int get_complication_score(struct expression
*expr
)
536 expr
= strip_expr(expr
);
539 * Don't forget to keep get_complication_score() and store_all_links()
547 switch (expr
->type
) {
552 return get_complication_score(expr
->left
) +
553 get_complication_score(expr
->right
);
557 if (expr
->op
== '*' || expr
->op
== '(')
558 return get_complication_score(expr
->unop
);
561 return get_complication_score(expr
->deref
);
570 struct expression
*reorder_expr_alphabetically(struct expression
*expr
)
572 struct expression
*ret
;
575 if (expr
->type
!= EXPR_BINOP
)
577 if (expr
->op
!= '+' && expr
->op
!= '*')
580 left
= expr_to_var(expr
->left
);
581 right
= expr_to_var(expr
->right
);
585 if (strcmp(left
, right
) <= 0)
588 ret
= binop_expression(expr
->right
, expr
->op
, expr
->left
);
596 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
598 struct var_sym_list
*tmp_vsl
;
608 expr
= strip_parens(expr
);
612 name
= expr_to_var_sym(expr
, &tmp
);
617 add_var_sym(vsl
, name
, tmp
);
622 score
= get_complication_score(expr
);
623 if (score
<= 0 || score
> 2)
626 tmp_vsl
= expr_to_vsl(expr
);
633 if (ptr_list_size((struct ptr_list
*)tmp_vsl
) == 1) {
636 vs
= first_ptr_list((struct ptr_list
*)tmp_vsl
);
641 expr
= reorder_expr_alphabetically(expr
);
643 return expr_to_str(expr
);
646 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
648 return expr_to_chunk_helper(expr
, sym
, NULL
);
651 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
653 return expr_to_chunk_helper(expr
, sym
, vsl
);
656 int sym_name_is(const char *name
, struct expression
*expr
)
660 if (expr
->type
!= EXPR_SYMBOL
)
662 if (!strcmp(expr
->symbol_name
->name
, name
))
667 int expr_is_zero(struct expression
*expr
)
671 if (get_value(expr
, &sval
) && sval
.value
== 0)
676 int is_array(struct expression
*expr
)
680 expr
= strip_expr(expr
);
684 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
685 expr
= strip_expr(expr
->unop
);
688 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
692 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
695 type
= get_type(expr
->left
);
696 if (!type
|| type
->type
!= SYM_ARRAY
)
702 struct expression
*get_array_base(struct expression
*expr
)
706 expr
= strip_expr(expr
);
707 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
708 expr
= strip_expr(expr
->unop
);
709 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
711 return strip_parens(expr
->left
);
714 struct expression
*get_array_offset(struct expression
*expr
)
718 expr
= strip_expr(expr
);
719 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
720 expr
= strip_expr(expr
->unop
);
721 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
723 expr
= strip_parens(expr
->right
);
724 if (expr
->type
== EXPR_POSTOP
)
725 expr
= strip_parens(expr
->unop
);
729 const char *show_state(struct smatch_state
*state
)
736 struct statement
*get_expression_statement(struct expression
*expr
)
738 /* What are those things called? if (({....; ret;})) { ...*/
740 if (expr
->type
!= EXPR_PREOP
)
746 if (expr
->unop
->type
!= EXPR_STATEMENT
)
748 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
750 return expr
->unop
->statement
;
753 struct expression
*strip_parens(struct expression
*expr
)
758 if (expr
->type
== EXPR_PREOP
) {
760 return expr
; /* parsing invalid code */
762 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
763 expr
->unop
->statement
->type
== STMT_COMPOUND
)
766 return strip_parens(expr
->unop
);
771 struct expression
*strip__builtin_choose_expr(struct expression
*expr
)
773 struct expression
*const_expr
, *expr1
, *expr2
;
776 if (expr
->type
!= EXPR_CALL
)
779 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
782 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
783 expr1
= get_argument_from_call_expr(expr
->args
, 1);
784 expr2
= get_argument_from_call_expr(expr
->args
, 2);
786 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
790 return strip_expr(expr1
);
792 return strip_expr(expr2
);
795 struct expression
*strip_Generic(struct expression
*expr
)
797 struct type_expression
*map
;
798 struct symbol
*type
, *tmp
;
800 if (!expr
|| expr
->type
!= EXPR_GENERIC
)
803 type
= get_type(expr
->control
);
805 for (map
= expr
->map
; map
; map
= map
->next
) {
806 tmp
= get_real_base_type(map
->type
);
807 if (!types_equiv(type
, tmp
))
820 static struct expression
*strip_plus_zero(struct expression
*expr
, bool set_parent
, bool cast
, int *nest
)
822 struct symbol
*left_type
, *right_type
;
827 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
830 /* don't strip away zero from the my_array[0] */
831 if (!is_array(expr
->left
))
834 left_type
= get_type(expr
->left
);
835 right_type
= get_type(expr
->right
);
836 if (!left_type
|| !right_type
)
839 if (expr_is_zero(expr
->left
)) {
840 if (type_positive_bits(left_type
) > 31 &&
841 type_positive_bits(left_type
) > type_positive_bits(right_type
))
843 return strip_expr_helper(expr
->right
, set_parent
, cast
, nest
);
845 if (expr_is_zero(expr
->right
)) {
846 if (type_positive_bits(right_type
) > 31 &&
847 type_positive_bits(right_type
) > type_positive_bits(left_type
))
849 return strip_expr_helper(expr
->left
, set_parent
, cast
, nest
);
855 static struct expression
*strip_expr_helper(struct expression
*expr
, bool set_parent
, bool cast
, int *nest
)
862 switch (expr
->type
) {
863 case EXPR_FORCE_CAST
:
866 expr_set_parent_expr(expr
->cast_expression
, expr
);
868 if (!expr
->cast_expression
)
873 type
= get_type(expr
->cast_expression
);
874 if (type
!= expr
->cast_type
)
877 return strip_expr_helper(expr
->cast_expression
, set_parent
, cast
, nest
);
879 struct expression
*unop
;
881 if (!expr
->unop
) /* parsing invalid code */
884 expr_set_parent_expr(expr
->unop
, expr
);
886 while (expr
->op
== '(' &&
887 expr
->unop
->type
== EXPR_PREOP
&&
888 expr
->unop
->op
== '(')
891 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
892 expr
->unop
->statement
->type
== STMT_COMPOUND
)
895 unop
= strip_expr_helper(expr
->unop
, set_parent
, cast
, nest
);
897 if (expr
->op
== '*' && unop
&&
898 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
899 struct symbol
*type
= get_type(unop
->unop
);
901 if (type
&& type
->type
== SYM_ARRAY
)
903 return strip_expr_helper(unop
->unop
, set_parent
, cast
, nest
);
911 case EXPR_CONDITIONAL
:
912 if (known_condition_true(expr
->conditional
)) {
913 if (expr
->cond_true
) {
915 expr_set_parent_expr(expr
->cond_true
, expr
);
916 return strip_expr_helper(expr
->cond_true
, set_parent
, cast
, nest
);
919 expr_set_parent_expr(expr
->conditional
, expr
);
920 return strip_expr_helper(expr
->conditional
, set_parent
, cast
, nest
);
922 if (known_condition_false(expr
->conditional
)) {
924 expr_set_parent_expr(expr
->cond_false
, expr
);
925 return strip_expr_helper(expr
->cond_false
, set_parent
, cast
, nest
);
929 if (sym_name_is("__builtin_expect", expr
->fn
) ||
930 sym_name_is("__builtin_bswap16", expr
->fn
) ||
931 sym_name_is("__builtin_bswap32", expr
->fn
) ||
932 sym_name_is("__builtin_bswap64", expr
->fn
)) {
933 expr
= get_argument_from_call_expr(expr
->args
, 0);
934 return strip_expr_helper(expr
, set_parent
, cast
, nest
);
936 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
937 return strip__builtin_choose_expr(expr
);
940 return strip_plus_zero(expr
, set_parent
, cast
, nest
);
942 return strip_Generic(expr
);
947 struct strip_cache_res
{
948 struct expression
*expr
;
949 struct expression
*res
;
951 #define STRIP_CACHE_SIZE 4
952 static struct strip_cache_res strip_cache
[STRIP_CACHE_SIZE
];
953 static struct strip_cache_res strip_no_cast_cache
[STRIP_CACHE_SIZE
];
954 static struct strip_cache_res strip_set_parent_cache
[STRIP_CACHE_SIZE
];
956 static struct expression
*call_strip_helper(struct expression
*expr
,
957 struct strip_cache_res
*cache
,
962 struct expression
*ret
;
969 for (i
= 0; i
< ARRAY_SIZE(strip_cache
); i
++) {
970 if (cache
[i
].expr
== expr
)
974 ret
= strip_expr_helper(expr
, set_parent
, cast
, &nest
);
975 *idx
= (*idx
+ 1) % STRIP_CACHE_SIZE
;
976 cache
[*idx
].expr
= expr
;
977 cache
[*idx
].res
= ret
;
981 struct expression
*strip_expr(struct expression
*expr
)
983 static int cache_idx
;
985 return call_strip_helper(expr
, strip_cache
, &cache_idx
, false, true);
988 struct expression
*strip_no_cast(struct expression
*expr
)
990 static int cache_idx
;
992 return call_strip_helper(expr
, strip_no_cast_cache
, &cache_idx
, false, false);
995 struct expression
*strip_expr_set_parent(struct expression
*expr
)
997 static int cache_idx
;
999 return call_strip_helper(expr
, strip_set_parent_cache
, &cache_idx
, true, true);
1002 void clear_strip_cache(void)
1004 memset(strip_cache
, 0, sizeof(strip_cache
));
1005 memset(strip_no_cast_cache
, 0, sizeof(strip_no_cast_cache
));
1006 memset(strip_set_parent_cache
, 0, sizeof(strip_set_parent_cache
));
1009 int is_error_return(struct expression
*expr
)
1011 struct symbol
*cur_func
= cur_func_sym
;
1012 struct range_list
*rl
;
1017 if (cur_func
->type
!= SYM_NODE
)
1019 cur_func
= get_base_type(cur_func
);
1020 if (cur_func
->type
!= SYM_FN
)
1022 cur_func
= get_base_type(cur_func
);
1023 if (cur_func
== &void_ctype
)
1025 if (option_project
== PROJ_KERNEL
&&
1026 get_implied_rl(expr
, &rl
) &&
1027 rl_type(rl
) == &int_ctype
&&
1028 sval_is_negative(rl_min(rl
)) &&
1029 rl_max(rl
).value
== -1)
1031 if (!get_implied_value(expr
, &sval
))
1035 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
1040 int getting_address(struct expression
*expr
)
1042 int deref_count
= 0;
1044 while ((expr
= expr_get_parent_expr(expr
))) {
1045 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
1046 /* &foo->bar->baz dereferences "foo->bar" */
1047 if (deref_count
== 0)
1051 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
1057 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
1061 expr
= strip_expr(expr
);
1062 if (expr
->type
!= EXPR_DEREF
)
1067 sym
= get_type(expr
->deref
);
1070 if (sym
->type
== SYM_UNION
)
1075 *type
= sym
->ident
->name
;
1076 *member
= expr
->member
->name
;
1080 char *get_member_name(struct expression
*expr
)
1085 expr
= strip_expr(expr
);
1086 if (!expr
|| expr
->type
!= EXPR_DEREF
)
1091 sym
= get_type(expr
->deref
);
1094 if (sym
->type
== SYM_PTR
)
1095 sym
= get_real_base_type(sym
);
1096 if (sym
->type
== SYM_UNION
) {
1097 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
1098 sym
->ident
? sym
->ident
->name
: "anonymous",
1099 expr
->member
->name
);
1100 return alloc_string(buf
);
1103 struct expression
*deref
;
1108 * If we're in an anonymous struct then maybe we can find an
1109 * outer struct name to use as a name. This code should be
1110 * recursive and cleaner. I am not very proud of it.
1114 deref
= strip_parens(expr
->deref
);
1115 if (deref
->type
!= EXPR_DEREF
|| !deref
->member
)
1117 sym
= get_type(deref
->deref
);
1118 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
1121 full
= expr_to_str(expr
);
1124 deref
= deref
->deref
;
1125 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*')
1126 deref
= deref
->unop
;
1127 outer
= expr_to_str(deref
);
1132 len
= strlen(outer
);
1133 if (strncmp(outer
, full
, len
) != 0) {
1138 if (full
[len
] == '-' && full
[len
+ 1] == '>')
1140 if (full
[len
] == '.')
1142 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, full
+ len
);
1146 return alloc_string(buf
);
1148 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
1149 return alloc_string(buf
);
1152 int cmp_pos(struct position pos1
, struct position pos2
)
1154 /* the stream position is ... */
1155 if (pos1
.stream
> pos2
.stream
)
1157 if (pos1
.stream
< pos2
.stream
)
1160 if (pos1
.line
< pos2
.line
)
1162 if (pos1
.line
> pos2
.line
)
1165 if (pos1
.pos
< pos2
.pos
)
1167 if (pos1
.pos
> pos2
.pos
)
1173 int positions_eq(struct position pos1
, struct position pos2
)
1175 if (pos1
.line
!= pos2
.line
)
1177 if (pos1
.pos
!= pos2
.pos
)
1179 if (pos1
.stream
!= pos2
.stream
)
1184 struct statement
*get_current_statement(void)
1186 struct statement
*prev
, *tmp
;
1188 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
1190 if (!prev
|| !get_macro_name(prev
->pos
))
1193 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1194 if (positions_eq(tmp
->pos
, prev
->pos
))
1196 if (prev
->pos
.line
> tmp
->pos
.line
)
1199 } END_FOR_EACH_PTR_REVERSE(tmp
);
1203 struct statement
*get_prev_statement(void)
1205 struct statement
*tmp
;
1209 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1212 } END_FOR_EACH_PTR_REVERSE(tmp
);
1216 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
)
1218 struct statement
*stmt
;
1219 struct statement
*last_stmt
;
1221 while (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1223 if (expr
->type
!= EXPR_STATEMENT
)
1225 stmt
= expr
->statement
;
1228 if (stmt
->type
== STMT_COMPOUND
) {
1229 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1232 if (last_stmt
->type
== STMT_LABEL
)
1233 last_stmt
= last_stmt
->label_statement
;
1234 if (last_stmt
->type
!= STMT_EXPRESSION
)
1236 return last_stmt
->expression
;
1238 if (stmt
->type
== STMT_EXPRESSION
)
1239 return stmt
->expression
;
1243 int ms_since(struct timeval
*start
)
1248 gettimeofday(&end
, NULL
);
1249 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
1250 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
1254 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
1259 if (parent_is_err_or_null_var_sym(name
, sym
) ||
1260 parent_is_free_var_sym(name
, sym
))
1265 int parent_is_gone(struct expression
*expr
)
1271 expr
= strip_expr(expr
);
1272 var
= expr_to_var_sym(expr
, &sym
);
1275 ret
= parent_is_gone_var_sym(var
, sym
);
1281 int invert_op(int op
)
1292 case SPECIAL_LEFTSHIFT
:
1293 return SPECIAL_RIGHTSHIFT
;
1294 case SPECIAL_RIGHTSHIFT
:
1295 return SPECIAL_LEFTSHIFT
;
1300 int op_remove_assign(int op
)
1303 case SPECIAL_ADD_ASSIGN
:
1305 case SPECIAL_SUB_ASSIGN
:
1307 case SPECIAL_MUL_ASSIGN
:
1309 case SPECIAL_DIV_ASSIGN
:
1311 case SPECIAL_MOD_ASSIGN
:
1313 case SPECIAL_AND_ASSIGN
:
1315 case SPECIAL_OR_ASSIGN
:
1317 case SPECIAL_XOR_ASSIGN
:
1319 case SPECIAL_SHL_ASSIGN
:
1320 return SPECIAL_LEFTSHIFT
;
1321 case SPECIAL_SHR_ASSIGN
:
1322 return SPECIAL_RIGHTSHIFT
;
1328 int expr_equiv(struct expression
*one
, struct expression
*two
)
1330 struct symbol
*one_sym
= NULL
;
1331 struct symbol
*two_sym
= NULL
;
1332 char *one_name
= NULL
;
1333 char *two_name
= NULL
;
1340 if (one
->type
!= two
->type
)
1342 if (is_fake_call(one
) || is_fake_call(two
))
1345 one_name
= expr_to_str_sym(one
, &one_sym
);
1348 two_name
= expr_to_str_sym(two
, &two_sym
);
1351 if (one_sym
!= two_sym
)
1354 * This is a terrible hack because expr_to_str() sometimes gives up in
1355 * the middle and just returns what it has. If you see a () you know
1356 * the string is bogus.
1358 if (strstr(one_name
, "()"))
1360 if (strcmp(one_name
, two_name
) == 0)
1363 free_string(one_name
);
1364 free_string(two_name
);
1368 void push_int(struct int_stack
**stack
, int num
)
1373 * Just put the int on directly instead of a pointer to the int.
1374 * Shift it to the left because Sparse uses the last two bits.
1375 * This is sort of a dirty hack, yes.
1378 munged
= INT_PTR(num
<< 2);
1380 add_ptr_list(stack
, munged
);
1383 int pop_int(struct int_stack
**stack
)
1387 num
= last_ptr_list((struct ptr_list
*)*stack
);
1388 delete_ptr_list_last((struct ptr_list
**)stack
);
1390 return PTR_INT(num
) >> 2;
1393 bool token_to_ul(struct token
*token
, unsigned long *val
)
1397 /* this function only works for very specific simple defines */
1398 while (cnt
++ < 20 && token
) {
1399 switch (token_type(token
)) {
1401 if (macro_to_ul(show_ident(token
->ident
), val
))
1405 *val
= strtoul(token
->number
, NULL
, 0);
1408 token
= token
->next
;
1413 bool macro_to_ul(const char *macro
, unsigned long *val
)
1415 struct symbol
*macro_sym
;
1420 macro_sym
= lookup_macro_symbol(macro
);
1421 if (!macro_sym
|| !macro_sym
->expansion
)
1423 return token_to_ul(macro_sym
->expansion
, val
);
1426 int success_fail_return(struct range_list
*rl
)
1434 /* NFSv3 uses negative error codes such as -EIOCBQUEUED for success */
1435 if (rl_to_sval(rl
, &sval
) && sval
.value
== -529)
1438 // Negatives are a failure
1439 if (sval_is_negative(rl_max(rl
)))
1442 // NULL and error pointers are a failure
1443 if (type_is_ptr(rl_type(rl
)) && is_err_or_null(rl
))
1446 if (rl_to_sval(rl
, &sval
)) {
1447 if (sval
.value
== 0) {
1448 // Zero is normally success but false is a failure
1449 if (type_bits(sval
.type
) == 1)
1455 if (sval
.value
== 1 && type_bits(sval
.type
) == 1)
1460 if (strcmp(str
, "s32min-(-1),1-s32max") == 0)
1463 if (strcmp(str
, "0-s32max") == 0)
1469 bool has_cleanup(struct expression
*expr
)
1471 expr
= strip_expr(expr
);
1472 if (!expr
|| expr
->type
!= EXPR_SYMBOL
)
1475 return !!expr
->symbol
->cleanup
;