Or vs and: warn about bitwise ANDs that always give zero
[smatch.git] / smatch_helper.c
blob871edf83c95b5bafe61bc714cd15cadfb42ef7eb
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_BINOP: {
160 char tmp[10];
161 struct expression *array_expr;
163 *complicated = 1;
164 array_expr = get_array_expr(expr);
165 if (array_expr) {
166 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated);
167 append(buf, "[", len);
168 } else {
169 __get_variable_from_expr(sym_ptr, buf, expr->left, len,
170 complicated);
171 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
172 append(buf, tmp, len);
174 __get_variable_from_expr(NULL, buf, expr->right,
175 len, complicated);
176 if (array_expr)
177 append(buf, "]", len);
178 return;
180 case EXPR_VALUE: {
181 char tmp[25];
183 *complicated = 1;
184 snprintf(tmp, 25, "%lld", expr->value);
185 append(buf, tmp, len);
186 return;
188 case EXPR_STRING:
189 append(buf, "\"", len);
190 append(buf, expr->string->data, len);
191 append(buf, "\"", len);
192 return;
193 case EXPR_CALL: {
194 struct expression *tmp;
195 int i;
197 *complicated = 1;
198 __get_variable_from_expr(NULL, buf, expr->fn, len,
199 complicated);
200 append(buf, "(", len);
201 i = 0;
202 FOR_EACH_PTR_REVERSE(expr->args, tmp) {
203 if (i++)
204 append(buf, ", ", len);
205 __get_variable_from_expr(NULL, buf, tmp, len,
206 complicated);
207 } END_FOR_EACH_PTR_REVERSE(tmp);
208 append(buf, ")", len);
209 return;
211 case EXPR_CAST:
212 __get_variable_from_expr(sym_ptr, buf,
213 expr->cast_expression, len,
214 complicated);
215 return;
216 case EXPR_SIZEOF: {
217 int size;
218 char tmp[25];
220 if (expr->cast_type && get_base_type(expr->cast_type)) {
221 size = (get_base_type(expr->cast_type))->bit_size;
222 snprintf(tmp, 25, "%d", bits_to_bytes(size));
223 append(buf, tmp, len);
225 return;
227 default:
228 *complicated = 1;
229 //printf("unknown type = %d\n", expr->type);
230 return;
235 * This is returns a stylized "c looking" representation of the
236 * variable name.
238 * It uses the same buffer every time so you have to save the result
239 * yourself if you want to keep it.
243 char *get_variable_from_expr_complex(struct expression *expr, struct symbol **sym_ptr)
245 static char var_name[VAR_LEN];
246 int complicated = 0;
248 if (sym_ptr)
249 *sym_ptr = NULL;
250 var_name[0] = '\0';
252 if (!expr)
253 return NULL;
254 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
255 &complicated);
256 if (complicated < 2)
257 return alloc_string(var_name);
258 else
259 return NULL;
263 * get_variable_from_expr_simple() only returns simple variables.
264 * If it's a complicated variable like a->foo instead of just 'a'
265 * then it returns NULL.
268 char *get_variable_from_expr(struct expression *expr,
269 struct symbol **sym_ptr)
271 static char var_name[VAR_LEN];
272 int complicated = 0;
274 if (sym_ptr)
275 *sym_ptr = NULL;
276 var_name[0] = '\0';
278 if (!expr)
279 return NULL;
280 expr = strip_expr(expr);
281 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
282 &complicated);
284 if (complicated) {
285 if (sym_ptr)
286 *sym_ptr = NULL;
287 return NULL;
289 return alloc_string(var_name);
292 int sym_name_is(const char *name, struct expression *expr)
294 if (!expr)
295 return 0;
296 if (expr->type != EXPR_SYMBOL)
297 return 0;
298 if (!strcmp(expr->symbol_name->name, name))
299 return 1;
300 return 0;
303 int is_zero(struct expression *expr)
305 long long val;
307 if (get_value(expr, &val) && val == 0)
308 return 1;
309 return 0;
312 int is_array(struct expression *expr)
314 expr = strip_expr(expr);
315 if (expr->type != EXPR_PREOP || expr->op != '*')
316 return 0;
317 expr = expr->unop;
318 if (expr->op != '+')
319 return 0;
320 return 1;
323 struct expression *get_array_name(struct expression *expr)
325 if (!is_array(expr))
326 return NULL;
327 return strip_expr(expr->unop->left);
330 struct expression *get_array_offset(struct expression *expr)
332 if (!is_array(expr))
333 return NULL;
334 return expr->unop->right;
337 const char *show_state(struct smatch_state *state)
339 if (!state)
340 return NULL;
341 return state->name;
344 struct statement *get_expression_statement(struct expression *expr)
346 /* What are those things called? if (({....; ret;})) { ...*/
348 if (expr->type != EXPR_PREOP)
349 return NULL;
350 if (expr->op != '(')
351 return NULL;
352 if (expr->unop->type != EXPR_STATEMENT)
353 return NULL;
354 if (expr->unop->statement->type != STMT_COMPOUND)
355 return NULL;
356 return expr->unop->statement;
359 struct expression *strip_parens(struct expression *expr)
361 if (!expr)
362 return NULL;
364 if (expr->type == EXPR_PREOP) {
365 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
366 expr->unop->statement->type == STMT_COMPOUND)
367 return expr;
368 if (expr->op == '(')
369 return strip_parens(expr->unop);
371 return expr;
374 struct expression *strip_expr(struct expression *expr)
376 if (!expr)
377 return NULL;
379 switch (expr->type) {
380 case EXPR_FORCE_CAST:
381 case EXPR_CAST:
382 return strip_expr(expr->cast_expression);
383 case EXPR_PREOP:
384 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
385 expr->unop->statement->type == STMT_COMPOUND)
386 return expr;
387 if (expr->op == '(')
388 return strip_expr(expr->unop);
390 return expr;
393 static void delete_state_tracker(struct tracker *t)
395 delete_state(t->owner, t->name, t->sym);
396 __free_tracker(t);
399 void scoped_state(int my_id, const char *name, struct symbol *sym)
401 struct tracker *t;
403 t = alloc_tracker(my_id, name, sym);
404 add_scope_hook((scope_hook *)&delete_state_tracker, t);
407 int is_error_return(struct expression *expr)
409 struct symbol *cur_func = cur_func_sym;
410 long long val;
412 if (!expr)
413 return 0;
414 if (cur_func->type != SYM_NODE)
415 return 0;
416 cur_func = get_base_type(cur_func);
417 if (cur_func->type != SYM_FN)
418 return 0;
419 cur_func = get_base_type(cur_func);
420 if (cur_func == &void_ctype)
421 return 0;
422 if (!get_value(expr, &val))
423 return 0;
424 if (val < 0)
425 return 1;
426 if (cur_func->type == SYM_PTR && val == 0)
427 return 1;
428 return 0;
431 int getting_address(void)
433 struct expression *tmp;
434 int i = 0;
435 int dot_ops = 0;
437 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
438 if (!i++)
439 continue;
440 if (tmp->type == EXPR_PREOP && tmp->op == '(')
441 continue;
442 if (tmp->op == '.' && !dot_ops++)
443 continue;
444 if (tmp->op == '&')
445 return 1;
446 return 0;
447 } END_FOR_EACH_PTR_REVERSE(tmp);
448 return 0;
451 char *get_member_name(struct expression *expr)
453 char buf[256];
454 struct symbol *sym;
456 if (expr->type != EXPR_DEREF)
457 return NULL;
458 sym = get_type(expr->deref);
459 if (!sym || !sym->ident)
460 return NULL;
461 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
462 return alloc_string(buf);
465 char *get_fnptr_name(struct expression *expr)
467 if (expr->type == EXPR_SYMBOL)
468 return get_variable_from_expr(expr, NULL);
469 return get_member_name(expr);
472 int positions_eq(struct position pos1, struct position pos2)
474 if (pos1.line != pos2.line)
475 return 0;
476 if (pos1.pos != pos2.pos)
477 return 0;
478 if (pos1.stream != pos2.stream)
479 return 0;
480 return 1;