de3057b176ee10c87a8d5bce9151935ba49a8315
[smatch.git] / smatch_helper.c
blobde3057b176ee10c87a8d5bce9151935ba49a8315
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 ||
141 expr->op == SPECIAL_INCREMENT ||
142 expr->op == '&')
143 *complicated = 1;
145 return;
147 case EXPR_POSTOP: {
148 const char *tmp;
150 __get_variable_from_expr(sym_ptr, buf, expr->unop,
151 len, complicated);
152 tmp = show_special(expr->op);
153 append(buf, tmp, len);
155 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
156 *complicated = 1;
157 return;
159 case EXPR_COMPARE:
160 case EXPR_LOGICAL:
161 case EXPR_BINOP: {
162 char tmp[10];
163 struct expression *array_expr;
165 *complicated = 1;
166 array_expr = get_array_expr(expr);
167 if (array_expr) {
168 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated);
169 append(buf, "[", len);
170 } else {
171 __get_variable_from_expr(sym_ptr, buf, expr->left, len,
172 complicated);
173 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
174 append(buf, tmp, len);
176 __get_variable_from_expr(NULL, buf, expr->right,
177 len, complicated);
178 if (array_expr)
179 append(buf, "]", len);
180 return;
182 case EXPR_VALUE: {
183 char tmp[25];
185 *complicated = 1;
186 snprintf(tmp, 25, "%lld", expr->value);
187 append(buf, tmp, len);
188 return;
190 case EXPR_STRING:
191 append(buf, "\"", len);
192 append(buf, expr->string->data, len);
193 append(buf, "\"", len);
194 return;
195 case EXPR_CALL: {
196 struct expression *tmp;
197 int i;
199 *complicated = 1;
200 __get_variable_from_expr(NULL, buf, expr->fn, len,
201 complicated);
202 append(buf, "(", len);
203 i = 0;
204 FOR_EACH_PTR_REVERSE(expr->args, tmp) {
205 if (i++)
206 append(buf, ", ", len);
207 __get_variable_from_expr(NULL, buf, tmp, len,
208 complicated);
209 } END_FOR_EACH_PTR_REVERSE(tmp);
210 append(buf, ")", len);
211 return;
213 case EXPR_CAST:
214 __get_variable_from_expr(sym_ptr, buf,
215 expr->cast_expression, len,
216 complicated);
217 return;
218 case EXPR_SIZEOF: {
219 int size;
220 char tmp[25];
222 if (expr->cast_type && get_base_type(expr->cast_type)) {
223 size = (get_base_type(expr->cast_type))->bit_size;
224 snprintf(tmp, 25, "%d", bits_to_bytes(size));
225 append(buf, tmp, len);
227 return;
229 default:
230 *complicated = 1;
231 //printf("unknown type = %d\n", expr->type);
232 return;
237 * This is returns a stylized "c looking" representation of the
238 * variable name.
240 * It uses the same buffer every time so you have to save the result
241 * yourself if you want to keep it.
245 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
247 static char var_name[VAR_LEN];
248 int complicated = 0;
250 if (sym_ptr)
251 *sym_ptr = NULL;
252 var_name[0] = '\0';
254 if (!expr)
255 return NULL;
256 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
257 &complicated);
258 if (complicated < 2)
259 return alloc_string(var_name);
260 else
261 return NULL;
264 char *expr_to_str(struct expression *expr)
266 return expr_to_str_sym(expr, NULL);
270 * get_variable_from_expr_simple() only returns simple variables.
271 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
272 * then it returns NULL.
275 char *expr_to_var_sym(struct expression *expr,
276 struct symbol **sym_ptr)
278 static char var_name[VAR_LEN];
279 int complicated = 0;
281 if (sym_ptr)
282 *sym_ptr = NULL;
283 var_name[0] = '\0';
285 if (!expr)
286 return NULL;
287 expr = strip_expr(expr);
288 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
289 &complicated);
291 if (complicated) {
292 if (sym_ptr)
293 *sym_ptr = NULL;
294 return NULL;
296 return alloc_string(var_name);
299 char *expr_to_var(struct expression *expr)
301 return expr_to_var_sym(expr, NULL);
304 int sym_name_is(const char *name, struct expression *expr)
306 if (!expr)
307 return 0;
308 if (expr->type != EXPR_SYMBOL)
309 return 0;
310 if (!strcmp(expr->symbol_name->name, name))
311 return 1;
312 return 0;
315 int is_zero(struct expression *expr)
317 sval_t sval;
319 if (get_value(expr, &sval) && sval.value == 0)
320 return 1;
321 return 0;
324 int is_array(struct expression *expr)
326 expr = strip_expr(expr);
327 if (expr->type != EXPR_PREOP || expr->op != '*')
328 return 0;
329 expr = expr->unop;
330 if (expr->op != '+')
331 return 0;
332 return 1;
335 struct expression *get_array_name(struct expression *expr)
337 if (!is_array(expr))
338 return NULL;
339 return strip_expr(expr->unop->left);
342 struct expression *get_array_offset(struct expression *expr)
344 if (!is_array(expr))
345 return NULL;
346 return expr->unop->right;
349 const char *show_state(struct smatch_state *state)
351 if (!state)
352 return NULL;
353 return state->name;
356 struct statement *get_expression_statement(struct expression *expr)
358 /* What are those things called? if (({....; ret;})) { ...*/
360 if (expr->type != EXPR_PREOP)
361 return NULL;
362 if (expr->op != '(')
363 return NULL;
364 if (expr->unop->type != EXPR_STATEMENT)
365 return NULL;
366 if (expr->unop->statement->type != STMT_COMPOUND)
367 return NULL;
368 return expr->unop->statement;
371 struct expression *strip_parens(struct expression *expr)
373 if (!expr)
374 return NULL;
376 if (expr->type == EXPR_PREOP) {
377 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
378 expr->unop->statement->type == STMT_COMPOUND)
379 return expr;
380 if (expr->op == '(')
381 return strip_parens(expr->unop);
383 return expr;
386 struct expression *strip_expr(struct expression *expr)
388 if (!expr)
389 return NULL;
391 switch (expr->type) {
392 case EXPR_FORCE_CAST:
393 case EXPR_CAST:
394 return strip_expr(expr->cast_expression);
395 case 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_expr(expr->unop);
402 return expr;
405 static void delete_state_tracker(struct tracker *t)
407 delete_state(t->owner, t->name, t->sym);
408 __free_tracker(t);
411 void scoped_state(int my_id, const char *name, struct symbol *sym)
413 struct tracker *t;
415 t = alloc_tracker(my_id, name, sym);
416 add_scope_hook((scope_hook *)&delete_state_tracker, t);
419 int is_error_return(struct expression *expr)
421 struct symbol *cur_func = cur_func_sym;
422 sval_t sval;
424 if (!expr)
425 return 0;
426 if (cur_func->type != SYM_NODE)
427 return 0;
428 cur_func = get_base_type(cur_func);
429 if (cur_func->type != SYM_FN)
430 return 0;
431 cur_func = get_base_type(cur_func);
432 if (cur_func == &void_ctype)
433 return 0;
434 if (!get_value(expr, &sval))
435 return 0;
436 if (sval_cmp_val(sval, 0) < 0)
437 return 1;
438 if (cur_func->type == SYM_PTR && sval.value == 0)
439 return 1;
440 return 0;
443 int getting_address(void)
445 struct expression *tmp;
446 int i = 0;
447 int dot_ops = 0;
449 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
450 if (!i++)
451 continue;
452 if (tmp->type == EXPR_PREOP && tmp->op == '(')
453 continue;
454 if (tmp->op == '.' && !dot_ops++)
455 continue;
456 if (tmp->op == '&')
457 return 1;
458 return 0;
459 } END_FOR_EACH_PTR_REVERSE(tmp);
460 return 0;
463 char *get_member_name(struct expression *expr)
465 char buf[256];
466 struct symbol *sym;
468 if (expr->type != EXPR_DEREF)
469 return NULL;
470 sym = get_type(expr->deref);
471 if (!sym || !sym->ident)
472 return NULL;
473 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
474 return alloc_string(buf);
477 char *get_fnptr_name(struct expression *expr)
479 if (expr->type == EXPR_SYMBOL)
480 return expr_to_var(expr);
481 return get_member_name(expr);
484 int positions_eq(struct position pos1, struct position pos2)
486 if (pos1.line != pos2.line)
487 return 0;
488 if (pos1.pos != pos2.pos)
489 return 0;
490 if (pos1.stream != pos2.stream)
491 return 0;
492 return 1;
495 struct statement *get_current_statement(void)
497 struct statement *prev, *tmp;
499 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
501 if (!prev || !get_macro_name(prev->pos))
502 return prev;
504 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
505 if (positions_eq(tmp->pos, prev->pos))
506 continue;
507 if (prev->pos.line > tmp->pos.line)
508 return prev;
509 return tmp;
510 } END_FOR_EACH_PTR_REVERSE(tmp);
511 return prev;
514 int get_param_num_from_sym(struct symbol *sym)
516 struct symbol *tmp;
517 int i;
519 i = 0;
520 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
521 if (tmp == sym)
522 return i;
523 i++;
524 } END_FOR_EACH_PTR(tmp);
525 return -1;