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_expr_helper(struct expression
*expr
, bool set_parent
)
756 switch (expr
->type
) {
757 case EXPR_FORCE_CAST
:
760 expr_set_parent_expr(expr
->cast_expression
, expr
);
762 if (!expr
->cast_expression
)
764 return strip_expr_helper(expr
->cast_expression
, set_parent
);
766 struct expression
*unop
;
768 if (!expr
->unop
) /* parsing invalid code */
771 expr_set_parent_expr(expr
->unop
, expr
);
774 if (expr
->op
== '(' && expr
->unop
->type
== EXPR_STATEMENT
&&
775 expr
->unop
->statement
->type
== STMT_COMPOUND
)
778 unop
= strip_expr_helper(expr
->unop
, set_parent
);
780 if (expr
->op
== '*' && unop
&&
781 unop
->type
== EXPR_PREOP
&& unop
->op
== '&') {
782 struct symbol
*type
= get_type(unop
->unop
);
784 if (type
&& type
->type
== SYM_ARRAY
)
786 return strip_expr_helper(unop
->unop
, set_parent
);
794 case EXPR_CONDITIONAL
:
795 if (known_condition_true(expr
->conditional
)) {
796 if (expr
->cond_true
) {
798 expr_set_parent_expr(expr
->cond_true
, expr
);
799 return strip_expr_helper(expr
->cond_true
, set_parent
);
802 expr_set_parent_expr(expr
->conditional
, expr
);
803 return strip_expr_helper(expr
->conditional
, set_parent
);
805 if (known_condition_false(expr
->conditional
)) {
807 expr_set_parent_expr(expr
->cond_false
, expr
);
808 return strip_expr_helper(expr
->cond_false
, set_parent
);
812 if (sym_name_is("__builtin_expect", expr
->fn
) ||
813 sym_name_is("__builtin_bswap16", expr
->fn
) ||
814 sym_name_is("__builtin_bswap32", expr
->fn
) ||
815 sym_name_is("__builtin_bswap64", expr
->fn
)) {
816 expr
= get_argument_from_call_expr(expr
->args
, 0);
817 return strip_expr_helper(expr
, set_parent
);
824 struct expression
*strip_expr(struct expression
*expr
)
826 return strip_expr_helper(expr
, false);
829 struct expression
*strip_expr_set_parent(struct expression
*expr
)
831 return strip_expr_helper(expr
, true);
834 static void delete_state_tracker(struct tracker
*t
)
836 __delete_state(t
->owner
, t
->name
, t
->sym
);
840 void scoped_state(int my_id
, const char *name
, struct symbol
*sym
)
844 t
= alloc_tracker(my_id
, name
, sym
);
845 add_scope_hook((scope_hook
*)&delete_state_tracker
, t
);
848 int is_error_return(struct expression
*expr
)
850 struct symbol
*cur_func
= cur_func_sym
;
851 struct range_list
*rl
;
856 if (cur_func
->type
!= SYM_NODE
)
858 cur_func
= get_base_type(cur_func
);
859 if (cur_func
->type
!= SYM_FN
)
861 cur_func
= get_base_type(cur_func
);
862 if (cur_func
== &void_ctype
)
864 if (option_project
== PROJ_KERNEL
&&
865 get_implied_rl(expr
, &rl
) &&
866 rl_type(rl
) == &int_ctype
&&
867 sval_is_negative(rl_min(rl
)) &&
868 rl_max(rl
).value
== -1)
870 if (!get_implied_value(expr
, &sval
))
874 if (cur_func
->type
== SYM_PTR
&& sval
.value
== 0)
879 int getting_address(struct expression
*expr
)
883 while ((expr
= expr_get_parent_expr(expr
))) {
884 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') {
885 /* &foo->bar->baz dereferences "foo->bar" */
886 if (deref_count
== 0)
890 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&')
896 int get_struct_and_member(struct expression
*expr
, const char **type
, const char **member
)
900 expr
= strip_expr(expr
);
901 if (expr
->type
!= EXPR_DEREF
)
906 sym
= get_type(expr
->deref
);
909 if (sym
->type
== SYM_UNION
)
914 *type
= sym
->ident
->name
;
915 *member
= expr
->member
->name
;
919 char *get_member_name(struct expression
*expr
)
924 expr
= strip_expr(expr
);
925 if (!expr
|| expr
->type
!= EXPR_DEREF
)
930 sym
= get_type(expr
->deref
);
933 if (sym
->type
== SYM_UNION
) {
934 snprintf(buf
, sizeof(buf
), "(union %s)->%s",
935 sym
->ident
? sym
->ident
->name
: "anonymous",
937 return alloc_string(buf
);
940 struct expression
*deref
;
945 * If we're in an anonymous struct then maybe we can find an
946 * outer struct name to use as a name. This code should be
947 * recursive and cleaner. I am not very proud of it.
952 if (deref
->type
!= EXPR_DEREF
|| !deref
->member
)
954 sym
= get_type(deref
->deref
);
955 if (!sym
|| sym
->type
!= SYM_STRUCT
|| !sym
->ident
)
958 full
= expr_to_str(expr
);
961 deref
= deref
->deref
;
962 if (deref
->type
== EXPR_PREOP
&& deref
->op
== '*')
964 outer
= expr_to_str(deref
);
970 if (strncmp(outer
, full
, len
) != 0) {
975 if (full
[len
] == '-' && full
[len
+ 1] == '>')
977 if (full
[len
] == '.')
979 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, full
+ len
);
983 return alloc_string(buf
);
985 snprintf(buf
, sizeof(buf
), "(struct %s)->%s", sym
->ident
->name
, expr
->member
->name
);
986 return alloc_string(buf
);
989 int cmp_pos(struct position pos1
, struct position pos2
)
991 /* the stream position is ... */
992 if (pos1
.stream
> pos2
.stream
)
994 if (pos1
.stream
< pos2
.stream
)
997 if (pos1
.line
< pos2
.line
)
999 if (pos1
.line
> pos2
.line
)
1002 if (pos1
.pos
< pos2
.pos
)
1004 if (pos1
.pos
> pos2
.pos
)
1010 int positions_eq(struct position pos1
, struct position pos2
)
1012 if (pos1
.line
!= pos2
.line
)
1014 if (pos1
.pos
!= pos2
.pos
)
1016 if (pos1
.stream
!= pos2
.stream
)
1021 struct statement
*get_current_statement(void)
1023 struct statement
*prev
, *tmp
;
1025 prev
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
1027 if (!prev
|| !get_macro_name(prev
->pos
))
1030 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1031 if (positions_eq(tmp
->pos
, prev
->pos
))
1033 if (prev
->pos
.line
> tmp
->pos
.line
)
1036 } END_FOR_EACH_PTR_REVERSE(tmp
);
1040 struct statement
*get_prev_statement(void)
1042 struct statement
*tmp
;
1046 FOR_EACH_PTR_REVERSE(big_statement_stack
, tmp
) {
1049 } END_FOR_EACH_PTR_REVERSE(tmp
);
1053 struct expression
*get_last_expr_from_expression_stmt(struct expression
*expr
)
1055 struct statement
*stmt
;
1056 struct statement
*last_stmt
;
1058 while (expr
->type
== EXPR_PREOP
&& expr
->op
== '(')
1060 if (expr
->type
!= EXPR_STATEMENT
)
1062 stmt
= expr
->statement
;
1065 if (stmt
->type
== STMT_COMPOUND
) {
1066 last_stmt
= last_ptr_list((struct ptr_list
*)stmt
->stmts
);
1069 if (last_stmt
->type
== STMT_LABEL
)
1070 last_stmt
= last_stmt
->label_statement
;
1071 if (last_stmt
->type
!= STMT_EXPRESSION
)
1073 return last_stmt
->expression
;
1075 if (stmt
->type
== STMT_EXPRESSION
)
1076 return stmt
->expression
;
1080 int ms_since(struct timeval
*start
)
1085 gettimeofday(&end
, NULL
);
1086 diff
= (end
.tv_sec
- start
->tv_sec
) * 1000.0;
1087 diff
+= (end
.tv_usec
- start
->tv_usec
) / 1000.0;
1091 int parent_is_gone_var_sym(const char *name
, struct symbol
*sym
)
1096 if (parent_is_err_or_null_var_sym(name
, sym
) ||
1097 parent_is_free_var_sym(name
, sym
))
1102 int parent_is_gone(struct expression
*expr
)
1108 expr
= strip_expr(expr
);
1109 var
= expr_to_var_sym(expr
, &sym
);
1112 ret
= parent_is_gone_var_sym(var
, sym
);
1118 int invert_op(int op
)
1129 case SPECIAL_LEFTSHIFT
:
1130 return SPECIAL_RIGHTSHIFT
;
1131 case SPECIAL_RIGHTSHIFT
:
1132 return SPECIAL_LEFTSHIFT
;
1137 int op_remove_assign(int op
)
1140 case SPECIAL_ADD_ASSIGN
:
1142 case SPECIAL_SUB_ASSIGN
:
1144 case SPECIAL_MUL_ASSIGN
:
1146 case SPECIAL_DIV_ASSIGN
:
1148 case SPECIAL_MOD_ASSIGN
:
1150 case SPECIAL_AND_ASSIGN
:
1152 case SPECIAL_OR_ASSIGN
:
1154 case SPECIAL_XOR_ASSIGN
:
1156 case SPECIAL_SHL_ASSIGN
:
1157 return SPECIAL_LEFTSHIFT
;
1158 case SPECIAL_SHR_ASSIGN
:
1159 return SPECIAL_RIGHTSHIFT
;
1165 int expr_equiv(struct expression
*one
, struct expression
*two
)
1167 struct symbol
*one_sym
= NULL
;
1168 struct symbol
*two_sym
= NULL
;
1169 char *one_name
= NULL
;
1170 char *two_name
= NULL
;
1177 if (one
->type
!= two
->type
)
1179 if (is_fake_call(one
) || is_fake_call(two
))
1182 one_name
= expr_to_str_sym(one
, &one_sym
);
1185 two_name
= expr_to_str_sym(two
, &two_sym
);
1188 if (one_sym
!= two_sym
)
1191 * This is a terrible hack because expr_to_str() sometimes gives up in
1192 * the middle and just returns what it has. If you see a () you know
1193 * the string is bogus.
1195 if (strstr(one_name
, "()"))
1197 if (strcmp(one_name
, two_name
) == 0)
1200 free_string(one_name
);
1201 free_string(two_name
);
1205 void push_int(struct int_stack
**stack
, int num
)
1210 * Just put the int on directly instead of a pointer to the int.
1211 * Shift it to the left because Sparse uses the last two bits.
1212 * This is sort of a dirty hack, yes.
1215 munged
= INT_PTR(num
<< 2);
1217 add_ptr_list(stack
, munged
);
1220 int pop_int(struct int_stack
**stack
)
1224 num
= last_ptr_list((struct ptr_list
*)*stack
);
1225 delete_ptr_list_last((struct ptr_list
**)stack
);
1227 return PTR_INT(num
) >> 2;