db: split success/fail returns as best we can
[smatch.git] / smatch_helper.c
blobc0f217947caeebcc2497d6e6fe4e12704ec9ad8e
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 struct symbol *type;
369 expr = strip_expr(expr);
370 if (!expr)
371 return 0;
373 if (expr->type == EXPR_PREOP || expr->op == '*') {
374 expr = strip_expr(expr->unop);
375 if (expr->type == EXPR_BINOP && expr->op == '+')
376 return 1;
379 if (expr->type != EXPR_BINOP || expr->op != '+')
380 return 0;
382 type = get_type(expr->left);
383 if (!type || type->type != SYM_ARRAY)
384 return 0;
386 return 1;
389 struct expression *get_array_base(struct expression *expr)
391 if (!is_array(expr))
392 return NULL;
393 expr = strip_expr(expr);
394 if (expr->type == EXPR_PREOP && expr->op == '*')
395 expr = strip_expr(expr->unop);
396 if (expr->type != EXPR_BINOP || expr->op != '+')
397 return NULL;
398 return strip_parens(expr->left);
401 struct expression *get_array_offset(struct expression *expr)
403 if (!is_array(expr))
404 return NULL;
405 expr = strip_expr(expr);
406 if (expr->type == EXPR_PREOP && expr->op == '*')
407 expr = strip_expr(expr->unop);
408 if (expr->type != EXPR_BINOP || expr->op != '+')
409 return NULL;
410 return strip_expr(expr->right);
413 const char *show_state(struct smatch_state *state)
415 if (!state)
416 return NULL;
417 return state->name;
420 struct statement *get_expression_statement(struct expression *expr)
422 /* What are those things called? if (({....; ret;})) { ...*/
424 if (expr->type != EXPR_PREOP)
425 return NULL;
426 if (expr->op != '(')
427 return NULL;
428 if (expr->unop->type != EXPR_STATEMENT)
429 return NULL;
430 if (expr->unop->statement->type != STMT_COMPOUND)
431 return NULL;
432 return expr->unop->statement;
435 struct expression *strip_parens(struct expression *expr)
437 if (!expr)
438 return NULL;
440 if (expr->type == EXPR_PREOP) {
441 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
442 expr->unop->statement->type == STMT_COMPOUND)
443 return expr;
444 if (expr->op == '(')
445 return strip_parens(expr->unop);
447 return expr;
450 struct expression *strip_expr(struct expression *expr)
452 if (!expr)
453 return NULL;
455 switch (expr->type) {
456 case EXPR_FORCE_CAST:
457 case EXPR_CAST:
458 return strip_expr(expr->cast_expression);
459 case EXPR_PREOP: {
460 struct expression *unop;
462 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
463 expr->unop->statement->type == STMT_COMPOUND)
464 return expr;
466 unop = strip_expr(expr->unop);
468 if (expr->op == '*' &&
469 unop->type == EXPR_PREOP && unop->op == '&') {
470 struct symbol *type = get_type(unop->unop);
472 if (type && type->type == SYM_ARRAY)
473 return expr;
474 return strip_expr(unop->unop);
477 if (expr->op == '(')
478 return unop;
480 return expr;
482 case EXPR_CONDITIONAL:
483 if (known_condition_true(expr->conditional)) {
484 if (expr->cond_true)
485 return strip_expr(expr->cond_true);
486 return strip_expr(expr->conditional);
488 if (known_condition_false(expr->conditional))
489 return strip_expr(expr->cond_false);
490 return expr;
491 case EXPR_CALL:
492 if (sym_name_is("__builtin_expect", expr->fn)) {
493 expr = get_argument_from_call_expr(expr->args, 0);
494 return strip_expr(expr);
496 return expr;
498 return expr;
501 static void delete_state_tracker(struct tracker *t)
503 delete_state(t->owner, t->name, t->sym);
504 __free_tracker(t);
507 void scoped_state(int my_id, const char *name, struct symbol *sym)
509 struct tracker *t;
511 t = alloc_tracker(my_id, name, sym);
512 add_scope_hook((scope_hook *)&delete_state_tracker, t);
515 int is_error_return(struct expression *expr)
517 struct symbol *cur_func = cur_func_sym;
518 sval_t sval;
520 if (!expr)
521 return 0;
522 if (cur_func->type != SYM_NODE)
523 return 0;
524 cur_func = get_base_type(cur_func);
525 if (cur_func->type != SYM_FN)
526 return 0;
527 cur_func = get_base_type(cur_func);
528 if (cur_func == &void_ctype)
529 return 0;
530 if (!get_implied_value(expr, &sval))
531 return 0;
532 if (sval.value < 0)
533 return 1;
534 if (cur_func->type == SYM_PTR && sval.value == 0)
535 return 1;
536 return 0;
539 int getting_address(void)
541 struct expression *tmp;
542 int i = 0;
543 int dot_ops = 0;
545 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
546 if (!i++)
547 continue;
548 if (tmp->type == EXPR_PREOP && tmp->op == '(')
549 continue;
550 if (tmp->op == '.' && !dot_ops++)
551 continue;
552 if (tmp->op == '&')
553 return 1;
554 return 0;
555 } END_FOR_EACH_PTR_REVERSE(tmp);
556 return 0;
559 char *get_member_name(struct expression *expr)
561 char buf[256];
562 struct symbol *sym;
564 expr = strip_expr(expr);
565 if (expr->type != EXPR_DEREF)
566 return NULL;
567 sym = get_type(expr->deref);
568 if (!sym)
569 return NULL;
570 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
571 if (sym->type == SYM_UNION) {
572 struct symbol *tmp;
574 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
575 if (tmp->ident) {
576 const char *member_name;
578 if (expr->member)
579 member_name = expr->member->name;
580 else
581 member_name = "unknown_member";
582 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, member_name);
583 return alloc_string(buf);
585 } END_FOR_EACH_PTR_REVERSE(tmp);
586 return NULL;
588 if (!sym->ident || !expr->member)
589 return NULL;
590 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
591 return alloc_string(buf);
594 int cmp_pos(struct position pos1, struct position pos2)
596 /* the stream position is ... */
597 if (pos1.stream > pos2.stream)
598 return -1;
599 if (pos1.stream < pos2.stream)
600 return 1;
602 if (pos1.line < pos2.line)
603 return -1;
604 if (pos1.line > pos2.line)
605 return 1;
607 if (pos1.pos < pos2.pos)
608 return -1;
609 if (pos1.pos > pos2.pos)
610 return 1;
612 return 0;
615 int positions_eq(struct position pos1, struct position pos2)
617 if (pos1.line != pos2.line)
618 return 0;
619 if (pos1.pos != pos2.pos)
620 return 0;
621 if (pos1.stream != pos2.stream)
622 return 0;
623 return 1;
626 struct statement *get_current_statement(void)
628 struct statement *prev, *tmp;
630 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
632 if (!prev || !get_macro_name(prev->pos))
633 return prev;
635 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
636 if (positions_eq(tmp->pos, prev->pos))
637 continue;
638 if (prev->pos.line > tmp->pos.line)
639 return prev;
640 return tmp;
641 } END_FOR_EACH_PTR_REVERSE(tmp);
642 return prev;
645 struct statement *get_prev_statement(void)
647 struct statement *tmp;
648 int i;
650 i = 0;
651 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
652 if (i++ == 1)
653 return tmp;
654 } END_FOR_EACH_PTR_REVERSE(tmp);
655 return NULL;
658 int get_param_num_from_sym(struct symbol *sym)
660 struct symbol *tmp;
661 int i;
663 if (!cur_func_sym)
664 return -1;
666 i = 0;
667 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
668 if (tmp == sym)
669 return i;
670 i++;
671 } END_FOR_EACH_PTR(tmp);
672 return -1;
675 int get_param_num(struct expression *expr)
677 struct symbol *sym;
678 char *name;
680 if (!cur_func_sym)
681 return -1;
682 name = expr_to_var_sym(expr, &sym);
683 free_string(name);
684 if (!sym)
685 return -1;
686 return get_param_num_from_sym(sym);
689 int ms_since(struct timeval *start)
691 struct timeval end;
692 double diff;
694 gettimeofday(&end, NULL);
695 diff = (end.tv_sec - start->tv_sec) * 1000.0;
696 diff += (end.tv_usec - start->tv_usec) / 1000.0;
697 return (int)diff;
700 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
702 if (!name || !sym)
703 return 0;
705 if (parent_is_null_var_sym(name, sym) ||
706 parent_is_free_var_sym(name, sym))
707 return 1;
708 return 0;
711 int parent_is_gone(struct expression *expr)
713 struct symbol *sym;
714 char *var;
715 int ret = 0;
717 expr = strip_expr(expr);
718 var = expr_to_var_sym(expr, &sym);
719 if (!var || !sym)
720 goto free;
721 ret = parent_is_gone_var_sym(var, sym);
722 free:
723 free_string(var);
724 return ret;