math: handle LEFT_SHIFT (a << b)
[smatch.git] / smatch_helper.c
blob7f832f4f1ef42eec33735df67be020b0360d4766
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 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 == '(') {
146 if (!no_parens)
147 append(buf, "(", len);
148 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
149 tmp = show_special(expr->op);
150 append(buf, tmp, len);
152 __get_variable_from_expr(sym_ptr, buf, expr->unop,
153 len, complicated, no_parens);
155 if (expr->op == '(' && !no_parens)
156 append(buf, ")", len);
158 if (expr->op == SPECIAL_DECREMENT ||
159 expr->op == SPECIAL_INCREMENT ||
160 expr->op == '&')
161 *complicated = 1;
163 return;
165 case EXPR_POSTOP: {
166 const char *tmp;
168 __get_variable_from_expr(sym_ptr, buf, expr->unop,
169 len, complicated, no_parens);
170 tmp = show_special(expr->op);
171 append(buf, tmp, len);
173 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
174 *complicated = 1;
175 return;
177 case EXPR_ASSIGNMENT:
178 case EXPR_COMPARE:
179 case EXPR_LOGICAL:
180 case EXPR_BINOP: {
181 char tmp[10];
182 struct expression *array_expr;
184 *complicated = 1;
185 array_expr = get_array_expr(expr);
186 if (array_expr) {
187 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
188 append(buf, "[", len);
189 } else {
190 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
191 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
192 append(buf, tmp, len);
194 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
195 if (array_expr)
196 append(buf, "]", len);
197 return;
199 case EXPR_VALUE: {
200 char tmp[25];
202 *complicated = 1;
203 snprintf(tmp, 25, "%lld", expr->value);
204 append(buf, tmp, len);
205 return;
207 case EXPR_STRING:
208 append(buf, "\"", len);
209 append(buf, expr->string->data, len);
210 append(buf, "\"", len);
211 return;
212 case EXPR_CALL: {
213 struct expression *tmp;
214 int i;
216 *complicated = 1;
217 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
218 append(buf, "(", len);
219 i = 0;
220 FOR_EACH_PTR(expr->args, tmp) {
221 if (i++)
222 append(buf, ", ", len);
223 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
224 } END_FOR_EACH_PTR(tmp);
225 append(buf, ")", len);
226 return;
228 case EXPR_CAST:
229 __get_variable_from_expr(sym_ptr, buf,
230 expr->cast_expression, len,
231 complicated, no_parens);
232 return;
233 case EXPR_SIZEOF: {
234 int size;
235 char tmp[25];
237 if (expr->cast_type && get_base_type(expr->cast_type)) {
238 size = (get_base_type(expr->cast_type))->bit_size;
239 snprintf(tmp, 25, "%d", bits_to_bytes(size));
240 append(buf, tmp, len);
242 return;
244 case EXPR_IDENTIFIER:
245 *complicated = 1;
246 if (expr->expr_ident && expr->expr_ident->name)
247 append(buf, expr->expr_ident->name, len);
248 return;
249 default:
250 *complicated = 1;
251 //printf("unknown type = %d\n", expr->type);
252 return;
257 * This is returns a stylized "c looking" representation of the
258 * variable name.
260 * It uses the same buffer every time so you have to save the result
261 * yourself if you want to keep it.
265 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
267 static char var_name[VAR_LEN];
268 int complicated = 0;
270 if (sym_ptr)
271 *sym_ptr = NULL;
272 var_name[0] = '\0';
274 if (!expr)
275 return NULL;
276 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
277 &complicated, 0);
278 if (complicated < 2)
279 return alloc_string(var_name);
280 else
281 return NULL;
284 char *expr_to_str(struct expression *expr)
286 return expr_to_str_sym(expr, NULL);
290 * get_variable_from_expr_simple() only returns simple variables.
291 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
292 * then it returns NULL.
294 char *expr_to_var_sym(struct expression *expr,
295 struct symbol **sym_ptr)
297 static char var_name[VAR_LEN];
298 int complicated = 0;
300 if (sym_ptr)
301 *sym_ptr = NULL;
302 var_name[0] = '\0';
304 if (!expr)
305 return NULL;
306 expr = strip_expr(expr);
307 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
308 &complicated, 1);
310 if (complicated) {
311 if (sym_ptr)
312 *sym_ptr = NULL;
313 return NULL;
315 return alloc_string(var_name);
318 char *expr_to_var(struct expression *expr)
320 return expr_to_var_sym(expr, NULL);
323 int sym_name_is(const char *name, struct expression *expr)
325 if (!expr)
326 return 0;
327 if (expr->type != EXPR_SYMBOL)
328 return 0;
329 if (!strcmp(expr->symbol_name->name, name))
330 return 1;
331 return 0;
334 int is_zero(struct expression *expr)
336 sval_t sval;
338 if (get_value(expr, &sval) && sval.value == 0)
339 return 1;
340 return 0;
343 int is_array(struct expression *expr)
345 expr = strip_expr(expr);
346 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
347 return 0;
348 expr = expr->unop;
349 if (expr->op != '+')
350 return 0;
351 return 1;
354 struct expression *get_array_name(struct expression *expr)
356 if (!is_array(expr))
357 return NULL;
358 return strip_expr(expr->unop->left);
361 struct expression *get_array_offset(struct expression *expr)
363 if (!is_array(expr))
364 return NULL;
365 return expr->unop->right;
368 const char *show_state(struct smatch_state *state)
370 if (!state)
371 return NULL;
372 return state->name;
375 struct statement *get_expression_statement(struct expression *expr)
377 /* What are those things called? if (({....; ret;})) { ...*/
379 if (expr->type != EXPR_PREOP)
380 return NULL;
381 if (expr->op != '(')
382 return NULL;
383 if (expr->unop->type != EXPR_STATEMENT)
384 return NULL;
385 if (expr->unop->statement->type != STMT_COMPOUND)
386 return NULL;
387 return expr->unop->statement;
390 struct expression *strip_parens(struct expression *expr)
392 if (!expr)
393 return NULL;
395 if (expr->type == EXPR_PREOP) {
396 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
397 expr->unop->statement->type == STMT_COMPOUND)
398 return expr;
399 if (expr->op == '(')
400 return strip_parens(expr->unop);
402 return expr;
405 struct expression *strip_expr(struct expression *expr)
407 if (!expr)
408 return NULL;
410 switch (expr->type) {
411 case EXPR_FORCE_CAST:
412 case EXPR_CAST:
413 return strip_expr(expr->cast_expression);
414 case EXPR_PREOP:
415 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
416 expr->unop->statement->type == STMT_COMPOUND)
417 return expr;
418 if (expr->op == '(')
419 return strip_expr(expr->unop);
421 return expr;
424 static void delete_state_tracker(struct tracker *t)
426 delete_state(t->owner, t->name, t->sym);
427 __free_tracker(t);
430 void scoped_state(int my_id, const char *name, struct symbol *sym)
432 struct tracker *t;
434 t = alloc_tracker(my_id, name, sym);
435 add_scope_hook((scope_hook *)&delete_state_tracker, t);
438 int is_error_return(struct expression *expr)
440 struct symbol *cur_func = cur_func_sym;
441 sval_t sval;
443 if (!expr)
444 return 0;
445 if (cur_func->type != SYM_NODE)
446 return 0;
447 cur_func = get_base_type(cur_func);
448 if (cur_func->type != SYM_FN)
449 return 0;
450 cur_func = get_base_type(cur_func);
451 if (cur_func == &void_ctype)
452 return 0;
453 if (!get_value(expr, &sval))
454 return 0;
455 if (sval_cmp_val(sval, 0) < 0)
456 return 1;
457 if (cur_func->type == SYM_PTR && sval.value == 0)
458 return 1;
459 return 0;
462 int getting_address(void)
464 struct expression *tmp;
465 int i = 0;
466 int dot_ops = 0;
468 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
469 if (!i++)
470 continue;
471 if (tmp->type == EXPR_PREOP && tmp->op == '(')
472 continue;
473 if (tmp->op == '.' && !dot_ops++)
474 continue;
475 if (tmp->op == '&')
476 return 1;
477 return 0;
478 } END_FOR_EACH_PTR_REVERSE(tmp);
479 return 0;
482 char *get_member_name(struct expression *expr)
484 char buf[256];
485 struct symbol *sym;
487 expr = strip_expr(expr);
488 if (expr->type != EXPR_DEREF)
489 return NULL;
490 sym = get_type(expr->deref);
491 if (!sym)
492 return NULL;
493 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
494 if (sym->type == SYM_UNION) {
495 struct symbol *tmp;
497 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
498 if (tmp->ident) {
499 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, expr->member->name);
500 return alloc_string(buf);
502 } END_FOR_EACH_PTR_REVERSE(tmp);
503 return NULL;
505 if (!sym->ident)
506 return NULL;
507 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
508 return alloc_string(buf);
511 int positions_eq(struct position pos1, struct position pos2)
513 if (pos1.line != pos2.line)
514 return 0;
515 if (pos1.pos != pos2.pos)
516 return 0;
517 if (pos1.stream != pos2.stream)
518 return 0;
519 return 1;
522 struct statement *get_current_statement(void)
524 struct statement *prev, *tmp;
526 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
528 if (!prev || !get_macro_name(prev->pos))
529 return prev;
531 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
532 if (positions_eq(tmp->pos, prev->pos))
533 continue;
534 if (prev->pos.line > tmp->pos.line)
535 return prev;
536 return tmp;
537 } END_FOR_EACH_PTR_REVERSE(tmp);
538 return prev;
541 int get_param_num_from_sym(struct symbol *sym)
543 struct symbol *tmp;
544 int i;
546 if (!cur_func_sym)
547 return -1;
549 i = 0;
550 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
551 if (tmp == sym)
552 return i;
553 i++;
554 } END_FOR_EACH_PTR(tmp);
555 return -1;
558 int ms_since(struct timeval *start)
560 struct timeval end;
561 double diff;
563 gettimeofday(&end, NULL);
564 diff = (end.tv_sec - start->tv_sec) * 1000.0;
565 diff += (end.tv_usec - start->tv_usec) / 1000.0;
566 return (int)diff;