db: member_info callback should pass the sm_state instead of the state
[smatch.git] / smatch_helper.c
blobfadcf73a7b27fd29a92632554756ba8b8a329429
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 void append(char *dest, const char *data, int buff_len)
76 strncat(dest, data, buff_len - strlen(dest) - 1);
80 * If you have "foo(a, b, 1);" then use
81 * get_argument_from_call_expr(expr, 0) to return the expression for
82 * a. Yes, it does start counting from 0.
84 struct expression *get_argument_from_call_expr(struct expression_list *args,
85 int num)
87 struct expression *expr;
88 int i = 0;
90 if (!args)
91 return NULL;
93 FOR_EACH_PTR(args, expr) {
94 if (i == num)
95 return expr;
96 i++;
97 } END_FOR_EACH_PTR(expr);
98 return NULL;
101 static struct expression *get_array_expr(struct expression *expr)
103 struct symbol *type;
105 if (expr->type != EXPR_BINOP || expr->op != '+')
106 return NULL;
108 type = get_type(expr->left);
109 if (!type || type->type != SYM_ARRAY)
110 return NULL;
111 return expr->left;
114 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
115 struct expression *expr, int len,
116 int *complicated, int no_parens)
118 switch (expr->type) {
119 case EXPR_DEREF: {
120 struct expression *deref;
121 int op;
123 deref = expr->deref;
124 op = deref->op;
125 if (op == '*') {
126 struct expression *unop = strip_expr(deref->unop);
128 if (unop->type == EXPR_PREOP && unop->op == '&') {
129 deref = unop->unop;
130 op = '.';
131 } else {
132 deref = deref->unop;
136 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
138 if (op == '*')
139 append(buf, "->", len);
140 else
141 append(buf, ".", len);
143 if (expr->member)
144 append(buf, expr->member->name, len);
145 else
146 append(buf, "unknown_member", len);
148 return;
150 case EXPR_SYMBOL:
151 if (expr->symbol_name)
152 append(buf, expr->symbol_name->name, len);
153 if (sym_ptr) {
154 if (*sym_ptr)
155 *complicated = 1;
156 *sym_ptr = expr->symbol;
158 return;
159 case EXPR_PREOP: {
160 const char *tmp;
162 if (get_expression_statement(expr)) {
163 *complicated = 2;
164 return;
167 if (expr->op == '(') {
168 if (!no_parens)
169 append(buf, "(", len);
170 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
171 tmp = show_special(expr->op);
172 append(buf, tmp, len);
174 __get_variable_from_expr(sym_ptr, buf, expr->unop,
175 len, complicated, no_parens);
177 if (expr->op == '(' && !no_parens)
178 append(buf, ")", len);
180 if (expr->op == SPECIAL_DECREMENT ||
181 expr->op == SPECIAL_INCREMENT)
182 *complicated = 1;
184 return;
186 case EXPR_POSTOP: {
187 const char *tmp;
189 __get_variable_from_expr(sym_ptr, buf, expr->unop,
190 len, complicated, no_parens);
191 tmp = show_special(expr->op);
192 append(buf, tmp, len);
194 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
195 *complicated = 1;
196 return;
198 case EXPR_ASSIGNMENT:
199 case EXPR_COMPARE:
200 case EXPR_LOGICAL:
201 case EXPR_BINOP: {
202 char tmp[10];
203 struct expression *array_expr;
205 *complicated = 1;
206 array_expr = get_array_expr(expr);
207 if (array_expr) {
208 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
209 append(buf, "[", len);
210 } else {
211 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
212 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
213 append(buf, tmp, len);
215 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
216 if (array_expr)
217 append(buf, "]", len);
218 return;
220 case EXPR_VALUE: {
221 char tmp[25];
223 *complicated = 1;
224 snprintf(tmp, 25, "%lld", expr->value);
225 append(buf, tmp, len);
226 return;
228 case EXPR_STRING:
229 append(buf, "\"", len);
230 if (expr->string)
231 append(buf, expr->string->data, len);
232 append(buf, "\"", len);
233 return;
234 case EXPR_CALL: {
235 struct expression *tmp;
236 int i;
238 *complicated = 1;
239 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
240 append(buf, "(", len);
241 i = 0;
242 FOR_EACH_PTR(expr->args, tmp) {
243 if (i++)
244 append(buf, ", ", len);
245 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
246 } END_FOR_EACH_PTR(tmp);
247 append(buf, ")", len);
248 return;
250 case EXPR_CAST:
251 __get_variable_from_expr(sym_ptr, buf,
252 expr->cast_expression, len,
253 complicated, no_parens);
254 return;
255 case EXPR_SIZEOF: {
256 int size;
257 char tmp[25];
259 if (expr->cast_type && get_base_type(expr->cast_type)) {
260 size = type_bytes(get_base_type(expr->cast_type));
261 snprintf(tmp, 25, "%d", size);
262 append(buf, tmp, len);
264 return;
266 case EXPR_IDENTIFIER:
267 *complicated = 1;
268 if (expr->expr_ident)
269 append(buf, expr->expr_ident->name, len);
270 return;
271 default:
272 *complicated = 1;
273 //printf("unknown type = %d\n", expr->type);
274 return;
279 * This is returns a stylized "c looking" representation of the
280 * variable name.
282 * It uses the same buffer every time so you have to save the result
283 * yourself if you want to keep it.
287 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
289 static char var_name[VAR_LEN];
290 int complicated = 0;
292 if (sym_ptr)
293 *sym_ptr = NULL;
294 var_name[0] = '\0';
296 if (!expr)
297 return NULL;
298 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
299 &complicated, 0);
300 if (complicated < 2)
301 return alloc_string(var_name);
302 else
303 return NULL;
306 char *expr_to_str(struct expression *expr)
308 return expr_to_str_sym(expr, NULL);
312 * get_variable_from_expr_simple() only returns simple variables.
313 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
314 * then it returns NULL.
316 char *expr_to_var_sym(struct expression *expr,
317 struct symbol **sym_ptr)
319 static char var_name[VAR_LEN];
320 int complicated = 0;
322 if (sym_ptr)
323 *sym_ptr = NULL;
324 var_name[0] = '\0';
326 if (!expr)
327 return NULL;
328 expr = strip_expr(expr);
329 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
330 &complicated, 1);
332 if (complicated) {
333 if (sym_ptr)
334 *sym_ptr = NULL;
335 return NULL;
337 return alloc_string(var_name);
340 char *expr_to_var(struct expression *expr)
342 return expr_to_var_sym(expr, NULL);
345 struct symbol *expr_to_sym(struct expression *expr)
347 struct symbol *sym;
348 char *name;
350 name = expr_to_var_sym(expr, &sym);
351 free_string(name);
352 return sym;
355 int sym_name_is(const char *name, struct expression *expr)
357 if (!expr)
358 return 0;
359 if (expr->type != EXPR_SYMBOL)
360 return 0;
361 if (!strcmp(expr->symbol_name->name, name))
362 return 1;
363 return 0;
366 int is_zero(struct expression *expr)
368 sval_t sval;
370 if (get_value(expr, &sval) && sval.value == 0)
371 return 1;
372 return 0;
375 int is_array(struct expression *expr)
377 struct symbol *type;
379 expr = strip_expr(expr);
380 if (!expr)
381 return 0;
383 if (expr->type == EXPR_PREOP && expr->op == '*') {
384 expr = strip_expr(expr->unop);
385 if (expr->type == EXPR_BINOP && expr->op == '+')
386 return 1;
389 if (expr->type != EXPR_BINOP || expr->op != '+')
390 return 0;
392 type = get_type(expr->left);
393 if (!type || type->type != SYM_ARRAY)
394 return 0;
396 return 1;
399 struct expression *get_array_base(struct expression *expr)
401 if (!is_array(expr))
402 return NULL;
403 expr = strip_expr(expr);
404 if (expr->type == EXPR_PREOP && expr->op == '*')
405 expr = strip_expr(expr->unop);
406 if (expr->type != EXPR_BINOP || expr->op != '+')
407 return NULL;
408 return strip_parens(expr->left);
411 struct expression *get_array_offset(struct expression *expr)
413 if (!is_array(expr))
414 return NULL;
415 expr = strip_expr(expr);
416 if (expr->type == EXPR_PREOP && expr->op == '*')
417 expr = strip_expr(expr->unop);
418 if (expr->type != EXPR_BINOP || expr->op != '+')
419 return NULL;
420 return strip_expr(expr->right);
423 const char *show_state(struct smatch_state *state)
425 if (!state)
426 return NULL;
427 return state->name;
430 struct statement *get_expression_statement(struct expression *expr)
432 /* What are those things called? if (({....; ret;})) { ...*/
434 if (expr->type != EXPR_PREOP)
435 return NULL;
436 if (expr->op != '(')
437 return NULL;
438 if (expr->unop->type != EXPR_STATEMENT)
439 return NULL;
440 if (expr->unop->statement->type != STMT_COMPOUND)
441 return NULL;
442 return expr->unop->statement;
445 struct expression *strip_parens(struct expression *expr)
447 if (!expr)
448 return NULL;
450 if (expr->type == EXPR_PREOP) {
451 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
452 expr->unop->statement->type == STMT_COMPOUND)
453 return expr;
454 if (expr->op == '(')
455 return strip_parens(expr->unop);
457 return expr;
460 struct expression *strip_expr(struct expression *expr)
462 if (!expr)
463 return NULL;
465 switch (expr->type) {
466 case EXPR_FORCE_CAST:
467 case EXPR_CAST:
468 return strip_expr(expr->cast_expression);
469 case EXPR_PREOP: {
470 struct expression *unop;
472 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
473 expr->unop->statement->type == STMT_COMPOUND)
474 return expr;
476 unop = strip_expr(expr->unop);
478 if (expr->op == '*' &&
479 unop->type == EXPR_PREOP && unop->op == '&') {
480 struct symbol *type = get_type(unop->unop);
482 if (type && type->type == SYM_ARRAY)
483 return expr;
484 return strip_expr(unop->unop);
487 if (expr->op == '(')
488 return unop;
490 return expr;
492 case EXPR_CONDITIONAL:
493 if (known_condition_true(expr->conditional)) {
494 if (expr->cond_true)
495 return strip_expr(expr->cond_true);
496 return strip_expr(expr->conditional);
498 if (known_condition_false(expr->conditional))
499 return strip_expr(expr->cond_false);
500 return expr;
501 case EXPR_CALL:
502 if (sym_name_is("__builtin_expect", expr->fn)) {
503 expr = get_argument_from_call_expr(expr->args, 0);
504 return strip_expr(expr);
506 return expr;
508 return expr;
511 static void delete_state_tracker(struct tracker *t)
513 delete_state(t->owner, t->name, t->sym);
514 __free_tracker(t);
517 void scoped_state(int my_id, const char *name, struct symbol *sym)
519 struct tracker *t;
521 t = alloc_tracker(my_id, name, sym);
522 add_scope_hook((scope_hook *)&delete_state_tracker, t);
525 int is_error_return(struct expression *expr)
527 struct symbol *cur_func = cur_func_sym;
528 sval_t sval;
530 if (!expr)
531 return 0;
532 if (cur_func->type != SYM_NODE)
533 return 0;
534 cur_func = get_base_type(cur_func);
535 if (cur_func->type != SYM_FN)
536 return 0;
537 cur_func = get_base_type(cur_func);
538 if (cur_func == &void_ctype)
539 return 0;
540 if (!get_implied_value(expr, &sval))
541 return 0;
542 if (sval.value < 0)
543 return 1;
544 if (cur_func->type == SYM_PTR && sval.value == 0)
545 return 1;
546 return 0;
549 int getting_address(void)
551 struct expression *tmp;
552 int i = 0;
553 int dot_ops = 0;
555 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
556 if (!i++)
557 continue;
558 if (tmp->type == EXPR_PREOP && tmp->op == '(')
559 continue;
560 if (tmp->op == '.' && !dot_ops++)
561 continue;
562 if (tmp->op == '&')
563 return 1;
564 return 0;
565 } END_FOR_EACH_PTR_REVERSE(tmp);
566 return 0;
569 char *get_member_name(struct expression *expr)
571 char buf[256];
572 struct symbol *sym;
574 expr = strip_expr(expr);
575 if (expr->type != EXPR_DEREF)
576 return NULL;
577 sym = get_type(expr->deref);
578 if (!sym)
579 return NULL;
580 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
581 if (sym->type == SYM_UNION) {
582 struct symbol *tmp;
584 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
585 if (tmp->ident) {
586 const char *member_name;
588 if (expr->member)
589 member_name = expr->member->name;
590 else
591 member_name = "unknown_member";
592 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, member_name);
593 return alloc_string(buf);
595 } END_FOR_EACH_PTR_REVERSE(tmp);
596 return NULL;
598 if (!sym->ident || !expr->member)
599 return NULL;
600 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
601 return alloc_string(buf);
604 int cmp_pos(struct position pos1, struct position pos2)
606 /* the stream position is ... */
607 if (pos1.stream > pos2.stream)
608 return -1;
609 if (pos1.stream < pos2.stream)
610 return 1;
612 if (pos1.line < pos2.line)
613 return -1;
614 if (pos1.line > pos2.line)
615 return 1;
617 if (pos1.pos < pos2.pos)
618 return -1;
619 if (pos1.pos > pos2.pos)
620 return 1;
622 return 0;
625 int positions_eq(struct position pos1, struct position pos2)
627 if (pos1.line != pos2.line)
628 return 0;
629 if (pos1.pos != pos2.pos)
630 return 0;
631 if (pos1.stream != pos2.stream)
632 return 0;
633 return 1;
636 struct statement *get_current_statement(void)
638 struct statement *prev, *tmp;
640 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
642 if (!prev || !get_macro_name(prev->pos))
643 return prev;
645 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
646 if (positions_eq(tmp->pos, prev->pos))
647 continue;
648 if (prev->pos.line > tmp->pos.line)
649 return prev;
650 return tmp;
651 } END_FOR_EACH_PTR_REVERSE(tmp);
652 return prev;
655 struct statement *get_prev_statement(void)
657 struct statement *tmp;
658 int i;
660 i = 0;
661 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
662 if (i++ == 1)
663 return tmp;
664 } END_FOR_EACH_PTR_REVERSE(tmp);
665 return NULL;
668 int get_param_num_from_sym(struct symbol *sym)
670 struct symbol *tmp;
671 int i;
673 if (!cur_func_sym)
674 return -1;
676 i = 0;
677 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
678 if (tmp == sym)
679 return i;
680 i++;
681 } END_FOR_EACH_PTR(tmp);
682 return -1;
685 int get_param_num(struct expression *expr)
687 struct symbol *sym;
688 char *name;
690 if (!cur_func_sym)
691 return -1;
692 name = expr_to_var_sym(expr, &sym);
693 free_string(name);
694 if (!sym)
695 return -1;
696 return get_param_num_from_sym(sym);
699 int ms_since(struct timeval *start)
701 struct timeval end;
702 double diff;
704 gettimeofday(&end, NULL);
705 diff = (end.tv_sec - start->tv_sec) * 1000.0;
706 diff += (end.tv_usec - start->tv_usec) / 1000.0;
707 return (int)diff;
710 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
712 if (!name || !sym)
713 return 0;
715 if (parent_is_null_var_sym(name, sym) ||
716 parent_is_free_var_sym(name, sym))
717 return 1;
718 return 0;
721 int parent_is_gone(struct expression *expr)
723 struct symbol *sym;
724 char *var;
725 int ret = 0;
727 expr = strip_expr(expr);
728 var = expr_to_var_sym(expr, &sym);
729 if (!var || !sym)
730 goto free;
731 ret = parent_is_gone_var_sym(var, sym);
732 free:
733 free_string(var);
734 return ret;