implied: remove unused add_pool() function
[smatch.git] / smatch_helper.c
blob9227b02ee0e8b65158d91c509a16679f9fe80cd1
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"
28 #define VAR_LEN 512
30 char *alloc_string(const char *str)
32 char *tmp;
34 if (!str)
35 return NULL;
36 tmp = malloc(strlen(str) + 1);
37 strcpy(tmp, str);
38 return tmp;
41 void free_string(char *str)
43 free(str);
46 void remove_parens(char *str)
48 char *src, *dst;
50 dst = src = str;
51 while (*src != '\0') {
52 if (*src == '(' || *src == ')') {
53 src++;
54 continue;
56 *dst++ = *src++;
58 *dst = *src;
61 struct smatch_state *alloc_state_num(int num)
63 struct smatch_state *state;
64 static char buff[256];
66 state = __alloc_smatch_state(0);
67 snprintf(buff, 255, "%d", num);
68 buff[255] = '\0';
69 state->name = alloc_string(buff);
70 state->data = INT_PTR(num);
71 return state;
74 struct smatch_state *alloc_state_str(const char *name)
76 struct smatch_state *state;
78 state = __alloc_smatch_state(0);
79 state->name = alloc_string(name);
80 return state;
83 void append(char *dest, const char *data, int buff_len)
85 strncat(dest, data, buff_len - strlen(dest) - 1);
89 * If you have "foo(a, b, 1);" then use
90 * get_argument_from_call_expr(expr, 0) to return the expression for
91 * a. Yes, it does start counting from 0.
93 struct expression *get_argument_from_call_expr(struct expression_list *args,
94 int num)
96 struct expression *expr;
97 int i = 0;
99 if (!args)
100 return NULL;
102 FOR_EACH_PTR(args, expr) {
103 if (i == num)
104 return expr;
105 i++;
106 } END_FOR_EACH_PTR(expr);
107 return NULL;
110 static struct expression *get_array_expr(struct expression *expr)
112 struct symbol *type;
114 if (expr->type != EXPR_BINOP || expr->op != '+')
115 return NULL;
117 type = get_type(expr->left);
118 if (!type || type->type != SYM_ARRAY)
119 return NULL;
120 return expr->left;
123 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
124 struct expression *expr, int len,
125 int *complicated, int no_parens)
127 switch (expr->type) {
128 case EXPR_DEREF: {
129 struct expression *deref;
130 int op;
132 deref = expr->deref;
133 op = deref->op;
134 if (op == '*') {
135 struct expression *unop = strip_expr(deref->unop);
137 if (unop->type == EXPR_PREOP && unop->op == '&') {
138 deref = unop->unop;
139 op = '.';
140 } else {
141 deref = deref->unop;
142 if (!is_pointer(deref))
143 op = '.';
147 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
149 if (op == '*')
150 append(buf, "->", len);
151 else
152 append(buf, ".", len);
154 if (expr->member)
155 append(buf, expr->member->name, len);
156 else
157 append(buf, "unknown_member", len);
159 return;
161 case EXPR_SYMBOL:
162 if (expr->symbol_name)
163 append(buf, expr->symbol_name->name, len);
164 if (sym_ptr) {
165 if (*sym_ptr)
166 *complicated = 1;
167 *sym_ptr = expr->symbol;
169 return;
170 case EXPR_PREOP: {
171 const char *tmp;
173 if (get_expression_statement(expr)) {
174 *complicated = 2;
175 return;
178 if (expr->op == '(') {
179 if (!no_parens)
180 append(buf, "(", len);
181 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
182 tmp = show_special(expr->op);
183 append(buf, tmp, len);
185 __get_variable_from_expr(sym_ptr, buf, expr->unop,
186 len, complicated, no_parens);
188 if (expr->op == '(' && !no_parens)
189 append(buf, ")", len);
191 if (expr->op == SPECIAL_DECREMENT ||
192 expr->op == SPECIAL_INCREMENT)
193 *complicated = 1;
195 return;
197 case EXPR_POSTOP: {
198 const char *tmp;
200 __get_variable_from_expr(sym_ptr, buf, expr->unop,
201 len, complicated, no_parens);
202 tmp = show_special(expr->op);
203 append(buf, tmp, len);
205 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
206 *complicated = 1;
207 return;
209 case EXPR_ASSIGNMENT:
210 case EXPR_COMPARE:
211 case EXPR_LOGICAL:
212 case EXPR_BINOP: {
213 char tmp[10];
214 struct expression *array_expr;
216 *complicated = 1;
217 array_expr = get_array_expr(expr);
218 if (array_expr) {
219 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
220 append(buf, "[", len);
221 } else {
222 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
223 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
224 append(buf, tmp, len);
226 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
227 if (array_expr)
228 append(buf, "]", len);
229 return;
231 case EXPR_VALUE: {
232 char tmp[25];
234 *complicated = 1;
235 snprintf(tmp, 25, "%lld", expr->value);
236 append(buf, tmp, len);
237 return;
239 case EXPR_STRING:
240 append(buf, "\"", len);
241 if (expr->string)
242 append(buf, expr->string->data, len);
243 append(buf, "\"", len);
244 return;
245 case EXPR_CALL: {
246 struct expression *tmp;
247 int i;
249 *complicated = 1;
250 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
251 append(buf, "(", len);
252 i = 0;
253 FOR_EACH_PTR(expr->args, tmp) {
254 if (i++)
255 append(buf, ", ", len);
256 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
257 } END_FOR_EACH_PTR(tmp);
258 append(buf, ")", len);
259 return;
261 case EXPR_CAST:
262 case EXPR_FORCE_CAST:
263 __get_variable_from_expr(sym_ptr, buf,
264 expr->cast_expression, len,
265 complicated, no_parens);
266 return;
267 case EXPR_SIZEOF: {
268 int size;
269 char tmp[25];
271 if (expr->cast_type && get_base_type(expr->cast_type)) {
272 size = type_bytes(get_base_type(expr->cast_type));
273 snprintf(tmp, 25, "%d", size);
274 append(buf, tmp, len);
276 return;
278 case EXPR_IDENTIFIER:
279 *complicated = 1;
280 if (expr->expr_ident)
281 append(buf, expr->expr_ident->name, len);
282 return;
283 default:
284 *complicated = 1;
285 //printf("unknown type = %d\n", expr->type);
286 return;
291 * This is returns a stylized "c looking" representation of the
292 * variable name.
294 * It uses the same buffer every time so you have to save the result
295 * yourself if you want to keep it.
299 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
301 static char var_name[VAR_LEN];
302 int complicated = 0;
304 if (sym_ptr)
305 *sym_ptr = NULL;
306 var_name[0] = '\0';
308 if (!expr)
309 return NULL;
310 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
311 &complicated, 0);
312 if (complicated < 2)
313 return alloc_string(var_name);
314 else
315 return NULL;
318 char *expr_to_str(struct expression *expr)
320 return expr_to_str_sym(expr, NULL);
324 * get_variable_from_expr_simple() only returns simple variables.
325 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
326 * then it returns NULL.
328 char *expr_to_var_sym(struct expression *expr,
329 struct symbol **sym_ptr)
331 static char var_name[VAR_LEN];
332 int complicated = 0;
334 if (sym_ptr)
335 *sym_ptr = NULL;
336 var_name[0] = '\0';
338 if (!expr)
339 return NULL;
340 expr = strip_expr(expr);
341 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
342 &complicated, 1);
344 if (complicated) {
345 if (sym_ptr)
346 *sym_ptr = NULL;
347 return NULL;
349 return alloc_string(var_name);
352 char *expr_to_var(struct expression *expr)
354 return expr_to_var_sym(expr, NULL);
357 struct symbol *expr_to_sym(struct expression *expr)
359 struct symbol *sym;
360 char *name;
362 name = expr_to_var_sym(expr, &sym);
363 free_string(name);
364 return sym;
367 int get_complication_score(struct expression *expr)
369 int score = 0;
371 expr = strip_expr(expr);
374 * Don't forget to keep get_complication_score() and store_all_links()
375 * in sync.
379 switch (expr->type) {
380 case EXPR_CALL:
381 return 999;
382 case EXPR_COMPARE:
383 case EXPR_BINOP:
384 score += get_complication_score(expr->left);
385 score += get_complication_score(expr->right);
386 return score;
387 case EXPR_SYMBOL:
388 if (is_local_variable(expr))
389 return 1;
390 return 999;
391 case EXPR_PREOP:
392 if (expr->op == '*')
393 return score + get_complication_score(expr->unop);
394 return 999;
395 case EXPR_DEREF:
396 return score + get_complication_score(expr->deref);
397 case EXPR_VALUE:
398 return 0;
399 default:
400 return 999;
404 char *expr_to_chunk_helper(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
406 char *name;
407 struct symbol *tmp;
408 int score;
410 if (vsl)
411 *vsl = NULL;
412 if (sym)
413 *sym = NULL;
415 expr = strip_parens(expr);
416 if (!expr)
417 return NULL;
419 name = expr_to_var_sym(expr, &tmp);
420 if (name && tmp) {
421 if (sym)
422 *sym = tmp;
423 if (vsl)
424 *vsl = expr_to_vsl(expr);
425 return name;
427 free_string(name);
429 score = get_complication_score(expr);
430 if (score <= 0 || score > 2)
431 return NULL;
433 if (vsl) {
434 *vsl = expr_to_vsl(expr);
435 if (!*vsl)
436 return NULL;
439 return expr_to_str(expr);
442 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
444 return expr_to_chunk_helper(expr, sym, NULL);
447 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
449 return expr_to_chunk_helper(expr, sym, vsl);
452 int sym_name_is(const char *name, struct expression *expr)
454 if (!expr)
455 return 0;
456 if (expr->type != EXPR_SYMBOL)
457 return 0;
458 if (!strcmp(expr->symbol_name->name, name))
459 return 1;
460 return 0;
463 int is_zero(struct expression *expr)
465 sval_t sval;
467 if (get_value(expr, &sval) && sval.value == 0)
468 return 1;
469 return 0;
472 int is_array(struct expression *expr)
474 struct symbol *type;
476 expr = strip_expr(expr);
477 if (!expr)
478 return 0;
480 if (expr->type == EXPR_PREOP && expr->op == '*') {
481 expr = strip_expr(expr->unop);
482 if (expr->type == EXPR_BINOP && expr->op == '+')
483 return 1;
486 if (expr->type != EXPR_BINOP || expr->op != '+')
487 return 0;
489 type = get_type(expr->left);
490 if (!type || type->type != SYM_ARRAY)
491 return 0;
493 return 1;
496 struct expression *get_array_base(struct expression *expr)
498 if (!is_array(expr))
499 return NULL;
500 expr = strip_expr(expr);
501 if (expr->type == EXPR_PREOP && expr->op == '*')
502 expr = strip_expr(expr->unop);
503 if (expr->type != EXPR_BINOP || expr->op != '+')
504 return NULL;
505 return strip_parens(expr->left);
508 struct expression *get_array_offset(struct expression *expr)
510 if (!is_array(expr))
511 return NULL;
512 expr = strip_expr(expr);
513 if (expr->type == EXPR_PREOP && expr->op == '*')
514 expr = strip_expr(expr->unop);
515 if (expr->type != EXPR_BINOP || expr->op != '+')
516 return NULL;
517 return strip_parens(expr->right);
520 const char *show_state(struct smatch_state *state)
522 if (!state)
523 return NULL;
524 return state->name;
527 struct statement *get_expression_statement(struct expression *expr)
529 /* What are those things called? if (({....; ret;})) { ...*/
531 if (expr->type != EXPR_PREOP)
532 return NULL;
533 if (expr->op != '(')
534 return NULL;
535 if (expr->unop->type != EXPR_STATEMENT)
536 return NULL;
537 if (expr->unop->statement->type != STMT_COMPOUND)
538 return NULL;
539 return expr->unop->statement;
542 struct expression *strip_parens(struct expression *expr)
544 if (!expr)
545 return NULL;
547 if (expr->type == EXPR_PREOP) {
548 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
549 expr->unop->statement->type == STMT_COMPOUND)
550 return expr;
551 if (expr->op == '(')
552 return strip_parens(expr->unop);
554 return expr;
557 struct expression *strip_expr(struct expression *expr)
559 if (!expr)
560 return NULL;
562 switch (expr->type) {
563 case EXPR_FORCE_CAST:
564 case EXPR_CAST:
565 return strip_expr(expr->cast_expression);
566 case EXPR_PREOP: {
567 struct expression *unop;
569 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
570 expr->unop->statement->type == STMT_COMPOUND)
571 return expr;
573 unop = strip_expr(expr->unop);
575 if (expr->op == '*' &&
576 unop->type == EXPR_PREOP && unop->op == '&') {
577 struct symbol *type = get_type(unop->unop);
579 if (type && type->type == SYM_ARRAY)
580 return expr;
581 return strip_expr(unop->unop);
584 if (expr->op == '(')
585 return unop;
587 return expr;
589 case EXPR_CONDITIONAL:
590 if (known_condition_true(expr->conditional)) {
591 if (expr->cond_true)
592 return strip_expr(expr->cond_true);
593 return strip_expr(expr->conditional);
595 if (known_condition_false(expr->conditional))
596 return strip_expr(expr->cond_false);
597 return expr;
598 case EXPR_CALL:
599 if (sym_name_is("__builtin_expect", expr->fn)) {
600 expr = get_argument_from_call_expr(expr->args, 0);
601 return strip_expr(expr);
603 return expr;
605 return expr;
608 static void delete_state_tracker(struct tracker *t)
610 delete_state(t->owner, t->name, t->sym);
611 __free_tracker(t);
614 void scoped_state(int my_id, const char *name, struct symbol *sym)
616 struct tracker *t;
618 t = alloc_tracker(my_id, name, sym);
619 add_scope_hook((scope_hook *)&delete_state_tracker, t);
622 int is_error_return(struct expression *expr)
624 struct symbol *cur_func = cur_func_sym;
625 sval_t sval;
627 if (!expr)
628 return 0;
629 if (cur_func->type != SYM_NODE)
630 return 0;
631 cur_func = get_base_type(cur_func);
632 if (cur_func->type != SYM_FN)
633 return 0;
634 cur_func = get_base_type(cur_func);
635 if (cur_func == &void_ctype)
636 return 0;
637 if (!get_implied_value(expr, &sval))
638 return 0;
639 if (sval.value < 0)
640 return 1;
641 if (cur_func->type == SYM_PTR && sval.value == 0)
642 return 1;
643 return 0;
646 int getting_address(void)
648 struct expression *tmp;
649 int i = 0;
650 int dot_ops = 0;
652 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
653 if (!i++)
654 continue;
655 if (tmp->type == EXPR_PREOP && tmp->op == '(')
656 continue;
657 if (tmp->op == '.' && !dot_ops++)
658 continue;
659 if (tmp->op == '&')
660 return 1;
661 return 0;
662 } END_FOR_EACH_PTR_REVERSE(tmp);
663 return 0;
666 char *get_member_name(struct expression *expr)
668 char buf[256];
669 struct symbol *sym;
671 expr = strip_expr(expr);
672 if (expr->type != EXPR_DEREF)
673 return NULL;
674 if (!expr->member)
675 return NULL;
677 sym = get_type(expr->deref);
678 if (!sym)
679 return NULL;
680 if (sym->type == SYM_UNION) {
681 sym = expr_to_sym(expr->deref);
682 sym = get_real_base_type(sym);
683 if (sym && sym->type == SYM_PTR)
684 sym = get_real_base_type(sym);
685 if (!sym || !sym->ident) {
686 snprintf(buf, sizeof(buf), "(union hack)->%s", expr->member->name);
687 return alloc_string(buf);
690 if (!sym->ident)
691 return NULL;
692 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
693 return alloc_string(buf);
696 int cmp_pos(struct position pos1, struct position pos2)
698 /* the stream position is ... */
699 if (pos1.stream > pos2.stream)
700 return -1;
701 if (pos1.stream < pos2.stream)
702 return 1;
704 if (pos1.line < pos2.line)
705 return -1;
706 if (pos1.line > pos2.line)
707 return 1;
709 if (pos1.pos < pos2.pos)
710 return -1;
711 if (pos1.pos > pos2.pos)
712 return 1;
714 return 0;
717 int positions_eq(struct position pos1, struct position pos2)
719 if (pos1.line != pos2.line)
720 return 0;
721 if (pos1.pos != pos2.pos)
722 return 0;
723 if (pos1.stream != pos2.stream)
724 return 0;
725 return 1;
728 struct statement *get_current_statement(void)
730 struct statement *prev, *tmp;
732 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
734 if (!prev || !get_macro_name(prev->pos))
735 return prev;
737 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
738 if (positions_eq(tmp->pos, prev->pos))
739 continue;
740 if (prev->pos.line > tmp->pos.line)
741 return prev;
742 return tmp;
743 } END_FOR_EACH_PTR_REVERSE(tmp);
744 return prev;
747 struct statement *get_prev_statement(void)
749 struct statement *tmp;
750 int i;
752 i = 0;
753 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
754 if (i++ == 1)
755 return tmp;
756 } END_FOR_EACH_PTR_REVERSE(tmp);
757 return NULL;
760 int get_param_num_from_sym(struct symbol *sym)
762 struct symbol *tmp;
763 int i;
765 if (!cur_func_sym)
766 return -1;
768 i = 0;
769 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
770 if (tmp == sym)
771 return i;
772 i++;
773 } END_FOR_EACH_PTR(tmp);
774 return -1;
777 int get_param_num(struct expression *expr)
779 struct symbol *sym;
780 char *name;
782 if (!cur_func_sym)
783 return -1;
784 name = expr_to_var_sym(expr, &sym);
785 free_string(name);
786 if (!sym)
787 return -1;
788 return get_param_num_from_sym(sym);
791 int ms_since(struct timeval *start)
793 struct timeval end;
794 double diff;
796 gettimeofday(&end, NULL);
797 diff = (end.tv_sec - start->tv_sec) * 1000.0;
798 diff += (end.tv_usec - start->tv_usec) / 1000.0;
799 return (int)diff;
802 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
804 if (!name || !sym)
805 return 0;
807 if (parent_is_null_var_sym(name, sym) ||
808 parent_is_free_var_sym(name, sym))
809 return 1;
810 return 0;
813 int parent_is_gone(struct expression *expr)
815 struct symbol *sym;
816 char *var;
817 int ret = 0;
819 expr = strip_expr(expr);
820 var = expr_to_var_sym(expr, &sym);
821 if (!var || !sym)
822 goto free;
823 ret = parent_is_gone_var_sym(var, sym);
824 free:
825 free_string(var);
826 return ret;
829 int invert_op(int op)
831 switch (op) {
832 case '*':
833 return '/';
834 case '/':
835 return '*';
836 case '+':
837 return '-';
838 case '-':
839 return '+';
840 case SPECIAL_LEFTSHIFT:
841 return SPECIAL_RIGHTSHIFT;
842 case SPECIAL_RIGHTSHIFT:
843 return SPECIAL_LEFTSHIFT;
845 return 0;
848 int expr_equiv(struct expression *one, struct expression *two)
850 struct symbol *one_sym, *two_sym;
851 char *one_name = NULL;
852 char *two_name = NULL;
853 int ret = 0;
855 if (!one || !two)
856 return 0;
857 if (one->type != two->type)
858 return 0;
860 one_name = expr_to_str_sym(one, &one_sym);
861 if (!one_name || !one_sym)
862 goto free;
863 two_name = expr_to_str_sym(two, &two_sym);
864 if (!two_name || !two_sym)
865 goto free;
866 if (one_sym != two_sym)
867 goto free;
868 if (strcmp(one_name, two_name) == 0)
869 ret = 1;
870 free:
871 free_string(one_name);
872 free_string(two_name);
873 return ret;