wine: add more functions that don't return
[smatch.git] / smatch_helper.c
blob907a9e729f829218a44920d6c6d6c3cea4006d17
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 static void append(char *dest, const char *data, int buff_len)
40 strncat(dest, data, buff_len - strlen(dest) - 1);
44 * If you have "foo(a, b, 1);" then use
45 * get_argument_from_call_expr(expr, 0) to return the expression for
46 * a. Yes, it does start counting from 0.
48 struct expression *get_argument_from_call_expr(struct expression_list *args,
49 int num)
51 struct expression *expr;
52 int i = 0;
54 if (!args)
55 return NULL;
57 FOR_EACH_PTR(args, expr) {
58 if (i == num)
59 return expr;
60 i++;
61 } END_FOR_EACH_PTR(expr);
62 return NULL;
65 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
66 struct expression *expr, int len,
67 int *complicated)
69 struct expression *tmp;
71 switch(expr->type) {
72 case EXPR_DEREF:
73 tmp = expr->deref;
74 if (tmp->op == '*') {
75 tmp = tmp->unop;
77 __get_variable_from_expr(sym_ptr, buf, tmp, len, complicated);
79 tmp = expr->deref;
80 if (tmp->op == '*') {
81 append(buf, "->", len);
82 } else {
83 append(buf, ".", len);
85 append(buf, expr->member->name, len);
87 return;
88 case EXPR_SYMBOL:
89 if (expr->symbol_name)
90 append(buf, expr->symbol_name->name, len);
91 if (sym_ptr) {
92 if (*sym_ptr)
93 *complicated = 1;
94 *sym_ptr = expr->symbol;
96 return;
97 case EXPR_PREOP: {
98 const char *tmp;
100 if (get_block_thing(expr)) {
101 *complicated = 2;
102 return;
105 tmp = show_special(expr->op);
106 append(buf, tmp, len);
107 __get_variable_from_expr(sym_ptr, buf, expr->unop,
108 len, complicated);
110 if (expr->op == '(') {
111 append(buf, ")", len);
114 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
115 *complicated = 1;
117 return;
119 case EXPR_POSTOP: {
120 const char *tmp;
122 __get_variable_from_expr(sym_ptr, buf, expr->unop,
123 len, complicated);
124 tmp = show_special(expr->op);
125 append(buf, tmp, len);
127 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
128 *complicated = 1;
129 return;
131 case EXPR_BINOP: {
132 const char *tmp;
134 *complicated = 1;
135 append(buf, "(", len);
136 __get_variable_from_expr(NULL, buf, expr->left, len,
137 complicated);
138 tmp = show_special(expr->op);
139 append(buf, tmp, len);
140 __get_variable_from_expr(sym_ptr, buf, expr->right,
141 len, complicated);
142 append(buf, ")", len);
143 return;
145 case EXPR_VALUE: {
146 char tmp[25];
148 snprintf(tmp, 25, "%lld", expr->value);
149 append(buf, tmp, len);
150 return;
152 case EXPR_STRING:
153 append(buf, expr->string->data, len);
154 return;
155 case EXPR_CALL: {
156 struct expression *tmp;
157 int i;
159 *complicated = 1;
160 __get_variable_from_expr(NULL, buf, expr->fn, len,
161 complicated);
162 append(buf, "(", len);
163 i = 0;
164 FOR_EACH_PTR_REVERSE(expr->args, tmp) {
165 if (i++)
166 append(buf, ", ", len);
167 __get_variable_from_expr(NULL, buf, tmp, len,
168 complicated);
169 } END_FOR_EACH_PTR_REVERSE(tmp);
170 append(buf, ")", len);
171 return;
173 case EXPR_CAST:
174 __get_variable_from_expr(sym_ptr, buf,
175 expr->cast_expression, len,
176 complicated);
177 return;
178 case EXPR_SIZEOF: {
179 int size;
180 char tmp[25];
182 if (expr->cast_type && get_base_type(expr->cast_type)) {
183 size = (get_base_type(expr->cast_type))->bit_size;
184 snprintf(tmp, 25, "%d", size);
185 append(buf, tmp, len);
187 return;
189 default:
190 *complicated = 1;
191 //printf("unknown type = %d\n", expr->type);
192 return;
198 * This is returns a stylized "c looking" representation of the
199 * variable name.
201 * It uses the same buffer every time so you have to save the result
202 * yourself if you want to keep it.
206 char *get_variable_from_expr_complex(struct expression *expr, struct symbol **sym_ptr)
208 static char var_name[VAR_LEN];
209 int complicated = 0;
211 if (sym_ptr)
212 *sym_ptr = NULL;
213 var_name[0] = '\0';
215 if (!expr)
216 return NULL;
217 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
218 &complicated);
219 if (complicated < 2)
220 return alloc_string(var_name);
221 else
222 return NULL;
226 * get_variable_from_expr_simple() only returns simple variables.
227 * If it's a complicated variable like a->foo instead of just 'a'
228 * then it returns NULL.
231 char *get_variable_from_expr(struct expression *expr,
232 struct symbol **sym_ptr)
234 static char var_name[VAR_LEN];
235 int complicated = 0;
237 if (sym_ptr)
238 *sym_ptr = NULL;
239 var_name[0] = '\0';
241 if (!expr)
242 return NULL;
243 expr = strip_expr(expr);
244 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
245 &complicated);
247 if (complicated) {
248 if (sym_ptr)
249 *sym_ptr = NULL;
250 return NULL;
252 return alloc_string(var_name);
255 struct symbol *get_ptr_type_ptr(struct symbol *sym)
257 if (!sym) {
258 return NULL;
261 if (sym->type != SYM_NODE)
262 return NULL;
263 sym = get_base_type(sym);
264 if (sym->type != SYM_PTR)
265 return NULL;
266 sym = get_base_type(sym);
267 return sym;
270 static struct symbol *get_struct_sym(struct expression *expr)
272 struct symbol *base_type;
273 struct symbol *parent_struct;
274 struct symbol *tmp;
276 if (expr->type != EXPR_PREOP)
277 return NULL;
279 expr = expr->unop;
280 if (expr->type == EXPR_DEREF) {
281 parent_struct = get_struct_sym(expr->deref);
282 if (!parent_struct)
283 return NULL;
284 tmp = NULL;
285 FOR_EACH_PTR(parent_struct->symbol_list, tmp) {
286 if (tmp->ident == expr->member)
287 break;
288 } END_FOR_EACH_PTR(tmp);
289 if (!tmp || tmp->ident != expr->member)
290 return NULL;
291 base_type = get_base_type(tmp);
292 } else if (expr->type == EXPR_SYMBOL) {
293 base_type = get_base_type(expr->symbol);
294 } else {
295 return NULL;
297 if (base_type->type != SYM_PTR)
298 return NULL;
299 base_type = get_base_type(base_type);
300 if (base_type->type != SYM_STRUCT && base_type->type != SYM_UNION)
301 return NULL;
302 return base_type;
305 struct symbol *get_deref_type(struct expression *expr)
307 struct ident *member = expr->member;
308 struct symbol *struct_sym;
309 struct symbol *tmp;
311 struct_sym = get_struct_sym(expr->deref);
312 if (!struct_sym || (struct_sym->type != SYM_STRUCT
313 && struct_sym->type != SYM_UNION))
314 return NULL;
315 FOR_EACH_PTR(struct_sym->symbol_list, tmp) {
316 if (tmp->ident == member)
317 return get_ptr_type_ptr(tmp);
318 } END_FOR_EACH_PTR(tmp);
319 return NULL;
322 struct symbol *get_ptr_type(struct expression *expr)
324 struct symbol *ptr_type = NULL;
326 if (!expr)
327 return NULL;
328 if (expr->type == EXPR_DEREF)
329 ptr_type = get_deref_type(expr);
330 if (expr->type == EXPR_SYMBOL)
331 ptr_type = get_ptr_type_ptr(expr->symbol);
332 return ptr_type;
335 int sym_name_is(const char *name, struct expression *expr)
337 if (!expr)
338 return 0;
339 if (expr->type != EXPR_SYMBOL)
340 return 0;
341 if (!strcmp(expr->symbol_name->name, name))
342 return 1;
343 return 0;
346 #define NOTIMPLIED 0
347 #define IMPLIED 1
349 static int _get_value(struct expression *expr, int *discard, int *undefined, int implied)
351 int dis = 0;
352 long long ret = BOGUS;
354 if (!expr) {
355 *undefined = 1;
356 return BOGUS;
358 if (!discard)
359 discard = &dis;
360 if (*discard) {
361 *undefined = 1;
362 return BOGUS;
365 expr = strip_expr(expr);
367 switch (expr->type){
368 case EXPR_VALUE:
369 ret = expr->value;
370 break;
371 case EXPR_PREOP:
372 if (expr->op == '-') {
373 ret = - _get_value(expr->unop, discard, undefined, implied);
374 } else {
375 *undefined = 1;
376 *discard = 1;
378 break;
379 case EXPR_BINOP: {
380 int left, right;
382 left = _get_value(expr->left, discard, undefined, implied);
383 right = _get_value(expr->right, discard, undefined, implied);
384 if (expr->op == '*') {
385 ret = left * right;
386 } else if (expr->op == '/') {
387 ret = left / right;
388 } else if (expr->op == '+') {
389 ret = left + right;
390 } else if (expr->op == '-') {
391 ret = left - right;
392 } else if (expr->op == '|') {
393 ret = left | right;
394 } else if (expr->op == '&') {
395 ret = left & right;
396 } else if (expr->op == SPECIAL_RIGHTSHIFT) {
397 ret = left >> right;
398 } else if (expr->op == SPECIAL_LEFTSHIFT) {
399 ret = left << right;
400 } else {
401 *undefined = 1;
402 *discard = 1;
404 break;
406 case EXPR_PTRSIZEOF:
407 case EXPR_SIZEOF:
408 ret = get_expression_value(expr);
409 break;
410 default:
411 if (implied) {
412 if (!get_implied_single_val(expr, &ret)) {
413 *undefined = 1;
414 *discard = 1;
416 } else {
417 *undefined = 1;
418 *discard = 1;
421 if (*discard) {
422 *undefined = 1;
423 return BOGUS;
425 return ret;
428 /* returns 1 if it can get a value literal or else returns 0 */
429 int get_value(struct expression *expr, long long *val)
431 int undefined = 0;
433 *val = _get_value(expr, NULL, &undefined, NOTIMPLIED);
434 if (undefined)
435 return 0;
436 return 1;
439 int get_implied_value(struct expression *expr, long long *val)
441 int undefined = 0;
443 *val = _get_value(expr, NULL, &undefined, IMPLIED);
444 return !undefined;
447 int is_zero(struct expression *expr)
449 long long val;
451 if (get_value(expr, &val) && val == 0)
452 return 1;
453 return 0;
456 int is_array(struct expression *expr)
458 expr = strip_expr(expr);
459 if (expr->type != EXPR_PREOP || expr->op != '*')
460 return 0;
461 expr = expr->unop;
462 if (expr->op != '+')
463 return 0;
464 return 1;
467 struct expression *get_array_name(struct expression *expr)
469 if (!is_array(expr))
470 return NULL;
471 return expr->unop->left;
474 struct expression *get_array_offset(struct expression *expr)
476 if (!is_array(expr))
477 return NULL;
478 return expr->unop->right;
481 const char *show_state(struct smatch_state *state)
483 if (!state)
484 return NULL;
485 return state->name;
488 struct statement *get_block_thing(struct expression *expr)
490 /* What are those things called? if (({....; ret;})) { ...*/
492 if (expr->type != EXPR_PREOP)
493 return NULL;
494 if (expr->op != '(')
495 return NULL;
496 if (expr->unop->type != EXPR_STATEMENT)
497 return NULL;
498 if (expr->unop->statement->type != STMT_COMPOUND)
499 return NULL;
500 return expr->unop->statement;
503 struct expression *strip_expr(struct expression *expr)
505 if (!expr)
506 return NULL;
508 switch(expr->type) {
509 case EXPR_CAST:
510 return strip_expr(expr->cast_expression);
511 case 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_expr(expr->unop);
518 return expr;
521 static void delete_state_tracker(struct tracker *t)
523 delete_state(t->owner, t->name, t->sym);
524 __free_tracker(t);
527 void scoped_state(int my_id, const char *name, struct symbol *sym)
529 struct tracker *t;
531 t = alloc_tracker(my_id, name, sym);
532 add_scope_hook((scope_hook *)&delete_state_tracker, t);
535 int is_error_return(struct expression *expr)
537 struct symbol *cur_func = cur_func_sym;
538 long long val;
540 if (!expr)
541 return 0;
542 if (cur_func->type != SYM_NODE)
543 return 0;
544 cur_func = get_base_type(cur_func);
545 if (cur_func->type != SYM_FN)
546 return 0;
547 cur_func = get_base_type(cur_func);
548 if (cur_func == &void_ctype)
549 return 0;
550 if (!get_value(expr, &val))
551 return 0;
552 if (val < 0)
553 return 1;
554 if (cur_func->type == SYM_PTR && val == 0)
555 return 1;
556 return 0;