debug: add __smatch_exit() which halts parsing
[smatch.git] / smatch_helper.c
blob177a25be6fcdf3828fada950477b4b0ea500d160
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 char *expr_to_chunk_helper(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
432 char *name;
433 struct symbol *tmp;
434 int score;
436 if (vsl)
437 *vsl = NULL;
438 if (sym)
439 *sym = NULL;
441 expr = strip_parens(expr);
442 if (!expr)
443 return NULL;
445 name = expr_to_var_sym(expr, &tmp);
446 if (name && tmp) {
447 if (sym)
448 *sym = tmp;
449 if (vsl)
450 *vsl = expr_to_vsl(expr);
451 return name;
453 free_string(name);
455 score = get_complication_score(expr);
456 if (score <= 0 || score > 2)
457 return NULL;
459 if (vsl) {
460 *vsl = expr_to_vsl(expr);
461 if (!*vsl)
462 return NULL;
465 return expr_to_str(expr);
468 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
470 return expr_to_chunk_helper(expr, sym, NULL);
473 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
475 return expr_to_chunk_helper(expr, sym, vsl);
478 int sym_name_is(const char *name, struct expression *expr)
480 if (!expr)
481 return 0;
482 if (expr->type != EXPR_SYMBOL)
483 return 0;
484 if (!strcmp(expr->symbol_name->name, name))
485 return 1;
486 return 0;
489 int is_zero(struct expression *expr)
491 sval_t sval;
493 if (get_value(expr, &sval) && sval.value == 0)
494 return 1;
495 return 0;
498 int is_array(struct expression *expr)
500 struct symbol *type;
502 expr = strip_expr(expr);
503 if (!expr)
504 return 0;
506 if (expr->type == EXPR_PREOP && expr->op == '*') {
507 expr = strip_expr(expr->unop);
508 if (!expr)
509 return 0;
510 if (expr->type == EXPR_BINOP && expr->op == '+')
511 return 1;
514 if (expr->type != EXPR_BINOP || expr->op != '+')
515 return 0;
517 type = get_type(expr->left);
518 if (!type || type->type != SYM_ARRAY)
519 return 0;
521 return 1;
524 struct expression *get_array_base(struct expression *expr)
526 if (!is_array(expr))
527 return NULL;
528 expr = strip_expr(expr);
529 if (expr->type == EXPR_PREOP && expr->op == '*')
530 expr = strip_expr(expr->unop);
531 if (expr->type != EXPR_BINOP || expr->op != '+')
532 return NULL;
533 return strip_parens(expr->left);
536 struct expression *get_array_offset(struct expression *expr)
538 if (!is_array(expr))
539 return NULL;
540 expr = strip_expr(expr);
541 if (expr->type == EXPR_PREOP && expr->op == '*')
542 expr = strip_expr(expr->unop);
543 if (expr->type != EXPR_BINOP || expr->op != '+')
544 return NULL;
545 return strip_parens(expr->right);
548 const char *show_state(struct smatch_state *state)
550 if (!state)
551 return NULL;
552 return state->name;
555 struct statement *get_expression_statement(struct expression *expr)
557 /* What are those things called? if (({....; ret;})) { ...*/
559 if (expr->type != EXPR_PREOP)
560 return NULL;
561 if (expr->op != '(')
562 return NULL;
563 if (expr->unop->type != EXPR_STATEMENT)
564 return NULL;
565 if (expr->unop->statement->type != STMT_COMPOUND)
566 return NULL;
567 return expr->unop->statement;
570 struct expression *strip_parens(struct expression *expr)
572 if (!expr)
573 return NULL;
575 if (expr->type == EXPR_PREOP) {
576 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
577 expr->unop->statement->type == STMT_COMPOUND)
578 return expr;
579 if (expr->op == '(')
580 return strip_parens(expr->unop);
582 return expr;
585 struct expression *strip_expr(struct expression *expr)
587 if (!expr)
588 return NULL;
590 switch (expr->type) {
591 case EXPR_FORCE_CAST:
592 case EXPR_CAST:
593 return strip_expr(expr->cast_expression);
594 case EXPR_PREOP: {
595 struct expression *unop;
597 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
598 expr->unop->statement->type == STMT_COMPOUND)
599 return expr;
601 unop = strip_expr(expr->unop);
603 if (expr->op == '*' && unop &&
604 unop->type == EXPR_PREOP && unop->op == '&') {
605 struct symbol *type = get_type(unop->unop);
607 if (type && type->type == SYM_ARRAY)
608 return expr;
609 return strip_expr(unop->unop);
612 if (expr->op == '(')
613 return unop;
615 return expr;
617 case EXPR_CONDITIONAL:
618 if (known_condition_true(expr->conditional)) {
619 if (expr->cond_true)
620 return strip_expr(expr->cond_true);
621 return strip_expr(expr->conditional);
623 if (known_condition_false(expr->conditional))
624 return strip_expr(expr->cond_false);
625 return expr;
626 case EXPR_CALL:
627 if (sym_name_is("__builtin_expect", expr->fn)) {
628 expr = get_argument_from_call_expr(expr->args, 0);
629 return strip_expr(expr);
631 return expr;
633 return expr;
636 static void delete_state_tracker(struct tracker *t)
638 delete_state(t->owner, t->name, t->sym);
639 __free_tracker(t);
642 void scoped_state(int my_id, const char *name, struct symbol *sym)
644 struct tracker *t;
646 t = alloc_tracker(my_id, name, sym);
647 add_scope_hook((scope_hook *)&delete_state_tracker, t);
650 int is_error_return(struct expression *expr)
652 struct symbol *cur_func = cur_func_sym;
653 sval_t sval;
655 if (!expr)
656 return 0;
657 if (cur_func->type != SYM_NODE)
658 return 0;
659 cur_func = get_base_type(cur_func);
660 if (cur_func->type != SYM_FN)
661 return 0;
662 cur_func = get_base_type(cur_func);
663 if (cur_func == &void_ctype)
664 return 0;
665 if (!get_implied_value(expr, &sval))
666 return 0;
667 if (sval.value < 0)
668 return 1;
669 if (cur_func->type == SYM_PTR && sval.value == 0)
670 return 1;
671 return 0;
674 int getting_address(void)
676 struct expression *tmp;
677 int i = 0;
678 int dot_ops = 0;
680 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
681 if (!i++)
682 continue;
683 if (tmp->type == EXPR_PREOP && tmp->op == '(')
684 continue;
685 if (tmp->op == '.' && !dot_ops++)
686 continue;
687 if (tmp->op == '&')
688 return 1;
689 return 0;
690 } END_FOR_EACH_PTR_REVERSE(tmp);
691 return 0;
694 int get_struct_and_member(struct expression *expr, const char **type, const char **member)
696 struct symbol *sym;
698 expr = strip_expr(expr);
699 if (expr->type != EXPR_DEREF)
700 return 0;
701 if (!expr->member)
702 return 0;
704 sym = get_type(expr->deref);
705 if (!sym)
706 return 0;
707 if (sym->type == SYM_UNION)
708 return 0;
709 if (!sym->ident)
710 return 0;
712 *type = sym->ident->name;
713 *member = expr->member->name;
714 return 1;
717 char *get_member_name(struct expression *expr)
719 char buf[256];
720 struct symbol *sym;
722 expr = strip_expr(expr);
723 if (expr->type != EXPR_DEREF)
724 return NULL;
725 if (!expr->member)
726 return NULL;
728 sym = get_type(expr->deref);
729 if (!sym)
730 return NULL;
731 if (sym->type == SYM_UNION) {
732 snprintf(buf, sizeof(buf), "(union %s)->%s",
733 sym->ident ? sym->ident->name : "anonymous",
734 expr->member->name);
735 return alloc_string(buf);
737 if (!sym->ident)
738 return NULL;
739 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
740 return alloc_string(buf);
743 int cmp_pos(struct position pos1, struct position pos2)
745 /* the stream position is ... */
746 if (pos1.stream > pos2.stream)
747 return -1;
748 if (pos1.stream < pos2.stream)
749 return 1;
751 if (pos1.line < pos2.line)
752 return -1;
753 if (pos1.line > pos2.line)
754 return 1;
756 if (pos1.pos < pos2.pos)
757 return -1;
758 if (pos1.pos > pos2.pos)
759 return 1;
761 return 0;
764 int positions_eq(struct position pos1, struct position pos2)
766 if (pos1.line != pos2.line)
767 return 0;
768 if (pos1.pos != pos2.pos)
769 return 0;
770 if (pos1.stream != pos2.stream)
771 return 0;
772 return 1;
775 struct statement *get_current_statement(void)
777 struct statement *prev, *tmp;
779 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
781 if (!prev || !get_macro_name(prev->pos))
782 return prev;
784 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
785 if (positions_eq(tmp->pos, prev->pos))
786 continue;
787 if (prev->pos.line > tmp->pos.line)
788 return prev;
789 return tmp;
790 } END_FOR_EACH_PTR_REVERSE(tmp);
791 return prev;
794 struct statement *get_prev_statement(void)
796 struct statement *tmp;
797 int i;
799 i = 0;
800 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
801 if (i++ == 1)
802 return tmp;
803 } END_FOR_EACH_PTR_REVERSE(tmp);
804 return NULL;
807 int get_param_num_from_sym(struct symbol *sym)
809 struct symbol *tmp;
810 int i;
812 if (!cur_func_sym)
813 return -1;
815 i = 0;
816 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
817 if (tmp == sym)
818 return i;
819 i++;
820 } END_FOR_EACH_PTR(tmp);
821 return -1;
824 int get_param_num(struct expression *expr)
826 struct symbol *sym;
827 char *name;
829 if (!cur_func_sym)
830 return -1;
831 name = expr_to_var_sym(expr, &sym);
832 free_string(name);
833 if (!sym)
834 return -1;
835 return get_param_num_from_sym(sym);
838 int ms_since(struct timeval *start)
840 struct timeval end;
841 double diff;
843 gettimeofday(&end, NULL);
844 diff = (end.tv_sec - start->tv_sec) * 1000.0;
845 diff += (end.tv_usec - start->tv_usec) / 1000.0;
846 return (int)diff;
849 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
851 if (!name || !sym)
852 return 0;
854 if (parent_is_null_var_sym(name, sym) ||
855 parent_is_free_var_sym(name, sym))
856 return 1;
857 return 0;
860 int parent_is_gone(struct expression *expr)
862 struct symbol *sym;
863 char *var;
864 int ret = 0;
866 expr = strip_expr(expr);
867 var = expr_to_var_sym(expr, &sym);
868 if (!var || !sym)
869 goto free;
870 ret = parent_is_gone_var_sym(var, sym);
871 free:
872 free_string(var);
873 return ret;
876 int invert_op(int op)
878 switch (op) {
879 case '*':
880 return '/';
881 case '/':
882 return '*';
883 case '+':
884 return '-';
885 case '-':
886 return '+';
887 case SPECIAL_LEFTSHIFT:
888 return SPECIAL_RIGHTSHIFT;
889 case SPECIAL_RIGHTSHIFT:
890 return SPECIAL_LEFTSHIFT;
892 return 0;
895 int expr_equiv(struct expression *one, struct expression *two)
897 struct symbol *one_sym, *two_sym;
898 char *one_name = NULL;
899 char *two_name = NULL;
900 int ret = 0;
902 if (!one || !two)
903 return 0;
904 if (one->type != two->type)
905 return 0;
907 one_name = expr_to_str_sym(one, &one_sym);
908 if (!one_name || !one_sym)
909 goto free;
910 two_name = expr_to_str_sym(two, &two_sym);
911 if (!two_name || !two_sym)
912 goto free;
913 if (one_sym != two_sym)
914 goto free;
915 if (strcmp(one_name, two_name) == 0)
916 ret = 1;
917 free:
918 free_string(one_name);
919 free_string(two_name);
920 return ret;
923 void push_int(struct int_stack **stack, int num)
925 int *munged;
928 * Just put the int on directly instead of a pointer to the int.
929 * Shift it to the left because Sparse uses the last two bits.
930 * This is sort of a dirty hack, yes.
933 munged = INT_PTR(num << 2);
935 add_ptr_list(stack, munged);
938 int pop_int(struct int_stack **stack)
940 int *num;
942 num = last_ptr_list((struct ptr_list *)*stack);
943 delete_ptr_list_last((struct ptr_list **)stack);
945 return PTR_INT(num) >> 2;