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
:
317 struct expression
*array_expr
;
320 array_expr
= get_array_expr(expr
);
322 off
+= __get_variable_from_expr(sym_ptr
, buf
, array_expr
, off
, len
, complicated
);
323 off
+= append(buf
, off
, len
, "[");
325 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, off
, len
, complicated
);
326 off
+= append(buf
, off
, len
, " %s ", show_special(expr
->op
));
328 off
+= __get_variable_from_expr(NULL
, buf
, expr
->right
, off
, len
, complicated
);
330 off
+= append(buf
, off
, len
, "]");
331 return off
- orig_off
;
337 if (!get_value(expr
, &sval
))
339 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
340 return off
- orig_off
;
346 if (!get_value(expr
, &sval
))
349 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
350 return off
- orig_off
;
353 off
+= append(buf
, off
, len
, "\"");
355 off
+= append(buf
, off
, len
, "%s", expr
->string
->data
);
356 off
+= append(buf
, off
, len
, "\"");
357 return off
- orig_off
;
359 struct expression
*tmp
;
363 off
+= __get_variable_from_expr(NULL
, buf
, expr
->fn
, off
, len
, complicated
);
364 off
+= append(buf
, off
, len
, "(");
366 FOR_EACH_PTR(expr
->args
, tmp
) {
368 off
+= append(buf
, off
, len
, ", ");
369 off
+= __get_variable_from_expr(NULL
, buf
, tmp
, off
, len
, complicated
);
370 } END_FOR_EACH_PTR(tmp
);
371 off
+= append(buf
, off
, len
, ")");
372 return off
- orig_off
;
375 case EXPR_FORCE_CAST
:
376 return __get_variable_from_expr(sym_ptr
, buf
,
377 expr
->cast_expression
, off
, len
,
383 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
384 size
= type_bytes(get_base_type(expr
->cast_type
));
385 off
+= append(buf
, off
, len
, "%d", size
);
386 } else if (get_value(expr
, &sval
)) {
387 off
+= append(buf
, off
, len
, "%s", sval_to_str(sval
));
389 return off
- orig_off
;
391 case EXPR_IDENTIFIER
:
393 if (expr
->expr_ident
)
394 off
+= append(buf
, off
, len
, "%s", expr
->expr_ident
->name
);
395 return off
- orig_off
;
397 case EXPR_CONDITIONAL
:
399 off
+= append(buf
, off
, len
, "(");
400 off
+= __get_variable_from_expr(NULL
, buf
, expr
->conditional
, off
, len
, complicated
);
401 off
+= append(buf
, off
, len
, ") ?");
403 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_true
, off
, len
, complicated
);
404 off
+= append(buf
, off
, len
, ":");
405 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_false
, off
, len
, complicated
);
406 return off
- orig_off
;
408 off
+= append(buf
, off
, len
, "$expr_%p(%d)", expr
, expr
->type
);
409 return off
- orig_off
;
413 struct expr_str_cache_results
{
414 struct expression
*expr
;
420 static void get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
421 struct expression
*expr
, int len
,
424 static struct expr_str_cache_results cached
[8];
425 struct symbol
*tmp_sym
= NULL
;
429 for (i
= 0; i
< ARRAY_SIZE(cached
); i
++) {
430 if (expr
== cached
[i
].expr
) {
431 strncpy(buf
, cached
[i
].str
, len
);
433 *sym_ptr
= cached
[i
].sym
;
434 *complicated
= cached
[i
].complicated
;
439 __get_variable_from_expr(&tmp_sym
, buf
, expr
, 0, len
, complicated
);
443 if (expr
->smatch_flags
& Tmp
)
446 cached
[idx
].expr
= expr
;
447 strncpy(cached
[idx
].str
, buf
, VAR_LEN
);
448 cached
[idx
].sym
= tmp_sym
;
449 cached
[idx
].complicated
= *complicated
;
451 idx
= (idx
+ 1) % ARRAY_SIZE(cached
);
455 * This is returns a stylized "c looking" representation of the
458 * It uses the same buffer every time so you have to save the result
459 * yourself if you want to keep it.
463 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
465 static char var_name
[VAR_LEN
];
474 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
477 return alloc_string(var_name
);
482 char *expr_to_str(struct expression
*expr
)
484 return expr_to_str_sym(expr
, NULL
);
488 * get_variable_from_expr_simple() only returns simple variables.
489 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
490 * then it returns NULL.
492 char *expr_to_var_sym(struct expression
*expr
,
493 struct symbol
**sym_ptr
)
495 static char var_name
[VAR_LEN
];
504 expr
= strip_expr(expr
);
505 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
513 return alloc_string(var_name
);
516 char *expr_to_var(struct expression
*expr
)
518 return expr_to_var_sym(expr
, NULL
);
521 struct symbol
*expr_to_sym(struct expression
*expr
)
526 name
= expr_to_var_sym(expr
, &sym
);
531 int get_complication_score(struct expression
*expr
)
533 expr
= strip_expr(expr
);
536 * Don't forget to keep get_complication_score() and store_all_links()
544 switch (expr
->type
) {
549 return get_complication_score(expr
->left
) +
550 get_complication_score(expr
->right
);
554 if (expr
->op
== '*' || expr
->op
== '(')
555 return get_complication_score(expr
->unop
);
558 return get_complication_score(expr
->deref
);
567 struct expression
*reorder_expr_alphabetically(struct expression
*expr
)
569 struct expression
*ret
;
572 if (expr
->type
!= EXPR_BINOP
)
574 if (expr
->op
!= '+' && expr
->op
!= '*')
577 left
= expr_to_var(expr
->left
);
578 right
= expr_to_var(expr
->right
);
582 if (strcmp(left
, right
) <= 0)
585 ret
= binop_expression(expr
->right
, expr
->op
, expr
->left
);
593 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
595 struct var_sym_list
*tmp_vsl
;
605 expr
= strip_parens(expr
);
609 name
= expr_to_var_sym(expr
, &tmp
);
614 add_var_sym(vsl
, name
, tmp
);
619 score
= get_complication_score(expr
);
620 if (score
<= 0 || score
> 2)
623 tmp_vsl
= expr_to_vsl(expr
);
630 if (ptr_list_size((struct ptr_list
*)tmp_vsl
) == 1) {
633 vs
= first_ptr_list((struct ptr_list
*)tmp_vsl
);
638 expr
= reorder_expr_alphabetically(expr
);
640 return expr_to_str(expr
);
643 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
645 return expr_to_chunk_helper(expr
, sym
, NULL
);
648 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
650 return expr_to_chunk_helper(expr
, sym
, vsl
);
653 int sym_name_is(const char *name
, struct expression
*expr
)
657 if (expr
->type
!= EXPR_SYMBOL
)
659 if (!strcmp(expr
->symbol_name
->name
, name
))
664 int expr_is_zero(struct expression
*expr
)
668 if (get_value(expr
, &sval
) && sval
.value
== 0)
673 int is_array(struct expression
*expr
)
677 expr
= strip_expr(expr
);
681 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
682 expr
= strip_expr(expr
->unop
);
685 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
689 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
692 type
= get_type(expr
->left
);
693 if (!type
|| type
->type
!= SYM_ARRAY
)
699 struct expression
*get_array_base(struct expression
*expr
)
703 expr
= strip_expr(expr
);
704 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
705 expr
= strip_expr(expr
->unop
);
706 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
708 return strip_parens(expr
->left
);
711 struct expression
*get_array_offset(struct expression
*expr
)
715 expr
= strip_expr(expr
);
716 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
717 expr
= strip_expr(expr
->unop
);
718 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
720 expr
= strip_parens(expr
->right
);
721 if (expr
->type
== EXPR_POSTOP
)
722 expr
= strip_parens(expr
->unop
);
726 const char *show_state(struct smatch_state
*state
)
733 struct statement
*get_expression_statement(struct expression
*expr
)
735 /* What are those things called? if (({....; ret;})) { ...*/
737 if (expr
->type
!= EXPR_PREOP
)
743 if (expr
->unop
->type
!= EXPR_STATEMENT
)
745 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
747 return expr
->unop
->statement
;
750 struct expression
*strip_parens(struct expression
*expr
)
755 if (expr
->type
== EXPR_PREOP
) {
757 return expr
; /* parsing invalid code */
759 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
760 expr
->unop
->statement
->type
== STMT_COMPOUND
)
763 return strip_parens(expr
->unop
);
768 struct expression
*strip__builtin_choose_expr(struct expression
*expr
)
770 struct expression
*const_expr
, *expr1
, *expr2
;
773 if (expr
->type
!= EXPR_CALL
)
776 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
779 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
780 expr1
= get_argument_from_call_expr(expr
->args
, 1);
781 expr2
= get_argument_from_call_expr(expr
->args
, 2);
783 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
787 return strip_expr(expr1
);
789 return strip_expr(expr2
);
792 struct expression
*strip_Generic(struct expression
*expr
)
794 struct type_expression
*map
;
795 struct symbol
*type
, *tmp
;
797 if (!expr
|| expr
->type
!= EXPR_GENERIC
)
800 type
= get_type(expr
->control
);
802 for (map
= expr
->map
; map
; map
= map
->next
) {
803 tmp
= get_real_base_type(map
->type
);
804 if (!types_equiv(type
, tmp
))
817 static struct expression
*strip_plus_zero(struct expression
*expr
, bool set_parent
, bool cast
, int *nest
)
819 struct symbol
*left_type
, *right_type
;
824 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
827 /* don't strip away zero from the my_array[0] */
828 if (!is_array(expr
->left
))
831 left_type
= get_type(expr
->left
);
832 right_type
= get_type(expr
->right
);
833 if (!left_type
|| !right_type
)
836 if (expr_is_zero(expr
->left
)) {
837 if (type_positive_bits(left_type
) > 31 &&
838 type_positive_bits(left_type
) > type_positive_bits(right_type
))
840 return strip_expr_helper(expr
->right
, set_parent
, cast
, nest
);
842 if (expr_is_zero(expr
->right
)) {
843 if (type_positive_bits(right_type
) > 31 &&
844 type_positive_bits(right_type
) > type_positive_bits(left_type
))
846 return strip_expr_helper(expr
->left
, set_parent
, cast
, nest
);
852 static struct expression
*strip_expr_helper(struct expression
*expr
, bool set_parent
, bool cast
, int *nest
)
859 switch (expr
->type
) {
860 case EXPR_FORCE_CAST
:
863 expr_set_parent_expr(expr
->cast_expression
, expr
);
865 if (!expr
->cast_expression
)
870 type
= get_type(expr
->cast_expression
);
871 if (type
!= expr
->cast_type
)
874 return strip_expr_helper(expr
->cast_expression
, set_parent
, cast
, nest
);
876 struct expression
*unop
;
878 if (!expr
->unop
) /* parsing invalid code */
881 expr_set_parent_expr(expr
->unop
, expr
);
883 while (expr
->op
== '(' &&
884 expr
->unop
->type
== EXPR_PREOP
&&
885 expr
->unop
->op
== '(')
888 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
889 expr
->unop
->statement
->type
== STMT_COMPOUND
)
892 unop
= strip_expr_helper(expr
->unop
, set_parent
, cast
, nest
);
894 if (expr
->op
== '*' && unop
&&
895 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
896 struct symbol
*type
= get_type(unop
->unop
);
898 if (type
&& type
->type
== SYM_ARRAY
)
900 return strip_expr_helper(unop
->unop
, set_parent
, cast
, nest
);
908 case EXPR_CONDITIONAL
:
909 if (known_condition_true(expr
->conditional
)) {
910 if (expr
->cond_true
) {
912 expr_set_parent_expr(expr
->cond_true
, expr
);
913 return strip_expr_helper(expr
->cond_true
, set_parent
, cast
, nest
);
916 expr_set_parent_expr(expr
->conditional
, expr
);
917 return strip_expr_helper(expr
->conditional
, set_parent
, cast
, nest
);
919 if (known_condition_false(expr
->conditional
)) {
921 expr_set_parent_expr(expr
->cond_false
, expr
);
922 return strip_expr_helper(expr
->cond_false
, set_parent
, cast
, nest
);
926 if (sym_name_is("__builtin_expect", expr
->fn
) ||
927 sym_name_is("__builtin_bswap16", expr
->fn
) ||
928 sym_name_is("__builtin_bswap32", expr
->fn
) ||
929 sym_name_is("__builtin_bswap64", expr
->fn
)) {
930 expr
= get_argument_from_call_expr(expr
->args
, 0);
931 return strip_expr_helper(expr
, set_parent
, cast
, nest
);
933 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
934 return strip__builtin_choose_expr(expr
);
937 return strip_plus_zero(expr
, set_parent
, cast
, nest
);
939 return strip_Generic(expr
);
944 struct strip_cache_res
{
945 struct expression
*expr
;
946 struct expression
*res
;
948 #define STRIP_CACHE_SIZE 4
949 static struct strip_cache_res strip_cache
[STRIP_CACHE_SIZE
];
950 static struct strip_cache_res strip_no_cast_cache
[STRIP_CACHE_SIZE
];
951 static struct strip_cache_res strip_set_parent_cache
[STRIP_CACHE_SIZE
];
953 static struct expression
*call_strip_helper(struct expression
*expr
,
954 struct strip_cache_res
*cache
,
959 struct expression
*ret
;
966 for (i
= 0; i
< ARRAY_SIZE(strip_cache
); i
++) {
967 if (cache
[i
].expr
== expr
)
971 ret
= strip_expr_helper(expr
, set_parent
, cast
, &nest
);
972 *idx
= (*idx
+ 1) % STRIP_CACHE_SIZE
;
973 cache
[*idx
].expr
= expr
;
974 cache
[*idx
].res
= ret
;
978 struct expression
*strip_expr(struct expression
*expr
)
980 static int cache_idx
;
982 return call_strip_helper(expr
, strip_cache
, &cache_idx
, false, true);
985 struct expression
*strip_no_cast(struct expression
*expr
)
987 static int cache_idx
;
989 return call_strip_helper(expr
, strip_no_cast_cache
, &cache_idx
, false, false);
992 struct expression
*strip_expr_set_parent(struct expression
*expr
)
994 static int cache_idx
;
996 return call_strip_helper(expr
, strip_set_parent_cache
, &cache_idx
, true, true);
999 void clear_strip_cache(void)
1001 memset(strip_cache
, 0, sizeof(strip_cache
));
1002 memset(strip_no_cast_cache
, 0, sizeof(strip_no_cast_cache
));
1003 memset(strip_set_parent_cache
, 0, sizeof(strip_set_parent_cache
));
1006 static void delete_state_tracker(struct tracker
*t
)
1008 __delete_state(t
->owner
, t
->name
, t
->sym
);
1012 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
1016 t
= alloc_tracker(my_id
, name
, sym
);
1017 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
1020 int is_error_return(struct expression
*expr
)
1022 struct symbol
*cur_func
= cur_func_sym
;
1023 struct range_list
*rl
;
1028 if (cur_func
->type
!= SYM_NODE
)
1030 cur_func
= get_base_type(cur_func
);
1031 if (cur_func
->type
!= SYM_FN
)
1033 cur_func
= get_base_type(cur_func
);
1034 if (cur_func
== &void_ctype
)
1036 if (option_project
== PROJ_KERNEL
&&
1037 get_implied_rl(expr
, &rl
) &&
1038 rl_type(rl
) == &int_ctype
&&
1039 sval_is_negative(rl_min(rl
)) &&
1040 rl_max(rl
).value
== -1)
1042 if (!get_implied_value(expr
, &sval
))
1046 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
1051 int getting_address(struct expression
*expr
)
1053 int deref_count
= 0;
1055 while ((expr
= expr_get_parent_expr(expr
))) {
1056 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
1057 /* &foo->bar->baz dereferences "foo->bar" */
1058 if (deref_count
== 0)
1062 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
1068 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
1072 expr
= strip_expr(expr
);
1073 if (expr
->type
!= EXPR_DEREF
)
1078 sym
= get_type(expr
->deref
);
1081 if (sym
->type
== SYM_UNION
)
1086 *type
= sym
->ident
->name
;
1087 *member
= expr
->member
->name
;
1091 char *get_member_name(struct expression
*expr
)
1096 expr
= strip_expr(expr
);
1097 if (!expr
|| expr
->type
!= EXPR_DEREF
)
1102 sym
= get_type(expr
->deref
);
1105 if (sym
->type
== SYM_UNION
) {
1106 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
1107 sym
->ident
? sym
->ident
->name
: "anonymous",
1108 expr
->member
->name
);
1109 return alloc_string(buf
);
1112 struct expression
*deref
;
1117 * If we're in an anonymous struct then maybe we can find an
1118 * outer struct name to use as a name. This code should be
1119 * recursive and cleaner. I am not very proud of it.
1123 deref
= strip_parens(expr
->deref
);
1124 if (deref
->type
!= EXPR_DEREF
|| !deref
->member
)
1126 sym
= get_type(deref
->deref
);
1127 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
1130 full
= expr_to_str(expr
);
1133 deref
= deref
->deref
;
1134 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*')
1135 deref
= deref
->unop
;
1136 outer
= expr_to_str(deref
);
1141 len
= strlen(outer
);
1142 if (strncmp(outer
, full
, len
) != 0) {
1147 if (full
[len
] == '-' && full
[len
+ 1] == '>')
1149 if (full
[len
] == '.')
1151 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, full
+ len
);
1155 return alloc_string(buf
);
1157 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
1158 return alloc_string(buf
);
1161 int cmp_pos(struct position pos1
, struct position pos2
)
1163 /* the stream position is ... */
1164 if (pos1
.stream
> pos2
.stream
)
1166 if (pos1
.stream
< pos2
.stream
)
1169 if (pos1
.line
< pos2
.line
)
1171 if (pos1
.line
> pos2
.line
)
1174 if (pos1
.pos
< pos2
.pos
)
1176 if (pos1
.pos
> pos2
.pos
)
1182 int positions_eq(struct position pos1
, struct position pos2
)
1184 if (pos1
.line
!= pos2
.line
)
1186 if (pos1
.pos
!= pos2
.pos
)
1188 if (pos1
.stream
!= pos2
.stream
)
1193 struct statement
*get_current_statement(void)
1195 struct statement
*prev
, *tmp
;
1197 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
1199 if (!prev
|| !get_macro_name(prev
->pos
))
1202 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1203 if (positions_eq(tmp
->pos
, prev
->pos
))
1205 if (prev
->pos
.line
> tmp
->pos
.line
)
1208 } END_FOR_EACH_PTR_REVERSE(tmp
);
1212 struct statement
*get_prev_statement(void)
1214 struct statement
*tmp
;
1218 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1221 } END_FOR_EACH_PTR_REVERSE(tmp
);
1225 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
)
1227 struct statement
*stmt
;
1228 struct statement
*last_stmt
;
1230 while (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1232 if (expr
->type
!= EXPR_STATEMENT
)
1234 stmt
= expr
->statement
;
1237 if (stmt
->type
== STMT_COMPOUND
) {
1238 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1241 if (last_stmt
->type
== STMT_LABEL
)
1242 last_stmt
= last_stmt
->label_statement
;
1243 if (last_stmt
->type
!= STMT_EXPRESSION
)
1245 return last_stmt
->expression
;
1247 if (stmt
->type
== STMT_EXPRESSION
)
1248 return stmt
->expression
;
1252 int ms_since(struct timeval
*start
)
1257 gettimeofday(&end
, NULL
);
1258 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
1259 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
1263 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
1268 if (parent_is_err_or_null_var_sym(name
, sym
) ||
1269 parent_is_free_var_sym(name
, sym
))
1274 int parent_is_gone(struct expression
*expr
)
1280 expr
= strip_expr(expr
);
1281 var
= expr_to_var_sym(expr
, &sym
);
1284 ret
= parent_is_gone_var_sym(var
, sym
);
1290 int invert_op(int op
)
1301 case SPECIAL_LEFTSHIFT
:
1302 return SPECIAL_RIGHTSHIFT
;
1303 case SPECIAL_RIGHTSHIFT
:
1304 return SPECIAL_LEFTSHIFT
;
1309 int op_remove_assign(int op
)
1312 case SPECIAL_ADD_ASSIGN
:
1314 case SPECIAL_SUB_ASSIGN
:
1316 case SPECIAL_MUL_ASSIGN
:
1318 case SPECIAL_DIV_ASSIGN
:
1320 case SPECIAL_MOD_ASSIGN
:
1322 case SPECIAL_AND_ASSIGN
:
1324 case SPECIAL_OR_ASSIGN
:
1326 case SPECIAL_XOR_ASSIGN
:
1328 case SPECIAL_SHL_ASSIGN
:
1329 return SPECIAL_LEFTSHIFT
;
1330 case SPECIAL_SHR_ASSIGN
:
1331 return SPECIAL_RIGHTSHIFT
;
1337 int expr_equiv(struct expression
*one
, struct expression
*two
)
1339 struct symbol
*one_sym
= NULL
;
1340 struct symbol
*two_sym
= NULL
;
1341 char *one_name
= NULL
;
1342 char *two_name
= NULL
;
1349 if (one
->type
!= two
->type
)
1351 if (is_fake_call(one
) || is_fake_call(two
))
1354 one_name
= expr_to_str_sym(one
, &one_sym
);
1357 two_name
= expr_to_str_sym(two
, &two_sym
);
1360 if (one_sym
!= two_sym
)
1363 * This is a terrible hack because expr_to_str() sometimes gives up in
1364 * the middle and just returns what it has. If you see a () you know
1365 * the string is bogus.
1367 if (strstr(one_name
, "()"))
1369 if (strcmp(one_name
, two_name
) == 0)
1372 free_string(one_name
);
1373 free_string(two_name
);
1377 void push_int(struct int_stack
**stack
, int num
)
1382 * Just put the int on directly instead of a pointer to the int.
1383 * Shift it to the left because Sparse uses the last two bits.
1384 * This is sort of a dirty hack, yes.
1387 munged
= INT_PTR(num
<< 2);
1389 add_ptr_list(stack
, munged
);
1392 int pop_int(struct int_stack
**stack
)
1396 num
= last_ptr_list((struct ptr_list
*)*stack
);
1397 delete_ptr_list_last((struct ptr_list
**)stack
);
1399 return PTR_INT(num
) >> 2;
1402 bool token_to_ul(struct token
*token
, unsigned long *val
)
1406 /* this function only works for very specific simple defines */
1407 while (cnt
++ < 20 && token
) {
1408 switch (token_type(token
)) {
1410 if (macro_to_ul(show_ident(token
->ident
), val
))
1414 *val
= strtoul(token
->number
, NULL
, 0);
1417 token
= token
->next
;
1422 bool macro_to_ul(const char *macro
, unsigned long *val
)
1424 struct symbol
*macro_sym
;
1429 macro_sym
= lookup_macro_symbol(macro
);
1430 if (!macro_sym
|| !macro_sym
->expansion
)
1432 return token_to_ul(macro_sym
->expansion
, val
);
1435 int success_fail_return(struct range_list
*rl
)
1443 /* NFSv3 uses negative error codes such as -EIOCBQUEUED for success */
1444 if (rl_to_sval(rl
, &sval
) && sval
.value
== -529)
1447 // Negatives are a failure
1448 if (sval_is_negative(rl_max(rl
)))
1451 // NULL and error pointers are a failure
1452 if (type_is_ptr(rl_type(rl
)) && is_err_or_null(rl
))
1455 if (rl_to_sval(rl
, &sval
)) {
1456 if (sval
.value
== 0) {
1457 // Zero is normally success but false is a failure
1458 if (type_bits(sval
.type
) == 1)
1464 if (sval
.value
== 1 && type_bits(sval
.type
) == 1)
1469 if (strcmp(str
, "s32min-(-1),1-s32max") == 0)
1472 if (strcmp(str
, "0-s32max") == 0)