kernel, db: consider x = htonl(10) and x = 10 equivalent.
[smatch.git] / smatch_helper.c
blobb3e495e2d7636b68fe0e165210ed81a728e4bbea
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 void remove_parens(char *str)
39 char *src, *dst;
41 dst = src = str;
42 while (*src != '\0') {
43 if (*src == '(' || *src == ')') {
44 src++;
45 continue;
47 *dst++ = *src++;
49 *dst = *src;
52 struct smatch_state *alloc_state_num(int num)
54 struct smatch_state *state;
55 static char buff[256];
57 state = __alloc_smatch_state(0);
58 snprintf(buff, 255, "%d", num);
59 buff[255] = '\0';
60 state->name = alloc_string(buff);
61 state->data = INT_PTR(num);
62 return state;
65 void append(char *dest, const char *data, int buff_len)
67 strncat(dest, data, buff_len - strlen(dest) - 1);
71 * If you have "foo(a, b, 1);" then use
72 * get_argument_from_call_expr(expr, 0) to return the expression for
73 * a. Yes, it does start counting from 0.
75 struct expression *get_argument_from_call_expr(struct expression_list *args,
76 int num)
78 struct expression *expr;
79 int i = 0;
81 if (!args)
82 return NULL;
84 FOR_EACH_PTR(args, expr) {
85 if (i == num)
86 return expr;
87 i++;
88 } END_FOR_EACH_PTR(expr);
89 return NULL;
92 static struct expression *get_array_expr(struct expression *expr)
94 struct symbol *type;
96 if (expr->type != EXPR_BINOP || expr->op != '+')
97 return NULL;
99 type = get_type(expr->left);
100 if (!type || type->type != SYM_ARRAY)
101 return NULL;
102 return expr->left;
105 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
106 struct expression *expr, int len,
107 int *complicated, int no_parens)
109 struct expression *tmp;
111 switch (expr->type) {
112 case EXPR_DEREF:
113 tmp = expr->deref;
114 if (tmp->op == '*')
115 tmp = tmp->unop;
117 __get_variable_from_expr(sym_ptr, buf, tmp, len, complicated, no_parens);
119 tmp = expr->deref;
120 if (tmp->op == '*')
121 append(buf, "->", len);
122 else
123 append(buf, ".", len);
125 if (expr->member && expr->member->name)
126 append(buf, expr->member->name, len);
127 else
128 append(buf, "unknown_member", len);
130 return;
131 case EXPR_SYMBOL:
132 if (expr->symbol_name)
133 append(buf, expr->symbol_name->name, len);
134 if (sym_ptr) {
135 if (*sym_ptr)
136 *complicated = 1;
137 *sym_ptr = expr->symbol;
139 return;
140 case EXPR_PREOP: {
141 const char *tmp;
143 if (get_expression_statement(expr)) {
144 *complicated = 2;
145 return;
148 if (expr->op == '(') {
149 if (!no_parens)
150 append(buf, "(", len);
151 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
152 tmp = show_special(expr->op);
153 append(buf, tmp, len);
155 __get_variable_from_expr(sym_ptr, buf, expr->unop,
156 len, complicated, no_parens);
158 if (expr->op == '(' && !no_parens)
159 append(buf, ")", len);
161 if (expr->op == SPECIAL_DECREMENT ||
162 expr->op == SPECIAL_INCREMENT ||
163 expr->op == '&')
164 *complicated = 1;
166 return;
168 case EXPR_POSTOP: {
169 const char *tmp;
171 __get_variable_from_expr(sym_ptr, buf, expr->unop,
172 len, complicated, no_parens);
173 tmp = show_special(expr->op);
174 append(buf, tmp, len);
176 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
177 *complicated = 1;
178 return;
180 case EXPR_ASSIGNMENT:
181 case EXPR_COMPARE:
182 case EXPR_LOGICAL:
183 case EXPR_BINOP: {
184 char tmp[10];
185 struct expression *array_expr;
187 *complicated = 1;
188 array_expr = get_array_expr(expr);
189 if (array_expr) {
190 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
191 append(buf, "[", len);
192 } else {
193 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
194 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
195 append(buf, tmp, len);
197 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
198 if (array_expr)
199 append(buf, "]", len);
200 return;
202 case EXPR_VALUE: {
203 char tmp[25];
205 *complicated = 1;
206 snprintf(tmp, 25, "%lld", expr->value);
207 append(buf, tmp, len);
208 return;
210 case EXPR_STRING:
211 append(buf, "\"", len);
212 append(buf, expr->string->data, len);
213 append(buf, "\"", len);
214 return;
215 case EXPR_CALL: {
216 struct expression *tmp;
217 int i;
219 *complicated = 1;
220 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
221 append(buf, "(", len);
222 i = 0;
223 FOR_EACH_PTR(expr->args, tmp) {
224 if (i++)
225 append(buf, ", ", len);
226 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
227 } END_FOR_EACH_PTR(tmp);
228 append(buf, ")", len);
229 return;
231 case EXPR_CAST:
232 __get_variable_from_expr(sym_ptr, buf,
233 expr->cast_expression, len,
234 complicated, no_parens);
235 return;
236 case EXPR_SIZEOF: {
237 int size;
238 char tmp[25];
240 if (expr->cast_type && get_base_type(expr->cast_type)) {
241 size = (get_base_type(expr->cast_type))->bit_size;
242 snprintf(tmp, 25, "%d", bits_to_bytes(size));
243 append(buf, tmp, len);
245 return;
247 case EXPR_IDENTIFIER:
248 *complicated = 1;
249 if (expr->expr_ident && expr->expr_ident->name)
250 append(buf, expr->expr_ident->name, len);
251 return;
252 default:
253 *complicated = 1;
254 //printf("unknown type = %d\n", expr->type);
255 return;
260 * This is returns a stylized "c looking" representation of the
261 * variable name.
263 * It uses the same buffer every time so you have to save the result
264 * yourself if you want to keep it.
268 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
270 static char var_name[VAR_LEN];
271 int complicated = 0;
273 if (sym_ptr)
274 *sym_ptr = NULL;
275 var_name[0] = '\0';
277 if (!expr)
278 return NULL;
279 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
280 &complicated, 0);
281 if (complicated < 2)
282 return alloc_string(var_name);
283 else
284 return NULL;
287 char *expr_to_str(struct expression *expr)
289 return expr_to_str_sym(expr, NULL);
293 * get_variable_from_expr_simple() only returns simple variables.
294 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
295 * then it returns NULL.
297 char *expr_to_var_sym(struct expression *expr,
298 struct symbol **sym_ptr)
300 static char var_name[VAR_LEN];
301 int complicated = 0;
303 if (sym_ptr)
304 *sym_ptr = NULL;
305 var_name[0] = '\0';
307 if (!expr)
308 return NULL;
309 expr = strip_expr(expr);
310 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
311 &complicated, 1);
313 if (complicated) {
314 if (sym_ptr)
315 *sym_ptr = NULL;
316 return NULL;
318 return alloc_string(var_name);
321 char *expr_to_var(struct expression *expr)
323 return expr_to_var_sym(expr, NULL);
326 int sym_name_is(const char *name, struct expression *expr)
328 if (!expr)
329 return 0;
330 if (expr->type != EXPR_SYMBOL)
331 return 0;
332 if (!strcmp(expr->symbol_name->name, name))
333 return 1;
334 return 0;
337 int is_zero(struct expression *expr)
339 sval_t sval;
341 if (get_value(expr, &sval) && sval.value == 0)
342 return 1;
343 return 0;
346 int is_array(struct expression *expr)
348 expr = strip_expr(expr);
349 if (!expr || expr->type != EXPR_PREOP || expr->op != '*')
350 return 0;
351 expr = expr->unop;
352 if (expr->op != '+')
353 return 0;
354 return 1;
357 struct expression *get_array_name(struct expression *expr)
359 if (!is_array(expr))
360 return NULL;
361 return strip_expr(expr->unop->left);
364 struct expression *get_array_offset(struct expression *expr)
366 if (!is_array(expr))
367 return NULL;
368 return expr->unop->right;
371 const char *show_state(struct smatch_state *state)
373 if (!state)
374 return NULL;
375 return state->name;
378 struct statement *get_expression_statement(struct expression *expr)
380 /* What are those things called? if (({....; ret;})) { ...*/
382 if (expr->type != EXPR_PREOP)
383 return NULL;
384 if (expr->op != '(')
385 return NULL;
386 if (expr->unop->type != EXPR_STATEMENT)
387 return NULL;
388 if (expr->unop->statement->type != STMT_COMPOUND)
389 return NULL;
390 return expr->unop->statement;
393 struct expression *strip_parens(struct expression *expr)
395 if (!expr)
396 return NULL;
398 if (expr->type == EXPR_PREOP) {
399 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
400 expr->unop->statement->type == STMT_COMPOUND)
401 return expr;
402 if (expr->op == '(')
403 return strip_parens(expr->unop);
405 return expr;
408 struct expression *strip_expr(struct expression *expr)
410 if (!expr)
411 return NULL;
413 switch (expr->type) {
414 case EXPR_FORCE_CAST:
415 case EXPR_CAST:
416 return strip_expr(expr->cast_expression);
417 case EXPR_PREOP:
418 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
419 expr->unop->statement->type == STMT_COMPOUND)
420 return expr;
421 if (expr->op == '(')
422 return strip_expr(expr->unop);
424 return expr;
427 static void delete_state_tracker(struct tracker *t)
429 delete_state(t->owner, t->name, t->sym);
430 __free_tracker(t);
433 void scoped_state(int my_id, const char *name, struct symbol *sym)
435 struct tracker *t;
437 t = alloc_tracker(my_id, name, sym);
438 add_scope_hook((scope_hook *)&delete_state_tracker, t);
441 int is_error_return(struct expression *expr)
443 struct symbol *cur_func = cur_func_sym;
444 sval_t sval;
446 if (!expr)
447 return 0;
448 if (cur_func->type != SYM_NODE)
449 return 0;
450 cur_func = get_base_type(cur_func);
451 if (cur_func->type != SYM_FN)
452 return 0;
453 cur_func = get_base_type(cur_func);
454 if (cur_func == &void_ctype)
455 return 0;
456 if (!get_implied_value(expr, &sval))
457 return 0;
458 if (sval.value < 0)
459 return 1;
460 if (cur_func->type == SYM_PTR && sval.value == 0)
461 return 1;
462 return 0;
465 int getting_address(void)
467 struct expression *tmp;
468 int i = 0;
469 int dot_ops = 0;
471 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
472 if (!i++)
473 continue;
474 if (tmp->type == EXPR_PREOP && tmp->op == '(')
475 continue;
476 if (tmp->op == '.' && !dot_ops++)
477 continue;
478 if (tmp->op == '&')
479 return 1;
480 return 0;
481 } END_FOR_EACH_PTR_REVERSE(tmp);
482 return 0;
485 char *get_member_name(struct expression *expr)
487 char buf[256];
488 struct symbol *sym;
490 expr = strip_expr(expr);
491 if (expr->type != EXPR_DEREF)
492 return NULL;
493 sym = get_type(expr->deref);
494 if (!sym)
495 return NULL;
496 /* FIXME: HACK! Sparse isn't storing the names of unions... Just take last member */
497 if (sym->type == SYM_UNION) {
498 struct symbol *tmp;
500 FOR_EACH_PTR_REVERSE(sym->symbol_list, tmp) {
501 if (tmp->ident) {
502 const char *member_name;
504 if (expr->member)
505 member_name = expr->member->name;
506 else
507 member_name = "unknown_member";
508 snprintf(buf, sizeof(buf), "(union hack %s)->%s", tmp->ident->name, member_name);
509 return alloc_string(buf);
511 } END_FOR_EACH_PTR_REVERSE(tmp);
512 return NULL;
514 if (!sym->ident)
515 return NULL;
516 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
517 return alloc_string(buf);
520 int positions_eq(struct position pos1, struct position pos2)
522 if (pos1.line != pos2.line)
523 return 0;
524 if (pos1.pos != pos2.pos)
525 return 0;
526 if (pos1.stream != pos2.stream)
527 return 0;
528 return 1;
531 struct statement *get_current_statement(void)
533 struct statement *prev, *tmp;
535 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
537 if (!prev || !get_macro_name(prev->pos))
538 return prev;
540 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
541 if (positions_eq(tmp->pos, prev->pos))
542 continue;
543 if (prev->pos.line > tmp->pos.line)
544 return prev;
545 return tmp;
546 } END_FOR_EACH_PTR_REVERSE(tmp);
547 return prev;
550 int get_param_num_from_sym(struct symbol *sym)
552 struct symbol *tmp;
553 int i;
555 if (!cur_func_sym)
556 return -1;
558 i = 0;
559 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
560 if (tmp == sym)
561 return i;
562 i++;
563 } END_FOR_EACH_PTR(tmp);
564 return -1;
567 int ms_since(struct timeval *start)
569 struct timeval end;
570 double diff;
572 gettimeofday(&end, NULL);
573 diff = (end.tv_sec - start->tv_sec) * 1000.0;
574 diff += (end.tv_usec - start->tv_usec) / 1000.0;
575 return (int)diff;