data/kernel.sizeof_param.remove: add __dynamic_pr_debug()
[smatch.git] / smatch_helper.c
blob45397b62d009893c9512a88508841438cf9b753b
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 && expr->member->name)
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 append(buf, expr->string->data, len);
220 append(buf, "\"", len);
221 return;
222 case EXPR_CALL: {
223 struct expression *tmp;
224 int i;
226 *complicated = 1;
227 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
228 append(buf, "(", len);
229 i = 0;
230 FOR_EACH_PTR(expr->args, tmp) {
231 if (i++)
232 append(buf, ", ", len);
233 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
234 } END_FOR_EACH_PTR(tmp);
235 append(buf, ")", len);
236 return;
238 case EXPR_CAST:
239 __get_variable_from_expr(sym_ptr, buf,
240 expr->cast_expression, len,
241 complicated, no_parens);
242 return;
243 case EXPR_SIZEOF: {
244 int size;
245 char tmp[25];
247 if (expr->cast_type && get_base_type(expr->cast_type)) {
248 size = type_bytes(get_base_type(expr->cast_type));
249 snprintf(tmp, 25, "%d", size);
250 append(buf, tmp, len);
252 return;
254 case EXPR_IDENTIFIER:
255 *complicated = 1;
256 if (expr->expr_ident && expr->expr_ident->name)
257 append(buf, expr->expr_ident->name, len);
258 return;
259 default:
260 *complicated = 1;
261 //printf("unknown type = %d\n", expr->type);
262 return;
267 * This is returns a stylized "c looking" representation of the
268 * variable name.
270 * It uses the same buffer every time so you have to save the result
271 * yourself if you want to keep it.
275 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
277 static char var_name[VAR_LEN];
278 int complicated = 0;
280 if (sym_ptr)
281 *sym_ptr = NULL;
282 var_name[0] = '\0';
284 if (!expr)
285 return NULL;
286 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
287 &complicated, 0);
288 if (complicated < 2)
289 return alloc_string(var_name);
290 else
291 return NULL;
294 char *expr_to_str(struct expression *expr)
296 return expr_to_str_sym(expr, NULL);
300 * get_variable_from_expr_simple() only returns simple variables.
301 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
302 * then it returns NULL.
304 char *expr_to_var_sym(struct expression *expr,
305 struct symbol **sym_ptr)
307 static char var_name[VAR_LEN];
308 int complicated = 0;
310 if (sym_ptr)
311 *sym_ptr = NULL;
312 var_name[0] = '\0';
314 if (!expr)
315 return NULL;
316 expr = strip_expr(expr);
317 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
318 &complicated, 1);
320 if (complicated) {
321 if (sym_ptr)
322 *sym_ptr = NULL;
323 return NULL;
325 return alloc_string(var_name);
328 char *expr_to_var(struct expression *expr)
330 return expr_to_var_sym(expr, NULL);
333 int sym_name_is(const char *name, struct expression *expr)
335 if (!expr)
336 return 0;
337 if (expr->type != EXPR_SYMBOL)
338 return 0;
339 if (!strcmp(expr->symbol_name->name, name))
340 return 1;
341 return 0;
344 int is_zero(struct expression *expr)
346 sval_t sval;
348 if (get_value(expr, &sval) && sval.value == 0)
349 return 1;
350 return 0;
353 int is_array(struct expression *expr)
355 expr = strip_expr(expr);
356 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
357 return 0;
358 expr = expr->unop;
359 if (expr->op != '+')
360 return 0;
361 return 1;
364 struct expression *get_array_name(struct expression *expr)
366 if (!is_array(expr))
367 return NULL;
368 return strip_expr(expr->unop->left);
371 struct expression *get_array_offset(struct expression *expr)
373 if (!is_array(expr))
374 return NULL;
375 return expr->unop->right;
378 const char *show_state(struct smatch_state *state)
380 if (!state)
381 return NULL;
382 return state->name;
385 struct statement *get_expression_statement(struct expression *expr)
387 /* What are those things called? if (({....; ret;})) { ...*/
389 if (expr->type != EXPR_PREOP)
390 return NULL;
391 if (expr->op != '(')
392 return NULL;
393 if (expr->unop->type != EXPR_STATEMENT)
394 return NULL;
395 if (expr->unop->statement->type != STMT_COMPOUND)
396 return NULL;
397 return expr->unop->statement;
400 struct expression *strip_parens(struct expression *expr)
402 if (!expr)
403 return NULL;
405 if (expr->type == EXPR_PREOP) {
406 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
407 expr->unop->statement->type == STMT_COMPOUND)
408 return expr;
409 if (expr->op == '(')
410 return strip_parens(expr->unop);
412 return expr;
415 struct expression *strip_expr(struct expression *expr)
417 if (!expr)
418 return NULL;
420 switch (expr->type) {
421 case EXPR_FORCE_CAST:
422 case EXPR_CAST:
423 return strip_expr(expr->cast_expression);
424 case EXPR_PREOP:
425 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
426 expr->unop->statement->type == STMT_COMPOUND)
427 return expr;
428 if (expr->op == '(')
429 return strip_expr(expr->unop);
431 return expr;
434 static void delete_state_tracker(struct tracker *t)
436 delete_state(t->owner, t->name, t->sym);
437 __free_tracker(t);
440 void scoped_state(int my_id, const char *name, struct symbol *sym)
442 struct tracker *t;
444 t = alloc_tracker(my_id, name, sym);
445 add_scope_hook((scope_hook *)&delete_state_tracker, t);
448 int is_error_return(struct expression *expr)
450 struct symbol *cur_func = cur_func_sym;
451 sval_t sval;
453 if (!expr)
454 return 0;
455 if (cur_func->type != SYM_NODE)
456 return 0;
457 cur_func = get_base_type(cur_func);
458 if (cur_func->type != SYM_FN)
459 return 0;
460 cur_func = get_base_type(cur_func);
461 if (cur_func == &void_ctype)
462 return 0;
463 if (!get_implied_value(expr, &sval))
464 return 0;
465 if (sval.value < 0)
466 return 1;
467 if (cur_func->type == SYM_PTR && sval.value == 0)
468 return 1;
469 return 0;
472 int getting_address(void)
474 struct expression *tmp;
475 int i = 0;
476 int dot_ops = 0;
478 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
479 if (!i++)
480 continue;
481 if (tmp->type == EXPR_PREOP && tmp->op == '(')
482 continue;
483 if (tmp->op == '.' && !dot_ops++)
484 continue;
485 if (tmp->op == '&')
486 return 1;
487 return 0;
488 } END_FOR_EACH_PTR_REVERSE(tmp);
489 return 0;
492 char *get_member_name(struct expression *expr)
494 char buf[256];
495 struct symbol *sym;
497 expr = strip_expr(expr);
498 if (expr->type != EXPR_DEREF)
499 return NULL;
500 sym = get_type(expr->deref);
501 if (!sym)
502 return NULL;
503 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
504 if (sym->type == SYM_UNION) {
505 struct symbol *tmp;
507 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
508 if (tmp->ident) {
509 const char *member_name;
511 if (expr->member)
512 member_name = expr->member->name;
513 else
514 member_name = "unknown_member";
515 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, member_name);
516 return alloc_string(buf);
518 } END_FOR_EACH_PTR_REVERSE(tmp);
519 return NULL;
521 if (!sym->ident || !expr->member)
522 return NULL;
523 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
524 return alloc_string(buf);
527 int cmp_pos(struct position pos1, struct position pos2)
529 /* the stream position is ... */
530 if (pos1.stream > pos2.stream)
531 return -1;
532 if (pos1.stream < pos2.stream)
533 return 1;
535 if (pos1.line < pos2.line)
536 return -1;
537 if (pos1.line > pos2.line)
538 return 1;
540 if (pos1.pos < pos2.pos)
541 return -1;
542 if (pos1.pos > pos2.pos)
543 return 1;
545 return 0;
548 int positions_eq(struct position pos1, struct position pos2)
550 if (pos1.line != pos2.line)
551 return 0;
552 if (pos1.pos != pos2.pos)
553 return 0;
554 if (pos1.stream != pos2.stream)
555 return 0;
556 return 1;
559 struct statement *get_current_statement(void)
561 struct statement *prev, *tmp;
563 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
565 if (!prev || !get_macro_name(prev->pos))
566 return prev;
568 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
569 if (positions_eq(tmp->pos, prev->pos))
570 continue;
571 if (prev->pos.line > tmp->pos.line)
572 return prev;
573 return tmp;
574 } END_FOR_EACH_PTR_REVERSE(tmp);
575 return prev;
578 struct statement *get_prev_statement(void)
580 struct statement *tmp;
581 int i;
583 i = 0;
584 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
585 if (i++ == 1)
586 return tmp;
587 } END_FOR_EACH_PTR_REVERSE(tmp);
588 return NULL;
591 int get_param_num_from_sym(struct symbol *sym)
593 struct symbol *tmp;
594 int i;
596 if (!cur_func_sym)
597 return -1;
599 i = 0;
600 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
601 if (tmp == sym)
602 return i;
603 i++;
604 } END_FOR_EACH_PTR(tmp);
605 return -1;
608 int get_param_num(struct expression *expr)
610 struct symbol *sym;
611 char *name;
613 if (!cur_func_sym)
614 return -1;
615 name = expr_to_var_sym(expr, &sym);
616 free_string(name);
617 if (!sym)
618 return -1;
619 return get_param_num_from_sym(sym);
622 int ms_since(struct timeval *start)
624 struct timeval end;
625 double diff;
627 gettimeofday(&end, NULL);
628 diff = (end.tv_sec - start->tv_sec) * 1000.0;
629 diff += (end.tv_usec - start->tv_usec) / 1000.0;
630 return (int)diff;