helper: speed up expr_to_var_sym()
[smatch.git] / smatch_helper.c
blob0ec43d555a84906da8381dd950a3b4026defe18a
1 /*
2 * sparse/smatch_helper.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * Miscellaneous helper functions.
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include "allocate.h"
17 #include "smatch.h"
19 #define VAR_LEN 512
21 char *alloc_string(const char *str)
23 char *tmp;
25 if (!str)
26 return NULL;
27 tmp = malloc(strlen(str) + 1);
28 strcpy(tmp, str);
29 return tmp;
32 void free_string(char *str)
34 free(str);
37 void remove_parens(char *str)
39 char *src, *dst;
41 dst = src = str;
42 while (*src != '\0') {
43 if (*src == '(' || *src == ')') {
44 src++;
45 continue;
47 *dst++ = *src++;
49 *dst = *src;
52 struct smatch_state *alloc_state_num(int num)
54 struct smatch_state *state;
55 static char buff[256];
57 state = __alloc_smatch_state(0);
58 snprintf(buff, 255, "%d", num);
59 buff[255] = '\0';
60 state->name = alloc_string(buff);
61 state->data = INT_PTR(num);
62 return state;
65 static void append(char *dest, const char *data, int buff_len)
67 strncat(dest, data, buff_len - strlen(dest) - 1);
71 * If you have "foo(a, b, 1);" then use
72 * get_argument_from_call_expr(expr, 0) to return the expression for
73 * a. Yes, it does start counting from 0.
75 struct expression *get_argument_from_call_expr(struct expression_list *args,
76 int num)
78 struct expression *expr;
79 int i = 0;
81 if (!args)
82 return NULL;
84 FOR_EACH_PTR(args, expr) {
85 if (i == num)
86 return expr;
87 i++;
88 } END_FOR_EACH_PTR(expr);
89 return NULL;
92 static struct expression *get_array_expr(struct expression *expr)
94 struct symbol *type;
96 if (expr->type != EXPR_BINOP || expr->op != '+')
97 return NULL;
99 type = get_type(expr->left);
100 if (!type || type->type != SYM_ARRAY)
101 return NULL;
102 return expr->left;
105 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
106 struct expression *expr, int len,
107 int *complicated, int no_parens)
109 struct expression *tmp;
111 switch (expr->type) {
112 case EXPR_DEREF:
113 tmp = expr->deref;
114 if (tmp->op == '*')
115 tmp = tmp->unop;
117 __get_variable_from_expr(sym_ptr, buf, tmp, len, complicated, no_parens);
119 tmp = expr->deref;
120 if (tmp->op == '*')
121 append(buf, "->", len);
122 else
123 append(buf, ".", len);
125 append(buf, expr->member->name, len);
127 return;
128 case EXPR_SYMBOL:
129 if (expr->symbol_name)
130 append(buf, expr->symbol_name->name, len);
131 if (sym_ptr) {
132 if (*sym_ptr)
133 *complicated = 1;
134 *sym_ptr = expr->symbol;
136 return;
137 case EXPR_PREOP: {
138 const char *tmp;
140 if (get_expression_statement(expr)) {
141 *complicated = 2;
142 return;
145 if (expr->op == '(' && !no_parens) {
146 append(buf, "(", len);
147 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
148 tmp = show_special(expr->op);
149 append(buf, tmp, len);
151 __get_variable_from_expr(sym_ptr, buf, expr->unop,
152 len, complicated, no_parens);
154 if (expr->op == '(' && !no_parens)
155 append(buf, ")", len);
157 if (expr->op == SPECIAL_DECREMENT ||
158 expr->op == SPECIAL_INCREMENT ||
159 expr->op == '&')
160 *complicated = 1;
162 return;
164 case EXPR_POSTOP: {
165 const char *tmp;
167 __get_variable_from_expr(sym_ptr, buf, expr->unop,
168 len, complicated, no_parens);
169 tmp = show_special(expr->op);
170 append(buf, tmp, len);
172 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
173 *complicated = 1;
174 return;
176 case EXPR_ASSIGNMENT:
177 case EXPR_COMPARE:
178 case EXPR_LOGICAL:
179 case EXPR_BINOP: {
180 char tmp[10];
181 struct expression *array_expr;
183 *complicated = 1;
184 array_expr = get_array_expr(expr);
185 if (array_expr) {
186 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
187 append(buf, "[", len);
188 } else {
189 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
190 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
191 append(buf, tmp, len);
193 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
194 if (array_expr)
195 append(buf, "]", len);
196 return;
198 case EXPR_VALUE: {
199 char tmp[25];
201 *complicated = 1;
202 snprintf(tmp, 25, "%lld", expr->value);
203 append(buf, tmp, len);
204 return;
206 case EXPR_STRING:
207 append(buf, "\"", len);
208 append(buf, expr->string->data, len);
209 append(buf, "\"", len);
210 return;
211 case EXPR_CALL: {
212 struct expression *tmp;
213 int i;
215 *complicated = 1;
216 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
217 append(buf, "(", len);
218 i = 0;
219 FOR_EACH_PTR(expr->args, tmp) {
220 if (i++)
221 append(buf, ", ", len);
222 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
223 } END_FOR_EACH_PTR(tmp);
224 append(buf, ")", len);
225 return;
227 case EXPR_CAST:
228 __get_variable_from_expr(sym_ptr, buf,
229 expr->cast_expression, len,
230 complicated, no_parens);
231 return;
232 case EXPR_SIZEOF: {
233 int size;
234 char tmp[25];
236 if (expr->cast_type && get_base_type(expr->cast_type)) {
237 size = (get_base_type(expr->cast_type))->bit_size;
238 snprintf(tmp, 25, "%d", bits_to_bytes(size));
239 append(buf, tmp, len);
241 return;
243 default:
244 *complicated = 1;
245 //printf("unknown type = %d\n", expr->type);
246 return;
251 * This is returns a stylized "c looking" representation of the
252 * variable name.
254 * It uses the same buffer every time so you have to save the result
255 * yourself if you want to keep it.
259 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
261 static char var_name[VAR_LEN];
262 int complicated = 0;
264 if (sym_ptr)
265 *sym_ptr = NULL;
266 var_name[0] = '\0';
268 if (!expr)
269 return NULL;
270 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
271 &complicated, 0);
272 if (complicated < 2)
273 return alloc_string(var_name);
274 else
275 return NULL;
278 char *expr_to_str(struct expression *expr)
280 return expr_to_str_sym(expr, NULL);
284 * get_variable_from_expr_simple() only returns simple variables.
285 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
286 * then it returns NULL.
288 char *expr_to_var_sym(struct expression *expr,
289 struct symbol **sym_ptr)
291 static char var_name[VAR_LEN];
292 int complicated = 0;
294 if (sym_ptr)
295 *sym_ptr = NULL;
296 var_name[0] = '\0';
298 if (!expr)
299 return NULL;
300 expr = strip_expr(expr);
301 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
302 &complicated, 1);
304 if (complicated) {
305 if (sym_ptr)
306 *sym_ptr = NULL;
307 return NULL;
309 return alloc_string(var_name);
312 char *expr_to_var(struct expression *expr)
314 return expr_to_var_sym(expr, NULL);
317 int sym_name_is(const char *name, struct expression *expr)
319 if (!expr)
320 return 0;
321 if (expr->type != EXPR_SYMBOL)
322 return 0;
323 if (!strcmp(expr->symbol_name->name, name))
324 return 1;
325 return 0;
328 int is_zero(struct expression *expr)
330 sval_t sval;
332 if (get_value(expr, &sval) && sval.value == 0)
333 return 1;
334 return 0;
337 int is_array(struct expression *expr)
339 expr = strip_expr(expr);
340 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
341 return 0;
342 expr = expr->unop;
343 if (expr->op != '+')
344 return 0;
345 return 1;
348 struct expression *get_array_name(struct expression *expr)
350 if (!is_array(expr))
351 return NULL;
352 return strip_expr(expr->unop->left);
355 struct expression *get_array_offset(struct expression *expr)
357 if (!is_array(expr))
358 return NULL;
359 return expr->unop->right;
362 const char *show_state(struct smatch_state *state)
364 if (!state)
365 return NULL;
366 return state->name;
369 struct statement *get_expression_statement(struct expression *expr)
371 /* What are those things called? if (({....; ret;})) { ...*/
373 if (expr->type != EXPR_PREOP)
374 return NULL;
375 if (expr->op != '(')
376 return NULL;
377 if (expr->unop->type != EXPR_STATEMENT)
378 return NULL;
379 if (expr->unop->statement->type != STMT_COMPOUND)
380 return NULL;
381 return expr->unop->statement;
384 struct expression *strip_parens(struct expression *expr)
386 if (!expr)
387 return NULL;
389 if (expr->type == EXPR_PREOP) {
390 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
391 expr->unop->statement->type == STMT_COMPOUND)
392 return expr;
393 if (expr->op == '(')
394 return strip_parens(expr->unop);
396 return expr;
399 struct expression *strip_expr(struct expression *expr)
401 if (!expr)
402 return NULL;
404 switch (expr->type) {
405 case EXPR_FORCE_CAST:
406 case EXPR_CAST:
407 return strip_expr(expr->cast_expression);
408 case EXPR_PREOP:
409 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
410 expr->unop->statement->type == STMT_COMPOUND)
411 return expr;
412 if (expr->op == '(')
413 return strip_expr(expr->unop);
415 return expr;
418 static void delete_state_tracker(struct tracker *t)
420 delete_state(t->owner, t->name, t->sym);
421 __free_tracker(t);
424 void scoped_state(int my_id, const char *name, struct symbol *sym)
426 struct tracker *t;
428 t = alloc_tracker(my_id, name, sym);
429 add_scope_hook((scope_hook *)&delete_state_tracker, t);
432 int is_error_return(struct expression *expr)
434 struct symbol *cur_func = cur_func_sym;
435 sval_t sval;
437 if (!expr)
438 return 0;
439 if (cur_func->type != SYM_NODE)
440 return 0;
441 cur_func = get_base_type(cur_func);
442 if (cur_func->type != SYM_FN)
443 return 0;
444 cur_func = get_base_type(cur_func);
445 if (cur_func == &void_ctype)
446 return 0;
447 if (!get_value(expr, &sval))
448 return 0;
449 if (sval_cmp_val(sval, 0) < 0)
450 return 1;
451 if (cur_func->type == SYM_PTR && sval.value == 0)
452 return 1;
453 return 0;
456 int getting_address(void)
458 struct expression *tmp;
459 int i = 0;
460 int dot_ops = 0;
462 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
463 if (!i++)
464 continue;
465 if (tmp->type == EXPR_PREOP && tmp->op == '(')
466 continue;
467 if (tmp->op == '.' && !dot_ops++)
468 continue;
469 if (tmp->op == '&')
470 return 1;
471 return 0;
472 } END_FOR_EACH_PTR_REVERSE(tmp);
473 return 0;
476 char *get_member_name(struct expression *expr)
478 char buf[256];
479 struct symbol *sym;
481 expr = strip_expr(expr);
482 if (expr->type != EXPR_DEREF)
483 return NULL;
484 sym = get_type(expr->deref);
485 if (!sym || !sym->ident)
486 return NULL;
487 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
488 return alloc_string(buf);
491 int positions_eq(struct position pos1, struct position pos2)
493 if (pos1.line != pos2.line)
494 return 0;
495 if (pos1.pos != pos2.pos)
496 return 0;
497 if (pos1.stream != pos2.stream)
498 return 0;
499 return 1;
502 struct statement *get_current_statement(void)
504 struct statement *prev, *tmp;
506 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
508 if (!prev || !get_macro_name(prev->pos))
509 return prev;
511 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
512 if (positions_eq(tmp->pos, prev->pos))
513 continue;
514 if (prev->pos.line > tmp->pos.line)
515 return prev;
516 return tmp;
517 } END_FOR_EACH_PTR_REVERSE(tmp);
518 return prev;
521 int get_param_num_from_sym(struct symbol *sym)
523 struct symbol *tmp;
524 int i;
526 if (!cur_func_sym)
527 return -1;
529 i = 0;
530 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
531 if (tmp == sym)
532 return i;
533 i++;
534 } END_FOR_EACH_PTR(tmp);
535 return -1;