helper: taking the address is not a complicated variable
[smatch.git] / smatch_helper.c
blob7e9871d02ef1014cc2042fe81a3ac18ae51db49f
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 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 if (expr->member && expr->member->name)
126 append(buf, expr->member->name, len);
127 else
128 append(buf, "unknown_member", len);
130 return;
131 case EXPR_SYMBOL:
132 if (expr->symbol_name)
133 append(buf, expr->symbol_name->name, len);
134 if (sym_ptr) {
135 if (*sym_ptr)
136 *complicated = 1;
137 *sym_ptr = expr->symbol;
139 return;
140 case EXPR_PREOP: {
141 const char *tmp;
143 if (get_expression_statement(expr)) {
144 *complicated = 2;
145 return;
148 if (expr->op == '(') {
149 if (!no_parens)
150 append(buf, "(", len);
151 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
152 tmp = show_special(expr->op);
153 append(buf, tmp, len);
155 __get_variable_from_expr(sym_ptr, buf, expr->unop,
156 len, complicated, no_parens);
158 if (expr->op == '(' && !no_parens)
159 append(buf, ")", len);
161 if (expr->op == SPECIAL_DECREMENT ||
162 expr->op == SPECIAL_INCREMENT)
163 *complicated = 1;
165 return;
167 case EXPR_POSTOP: {
168 const char *tmp;
170 __get_variable_from_expr(sym_ptr, buf, expr->unop,
171 len, complicated, no_parens);
172 tmp = show_special(expr->op);
173 append(buf, tmp, len);
175 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
176 *complicated = 1;
177 return;
179 case EXPR_ASSIGNMENT:
180 case EXPR_COMPARE:
181 case EXPR_LOGICAL:
182 case EXPR_BINOP: {
183 char tmp[10];
184 struct expression *array_expr;
186 *complicated = 1;
187 array_expr = get_array_expr(expr);
188 if (array_expr) {
189 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
190 append(buf, "[", len);
191 } else {
192 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
193 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
194 append(buf, tmp, len);
196 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
197 if (array_expr)
198 append(buf, "]", len);
199 return;
201 case EXPR_VALUE: {
202 char tmp[25];
204 *complicated = 1;
205 snprintf(tmp, 25, "%lld", expr->value);
206 append(buf, tmp, len);
207 return;
209 case EXPR_STRING:
210 append(buf, "\"", len);
211 append(buf, expr->string->data, len);
212 append(buf, "\"", len);
213 return;
214 case EXPR_CALL: {
215 struct expression *tmp;
216 int i;
218 *complicated = 1;
219 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
220 append(buf, "(", len);
221 i = 0;
222 FOR_EACH_PTR(expr->args, tmp) {
223 if (i++)
224 append(buf, ", ", len);
225 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
226 } END_FOR_EACH_PTR(tmp);
227 append(buf, ")", len);
228 return;
230 case EXPR_CAST:
231 __get_variable_from_expr(sym_ptr, buf,
232 expr->cast_expression, len,
233 complicated, no_parens);
234 return;
235 case EXPR_SIZEOF: {
236 int size;
237 char tmp[25];
239 if (expr->cast_type && get_base_type(expr->cast_type)) {
240 size = (get_base_type(expr->cast_type))->bit_size;
241 snprintf(tmp, 25, "%d", bits_to_bytes(size));
242 append(buf, tmp, len);
244 return;
246 case EXPR_IDENTIFIER:
247 *complicated = 1;
248 if (expr->expr_ident && expr->expr_ident->name)
249 append(buf, expr->expr_ident->name, len);
250 return;
251 default:
252 *complicated = 1;
253 //printf("unknown type = %d\n", expr->type);
254 return;
259 * This is returns a stylized "c looking" representation of the
260 * variable name.
262 * It uses the same buffer every time so you have to save the result
263 * yourself if you want to keep it.
267 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
269 static char var_name[VAR_LEN];
270 int complicated = 0;
272 if (sym_ptr)
273 *sym_ptr = NULL;
274 var_name[0] = '\0';
276 if (!expr)
277 return NULL;
278 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
279 &complicated, 0);
280 if (complicated < 2)
281 return alloc_string(var_name);
282 else
283 return NULL;
286 char *expr_to_str(struct expression *expr)
288 return expr_to_str_sym(expr, NULL);
292 * get_variable_from_expr_simple() only returns simple variables.
293 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
294 * then it returns NULL.
296 char *expr_to_var_sym(struct expression *expr,
297 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 expr = strip_expr(expr);
309 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
310 &complicated, 1);
312 if (complicated) {
313 if (sym_ptr)
314 *sym_ptr = NULL;
315 return NULL;
317 return alloc_string(var_name);
320 char *expr_to_var(struct expression *expr)
322 return expr_to_var_sym(expr, NULL);
325 int sym_name_is(const char *name, struct expression *expr)
327 if (!expr)
328 return 0;
329 if (expr->type != EXPR_SYMBOL)
330 return 0;
331 if (!strcmp(expr->symbol_name->name, name))
332 return 1;
333 return 0;
336 int is_zero(struct expression *expr)
338 sval_t sval;
340 if (get_value(expr, &sval) && sval.value == 0)
341 return 1;
342 return 0;
345 int is_array(struct expression *expr)
347 expr = strip_expr(expr);
348 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
349 return 0;
350 expr = expr->unop;
351 if (expr->op != '+')
352 return 0;
353 return 1;
356 struct expression *get_array_name(struct expression *expr)
358 if (!is_array(expr))
359 return NULL;
360 return strip_expr(expr->unop->left);
363 struct expression *get_array_offset(struct expression *expr)
365 if (!is_array(expr))
366 return NULL;
367 return expr->unop->right;
370 const char *show_state(struct smatch_state *state)
372 if (!state)
373 return NULL;
374 return state->name;
377 struct statement *get_expression_statement(struct expression *expr)
379 /* What are those things called? if (({....; ret;})) { ...*/
381 if (expr->type != EXPR_PREOP)
382 return NULL;
383 if (expr->op != '(')
384 return NULL;
385 if (expr->unop->type != EXPR_STATEMENT)
386 return NULL;
387 if (expr->unop->statement->type != STMT_COMPOUND)
388 return NULL;
389 return expr->unop->statement;
392 struct expression *strip_parens(struct expression *expr)
394 if (!expr)
395 return NULL;
397 if (expr->type == EXPR_PREOP) {
398 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
399 expr->unop->statement->type == STMT_COMPOUND)
400 return expr;
401 if (expr->op == '(')
402 return strip_parens(expr->unop);
404 return expr;
407 struct expression *strip_expr(struct expression *expr)
409 if (!expr)
410 return NULL;
412 switch (expr->type) {
413 case EXPR_FORCE_CAST:
414 case EXPR_CAST:
415 return strip_expr(expr->cast_expression);
416 case EXPR_PREOP:
417 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
418 expr->unop->statement->type == STMT_COMPOUND)
419 return expr;
420 if (expr->op == '(')
421 return strip_expr(expr->unop);
423 return expr;
426 static void delete_state_tracker(struct tracker *t)
428 delete_state(t->owner, t->name, t->sym);
429 __free_tracker(t);
432 void scoped_state(int my_id, const char *name, struct symbol *sym)
434 struct tracker *t;
436 t = alloc_tracker(my_id, name, sym);
437 add_scope_hook((scope_hook *)&delete_state_tracker, t);
440 int is_error_return(struct expression *expr)
442 struct symbol *cur_func = cur_func_sym;
443 sval_t sval;
445 if (!expr)
446 return 0;
447 if (cur_func->type != SYM_NODE)
448 return 0;
449 cur_func = get_base_type(cur_func);
450 if (cur_func->type != SYM_FN)
451 return 0;
452 cur_func = get_base_type(cur_func);
453 if (cur_func == &void_ctype)
454 return 0;
455 if (!get_implied_value(expr, &sval))
456 return 0;
457 if (sval.value < 0)
458 return 1;
459 if (cur_func->type == SYM_PTR && sval.value == 0)
460 return 1;
461 return 0;
464 int getting_address(void)
466 struct expression *tmp;
467 int i = 0;
468 int dot_ops = 0;
470 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
471 if (!i++)
472 continue;
473 if (tmp->type == EXPR_PREOP && tmp->op == '(')
474 continue;
475 if (tmp->op == '.' && !dot_ops++)
476 continue;
477 if (tmp->op == '&')
478 return 1;
479 return 0;
480 } END_FOR_EACH_PTR_REVERSE(tmp);
481 return 0;
484 char *get_member_name(struct expression *expr)
486 char buf[256];
487 struct symbol *sym;
489 expr = strip_expr(expr);
490 if (expr->type != EXPR_DEREF)
491 return NULL;
492 sym = get_type(expr->deref);
493 if (!sym)
494 return NULL;
495 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
496 if (sym->type == SYM_UNION) {
497 struct symbol *tmp;
499 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
500 if (tmp->ident) {
501 const char *member_name;
503 if (expr->member)
504 member_name = expr->member->name;
505 else
506 member_name = "unknown_member";
507 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, member_name);
508 return alloc_string(buf);
510 } END_FOR_EACH_PTR_REVERSE(tmp);
511 return NULL;
513 if (!sym->ident || !expr->member)
514 return NULL;
515 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
516 return alloc_string(buf);
519 int positions_eq(struct position pos1, struct position pos2)
521 if (pos1.line != pos2.line)
522 return 0;
523 if (pos1.pos != pos2.pos)
524 return 0;
525 if (pos1.stream != pos2.stream)
526 return 0;
527 return 1;
530 struct statement *get_current_statement(void)
532 struct statement *prev, *tmp;
534 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
536 if (!prev || !get_macro_name(prev->pos))
537 return prev;
539 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
540 if (positions_eq(tmp->pos, prev->pos))
541 continue;
542 if (prev->pos.line > tmp->pos.line)
543 return prev;
544 return tmp;
545 } END_FOR_EACH_PTR_REVERSE(tmp);
546 return prev;
549 int get_param_num_from_sym(struct symbol *sym)
551 struct symbol *tmp;
552 int i;
554 if (!cur_func_sym)
555 return -1;
557 i = 0;
558 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
559 if (tmp == sym)
560 return i;
561 i++;
562 } END_FOR_EACH_PTR(tmp);
563 return -1;
566 int get_param_num(struct expression *expr)
568 struct symbol *sym;
569 char *name;
571 if (!cur_func_sym)
572 return -1;
573 name = expr_to_var_sym(expr, &sym);
574 free_string(name);
575 if (!sym)
576 return -1;
577 return get_param_num_from_sym(sym);
580 int ms_since(struct timeval *start)
582 struct timeval end;
583 double diff;
585 gettimeofday(&end, NULL);
586 diff = (end.tv_sec - start->tv_sec) * 1000.0;
587 diff += (end.tv_usec - start->tv_usec) / 1000.0;
588 return (int)diff;