locking: ignore parenthesis
[smatch.git] / smatch_helper.c
blobba11ab3b9640a160fbc96c5a24dd630b5b088a33
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_ASSIGNMENT:
160 case EXPR_COMPARE:
161 case EXPR_LOGICAL:
162 case EXPR_BINOP: {
163 char tmp[10];
164 struct expression *array_expr;
166 *complicated = 1;
167 array_expr = get_array_expr(expr);
168 if (array_expr) {
169 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated);
170 append(buf, "[", len);
171 } else {
172 __get_variable_from_expr(sym_ptr, buf, expr->left, len,
173 complicated);
174 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
175 append(buf, tmp, len);
177 __get_variable_from_expr(NULL, buf, expr->right,
178 len, complicated);
179 if (array_expr)
180 append(buf, "]", len);
181 return;
183 case EXPR_VALUE: {
184 char tmp[25];
186 *complicated = 1;
187 snprintf(tmp, 25, "%lld", expr->value);
188 append(buf, tmp, len);
189 return;
191 case EXPR_STRING:
192 append(buf, "\"", len);
193 append(buf, expr->string->data, len);
194 append(buf, "\"", len);
195 return;
196 case EXPR_CALL: {
197 struct expression *tmp;
198 int i;
200 *complicated = 1;
201 __get_variable_from_expr(NULL, buf, expr->fn, len,
202 complicated);
203 append(buf, "(", len);
204 i = 0;
205 FOR_EACH_PTR_REVERSE(expr->args, tmp) {
206 if (i++)
207 append(buf, ", ", len);
208 __get_variable_from_expr(NULL, buf, tmp, len,
209 complicated);
210 } END_FOR_EACH_PTR_REVERSE(tmp);
211 append(buf, ")", len);
212 return;
214 case EXPR_CAST:
215 __get_variable_from_expr(sym_ptr, buf,
216 expr->cast_expression, len,
217 complicated);
218 return;
219 case EXPR_SIZEOF: {
220 int size;
221 char tmp[25];
223 if (expr->cast_type && get_base_type(expr->cast_type)) {
224 size = (get_base_type(expr->cast_type))->bit_size;
225 snprintf(tmp, 25, "%d", bits_to_bytes(size));
226 append(buf, tmp, len);
228 return;
230 default:
231 *complicated = 1;
232 //printf("unknown type = %d\n", expr->type);
233 return;
238 * This is returns a stylized "c looking" representation of the
239 * variable name.
241 * It uses the same buffer every time so you have to save the result
242 * yourself if you want to keep it.
246 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
248 static char var_name[VAR_LEN];
249 int complicated = 0;
251 if (sym_ptr)
252 *sym_ptr = NULL;
253 var_name[0] = '\0';
255 if (!expr)
256 return NULL;
257 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
258 &complicated);
259 if (complicated < 2)
260 return alloc_string(var_name);
261 else
262 return NULL;
265 char *expr_to_str(struct expression *expr)
267 return expr_to_str_sym(expr, NULL);
271 * get_variable_from_expr_simple() only returns simple variables.
272 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
273 * then it returns NULL.
276 char *expr_to_var_sym(struct expression *expr,
277 struct symbol **sym_ptr)
279 static char var_name[VAR_LEN];
280 int complicated = 0;
282 if (sym_ptr)
283 *sym_ptr = NULL;
284 var_name[0] = '\0';
286 if (!expr)
287 return NULL;
288 expr = strip_expr(expr);
289 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
290 &complicated);
292 if (complicated) {
293 if (sym_ptr)
294 *sym_ptr = NULL;
295 return NULL;
297 return alloc_string(var_name);
300 char *expr_to_var(struct expression *expr)
302 return expr_to_var_sym(expr, NULL);
305 int sym_name_is(const char *name, struct expression *expr)
307 if (!expr)
308 return 0;
309 if (expr->type != EXPR_SYMBOL)
310 return 0;
311 if (!strcmp(expr->symbol_name->name, name))
312 return 1;
313 return 0;
316 int is_zero(struct expression *expr)
318 sval_t sval;
320 if (get_value(expr, &sval) && sval.value == 0)
321 return 1;
322 return 0;
325 int is_array(struct expression *expr)
327 expr = strip_expr(expr);
328 if (expr->type != EXPR_PREOP || expr->op != '*')
329 return 0;
330 expr = expr->unop;
331 if (expr->op != '+')
332 return 0;
333 return 1;
336 struct expression *get_array_name(struct expression *expr)
338 if (!is_array(expr))
339 return NULL;
340 return strip_expr(expr->unop->left);
343 struct expression *get_array_offset(struct expression *expr)
345 if (!is_array(expr))
346 return NULL;
347 return expr->unop->right;
350 const char *show_state(struct smatch_state *state)
352 if (!state)
353 return NULL;
354 return state->name;
357 struct statement *get_expression_statement(struct expression *expr)
359 /* What are those things called? if (({....; ret;})) { ...*/
361 if (expr->type != EXPR_PREOP)
362 return NULL;
363 if (expr->op != '(')
364 return NULL;
365 if (expr->unop->type != EXPR_STATEMENT)
366 return NULL;
367 if (expr->unop->statement->type != STMT_COMPOUND)
368 return NULL;
369 return expr->unop->statement;
372 struct expression *strip_parens(struct expression *expr)
374 if (!expr)
375 return NULL;
377 if (expr->type == EXPR_PREOP) {
378 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
379 expr->unop->statement->type == STMT_COMPOUND)
380 return expr;
381 if (expr->op == '(')
382 return strip_parens(expr->unop);
384 return expr;
387 struct expression *strip_expr(struct expression *expr)
389 if (!expr)
390 return NULL;
392 switch (expr->type) {
393 case EXPR_FORCE_CAST:
394 case EXPR_CAST:
395 return strip_expr(expr->cast_expression);
396 case EXPR_PREOP:
397 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
398 expr->unop->statement->type == STMT_COMPOUND)
399 return expr;
400 if (expr->op == '(')
401 return strip_expr(expr->unop);
403 return expr;
406 static void delete_state_tracker(struct tracker *t)
408 delete_state(t->owner, t->name, t->sym);
409 __free_tracker(t);
412 void scoped_state(int my_id, const char *name, struct symbol *sym)
414 struct tracker *t;
416 t = alloc_tracker(my_id, name, sym);
417 add_scope_hook((scope_hook *)&delete_state_tracker, t);
420 int is_error_return(struct expression *expr)
422 struct symbol *cur_func = cur_func_sym;
423 sval_t sval;
425 if (!expr)
426 return 0;
427 if (cur_func->type != SYM_NODE)
428 return 0;
429 cur_func = get_base_type(cur_func);
430 if (cur_func->type != SYM_FN)
431 return 0;
432 cur_func = get_base_type(cur_func);
433 if (cur_func == &void_ctype)
434 return 0;
435 if (!get_value(expr, &sval))
436 return 0;
437 if (sval_cmp_val(sval, 0) < 0)
438 return 1;
439 if (cur_func->type == SYM_PTR && sval.value == 0)
440 return 1;
441 return 0;
444 int getting_address(void)
446 struct expression *tmp;
447 int i = 0;
448 int dot_ops = 0;
450 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
451 if (!i++)
452 continue;
453 if (tmp->type == EXPR_PREOP && tmp->op == '(')
454 continue;
455 if (tmp->op == '.' && !dot_ops++)
456 continue;
457 if (tmp->op == '&')
458 return 1;
459 return 0;
460 } END_FOR_EACH_PTR_REVERSE(tmp);
461 return 0;
464 char *get_member_name(struct expression *expr)
466 char buf[256];
467 struct symbol *sym;
469 expr = strip_expr(expr);
470 if (expr->type != EXPR_DEREF)
471 return NULL;
472 sym = get_type(expr->deref);
473 if (!sym || !sym->ident)
474 return NULL;
475 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
476 return alloc_string(buf);
479 int positions_eq(struct position pos1, struct position pos2)
481 if (pos1.line != pos2.line)
482 return 0;
483 if (pos1.pos != pos2.pos)
484 return 0;
485 if (pos1.stream != pos2.stream)
486 return 0;
487 return 1;
490 struct statement *get_current_statement(void)
492 struct statement *prev, *tmp;
494 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
496 if (!prev || !get_macro_name(prev->pos))
497 return prev;
499 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
500 if (positions_eq(tmp->pos, prev->pos))
501 continue;
502 if (prev->pos.line > tmp->pos.line)
503 return prev;
504 return tmp;
505 } END_FOR_EACH_PTR_REVERSE(tmp);
506 return prev;
509 int get_param_num_from_sym(struct symbol *sym)
511 struct symbol *tmp;
512 int i;
514 i = 0;
515 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
516 if (tmp == sym)
517 return i;
518 i++;
519 } END_FOR_EACH_PTR(tmp);
520 return -1;