helper: improve get_complication_score()
[smatch.git] / smatch_helper.c
blob034e92d18349c162be2827d5693b39e54af6ae50
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;
145 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
147 if (op == '*')
148 append(buf, "->", len);
149 else
150 append(buf, ".", len);
152 if (expr->member)
153 append(buf, expr->member->name, len);
154 else
155 append(buf, "unknown_member", len);
157 return;
159 case EXPR_SYMBOL:
160 if (expr->symbol_name)
161 append(buf, expr->symbol_name->name, len);
162 if (sym_ptr) {
163 if (*sym_ptr)
164 *complicated = 1;
165 *sym_ptr = expr->symbol;
167 return;
168 case EXPR_PREOP: {
169 const char *tmp;
171 if (get_expression_statement(expr)) {
172 *complicated = 2;
173 return;
176 if (expr->op == '(') {
177 if (!no_parens)
178 append(buf, "(", len);
179 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
180 tmp = show_special(expr->op);
181 append(buf, tmp, len);
183 __get_variable_from_expr(sym_ptr, buf, expr->unop,
184 len, complicated, no_parens);
186 if (expr->op == '(' && !no_parens)
187 append(buf, ")", len);
189 if (expr->op == SPECIAL_DECREMENT ||
190 expr->op == SPECIAL_INCREMENT)
191 *complicated = 1;
193 return;
195 case EXPR_POSTOP: {
196 const char *tmp;
198 __get_variable_from_expr(sym_ptr, buf, expr->unop,
199 len, complicated, no_parens);
200 tmp = show_special(expr->op);
201 append(buf, tmp, len);
203 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
204 *complicated = 1;
205 return;
207 case EXPR_ASSIGNMENT:
208 case EXPR_COMPARE:
209 case EXPR_LOGICAL:
210 case EXPR_BINOP: {
211 char tmp[10];
212 struct expression *array_expr;
214 *complicated = 1;
215 array_expr = get_array_expr(expr);
216 if (array_expr) {
217 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
218 append(buf, "[", len);
219 } else {
220 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
221 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
222 append(buf, tmp, len);
224 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
225 if (array_expr)
226 append(buf, "]", len);
227 return;
229 case EXPR_VALUE: {
230 char tmp[25];
232 *complicated = 1;
233 snprintf(tmp, 25, "%lld", expr->value);
234 append(buf, tmp, len);
235 return;
237 case EXPR_STRING:
238 append(buf, "\"", len);
239 if (expr->string)
240 append(buf, expr->string->data, len);
241 append(buf, "\"", len);
242 return;
243 case EXPR_CALL: {
244 struct expression *tmp;
245 int i;
247 *complicated = 1;
248 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
249 append(buf, "(", len);
250 i = 0;
251 FOR_EACH_PTR(expr->args, tmp) {
252 if (i++)
253 append(buf, ", ", len);
254 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
255 } END_FOR_EACH_PTR(tmp);
256 append(buf, ")", len);
257 return;
259 case EXPR_CAST:
260 case EXPR_FORCE_CAST:
261 __get_variable_from_expr(sym_ptr, buf,
262 expr->cast_expression, len,
263 complicated, no_parens);
264 return;
265 case EXPR_SIZEOF: {
266 int size;
267 char tmp[25];
269 if (expr->cast_type && get_base_type(expr->cast_type)) {
270 size = type_bytes(get_base_type(expr->cast_type));
271 snprintf(tmp, 25, "%d", size);
272 append(buf, tmp, len);
274 return;
276 case EXPR_IDENTIFIER:
277 *complicated = 1;
278 if (expr->expr_ident)
279 append(buf, expr->expr_ident->name, len);
280 return;
281 default:
282 *complicated = 1;
283 //printf("unknown type = %d\n", expr->type);
284 return;
289 * This is returns a stylized "c looking" representation of the
290 * variable name.
292 * It uses the same buffer every time so you have to save the result
293 * yourself if you want to keep it.
297 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
299 static char var_name[VAR_LEN];
300 int complicated = 0;
302 if (sym_ptr)
303 *sym_ptr = NULL;
304 var_name[0] = '\0';
306 if (!expr)
307 return NULL;
308 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
309 &complicated, 0);
310 if (complicated < 2)
311 return alloc_string(var_name);
312 else
313 return NULL;
316 char *expr_to_str(struct expression *expr)
318 return expr_to_str_sym(expr, NULL);
322 * get_variable_from_expr_simple() only returns simple variables.
323 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
324 * then it returns NULL.
326 char *expr_to_var_sym(struct expression *expr,
327 struct symbol **sym_ptr)
329 static char var_name[VAR_LEN];
330 int complicated = 0;
332 if (sym_ptr)
333 *sym_ptr = NULL;
334 var_name[0] = '\0';
336 if (!expr)
337 return NULL;
338 expr = strip_expr(expr);
339 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
340 &complicated, 1);
342 if (complicated) {
343 if (sym_ptr)
344 *sym_ptr = NULL;
345 return NULL;
347 return alloc_string(var_name);
350 char *expr_to_var(struct expression *expr)
352 return expr_to_var_sym(expr, NULL);
355 struct symbol *expr_to_sym(struct expression *expr)
357 struct symbol *sym;
358 char *name;
360 name = expr_to_var_sym(expr, &sym);
361 free_string(name);
362 return sym;
365 int get_complication_score(struct expression *expr)
367 int score = 0;
369 expr = strip_expr(expr);
372 * Don't forget to keep get_complication_score() and store_all_links()
373 * in sync.
377 switch (expr->type) {
378 case EXPR_CALL:
379 return 999;
380 case EXPR_COMPARE:
381 case EXPR_BINOP:
382 score += get_complication_score(expr->left);
383 score += get_complication_score(expr->right);
384 return score;
385 case EXPR_SYMBOL:
386 if (is_local_variable(expr))
387 return 1;
388 return 999;
389 case EXPR_PREOP:
390 if (expr->op == '*')
391 return score + get_complication_score(expr->unop);
392 else
393 return 999;
394 break;
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, *left_name, *right_name;
407 struct symbol *tmp, *left_sym, *right_sym;
408 char buf[128];
410 if (vsl)
411 *vsl = NULL;
413 expr = strip_parens(expr);
414 if (!expr)
415 return NULL;
416 if (sym)
417 *sym = 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 if (expr->type != EXPR_BINOP)
430 return NULL;
431 if (expr->op != '-' && expr->op != '+')
432 return NULL;
434 if (vsl) {
435 *vsl = expr_to_vsl(expr);
436 if (!*vsl)
437 return NULL;
440 left_name = expr_to_var_sym(expr->left, &left_sym);
441 if (!left_name || !left_sym)
442 return NULL;
443 right_name = expr_to_var_sym(expr->right, &right_sym);
444 if (!right_name || !right_sym) {
445 free_string(left_name);
446 return NULL;
449 if (expr->op == '+') {
450 if (strcmp(left_name, right_name) > 0) {
451 char *tmp_name;
453 tmp = left_sym;
454 left_sym = right_sym;
455 right_sym = tmp;
457 tmp_name = left_name;
458 left_name = right_name;
459 right_name = tmp_name;
463 snprintf(buf, sizeof(buf), "%s %s %s", left_name, show_special(expr->op), right_name);
464 *sym = left_sym;
465 free_string(left_name);
466 free_string(right_name);
467 return alloc_string(buf);
470 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
472 return expr_to_chunk_helper(expr, sym, NULL);
475 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
477 return expr_to_chunk_helper(expr, sym, vsl);
480 int sym_name_is(const char *name, struct expression *expr)
482 if (!expr)
483 return 0;
484 if (expr->type != EXPR_SYMBOL)
485 return 0;
486 if (!strcmp(expr->symbol_name->name, name))
487 return 1;
488 return 0;
491 int is_zero(struct expression *expr)
493 sval_t sval;
495 if (get_value(expr, &sval) && sval.value == 0)
496 return 1;
497 return 0;
500 int is_array(struct expression *expr)
502 struct symbol *type;
504 expr = strip_expr(expr);
505 if (!expr)
506 return 0;
508 if (expr->type == EXPR_PREOP && expr->op == '*') {
509 expr = strip_expr(expr->unop);
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 == '*' &&
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 char *get_member_name(struct expression *expr)
696 char buf[256];
697 struct symbol *sym;
699 expr = strip_expr(expr);
700 if (expr->type != EXPR_DEREF)
701 return NULL;
702 if (!expr->member)
703 return NULL;
705 sym = get_type(expr->deref);
706 if (!sym)
707 return NULL;
708 if (sym->type == SYM_UNION) {
709 sym = expr_to_sym(expr->deref);
710 sym = get_real_base_type(sym);
711 if (sym && sym->type == SYM_PTR)
712 sym = get_real_base_type(sym);
713 if (!sym || !sym->ident) {
714 snprintf(buf, sizeof(buf), "(union hack)->%s", expr->member->name);
715 return alloc_string(buf);
718 if (!sym->ident)
719 return NULL;
720 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
721 return alloc_string(buf);
724 int cmp_pos(struct position pos1, struct position pos2)
726 /* the stream position is ... */
727 if (pos1.stream > pos2.stream)
728 return -1;
729 if (pos1.stream < pos2.stream)
730 return 1;
732 if (pos1.line < pos2.line)
733 return -1;
734 if (pos1.line > pos2.line)
735 return 1;
737 if (pos1.pos < pos2.pos)
738 return -1;
739 if (pos1.pos > pos2.pos)
740 return 1;
742 return 0;
745 int positions_eq(struct position pos1, struct position pos2)
747 if (pos1.line != pos2.line)
748 return 0;
749 if (pos1.pos != pos2.pos)
750 return 0;
751 if (pos1.stream != pos2.stream)
752 return 0;
753 return 1;
756 struct statement *get_current_statement(void)
758 struct statement *prev, *tmp;
760 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
762 if (!prev || !get_macro_name(prev->pos))
763 return prev;
765 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
766 if (positions_eq(tmp->pos, prev->pos))
767 continue;
768 if (prev->pos.line > tmp->pos.line)
769 return prev;
770 return tmp;
771 } END_FOR_EACH_PTR_REVERSE(tmp);
772 return prev;
775 struct statement *get_prev_statement(void)
777 struct statement *tmp;
778 int i;
780 i = 0;
781 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
782 if (i++ == 1)
783 return tmp;
784 } END_FOR_EACH_PTR_REVERSE(tmp);
785 return NULL;
788 int get_param_num_from_sym(struct symbol *sym)
790 struct symbol *tmp;
791 int i;
793 if (!cur_func_sym)
794 return -1;
796 i = 0;
797 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
798 if (tmp == sym)
799 return i;
800 i++;
801 } END_FOR_EACH_PTR(tmp);
802 return -1;
805 int get_param_num(struct expression *expr)
807 struct symbol *sym;
808 char *name;
810 if (!cur_func_sym)
811 return -1;
812 name = expr_to_var_sym(expr, &sym);
813 free_string(name);
814 if (!sym)
815 return -1;
816 return get_param_num_from_sym(sym);
819 int ms_since(struct timeval *start)
821 struct timeval end;
822 double diff;
824 gettimeofday(&end, NULL);
825 diff = (end.tv_sec - start->tv_sec) * 1000.0;
826 diff += (end.tv_usec - start->tv_usec) / 1000.0;
827 return (int)diff;
830 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
832 if (!name || !sym)
833 return 0;
835 if (parent_is_null_var_sym(name, sym) ||
836 parent_is_free_var_sym(name, sym))
837 return 1;
838 return 0;
841 int parent_is_gone(struct expression *expr)
843 struct symbol *sym;
844 char *var;
845 int ret = 0;
847 expr = strip_expr(expr);
848 var = expr_to_var_sym(expr, &sym);
849 if (!var || !sym)
850 goto free;
851 ret = parent_is_gone_var_sym(var, sym);
852 free:
853 free_string(var);
854 return ret;
857 int invert_op(int op)
859 switch (op) {
860 case '*':
861 return '/';
862 case '/':
863 return '*';
864 case '+':
865 return '-';
866 case '-':
867 return '+';
868 case SPECIAL_LEFTSHIFT:
869 return SPECIAL_RIGHTSHIFT;
870 case SPECIAL_RIGHTSHIFT:
871 return SPECIAL_LEFTSHIFT;
873 return 0;