ranges: problems parsing "s32min-(-1)[<=p2]"
[smatch.git] / smatch_helper.c
blob41a4915d528c4919b03b2fb0c8c4497fe5b049e5
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 == '(') {
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 default:
245 *complicated = 1;
246 //printf("unknown type = %d\n", expr->type);
247 return;
252 * This is returns a stylized "c looking" representation of the
253 * variable name.
255 * It uses the same buffer every time so you have to save the result
256 * yourself if you want to keep it.
260 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
262 static char var_name[VAR_LEN];
263 int complicated = 0;
265 if (sym_ptr)
266 *sym_ptr = NULL;
267 var_name[0] = '\0';
269 if (!expr)
270 return NULL;
271 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
272 &complicated, 0);
273 if (complicated < 2)
274 return alloc_string(var_name);
275 else
276 return NULL;
279 char *expr_to_str(struct expression *expr)
281 return expr_to_str_sym(expr, NULL);
285 * get_variable_from_expr_simple() only returns simple variables.
286 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
287 * then it returns NULL.
289 char *expr_to_var_sym(struct expression *expr,
290 struct symbol **sym_ptr)
292 static char var_name[VAR_LEN];
293 int complicated = 0;
295 if (sym_ptr)
296 *sym_ptr = NULL;
297 var_name[0] = '\0';
299 if (!expr)
300 return NULL;
301 expr = strip_expr(expr);
302 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
303 &complicated, 1);
305 if (complicated) {
306 if (sym_ptr)
307 *sym_ptr = NULL;
308 return NULL;
310 return alloc_string(var_name);
313 char *expr_to_var(struct expression *expr)
315 return expr_to_var_sym(expr, NULL);
318 int sym_name_is(const char *name, struct expression *expr)
320 if (!expr)
321 return 0;
322 if (expr->type != EXPR_SYMBOL)
323 return 0;
324 if (!strcmp(expr->symbol_name->name, name))
325 return 1;
326 return 0;
329 int is_zero(struct expression *expr)
331 sval_t sval;
333 if (get_value(expr, &sval) && sval.value == 0)
334 return 1;
335 return 0;
338 int is_array(struct expression *expr)
340 expr = strip_expr(expr);
341 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
342 return 0;
343 expr = expr->unop;
344 if (expr->op != '+')
345 return 0;
346 return 1;
349 struct expression *get_array_name(struct expression *expr)
351 if (!is_array(expr))
352 return NULL;
353 return strip_expr(expr->unop->left);
356 struct expression *get_array_offset(struct expression *expr)
358 if (!is_array(expr))
359 return NULL;
360 return expr->unop->right;
363 const char *show_state(struct smatch_state *state)
365 if (!state)
366 return NULL;
367 return state->name;
370 struct statement *get_expression_statement(struct expression *expr)
372 /* What are those things called? if (({....; ret;})) { ...*/
374 if (expr->type != EXPR_PREOP)
375 return NULL;
376 if (expr->op != '(')
377 return NULL;
378 if (expr->unop->type != EXPR_STATEMENT)
379 return NULL;
380 if (expr->unop->statement->type != STMT_COMPOUND)
381 return NULL;
382 return expr->unop->statement;
385 struct expression *strip_parens(struct expression *expr)
387 if (!expr)
388 return NULL;
390 if (expr->type == EXPR_PREOP) {
391 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
392 expr->unop->statement->type == STMT_COMPOUND)
393 return expr;
394 if (expr->op == '(')
395 return strip_parens(expr->unop);
397 return expr;
400 struct expression *strip_expr(struct expression *expr)
402 if (!expr)
403 return NULL;
405 switch (expr->type) {
406 case EXPR_FORCE_CAST:
407 case EXPR_CAST:
408 return strip_expr(expr->cast_expression);
409 case EXPR_PREOP:
410 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
411 expr->unop->statement->type == STMT_COMPOUND)
412 return expr;
413 if (expr->op == '(')
414 return strip_expr(expr->unop);
416 return expr;
419 static void delete_state_tracker(struct tracker *t)
421 delete_state(t->owner, t->name, t->sym);
422 __free_tracker(t);
425 void scoped_state(int my_id, const char *name, struct symbol *sym)
427 struct tracker *t;
429 t = alloc_tracker(my_id, name, sym);
430 add_scope_hook((scope_hook *)&delete_state_tracker, t);
433 int is_error_return(struct expression *expr)
435 struct symbol *cur_func = cur_func_sym;
436 sval_t sval;
438 if (!expr)
439 return 0;
440 if (cur_func->type != SYM_NODE)
441 return 0;
442 cur_func = get_base_type(cur_func);
443 if (cur_func->type != SYM_FN)
444 return 0;
445 cur_func = get_base_type(cur_func);
446 if (cur_func == &void_ctype)
447 return 0;
448 if (!get_value(expr, &sval))
449 return 0;
450 if (sval_cmp_val(sval, 0) < 0)
451 return 1;
452 if (cur_func->type == SYM_PTR && sval.value == 0)
453 return 1;
454 return 0;
457 int getting_address(void)
459 struct expression *tmp;
460 int i = 0;
461 int dot_ops = 0;
463 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
464 if (!i++)
465 continue;
466 if (tmp->type == EXPR_PREOP && tmp->op == '(')
467 continue;
468 if (tmp->op == '.' && !dot_ops++)
469 continue;
470 if (tmp->op == '&')
471 return 1;
472 return 0;
473 } END_FOR_EACH_PTR_REVERSE(tmp);
474 return 0;
477 char *get_member_name(struct expression *expr)
479 char buf[256];
480 struct symbol *sym;
482 expr = strip_expr(expr);
483 if (expr->type != EXPR_DEREF)
484 return NULL;
485 sym = get_type(expr->deref);
486 if (!sym || !sym->ident)
487 return NULL;
488 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
489 return alloc_string(buf);
492 int positions_eq(struct position pos1, struct position pos2)
494 if (pos1.line != pos2.line)
495 return 0;
496 if (pos1.pos != pos2.pos)
497 return 0;
498 if (pos1.stream != pos2.stream)
499 return 0;
500 return 1;
503 struct statement *get_current_statement(void)
505 struct statement *prev, *tmp;
507 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
509 if (!prev || !get_macro_name(prev->pos))
510 return prev;
512 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
513 if (positions_eq(tmp->pos, prev->pos))
514 continue;
515 if (prev->pos.line > tmp->pos.line)
516 return prev;
517 return tmp;
518 } END_FOR_EACH_PTR_REVERSE(tmp);
519 return prev;
522 int get_param_num_from_sym(struct symbol *sym)
524 struct symbol *tmp;
525 int i;
527 if (!cur_func_sym)
528 return -1;
530 i = 0;
531 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
532 if (tmp == sym)
533 return i;
534 i++;
535 } END_FOR_EACH_PTR(tmp);
536 return -1;
539 int ms_since(struct timeval *start)
541 struct timeval end;
542 double diff;
544 gettimeofday(&end, NULL);
545 diff = (end.tv_sec - start->tv_sec) * 1000.0;
546 diff += (end.tv_usec - start->tv_usec) / 1000.0;
547 return (int)diff;