helper: in strip_expr() "*&foo" is basically the same as "foo"
[smatch.git] / smatch_helper.c
blob2ebe0718b10cc1f30b1645d2cb29ccbe944e125f
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"
27 #define VAR_LEN 512
29 char *alloc_string(const char *str)
31 char *tmp;
33 if (!str)
34 return NULL;
35 tmp = malloc(strlen(str) + 1);
36 strcpy(tmp, str);
37 return tmp;
40 void free_string(char *str)
42 free(str);
45 void remove_parens(char *str)
47 char *src, *dst;
49 dst = src = str;
50 while (*src != '\0') {
51 if (*src == '(' || *src == ')') {
52 src++;
53 continue;
55 *dst++ = *src++;
57 *dst = *src;
60 struct smatch_state *alloc_state_num(int num)
62 struct smatch_state *state;
63 static char buff[256];
65 state = __alloc_smatch_state(0);
66 snprintf(buff, 255, "%d", num);
67 buff[255] = '\0';
68 state->name = alloc_string(buff);
69 state->data = INT_PTR(num);
70 return state;
73 void append(char *dest, const char *data, int buff_len)
75 strncat(dest, data, buff_len - strlen(dest) - 1);
79 * If you have "foo(a, b, 1);" then use
80 * get_argument_from_call_expr(expr, 0) to return the expression for
81 * a. Yes, it does start counting from 0.
83 struct expression *get_argument_from_call_expr(struct expression_list *args,
84 int num)
86 struct expression *expr;
87 int i = 0;
89 if (!args)
90 return NULL;
92 FOR_EACH_PTR(args, expr) {
93 if (i == num)
94 return expr;
95 i++;
96 } END_FOR_EACH_PTR(expr);
97 return NULL;
100 static struct expression *get_array_expr(struct expression *expr)
102 struct symbol *type;
104 if (expr->type != EXPR_BINOP || expr->op != '+')
105 return NULL;
107 type = get_type(expr->left);
108 if (!type || type->type != SYM_ARRAY)
109 return NULL;
110 return expr->left;
113 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
114 struct expression *expr, int len,
115 int *complicated, int no_parens)
117 struct expression *tmp;
119 switch (expr->type) {
120 case EXPR_DEREF:
121 tmp = expr->deref;
122 if (tmp->op == '*')
123 tmp = tmp->unop;
125 __get_variable_from_expr(sym_ptr, buf, tmp, len, complicated, no_parens);
127 tmp = expr->deref;
128 if (tmp->op == '*')
129 append(buf, "->", len);
130 else
131 append(buf, ".", len);
133 if (expr->member)
134 append(buf, expr->member->name, len);
135 else
136 append(buf, "unknown_member", len);
138 return;
139 case EXPR_SYMBOL:
140 if (expr->symbol_name)
141 append(buf, expr->symbol_name->name, len);
142 if (sym_ptr) {
143 if (*sym_ptr)
144 *complicated = 1;
145 *sym_ptr = expr->symbol;
147 return;
148 case EXPR_PREOP: {
149 const char *tmp;
151 if (get_expression_statement(expr)) {
152 *complicated = 2;
153 return;
156 if (expr->op == '(') {
157 if (!no_parens)
158 append(buf, "(", len);
159 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
160 tmp = show_special(expr->op);
161 append(buf, tmp, len);
163 __get_variable_from_expr(sym_ptr, buf, expr->unop,
164 len, complicated, no_parens);
166 if (expr->op == '(' && !no_parens)
167 append(buf, ")", len);
169 if (expr->op == SPECIAL_DECREMENT ||
170 expr->op == SPECIAL_INCREMENT)
171 *complicated = 1;
173 return;
175 case EXPR_POSTOP: {
176 const char *tmp;
178 __get_variable_from_expr(sym_ptr, buf, expr->unop,
179 len, complicated, no_parens);
180 tmp = show_special(expr->op);
181 append(buf, tmp, len);
183 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
184 *complicated = 1;
185 return;
187 case EXPR_ASSIGNMENT:
188 case EXPR_COMPARE:
189 case EXPR_LOGICAL:
190 case EXPR_BINOP: {
191 char tmp[10];
192 struct expression *array_expr;
194 *complicated = 1;
195 array_expr = get_array_expr(expr);
196 if (array_expr) {
197 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
198 append(buf, "[", len);
199 } else {
200 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
201 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
202 append(buf, tmp, len);
204 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
205 if (array_expr)
206 append(buf, "]", len);
207 return;
209 case EXPR_VALUE: {
210 char tmp[25];
212 *complicated = 1;
213 snprintf(tmp, 25, "%lld", expr->value);
214 append(buf, tmp, len);
215 return;
217 case EXPR_STRING:
218 append(buf, "\"", len);
219 if (expr->string)
220 append(buf, expr->string->data, len);
221 append(buf, "\"", len);
222 return;
223 case EXPR_CALL: {
224 struct expression *tmp;
225 int i;
227 *complicated = 1;
228 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
229 append(buf, "(", len);
230 i = 0;
231 FOR_EACH_PTR(expr->args, tmp) {
232 if (i++)
233 append(buf, ", ", len);
234 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
235 } END_FOR_EACH_PTR(tmp);
236 append(buf, ")", len);
237 return;
239 case EXPR_CAST:
240 __get_variable_from_expr(sym_ptr, buf,
241 expr->cast_expression, len,
242 complicated, no_parens);
243 return;
244 case EXPR_SIZEOF: {
245 int size;
246 char tmp[25];
248 if (expr->cast_type && get_base_type(expr->cast_type)) {
249 size = type_bytes(get_base_type(expr->cast_type));
250 snprintf(tmp, 25, "%d", size);
251 append(buf, tmp, len);
253 return;
255 case EXPR_IDENTIFIER:
256 *complicated = 1;
257 if (expr->expr_ident)
258 append(buf, expr->expr_ident->name, len);
259 return;
260 default:
261 *complicated = 1;
262 //printf("unknown type = %d\n", expr->type);
263 return;
268 * This is returns a stylized "c looking" representation of the
269 * variable name.
271 * It uses the same buffer every time so you have to save the result
272 * yourself if you want to keep it.
276 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
278 static char var_name[VAR_LEN];
279 int complicated = 0;
281 if (sym_ptr)
282 *sym_ptr = NULL;
283 var_name[0] = '\0';
285 if (!expr)
286 return NULL;
287 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
288 &complicated, 0);
289 if (complicated < 2)
290 return alloc_string(var_name);
291 else
292 return NULL;
295 char *expr_to_str(struct expression *expr)
297 return expr_to_str_sym(expr, NULL);
301 * get_variable_from_expr_simple() only returns simple variables.
302 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
303 * then it returns NULL.
305 char *expr_to_var_sym(struct expression *expr,
306 struct symbol **sym_ptr)
308 static char var_name[VAR_LEN];
309 int complicated = 0;
311 if (sym_ptr)
312 *sym_ptr = NULL;
313 var_name[0] = '\0';
315 if (!expr)
316 return NULL;
317 expr = strip_expr(expr);
318 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
319 &complicated, 1);
321 if (complicated) {
322 if (sym_ptr)
323 *sym_ptr = NULL;
324 return NULL;
326 return alloc_string(var_name);
329 char *expr_to_var(struct expression *expr)
331 return expr_to_var_sym(expr, NULL);
334 int sym_name_is(const char *name, struct expression *expr)
336 if (!expr)
337 return 0;
338 if (expr->type != EXPR_SYMBOL)
339 return 0;
340 if (!strcmp(expr->symbol_name->name, name))
341 return 1;
342 return 0;
345 int is_zero(struct expression *expr)
347 sval_t sval;
349 if (get_value(expr, &sval) && sval.value == 0)
350 return 1;
351 return 0;
354 int is_array(struct expression *expr)
356 expr = strip_expr(expr);
357 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
358 return 0;
359 expr = expr->unop;
360 if (expr->op != '+')
361 return 0;
362 return 1;
365 struct expression *get_array_name(struct expression *expr)
367 if (!is_array(expr))
368 return NULL;
369 return strip_expr(expr->unop->left);
372 struct expression *get_array_offset(struct expression *expr)
374 if (!is_array(expr))
375 return NULL;
376 return expr->unop->right;
379 const char *show_state(struct smatch_state *state)
381 if (!state)
382 return NULL;
383 return state->name;
386 struct statement *get_expression_statement(struct expression *expr)
388 /* What are those things called? if (({....; ret;})) { ...*/
390 if (expr->type != EXPR_PREOP)
391 return NULL;
392 if (expr->op != '(')
393 return NULL;
394 if (expr->unop->type != EXPR_STATEMENT)
395 return NULL;
396 if (expr->unop->statement->type != STMT_COMPOUND)
397 return NULL;
398 return expr->unop->statement;
401 struct expression *strip_parens(struct expression *expr)
403 if (!expr)
404 return NULL;
406 if (expr->type == EXPR_PREOP) {
407 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
408 expr->unop->statement->type == STMT_COMPOUND)
409 return expr;
410 if (expr->op == '(')
411 return strip_parens(expr->unop);
413 return expr;
416 struct expression *strip_expr(struct expression *expr)
418 if (!expr)
419 return NULL;
421 switch (expr->type) {
422 case EXPR_FORCE_CAST:
423 case EXPR_CAST:
424 return strip_expr(expr->cast_expression);
425 case EXPR_PREOP: {
426 struct expression *unop;
428 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
429 expr->unop->statement->type == STMT_COMPOUND)
430 return expr;
432 unop = strip_expr(expr->unop);
434 if (expr->op == '*' &&
435 unop->type == EXPR_PREOP && unop->op == '&') {
436 struct symbol *type = get_type(unop->unop);
438 if (type && type->type == SYM_ARRAY)
439 return expr;
440 return strip_expr(unop->unop);
443 if (expr->op == '(')
444 return unop;
446 return expr;
448 case EXPR_CONDITIONAL:
449 if (known_condition_true(expr->conditional)) {
450 if (expr->cond_true)
451 return strip_expr(expr->cond_true);
452 return strip_expr(expr->conditional);
454 if (known_condition_false(expr->conditional))
455 return strip_expr(expr->cond_false);
456 return expr;
457 case EXPR_CALL:
458 if (sym_name_is("__builtin_expect", expr->fn)) {
459 expr = get_argument_from_call_expr(expr->args, 0);
460 return strip_expr(expr);
462 return expr;
464 return expr;
467 static void delete_state_tracker(struct tracker *t)
469 delete_state(t->owner, t->name, t->sym);
470 __free_tracker(t);
473 void scoped_state(int my_id, const char *name, struct symbol *sym)
475 struct tracker *t;
477 t = alloc_tracker(my_id, name, sym);
478 add_scope_hook((scope_hook *)&delete_state_tracker, t);
481 int is_error_return(struct expression *expr)
483 struct symbol *cur_func = cur_func_sym;
484 sval_t sval;
486 if (!expr)
487 return 0;
488 if (cur_func->type != SYM_NODE)
489 return 0;
490 cur_func = get_base_type(cur_func);
491 if (cur_func->type != SYM_FN)
492 return 0;
493 cur_func = get_base_type(cur_func);
494 if (cur_func == &void_ctype)
495 return 0;
496 if (!get_implied_value(expr, &sval))
497 return 0;
498 if (sval.value < 0)
499 return 1;
500 if (cur_func->type == SYM_PTR && sval.value == 0)
501 return 1;
502 return 0;
505 int getting_address(void)
507 struct expression *tmp;
508 int i = 0;
509 int dot_ops = 0;
511 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
512 if (!i++)
513 continue;
514 if (tmp->type == EXPR_PREOP && tmp->op == '(')
515 continue;
516 if (tmp->op == '.' && !dot_ops++)
517 continue;
518 if (tmp->op == '&')
519 return 1;
520 return 0;
521 } END_FOR_EACH_PTR_REVERSE(tmp);
522 return 0;
525 char *get_member_name(struct expression *expr)
527 char buf[256];
528 struct symbol *sym;
530 expr = strip_expr(expr);
531 if (expr->type != EXPR_DEREF)
532 return NULL;
533 sym = get_type(expr->deref);
534 if (!sym)
535 return NULL;
536 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
537 if (sym->type == SYM_UNION) {
538 struct symbol *tmp;
540 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
541 if (tmp->ident) {
542 const char *member_name;
544 if (expr->member)
545 member_name = expr->member->name;
546 else
547 member_name = "unknown_member";
548 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, member_name);
549 return alloc_string(buf);
551 } END_FOR_EACH_PTR_REVERSE(tmp);
552 return NULL;
554 if (!sym->ident || !expr->member)
555 return NULL;
556 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
557 return alloc_string(buf);
560 int cmp_pos(struct position pos1, struct position pos2)
562 /* the stream position is ... */
563 if (pos1.stream > pos2.stream)
564 return -1;
565 if (pos1.stream < pos2.stream)
566 return 1;
568 if (pos1.line < pos2.line)
569 return -1;
570 if (pos1.line > pos2.line)
571 return 1;
573 if (pos1.pos < pos2.pos)
574 return -1;
575 if (pos1.pos > pos2.pos)
576 return 1;
578 return 0;
581 int positions_eq(struct position pos1, struct position pos2)
583 if (pos1.line != pos2.line)
584 return 0;
585 if (pos1.pos != pos2.pos)
586 return 0;
587 if (pos1.stream != pos2.stream)
588 return 0;
589 return 1;
592 struct statement *get_current_statement(void)
594 struct statement *prev, *tmp;
596 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
598 if (!prev || !get_macro_name(prev->pos))
599 return prev;
601 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
602 if (positions_eq(tmp->pos, prev->pos))
603 continue;
604 if (prev->pos.line > tmp->pos.line)
605 return prev;
606 return tmp;
607 } END_FOR_EACH_PTR_REVERSE(tmp);
608 return prev;
611 struct statement *get_prev_statement(void)
613 struct statement *tmp;
614 int i;
616 i = 0;
617 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
618 if (i++ == 1)
619 return tmp;
620 } END_FOR_EACH_PTR_REVERSE(tmp);
621 return NULL;
624 int get_param_num_from_sym(struct symbol *sym)
626 struct symbol *tmp;
627 int i;
629 if (!cur_func_sym)
630 return -1;
632 i = 0;
633 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
634 if (tmp == sym)
635 return i;
636 i++;
637 } END_FOR_EACH_PTR(tmp);
638 return -1;
641 int get_param_num(struct expression *expr)
643 struct symbol *sym;
644 char *name;
646 if (!cur_func_sym)
647 return -1;
648 name = expr_to_var_sym(expr, &sym);
649 free_string(name);
650 if (!sym)
651 return -1;
652 return get_param_num_from_sym(sym);
655 int ms_since(struct timeval *start)
657 struct timeval end;
658 double diff;
660 gettimeofday(&end, NULL);
661 diff = (end.tv_sec - start->tv_sec) * 1000.0;
662 diff += (end.tv_usec - start->tv_usec) / 1000.0;
663 return (int)diff;
666 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
668 if (!name || !sym)
669 return 0;
671 if (parent_is_null_var_sym(name, sym) ||
672 parent_is_free_var_sym(name, sym))
673 return 1;
674 return 0;
677 int parent_is_gone(struct expression *expr)
679 struct symbol *sym;
680 char *var;
681 int ret = 0;
683 expr = strip_expr(expr);
684 var = expr_to_var_sym(expr, &sym);
685 if (!var || !sym)
686 goto free;
687 ret = parent_is_gone_var_sym(var, sym);
688 free:
689 free_string(var);
690 return ret;