unreachable: don't complain about iterate_all_kinds()
[smatch.git] / smatch_helper.c
blob3c609854e15dfccd9f439397bc82bc3c6b8926e8
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 __get_variable_from_expr(sym_ptr, buf,
261 expr->cast_expression, len,
262 complicated, no_parens);
263 return;
264 case EXPR_SIZEOF: {
265 int size;
266 char tmp[25];
268 if (expr->cast_type && get_base_type(expr->cast_type)) {
269 size = type_bytes(get_base_type(expr->cast_type));
270 snprintf(tmp, 25, "%d", size);
271 append(buf, tmp, len);
273 return;
275 case EXPR_IDENTIFIER:
276 *complicated = 1;
277 if (expr->expr_ident)
278 append(buf, expr->expr_ident->name, len);
279 return;
280 default:
281 *complicated = 1;
282 //printf("unknown type = %d\n", expr->type);
283 return;
288 * This is returns a stylized "c looking" representation of the
289 * variable name.
291 * It uses the same buffer every time so you have to save the result
292 * yourself if you want to keep it.
296 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
298 static char var_name[VAR_LEN];
299 int complicated = 0;
301 if (sym_ptr)
302 *sym_ptr = NULL;
303 var_name[0] = '\0';
305 if (!expr)
306 return NULL;
307 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
308 &complicated, 0);
309 if (complicated < 2)
310 return alloc_string(var_name);
311 else
312 return NULL;
315 char *expr_to_str(struct expression *expr)
317 return expr_to_str_sym(expr, NULL);
321 * get_variable_from_expr_simple() only returns simple variables.
322 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
323 * then it returns NULL.
325 char *expr_to_var_sym(struct expression *expr,
326 struct symbol **sym_ptr)
328 static char var_name[VAR_LEN];
329 int complicated = 0;
331 if (sym_ptr)
332 *sym_ptr = NULL;
333 var_name[0] = '\0';
335 if (!expr)
336 return NULL;
337 expr = strip_expr(expr);
338 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
339 &complicated, 1);
341 if (complicated) {
342 if (sym_ptr)
343 *sym_ptr = NULL;
344 return NULL;
346 return alloc_string(var_name);
349 char *expr_to_var(struct expression *expr)
351 return expr_to_var_sym(expr, NULL);
354 struct symbol *expr_to_sym(struct expression *expr)
356 struct symbol *sym;
357 char *name;
359 name = expr_to_var_sym(expr, &sym);
360 free_string(name);
361 return sym;
364 int sym_name_is(const char *name, struct expression *expr)
366 if (!expr)
367 return 0;
368 if (expr->type != EXPR_SYMBOL)
369 return 0;
370 if (!strcmp(expr->symbol_name->name, name))
371 return 1;
372 return 0;
375 int is_zero(struct expression *expr)
377 sval_t sval;
379 if (get_value(expr, &sval) && sval.value == 0)
380 return 1;
381 return 0;
384 int is_array(struct expression *expr)
386 struct symbol *type;
388 expr = strip_expr(expr);
389 if (!expr)
390 return 0;
392 if (expr->type == EXPR_PREOP && expr->op == '*') {
393 expr = strip_expr(expr->unop);
394 if (expr->type == EXPR_BINOP && expr->op == '+')
395 return 1;
398 if (expr->type != EXPR_BINOP || expr->op != '+')
399 return 0;
401 type = get_type(expr->left);
402 if (!type || type->type != SYM_ARRAY)
403 return 0;
405 return 1;
408 struct expression *get_array_base(struct expression *expr)
410 if (!is_array(expr))
411 return NULL;
412 expr = strip_expr(expr);
413 if (expr->type == EXPR_PREOP && expr->op == '*')
414 expr = strip_expr(expr->unop);
415 if (expr->type != EXPR_BINOP || expr->op != '+')
416 return NULL;
417 return strip_parens(expr->left);
420 struct expression *get_array_offset(struct expression *expr)
422 if (!is_array(expr))
423 return NULL;
424 expr = strip_expr(expr);
425 if (expr->type == EXPR_PREOP && expr->op == '*')
426 expr = strip_expr(expr->unop);
427 if (expr->type != EXPR_BINOP || expr->op != '+')
428 return NULL;
429 return strip_expr(expr->right);
432 const char *show_state(struct smatch_state *state)
434 if (!state)
435 return NULL;
436 return state->name;
439 struct statement *get_expression_statement(struct expression *expr)
441 /* What are those things called? if (({....; ret;})) { ...*/
443 if (expr->type != EXPR_PREOP)
444 return NULL;
445 if (expr->op != '(')
446 return NULL;
447 if (expr->unop->type != EXPR_STATEMENT)
448 return NULL;
449 if (expr->unop->statement->type != STMT_COMPOUND)
450 return NULL;
451 return expr->unop->statement;
454 struct expression *strip_parens(struct expression *expr)
456 if (!expr)
457 return NULL;
459 if (expr->type == EXPR_PREOP) {
460 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
461 expr->unop->statement->type == STMT_COMPOUND)
462 return expr;
463 if (expr->op == '(')
464 return strip_parens(expr->unop);
466 return expr;
469 struct expression *strip_expr(struct expression *expr)
471 if (!expr)
472 return NULL;
474 switch (expr->type) {
475 case EXPR_FORCE_CAST:
476 case EXPR_CAST:
477 return strip_expr(expr->cast_expression);
478 case EXPR_PREOP: {
479 struct expression *unop;
481 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
482 expr->unop->statement->type == STMT_COMPOUND)
483 return expr;
485 unop = strip_expr(expr->unop);
487 if (expr->op == '*' &&
488 unop->type == EXPR_PREOP && unop->op == '&') {
489 struct symbol *type = get_type(unop->unop);
491 if (type && type->type == SYM_ARRAY)
492 return expr;
493 return strip_expr(unop->unop);
496 if (expr->op == '(')
497 return unop;
499 return expr;
501 case EXPR_CONDITIONAL:
502 if (known_condition_true(expr->conditional)) {
503 if (expr->cond_true)
504 return strip_expr(expr->cond_true);
505 return strip_expr(expr->conditional);
507 if (known_condition_false(expr->conditional))
508 return strip_expr(expr->cond_false);
509 return expr;
510 case EXPR_CALL:
511 if (sym_name_is("__builtin_expect", expr->fn)) {
512 expr = get_argument_from_call_expr(expr->args, 0);
513 return strip_expr(expr);
515 return expr;
517 return expr;
520 static void delete_state_tracker(struct tracker *t)
522 delete_state(t->owner, t->name, t->sym);
523 __free_tracker(t);
526 void scoped_state(int my_id, const char *name, struct symbol *sym)
528 struct tracker *t;
530 t = alloc_tracker(my_id, name, sym);
531 add_scope_hook((scope_hook *)&delete_state_tracker, t);
534 int is_error_return(struct expression *expr)
536 struct symbol *cur_func = cur_func_sym;
537 sval_t sval;
539 if (!expr)
540 return 0;
541 if (cur_func->type != SYM_NODE)
542 return 0;
543 cur_func = get_base_type(cur_func);
544 if (cur_func->type != SYM_FN)
545 return 0;
546 cur_func = get_base_type(cur_func);
547 if (cur_func == &void_ctype)
548 return 0;
549 if (!get_implied_value(expr, &sval))
550 return 0;
551 if (sval.value < 0)
552 return 1;
553 if (cur_func->type == SYM_PTR && sval.value == 0)
554 return 1;
555 return 0;
558 int getting_address(void)
560 struct expression *tmp;
561 int i = 0;
562 int dot_ops = 0;
564 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
565 if (!i++)
566 continue;
567 if (tmp->type == EXPR_PREOP && tmp->op == '(')
568 continue;
569 if (tmp->op == '.' && !dot_ops++)
570 continue;
571 if (tmp->op == '&')
572 return 1;
573 return 0;
574 } END_FOR_EACH_PTR_REVERSE(tmp);
575 return 0;
578 char *get_member_name(struct expression *expr)
580 char buf[256];
581 struct symbol *sym;
583 expr = strip_expr(expr);
584 if (expr->type != EXPR_DEREF)
585 return NULL;
586 if (!expr->member)
587 return NULL;
589 sym = get_type(expr->deref);
590 if (!sym)
591 return NULL;
592 if (sym->type == SYM_UNION) {
593 sym = expr_to_sym(expr->deref);
594 sym = get_real_base_type(sym);
595 if (sym && sym->type == SYM_PTR)
596 sym = get_real_base_type(sym);
597 if (!sym || !sym->ident) {
598 snprintf(buf, sizeof(buf), "(union hack)->%s", expr->member->name);
599 return alloc_string(buf);
602 if (!sym->ident)
603 return NULL;
604 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
605 return alloc_string(buf);
608 int cmp_pos(struct position pos1, struct position pos2)
610 /* the stream position is ... */
611 if (pos1.stream > pos2.stream)
612 return -1;
613 if (pos1.stream < pos2.stream)
614 return 1;
616 if (pos1.line < pos2.line)
617 return -1;
618 if (pos1.line > pos2.line)
619 return 1;
621 if (pos1.pos < pos2.pos)
622 return -1;
623 if (pos1.pos > pos2.pos)
624 return 1;
626 return 0;
629 int positions_eq(struct position pos1, struct position pos2)
631 if (pos1.line != pos2.line)
632 return 0;
633 if (pos1.pos != pos2.pos)
634 return 0;
635 if (pos1.stream != pos2.stream)
636 return 0;
637 return 1;
640 struct statement *get_current_statement(void)
642 struct statement *prev, *tmp;
644 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
646 if (!prev || !get_macro_name(prev->pos))
647 return prev;
649 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
650 if (positions_eq(tmp->pos, prev->pos))
651 continue;
652 if (prev->pos.line > tmp->pos.line)
653 return prev;
654 return tmp;
655 } END_FOR_EACH_PTR_REVERSE(tmp);
656 return prev;
659 struct statement *get_prev_statement(void)
661 struct statement *tmp;
662 int i;
664 i = 0;
665 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
666 if (i++ == 1)
667 return tmp;
668 } END_FOR_EACH_PTR_REVERSE(tmp);
669 return NULL;
672 int get_param_num_from_sym(struct symbol *sym)
674 struct symbol *tmp;
675 int i;
677 if (!cur_func_sym)
678 return -1;
680 i = 0;
681 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
682 if (tmp == sym)
683 return i;
684 i++;
685 } END_FOR_EACH_PTR(tmp);
686 return -1;
689 int get_param_num(struct expression *expr)
691 struct symbol *sym;
692 char *name;
694 if (!cur_func_sym)
695 return -1;
696 name = expr_to_var_sym(expr, &sym);
697 free_string(name);
698 if (!sym)
699 return -1;
700 return get_param_num_from_sym(sym);
703 int ms_since(struct timeval *start)
705 struct timeval end;
706 double diff;
708 gettimeofday(&end, NULL);
709 diff = (end.tv_sec - start->tv_sec) * 1000.0;
710 diff += (end.tv_usec - start->tv_usec) / 1000.0;
711 return (int)diff;
714 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
716 if (!name || !sym)
717 return 0;
719 if (parent_is_null_var_sym(name, sym) ||
720 parent_is_free_var_sym(name, sym))
721 return 1;
722 return 0;
725 int parent_is_gone(struct expression *expr)
727 struct symbol *sym;
728 char *var;
729 int ret = 0;
731 expr = strip_expr(expr);
732 var = expr_to_var_sym(expr, &sym);
733 if (!var || !sym)
734 goto free;
735 ret = parent_is_gone_var_sym(var, sym);
736 free:
737 free_string(var);
738 return ret;
741 int invert_op(int op)
743 switch (op) {
744 case '*':
745 return '/';
746 case '/':
747 return '*';
748 case '+':
749 return '-';
750 case '-':
751 return '+';
752 case SPECIAL_LEFTSHIFT:
753 return SPECIAL_RIGHTSHIFT;
754 case SPECIAL_RIGHTSHIFT:
755 return SPECIAL_LEFTSHIFT;
757 return 0;