small clean up.
[smatch.git] / smatch_helper.c
blob2ebfd8724fff7501177dd085ebffbe7446d94df2
1 /*
2 * sparse/smatch_helper.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include "allocate.h"
13 #include "smatch.h"
15 #define VAR_LEN 512
18 char *alloc_string(const char *str)
20 char *tmp;
22 if (!str)
23 return NULL;
24 tmp = malloc(strlen(str) + 1);
25 strcpy(tmp, str);
26 return tmp;
29 void free_string(char *str)
31 free(str);
34 static void append(char *dest, const char *data, int buff_len)
36 strncat(dest, data, buff_len - strlen(dest) - 1);
40 * If you have "foo(a, b, 1);" then use
41 * get_argument_from_call_expr(expr, 0) to return the expression for
42 * a. Yes, it does start counting from 0.
44 struct expression *get_argument_from_call_expr(struct expression_list *args,
45 int num)
47 struct expression *expr;
48 int i = 0;
50 if (!args)
51 return NULL;
53 FOR_EACH_PTR(args, expr) {
54 if (i == num)
55 return expr;
56 i++;
57 } END_FOR_EACH_PTR(expr);
58 return NULL;
61 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
62 struct expression *expr, int len,
63 int *complicated)
65 struct expression *tmp;
67 switch(expr->type) {
68 case EXPR_DEREF:
69 tmp = expr->deref;
70 if (!strcmp(show_special(tmp->op), "*")) {
71 tmp = tmp->unop;
73 __get_variable_from_expr(sym_ptr, buf, tmp, len, complicated);
75 tmp = expr->deref;
76 if (!strcmp(show_special(tmp->op), "*")) {
77 append(buf, "->", len);
78 } else {
79 append(buf, ".", len);
81 append(buf, expr->member->name, len);
83 return;
84 case EXPR_SYMBOL:
85 if (expr->symbol_name)
86 append(buf, expr->symbol_name->name, len);
87 if (sym_ptr) {
88 if (*sym_ptr)
89 *complicated = 1;
90 *sym_ptr = expr->symbol;
92 return;
93 case EXPR_PREOP: {
94 const char *tmp;
96 if (get_block_thing(expr)) {
97 *complicated = 2;
98 return;
102 tmp = show_special(expr->op);
103 append(buf, tmp, len);
104 __get_variable_from_expr(sym_ptr, buf, expr->unop,
105 len, complicated);
107 if (tmp[0] == '(') {
108 append(buf, ")", len);
111 if ((!strcmp(tmp, "--")) || (!strcmp(tmp, "++")))
112 *complicated = 1;
114 return;
116 case EXPR_POSTOP: {
117 const char *tmp;
119 __get_variable_from_expr(sym_ptr, buf, expr->unop,
120 len, complicated);
121 tmp = show_special(expr->op);
122 append(buf, tmp, len);
124 if ((!strcmp(tmp, "--")) || (!strcmp(tmp, "++")))
125 *complicated = 1;
127 return;
129 case EXPR_BINOP: {
130 const char *tmp;
132 *complicated = 1;
133 append(buf, "(", len);
134 __get_variable_from_expr(NULL, buf, expr->left, len,
135 complicated);
136 tmp = show_special(expr->op);
137 append(buf, tmp, len);
138 __get_variable_from_expr(sym_ptr, buf, expr->right,
139 len, complicated);
140 append(buf, ")", len);
141 return;
143 case EXPR_VALUE: {
144 char tmp[25];
146 snprintf(tmp, 25, "%lld", expr->value);
147 append(buf, tmp, len);
148 return;
150 case EXPR_STRING:
151 append(buf, expr->string->data, len);
152 return;
153 case EXPR_CALL: {
154 struct expression *tmp;
155 int i;
157 *complicated = 1;
158 __get_variable_from_expr(NULL, buf, expr->fn, len,
159 complicated);
160 append(buf, "(", len);
161 i = 0;
162 FOR_EACH_PTR_REVERSE(expr->args, tmp) {
163 if (i++)
164 append(buf, ", ", len);
165 __get_variable_from_expr(NULL, buf, tmp, len,
166 complicated);
167 } END_FOR_EACH_PTR_REVERSE(tmp);
168 append(buf, ")", len);
169 return;
171 case EXPR_CAST:
172 __get_variable_from_expr(sym_ptr, buf,
173 expr->cast_expression, len,
174 complicated);
175 return;
176 case EXPR_SIZEOF: {
177 int size;
178 char tmp[25];
180 if (expr->cast_type && get_base_type(expr->cast_type)) {
181 size = (get_base_type(expr->cast_type))->bit_size;
182 snprintf(tmp, 25, "%d", size);
183 append(buf, tmp, len);
185 return;
187 default:
188 *complicated = 1;
189 //printf("unknown type = %d\n", expr->type);
190 return;
196 * This is returns a stylized "c looking" representation of the
197 * variable name.
199 * It uses the same buffer every time so you have to save the result
200 * yourself if you want to keep it.
204 char *get_variable_from_expr_complex(struct expression *expr, struct symbol **sym_ptr)
206 static char var_name[VAR_LEN];
207 int complicated = 0;
209 if (sym_ptr)
210 *sym_ptr = NULL;
211 var_name[0] = '\0';
213 if (!expr)
214 return NULL;
215 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
216 &complicated);
217 if (complicated < 2)
218 return alloc_string(var_name);
219 else
220 return NULL;
224 * get_variable_from_expr_simple() only returns simple variables.
225 * If it's a complicated variable like a->foo instead of just 'a'
226 * then it returns NULL.
229 char *get_variable_from_expr(struct expression *expr,
230 struct symbol **sym_ptr)
232 static char var_name[VAR_LEN];
233 int complicated = 0;
235 if (sym_ptr)
236 *sym_ptr = NULL;
237 var_name[0] = '\0';
239 if (!expr)
240 return NULL;
241 expr = strip_expr(expr);
242 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
243 &complicated);
245 if (complicated) {
246 if (sym_ptr)
247 *sym_ptr = NULL;
248 return NULL;
250 return alloc_string(var_name);
253 struct symbol *get_ptr_type_ptr(struct symbol *sym)
255 if (!sym) {
256 return NULL;
259 if (sym->type != SYM_NODE)
260 return NULL;
261 sym = get_base_type(sym);
262 if (sym->type != SYM_PTR)
263 return NULL;
264 sym = get_base_type(sym);
265 return sym;
268 static struct symbol *get_struct_sym(struct expression *expr)
270 struct symbol *base_type;
271 struct symbol *parent_struct;
272 struct symbol *tmp;
274 if (expr->type != EXPR_PREOP)
275 return NULL;
277 expr = expr->unop;
278 if (expr->type == EXPR_DEREF) {
279 parent_struct = get_struct_sym(expr->deref);
280 if (!parent_struct)
281 return NULL;
282 tmp = NULL;
283 FOR_EACH_PTR(parent_struct->symbol_list, tmp) {
284 if (tmp->ident == expr->member)
285 break;
286 } END_FOR_EACH_PTR(tmp);
287 if (!tmp || tmp->ident != expr->member)
288 return NULL;
289 base_type = get_base_type(tmp);
290 } else if (expr->type == EXPR_SYMBOL) {
291 base_type = get_base_type(expr->symbol);
292 } else {
293 return NULL;
295 if (base_type->type != SYM_PTR)
296 return NULL;
297 base_type = get_base_type(base_type);
298 if (base_type->type != SYM_STRUCT && base_type->type != SYM_UNION)
299 return NULL;
300 return base_type;
303 struct symbol *get_deref_type(struct expression *expr)
305 struct ident *member = expr->member;
306 struct symbol *struct_sym;
307 struct symbol *tmp;
309 struct_sym = get_struct_sym(expr->deref);
310 if (!struct_sym || (struct_sym->type != SYM_STRUCT
311 && struct_sym->type != SYM_UNION))
312 return NULL;
313 FOR_EACH_PTR(struct_sym->symbol_list, tmp) {
314 if (tmp->ident == member)
315 return get_ptr_type_ptr(tmp);
316 } END_FOR_EACH_PTR(tmp);
317 return NULL;
320 struct symbol *get_ptr_type(struct expression *expr)
322 struct symbol *ptr_type = NULL;
324 if (!expr)
325 return NULL;
326 if (expr->type == EXPR_DEREF)
327 ptr_type = get_deref_type(expr);
328 if (expr->type == EXPR_SYMBOL)
329 ptr_type = get_ptr_type_ptr(expr->symbol);
330 return ptr_type;
333 int sym_name_is(const char *name, struct expression *expr)
335 if (!expr)
336 return 0;
337 if (expr->type != EXPR_SYMBOL)
338 return 0;
339 if (!strcmp(expr->symbol_name->name, name))
340 return 1;
341 return 0;
344 #define NOTIMPLIED 0
345 #define IMPLIED 1
347 static int _get_value(struct expression *expr, int *discard, int implied)
349 int dis = 0;
350 int ret = UNDEFINED;
352 if (!expr)
353 return UNDEFINED;
354 if (!discard)
355 discard = &dis;
356 if (*discard)
357 return UNDEFINED;
359 expr = strip_expr(expr);
361 switch (expr->type){
362 case EXPR_VALUE:
363 ret = expr->value;
364 break;
365 case EXPR_PREOP:
366 if (!strcmp("-", show_special(expr->op)))
367 ret = - _get_value(expr->unop, discard, implied);
368 else
369 *discard = 1;
370 break;
371 case EXPR_BINOP: {
372 int left, right;
374 if (!show_special(expr->op)) {
375 *discard = 1;
376 break;
378 left = _get_value(expr->left, discard, implied);
379 right = _get_value(expr->right, discard, implied);
380 if (!strcmp("*", show_special(expr->op))) {
381 ret = left * right;
382 } else if (!strcmp("/", show_special(expr->op))) {
383 ret = left / right;
384 } else if (!strcmp("+", show_special(expr->op))) {
385 ret = left + right;
386 } else if (!strcmp("-", show_special(expr->op))) {
387 ret = left - right;
388 } else if (!strcmp("|", show_special(expr->op))) {
389 ret = left | right;
390 } else if (!strcmp("&", show_special(expr->op))) {
391 ret = left & right;
392 } else if (!strcmp(">>", show_special(expr->op))) {
393 ret = left >> right;
394 } else if (!strcmp("<<", show_special(expr->op))) {
395 ret = left << right;
396 } else {
397 *discard = 1;
399 break;
401 case EXPR_PTRSIZEOF:
402 case EXPR_SIZEOF:
403 ret = get_expression_value(expr);
404 break;
405 default:
406 if (implied) {
407 ret = get_implied_single_val(expr);
408 if (ret == UNDEFINED)
409 *discard = 1;
410 } else {
411 *discard = 1;
414 if (*discard)
415 return UNDEFINED;
416 return ret;
419 /* returns UNDEFINED on error */
420 int get_value(struct expression *expr)
422 return _get_value(expr, NULL, NOTIMPLIED);
425 int get_implied_value(struct expression *expr)
427 return _get_value(expr, NULL, IMPLIED);
430 int is_zero(struct expression *expr)
432 if (get_value(expr) == 0)
433 return 1;
434 return 0;
437 int is_array(struct expression *expr)
439 expr = strip_expr(expr);
440 if (expr->type != EXPR_PREOP || strcmp(show_special(expr->op), "*"))
441 return 0;
442 expr = expr->unop;
443 if (strcmp(show_special(expr->op), "+"))
444 return 0;
445 return 1;
448 struct expression *get_array_name(struct expression *expr)
450 if (!is_array(expr))
451 return NULL;
452 return expr->unop->left;
455 struct expression *get_array_offset(struct expression *expr)
457 if (!is_array(expr))
458 return NULL;
459 return expr->unop->right;
462 const char *show_state(struct smatch_state *state)
464 if (!state)
465 return NULL;
466 return state->name;
469 struct statement *get_block_thing(struct expression *expr)
471 /* What are those things called? if (({....; ret;})) { ...*/
473 if (expr->type != EXPR_PREOP)
474 return NULL;
475 if (expr->op != '(')
476 return NULL;
477 if (expr->unop->type != EXPR_STATEMENT)
478 return NULL;
479 if (expr->unop->statement->type != STMT_COMPOUND)
480 return NULL;
481 return expr->unop->statement;
484 struct expression *strip_expr(struct expression *expr)
486 if (!expr)
487 return NULL;
489 switch(expr->type) {
490 case EXPR_CAST:
491 return strip_expr(expr->cast_expression);
492 case EXPR_PREOP:
493 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
494 expr->unop->statement->type == STMT_COMPOUND)
495 return expr;
496 if (expr->op == '(')
497 return strip_expr(expr->unop);
499 return expr;
502 static void delete_state_tracker(struct tracker *t)
504 delete_state(t->owner, t->name, t->sym);
505 __free_tracker(t);
508 void scoped_state(const char *name, int my_id, struct symbol *sym)
510 struct tracker *t;
512 t = alloc_tracker(my_id, name, sym);
513 add_scope_hook((scope_hook *)&delete_state_tracker, t);