db: don't increment the func_id too much
[smatch.git] / smatch_helper.c
blobcebe9f469f2002ceb37f036175c33dd88764b209
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 struct smatch_state *alloc_state_num(int num)
39 struct smatch_state *state;
40 static char buff[256];
42 state = __alloc_smatch_state(0);
43 snprintf(buff, 255, "%d", num);
44 buff[255] = '\0';
45 state->name = alloc_string(buff);
46 state->data = INT_PTR(num);
47 return state;
50 static void append(char *dest, const char *data, int buff_len)
52 strncat(dest, data, buff_len - strlen(dest) - 1);
56 * If you have "foo(a, b, 1);" then use
57 * get_argument_from_call_expr(expr, 0) to return the expression for
58 * a. Yes, it does start counting from 0.
60 struct expression *get_argument_from_call_expr(struct expression_list *args,
61 int num)
63 struct expression *expr;
64 int i = 0;
66 if (!args)
67 return NULL;
69 FOR_EACH_PTR(args, expr) {
70 if (i == num)
71 return expr;
72 i++;
73 } END_FOR_EACH_PTR(expr);
74 return NULL;
77 static struct expression *get_array_expr(struct expression *expr)
79 struct symbol *type;
81 if (expr->type != EXPR_BINOP || expr->op != '+')
82 return NULL;
84 type = get_type(expr->left);
85 if (!type || type->type != SYM_ARRAY)
86 return NULL;
87 return expr->left;
90 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
91 struct expression *expr, int len,
92 int *complicated)
94 struct expression *tmp;
96 switch (expr->type) {
97 case EXPR_DEREF:
98 tmp = expr->deref;
99 if (tmp->op == '*')
100 tmp = tmp->unop;
102 __get_variable_from_expr(sym_ptr, buf, tmp, len, complicated);
104 tmp = expr->deref;
105 if (tmp->op == '*')
106 append(buf, "->", len);
107 else
108 append(buf, ".", len);
110 append(buf, expr->member->name, len);
112 return;
113 case EXPR_SYMBOL:
114 if (expr->symbol_name)
115 append(buf, expr->symbol_name->name, len);
116 if (sym_ptr) {
117 if (*sym_ptr)
118 *complicated = 1;
119 *sym_ptr = expr->symbol;
121 return;
122 case EXPR_PREOP: {
123 const char *tmp;
125 if (get_expression_statement(expr)) {
126 *complicated = 2;
127 return;
130 if (expr->op != '*' || !get_array_expr(expr->unop)) {
131 tmp = show_special(expr->op);
132 append(buf, tmp, len);
134 __get_variable_from_expr(sym_ptr, buf, expr->unop,
135 len, complicated);
137 if (expr->op == '(')
138 append(buf, ")", len);
140 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
141 *complicated = 1;
143 return;
145 case EXPR_POSTOP: {
146 const char *tmp;
148 __get_variable_from_expr(sym_ptr, buf, expr->unop,
149 len, complicated);
150 tmp = show_special(expr->op);
151 append(buf, tmp, len);
153 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
154 *complicated = 1;
155 return;
157 case EXPR_BINOP: {
158 char tmp[10];
159 struct expression *array_expr;
161 *complicated = 1;
162 array_expr = get_array_expr(expr);
163 if (array_expr) {
164 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated);
165 append(buf, "[", len);
166 } else {
167 __get_variable_from_expr(sym_ptr, buf, expr->left, len,
168 complicated);
169 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
170 append(buf, tmp, len);
172 __get_variable_from_expr(NULL, buf, expr->right,
173 len, complicated);
174 if (array_expr)
175 append(buf, "]", len);
176 return;
178 case EXPR_VALUE: {
179 char tmp[25];
181 *complicated = 1;
182 snprintf(tmp, 25, "%lld", expr->value);
183 append(buf, tmp, len);
184 return;
186 case EXPR_STRING:
187 append(buf, "\"", len);
188 append(buf, expr->string->data, len);
189 append(buf, "\"", len);
190 return;
191 case EXPR_CALL: {
192 struct expression *tmp;
193 int i;
195 *complicated = 1;
196 __get_variable_from_expr(NULL, buf, expr->fn, len,
197 complicated);
198 append(buf, "(", len);
199 i = 0;
200 FOR_EACH_PTR_REVERSE(expr->args, tmp) {
201 if (i++)
202 append(buf, ", ", len);
203 __get_variable_from_expr(NULL, buf, tmp, len,
204 complicated);
205 } END_FOR_EACH_PTR_REVERSE(tmp);
206 append(buf, ")", len);
207 return;
209 case EXPR_CAST:
210 __get_variable_from_expr(sym_ptr, buf,
211 expr->cast_expression, len,
212 complicated);
213 return;
214 case EXPR_SIZEOF: {
215 int size;
216 char tmp[25];
218 if (expr->cast_type && get_base_type(expr->cast_type)) {
219 size = (get_base_type(expr->cast_type))->bit_size;
220 snprintf(tmp, 25, "%d", bits_to_bytes(size));
221 append(buf, tmp, len);
223 return;
225 default:
226 *complicated = 1;
227 //printf("unknown type = %d\n", expr->type);
228 return;
233 * This is returns a stylized "c looking" representation of the
234 * variable name.
236 * It uses the same buffer every time so you have to save the result
237 * yourself if you want to keep it.
241 char *get_variable_from_expr_complex(struct expression *expr, struct symbol **sym_ptr)
243 static char var_name[VAR_LEN];
244 int complicated = 0;
246 if (sym_ptr)
247 *sym_ptr = NULL;
248 var_name[0] = '\0';
250 if (!expr)
251 return NULL;
252 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
253 &complicated);
254 if (complicated < 2)
255 return alloc_string(var_name);
256 else
257 return NULL;
261 * get_variable_from_expr_simple() only returns simple variables.
262 * If it's a complicated variable like a->foo instead of just 'a'
263 * then it returns NULL.
266 char *get_variable_from_expr(struct expression *expr,
267 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 expr = strip_expr(expr);
279 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
280 &complicated);
282 if (complicated) {
283 if (sym_ptr)
284 *sym_ptr = NULL;
285 return NULL;
287 return alloc_string(var_name);
290 int sym_name_is(const char *name, struct expression *expr)
292 if (!expr)
293 return 0;
294 if (expr->type != EXPR_SYMBOL)
295 return 0;
296 if (!strcmp(expr->symbol_name->name, name))
297 return 1;
298 return 0;
301 int is_zero(struct expression *expr)
303 long long val;
305 if (get_value(expr, &val) && val == 0)
306 return 1;
307 return 0;
310 int is_array(struct expression *expr)
312 expr = strip_expr(expr);
313 if (expr->type != EXPR_PREOP || expr->op != '*')
314 return 0;
315 expr = expr->unop;
316 if (expr->op != '+')
317 return 0;
318 return 1;
321 struct expression *get_array_name(struct expression *expr)
323 if (!is_array(expr))
324 return NULL;
325 return strip_expr(expr->unop->left);
328 struct expression *get_array_offset(struct expression *expr)
330 if (!is_array(expr))
331 return NULL;
332 return expr->unop->right;
335 const char *show_state(struct smatch_state *state)
337 if (!state)
338 return NULL;
339 return state->name;
342 struct statement *get_expression_statement(struct expression *expr)
344 /* What are those things called? if (({....; ret;})) { ...*/
346 if (expr->type != EXPR_PREOP)
347 return NULL;
348 if (expr->op != '(')
349 return NULL;
350 if (expr->unop->type != EXPR_STATEMENT)
351 return NULL;
352 if (expr->unop->statement->type != STMT_COMPOUND)
353 return NULL;
354 return expr->unop->statement;
357 struct expression *strip_parens(struct expression *expr)
359 if (!expr)
360 return NULL;
362 if (expr->type == EXPR_PREOP) {
363 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
364 expr->unop->statement->type == STMT_COMPOUND)
365 return expr;
366 if (expr->op == '(')
367 return strip_parens(expr->unop);
369 return expr;
372 struct expression *strip_expr(struct expression *expr)
374 if (!expr)
375 return NULL;
377 switch (expr->type) {
378 case EXPR_FORCE_CAST:
379 case EXPR_CAST:
380 return strip_expr(expr->cast_expression);
381 case EXPR_PREOP:
382 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
383 expr->unop->statement->type == STMT_COMPOUND)
384 return expr;
385 if (expr->op == '(')
386 return strip_expr(expr->unop);
388 return expr;
391 static void delete_state_tracker(struct tracker *t)
393 delete_state(t->owner, t->name, t->sym);
394 __free_tracker(t);
397 void scoped_state(int my_id, const char *name, struct symbol *sym)
399 struct tracker *t;
401 t = alloc_tracker(my_id, name, sym);
402 add_scope_hook((scope_hook *)&delete_state_tracker, t);
405 int is_error_return(struct expression *expr)
407 struct symbol *cur_func = cur_func_sym;
408 long long val;
410 if (!expr)
411 return 0;
412 if (cur_func->type != SYM_NODE)
413 return 0;
414 cur_func = get_base_type(cur_func);
415 if (cur_func->type != SYM_FN)
416 return 0;
417 cur_func = get_base_type(cur_func);
418 if (cur_func == &void_ctype)
419 return 0;
420 if (!get_value(expr, &val))
421 return 0;
422 if (val < 0)
423 return 1;
424 if (cur_func->type == SYM_PTR && val == 0)
425 return 1;
426 return 0;
429 int getting_address(void)
431 struct expression *tmp;
432 int i = 0;
433 int dot_ops = 0;
435 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
436 if (!i++)
437 continue;
438 if (tmp->type == EXPR_PREOP && tmp->op == '(')
439 continue;
440 if (tmp->op == '.' && !dot_ops++)
441 continue;
442 if (tmp->op == '&')
443 return 1;
444 return 0;
445 } END_FOR_EACH_PTR_REVERSE(tmp);
446 return 0;
449 char *get_fnptr_name(struct expression *expr)
451 char buf[256];
452 struct symbol *sym;
454 if (expr->type == EXPR_SYMBOL)
455 return get_variable_from_expr(expr, NULL);
457 if (expr->type != EXPR_DEREF)
458 return NULL;
459 sym = get_type(expr->deref);
460 if (!sym || !sym->ident)
461 return NULL;
462 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
463 return alloc_string(buf);