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;
141 * If you have "foo(a, b, 1);" then use
142 * get_argument_from_call_expr(expr, 0) to return the expression for
143 * a. Yes, it does start counting from 0.
145 struct expression
*get_argument_from_call_expr(struct expression_list
*args
,
148 struct expression
*expr
;
154 FOR_EACH_PTR(args
, expr
) {
158 } END_FOR_EACH_PTR(expr
);
162 struct expression
*get_array_expr(struct expression
*expr
)
164 struct expression
*parent
;
167 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
170 type
= get_type(expr
->left
);
173 if (type
->type
== SYM_ARRAY
)
175 if (type
->type
!= SYM_PTR
)
178 parent
= expr_get_parent_expr(expr
);
179 if (!parent
) /* Sometimes we haven't set up the ->parent yet. FIXME!! */
181 if (parent
->type
== EXPR_PREOP
&& parent
->op
== '*')
187 static struct expression
*strip_star_address(struct expression
*expr
)
189 struct expression
*unop
;
191 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '*')
193 unop
= strip_parens(expr
->unop
);
194 if (unop
->type
!= EXPR_PREOP
|| unop
->op
!= '&')
200 static struct expression
*strip_parens_symbol(struct expression
*expr
)
202 struct expression
*unop
;
204 if (expr
->type
!= EXPR_PREOP
|| expr
->op
!= '(')
206 unop
= strip_parens(expr
->unop
);
207 if (unop
->type
!= EXPR_SYMBOL
)
212 static int __get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
213 struct expression
*expr
, int off
, int len
,
219 /* can't happen on valid code */
224 expr
= strip_star_address(expr
);
225 expr
= strip_parens_symbol(expr
);
227 switch (expr
->type
) {
229 struct expression
*deref
;
235 /* this is pure guess work and nonsense programming */
236 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*') {
241 off
+= __get_variable_from_expr(sym_ptr
, buf
, deref
, off
, len
, complicated
);
244 off
+= append(buf
, off
, len
, "->");
246 off
+= append(buf
, off
, len
, ".");
249 off
+= append(buf
, off
, len
, "%s", expr
->member
->name
);
251 off
+= append(buf
, off
, len
, "unknown_member");
252 return off
- orig_off
;
255 if (expr
->symbol_name
)
256 off
+= append(buf
, off
, len
, "%s", expr
->symbol_name
->name
);
260 *sym_ptr
= expr
->symbol
;
262 return off
- orig_off
;
266 if (get_expression_statement(expr
)) {
270 if (expr
->op
== SPECIAL_DECREMENT
||
271 expr
->op
== SPECIAL_INCREMENT
)
274 if (expr
->op
== '*' && get_array_expr(expr
->unop
))
277 tmp
= show_special(expr
->op
);
279 off
+= append(buf
, off
, len
, "%s", tmp
);
280 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
,
283 off
+= append(buf
, off
, len
, ")");
285 return off
- orig_off
;
288 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->unop
, off
, len
,
290 off
+= append(buf
, off
, len
, "%s", show_special(expr
->op
));
292 if (expr
->op
== SPECIAL_DECREMENT
|| expr
->op
== SPECIAL_INCREMENT
)
294 return off
- orig_off
;
296 case EXPR_ASSIGNMENT
:
300 struct expression
*array_expr
;
303 array_expr
= get_array_expr(expr
);
305 off
+= __get_variable_from_expr(sym_ptr
, buf
, array_expr
, off
, len
, complicated
);
306 off
+= append(buf
, off
, len
, "[");
308 off
+= __get_variable_from_expr(sym_ptr
, buf
, expr
->left
, off
, len
, complicated
);
309 off
+= append(buf
, off
, len
, " %s ", show_special(expr
->op
));
311 off
+= __get_variable_from_expr(NULL
, buf
, expr
->right
, off
, len
, complicated
);
313 off
+= append(buf
, off
, len
, "]");
314 return off
- orig_off
;
320 if (!get_value(expr
, &sval
))
322 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
323 return off
- orig_off
;
329 if (!get_value(expr
, &sval
))
332 off
+= append(buf
, off
, len
, "%s", sval_to_numstr(sval
));
333 return off
- orig_off
;
336 off
+= append(buf
, off
, len
, "\"");
338 off
+= append(buf
, off
, len
, "%s", expr
->string
->data
);
339 off
+= append(buf
, off
, len
, "\"");
340 return off
- orig_off
;
342 struct expression
*tmp
;
346 off
+= __get_variable_from_expr(NULL
, buf
, expr
->fn
, off
, len
, complicated
);
347 off
+= append(buf
, off
, len
, "(");
349 FOR_EACH_PTR(expr
->args
, tmp
) {
351 off
+= append(buf
, off
, len
, ", ");
352 off
+= __get_variable_from_expr(NULL
, buf
, tmp
, off
, len
, complicated
);
353 } END_FOR_EACH_PTR(tmp
);
354 off
+= append(buf
, off
, len
, ")");
355 return off
- orig_off
;
358 case EXPR_FORCE_CAST
:
359 return __get_variable_from_expr(sym_ptr
, buf
,
360 expr
->cast_expression
, off
, len
,
366 if (expr
->cast_type
&& get_base_type(expr
->cast_type
)) {
367 size
= type_bytes(get_base_type(expr
->cast_type
));
368 off
+= append(buf
, off
, len
, "%d", size
);
369 } else if (get_value(expr
, &sval
)) {
370 off
+= append(buf
, off
, len
, "%s", sval_to_str(sval
));
372 return off
- orig_off
;
374 case EXPR_IDENTIFIER
:
376 if (expr
->expr_ident
)
377 off
+= append(buf
, off
, len
, "%s", expr
->expr_ident
->name
);
378 return off
- orig_off
;
380 case EXPR_CONDITIONAL
:
382 off
+= append(buf
, off
, len
, "(");
383 off
+= __get_variable_from_expr(NULL
, buf
, expr
->conditional
, off
, len
, complicated
);
384 off
+= append(buf
, off
, len
, ") ?");
386 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_true
, off
, len
, complicated
);
387 off
+= append(buf
, off
, len
, ":");
388 off
+= __get_variable_from_expr(NULL
, buf
, expr
->cond_false
, off
, len
, complicated
);
389 return off
- orig_off
;
391 off
+= append(buf
, off
, len
, "$expr_%p(%d)", expr
, expr
->type
);
392 return off
- orig_off
;
396 struct expr_str_cache_results
{
397 struct expression
*expr
;
403 static void get_variable_from_expr(struct symbol
**sym_ptr
, char *buf
,
404 struct expression
*expr
, int len
,
407 static struct expr_str_cache_results cached
[8];
408 struct symbol
*tmp_sym
= NULL
;
412 for (i
= 0; i
< ARRAY_SIZE(cached
); i
++) {
413 if (expr
== cached
[i
].expr
) {
414 strncpy(buf
, cached
[i
].str
, len
);
416 *sym_ptr
= cached
[i
].sym
;
417 *complicated
= cached
[i
].complicated
;
422 __get_variable_from_expr(&tmp_sym
, buf
, expr
, 0, len
, complicated
);
426 if (expr
->smatch_flags
& Tmp
)
429 cached
[idx
].expr
= expr
;
430 strncpy(cached
[idx
].str
, buf
, VAR_LEN
);
431 cached
[idx
].sym
= tmp_sym
;
432 cached
[idx
].complicated
= *complicated
;
434 idx
= (idx
+ 1) % ARRAY_SIZE(cached
);
438 * This is returns a stylized "c looking" representation of the
441 * It uses the same buffer every time so you have to save the result
442 * yourself if you want to keep it.
446 char *expr_to_str_sym(struct expression
*expr
, struct symbol
**sym_ptr
)
448 static char var_name
[VAR_LEN
];
457 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
460 return alloc_string(var_name
);
465 char *expr_to_str(struct expression
*expr
)
467 return expr_to_str_sym(expr
, NULL
);
471 * get_variable_from_expr_simple() only returns simple variables.
472 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
473 * then it returns NULL.
475 char *expr_to_var_sym(struct expression
*expr
,
476 struct symbol
**sym_ptr
)
478 static char var_name
[VAR_LEN
];
487 expr
= strip_expr(expr
);
488 get_variable_from_expr(sym_ptr
, var_name
, expr
, sizeof(var_name
),
496 return alloc_string(var_name
);
499 char *expr_to_var(struct expression
*expr
)
501 return expr_to_var_sym(expr
, NULL
);
504 struct symbol
*expr_to_sym(struct expression
*expr
)
509 name
= expr_to_var_sym(expr
, &sym
);
514 int get_complication_score(struct expression
*expr
)
516 expr
= strip_expr(expr
);
519 * Don't forget to keep get_complication_score() and store_all_links()
527 switch (expr
->type
) {
532 return get_complication_score(expr
->left
) +
533 get_complication_score(expr
->right
);
537 if (expr
->op
== '*' || expr
->op
== '(')
538 return get_complication_score(expr
->unop
);
541 return get_complication_score(expr
->deref
);
550 struct expression
*reorder_expr_alphabetically(struct expression
*expr
)
552 struct expression
*ret
;
555 if (expr
->type
!= EXPR_BINOP
)
557 if (expr
->op
!= '+' && expr
->op
!= '*')
560 left
= expr_to_var(expr
->left
);
561 right
= expr_to_var(expr
->right
);
565 if (strcmp(left
, right
) <= 0)
568 ret
= binop_expression(expr
->right
, expr
->op
, expr
->left
);
576 char *expr_to_chunk_helper(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
578 struct var_sym_list
*tmp_vsl
;
588 expr
= strip_parens(expr
);
592 name
= expr_to_var_sym(expr
, &tmp
);
597 add_var_sym(vsl
, name
, tmp
);
602 score
= get_complication_score(expr
);
603 if (score
<= 0 || score
> 2)
606 tmp_vsl
= expr_to_vsl(expr
);
613 if (ptr_list_size((struct ptr_list
*)tmp_vsl
) == 1) {
616 vs
= first_ptr_list((struct ptr_list
*)tmp_vsl
);
621 expr
= reorder_expr_alphabetically(expr
);
623 return expr_to_str(expr
);
626 char *expr_to_known_chunk_sym(struct expression
*expr
, struct symbol
**sym
)
628 return expr_to_chunk_helper(expr
, sym
, NULL
);
631 char *expr_to_chunk_sym_vsl(struct expression
*expr
, struct symbol
**sym
, struct var_sym_list
**vsl
)
633 return expr_to_chunk_helper(expr
, sym
, vsl
);
636 int sym_name_is(const char *name
, struct expression
*expr
)
640 if (expr
->type
!= EXPR_SYMBOL
)
642 if (!strcmp(expr
->symbol_name
->name
, name
))
647 int expr_is_zero(struct expression
*expr
)
651 if (get_value(expr
, &sval
) && sval
.value
== 0)
656 int is_array(struct expression
*expr
)
660 expr
= strip_expr(expr
);
664 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
665 expr
= strip_expr(expr
->unop
);
668 if (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
672 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
675 type
= get_type(expr
->left
);
676 if (!type
|| type
->type
!= SYM_ARRAY
)
682 struct expression
*get_array_base(struct expression
*expr
)
686 expr
= strip_expr(expr
);
687 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
688 expr
= strip_expr(expr
->unop
);
689 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
691 return strip_parens(expr
->left
);
694 struct expression
*get_array_offset(struct expression
*expr
)
698 expr
= strip_expr(expr
);
699 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
700 expr
= strip_expr(expr
->unop
);
701 if (expr
->type
!= EXPR_BINOP
|| expr
->op
!= '+')
703 expr
= strip_parens(expr
->right
);
704 if (expr
->type
== EXPR_POSTOP
)
705 expr
= strip_parens(expr
->unop
);
709 const char *show_state(struct smatch_state
*state
)
716 struct statement
*get_expression_statement(struct expression
*expr
)
718 /* What are those things called? if (({....; ret;})) { ...*/
720 if (expr
->type
!= EXPR_PREOP
)
726 if (expr
->unop
->type
!= EXPR_STATEMENT
)
728 if (expr
->unop
->statement
->type
!= STMT_COMPOUND
)
730 return expr
->unop
->statement
;
733 struct expression
*strip_parens(struct expression
*expr
)
738 if (expr
->type
== EXPR_PREOP
) {
740 return expr
; /* parsing invalid code */
742 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
743 expr
->unop
->statement
->type
== STMT_COMPOUND
)
746 return strip_parens(expr
->unop
);
751 static struct expression
*strip__builtin_choose_expr(struct expression
*expr
)
753 struct expression
*const_expr
, *expr1
, *expr2
;
756 if (!sym_name_is("__builtin_choose_expr", expr
->fn
))
759 const_expr
= get_argument_from_call_expr(expr
->args
, 0);
760 expr1
= get_argument_from_call_expr(expr
->args
, 1);
761 expr2
= get_argument_from_call_expr(expr
->args
, 2);
763 if (!get_value(const_expr
, &sval
) || !expr1
|| !expr2
)
767 return strip_expr(expr1
);
769 return strip_expr(expr2
);
772 static struct expression
*strip_expr_helper(struct expression
*expr
, bool set_parent
, bool cast
)
777 switch (expr
->type
) {
778 case EXPR_FORCE_CAST
:
781 expr_set_parent_expr(expr
->cast_expression
, expr
);
783 if (!expr
->cast_expression
)
788 type
= get_type(expr
->cast_expression
);
789 if (type
!= expr
->cast_type
)
792 return strip_expr_helper(expr
->cast_expression
, set_parent
, cast
);
794 struct expression
*unop
;
796 if (!expr
->unop
) /* parsing invalid code */
799 expr_set_parent_expr(expr
->unop
, expr
);
801 while (expr
->op
== '(' &&
802 expr
->unop
->type
== EXPR_PREOP
&&
803 expr
->unop
->op
== '(')
806 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
807 expr
->unop
->statement
->type
== STMT_COMPOUND
)
810 unop
= strip_expr_helper(expr
->unop
, set_parent
, cast
);
812 if (expr
->op
== '*' && unop
&&
813 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
814 struct symbol
*type
= get_type(unop
->unop
);
816 if (type
&& type
->type
== SYM_ARRAY
)
818 return strip_expr_helper(unop
->unop
, set_parent
, cast
);
826 case EXPR_CONDITIONAL
:
827 if (known_condition_true(expr
->conditional
)) {
828 if (expr
->cond_true
) {
830 expr_set_parent_expr(expr
->cond_true
, expr
);
831 return strip_expr_helper(expr
->cond_true
, set_parent
, cast
);
834 expr_set_parent_expr(expr
->conditional
, expr
);
835 return strip_expr_helper(expr
->conditional
, set_parent
, cast
);
837 if (known_condition_false(expr
->conditional
)) {
839 expr_set_parent_expr(expr
->cond_false
, expr
);
840 return strip_expr_helper(expr
->cond_false
, set_parent
, cast
);
844 if (sym_name_is("__builtin_expect", expr
->fn
) ||
845 sym_name_is("__builtin_bswap16", expr
->fn
) ||
846 sym_name_is("__builtin_bswap32", expr
->fn
) ||
847 sym_name_is("__builtin_bswap64", expr
->fn
)) {
848 expr
= get_argument_from_call_expr(expr
->args
, 0);
849 return strip_expr_helper(expr
, set_parent
, cast
);
851 if (sym_name_is("__builtin_choose_expr", expr
->fn
))
852 return strip__builtin_choose_expr(expr
);
858 struct expression
*strip_expr(struct expression
*expr
)
860 return strip_expr_helper(expr
, false, true);
863 struct expression
*strip_no_cast(struct expression
*expr
)
865 return strip_expr_helper(expr
, false, false);
868 struct expression
*strip_expr_set_parent(struct expression
*expr
)
870 return strip_expr_helper(expr
, true, true);
873 static void delete_state_tracker(struct tracker
*t
)
875 __delete_state(t
->owner
, t
->name
, t
->sym
);
879 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
883 t
= alloc_tracker(my_id
, name
, sym
);
884 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
887 int is_error_return(struct expression
*expr
)
889 struct symbol
*cur_func
= cur_func_sym
;
890 struct range_list
*rl
;
895 if (cur_func
->type
!= SYM_NODE
)
897 cur_func
= get_base_type(cur_func
);
898 if (cur_func
->type
!= SYM_FN
)
900 cur_func
= get_base_type(cur_func
);
901 if (cur_func
== &void_ctype
)
903 if (option_project
== PROJ_KERNEL
&&
904 get_implied_rl(expr
, &rl
) &&
905 rl_type(rl
) == &int_ctype
&&
906 sval_is_negative(rl_min(rl
)) &&
907 rl_max(rl
).value
== -1)
909 if (!get_implied_value(expr
, &sval
))
913 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
918 int getting_address(struct expression
*expr
)
922 while ((expr
= expr_get_parent_expr(expr
))) {
923 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
924 /* &foo->bar->baz dereferences "foo->bar" */
925 if (deref_count
== 0)
929 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
935 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
939 expr
= strip_expr(expr
);
940 if (expr
->type
!= EXPR_DEREF
)
945 sym
= get_type(expr
->deref
);
948 if (sym
->type
== SYM_UNION
)
953 *type
= sym
->ident
->name
;
954 *member
= expr
->member
->name
;
958 char *get_member_name(struct expression
*expr
)
963 expr
= strip_expr(expr
);
964 if (!expr
|| expr
->type
!= EXPR_DEREF
)
969 sym
= get_type(expr
->deref
);
972 if (sym
->type
== SYM_UNION
) {
973 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
974 sym
->ident
? sym
->ident
->name
: "anonymous",
976 return alloc_string(buf
);
979 struct expression
*deref
;
984 * If we're in an anonymous struct then maybe we can find an
985 * outer struct name to use as a name. This code should be
986 * recursive and cleaner. I am not very proud of it.
991 if (deref
->type
!= EXPR_DEREF
|| !deref
->member
)
993 sym
= get_type(deref
->deref
);
994 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
997 full
= expr_to_str(expr
);
1000 deref
= deref
->deref
;
1001 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*')
1002 deref
= deref
->unop
;
1003 outer
= expr_to_str(deref
);
1008 len
= strlen(outer
);
1009 if (strncmp(outer
, full
, len
) != 0) {
1014 if (full
[len
] == '-' && full
[len
+ 1] == '>')
1016 if (full
[len
] == '.')
1018 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, full
+ len
);
1022 return alloc_string(buf
);
1024 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
1025 return alloc_string(buf
);
1028 int cmp_pos(struct position pos1
, struct position pos2
)
1030 /* the stream position is ... */
1031 if (pos1
.stream
> pos2
.stream
)
1033 if (pos1
.stream
< pos2
.stream
)
1036 if (pos1
.line
< pos2
.line
)
1038 if (pos1
.line
> pos2
.line
)
1041 if (pos1
.pos
< pos2
.pos
)
1043 if (pos1
.pos
> pos2
.pos
)
1049 int positions_eq(struct position pos1
, struct position pos2
)
1051 if (pos1
.line
!= pos2
.line
)
1053 if (pos1
.pos
!= pos2
.pos
)
1055 if (pos1
.stream
!= pos2
.stream
)
1060 struct statement
*get_current_statement(void)
1062 struct statement
*prev
, *tmp
;
1064 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
1066 if (!prev
|| !get_macro_name(prev
->pos
))
1069 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1070 if (positions_eq(tmp
->pos
, prev
->pos
))
1072 if (prev
->pos
.line
> tmp
->pos
.line
)
1075 } END_FOR_EACH_PTR_REVERSE(tmp
);
1079 struct statement
*get_prev_statement(void)
1081 struct statement
*tmp
;
1085 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1088 } END_FOR_EACH_PTR_REVERSE(tmp
);
1092 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
)
1094 struct statement
*stmt
;
1095 struct statement
*last_stmt
;
1097 while (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1099 if (expr
->type
!= EXPR_STATEMENT
)
1101 stmt
= expr
->statement
;
1104 if (stmt
->type
== STMT_COMPOUND
) {
1105 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1108 if (last_stmt
->type
== STMT_LABEL
)
1109 last_stmt
= last_stmt
->label_statement
;
1110 if (last_stmt
->type
!= STMT_EXPRESSION
)
1112 return last_stmt
->expression
;
1114 if (stmt
->type
== STMT_EXPRESSION
)
1115 return stmt
->expression
;
1119 int ms_since(struct timeval
*start
)
1124 gettimeofday(&end
, NULL
);
1125 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
1126 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
1130 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
1135 if (parent_is_err_or_null_var_sym(name
, sym
) ||
1136 parent_is_free_var_sym(name
, sym
))
1141 int parent_is_gone(struct expression
*expr
)
1147 expr
= strip_expr(expr
);
1148 var
= expr_to_var_sym(expr
, &sym
);
1151 ret
= parent_is_gone_var_sym(var
, sym
);
1157 int invert_op(int op
)
1168 case SPECIAL_LEFTSHIFT
:
1169 return SPECIAL_RIGHTSHIFT
;
1170 case SPECIAL_RIGHTSHIFT
:
1171 return SPECIAL_LEFTSHIFT
;
1176 int op_remove_assign(int op
)
1179 case SPECIAL_ADD_ASSIGN
:
1181 case SPECIAL_SUB_ASSIGN
:
1183 case SPECIAL_MUL_ASSIGN
:
1185 case SPECIAL_DIV_ASSIGN
:
1187 case SPECIAL_MOD_ASSIGN
:
1189 case SPECIAL_AND_ASSIGN
:
1191 case SPECIAL_OR_ASSIGN
:
1193 case SPECIAL_XOR_ASSIGN
:
1195 case SPECIAL_SHL_ASSIGN
:
1196 return SPECIAL_LEFTSHIFT
;
1197 case SPECIAL_SHR_ASSIGN
:
1198 return SPECIAL_RIGHTSHIFT
;
1204 int expr_equiv(struct expression
*one
, struct expression
*two
)
1206 struct symbol
*one_sym
= NULL
;
1207 struct symbol
*two_sym
= NULL
;
1208 char *one_name
= NULL
;
1209 char *two_name
= NULL
;
1216 if (one
->type
!= two
->type
)
1218 if (is_fake_call(one
) || is_fake_call(two
))
1221 one_name
= expr_to_str_sym(one
, &one_sym
);
1224 two_name
= expr_to_str_sym(two
, &two_sym
);
1227 if (one_sym
!= two_sym
)
1230 * This is a terrible hack because expr_to_str() sometimes gives up in
1231 * the middle and just returns what it has. If you see a () you know
1232 * the string is bogus.
1234 if (strstr(one_name
, "()"))
1236 if (strcmp(one_name
, two_name
) == 0)
1239 free_string(one_name
);
1240 free_string(two_name
);
1244 void push_int(struct int_stack
**stack
, int num
)
1249 * Just put the int on directly instead of a pointer to the int.
1250 * Shift it to the left because Sparse uses the last two bits.
1251 * This is sort of a dirty hack, yes.
1254 munged
= INT_PTR(num
<< 2);
1256 add_ptr_list(stack
, munged
);
1259 int pop_int(struct int_stack
**stack
)
1263 num
= last_ptr_list((struct ptr_list
*)*stack
);
1264 delete_ptr_list_last((struct ptr_list
**)stack
);
1266 return PTR_INT(num
) >> 2;
1269 bool token_to_ul(struct token
*token
, unsigned long *val
)
1273 /* this function only works for very specific simple defines */
1274 while (cnt
++ < 20 && token
) {
1275 switch (token_type(token
)) {
1277 if (macro_to_ul(show_ident(token
->ident
), val
))
1281 *val
= strtoul(token
->number
, NULL
, 0);
1284 token
= token
->next
;
1289 bool macro_to_ul(const char *macro
, unsigned long *val
)
1291 struct symbol
*macro_sym
;
1296 macro_sym
= lookup_macro_symbol(macro
);
1297 if (!macro_sym
|| !macro_sym
->expansion
)
1299 return token_to_ul(macro_sym
->expansion
, val
);
1302 int success_fail_return(struct range_list
*rl
)
1310 // Negatives are a failure
1311 if (sval_is_negative(rl_max(rl
)))
1314 // NULL and error pointers are a failure
1315 if (type_is_ptr(rl_type(rl
)) && is_err_or_null(rl
))
1318 if (rl_to_sval(rl
, &sval
)) {
1319 if (sval
.value
== 0) {
1320 // Zero is normally success but false is a failure
1321 if (type_bits(sval
.type
) == 1)
1327 if (sval
.value
== 1 && type_bits(sval
.type
) == 1)
1332 if (strcmp(str
, "s32min-(-1),1-s32max") == 0)