rosenberg: fix a crashing bug
[smatch.git] / smatch_helper.c
blob3aa8f043d9b2b0b43991b549bf70971472eed120
1 /*
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.
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "allocate.h"
25 #include "smatch.h"
26 #include "smatch_extra.h"
27 #include "smatch_slist.h"
29 #define VAR_LEN 512
31 char *alloc_string(const char *str)
33 char *tmp;
35 if (!str)
36 return NULL;
37 tmp = malloc(strlen(str) + 1);
38 strcpy(tmp, str);
39 return tmp;
42 void free_string(char *str)
44 free(str);
47 void remove_parens(char *str)
49 char *src, *dst;
51 dst = src = str;
52 while (*src != '\0') {
53 if (*src == '(' || *src == ')') {
54 src++;
55 continue;
57 *dst++ = *src++;
59 *dst = *src;
62 struct smatch_state *alloc_state_num(int num)
64 struct smatch_state *state;
65 static char buff[256];
67 state = __alloc_smatch_state(0);
68 snprintf(buff, 255, "%d", num);
69 buff[255] = '\0';
70 state->name = alloc_string(buff);
71 state->data = INT_PTR(num);
72 return state;
75 struct smatch_state *alloc_state_str(const char *name)
77 struct smatch_state *state;
79 state = __alloc_smatch_state(0);
80 state->name = alloc_string(name);
81 return state;
84 struct smatch_state *alloc_state_expr(struct expression *expr)
86 struct smatch_state *state;
87 char *name;
89 state = __alloc_smatch_state(0);
90 expr = strip_expr(expr);
91 name = expr_to_str(expr);
92 state->name = alloc_sname(name);
93 free_string(name);
94 state->data = expr;
95 return state;
98 void append(char *dest, const char *data, int buff_len)
100 strncat(dest, data, buff_len - strlen(dest) - 1);
104 * If you have "foo(a, b, 1);" then use
105 * get_argument_from_call_expr(expr, 0) to return the expression for
106 * a. Yes, it does start counting from 0.
108 struct expression *get_argument_from_call_expr(struct expression_list *args,
109 int num)
111 struct expression *expr;
112 int i = 0;
114 if (!args)
115 return NULL;
117 FOR_EACH_PTR(args, expr) {
118 if (i == num)
119 return expr;
120 i++;
121 } END_FOR_EACH_PTR(expr);
122 return NULL;
125 static struct expression *get_array_expr(struct expression *expr)
127 struct expression *parent;
128 struct symbol *type;
130 if (expr->type != EXPR_BINOP || expr->op != '+')
131 return NULL;
133 type = get_type(expr->left);
134 if (!type)
135 return NULL;
136 if (type->type == SYM_ARRAY)
137 return expr->left;
138 if (type->type != SYM_PTR)
139 return NULL;
141 parent = expr_get_parent_expr(expr);
142 if (!parent) /* Sometimes we haven't set up the ->parent yet. FIXME!! */
143 return expr->left;
144 if (parent->type == EXPR_PREOP && parent->op == '*')
145 return expr->left;
147 return NULL;
150 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
151 struct expression *expr, int len,
152 int *complicated, int no_parens)
156 if (!expr) {
157 /* can't happen on valid code */
158 *complicated = 1;
159 return;
162 switch (expr->type) {
163 case EXPR_DEREF: {
164 struct expression *deref;
165 int op;
167 deref = expr->deref;
168 op = deref->op;
169 if (op == '*') {
170 struct expression *unop = strip_expr(deref->unop);
172 if (unop->type == EXPR_PREOP && unop->op == '&') {
173 deref = unop->unop;
174 op = '.';
175 } else {
176 deref = deref->unop;
177 if (!is_pointer(deref))
178 op = '.';
182 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
184 if (op == '*')
185 append(buf, "->", len);
186 else
187 append(buf, ".", len);
189 if (expr->member)
190 append(buf, expr->member->name, len);
191 else
192 append(buf, "unknown_member", len);
194 return;
196 case EXPR_SYMBOL:
197 if (expr->symbol_name)
198 append(buf, expr->symbol_name->name, len);
199 if (sym_ptr) {
200 if (*sym_ptr)
201 *complicated = 1;
202 *sym_ptr = expr->symbol;
204 return;
205 case EXPR_PREOP: {
206 const char *tmp;
208 if (get_expression_statement(expr)) {
209 *complicated = 2;
210 return;
213 if (expr->op == '(') {
214 if (!no_parens && expr->unop->type != EXPR_SYMBOL)
215 append(buf, "(", len);
216 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
217 tmp = show_special(expr->op);
218 append(buf, tmp, len);
220 __get_variable_from_expr(sym_ptr, buf, expr->unop,
221 len, complicated, no_parens);
223 if (expr->op == '(' && !no_parens && expr->unop->type != EXPR_SYMBOL)
224 append(buf, ")", len);
226 if (expr->op == SPECIAL_DECREMENT ||
227 expr->op == SPECIAL_INCREMENT)
228 *complicated = 1;
230 return;
232 case EXPR_POSTOP: {
233 const char *tmp;
235 __get_variable_from_expr(sym_ptr, buf, expr->unop,
236 len, complicated, no_parens);
237 tmp = show_special(expr->op);
238 append(buf, tmp, len);
240 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
241 *complicated = 1;
242 return;
244 case EXPR_ASSIGNMENT:
245 case EXPR_COMPARE:
246 case EXPR_LOGICAL:
247 case EXPR_BINOP: {
248 char tmp[10];
249 struct expression *array_expr;
251 *complicated = 1;
252 array_expr = get_array_expr(expr);
253 if (array_expr) {
254 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
255 append(buf, "[", len);
256 } else {
257 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
258 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
259 append(buf, tmp, len);
261 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
262 if (array_expr)
263 append(buf, "]", len);
264 return;
266 case EXPR_VALUE: {
267 char tmp[25];
269 *complicated = 1;
270 snprintf(tmp, 25, "%lld", expr->value);
271 append(buf, tmp, len);
272 return;
274 case EXPR_STRING:
275 append(buf, "\"", len);
276 if (expr->string)
277 append(buf, expr->string->data, len);
278 append(buf, "\"", len);
279 return;
280 case EXPR_CALL: {
281 struct expression *tmp;
282 int i;
284 *complicated = 1;
285 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
286 append(buf, "(", len);
287 i = 0;
288 FOR_EACH_PTR(expr->args, tmp) {
289 if (i++)
290 append(buf, ", ", len);
291 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
292 } END_FOR_EACH_PTR(tmp);
293 append(buf, ")", len);
294 return;
296 case EXPR_CAST:
297 case EXPR_FORCE_CAST:
298 __get_variable_from_expr(sym_ptr, buf,
299 expr->cast_expression, len,
300 complicated, no_parens);
301 return;
302 case EXPR_SIZEOF: {
303 sval_t sval;
304 int size;
305 char tmp[25];
307 if (expr->cast_type && get_base_type(expr->cast_type)) {
308 size = type_bytes(get_base_type(expr->cast_type));
309 snprintf(tmp, 25, "%d", size);
310 append(buf, tmp, len);
311 } else if (get_value(expr, &sval)) {
312 snprintf(tmp, 25, "%s", sval_to_str(sval));
313 append(buf, tmp, len);
315 return;
317 case EXPR_IDENTIFIER:
318 *complicated = 1;
319 if (expr->expr_ident)
320 append(buf, expr->expr_ident->name, len);
321 return;
322 default:
323 *complicated = 1;
324 //printf("unknown type = %d\n", expr->type);
325 return;
329 struct expr_str_cache_results {
330 struct expression *expr;
331 int no_parens;
332 char str[VAR_LEN];
333 struct symbol *sym;
334 int complicated;
337 static void get_variable_from_expr(struct symbol **sym_ptr, char *buf,
338 struct expression *expr, int len,
339 int *complicated, int no_parens)
341 static struct expr_str_cache_results cached[8];
342 struct symbol *tmp_sym = NULL;
343 static int idx;
344 int i;
346 for (i = 0; i < ARRAY_SIZE(cached); i++) {
347 if (expr == cached[i].expr &&
348 no_parens == cached[i].no_parens) {
349 strncpy(buf, cached[i].str, len);
350 if (sym_ptr)
351 *sym_ptr = cached[i].sym;
352 *complicated = cached[i].complicated;
353 return;
357 __get_variable_from_expr(&tmp_sym, buf, expr, len, complicated, no_parens);
358 if (sym_ptr)
359 *sym_ptr = tmp_sym;
361 cached[idx].expr = expr;
362 cached[idx].no_parens = no_parens;
363 strncpy(cached[idx].str, buf, VAR_LEN);
364 cached[idx].sym = tmp_sym;
365 cached[idx].complicated = *complicated;
367 idx = (idx + 1) % ARRAY_SIZE(cached);
371 * This is returns a stylized "c looking" representation of the
372 * variable name.
374 * It uses the same buffer every time so you have to save the result
375 * yourself if you want to keep it.
379 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
381 static char var_name[VAR_LEN];
382 int complicated = 0;
384 if (sym_ptr)
385 *sym_ptr = NULL;
386 var_name[0] = '\0';
388 if (!expr)
389 return NULL;
390 get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
391 &complicated, 0);
392 if (complicated < 2)
393 return alloc_string(var_name);
394 else
395 return NULL;
398 char *expr_to_str(struct expression *expr)
400 return expr_to_str_sym(expr, NULL);
404 * get_variable_from_expr_simple() only returns simple variables.
405 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
406 * then it returns NULL.
408 char *expr_to_var_sym(struct expression *expr,
409 struct symbol **sym_ptr)
411 static char var_name[VAR_LEN];
412 int complicated = 0;
414 if (sym_ptr)
415 *sym_ptr = NULL;
416 var_name[0] = '\0';
418 if (!expr)
419 return NULL;
420 expr = strip_expr(expr);
421 get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
422 &complicated, 1);
424 if (complicated) {
425 if (sym_ptr)
426 *sym_ptr = NULL;
427 return NULL;
429 return alloc_string(var_name);
432 char *expr_to_var(struct expression *expr)
434 return expr_to_var_sym(expr, NULL);
437 struct symbol *expr_to_sym(struct expression *expr)
439 struct symbol *sym;
440 char *name;
442 name = expr_to_var_sym(expr, &sym);
443 free_string(name);
444 return sym;
447 int get_complication_score(struct expression *expr)
449 int score = 0;
451 expr = strip_expr(expr);
454 * Don't forget to keep get_complication_score() and store_all_links()
455 * in sync.
459 if (!expr)
460 return 990;
462 switch (expr->type) {
463 case EXPR_CALL:
464 return 991;
465 case EXPR_COMPARE:
466 case EXPR_BINOP:
467 score += get_complication_score(expr->left);
468 score += get_complication_score(expr->right);
469 return score;
470 case EXPR_SYMBOL:
471 return 1;
472 case EXPR_PREOP:
473 if (expr->op == '*')
474 return score + get_complication_score(expr->unop);
475 return 993;
476 case EXPR_DEREF:
477 return score + get_complication_score(expr->deref);
478 case EXPR_VALUE:
479 return 0;
480 default:
481 return 994;
485 struct expression *reorder_expr_alphabetically(struct expression *expr)
487 struct expression *ret;
488 char *left, *right;
490 if (expr->type != EXPR_BINOP)
491 return expr;
492 if (expr->op != '+' && expr->op != '*')
493 return expr;
495 left = expr_to_var(expr->left);
496 right = expr_to_var(expr->right);
497 ret = expr;
498 if (!left || !right)
499 goto free;
500 if (strcmp(left, right) <= 0)
501 goto free;
503 ret = binop_expression(expr->right, expr->op, expr->left);
504 free:
505 free_string(left);
506 free_string(right);
508 return ret;
511 char *expr_to_chunk_helper(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
513 struct var_sym_list *tmp_vsl;
514 char *name;
515 struct symbol *tmp;
516 int score;
518 if (vsl)
519 *vsl = NULL;
520 if (sym)
521 *sym = NULL;
523 expr = strip_parens(expr);
524 if (!expr)
525 return NULL;
527 name = expr_to_var_sym(expr, &tmp);
528 if (name && tmp) {
529 if (sym)
530 *sym = tmp;
531 if (vsl)
532 *vsl = expr_to_vsl(expr);
533 return name;
535 free_string(name);
537 score = get_complication_score(expr);
538 if (score <= 0 || score > 2)
539 return NULL;
541 tmp_vsl = expr_to_vsl(expr);
542 if (vsl) {
543 *vsl = tmp_vsl;
544 if (!*vsl)
545 return NULL;
547 if (sym) {
548 if (ptr_list_size((struct ptr_list *)tmp_vsl) == 1) {
549 struct var_sym *vs;
551 vs = first_ptr_list((struct ptr_list *)tmp_vsl);
552 *sym = vs->sym;
556 expr = reorder_expr_alphabetically(expr);
558 return expr_to_str(expr);
561 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
563 return expr_to_chunk_helper(expr, sym, NULL);
566 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
568 return expr_to_chunk_helper(expr, sym, vsl);
571 int sym_name_is(const char *name, struct expression *expr)
573 if (!expr)
574 return 0;
575 if (expr->type != EXPR_SYMBOL)
576 return 0;
577 if (!strcmp(expr->symbol_name->name, name))
578 return 1;
579 return 0;
582 int is_zero(struct expression *expr)
584 sval_t sval;
586 if (get_value(expr, &sval) && sval.value == 0)
587 return 1;
588 return 0;
591 int is_array(struct expression *expr)
593 struct symbol *type;
595 expr = strip_expr(expr);
596 if (!expr)
597 return 0;
599 if (expr->type == EXPR_PREOP && expr->op == '*') {
600 expr = strip_expr(expr->unop);
601 if (!expr)
602 return 0;
603 if (expr->type == EXPR_BINOP && expr->op == '+')
604 return 1;
607 if (expr->type != EXPR_BINOP || expr->op != '+')
608 return 0;
610 type = get_type(expr->left);
611 if (!type || type->type != SYM_ARRAY)
612 return 0;
614 return 1;
617 struct expression *get_array_base(struct expression *expr)
619 if (!is_array(expr))
620 return NULL;
621 expr = strip_expr(expr);
622 if (expr->type == EXPR_PREOP && expr->op == '*')
623 expr = strip_expr(expr->unop);
624 if (expr->type != EXPR_BINOP || expr->op != '+')
625 return NULL;
626 return strip_parens(expr->left);
629 struct expression *get_array_offset(struct expression *expr)
631 if (!is_array(expr))
632 return NULL;
633 expr = strip_expr(expr);
634 if (expr->type == EXPR_PREOP && expr->op == '*')
635 expr = strip_expr(expr->unop);
636 if (expr->type != EXPR_BINOP || expr->op != '+')
637 return NULL;
638 return strip_parens(expr->right);
641 const char *show_state(struct smatch_state *state)
643 if (!state)
644 return NULL;
645 return state->name;
648 struct statement *get_expression_statement(struct expression *expr)
650 /* What are those things called? if (({....; ret;})) { ...*/
652 if (expr->type != EXPR_PREOP)
653 return NULL;
654 if (expr->op != '(')
655 return NULL;
656 if (!expr->unop)
657 return NULL;
658 if (expr->unop->type != EXPR_STATEMENT)
659 return NULL;
660 if (expr->unop->statement->type != STMT_COMPOUND)
661 return NULL;
662 return expr->unop->statement;
665 struct expression *strip_parens(struct expression *expr)
667 if (!expr)
668 return NULL;
670 if (expr->type == EXPR_PREOP) {
671 if (!expr->unop)
672 return expr; /* parsing invalid code */
674 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
675 expr->unop->statement->type == STMT_COMPOUND)
676 return expr;
677 if (expr->op == '(')
678 return strip_parens(expr->unop);
680 return expr;
683 struct expression *strip_expr(struct expression *expr)
685 if (!expr)
686 return NULL;
688 switch (expr->type) {
689 case EXPR_FORCE_CAST:
690 case EXPR_CAST:
691 return strip_expr(expr->cast_expression);
692 case EXPR_PREOP: {
693 struct expression *unop;
695 if (!expr->unop) /* parsing invalid code */
696 return expr;
698 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
699 expr->unop->statement->type == STMT_COMPOUND)
700 return expr;
702 unop = strip_expr(expr->unop);
704 if (expr->op == '*' && unop &&
705 unop->type == EXPR_PREOP && unop->op == '&') {
706 struct symbol *type = get_type(unop->unop);
708 if (type && type->type == SYM_ARRAY)
709 return expr;
710 return strip_expr(unop->unop);
713 if (expr->op == '(')
714 return unop;
716 return expr;
718 case EXPR_CONDITIONAL:
719 if (known_condition_true(expr->conditional)) {
720 if (expr->cond_true)
721 return strip_expr(expr->cond_true);
722 return strip_expr(expr->conditional);
724 if (known_condition_false(expr->conditional))
725 return strip_expr(expr->cond_false);
726 return expr;
727 case EXPR_CALL:
728 if (sym_name_is("__builtin_expect", expr->fn)) {
729 expr = get_argument_from_call_expr(expr->args, 0);
730 return strip_expr(expr);
732 return expr;
734 return expr;
737 static void delete_state_tracker(struct tracker *t)
739 delete_state(t->owner, t->name, t->sym);
740 __free_tracker(t);
743 void scoped_state(int my_id, const char *name, struct symbol *sym)
745 struct tracker *t;
747 t = alloc_tracker(my_id, name, sym);
748 add_scope_hook((scope_hook *)&delete_state_tracker, t);
751 int is_error_return(struct expression *expr)
753 struct symbol *cur_func = cur_func_sym;
754 sval_t sval;
756 if (!expr)
757 return 0;
758 if (cur_func->type != SYM_NODE)
759 return 0;
760 cur_func = get_base_type(cur_func);
761 if (cur_func->type != SYM_FN)
762 return 0;
763 cur_func = get_base_type(cur_func);
764 if (cur_func == &void_ctype)
765 return 0;
766 if (!get_implied_value(expr, &sval))
767 return 0;
768 if (sval.value < 0)
769 return 1;
770 if (cur_func->type == SYM_PTR && sval.value == 0)
771 return 1;
772 return 0;
775 int getting_address(void)
777 struct expression *tmp;
778 int i = 0;
779 int dot_ops = 0;
781 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
782 if (!i++)
783 continue;
784 if (tmp->type == EXPR_PREOP && tmp->op == '(')
785 continue;
786 if (tmp->op == '.' && !dot_ops++)
787 continue;
788 if (tmp->op == '&')
789 return 1;
790 return 0;
791 } END_FOR_EACH_PTR_REVERSE(tmp);
792 return 0;
795 int get_struct_and_member(struct expression *expr, const char **type, const char **member)
797 struct symbol *sym;
799 expr = strip_expr(expr);
800 if (expr->type != EXPR_DEREF)
801 return 0;
802 if (!expr->member)
803 return 0;
805 sym = get_type(expr->deref);
806 if (!sym)
807 return 0;
808 if (sym->type == SYM_UNION)
809 return 0;
810 if (!sym->ident)
811 return 0;
813 *type = sym->ident->name;
814 *member = expr->member->name;
815 return 1;
818 char *get_member_name(struct expression *expr)
820 char buf[256];
821 struct symbol *sym;
823 expr = strip_expr(expr);
824 if (!expr || expr->type != EXPR_DEREF)
825 return NULL;
826 if (!expr->member)
827 return NULL;
829 sym = get_type(expr->deref);
830 if (!sym)
831 return NULL;
832 if (sym->type == SYM_UNION) {
833 snprintf(buf, sizeof(buf), "(union %s)->%s",
834 sym->ident ? sym->ident->name : "anonymous",
835 expr->member->name);
836 return alloc_string(buf);
838 if (!sym->ident)
839 return NULL;
840 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
841 return alloc_string(buf);
844 int cmp_pos(struct position pos1, struct position pos2)
846 /* the stream position is ... */
847 if (pos1.stream > pos2.stream)
848 return -1;
849 if (pos1.stream < pos2.stream)
850 return 1;
852 if (pos1.line < pos2.line)
853 return -1;
854 if (pos1.line > pos2.line)
855 return 1;
857 if (pos1.pos < pos2.pos)
858 return -1;
859 if (pos1.pos > pos2.pos)
860 return 1;
862 return 0;
865 int positions_eq(struct position pos1, struct position pos2)
867 if (pos1.line != pos2.line)
868 return 0;
869 if (pos1.pos != pos2.pos)
870 return 0;
871 if (pos1.stream != pos2.stream)
872 return 0;
873 return 1;
876 struct statement *get_current_statement(void)
878 struct statement *prev, *tmp;
880 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
882 if (!prev || !get_macro_name(prev->pos))
883 return prev;
885 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
886 if (positions_eq(tmp->pos, prev->pos))
887 continue;
888 if (prev->pos.line > tmp->pos.line)
889 return prev;
890 return tmp;
891 } END_FOR_EACH_PTR_REVERSE(tmp);
892 return prev;
895 struct statement *get_prev_statement(void)
897 struct statement *tmp;
898 int i;
900 i = 0;
901 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
902 if (i++ == 1)
903 return tmp;
904 } END_FOR_EACH_PTR_REVERSE(tmp);
905 return NULL;
908 int get_param_num_from_sym(struct symbol *sym)
910 struct symbol *tmp;
911 int i;
913 if (!cur_func_sym)
914 return -1;
916 i = 0;
917 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
918 if (tmp == sym)
919 return i;
920 i++;
921 } END_FOR_EACH_PTR(tmp);
922 return -1;
925 int get_param_num(struct expression *expr)
927 struct symbol *sym;
928 char *name;
930 if (!cur_func_sym)
931 return -1;
932 name = expr_to_var_sym(expr, &sym);
933 free_string(name);
934 if (!sym)
935 return -1;
936 return get_param_num_from_sym(sym);
939 int ms_since(struct timeval *start)
941 struct timeval end;
942 double diff;
944 gettimeofday(&end, NULL);
945 diff = (end.tv_sec - start->tv_sec) * 1000.0;
946 diff += (end.tv_usec - start->tv_usec) / 1000.0;
947 return (int)diff;
950 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
952 if (!name || !sym)
953 return 0;
955 if (parent_is_null_var_sym(name, sym) ||
956 parent_is_free_var_sym(name, sym))
957 return 1;
958 return 0;
961 int parent_is_gone(struct expression *expr)
963 struct symbol *sym;
964 char *var;
965 int ret = 0;
967 expr = strip_expr(expr);
968 var = expr_to_var_sym(expr, &sym);
969 if (!var || !sym)
970 goto free;
971 ret = parent_is_gone_var_sym(var, sym);
972 free:
973 free_string(var);
974 return ret;
977 int invert_op(int op)
979 switch (op) {
980 case '*':
981 return '/';
982 case '/':
983 return '*';
984 case '+':
985 return '-';
986 case '-':
987 return '+';
988 case SPECIAL_LEFTSHIFT:
989 return SPECIAL_RIGHTSHIFT;
990 case SPECIAL_RIGHTSHIFT:
991 return SPECIAL_LEFTSHIFT;
993 return 0;
996 int expr_equiv(struct expression *one, struct expression *two)
998 struct symbol *one_sym = NULL;
999 struct symbol *two_sym = NULL;
1000 char *one_name = NULL;
1001 char *two_name = NULL;
1002 int ret = 0;
1004 if (!one || !two)
1005 return 0;
1006 if (one->type != two->type)
1007 return 0;
1009 one_name = expr_to_str_sym(one, &one_sym);
1010 if (!one_name)
1011 goto free;
1012 two_name = expr_to_str_sym(two, &two_sym);
1013 if (!two_name)
1014 goto free;
1015 if (one_sym != two_sym)
1016 goto free;
1018 * This is a terrible hack because expr_to_str() sometimes gives up in
1019 * the middle and just returns what it has. If you see a () you know
1020 * the string is bogus.
1022 if (strstr(one_name, "()"))
1023 goto free;
1024 if (strcmp(one_name, two_name) == 0)
1025 ret = 1;
1026 free:
1027 free_string(one_name);
1028 free_string(two_name);
1029 return ret;
1032 void push_int(struct int_stack **stack, int num)
1034 int *munged;
1037 * Just put the int on directly instead of a pointer to the int.
1038 * Shift it to the left because Sparse uses the last two bits.
1039 * This is sort of a dirty hack, yes.
1042 munged = INT_PTR(num << 2);
1044 add_ptr_list(stack, munged);
1047 int pop_int(struct int_stack **stack)
1049 int *num;
1051 num = last_ptr_list((struct ptr_list *)*stack);
1052 delete_ptr_list_last((struct ptr_list **)stack);
1054 return PTR_INT(num) >> 2;