precedence: cut down a few false positives
[smatch.git] / smatch_helper.c
blob4030b61ac8552e365d985a77703ed9f598b7614d
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
20 #define BOGUS 12345
22 char *alloc_string(const char *str)
24 char *tmp;
26 if (!str)
27 return NULL;
28 tmp = malloc(strlen(str) + 1);
29 strcpy(tmp, str);
30 return tmp;
33 void free_string(char *str)
35 free(str);
38 struct smatch_state *alloc_state_num(int num)
40 struct smatch_state *state;
41 static char buff[256];
43 state = __alloc_smatch_state(0);
44 snprintf(buff, 255, "%d", num);
45 buff[255] = '\0';
46 state->name = alloc_string(buff);
47 state->data = (void *)num;
48 return state;
51 static void append(char *dest, const char *data, int buff_len)
53 strncat(dest, data, buff_len - strlen(dest) - 1);
57 * If you have "foo(a, b, 1);" then use
58 * get_argument_from_call_expr(expr, 0) to return the expression for
59 * a. Yes, it does start counting from 0.
61 struct expression *get_argument_from_call_expr(struct expression_list *args,
62 int num)
64 struct expression *expr;
65 int i = 0;
67 if (!args)
68 return NULL;
70 FOR_EACH_PTR(args, expr) {
71 if (i == num)
72 return expr;
73 i++;
74 } END_FOR_EACH_PTR(expr);
75 return NULL;
78 static struct expression *get_array_expr(struct expression *expr)
80 struct symbol *type;
82 if (expr->type != EXPR_BINOP || expr->op != '+')
83 return NULL;
85 type = get_type(expr->left);
86 if (!type || type->type != SYM_ARRAY)
87 return NULL;
88 return expr->left;
91 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
92 struct expression *expr, int len,
93 int *complicated)
95 struct expression *tmp;
97 switch (expr->type) {
98 case EXPR_DEREF:
99 tmp = expr->deref;
100 if (tmp->op == '*') {
101 tmp = tmp->unop;
103 __get_variable_from_expr(sym_ptr, buf, tmp, len, complicated);
105 tmp = expr->deref;
106 if (tmp->op == '*') {
107 append(buf, "->", len);
108 } else {
109 append(buf, ".", len);
111 append(buf, expr->member->name, len);
113 return;
114 case EXPR_SYMBOL:
115 if (expr->symbol_name)
116 append(buf, expr->symbol_name->name, len);
117 if (sym_ptr) {
118 if (*sym_ptr)
119 *complicated = 1;
120 *sym_ptr = expr->symbol;
122 return;
123 case EXPR_PREOP: {
124 const char *tmp;
126 if (get_block_thing(expr)) {
127 *complicated = 2;
128 return;
131 if (expr->op != '*' || !get_array_expr(expr->unop)) {
132 tmp = show_special(expr->op);
133 append(buf, tmp, len);
135 __get_variable_from_expr(sym_ptr, buf, expr->unop,
136 len, complicated);
138 if (expr->op == '(') {
139 append(buf, ")", len);
142 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
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_BINOP: {
160 const char *tmp;
161 struct expression *array_expr;
163 *complicated = 1;
164 array_expr = get_array_expr(expr);
165 if (array_expr) {
166 __get_variable_from_expr(NULL, buf, array_expr, len, complicated);
167 append(buf, "[", len);
168 } else {
169 append(buf, "(", len);
170 __get_variable_from_expr(NULL, buf, expr->left, len,
171 complicated);
172 tmp = show_special(expr->op);
173 append(buf, tmp, len);
175 __get_variable_from_expr(sym_ptr, buf, expr->right,
176 len, complicated);
177 if (array_expr)
178 append(buf, "]", len);
179 else
180 append(buf, ")", len);
181 return;
183 case EXPR_VALUE: {
184 char tmp[25];
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", size);
225 append(buf, tmp, len);
227 return;
229 default:
230 *complicated = 1;
231 //printf("unknown type = %d\n", expr->type);
232 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 *get_variable_from_expr_complex(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;
266 * get_variable_from_expr_simple() only returns simple variables.
267 * If it's a complicated variable like a->foo instead of just 'a'
268 * then it returns NULL.
271 char *get_variable_from_expr(struct expression *expr,
272 struct symbol **sym_ptr)
274 static char var_name[VAR_LEN];
275 int complicated = 0;
277 if (sym_ptr)
278 *sym_ptr = NULL;
279 var_name[0] = '\0';
281 if (!expr)
282 return NULL;
283 expr = strip_expr(expr);
284 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
285 &complicated);
287 if (complicated) {
288 if (sym_ptr)
289 *sym_ptr = NULL;
290 return NULL;
292 return alloc_string(var_name);
295 int sym_name_is(const char *name, struct expression *expr)
297 if (!expr)
298 return 0;
299 if (expr->type != EXPR_SYMBOL)
300 return 0;
301 if (!strcmp(expr->symbol_name->name, name))
302 return 1;
303 return 0;
306 #define NOTIMPLIED 0
307 #define IMPLIED 1
308 #define FUZZYMAX 2
310 static long long _get_value(struct expression *expr, int *discard, int *undefined, int implied)
312 int dis = 0;
313 long long ret = BOGUS;
315 if (!expr) {
316 *undefined = 1;
317 return BOGUS;
319 if (!discard)
320 discard = &dis;
321 if (*discard) {
322 *undefined = 1;
323 return BOGUS;
326 expr = strip_parens(expr);
328 switch (expr->type){
329 case EXPR_VALUE:
330 ret = expr->value;
331 break;
332 case EXPR_PREOP:
333 if (expr->op == '-') {
334 ret = - _get_value(expr->unop, discard, undefined, implied);
335 } else {
336 *undefined = 1;
337 *discard = 1;
339 break;
340 case EXPR_CAST:
341 case EXPR_FORCE_CAST:
342 case EXPR_IMPLIED_CAST:
344 struct symbol *type = get_base_type(expr->cast_type);
346 ret = _get_value(expr->cast_expression, discard, undefined, implied);
347 switch (type->bit_size) {
348 case 8:
349 if (type->ctype.modifiers & MOD_UNSIGNED)
350 ret = (int)(unsigned char) ret;
351 else
352 ret = (int)(char) ret;
353 break;
354 case 16:
355 if (type->ctype.modifiers & MOD_UNSIGNED)
356 ret = (int)(unsigned short) ret;
357 else
358 ret = (int)(short) ret;
359 break;
360 case 32:
361 if (type->ctype.modifiers & MOD_UNSIGNED)
362 ret = (int)(unsigned int) ret;
363 else
364 ret = (int)(int) ret;
365 break;
367 return ret;
369 case EXPR_BINOP: {
370 int left, right;
372 left = _get_value(expr->left, discard, undefined, implied);
373 right = _get_value(expr->right, discard, undefined, implied);
374 if (expr->op == '*') {
375 ret = left * right;
376 } else if (expr->op == '/') {
377 ret = left / right;
378 } else if (expr->op == '+') {
379 ret = left + right;
380 } else if (expr->op == '-') {
381 ret = left - right;
382 } else if (expr->op == '|') {
383 ret = left | right;
384 } else if (expr->op == '&') {
385 ret = left & right;
386 } else if (expr->op == SPECIAL_RIGHTSHIFT) {
387 ret = left >> right;
388 } else if (expr->op == SPECIAL_LEFTSHIFT) {
389 ret = left << right;
390 } else {
391 *undefined = 1;
392 *discard = 1;
394 break;
396 case EXPR_PTRSIZEOF:
397 case EXPR_SIZEOF:
398 ret = get_expression_value(expr);
399 break;
400 default:
401 if (implied == IMPLIED) {
402 if (!get_implied_single_val(expr, &ret)) {
403 *undefined = 1;
404 *discard = 1;
406 } else if (implied == FUZZYMAX) {
407 if (!get_implied_single_fuzzy_max(expr, &ret)) {
408 *undefined = 1;
409 *discard = 1;
411 } else {
412 *undefined = 1;
413 *discard = 1;
416 if (*discard) {
417 *undefined = 1;
418 return BOGUS;
420 return ret;
423 /* returns 1 if it can get a value literal or else returns 0 */
424 int get_value(struct expression *expr, long long *val)
426 int undefined = 0;
428 *val = _get_value(expr, NULL, &undefined, NOTIMPLIED);
429 if (undefined)
430 return 0;
431 return 1;
434 int get_implied_value(struct expression *expr, long long *val)
436 int undefined = 0;
438 *val = _get_value(expr, NULL, &undefined, IMPLIED);
439 return !undefined;
442 int get_fuzzy_max(struct expression *expr, long long *val)
444 int undefined = 0;
446 *val = _get_value(expr, NULL, &undefined, FUZZYMAX);
447 return !undefined;
450 int is_zero(struct expression *expr)
452 long long val;
454 if (get_value(expr, &val) && val == 0)
455 return 1;
456 return 0;
459 int is_array(struct expression *expr)
461 expr = strip_expr(expr);
462 if (expr->type != EXPR_PREOP || expr->op != '*')
463 return 0;
464 expr = expr->unop;
465 if (expr->op != '+')
466 return 0;
467 return 1;
470 struct expression *get_array_name(struct expression *expr)
472 if (!is_array(expr))
473 return NULL;
474 return strip_expr(expr->unop->left);
477 struct expression *get_array_offset(struct expression *expr)
479 if (!is_array(expr))
480 return NULL;
481 return expr->unop->right;
484 const char *show_state(struct smatch_state *state)
486 if (!state)
487 return NULL;
488 return state->name;
491 struct statement *get_block_thing(struct expression *expr)
493 /* What are those things called? if (({....; ret;})) { ...*/
495 if (expr->type != EXPR_PREOP)
496 return NULL;
497 if (expr->op != '(')
498 return NULL;
499 if (expr->unop->type != EXPR_STATEMENT)
500 return NULL;
501 if (expr->unop->statement->type != STMT_COMPOUND)
502 return NULL;
503 return expr->unop->statement;
506 struct expression *strip_parens(struct expression *expr)
508 if (!expr)
509 return NULL;
511 if (expr->type == EXPR_PREOP) {
512 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
513 expr->unop->statement->type == STMT_COMPOUND)
514 return expr;
515 if (expr->op == '(')
516 return strip_parens(expr->unop);
518 return expr;
521 struct expression *strip_expr(struct expression *expr)
523 if (!expr)
524 return NULL;
526 switch (expr->type) {
527 case EXPR_FORCE_CAST:
528 case EXPR_CAST:
529 return strip_expr(expr->cast_expression);
530 case EXPR_PREOP:
531 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
532 expr->unop->statement->type == STMT_COMPOUND)
533 return expr;
534 if (expr->op == '(')
535 return strip_expr(expr->unop);
537 return expr;
540 static void delete_state_tracker(struct tracker *t)
542 delete_state(t->owner, t->name, t->sym);
543 __free_tracker(t);
546 void scoped_state(int my_id, const char *name, struct symbol *sym)
548 struct tracker *t;
550 t = alloc_tracker(my_id, name, sym);
551 add_scope_hook((scope_hook *)&delete_state_tracker, t);
554 int is_error_return(struct expression *expr)
556 struct symbol *cur_func = cur_func_sym;
557 long long val;
559 if (!expr)
560 return 0;
561 if (cur_func->type != SYM_NODE)
562 return 0;
563 cur_func = get_base_type(cur_func);
564 if (cur_func->type != SYM_FN)
565 return 0;
566 cur_func = get_base_type(cur_func);
567 if (cur_func == &void_ctype)
568 return 0;
569 if (!get_value(expr, &val))
570 return 0;
571 if (val < 0)
572 return 1;
573 if (cur_func->type == SYM_PTR && val == 0)
574 return 1;
575 return 0;
578 int getting_address(void)
580 struct expression *tmp;
581 int i = 0;
582 int dot_ops = 0;
584 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
585 if (!i++)
586 continue;
587 if (tmp->type == EXPR_PREOP && tmp->op == '(')
588 continue;
589 if (tmp->op == '.' && !dot_ops++)
590 continue;
591 if (tmp->op == '&')
592 return 1;
593 return 0;
594 } END_FOR_EACH_PTR_REVERSE(tmp);
595 return 0;