db: caller info needs to record the -1 parameters
[smatch.git] / smatch_helper.c
blob0babebe95b1fac982e3a240548fd26e25c972b3c
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);
141 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
142 *complicated = 1;
144 return;
146 case EXPR_POSTOP: {
147 const char *tmp;
149 __get_variable_from_expr(sym_ptr, buf, expr->unop,
150 len, complicated);
151 tmp = show_special(expr->op);
152 append(buf, tmp, len);
154 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
155 *complicated = 1;
156 return;
158 case EXPR_BINOP: {
159 char tmp[10];
160 struct expression *array_expr;
162 *complicated = 1;
163 array_expr = get_array_expr(expr);
164 if (array_expr) {
165 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated);
166 append(buf, "[", len);
167 } else {
168 __get_variable_from_expr(sym_ptr, buf, expr->left, len,
169 complicated);
170 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
171 append(buf, tmp, len);
173 __get_variable_from_expr(NULL, buf, expr->right,
174 len, complicated);
175 if (array_expr)
176 append(buf, "]", len);
177 return;
179 case EXPR_VALUE: {
180 char tmp[25];
182 *complicated = 1;
183 snprintf(tmp, 25, "%lld", expr->value);
184 append(buf, tmp, len);
185 return;
187 case EXPR_STRING:
188 append(buf, "\"", len);
189 append(buf, expr->string->data, len);
190 append(buf, "\"", len);
191 return;
192 case EXPR_CALL: {
193 struct expression *tmp;
194 int i;
196 *complicated = 1;
197 __get_variable_from_expr(NULL, buf, expr->fn, len,
198 complicated);
199 append(buf, "(", len);
200 i = 0;
201 FOR_EACH_PTR_REVERSE(expr->args, tmp) {
202 if (i++)
203 append(buf, ", ", len);
204 __get_variable_from_expr(NULL, buf, tmp, len,
205 complicated);
206 } END_FOR_EACH_PTR_REVERSE(tmp);
207 append(buf, ")", len);
208 return;
210 case EXPR_CAST:
211 __get_variable_from_expr(sym_ptr, buf,
212 expr->cast_expression, len,
213 complicated);
214 return;
215 case EXPR_SIZEOF: {
216 int size;
217 char tmp[25];
219 if (expr->cast_type && get_base_type(expr->cast_type)) {
220 size = (get_base_type(expr->cast_type))->bit_size;
221 snprintf(tmp, 25, "%d", bits_to_bytes(size));
222 append(buf, tmp, len);
224 return;
226 default:
227 *complicated = 1;
228 //printf("unknown type = %d\n", expr->type);
229 return;
234 * This is returns a stylized "c looking" representation of the
235 * variable name.
237 * It uses the same buffer every time so you have to save the result
238 * yourself if you want to keep it.
242 char *get_variable_from_expr_complex(struct expression *expr, struct symbol **sym_ptr)
244 static char var_name[VAR_LEN];
245 int complicated = 0;
247 if (sym_ptr)
248 *sym_ptr = NULL;
249 var_name[0] = '\0';
251 if (!expr)
252 return NULL;
253 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
254 &complicated);
255 if (complicated < 2)
256 return alloc_string(var_name);
257 else
258 return NULL;
262 * get_variable_from_expr_simple() only returns simple variables.
263 * If it's a complicated variable like a->foo instead of just 'a'
264 * then it returns NULL.
267 char *get_variable_from_expr(struct expression *expr,
268 struct symbol **sym_ptr)
270 static char var_name[VAR_LEN];
271 int complicated = 0;
273 if (sym_ptr)
274 *sym_ptr = NULL;
275 var_name[0] = '\0';
277 if (!expr)
278 return NULL;
279 expr = strip_expr(expr);
280 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
281 &complicated);
283 if (complicated) {
284 if (sym_ptr)
285 *sym_ptr = NULL;
286 return NULL;
288 return alloc_string(var_name);
291 int sym_name_is(const char *name, struct expression *expr)
293 if (!expr)
294 return 0;
295 if (expr->type != EXPR_SYMBOL)
296 return 0;
297 if (!strcmp(expr->symbol_name->name, name))
298 return 1;
299 return 0;
302 int is_zero(struct expression *expr)
304 long long val;
306 if (get_value(expr, &val) && val == 0)
307 return 1;
308 return 0;
311 int is_array(struct expression *expr)
313 expr = strip_expr(expr);
314 if (expr->type != EXPR_PREOP || expr->op != '*')
315 return 0;
316 expr = expr->unop;
317 if (expr->op != '+')
318 return 0;
319 return 1;
322 struct expression *get_array_name(struct expression *expr)
324 if (!is_array(expr))
325 return NULL;
326 return strip_expr(expr->unop->left);
329 struct expression *get_array_offset(struct expression *expr)
331 if (!is_array(expr))
332 return NULL;
333 return expr->unop->right;
336 const char *show_state(struct smatch_state *state)
338 if (!state)
339 return NULL;
340 return state->name;
343 struct statement *get_expression_statement(struct expression *expr)
345 /* What are those things called? if (({....; ret;})) { ...*/
347 if (expr->type != EXPR_PREOP)
348 return NULL;
349 if (expr->op != '(')
350 return NULL;
351 if (expr->unop->type != EXPR_STATEMENT)
352 return NULL;
353 if (expr->unop->statement->type != STMT_COMPOUND)
354 return NULL;
355 return expr->unop->statement;
358 struct expression *strip_parens(struct expression *expr)
360 if (!expr)
361 return NULL;
363 if (expr->type == EXPR_PREOP) {
364 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
365 expr->unop->statement->type == STMT_COMPOUND)
366 return expr;
367 if (expr->op == '(')
368 return strip_parens(expr->unop);
370 return expr;
373 struct expression *strip_expr(struct expression *expr)
375 if (!expr)
376 return NULL;
378 switch (expr->type) {
379 case EXPR_FORCE_CAST:
380 case EXPR_CAST:
381 return strip_expr(expr->cast_expression);
382 case EXPR_PREOP:
383 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
384 expr->unop->statement->type == STMT_COMPOUND)
385 return expr;
386 if (expr->op == '(')
387 return strip_expr(expr->unop);
389 return expr;
392 static void delete_state_tracker(struct tracker *t)
394 delete_state(t->owner, t->name, t->sym);
395 __free_tracker(t);
398 void scoped_state(int my_id, const char *name, struct symbol *sym)
400 struct tracker *t;
402 t = alloc_tracker(my_id, name, sym);
403 add_scope_hook((scope_hook *)&delete_state_tracker, t);
406 int is_error_return(struct expression *expr)
408 struct symbol *cur_func = cur_func_sym;
409 long long val;
411 if (!expr)
412 return 0;
413 if (cur_func->type != SYM_NODE)
414 return 0;
415 cur_func = get_base_type(cur_func);
416 if (cur_func->type != SYM_FN)
417 return 0;
418 cur_func = get_base_type(cur_func);
419 if (cur_func == &void_ctype)
420 return 0;
421 if (!get_value(expr, &val))
422 return 0;
423 if (val < 0)
424 return 1;
425 if (cur_func->type == SYM_PTR && val == 0)
426 return 1;
427 return 0;
430 int getting_address(void)
432 struct expression *tmp;
433 int i = 0;
434 int dot_ops = 0;
436 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
437 if (!i++)
438 continue;
439 if (tmp->type == EXPR_PREOP && tmp->op == '(')
440 continue;
441 if (tmp->op == '.' && !dot_ops++)
442 continue;
443 if (tmp->op == '&')
444 return 1;
445 return 0;
446 } END_FOR_EACH_PTR_REVERSE(tmp);
447 return 0;
450 char *get_fnptr_name(struct expression *expr)
452 char buf[256];
453 struct symbol *sym;
455 if (expr->type == EXPR_SYMBOL)
456 return get_variable_from_expr(expr, NULL);
458 if (expr->type != EXPR_DEREF)
459 return NULL;
460 sym = get_type(expr->deref);
461 if (!sym || !sym->ident)
462 return NULL;
463 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
464 return alloc_string(buf);