helper: change "&*foo" to "foo" at the lowest levels
[smatch.git] / smatch_helper.c
blob5a4fa3ddd9a9316f9bd20451ac2a15d863def7a4
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 int sym_name_is(const char *name, struct expression *expr)
347 if (!expr)
348 return 0;
349 if (expr->type != EXPR_SYMBOL)
350 return 0;
351 if (!strcmp(expr->symbol_name->name, name))
352 return 1;
353 return 0;
356 int is_zero(struct expression *expr)
358 sval_t sval;
360 if (get_value(expr, &sval) && sval.value == 0)
361 return 1;
362 return 0;
365 int is_array(struct expression *expr)
367 expr = strip_expr(expr);
368 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
369 return 0;
370 expr = expr->unop;
371 if (expr->op != '+')
372 return 0;
373 return 1;
376 struct expression *get_array_name(struct expression *expr)
378 if (!is_array(expr))
379 return NULL;
380 return strip_expr(expr->unop->left);
383 struct expression *get_array_offset(struct expression *expr)
385 if (!is_array(expr))
386 return NULL;
387 return expr->unop->right;
390 const char *show_state(struct smatch_state *state)
392 if (!state)
393 return NULL;
394 return state->name;
397 struct statement *get_expression_statement(struct expression *expr)
399 /* What are those things called? if (({....; ret;})) { ...*/
401 if (expr->type != EXPR_PREOP)
402 return NULL;
403 if (expr->op != '(')
404 return NULL;
405 if (expr->unop->type != EXPR_STATEMENT)
406 return NULL;
407 if (expr->unop->statement->type != STMT_COMPOUND)
408 return NULL;
409 return expr->unop->statement;
412 struct expression *strip_parens(struct expression *expr)
414 if (!expr)
415 return NULL;
417 if (expr->type == EXPR_PREOP) {
418 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
419 expr->unop->statement->type == STMT_COMPOUND)
420 return expr;
421 if (expr->op == '(')
422 return strip_parens(expr->unop);
424 return expr;
427 struct expression *strip_expr(struct expression *expr)
429 if (!expr)
430 return NULL;
432 switch (expr->type) {
433 case EXPR_FORCE_CAST:
434 case EXPR_CAST:
435 return strip_expr(expr->cast_expression);
436 case EXPR_PREOP: {
437 struct expression *unop;
439 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
440 expr->unop->statement->type == STMT_COMPOUND)
441 return expr;
443 unop = strip_expr(expr->unop);
445 if (expr->op == '*' &&
446 unop->type == EXPR_PREOP && unop->op == '&') {
447 struct symbol *type = get_type(unop->unop);
449 if (type && type->type == SYM_ARRAY)
450 return expr;
451 return strip_expr(unop->unop);
454 if (expr->op == '(')
455 return unop;
457 return expr;
459 case EXPR_CONDITIONAL:
460 if (known_condition_true(expr->conditional)) {
461 if (expr->cond_true)
462 return strip_expr(expr->cond_true);
463 return strip_expr(expr->conditional);
465 if (known_condition_false(expr->conditional))
466 return strip_expr(expr->cond_false);
467 return expr;
468 case EXPR_CALL:
469 if (sym_name_is("__builtin_expect", expr->fn)) {
470 expr = get_argument_from_call_expr(expr->args, 0);
471 return strip_expr(expr);
473 return expr;
475 return expr;
478 static void delete_state_tracker(struct tracker *t)
480 delete_state(t->owner, t->name, t->sym);
481 __free_tracker(t);
484 void scoped_state(int my_id, const char *name, struct symbol *sym)
486 struct tracker *t;
488 t = alloc_tracker(my_id, name, sym);
489 add_scope_hook((scope_hook *)&delete_state_tracker, t);
492 int is_error_return(struct expression *expr)
494 struct symbol *cur_func = cur_func_sym;
495 sval_t sval;
497 if (!expr)
498 return 0;
499 if (cur_func->type != SYM_NODE)
500 return 0;
501 cur_func = get_base_type(cur_func);
502 if (cur_func->type != SYM_FN)
503 return 0;
504 cur_func = get_base_type(cur_func);
505 if (cur_func == &void_ctype)
506 return 0;
507 if (!get_implied_value(expr, &sval))
508 return 0;
509 if (sval.value < 0)
510 return 1;
511 if (cur_func->type == SYM_PTR && sval.value == 0)
512 return 1;
513 return 0;
516 int getting_address(void)
518 struct expression *tmp;
519 int i = 0;
520 int dot_ops = 0;
522 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
523 if (!i++)
524 continue;
525 if (tmp->type == EXPR_PREOP && tmp->op == '(')
526 continue;
527 if (tmp->op == '.' && !dot_ops++)
528 continue;
529 if (tmp->op == '&')
530 return 1;
531 return 0;
532 } END_FOR_EACH_PTR_REVERSE(tmp);
533 return 0;
536 char *get_member_name(struct expression *expr)
538 char buf[256];
539 struct symbol *sym;
541 expr = strip_expr(expr);
542 if (expr->type != EXPR_DEREF)
543 return NULL;
544 sym = get_type(expr->deref);
545 if (!sym)
546 return NULL;
547 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
548 if (sym->type == SYM_UNION) {
549 struct symbol *tmp;
551 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
552 if (tmp->ident) {
553 const char *member_name;
555 if (expr->member)
556 member_name = expr->member->name;
557 else
558 member_name = "unknown_member";
559 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, member_name);
560 return alloc_string(buf);
562 } END_FOR_EACH_PTR_REVERSE(tmp);
563 return NULL;
565 if (!sym->ident || !expr->member)
566 return NULL;
567 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
568 return alloc_string(buf);
571 int cmp_pos(struct position pos1, struct position pos2)
573 /* the stream position is ... */
574 if (pos1.stream > pos2.stream)
575 return -1;
576 if (pos1.stream < pos2.stream)
577 return 1;
579 if (pos1.line < pos2.line)
580 return -1;
581 if (pos1.line > pos2.line)
582 return 1;
584 if (pos1.pos < pos2.pos)
585 return -1;
586 if (pos1.pos > pos2.pos)
587 return 1;
589 return 0;
592 int positions_eq(struct position pos1, struct position pos2)
594 if (pos1.line != pos2.line)
595 return 0;
596 if (pos1.pos != pos2.pos)
597 return 0;
598 if (pos1.stream != pos2.stream)
599 return 0;
600 return 1;
603 struct statement *get_current_statement(void)
605 struct statement *prev, *tmp;
607 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
609 if (!prev || !get_macro_name(prev->pos))
610 return prev;
612 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
613 if (positions_eq(tmp->pos, prev->pos))
614 continue;
615 if (prev->pos.line > tmp->pos.line)
616 return prev;
617 return tmp;
618 } END_FOR_EACH_PTR_REVERSE(tmp);
619 return prev;
622 struct statement *get_prev_statement(void)
624 struct statement *tmp;
625 int i;
627 i = 0;
628 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
629 if (i++ == 1)
630 return tmp;
631 } END_FOR_EACH_PTR_REVERSE(tmp);
632 return NULL;
635 int get_param_num_from_sym(struct symbol *sym)
637 struct symbol *tmp;
638 int i;
640 if (!cur_func_sym)
641 return -1;
643 i = 0;
644 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
645 if (tmp == sym)
646 return i;
647 i++;
648 } END_FOR_EACH_PTR(tmp);
649 return -1;
652 int get_param_num(struct expression *expr)
654 struct symbol *sym;
655 char *name;
657 if (!cur_func_sym)
658 return -1;
659 name = expr_to_var_sym(expr, &sym);
660 free_string(name);
661 if (!sym)
662 return -1;
663 return get_param_num_from_sym(sym);
666 int ms_since(struct timeval *start)
668 struct timeval end;
669 double diff;
671 gettimeofday(&end, NULL);
672 diff = (end.tv_sec - start->tv_sec) * 1000.0;
673 diff += (end.tv_usec - start->tv_usec) / 1000.0;
674 return (int)diff;
677 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
679 if (!name || !sym)
680 return 0;
682 if (parent_is_null_var_sym(name, sym) ||
683 parent_is_free_var_sym(name, sym))
684 return 1;
685 return 0;
688 int parent_is_gone(struct expression *expr)
690 struct symbol *sym;
691 char *var;
692 int ret = 0;
694 expr = strip_expr(expr);
695 var = expr_to_var_sym(expr, &sym);
696 if (!var || !sym)
697 goto free;
698 ret = parent_is_gone_var_sym(var, sym);
699 free:
700 free_string(var);
701 return ret;