helper: re-order symbol additions in expr_to_chunk()
[smatch.git] / smatch_helper.c
blob3cf75a88e2857e75132b7af36e75bb261a039d4e
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 symbol *type;
129 if (expr->type != EXPR_BINOP || expr->op != '+')
130 return NULL;
132 type = get_type(expr->left);
133 if (!type || type->type != SYM_ARRAY)
134 return NULL;
135 return expr->left;
138 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
139 struct expression *expr, int len,
140 int *complicated, int no_parens)
144 if (!expr) {
145 /* can't happen on valid code */
146 *complicated = 1;
147 return;
150 switch (expr->type) {
151 case EXPR_DEREF: {
152 struct expression *deref;
153 int op;
155 deref = expr->deref;
156 op = deref->op;
157 if (op == '*') {
158 struct expression *unop = strip_expr(deref->unop);
160 if (unop->type == EXPR_PREOP && unop->op == '&') {
161 deref = unop->unop;
162 op = '.';
163 } else {
164 deref = deref->unop;
165 if (!is_pointer(deref))
166 op = '.';
170 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
172 if (op == '*')
173 append(buf, "->", len);
174 else
175 append(buf, ".", len);
177 if (expr->member)
178 append(buf, expr->member->name, len);
179 else
180 append(buf, "unknown_member", len);
182 return;
184 case EXPR_SYMBOL:
185 if (expr->symbol_name)
186 append(buf, expr->symbol_name->name, len);
187 if (sym_ptr) {
188 if (*sym_ptr)
189 *complicated = 1;
190 *sym_ptr = expr->symbol;
192 return;
193 case EXPR_PREOP: {
194 const char *tmp;
196 if (get_expression_statement(expr)) {
197 *complicated = 2;
198 return;
201 if (expr->op == '(') {
202 if (!no_parens && expr->unop->type != EXPR_SYMBOL)
203 append(buf, "(", len);
204 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
205 tmp = show_special(expr->op);
206 append(buf, tmp, len);
208 __get_variable_from_expr(sym_ptr, buf, expr->unop,
209 len, complicated, no_parens);
211 if (expr->op == '(' && !no_parens && expr->unop->type != EXPR_SYMBOL)
212 append(buf, ")", len);
214 if (expr->op == SPECIAL_DECREMENT ||
215 expr->op == SPECIAL_INCREMENT)
216 *complicated = 1;
218 return;
220 case EXPR_POSTOP: {
221 const char *tmp;
223 __get_variable_from_expr(sym_ptr, buf, expr->unop,
224 len, complicated, no_parens);
225 tmp = show_special(expr->op);
226 append(buf, tmp, len);
228 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
229 *complicated = 1;
230 return;
232 case EXPR_ASSIGNMENT:
233 case EXPR_COMPARE:
234 case EXPR_LOGICAL:
235 case EXPR_BINOP: {
236 char tmp[10];
237 struct expression *array_expr;
239 *complicated = 1;
240 array_expr = get_array_expr(expr);
241 if (array_expr) {
242 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
243 append(buf, "[", len);
244 } else {
245 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
246 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
247 append(buf, tmp, len);
249 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
250 if (array_expr)
251 append(buf, "]", len);
252 return;
254 case EXPR_VALUE: {
255 char tmp[25];
257 *complicated = 1;
258 snprintf(tmp, 25, "%lld", expr->value);
259 append(buf, tmp, len);
260 return;
262 case EXPR_STRING:
263 append(buf, "\"", len);
264 if (expr->string)
265 append(buf, expr->string->data, len);
266 append(buf, "\"", len);
267 return;
268 case EXPR_CALL: {
269 struct expression *tmp;
270 int i;
272 *complicated = 1;
273 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
274 append(buf, "(", len);
275 i = 0;
276 FOR_EACH_PTR(expr->args, tmp) {
277 if (i++)
278 append(buf, ", ", len);
279 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
280 } END_FOR_EACH_PTR(tmp);
281 append(buf, ")", len);
282 return;
284 case EXPR_CAST:
285 case EXPR_FORCE_CAST:
286 __get_variable_from_expr(sym_ptr, buf,
287 expr->cast_expression, len,
288 complicated, no_parens);
289 return;
290 case EXPR_SIZEOF: {
291 int size;
292 char tmp[25];
294 if (expr->cast_type && get_base_type(expr->cast_type)) {
295 size = type_bytes(get_base_type(expr->cast_type));
296 snprintf(tmp, 25, "%d", size);
297 append(buf, tmp, len);
299 return;
301 case EXPR_IDENTIFIER:
302 *complicated = 1;
303 if (expr->expr_ident)
304 append(buf, expr->expr_ident->name, len);
305 return;
306 default:
307 *complicated = 1;
308 //printf("unknown type = %d\n", expr->type);
309 return;
314 * This is returns a stylized "c looking" representation of the
315 * variable name.
317 * It uses the same buffer every time so you have to save the result
318 * yourself if you want to keep it.
322 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
324 static char var_name[VAR_LEN];
325 int complicated = 0;
327 if (sym_ptr)
328 *sym_ptr = NULL;
329 var_name[0] = '\0';
331 if (!expr)
332 return NULL;
333 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
334 &complicated, 0);
335 if (complicated < 2)
336 return alloc_string(var_name);
337 else
338 return NULL;
341 char *expr_to_str(struct expression *expr)
343 return expr_to_str_sym(expr, NULL);
347 * get_variable_from_expr_simple() only returns simple variables.
348 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
349 * then it returns NULL.
351 char *expr_to_var_sym(struct expression *expr,
352 struct symbol **sym_ptr)
354 static char var_name[VAR_LEN];
355 int complicated = 0;
357 if (sym_ptr)
358 *sym_ptr = NULL;
359 var_name[0] = '\0';
361 if (!expr)
362 return NULL;
363 expr = strip_expr(expr);
364 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
365 &complicated, 1);
367 if (complicated) {
368 if (sym_ptr)
369 *sym_ptr = NULL;
370 return NULL;
372 return alloc_string(var_name);
375 char *expr_to_var(struct expression *expr)
377 return expr_to_var_sym(expr, NULL);
380 struct symbol *expr_to_sym(struct expression *expr)
382 struct symbol *sym;
383 char *name;
385 name = expr_to_var_sym(expr, &sym);
386 free_string(name);
387 return sym;
390 int get_complication_score(struct expression *expr)
392 int score = 0;
394 expr = strip_expr(expr);
397 * Don't forget to keep get_complication_score() and store_all_links()
398 * in sync.
402 if (!expr)
403 return 999;
405 switch (expr->type) {
406 case EXPR_CALL:
407 return 999;
408 case EXPR_COMPARE:
409 case EXPR_BINOP:
410 score += get_complication_score(expr->left);
411 score += get_complication_score(expr->right);
412 return score;
413 case EXPR_SYMBOL:
414 if (is_local_variable(expr))
415 return 1;
416 return 999;
417 case EXPR_PREOP:
418 if (expr->op == '*')
419 return score + get_complication_score(expr->unop);
420 return 999;
421 case EXPR_DEREF:
422 return score + get_complication_score(expr->deref);
423 case EXPR_VALUE:
424 return 0;
425 default:
426 return 999;
430 struct expression *reorder_expr_alphabetically(struct expression *expr)
432 struct expression *ret;
433 char *left, *right;
435 if (expr->type != EXPR_BINOP)
436 return expr;
437 if (expr->op != '+' && expr->op != '*')
438 return expr;
440 left = expr_to_var(expr->left);
441 right = expr_to_var(expr->right);
442 ret = expr;
443 if (!left || !right)
444 goto free;
445 if (strcmp(left, right) <= 0)
446 goto free;
448 ret = binop_expression(expr->right, expr->op, expr->left);
449 free:
450 free_string(left);
451 free_string(right);
453 return ret;
456 char *expr_to_chunk_helper(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
458 char *name;
459 struct symbol *tmp;
460 int score;
462 if (vsl)
463 *vsl = NULL;
464 if (sym)
465 *sym = NULL;
467 expr = strip_parens(expr);
468 if (!expr)
469 return NULL;
471 name = expr_to_var_sym(expr, &tmp);
472 if (name && tmp) {
473 if (sym)
474 *sym = tmp;
475 if (vsl)
476 *vsl = expr_to_vsl(expr);
477 return name;
479 free_string(name);
481 score = get_complication_score(expr);
482 if (score <= 0 || score > 2)
483 return NULL;
485 if (vsl) {
486 *vsl = expr_to_vsl(expr);
487 if (!*vsl)
488 return NULL;
491 expr = reorder_expr_alphabetically(expr);
493 return expr_to_str(expr);
496 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
498 return expr_to_chunk_helper(expr, sym, NULL);
501 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
503 return expr_to_chunk_helper(expr, sym, vsl);
506 int sym_name_is(const char *name, struct expression *expr)
508 if (!expr)
509 return 0;
510 if (expr->type != EXPR_SYMBOL)
511 return 0;
512 if (!strcmp(expr->symbol_name->name, name))
513 return 1;
514 return 0;
517 int is_zero(struct expression *expr)
519 sval_t sval;
521 if (get_value(expr, &sval) && sval.value == 0)
522 return 1;
523 return 0;
526 int is_array(struct expression *expr)
528 struct symbol *type;
530 expr = strip_expr(expr);
531 if (!expr)
532 return 0;
534 if (expr->type == EXPR_PREOP && expr->op == '*') {
535 expr = strip_expr(expr->unop);
536 if (!expr)
537 return 0;
538 if (expr->type == EXPR_BINOP && expr->op == '+')
539 return 1;
542 if (expr->type != EXPR_BINOP || expr->op != '+')
543 return 0;
545 type = get_type(expr->left);
546 if (!type || type->type != SYM_ARRAY)
547 return 0;
549 return 1;
552 struct expression *get_array_base(struct expression *expr)
554 if (!is_array(expr))
555 return NULL;
556 expr = strip_expr(expr);
557 if (expr->type == EXPR_PREOP && expr->op == '*')
558 expr = strip_expr(expr->unop);
559 if (expr->type != EXPR_BINOP || expr->op != '+')
560 return NULL;
561 return strip_parens(expr->left);
564 struct expression *get_array_offset(struct expression *expr)
566 if (!is_array(expr))
567 return NULL;
568 expr = strip_expr(expr);
569 if (expr->type == EXPR_PREOP && expr->op == '*')
570 expr = strip_expr(expr->unop);
571 if (expr->type != EXPR_BINOP || expr->op != '+')
572 return NULL;
573 return strip_parens(expr->right);
576 const char *show_state(struct smatch_state *state)
578 if (!state)
579 return NULL;
580 return state->name;
583 struct statement *get_expression_statement(struct expression *expr)
585 /* What are those things called? if (({....; ret;})) { ...*/
587 if (expr->type != EXPR_PREOP)
588 return NULL;
589 if (expr->op != '(')
590 return NULL;
591 if (expr->unop->type != EXPR_STATEMENT)
592 return NULL;
593 if (expr->unop->statement->type != STMT_COMPOUND)
594 return NULL;
595 return expr->unop->statement;
598 struct expression *strip_parens(struct expression *expr)
600 if (!expr)
601 return NULL;
603 if (expr->type == EXPR_PREOP) {
604 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
605 expr->unop->statement->type == STMT_COMPOUND)
606 return expr;
607 if (expr->op == '(')
608 return strip_parens(expr->unop);
610 return expr;
613 struct expression *strip_expr(struct expression *expr)
615 if (!expr)
616 return NULL;
618 switch (expr->type) {
619 case EXPR_FORCE_CAST:
620 case EXPR_CAST:
621 return strip_expr(expr->cast_expression);
622 case EXPR_PREOP: {
623 struct expression *unop;
625 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
626 expr->unop->statement->type == STMT_COMPOUND)
627 return expr;
629 unop = strip_expr(expr->unop);
631 if (expr->op == '*' && unop &&
632 unop->type == EXPR_PREOP && unop->op == '&') {
633 struct symbol *type = get_type(unop->unop);
635 if (type && type->type == SYM_ARRAY)
636 return expr;
637 return strip_expr(unop->unop);
640 if (expr->op == '(')
641 return unop;
643 return expr;
645 case EXPR_CONDITIONAL:
646 if (known_condition_true(expr->conditional)) {
647 if (expr->cond_true)
648 return strip_expr(expr->cond_true);
649 return strip_expr(expr->conditional);
651 if (known_condition_false(expr->conditional))
652 return strip_expr(expr->cond_false);
653 return expr;
654 case EXPR_CALL:
655 if (sym_name_is("__builtin_expect", expr->fn)) {
656 expr = get_argument_from_call_expr(expr->args, 0);
657 return strip_expr(expr);
659 return expr;
661 return expr;
664 static void delete_state_tracker(struct tracker *t)
666 delete_state(t->owner, t->name, t->sym);
667 __free_tracker(t);
670 void scoped_state(int my_id, const char *name, struct symbol *sym)
672 struct tracker *t;
674 t = alloc_tracker(my_id, name, sym);
675 add_scope_hook((scope_hook *)&delete_state_tracker, t);
678 int is_error_return(struct expression *expr)
680 struct symbol *cur_func = cur_func_sym;
681 sval_t sval;
683 if (!expr)
684 return 0;
685 if (cur_func->type != SYM_NODE)
686 return 0;
687 cur_func = get_base_type(cur_func);
688 if (cur_func->type != SYM_FN)
689 return 0;
690 cur_func = get_base_type(cur_func);
691 if (cur_func == &void_ctype)
692 return 0;
693 if (!get_implied_value(expr, &sval))
694 return 0;
695 if (sval.value < 0)
696 return 1;
697 if (cur_func->type == SYM_PTR && sval.value == 0)
698 return 1;
699 return 0;
702 int getting_address(void)
704 struct expression *tmp;
705 int i = 0;
706 int dot_ops = 0;
708 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
709 if (!i++)
710 continue;
711 if (tmp->type == EXPR_PREOP && tmp->op == '(')
712 continue;
713 if (tmp->op == '.' && !dot_ops++)
714 continue;
715 if (tmp->op == '&')
716 return 1;
717 return 0;
718 } END_FOR_EACH_PTR_REVERSE(tmp);
719 return 0;
722 int get_struct_and_member(struct expression *expr, const char **type, const char **member)
724 struct symbol *sym;
726 expr = strip_expr(expr);
727 if (expr->type != EXPR_DEREF)
728 return 0;
729 if (!expr->member)
730 return 0;
732 sym = get_type(expr->deref);
733 if (!sym)
734 return 0;
735 if (sym->type == SYM_UNION)
736 return 0;
737 if (!sym->ident)
738 return 0;
740 *type = sym->ident->name;
741 *member = expr->member->name;
742 return 1;
745 char *get_member_name(struct expression *expr)
747 char buf[256];
748 struct symbol *sym;
750 expr = strip_expr(expr);
751 if (expr->type != EXPR_DEREF)
752 return NULL;
753 if (!expr->member)
754 return NULL;
756 sym = get_type(expr->deref);
757 if (!sym)
758 return NULL;
759 if (sym->type == SYM_UNION) {
760 snprintf(buf, sizeof(buf), "(union %s)->%s",
761 sym->ident ? sym->ident->name : "anonymous",
762 expr->member->name);
763 return alloc_string(buf);
765 if (!sym->ident)
766 return NULL;
767 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
768 return alloc_string(buf);
771 int cmp_pos(struct position pos1, struct position pos2)
773 /* the stream position is ... */
774 if (pos1.stream > pos2.stream)
775 return -1;
776 if (pos1.stream < pos2.stream)
777 return 1;
779 if (pos1.line < pos2.line)
780 return -1;
781 if (pos1.line > pos2.line)
782 return 1;
784 if (pos1.pos < pos2.pos)
785 return -1;
786 if (pos1.pos > pos2.pos)
787 return 1;
789 return 0;
792 int positions_eq(struct position pos1, struct position pos2)
794 if (pos1.line != pos2.line)
795 return 0;
796 if (pos1.pos != pos2.pos)
797 return 0;
798 if (pos1.stream != pos2.stream)
799 return 0;
800 return 1;
803 struct statement *get_current_statement(void)
805 struct statement *prev, *tmp;
807 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
809 if (!prev || !get_macro_name(prev->pos))
810 return prev;
812 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
813 if (positions_eq(tmp->pos, prev->pos))
814 continue;
815 if (prev->pos.line > tmp->pos.line)
816 return prev;
817 return tmp;
818 } END_FOR_EACH_PTR_REVERSE(tmp);
819 return prev;
822 struct statement *get_prev_statement(void)
824 struct statement *tmp;
825 int i;
827 i = 0;
828 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
829 if (i++ == 1)
830 return tmp;
831 } END_FOR_EACH_PTR_REVERSE(tmp);
832 return NULL;
835 int get_param_num_from_sym(struct symbol *sym)
837 struct symbol *tmp;
838 int i;
840 if (!cur_func_sym)
841 return -1;
843 i = 0;
844 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
845 if (tmp == sym)
846 return i;
847 i++;
848 } END_FOR_EACH_PTR(tmp);
849 return -1;
852 int get_param_num(struct expression *expr)
854 struct symbol *sym;
855 char *name;
857 if (!cur_func_sym)
858 return -1;
859 name = expr_to_var_sym(expr, &sym);
860 free_string(name);
861 if (!sym)
862 return -1;
863 return get_param_num_from_sym(sym);
866 int ms_since(struct timeval *start)
868 struct timeval end;
869 double diff;
871 gettimeofday(&end, NULL);
872 diff = (end.tv_sec - start->tv_sec) * 1000.0;
873 diff += (end.tv_usec - start->tv_usec) / 1000.0;
874 return (int)diff;
877 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
879 if (!name || !sym)
880 return 0;
882 if (parent_is_null_var_sym(name, sym) ||
883 parent_is_free_var_sym(name, sym))
884 return 1;
885 return 0;
888 int parent_is_gone(struct expression *expr)
890 struct symbol *sym;
891 char *var;
892 int ret = 0;
894 expr = strip_expr(expr);
895 var = expr_to_var_sym(expr, &sym);
896 if (!var || !sym)
897 goto free;
898 ret = parent_is_gone_var_sym(var, sym);
899 free:
900 free_string(var);
901 return ret;
904 int invert_op(int op)
906 switch (op) {
907 case '*':
908 return '/';
909 case '/':
910 return '*';
911 case '+':
912 return '-';
913 case '-':
914 return '+';
915 case SPECIAL_LEFTSHIFT:
916 return SPECIAL_RIGHTSHIFT;
917 case SPECIAL_RIGHTSHIFT:
918 return SPECIAL_LEFTSHIFT;
920 return 0;
923 int expr_equiv(struct expression *one, struct expression *two)
925 struct symbol *one_sym, *two_sym;
926 char *one_name = NULL;
927 char *two_name = NULL;
928 int ret = 0;
930 if (!one || !two)
931 return 0;
932 if (one->type != two->type)
933 return 0;
935 one_name = expr_to_str_sym(one, &one_sym);
936 if (!one_name || !one_sym)
937 goto free;
938 two_name = expr_to_str_sym(two, &two_sym);
939 if (!two_name || !two_sym)
940 goto free;
941 if (one_sym != two_sym)
942 goto free;
943 if (strcmp(one_name, two_name) == 0)
944 ret = 1;
945 free:
946 free_string(one_name);
947 free_string(two_name);
948 return ret;
951 void push_int(struct int_stack **stack, int num)
953 int *munged;
956 * Just put the int on directly instead of a pointer to the int.
957 * Shift it to the left because Sparse uses the last two bits.
958 * This is sort of a dirty hack, yes.
961 munged = INT_PTR(num << 2);
963 add_ptr_list(stack, munged);
966 int pop_int(struct int_stack **stack)
968 int *num;
970 num = last_ptr_list((struct ptr_list *)*stack);
971 delete_ptr_list_last((struct ptr_list **)stack);
973 return PTR_INT(num) >> 2;