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 char *alloc_string_newline(const char *str
)
50 tmp
= malloc(len
+ 2);
51 snprintf(tmp
, len
+ 2, "%s\n", str
);
55 void free_string(char *str
)
60 void remove_parens(char *str
)
65 while (*src
!= '\0') {
66 if (*src
== '(' || *src
== ')') {
75 struct smatch_state
*alloc_state_num(int num
)
77 struct smatch_state
*state
;
78 static char buff
[256];
80 state
= __alloc_smatch_state(0);
81 snprintf(buff
, 255, "%d", num
);
83 state
->name
= alloc_string(buff
);
84 state
->data
= INT_PTR(num
);
88 struct smatch_state
*alloc_state_str(const char *name
)
90 struct smatch_state
*state
;
92 state
= __alloc_smatch_state(0);
93 state
->name
= alloc_string(name
);
97 struct smatch_state
*merge_str_state(struct smatch_state
*s1
, struct smatch_state
*s2
)
99 if (!s1
->name
|| !s2
->name
)
101 if (strcmp(s1
->name
, s2
->name
) == 0)
106 struct smatch_state
*alloc_state_expr(struct expression
*expr
)
108 struct smatch_state
*state
;
111 expr
= strip_expr(expr
);
112 name
= expr_to_str(expr
);
116 state
= __alloc_smatch_state(0);
117 state
->name
= alloc_sname(name
);
123 static int FORMAT_ATTR(4) append(char *dest
, int off
, int len
, const char *fmt
, ...)
128 if (len
<= 0 || off
< 0 || off
>= len
- 1)
132 n
= vsnprintf(dest
+ off
, len
- off
, fmt
, args
);
135 if (n
> len
- off
- 1)
136 return len
- off
- 1;
140 struct expression
*get_assigned_call(struct expression
*expr
)
142 while (expr
&& expr
->type
== EXPR_ASSIGNMENT
)
143 expr
= strip_expr(expr
->right
);
144 if (!expr
|| expr
->type
!= EXPR_CALL
)
150 * If you have "foo(a, b, 1);" then use
151 * get_argument_from_call_expr(expr, 0) to return the expression for
152 * a. Yes, it does start counting from 0.
154 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
157 struct expression
*expr
;
163 FOR_EACH_PTR(args
, expr
) {
167 } END_FOR_EACH_PTR(expr
);
171 struct expression
*get_array_expr(struct expression
*expr
)
173 struct expression
*parent
;
176 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
179 type
= get_type(expr
->left
);
182 if (type
->type
== SYM_ARRAY
)
184 if (type
->type
!= SYM_PTR
)
187 parent
= expr_get_parent_expr(expr
);
188 if (!parent
) /* Sometimes we haven't set up the ->parent yet. FIXME!! */
190 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
196 static struct expression
*strip_star_address(struct expression
*expr
)
198 struct expression
*unop
;
200 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
202 unop
= strip_parens(expr
->unop
);
203 if (unop
->type
!= EXPR_PREOP
|| unop
->op
!= '&')
209 static struct expression
*strip_parens_symbol(struct expression
*expr
)
211 struct expression
*unop
;
213 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '(')
216 * This should probably be strip_parens() but expr_to_str() doesn't
217 * print casts so we may as well strip those too. In other words,
218 * instead of fixing the code to print the cast, it's easier to just
219 * write even more code that relies on the bug. ;)
221 unop
= strip_expr(expr
->unop
);
222 if (unop
->type
!= EXPR_SYMBOL
)
227 static int __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
228 struct expression
*expr
, int off
, int len
,
234 /* can't happen on valid code */
239 expr
= strip_star_address(expr
);
240 expr
= strip_parens_symbol(expr
);
242 switch (expr
->type
) {
244 struct expression
*deref
;
250 /* this is pure guess work and nonsense programming */
251 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*') {
256 off
+= __get_variable_from_expr(sym_ptr
, buf
, deref
, off
, len
, complicated
);
259 off
+= append(buf
, off
, len
, "->");
261 off
+= append(buf
, off
, len
, ".");
264 off
+= append(buf
, off
, len
, "%s", expr
->member
->name
);
266 off
+= append(buf
, off
, len
, "unknown_member");
267 return off
- orig_off
;
270 if (expr
->symbol_name
)
271 off
+= append(buf
, off
, len
, "%s", expr
->symbol_name
->name
);
275 *sym_ptr
= expr
->symbol
;
277 return off
- orig_off
;
281 if (get_expression_statement(expr
)) {
285 if (expr
->op
== SPECIAL_DECREMENT
||
286 expr
->op
== SPECIAL_INCREMENT
)
289 if (expr
->op
== '*' && get_array_expr(expr
->unop
))
292 tmp
= show_special(expr
->op
);
294 off
+= append(buf
, off
, len
, "%s", tmp
);
295 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
,
298 off
+= append(buf
, off
, len
, ")");
300 return off
- orig_off
;
303 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
, len
,
305 off
+= append(buf
, off
, len
, "%s", show_special(expr
->op
));
307 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
309 return off
- orig_off
;
311 case EXPR_ASSIGNMENT
:
315 struct expression
*array_expr
;
318 array_expr
= get_array_expr(expr
);
320 off
+= __get_variable_from_expr(sym_ptr
, buf
, array_expr
, off
, len
, complicated
);
321 off
+= append(buf
, off
, len
, "[");
323 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, off
, len
, complicated
);
324 off
+= append(buf
, off
, len
, " %s ", show_special(expr
->op
));
326 off
+= __get_variable_from_expr(NULL
, buf
, expr
->right
, off
, len
, complicated
);
328 off
+= append(buf
, off
, len
, "]");
329 return off
- orig_off
;
335 if (!get_value(expr
, &sval
))
337 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
338 return off
- orig_off
;
344 if (!get_value(expr
, &sval
))
347 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
348 return off
- orig_off
;
351 off
+= append(buf
, off
, len
, "\"");
353 off
+= append(buf
, off
, len
, "%s", expr
->string
->data
);
354 off
+= append(buf
, off
, len
, "\"");
355 return off
- orig_off
;
357 struct expression
*tmp
;
361 off
+= __get_variable_from_expr(NULL
, buf
, expr
->fn
, off
, len
, complicated
);
362 off
+= append(buf
, off
, len
, "(");
364 FOR_EACH_PTR(expr
->args
, tmp
) {
366 off
+= append(buf
, off
, len
, ", ");
367 off
+= __get_variable_from_expr(NULL
, buf
, tmp
, off
, len
, complicated
);
368 } END_FOR_EACH_PTR(tmp
);
369 off
+= append(buf
, off
, len
, ")");
370 return off
- orig_off
;
373 case EXPR_FORCE_CAST
:
374 return __get_variable_from_expr(sym_ptr
, buf
,
375 expr
->cast_expression
, off
, len
,
381 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
382 size
= type_bytes(get_base_type(expr
->cast_type
));
383 off
+= append(buf
, off
, len
, "%d", size
);
384 } else if (get_value(expr
, &sval
)) {
385 off
+= append(buf
, off
, len
, "%s", sval_to_str(sval
));
387 return off
- orig_off
;
389 case EXPR_IDENTIFIER
:
391 if (expr
->expr_ident
)
392 off
+= append(buf
, off
, len
, "%s", expr
->expr_ident
->name
);
393 return off
- orig_off
;
395 case EXPR_CONDITIONAL
:
397 off
+= append(buf
, off
, len
, "(");
398 off
+= __get_variable_from_expr(NULL
, buf
, expr
->conditional
, off
, len
, complicated
);
399 off
+= append(buf
, off
, len
, ") ?");
401 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_true
, off
, len
, complicated
);
402 off
+= append(buf
, off
, len
, ":");
403 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_false
, off
, len
, complicated
);
404 return off
- orig_off
;
406 off
+= append(buf
, off
, len
, "$expr_%p(%d)", expr
, expr
->type
);
407 return off
- orig_off
;
411 struct expr_str_cache_results
{
412 struct expression
*expr
;
418 static void get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
419 struct expression
*expr
, int len
,
422 static struct expr_str_cache_results cached
[8];
423 struct symbol
*tmp_sym
= NULL
;
427 for (i
= 0; i
< ARRAY_SIZE(cached
); i
++) {
428 if (expr
== cached
[i
].expr
) {
429 strncpy(buf
, cached
[i
].str
, len
);
431 *sym_ptr
= cached
[i
].sym
;
432 *complicated
= cached
[i
].complicated
;
437 __get_variable_from_expr(&tmp_sym
, buf
, expr
, 0, len
, complicated
);
441 if (expr
->smatch_flags
& Tmp
)
444 cached
[idx
].expr
= expr
;
445 strncpy(cached
[idx
].str
, buf
, VAR_LEN
);
446 cached
[idx
].sym
= tmp_sym
;
447 cached
[idx
].complicated
= *complicated
;
449 idx
= (idx
+ 1) % ARRAY_SIZE(cached
);
453 * This is returns a stylized "c looking" representation of the
456 * It uses the same buffer every time so you have to save the result
457 * yourself if you want to keep it.
461 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
463 static char var_name
[VAR_LEN
];
472 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
475 return alloc_string(var_name
);
480 char *expr_to_str(struct expression
*expr
)
482 return expr_to_str_sym(expr
, NULL
);
486 * get_variable_from_expr_simple() only returns simple variables.
487 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
488 * then it returns NULL.
490 char *expr_to_var_sym(struct expression
*expr
,
491 struct symbol
**sym_ptr
)
493 static char var_name
[VAR_LEN
];
502 expr
= strip_expr(expr
);
503 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
511 return alloc_string(var_name
);
514 char *expr_to_var(struct expression
*expr
)
516 return expr_to_var_sym(expr
, NULL
);
519 struct symbol
*expr_to_sym(struct expression
*expr
)
524 name
= expr_to_var_sym(expr
, &sym
);
529 int get_complication_score(struct expression
*expr
)
531 expr
= strip_expr(expr
);
534 * Don't forget to keep get_complication_score() and store_all_links()
542 switch (expr
->type
) {
547 return get_complication_score(expr
->left
) +
548 get_complication_score(expr
->right
);
552 if (expr
->op
== '*' || expr
->op
== '(')
553 return get_complication_score(expr
->unop
);
556 return get_complication_score(expr
->deref
);
565 struct expression
*reorder_expr_alphabetically(struct expression
*expr
)
567 struct expression
*ret
;
570 if (expr
->type
!= EXPR_BINOP
)
572 if (expr
->op
!= '+' && expr
->op
!= '*')
575 left
= expr_to_var(expr
->left
);
576 right
= expr_to_var(expr
->right
);
580 if (strcmp(left
, right
) <= 0)
583 ret
= binop_expression(expr
->right
, expr
->op
, expr
->left
);
591 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
593 struct var_sym_list
*tmp_vsl
;
603 expr
= strip_parens(expr
);
607 name
= expr_to_var_sym(expr
, &tmp
);
612 add_var_sym(vsl
, name
, tmp
);
617 score
= get_complication_score(expr
);
618 if (score
<= 0 || score
> 2)
621 tmp_vsl
= expr_to_vsl(expr
);
628 if (ptr_list_size((struct ptr_list
*)tmp_vsl
) == 1) {
631 vs
= first_ptr_list((struct ptr_list
*)tmp_vsl
);
636 expr
= reorder_expr_alphabetically(expr
);
638 return expr_to_str(expr
);
641 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
643 return expr_to_chunk_helper(expr
, sym
, NULL
);
646 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
648 return expr_to_chunk_helper(expr
, sym
, vsl
);
651 int sym_name_is(const char *name
, struct expression
*expr
)
655 if (expr
->type
!= EXPR_SYMBOL
)
657 if (!strcmp(expr
->symbol_name
->name
, name
))
662 int expr_is_zero(struct expression
*expr
)
666 if (get_value(expr
, &sval
) && sval
.value
== 0)
671 int is_array(struct expression
*expr
)
675 expr
= strip_expr(expr
);
679 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
680 expr
= strip_expr(expr
->unop
);
683 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
687 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
690 type
= get_type(expr
->left
);
691 if (!type
|| type
->type
!= SYM_ARRAY
)
697 struct expression
*get_array_base(struct expression
*expr
)
701 expr
= strip_expr(expr
);
702 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
703 expr
= strip_expr(expr
->unop
);
704 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
706 return strip_parens(expr
->left
);
709 struct expression
*get_array_offset(struct expression
*expr
)
713 expr
= strip_expr(expr
);
714 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
715 expr
= strip_expr(expr
->unop
);
716 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
718 expr
= strip_parens(expr
->right
);
719 if (expr
->type
== EXPR_POSTOP
)
720 expr
= strip_parens(expr
->unop
);
724 const char *show_state(struct smatch_state
*state
)
731 struct statement
*get_expression_statement(struct expression
*expr
)
733 /* What are those things called? if (({....; ret;})) { ...*/
735 if (expr
->type
!= EXPR_PREOP
)
741 if (expr
->unop
->type
!= EXPR_STATEMENT
)
743 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
745 return expr
->unop
->statement
;
748 struct expression
*strip_parens(struct expression
*expr
)
753 if (expr
->type
== EXPR_PREOP
) {
755 return expr
; /* parsing invalid code */
757 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
758 expr
->unop
->statement
->type
== STMT_COMPOUND
)
761 return strip_parens(expr
->unop
);
766 struct expression
*strip__builtin_choose_expr(struct expression
*expr
)
768 struct expression
*const_expr
, *expr1
, *expr2
;
771 if (expr
->type
!= EXPR_CALL
)
774 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
777 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
778 expr1
= get_argument_from_call_expr(expr
->args
, 1);
779 expr2
= get_argument_from_call_expr(expr
->args
, 2);
781 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
785 return strip_expr(expr1
);
787 return strip_expr(expr2
);
790 struct expression
*strip_Generic(struct expression
*expr
)
792 struct type_expression
*map
;
793 struct symbol
*type
, *tmp
;
795 if (!expr
|| expr
->type
!= EXPR_GENERIC
)
798 type
= get_type(expr
->control
);
800 for (map
= expr
->map
; map
; map
= map
->next
) {
801 tmp
= get_real_base_type(map
->type
);
802 if (!types_equiv(type
, tmp
))
815 static struct expression
*strip_expr_helper(struct expression
*expr
, bool set_parent
, bool cast
)
820 switch (expr
->type
) {
821 case EXPR_FORCE_CAST
:
824 expr_set_parent_expr(expr
->cast_expression
, expr
);
826 if (!expr
->cast_expression
)
831 type
= get_type(expr
->cast_expression
);
832 if (type
!= expr
->cast_type
)
835 return strip_expr_helper(expr
->cast_expression
, set_parent
, cast
);
837 struct expression
*unop
;
839 if (!expr
->unop
) /* parsing invalid code */
842 expr_set_parent_expr(expr
->unop
, expr
);
844 while (expr
->op
== '(' &&
845 expr
->unop
->type
== EXPR_PREOP
&&
846 expr
->unop
->op
== '(')
849 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
850 expr
->unop
->statement
->type
== STMT_COMPOUND
)
853 unop
= strip_expr_helper(expr
->unop
, set_parent
, cast
);
855 if (expr
->op
== '*' && unop
&&
856 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
857 struct symbol
*type
= get_type(unop
->unop
);
859 if (type
&& type
->type
== SYM_ARRAY
)
861 return strip_expr_helper(unop
->unop
, set_parent
, cast
);
869 case EXPR_CONDITIONAL
:
870 if (known_condition_true(expr
->conditional
)) {
871 if (expr
->cond_true
) {
873 expr_set_parent_expr(expr
->cond_true
, expr
);
874 return strip_expr_helper(expr
->cond_true
, set_parent
, cast
);
877 expr_set_parent_expr(expr
->conditional
, expr
);
878 return strip_expr_helper(expr
->conditional
, set_parent
, cast
);
880 if (known_condition_false(expr
->conditional
)) {
882 expr_set_parent_expr(expr
->cond_false
, expr
);
883 return strip_expr_helper(expr
->cond_false
, set_parent
, cast
);
887 if (sym_name_is("__builtin_expect", expr
->fn
) ||
888 sym_name_is("__builtin_bswap16", expr
->fn
) ||
889 sym_name_is("__builtin_bswap32", expr
->fn
) ||
890 sym_name_is("__builtin_bswap64", expr
->fn
)) {
891 expr
= get_argument_from_call_expr(expr
->args
, 0);
892 return strip_expr_helper(expr
, set_parent
, cast
);
894 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
895 return strip__builtin_choose_expr(expr
);
898 return strip_Generic(expr
);
903 struct expression
*strip_expr(struct expression
*expr
)
905 return strip_expr_helper(expr
, false, true);
908 struct expression
*strip_no_cast(struct expression
*expr
)
910 return strip_expr_helper(expr
, false, false);
913 struct expression
*strip_expr_set_parent(struct expression
*expr
)
915 return strip_expr_helper(expr
, true, true);
918 static void delete_state_tracker(struct tracker
*t
)
920 __delete_state(t
->owner
, t
->name
, t
->sym
);
924 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
928 t
= alloc_tracker(my_id
, name
, sym
);
929 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
932 int is_error_return(struct expression
*expr
)
934 struct symbol
*cur_func
= cur_func_sym
;
935 struct range_list
*rl
;
940 if (cur_func
->type
!= SYM_NODE
)
942 cur_func
= get_base_type(cur_func
);
943 if (cur_func
->type
!= SYM_FN
)
945 cur_func
= get_base_type(cur_func
);
946 if (cur_func
== &void_ctype
)
948 if (option_project
== PROJ_KERNEL
&&
949 get_implied_rl(expr
, &rl
) &&
950 rl_type(rl
) == &int_ctype
&&
951 sval_is_negative(rl_min(rl
)) &&
952 rl_max(rl
).value
== -1)
954 if (!get_implied_value(expr
, &sval
))
958 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
963 int getting_address(struct expression
*expr
)
967 while ((expr
= expr_get_parent_expr(expr
))) {
968 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
969 /* &foo->bar->baz dereferences "foo->bar" */
970 if (deref_count
== 0)
974 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
980 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
984 expr
= strip_expr(expr
);
985 if (expr
->type
!= EXPR_DEREF
)
990 sym
= get_type(expr
->deref
);
993 if (sym
->type
== SYM_UNION
)
998 *type
= sym
->ident
->name
;
999 *member
= expr
->member
->name
;
1003 char *get_member_name(struct expression
*expr
)
1008 expr
= strip_expr(expr
);
1009 if (!expr
|| expr
->type
!= EXPR_DEREF
)
1014 sym
= get_type(expr
->deref
);
1017 if (sym
->type
== SYM_UNION
) {
1018 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
1019 sym
->ident
? sym
->ident
->name
: "anonymous",
1020 expr
->member
->name
);
1021 return alloc_string(buf
);
1024 struct expression
*deref
;
1029 * If we're in an anonymous struct then maybe we can find an
1030 * outer struct name to use as a name. This code should be
1031 * recursive and cleaner. I am not very proud of it.
1035 deref
= strip_parens(expr
->deref
);
1036 if (deref
->type
!= EXPR_DEREF
|| !deref
->member
)
1038 sym
= get_type(deref
->deref
);
1039 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
1042 full
= expr_to_str(expr
);
1045 deref
= deref
->deref
;
1046 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*')
1047 deref
= deref
->unop
;
1048 outer
= expr_to_str(deref
);
1053 len
= strlen(outer
);
1054 if (strncmp(outer
, full
, len
) != 0) {
1059 if (full
[len
] == '-' && full
[len
+ 1] == '>')
1061 if (full
[len
] == '.')
1063 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, full
+ len
);
1067 return alloc_string(buf
);
1069 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
1070 return alloc_string(buf
);
1073 int cmp_pos(struct position pos1
, struct position pos2
)
1075 /* the stream position is ... */
1076 if (pos1
.stream
> pos2
.stream
)
1078 if (pos1
.stream
< pos2
.stream
)
1081 if (pos1
.line
< pos2
.line
)
1083 if (pos1
.line
> pos2
.line
)
1086 if (pos1
.pos
< pos2
.pos
)
1088 if (pos1
.pos
> pos2
.pos
)
1094 int positions_eq(struct position pos1
, struct position pos2
)
1096 if (pos1
.line
!= pos2
.line
)
1098 if (pos1
.pos
!= pos2
.pos
)
1100 if (pos1
.stream
!= pos2
.stream
)
1105 struct statement
*get_current_statement(void)
1107 struct statement
*prev
, *tmp
;
1109 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
1111 if (!prev
|| !get_macro_name(prev
->pos
))
1114 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1115 if (positions_eq(tmp
->pos
, prev
->pos
))
1117 if (prev
->pos
.line
> tmp
->pos
.line
)
1120 } END_FOR_EACH_PTR_REVERSE(tmp
);
1124 struct statement
*get_prev_statement(void)
1126 struct statement
*tmp
;
1130 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1133 } END_FOR_EACH_PTR_REVERSE(tmp
);
1137 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
)
1139 struct statement
*stmt
;
1140 struct statement
*last_stmt
;
1142 while (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1144 if (expr
->type
!= EXPR_STATEMENT
)
1146 stmt
= expr
->statement
;
1149 if (stmt
->type
== STMT_COMPOUND
) {
1150 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1153 if (last_stmt
->type
== STMT_LABEL
)
1154 last_stmt
= last_stmt
->label_statement
;
1155 if (last_stmt
->type
!= STMT_EXPRESSION
)
1157 return last_stmt
->expression
;
1159 if (stmt
->type
== STMT_EXPRESSION
)
1160 return stmt
->expression
;
1164 int ms_since(struct timeval
*start
)
1169 gettimeofday(&end
, NULL
);
1170 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
1171 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
1175 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
1180 if (parent_is_err_or_null_var_sym(name
, sym
) ||
1181 parent_is_free_var_sym(name
, sym
))
1186 int parent_is_gone(struct expression
*expr
)
1192 expr
= strip_expr(expr
);
1193 var
= expr_to_var_sym(expr
, &sym
);
1196 ret
= parent_is_gone_var_sym(var
, sym
);
1202 int invert_op(int op
)
1213 case SPECIAL_LEFTSHIFT
:
1214 return SPECIAL_RIGHTSHIFT
;
1215 case SPECIAL_RIGHTSHIFT
:
1216 return SPECIAL_LEFTSHIFT
;
1221 int op_remove_assign(int op
)
1224 case SPECIAL_ADD_ASSIGN
:
1226 case SPECIAL_SUB_ASSIGN
:
1228 case SPECIAL_MUL_ASSIGN
:
1230 case SPECIAL_DIV_ASSIGN
:
1232 case SPECIAL_MOD_ASSIGN
:
1234 case SPECIAL_AND_ASSIGN
:
1236 case SPECIAL_OR_ASSIGN
:
1238 case SPECIAL_XOR_ASSIGN
:
1240 case SPECIAL_SHL_ASSIGN
:
1241 return SPECIAL_LEFTSHIFT
;
1242 case SPECIAL_SHR_ASSIGN
:
1243 return SPECIAL_RIGHTSHIFT
;
1249 int expr_equiv(struct expression
*one
, struct expression
*two
)
1251 struct symbol
*one_sym
= NULL
;
1252 struct symbol
*two_sym
= NULL
;
1253 char *one_name
= NULL
;
1254 char *two_name
= NULL
;
1261 if (one
->type
!= two
->type
)
1263 if (is_fake_call(one
) || is_fake_call(two
))
1266 one_name
= expr_to_str_sym(one
, &one_sym
);
1269 two_name
= expr_to_str_sym(two
, &two_sym
);
1272 if (one_sym
!= two_sym
)
1275 * This is a terrible hack because expr_to_str() sometimes gives up in
1276 * the middle and just returns what it has. If you see a () you know
1277 * the string is bogus.
1279 if (strstr(one_name
, "()"))
1281 if (strcmp(one_name
, two_name
) == 0)
1284 free_string(one_name
);
1285 free_string(two_name
);
1289 void push_int(struct int_stack
**stack
, int num
)
1294 * Just put the int on directly instead of a pointer to the int.
1295 * Shift it to the left because Sparse uses the last two bits.
1296 * This is sort of a dirty hack, yes.
1299 munged
= INT_PTR(num
<< 2);
1301 add_ptr_list(stack
, munged
);
1304 int pop_int(struct int_stack
**stack
)
1308 num
= last_ptr_list((struct ptr_list
*)*stack
);
1309 delete_ptr_list_last((struct ptr_list
**)stack
);
1311 return PTR_INT(num
) >> 2;
1314 bool token_to_ul(struct token
*token
, unsigned long *val
)
1318 /* this function only works for very specific simple defines */
1319 while (cnt
++ < 20 && token
) {
1320 switch (token_type(token
)) {
1322 if (macro_to_ul(show_ident(token
->ident
), val
))
1326 *val
= strtoul(token
->number
, NULL
, 0);
1329 token
= token
->next
;
1334 bool macro_to_ul(const char *macro
, unsigned long *val
)
1336 struct symbol
*macro_sym
;
1341 macro_sym
= lookup_macro_symbol(macro
);
1342 if (!macro_sym
|| !macro_sym
->expansion
)
1344 return token_to_ul(macro_sym
->expansion
, val
);
1347 int success_fail_return(struct range_list
*rl
)
1355 /* NFSv3 uses negative error codes such as -EIOCBQUEUED for success */
1356 if (rl_to_sval(rl
, &sval
) && sval
.value
== -529)
1359 // Negatives are a failure
1360 if (sval_is_negative(rl_max(rl
)))
1363 // NULL and error pointers are a failure
1364 if (type_is_ptr(rl_type(rl
)) && is_err_or_null(rl
))
1367 if (rl_to_sval(rl
, &sval
)) {
1368 if (sval
.value
== 0) {
1369 // Zero is normally success but false is a failure
1370 if (type_bits(sval
.type
) == 1)
1376 if (sval
.value
== 1 && type_bits(sval
.type
) == 1)
1381 if (strcmp(str
, "s32min-(-1),1-s32max") == 0)
1384 if (strcmp(str
, "0-s32max") == 0)