Documentation: make me less confused
[smatch.git] / smatch_helper.c
blob91043277318401dd5429d9ea3a4720f44a3f42d8
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 case EXPR_FORCE_CAST:
261 __get_variable_from_expr(sym_ptr, buf,
262 expr->cast_expression, len,
263 complicated, no_parens);
264 return;
265 case EXPR_SIZEOF: {
266 int size;
267 char tmp[25];
269 if (expr->cast_type && get_base_type(expr->cast_type)) {
270 size = type_bytes(get_base_type(expr->cast_type));
271 snprintf(tmp, 25, "%d", size);
272 append(buf, tmp, len);
274 return;
276 case EXPR_IDENTIFIER:
277 *complicated = 1;
278 if (expr->expr_ident)
279 append(buf, expr->expr_ident->name, len);
280 return;
281 default:
282 *complicated = 1;
283 //printf("unknown type = %d\n", expr->type);
284 return;
289 * This is returns a stylized "c looking" representation of the
290 * variable name.
292 * It uses the same buffer every time so you have to save the result
293 * yourself if you want to keep it.
297 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
299 static char var_name[VAR_LEN];
300 int complicated = 0;
302 if (sym_ptr)
303 *sym_ptr = NULL;
304 var_name[0] = '\0';
306 if (!expr)
307 return NULL;
308 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
309 &complicated, 0);
310 if (complicated < 2)
311 return alloc_string(var_name);
312 else
313 return NULL;
316 char *expr_to_str(struct expression *expr)
318 return expr_to_str_sym(expr, NULL);
322 * get_variable_from_expr_simple() only returns simple variables.
323 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
324 * then it returns NULL.
326 char *expr_to_var_sym(struct expression *expr,
327 struct symbol **sym_ptr)
329 static char var_name[VAR_LEN];
330 int complicated = 0;
332 if (sym_ptr)
333 *sym_ptr = NULL;
334 var_name[0] = '\0';
336 if (!expr)
337 return NULL;
338 expr = strip_expr(expr);
339 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
340 &complicated, 1);
342 if (complicated) {
343 if (sym_ptr)
344 *sym_ptr = NULL;
345 return NULL;
347 return alloc_string(var_name);
350 char *expr_to_var(struct expression *expr)
352 return expr_to_var_sym(expr, NULL);
355 struct symbol *expr_to_sym(struct expression *expr)
357 struct symbol *sym;
358 char *name;
360 name = expr_to_var_sym(expr, &sym);
361 free_string(name);
362 return sym;
365 int sym_name_is(const char *name, struct expression *expr)
367 if (!expr)
368 return 0;
369 if (expr->type != EXPR_SYMBOL)
370 return 0;
371 if (!strcmp(expr->symbol_name->name, name))
372 return 1;
373 return 0;
376 int is_zero(struct expression *expr)
378 sval_t sval;
380 if (get_value(expr, &sval) && sval.value == 0)
381 return 1;
382 return 0;
385 int is_array(struct expression *expr)
387 struct symbol *type;
389 expr = strip_expr(expr);
390 if (!expr)
391 return 0;
393 if (expr->type == EXPR_PREOP && expr->op == '*') {
394 expr = strip_expr(expr->unop);
395 if (expr->type == EXPR_BINOP && expr->op == '+')
396 return 1;
399 if (expr->type != EXPR_BINOP || expr->op != '+')
400 return 0;
402 type = get_type(expr->left);
403 if (!type || type->type != SYM_ARRAY)
404 return 0;
406 return 1;
409 struct expression *get_array_base(struct expression *expr)
411 if (!is_array(expr))
412 return NULL;
413 expr = strip_expr(expr);
414 if (expr->type == EXPR_PREOP && expr->op == '*')
415 expr = strip_expr(expr->unop);
416 if (expr->type != EXPR_BINOP || expr->op != '+')
417 return NULL;
418 return strip_parens(expr->left);
421 struct expression *get_array_offset(struct expression *expr)
423 if (!is_array(expr))
424 return NULL;
425 expr = strip_expr(expr);
426 if (expr->type == EXPR_PREOP && expr->op == '*')
427 expr = strip_expr(expr->unop);
428 if (expr->type != EXPR_BINOP || expr->op != '+')
429 return NULL;
430 return strip_parens(expr->right);
433 const char *show_state(struct smatch_state *state)
435 if (!state)
436 return NULL;
437 return state->name;
440 struct statement *get_expression_statement(struct expression *expr)
442 /* What are those things called? if (({....; ret;})) { ...*/
444 if (expr->type != EXPR_PREOP)
445 return NULL;
446 if (expr->op != '(')
447 return NULL;
448 if (expr->unop->type != EXPR_STATEMENT)
449 return NULL;
450 if (expr->unop->statement->type != STMT_COMPOUND)
451 return NULL;
452 return expr->unop->statement;
455 struct expression *strip_parens(struct expression *expr)
457 if (!expr)
458 return NULL;
460 if (expr->type == EXPR_PREOP) {
461 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
462 expr->unop->statement->type == STMT_COMPOUND)
463 return expr;
464 if (expr->op == '(')
465 return strip_parens(expr->unop);
467 return expr;
470 struct expression *strip_expr(struct expression *expr)
472 if (!expr)
473 return NULL;
475 switch (expr->type) {
476 case EXPR_FORCE_CAST:
477 case EXPR_CAST:
478 return strip_expr(expr->cast_expression);
479 case EXPR_PREOP: {
480 struct expression *unop;
482 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
483 expr->unop->statement->type == STMT_COMPOUND)
484 return expr;
486 unop = strip_expr(expr->unop);
488 if (expr->op == '*' &&
489 unop->type == EXPR_PREOP && unop->op == '&') {
490 struct symbol *type = get_type(unop->unop);
492 if (type && type->type == SYM_ARRAY)
493 return expr;
494 return strip_expr(unop->unop);
497 if (expr->op == '(')
498 return unop;
500 return expr;
502 case EXPR_CONDITIONAL:
503 if (known_condition_true(expr->conditional)) {
504 if (expr->cond_true)
505 return strip_expr(expr->cond_true);
506 return strip_expr(expr->conditional);
508 if (known_condition_false(expr->conditional))
509 return strip_expr(expr->cond_false);
510 return expr;
511 case EXPR_CALL:
512 if (sym_name_is("__builtin_expect", expr->fn)) {
513 expr = get_argument_from_call_expr(expr->args, 0);
514 return strip_expr(expr);
516 return expr;
518 return expr;
521 static void delete_state_tracker(struct tracker *t)
523 delete_state(t->owner, t->name, t->sym);
524 __free_tracker(t);
527 void scoped_state(int my_id, const char *name, struct symbol *sym)
529 struct tracker *t;
531 t = alloc_tracker(my_id, name, sym);
532 add_scope_hook((scope_hook *)&delete_state_tracker, t);
535 int is_error_return(struct expression *expr)
537 struct symbol *cur_func = cur_func_sym;
538 sval_t sval;
540 if (!expr)
541 return 0;
542 if (cur_func->type != SYM_NODE)
543 return 0;
544 cur_func = get_base_type(cur_func);
545 if (cur_func->type != SYM_FN)
546 return 0;
547 cur_func = get_base_type(cur_func);
548 if (cur_func == &void_ctype)
549 return 0;
550 if (!get_implied_value(expr, &sval))
551 return 0;
552 if (sval.value < 0)
553 return 1;
554 if (cur_func->type == SYM_PTR && sval.value == 0)
555 return 1;
556 return 0;
559 int getting_address(void)
561 struct expression *tmp;
562 int i = 0;
563 int dot_ops = 0;
565 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
566 if (!i++)
567 continue;
568 if (tmp->type == EXPR_PREOP && tmp->op == '(')
569 continue;
570 if (tmp->op == '.' && !dot_ops++)
571 continue;
572 if (tmp->op == '&')
573 return 1;
574 return 0;
575 } END_FOR_EACH_PTR_REVERSE(tmp);
576 return 0;
579 char *get_member_name(struct expression *expr)
581 char buf[256];
582 struct symbol *sym;
584 expr = strip_expr(expr);
585 if (expr->type != EXPR_DEREF)
586 return NULL;
587 if (!expr->member)
588 return NULL;
590 sym = get_type(expr->deref);
591 if (!sym)
592 return NULL;
593 if (sym->type == SYM_UNION) {
594 sym = expr_to_sym(expr->deref);
595 sym = get_real_base_type(sym);
596 if (sym && sym->type == SYM_PTR)
597 sym = get_real_base_type(sym);
598 if (!sym || !sym->ident) {
599 snprintf(buf, sizeof(buf), "(union hack)->%s", expr->member->name);
600 return alloc_string(buf);
603 if (!sym->ident)
604 return NULL;
605 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
606 return alloc_string(buf);
609 int cmp_pos(struct position pos1, struct position pos2)
611 /* the stream position is ... */
612 if (pos1.stream > pos2.stream)
613 return -1;
614 if (pos1.stream < pos2.stream)
615 return 1;
617 if (pos1.line < pos2.line)
618 return -1;
619 if (pos1.line > pos2.line)
620 return 1;
622 if (pos1.pos < pos2.pos)
623 return -1;
624 if (pos1.pos > pos2.pos)
625 return 1;
627 return 0;
630 int positions_eq(struct position pos1, struct position pos2)
632 if (pos1.line != pos2.line)
633 return 0;
634 if (pos1.pos != pos2.pos)
635 return 0;
636 if (pos1.stream != pos2.stream)
637 return 0;
638 return 1;
641 struct statement *get_current_statement(void)
643 struct statement *prev, *tmp;
645 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
647 if (!prev || !get_macro_name(prev->pos))
648 return prev;
650 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
651 if (positions_eq(tmp->pos, prev->pos))
652 continue;
653 if (prev->pos.line > tmp->pos.line)
654 return prev;
655 return tmp;
656 } END_FOR_EACH_PTR_REVERSE(tmp);
657 return prev;
660 struct statement *get_prev_statement(void)
662 struct statement *tmp;
663 int i;
665 i = 0;
666 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
667 if (i++ == 1)
668 return tmp;
669 } END_FOR_EACH_PTR_REVERSE(tmp);
670 return NULL;
673 int get_param_num_from_sym(struct symbol *sym)
675 struct symbol *tmp;
676 int i;
678 if (!cur_func_sym)
679 return -1;
681 i = 0;
682 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
683 if (tmp == sym)
684 return i;
685 i++;
686 } END_FOR_EACH_PTR(tmp);
687 return -1;
690 int get_param_num(struct expression *expr)
692 struct symbol *sym;
693 char *name;
695 if (!cur_func_sym)
696 return -1;
697 name = expr_to_var_sym(expr, &sym);
698 free_string(name);
699 if (!sym)
700 return -1;
701 return get_param_num_from_sym(sym);
704 int ms_since(struct timeval *start)
706 struct timeval end;
707 double diff;
709 gettimeofday(&end, NULL);
710 diff = (end.tv_sec - start->tv_sec) * 1000.0;
711 diff += (end.tv_usec - start->tv_usec) / 1000.0;
712 return (int)diff;
715 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
717 if (!name || !sym)
718 return 0;
720 if (parent_is_null_var_sym(name, sym) ||
721 parent_is_free_var_sym(name, sym))
722 return 1;
723 return 0;
726 int parent_is_gone(struct expression *expr)
728 struct symbol *sym;
729 char *var;
730 int ret = 0;
732 expr = strip_expr(expr);
733 var = expr_to_var_sym(expr, &sym);
734 if (!var || !sym)
735 goto free;
736 ret = parent_is_gone_var_sym(var, sym);
737 free:
738 free_string(var);
739 return ret;
742 int invert_op(int op)
744 switch (op) {
745 case '*':
746 return '/';
747 case '/':
748 return '*';
749 case '+':
750 return '-';
751 case '-':
752 return '+';
753 case SPECIAL_LEFTSHIFT:
754 return SPECIAL_RIGHTSHIFT;
755 case SPECIAL_RIGHTSHIFT:
756 return SPECIAL_LEFTSHIFT;
758 return 0;