implied: revert "tiny speed up"
[smatch.git] / smatch_helper.c
blobcb1cc20e0ebae3b45058cf1778b6fac141dd24d8
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)
142 switch (expr->type) {
143 case EXPR_DEREF: {
144 struct expression *deref;
145 int op;
147 deref = expr->deref;
148 op = deref->op;
149 if (op == '*') {
150 struct expression *unop = strip_expr(deref->unop);
152 if (unop->type == EXPR_PREOP && unop->op == '&') {
153 deref = unop->unop;
154 op = '.';
155 } else {
156 deref = deref->unop;
157 if (!is_pointer(deref))
158 op = '.';
162 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
164 if (op == '*')
165 append(buf, "->", len);
166 else
167 append(buf, ".", len);
169 if (expr->member)
170 append(buf, expr->member->name, len);
171 else
172 append(buf, "unknown_member", len);
174 return;
176 case EXPR_SYMBOL:
177 if (expr->symbol_name)
178 append(buf, expr->symbol_name->name, len);
179 if (sym_ptr) {
180 if (*sym_ptr)
181 *complicated = 1;
182 *sym_ptr = expr->symbol;
184 return;
185 case EXPR_PREOP: {
186 const char *tmp;
188 if (get_expression_statement(expr)) {
189 *complicated = 2;
190 return;
193 if (expr->op == '(') {
194 if (!no_parens && expr->unop->type != EXPR_SYMBOL)
195 append(buf, "(", len);
196 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
197 tmp = show_special(expr->op);
198 append(buf, tmp, len);
200 __get_variable_from_expr(sym_ptr, buf, expr->unop,
201 len, complicated, no_parens);
203 if (expr->op == '(' && !no_parens && expr->unop->type != EXPR_SYMBOL)
204 append(buf, ")", len);
206 if (expr->op == SPECIAL_DECREMENT ||
207 expr->op == SPECIAL_INCREMENT)
208 *complicated = 1;
210 return;
212 case EXPR_POSTOP: {
213 const char *tmp;
215 __get_variable_from_expr(sym_ptr, buf, expr->unop,
216 len, complicated, no_parens);
217 tmp = show_special(expr->op);
218 append(buf, tmp, len);
220 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
221 *complicated = 1;
222 return;
224 case EXPR_ASSIGNMENT:
225 case EXPR_COMPARE:
226 case EXPR_LOGICAL:
227 case EXPR_BINOP: {
228 char tmp[10];
229 struct expression *array_expr;
231 *complicated = 1;
232 array_expr = get_array_expr(expr);
233 if (array_expr) {
234 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
235 append(buf, "[", len);
236 } else {
237 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
238 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
239 append(buf, tmp, len);
241 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
242 if (array_expr)
243 append(buf, "]", len);
244 return;
246 case EXPR_VALUE: {
247 char tmp[25];
249 *complicated = 1;
250 snprintf(tmp, 25, "%lld", expr->value);
251 append(buf, tmp, len);
252 return;
254 case EXPR_STRING:
255 append(buf, "\"", len);
256 if (expr->string)
257 append(buf, expr->string->data, len);
258 append(buf, "\"", len);
259 return;
260 case EXPR_CALL: {
261 struct expression *tmp;
262 int i;
264 *complicated = 1;
265 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
266 append(buf, "(", len);
267 i = 0;
268 FOR_EACH_PTR(expr->args, tmp) {
269 if (i++)
270 append(buf, ", ", len);
271 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
272 } END_FOR_EACH_PTR(tmp);
273 append(buf, ")", len);
274 return;
276 case EXPR_CAST:
277 case EXPR_FORCE_CAST:
278 __get_variable_from_expr(sym_ptr, buf,
279 expr->cast_expression, len,
280 complicated, no_parens);
281 return;
282 case EXPR_SIZEOF: {
283 int size;
284 char tmp[25];
286 if (expr->cast_type && get_base_type(expr->cast_type)) {
287 size = type_bytes(get_base_type(expr->cast_type));
288 snprintf(tmp, 25, "%d", size);
289 append(buf, tmp, len);
291 return;
293 case EXPR_IDENTIFIER:
294 *complicated = 1;
295 if (expr->expr_ident)
296 append(buf, expr->expr_ident->name, len);
297 return;
298 default:
299 *complicated = 1;
300 //printf("unknown type = %d\n", expr->type);
301 return;
306 * This is returns a stylized "c looking" representation of the
307 * variable name.
309 * It uses the same buffer every time so you have to save the result
310 * yourself if you want to keep it.
314 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
316 static char var_name[VAR_LEN];
317 int complicated = 0;
319 if (sym_ptr)
320 *sym_ptr = NULL;
321 var_name[0] = '\0';
323 if (!expr)
324 return NULL;
325 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
326 &complicated, 0);
327 if (complicated < 2)
328 return alloc_string(var_name);
329 else
330 return NULL;
333 char *expr_to_str(struct expression *expr)
335 return expr_to_str_sym(expr, NULL);
339 * get_variable_from_expr_simple() only returns simple variables.
340 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
341 * then it returns NULL.
343 char *expr_to_var_sym(struct expression *expr,
344 struct symbol **sym_ptr)
346 static char var_name[VAR_LEN];
347 int complicated = 0;
349 if (sym_ptr)
350 *sym_ptr = NULL;
351 var_name[0] = '\0';
353 if (!expr)
354 return NULL;
355 expr = strip_expr(expr);
356 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
357 &complicated, 1);
359 if (complicated) {
360 if (sym_ptr)
361 *sym_ptr = NULL;
362 return NULL;
364 return alloc_string(var_name);
367 char *expr_to_var(struct expression *expr)
369 return expr_to_var_sym(expr, NULL);
372 struct symbol *expr_to_sym(struct expression *expr)
374 struct symbol *sym;
375 char *name;
377 name = expr_to_var_sym(expr, &sym);
378 free_string(name);
379 return sym;
382 int get_complication_score(struct expression *expr)
384 int score = 0;
386 expr = strip_expr(expr);
389 * Don't forget to keep get_complication_score() and store_all_links()
390 * in sync.
394 switch (expr->type) {
395 case EXPR_CALL:
396 return 999;
397 case EXPR_COMPARE:
398 case EXPR_BINOP:
399 score += get_complication_score(expr->left);
400 score += get_complication_score(expr->right);
401 return score;
402 case EXPR_SYMBOL:
403 if (is_local_variable(expr))
404 return 1;
405 return 999;
406 case EXPR_PREOP:
407 if (expr->op == '*')
408 return score + get_complication_score(expr->unop);
409 return 999;
410 case EXPR_DEREF:
411 return score + get_complication_score(expr->deref);
412 case EXPR_VALUE:
413 return 0;
414 default:
415 return 999;
419 char *expr_to_chunk_helper(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
421 char *name;
422 struct symbol *tmp;
423 int score;
425 if (vsl)
426 *vsl = NULL;
427 if (sym)
428 *sym = NULL;
430 expr = strip_parens(expr);
431 if (!expr)
432 return NULL;
434 name = expr_to_var_sym(expr, &tmp);
435 if (name && tmp) {
436 if (sym)
437 *sym = tmp;
438 if (vsl)
439 *vsl = expr_to_vsl(expr);
440 return name;
442 free_string(name);
444 score = get_complication_score(expr);
445 if (score <= 0 || score > 2)
446 return NULL;
448 if (vsl) {
449 *vsl = expr_to_vsl(expr);
450 if (!*vsl)
451 return NULL;
454 return expr_to_str(expr);
457 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
459 return expr_to_chunk_helper(expr, sym, NULL);
462 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
464 return expr_to_chunk_helper(expr, sym, vsl);
467 int sym_name_is(const char *name, struct expression *expr)
469 if (!expr)
470 return 0;
471 if (expr->type != EXPR_SYMBOL)
472 return 0;
473 if (!strcmp(expr->symbol_name->name, name))
474 return 1;
475 return 0;
478 int is_zero(struct expression *expr)
480 sval_t sval;
482 if (get_value(expr, &sval) && sval.value == 0)
483 return 1;
484 return 0;
487 int is_array(struct expression *expr)
489 struct symbol *type;
491 expr = strip_expr(expr);
492 if (!expr)
493 return 0;
495 if (expr->type == EXPR_PREOP && expr->op == '*') {
496 expr = strip_expr(expr->unop);
497 if (expr->type == EXPR_BINOP && expr->op == '+')
498 return 1;
501 if (expr->type != EXPR_BINOP || expr->op != '+')
502 return 0;
504 type = get_type(expr->left);
505 if (!type || type->type != SYM_ARRAY)
506 return 0;
508 return 1;
511 struct expression *get_array_base(struct expression *expr)
513 if (!is_array(expr))
514 return NULL;
515 expr = strip_expr(expr);
516 if (expr->type == EXPR_PREOP && expr->op == '*')
517 expr = strip_expr(expr->unop);
518 if (expr->type != EXPR_BINOP || expr->op != '+')
519 return NULL;
520 return strip_parens(expr->left);
523 struct expression *get_array_offset(struct expression *expr)
525 if (!is_array(expr))
526 return NULL;
527 expr = strip_expr(expr);
528 if (expr->type == EXPR_PREOP && expr->op == '*')
529 expr = strip_expr(expr->unop);
530 if (expr->type != EXPR_BINOP || expr->op != '+')
531 return NULL;
532 return strip_parens(expr->right);
535 const char *show_state(struct smatch_state *state)
537 if (!state)
538 return NULL;
539 return state->name;
542 struct statement *get_expression_statement(struct expression *expr)
544 /* What are those things called? if (({....; ret;})) { ...*/
546 if (expr->type != EXPR_PREOP)
547 return NULL;
548 if (expr->op != '(')
549 return NULL;
550 if (expr->unop->type != EXPR_STATEMENT)
551 return NULL;
552 if (expr->unop->statement->type != STMT_COMPOUND)
553 return NULL;
554 return expr->unop->statement;
557 struct expression *strip_parens(struct expression *expr)
559 if (!expr)
560 return NULL;
562 if (expr->type == EXPR_PREOP) {
563 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
564 expr->unop->statement->type == STMT_COMPOUND)
565 return expr;
566 if (expr->op == '(')
567 return strip_parens(expr->unop);
569 return expr;
572 struct expression *strip_expr(struct expression *expr)
574 if (!expr)
575 return NULL;
577 switch (expr->type) {
578 case EXPR_FORCE_CAST:
579 case EXPR_CAST:
580 return strip_expr(expr->cast_expression);
581 case EXPR_PREOP: {
582 struct expression *unop;
584 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
585 expr->unop->statement->type == STMT_COMPOUND)
586 return expr;
588 unop = strip_expr(expr->unop);
590 if (expr->op == '*' &&
591 unop->type == EXPR_PREOP && unop->op == '&') {
592 struct symbol *type = get_type(unop->unop);
594 if (type && type->type == SYM_ARRAY)
595 return expr;
596 return strip_expr(unop->unop);
599 if (expr->op == '(')
600 return unop;
602 return expr;
604 case EXPR_CONDITIONAL:
605 if (known_condition_true(expr->conditional)) {
606 if (expr->cond_true)
607 return strip_expr(expr->cond_true);
608 return strip_expr(expr->conditional);
610 if (known_condition_false(expr->conditional))
611 return strip_expr(expr->cond_false);
612 return expr;
613 case EXPR_CALL:
614 if (sym_name_is("__builtin_expect", expr->fn)) {
615 expr = get_argument_from_call_expr(expr->args, 0);
616 return strip_expr(expr);
618 return expr;
620 return expr;
623 static void delete_state_tracker(struct tracker *t)
625 delete_state(t->owner, t->name, t->sym);
626 __free_tracker(t);
629 void scoped_state(int my_id, const char *name, struct symbol *sym)
631 struct tracker *t;
633 t = alloc_tracker(my_id, name, sym);
634 add_scope_hook((scope_hook *)&delete_state_tracker, t);
637 int is_error_return(struct expression *expr)
639 struct symbol *cur_func = cur_func_sym;
640 sval_t sval;
642 if (!expr)
643 return 0;
644 if (cur_func->type != SYM_NODE)
645 return 0;
646 cur_func = get_base_type(cur_func);
647 if (cur_func->type != SYM_FN)
648 return 0;
649 cur_func = get_base_type(cur_func);
650 if (cur_func == &void_ctype)
651 return 0;
652 if (!get_implied_value(expr, &sval))
653 return 0;
654 if (sval.value < 0)
655 return 1;
656 if (cur_func->type == SYM_PTR && sval.value == 0)
657 return 1;
658 return 0;
661 int getting_address(void)
663 struct expression *tmp;
664 int i = 0;
665 int dot_ops = 0;
667 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
668 if (!i++)
669 continue;
670 if (tmp->type == EXPR_PREOP && tmp->op == '(')
671 continue;
672 if (tmp->op == '.' && !dot_ops++)
673 continue;
674 if (tmp->op == '&')
675 return 1;
676 return 0;
677 } END_FOR_EACH_PTR_REVERSE(tmp);
678 return 0;
681 int get_struct_and_member(struct expression *expr, const char **type, const char **member)
683 struct symbol *sym;
685 expr = strip_expr(expr);
686 if (expr->type != EXPR_DEREF)
687 return 0;
688 if (!expr->member)
689 return 0;
691 sym = get_type(expr->deref);
692 if (!sym)
693 return 0;
694 if (sym->type == SYM_UNION)
695 return 0;
696 if (!sym->ident)
697 return 0;
699 *type = sym->ident->name;
700 *member = expr->member->name;
701 return 1;
704 char *get_member_name(struct expression *expr)
706 char buf[256];
707 struct symbol *sym;
709 expr = strip_expr(expr);
710 if (expr->type != EXPR_DEREF)
711 return NULL;
712 if (!expr->member)
713 return NULL;
715 sym = get_type(expr->deref);
716 if (!sym)
717 return NULL;
718 if (sym->type == SYM_UNION) {
719 sym = expr_to_sym(expr->deref);
720 sym = get_real_base_type(sym);
721 if (sym && sym->type == SYM_PTR)
722 sym = get_real_base_type(sym);
723 if (!sym || !sym->ident) {
724 snprintf(buf, sizeof(buf), "(union hack)->%s", expr->member->name);
725 return alloc_string(buf);
728 if (!sym->ident)
729 return NULL;
730 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
731 return alloc_string(buf);
734 int cmp_pos(struct position pos1, struct position pos2)
736 /* the stream position is ... */
737 if (pos1.stream > pos2.stream)
738 return -1;
739 if (pos1.stream < pos2.stream)
740 return 1;
742 if (pos1.line < pos2.line)
743 return -1;
744 if (pos1.line > pos2.line)
745 return 1;
747 if (pos1.pos < pos2.pos)
748 return -1;
749 if (pos1.pos > pos2.pos)
750 return 1;
752 return 0;
755 int positions_eq(struct position pos1, struct position pos2)
757 if (pos1.line != pos2.line)
758 return 0;
759 if (pos1.pos != pos2.pos)
760 return 0;
761 if (pos1.stream != pos2.stream)
762 return 0;
763 return 1;
766 struct statement *get_current_statement(void)
768 struct statement *prev, *tmp;
770 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
772 if (!prev || !get_macro_name(prev->pos))
773 return prev;
775 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
776 if (positions_eq(tmp->pos, prev->pos))
777 continue;
778 if (prev->pos.line > tmp->pos.line)
779 return prev;
780 return tmp;
781 } END_FOR_EACH_PTR_REVERSE(tmp);
782 return prev;
785 struct statement *get_prev_statement(void)
787 struct statement *tmp;
788 int i;
790 i = 0;
791 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
792 if (i++ == 1)
793 return tmp;
794 } END_FOR_EACH_PTR_REVERSE(tmp);
795 return NULL;
798 int get_param_num_from_sym(struct symbol *sym)
800 struct symbol *tmp;
801 int i;
803 if (!cur_func_sym)
804 return -1;
806 i = 0;
807 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
808 if (tmp == sym)
809 return i;
810 i++;
811 } END_FOR_EACH_PTR(tmp);
812 return -1;
815 int get_param_num(struct expression *expr)
817 struct symbol *sym;
818 char *name;
820 if (!cur_func_sym)
821 return -1;
822 name = expr_to_var_sym(expr, &sym);
823 free_string(name);
824 if (!sym)
825 return -1;
826 return get_param_num_from_sym(sym);
829 int ms_since(struct timeval *start)
831 struct timeval end;
832 double diff;
834 gettimeofday(&end, NULL);
835 diff = (end.tv_sec - start->tv_sec) * 1000.0;
836 diff += (end.tv_usec - start->tv_usec) / 1000.0;
837 return (int)diff;
840 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
842 if (!name || !sym)
843 return 0;
845 if (parent_is_null_var_sym(name, sym) ||
846 parent_is_free_var_sym(name, sym))
847 return 1;
848 return 0;
851 int parent_is_gone(struct expression *expr)
853 struct symbol *sym;
854 char *var;
855 int ret = 0;
857 expr = strip_expr(expr);
858 var = expr_to_var_sym(expr, &sym);
859 if (!var || !sym)
860 goto free;
861 ret = parent_is_gone_var_sym(var, sym);
862 free:
863 free_string(var);
864 return ret;
867 int invert_op(int op)
869 switch (op) {
870 case '*':
871 return '/';
872 case '/':
873 return '*';
874 case '+':
875 return '-';
876 case '-':
877 return '+';
878 case SPECIAL_LEFTSHIFT:
879 return SPECIAL_RIGHTSHIFT;
880 case SPECIAL_RIGHTSHIFT:
881 return SPECIAL_LEFTSHIFT;
883 return 0;
886 int expr_equiv(struct expression *one, struct expression *two)
888 struct symbol *one_sym, *two_sym;
889 char *one_name = NULL;
890 char *two_name = NULL;
891 int ret = 0;
893 if (!one || !two)
894 return 0;
895 if (one->type != two->type)
896 return 0;
898 one_name = expr_to_str_sym(one, &one_sym);
899 if (!one_name || !one_sym)
900 goto free;
901 two_name = expr_to_str_sym(two, &two_sym);
902 if (!two_name || !two_sym)
903 goto free;
904 if (one_sym != two_sym)
905 goto free;
906 if (strcmp(one_name, two_name) == 0)
907 ret = 1;
908 free:
909 free_string(one_name);
910 free_string(two_name);
911 return ret;
914 void push_int(struct int_stack **stack, int num)
916 int *munged;
919 * Just put the int on directly instead of a pointer to the int.
920 * Shift it to the left because Sparse uses the last two bits.
921 * This is sort of a dirty hack, yes.
924 munged = INT_PTR(num << 2);
926 add_ptr_list(stack, munged);
929 int pop_int(struct int_stack **stack)
931 int *num;
933 num = last_ptr_list((struct ptr_list *)*stack);
934 delete_ptr_list_last((struct ptr_list **)stack);
936 return PTR_INT(num) >> 2;