extra: Fix segmentation fault in match_assign
[smatch.git] / smatch_helper.c
blob4375316086bcd8d989dabda1a7a89eb77ed4cbb7
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)
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);
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 != '*' || !get_array_expr(expr->unop)) {
146 tmp = show_special(expr->op);
147 append(buf, tmp, len);
149 __get_variable_from_expr(sym_ptr, buf, expr->unop,
150 len, complicated);
152 if (expr->op == '(')
153 append(buf, ")", len);
155 if (expr->op == SPECIAL_DECREMENT ||
156 expr->op == SPECIAL_INCREMENT ||
157 expr->op == '&')
158 *complicated = 1;
160 return;
162 case EXPR_POSTOP: {
163 const char *tmp;
165 __get_variable_from_expr(sym_ptr, buf, expr->unop,
166 len, complicated);
167 tmp = show_special(expr->op);
168 append(buf, tmp, len);
170 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
171 *complicated = 1;
172 return;
174 case EXPR_ASSIGNMENT:
175 case EXPR_COMPARE:
176 case EXPR_LOGICAL:
177 case EXPR_BINOP: {
178 char tmp[10];
179 struct expression *array_expr;
181 *complicated = 1;
182 array_expr = get_array_expr(expr);
183 if (array_expr) {
184 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated);
185 append(buf, "[", len);
186 } else {
187 __get_variable_from_expr(sym_ptr, buf, expr->left, len,
188 complicated);
189 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
190 append(buf, tmp, len);
192 __get_variable_from_expr(NULL, buf, expr->right,
193 len, complicated);
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,
217 complicated);
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,
224 complicated);
225 } END_FOR_EACH_PTR(tmp);
226 append(buf, ")", len);
227 return;
229 case EXPR_CAST:
230 __get_variable_from_expr(sym_ptr, buf,
231 expr->cast_expression, len,
232 complicated);
233 return;
234 case EXPR_SIZEOF: {
235 int size;
236 char tmp[25];
238 if (expr->cast_type && get_base_type(expr->cast_type)) {
239 size = (get_base_type(expr->cast_type))->bit_size;
240 snprintf(tmp, 25, "%d", bits_to_bytes(size));
241 append(buf, tmp, len);
243 return;
245 default:
246 *complicated = 1;
247 //printf("unknown type = %d\n", expr->type);
248 return;
253 * This is returns a stylized "c looking" representation of the
254 * variable name.
256 * It uses the same buffer every time so you have to save the result
257 * yourself if you want to keep it.
261 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
263 static char var_name[VAR_LEN];
264 int complicated = 0;
266 if (sym_ptr)
267 *sym_ptr = NULL;
268 var_name[0] = '\0';
270 if (!expr)
271 return NULL;
272 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
273 &complicated);
274 if (complicated < 2)
275 return alloc_string(var_name);
276 else
277 return NULL;
280 char *expr_to_str(struct expression *expr)
282 return expr_to_str_sym(expr, NULL);
286 * get_variable_from_expr_simple() only returns simple variables.
287 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
288 * then it returns NULL.
291 char *expr_to_var_sym(struct expression *expr,
292 struct symbol **sym_ptr)
294 static char var_name[VAR_LEN];
295 int complicated = 0;
297 if (sym_ptr)
298 *sym_ptr = NULL;
299 var_name[0] = '\0';
301 if (!expr)
302 return NULL;
303 expr = strip_expr(expr);
304 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
305 &complicated);
307 if (complicated) {
308 if (sym_ptr)
309 *sym_ptr = NULL;
310 return NULL;
312 remove_parens(var_name);
313 return alloc_string(var_name);
316 char *expr_to_var(struct expression *expr)
318 return expr_to_var_sym(expr, NULL);
321 int sym_name_is(const char *name, struct expression *expr)
323 if (!expr)
324 return 0;
325 if (expr->type != EXPR_SYMBOL)
326 return 0;
327 if (!strcmp(expr->symbol_name->name, name))
328 return 1;
329 return 0;
332 int is_zero(struct expression *expr)
334 sval_t sval;
336 if (get_value(expr, &sval) && sval.value == 0)
337 return 1;
338 return 0;
341 int is_array(struct expression *expr)
343 expr = strip_expr(expr);
344 if (expr->type != EXPR_PREOP || expr->op != '*')
345 return 0;
346 expr = expr->unop;
347 if (expr->op != '+')
348 return 0;
349 return 1;
352 struct expression *get_array_name(struct expression *expr)
354 if (!is_array(expr))
355 return NULL;
356 return strip_expr(expr->unop->left);
359 struct expression *get_array_offset(struct expression *expr)
361 if (!is_array(expr))
362 return NULL;
363 return expr->unop->right;
366 const char *show_state(struct smatch_state *state)
368 if (!state)
369 return NULL;
370 return state->name;
373 struct statement *get_expression_statement(struct expression *expr)
375 /* What are those things called? if (({....; ret;})) { ...*/
377 if (expr->type != EXPR_PREOP)
378 return NULL;
379 if (expr->op != '(')
380 return NULL;
381 if (expr->unop->type != EXPR_STATEMENT)
382 return NULL;
383 if (expr->unop->statement->type != STMT_COMPOUND)
384 return NULL;
385 return expr->unop->statement;
388 struct expression *strip_parens(struct expression *expr)
390 if (!expr)
391 return NULL;
393 if (expr->type == EXPR_PREOP) {
394 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
395 expr->unop->statement->type == STMT_COMPOUND)
396 return expr;
397 if (expr->op == '(')
398 return strip_parens(expr->unop);
400 return expr;
403 struct expression *strip_expr(struct expression *expr)
405 if (!expr)
406 return NULL;
408 switch (expr->type) {
409 case EXPR_FORCE_CAST:
410 case EXPR_CAST:
411 return strip_expr(expr->cast_expression);
412 case EXPR_PREOP:
413 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
414 expr->unop->statement->type == STMT_COMPOUND)
415 return expr;
416 if (expr->op == '(')
417 return strip_expr(expr->unop);
419 return expr;
422 static void delete_state_tracker(struct tracker *t)
424 delete_state(t->owner, t->name, t->sym);
425 __free_tracker(t);
428 void scoped_state(int my_id, const char *name, struct symbol *sym)
430 struct tracker *t;
432 t = alloc_tracker(my_id, name, sym);
433 add_scope_hook((scope_hook *)&delete_state_tracker, t);
436 int is_error_return(struct expression *expr)
438 struct symbol *cur_func = cur_func_sym;
439 sval_t sval;
441 if (!expr)
442 return 0;
443 if (cur_func->type != SYM_NODE)
444 return 0;
445 cur_func = get_base_type(cur_func);
446 if (cur_func->type != SYM_FN)
447 return 0;
448 cur_func = get_base_type(cur_func);
449 if (cur_func == &void_ctype)
450 return 0;
451 if (!get_value(expr, &sval))
452 return 0;
453 if (sval_cmp_val(sval, 0) < 0)
454 return 1;
455 if (cur_func->type == SYM_PTR && sval.value == 0)
456 return 1;
457 return 0;
460 int getting_address(void)
462 struct expression *tmp;
463 int i = 0;
464 int dot_ops = 0;
466 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
467 if (!i++)
468 continue;
469 if (tmp->type == EXPR_PREOP && tmp->op == '(')
470 continue;
471 if (tmp->op == '.' && !dot_ops++)
472 continue;
473 if (tmp->op == '&')
474 return 1;
475 return 0;
476 } END_FOR_EACH_PTR_REVERSE(tmp);
477 return 0;
480 char *get_member_name(struct expression *expr)
482 char buf[256];
483 struct symbol *sym;
485 expr = strip_expr(expr);
486 if (expr->type != EXPR_DEREF)
487 return NULL;
488 sym = get_type(expr->deref);
489 if (!sym || !sym->ident)
490 return NULL;
491 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
492 return alloc_string(buf);
495 int positions_eq(struct position pos1, struct position pos2)
497 if (pos1.line != pos2.line)
498 return 0;
499 if (pos1.pos != pos2.pos)
500 return 0;
501 if (pos1.stream != pos2.stream)
502 return 0;
503 return 1;
506 struct statement *get_current_statement(void)
508 struct statement *prev, *tmp;
510 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
512 if (!prev || !get_macro_name(prev->pos))
513 return prev;
515 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
516 if (positions_eq(tmp->pos, prev->pos))
517 continue;
518 if (prev->pos.line > tmp->pos.line)
519 return prev;
520 return tmp;
521 } END_FOR_EACH_PTR_REVERSE(tmp);
522 return prev;
525 int get_param_num_from_sym(struct symbol *sym)
527 struct symbol *tmp;
528 int i;
530 if (!cur_func_sym)
531 return -1;
533 i = 0;
534 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
535 if (tmp == sym)
536 return i;
537 i++;
538 } END_FOR_EACH_PTR(tmp);
539 return -1;