check_kernel_printf.c: handle new definition of KERN_CONT
[smatch.git] / smatch_helper.c
bloba02d6ed3f4eefab879afd6a277f04bea634f011b
1 /*
2 * Copyright (C) 2006 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * Miscellaneous helper functions.
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "allocate.h"
25 #include "smatch.h"
26 #include "smatch_extra.h"
27 #include "smatch_slist.h"
29 #define VAR_LEN 512
31 char *alloc_string(const char *str)
33 char *tmp;
35 if (!str)
36 return NULL;
37 tmp = malloc(strlen(str) + 1);
38 strcpy(tmp, str);
39 return tmp;
42 void free_string(char *str)
44 free(str);
47 void remove_parens(char *str)
49 char *src, *dst;
51 dst = src = str;
52 while (*src != '\0') {
53 if (*src == '(' || *src == ')') {
54 src++;
55 continue;
57 *dst++ = *src++;
59 *dst = *src;
62 struct smatch_state *alloc_state_num(int num)
64 struct smatch_state *state;
65 static char buff[256];
67 state = __alloc_smatch_state(0);
68 snprintf(buff, 255, "%d", num);
69 buff[255] = '\0';
70 state->name = alloc_string(buff);
71 state->data = INT_PTR(num);
72 return state;
75 struct smatch_state *alloc_state_str(const char *name)
77 struct smatch_state *state;
79 state = __alloc_smatch_state(0);
80 state->name = alloc_string(name);
81 return state;
84 struct smatch_state *alloc_state_expr(struct expression *expr)
86 struct smatch_state *state;
87 char *name;
89 state = __alloc_smatch_state(0);
90 expr = strip_expr(expr);
91 name = expr_to_str(expr);
92 state->name = alloc_sname(name);
93 free_string(name);
94 state->data = expr;
95 return state;
98 void append(char *dest, const char *data, int buff_len)
100 strncat(dest, data, buff_len - strlen(dest) - 1);
104 * If you have "foo(a, b, 1);" then use
105 * get_argument_from_call_expr(expr, 0) to return the expression for
106 * a. Yes, it does start counting from 0.
108 struct expression *get_argument_from_call_expr(struct expression_list *args,
109 int num)
111 struct expression *expr;
112 int i = 0;
114 if (!args)
115 return NULL;
117 FOR_EACH_PTR(args, expr) {
118 if (i == num)
119 return expr;
120 i++;
121 } END_FOR_EACH_PTR(expr);
122 return NULL;
125 static struct expression *get_array_expr(struct expression *expr)
127 struct symbol *type;
129 if (expr->type != EXPR_BINOP || expr->op != '+')
130 return NULL;
132 type = get_type(expr->left);
133 if (!type || type->type != SYM_ARRAY)
134 return NULL;
135 return expr->left;
138 static void __get_variable_from_expr(struct symbol **sym_ptr, char *buf,
139 struct expression *expr, int len,
140 int *complicated, int no_parens)
142 switch (expr->type) {
143 case EXPR_DEREF: {
144 struct expression *deref;
145 int op;
147 deref = expr->deref;
148 op = deref->op;
149 if (op == '*') {
150 struct expression *unop = strip_expr(deref->unop);
152 if (unop->type == EXPR_PREOP && unop->op == '&') {
153 deref = unop->unop;
154 op = '.';
155 } else {
156 deref = deref->unop;
157 if (!is_pointer(deref))
158 op = '.';
162 __get_variable_from_expr(sym_ptr, buf, deref, len, complicated, no_parens);
164 if (op == '*')
165 append(buf, "->", len);
166 else
167 append(buf, ".", len);
169 if (expr->member)
170 append(buf, expr->member->name, len);
171 else
172 append(buf, "unknown_member", len);
174 return;
176 case EXPR_SYMBOL:
177 if (expr->symbol_name)
178 append(buf, expr->symbol_name->name, len);
179 if (sym_ptr) {
180 if (*sym_ptr)
181 *complicated = 1;
182 *sym_ptr = expr->symbol;
184 return;
185 case EXPR_PREOP: {
186 const char *tmp;
188 if (get_expression_statement(expr)) {
189 *complicated = 2;
190 return;
193 if (expr->op == '(') {
194 if (!no_parens)
195 append(buf, "(", len);
196 } else if (expr->op != '*' || !get_array_expr(expr->unop)) {
197 tmp = show_special(expr->op);
198 append(buf, tmp, len);
200 __get_variable_from_expr(sym_ptr, buf, expr->unop,
201 len, complicated, no_parens);
203 if (expr->op == '(' && !no_parens)
204 append(buf, ")", len);
206 if (expr->op == SPECIAL_DECREMENT ||
207 expr->op == SPECIAL_INCREMENT)
208 *complicated = 1;
210 return;
212 case EXPR_POSTOP: {
213 const char *tmp;
215 __get_variable_from_expr(sym_ptr, buf, expr->unop,
216 len, complicated, no_parens);
217 tmp = show_special(expr->op);
218 append(buf, tmp, len);
220 if (expr->op == SPECIAL_DECREMENT || expr->op == SPECIAL_INCREMENT)
221 *complicated = 1;
222 return;
224 case EXPR_ASSIGNMENT:
225 case EXPR_COMPARE:
226 case EXPR_LOGICAL:
227 case EXPR_BINOP: {
228 char tmp[10];
229 struct expression *array_expr;
231 *complicated = 1;
232 array_expr = get_array_expr(expr);
233 if (array_expr) {
234 __get_variable_from_expr(sym_ptr, buf, array_expr, len, complicated, no_parens);
235 append(buf, "[", len);
236 } else {
237 __get_variable_from_expr(sym_ptr, buf, expr->left, len, complicated, no_parens);
238 snprintf(tmp, sizeof(tmp), " %s ", show_special(expr->op));
239 append(buf, tmp, len);
241 __get_variable_from_expr(NULL, buf, expr->right, len, complicated, no_parens);
242 if (array_expr)
243 append(buf, "]", len);
244 return;
246 case EXPR_VALUE: {
247 char tmp[25];
249 *complicated = 1;
250 snprintf(tmp, 25, "%lld", expr->value);
251 append(buf, tmp, len);
252 return;
254 case EXPR_STRING:
255 append(buf, "\"", len);
256 if (expr->string)
257 append(buf, expr->string->data, len);
258 append(buf, "\"", len);
259 return;
260 case EXPR_CALL: {
261 struct expression *tmp;
262 int i;
264 *complicated = 1;
265 __get_variable_from_expr(NULL, buf, expr->fn, len, complicated, no_parens);
266 append(buf, "(", len);
267 i = 0;
268 FOR_EACH_PTR(expr->args, tmp) {
269 if (i++)
270 append(buf, ", ", len);
271 __get_variable_from_expr(NULL, buf, tmp, len, complicated, no_parens);
272 } END_FOR_EACH_PTR(tmp);
273 append(buf, ")", len);
274 return;
276 case EXPR_CAST:
277 case EXPR_FORCE_CAST:
278 __get_variable_from_expr(sym_ptr, buf,
279 expr->cast_expression, len,
280 complicated, no_parens);
281 return;
282 case EXPR_SIZEOF: {
283 int size;
284 char tmp[25];
286 if (expr->cast_type && get_base_type(expr->cast_type)) {
287 size = type_bytes(get_base_type(expr->cast_type));
288 snprintf(tmp, 25, "%d", size);
289 append(buf, tmp, len);
291 return;
293 case EXPR_IDENTIFIER:
294 *complicated = 1;
295 if (expr->expr_ident)
296 append(buf, expr->expr_ident->name, len);
297 return;
298 default:
299 *complicated = 1;
300 //printf("unknown type = %d\n", expr->type);
301 return;
306 * This is returns a stylized "c looking" representation of the
307 * variable name.
309 * It uses the same buffer every time so you have to save the result
310 * yourself if you want to keep it.
314 char *expr_to_str_sym(struct expression *expr, struct symbol **sym_ptr)
316 static char var_name[VAR_LEN];
317 int complicated = 0;
319 if (sym_ptr)
320 *sym_ptr = NULL;
321 var_name[0] = '\0';
323 if (!expr)
324 return NULL;
325 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
326 &complicated, 0);
327 if (complicated < 2)
328 return alloc_string(var_name);
329 else
330 return NULL;
333 char *expr_to_str(struct expression *expr)
335 return expr_to_str_sym(expr, NULL);
339 * get_variable_from_expr_simple() only returns simple variables.
340 * If it's a complicated variable like a->foo[x] instead of just 'a->foo'
341 * then it returns NULL.
343 char *expr_to_var_sym(struct expression *expr,
344 struct symbol **sym_ptr)
346 static char var_name[VAR_LEN];
347 int complicated = 0;
349 if (sym_ptr)
350 *sym_ptr = NULL;
351 var_name[0] = '\0';
353 if (!expr)
354 return NULL;
355 expr = strip_expr(expr);
356 __get_variable_from_expr(sym_ptr, var_name, expr, sizeof(var_name),
357 &complicated, 1);
359 if (complicated) {
360 if (sym_ptr)
361 *sym_ptr = NULL;
362 return NULL;
364 return alloc_string(var_name);
367 char *expr_to_var(struct expression *expr)
369 return expr_to_var_sym(expr, NULL);
372 struct symbol *expr_to_sym(struct expression *expr)
374 struct symbol *sym;
375 char *name;
377 name = expr_to_var_sym(expr, &sym);
378 free_string(name);
379 return sym;
382 int get_complication_score(struct expression *expr)
384 int score = 0;
386 expr = strip_expr(expr);
389 * Don't forget to keep get_complication_score() and store_all_links()
390 * in sync.
394 switch (expr->type) {
395 case EXPR_CALL:
396 return 999;
397 case EXPR_COMPARE:
398 case EXPR_BINOP:
399 score += get_complication_score(expr->left);
400 score += get_complication_score(expr->right);
401 return score;
402 case EXPR_SYMBOL:
403 if (is_local_variable(expr))
404 return 1;
405 return 999;
406 case EXPR_PREOP:
407 if (expr->op == '*')
408 return score + get_complication_score(expr->unop);
409 return 999;
410 case EXPR_DEREF:
411 return score + get_complication_score(expr->deref);
412 case EXPR_VALUE:
413 return 0;
414 default:
415 return 999;
419 char *expr_to_chunk_helper(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
421 char *name;
422 struct symbol *tmp;
423 int score;
425 if (vsl)
426 *vsl = NULL;
427 if (sym)
428 *sym = NULL;
430 expr = strip_parens(expr);
431 if (!expr)
432 return NULL;
434 name = expr_to_var_sym(expr, &tmp);
435 if (name && tmp) {
436 if (sym)
437 *sym = tmp;
438 if (vsl)
439 *vsl = expr_to_vsl(expr);
440 return name;
442 free_string(name);
444 score = get_complication_score(expr);
445 if (score <= 0 || score > 2)
446 return NULL;
448 if (vsl) {
449 *vsl = expr_to_vsl(expr);
450 if (!*vsl)
451 return NULL;
454 return expr_to_str(expr);
457 char *expr_to_known_chunk_sym(struct expression *expr, struct symbol **sym)
459 return expr_to_chunk_helper(expr, sym, NULL);
462 char *expr_to_chunk_sym_vsl(struct expression *expr, struct symbol **sym, struct var_sym_list **vsl)
464 return expr_to_chunk_helper(expr, sym, vsl);
467 int sym_name_is(const char *name, struct expression *expr)
469 if (!expr)
470 return 0;
471 if (expr->type != EXPR_SYMBOL)
472 return 0;
473 if (!strcmp(expr->symbol_name->name, name))
474 return 1;
475 return 0;
478 int is_zero(struct expression *expr)
480 sval_t sval;
482 if (get_value(expr, &sval) && sval.value == 0)
483 return 1;
484 return 0;
487 int is_array(struct expression *expr)
489 struct symbol *type;
491 expr = strip_expr(expr);
492 if (!expr)
493 return 0;
495 if (expr->type == EXPR_PREOP && expr->op == '*') {
496 expr = strip_expr(expr->unop);
497 if (expr->type == EXPR_BINOP && expr->op == '+')
498 return 1;
501 if (expr->type != EXPR_BINOP || expr->op != '+')
502 return 0;
504 type = get_type(expr->left);
505 if (!type || type->type != SYM_ARRAY)
506 return 0;
508 return 1;
511 struct expression *get_array_base(struct expression *expr)
513 if (!is_array(expr))
514 return NULL;
515 expr = strip_expr(expr);
516 if (expr->type == EXPR_PREOP && expr->op == '*')
517 expr = strip_expr(expr->unop);
518 if (expr->type != EXPR_BINOP || expr->op != '+')
519 return NULL;
520 return strip_parens(expr->left);
523 struct expression *get_array_offset(struct expression *expr)
525 if (!is_array(expr))
526 return NULL;
527 expr = strip_expr(expr);
528 if (expr->type == EXPR_PREOP && expr->op == '*')
529 expr = strip_expr(expr->unop);
530 if (expr->type != EXPR_BINOP || expr->op != '+')
531 return NULL;
532 return strip_parens(expr->right);
535 const char *show_state(struct smatch_state *state)
537 if (!state)
538 return NULL;
539 return state->name;
542 struct statement *get_expression_statement(struct expression *expr)
544 /* What are those things called? if (({....; ret;})) { ...*/
546 if (expr->type != EXPR_PREOP)
547 return NULL;
548 if (expr->op != '(')
549 return NULL;
550 if (expr->unop->type != EXPR_STATEMENT)
551 return NULL;
552 if (expr->unop->statement->type != STMT_COMPOUND)
553 return NULL;
554 return expr->unop->statement;
557 struct expression *strip_parens(struct expression *expr)
559 if (!expr)
560 return NULL;
562 if (expr->type == EXPR_PREOP) {
563 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
564 expr->unop->statement->type == STMT_COMPOUND)
565 return expr;
566 if (expr->op == '(')
567 return strip_parens(expr->unop);
569 return expr;
572 struct expression *strip_expr(struct expression *expr)
574 if (!expr)
575 return NULL;
577 switch (expr->type) {
578 case EXPR_FORCE_CAST:
579 case EXPR_CAST:
580 return strip_expr(expr->cast_expression);
581 case EXPR_PREOP: {
582 struct expression *unop;
584 if (expr->op == '(' && expr->unop->type == EXPR_STATEMENT &&
585 expr->unop->statement->type == STMT_COMPOUND)
586 return expr;
588 unop = strip_expr(expr->unop);
590 if (expr->op == '*' &&
591 unop->type == EXPR_PREOP && unop->op == '&') {
592 struct symbol *type = get_type(unop->unop);
594 if (type && type->type == SYM_ARRAY)
595 return expr;
596 return strip_expr(unop->unop);
599 if (expr->op == '(')
600 return unop;
602 return expr;
604 case EXPR_CONDITIONAL:
605 if (known_condition_true(expr->conditional)) {
606 if (expr->cond_true)
607 return strip_expr(expr->cond_true);
608 return strip_expr(expr->conditional);
610 if (known_condition_false(expr->conditional))
611 return strip_expr(expr->cond_false);
612 return expr;
613 case EXPR_CALL:
614 if (sym_name_is("__builtin_expect", expr->fn)) {
615 expr = get_argument_from_call_expr(expr->args, 0);
616 return strip_expr(expr);
618 return expr;
620 return expr;
623 static void delete_state_tracker(struct tracker *t)
625 delete_state(t->owner, t->name, t->sym);
626 __free_tracker(t);
629 void scoped_state(int my_id, const char *name, struct symbol *sym)
631 struct tracker *t;
633 t = alloc_tracker(my_id, name, sym);
634 add_scope_hook((scope_hook *)&delete_state_tracker, t);
637 int is_error_return(struct expression *expr)
639 struct symbol *cur_func = cur_func_sym;
640 sval_t sval;
642 if (!expr)
643 return 0;
644 if (cur_func->type != SYM_NODE)
645 return 0;
646 cur_func = get_base_type(cur_func);
647 if (cur_func->type != SYM_FN)
648 return 0;
649 cur_func = get_base_type(cur_func);
650 if (cur_func == &void_ctype)
651 return 0;
652 if (!get_implied_value(expr, &sval))
653 return 0;
654 if (sval.value < 0)
655 return 1;
656 if (cur_func->type == SYM_PTR && sval.value == 0)
657 return 1;
658 return 0;
661 int getting_address(void)
663 struct expression *tmp;
664 int i = 0;
665 int dot_ops = 0;
667 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
668 if (!i++)
669 continue;
670 if (tmp->type == EXPR_PREOP && tmp->op == '(')
671 continue;
672 if (tmp->op == '.' && !dot_ops++)
673 continue;
674 if (tmp->op == '&')
675 return 1;
676 return 0;
677 } END_FOR_EACH_PTR_REVERSE(tmp);
678 return 0;
681 char *get_member_name(struct expression *expr)
683 char buf[256];
684 struct symbol *sym;
686 expr = strip_expr(expr);
687 if (expr->type != EXPR_DEREF)
688 return NULL;
689 if (!expr->member)
690 return NULL;
692 sym = get_type(expr->deref);
693 if (!sym)
694 return NULL;
695 if (sym->type == SYM_UNION) {
696 sym = expr_to_sym(expr->deref);
697 sym = get_real_base_type(sym);
698 if (sym && sym->type == SYM_PTR)
699 sym = get_real_base_type(sym);
700 if (!sym || !sym->ident) {
701 snprintf(buf, sizeof(buf), "(union hack)->%s", expr->member->name);
702 return alloc_string(buf);
705 if (!sym->ident)
706 return NULL;
707 snprintf(buf, sizeof(buf), "(struct %s)->%s", sym->ident->name, expr->member->name);
708 return alloc_string(buf);
711 int cmp_pos(struct position pos1, struct position pos2)
713 /* the stream position is ... */
714 if (pos1.stream > pos2.stream)
715 return -1;
716 if (pos1.stream < pos2.stream)
717 return 1;
719 if (pos1.line < pos2.line)
720 return -1;
721 if (pos1.line > pos2.line)
722 return 1;
724 if (pos1.pos < pos2.pos)
725 return -1;
726 if (pos1.pos > pos2.pos)
727 return 1;
729 return 0;
732 int positions_eq(struct position pos1, struct position pos2)
734 if (pos1.line != pos2.line)
735 return 0;
736 if (pos1.pos != pos2.pos)
737 return 0;
738 if (pos1.stream != pos2.stream)
739 return 0;
740 return 1;
743 struct statement *get_current_statement(void)
745 struct statement *prev, *tmp;
747 prev = last_ptr_list((struct ptr_list *)big_statement_stack);
749 if (!prev || !get_macro_name(prev->pos))
750 return prev;
752 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
753 if (positions_eq(tmp->pos, prev->pos))
754 continue;
755 if (prev->pos.line > tmp->pos.line)
756 return prev;
757 return tmp;
758 } END_FOR_EACH_PTR_REVERSE(tmp);
759 return prev;
762 struct statement *get_prev_statement(void)
764 struct statement *tmp;
765 int i;
767 i = 0;
768 FOR_EACH_PTR_REVERSE(big_statement_stack, tmp) {
769 if (i++ == 1)
770 return tmp;
771 } END_FOR_EACH_PTR_REVERSE(tmp);
772 return NULL;
775 int get_param_num_from_sym(struct symbol *sym)
777 struct symbol *tmp;
778 int i;
780 if (!cur_func_sym)
781 return -1;
783 i = 0;
784 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, tmp) {
785 if (tmp == sym)
786 return i;
787 i++;
788 } END_FOR_EACH_PTR(tmp);
789 return -1;
792 int get_param_num(struct expression *expr)
794 struct symbol *sym;
795 char *name;
797 if (!cur_func_sym)
798 return -1;
799 name = expr_to_var_sym(expr, &sym);
800 free_string(name);
801 if (!sym)
802 return -1;
803 return get_param_num_from_sym(sym);
806 int ms_since(struct timeval *start)
808 struct timeval end;
809 double diff;
811 gettimeofday(&end, NULL);
812 diff = (end.tv_sec - start->tv_sec) * 1000.0;
813 diff += (end.tv_usec - start->tv_usec) / 1000.0;
814 return (int)diff;
817 int parent_is_gone_var_sym(const char *name, struct symbol *sym)
819 if (!name || !sym)
820 return 0;
822 if (parent_is_null_var_sym(name, sym) ||
823 parent_is_free_var_sym(name, sym))
824 return 1;
825 return 0;
828 int parent_is_gone(struct expression *expr)
830 struct symbol *sym;
831 char *var;
832 int ret = 0;
834 expr = strip_expr(expr);
835 var = expr_to_var_sym(expr, &sym);
836 if (!var || !sym)
837 goto free;
838 ret = parent_is_gone_var_sym(var, sym);
839 free:
840 free_string(var);
841 return ret;
844 int invert_op(int op)
846 switch (op) {
847 case '*':
848 return '/';
849 case '/':
850 return '*';
851 case '+':
852 return '-';
853 case '-':
854 return '+';
855 case SPECIAL_LEFTSHIFT:
856 return SPECIAL_RIGHTSHIFT;
857 case SPECIAL_RIGHTSHIFT:
858 return SPECIAL_LEFTSHIFT;
860 return 0;
863 int expr_equiv(struct expression *one, struct expression *two)
865 struct symbol *one_sym, *two_sym;
866 char *one_name = NULL;
867 char *two_name = NULL;
868 int ret = 0;
870 if (!one || !two)
871 return 0;
872 if (one->type != two->type)
873 return 0;
875 one_name = expr_to_str_sym(one, &one_sym);
876 if (!one_name || !one_sym)
877 goto free;
878 two_name = expr_to_str_sym(two, &two_sym);
879 if (!two_name || !two_sym)
880 goto free;
881 if (one_sym != two_sym)
882 goto free;
883 if (strcmp(one_name, two_name) == 0)
884 ret = 1;
885 free:
886 free_string(one_name);
887 free_string(two_name);
888 return ret;
891 void push_int(struct int_stack **stack, int num)
893 int *munged;
896 * Just put the int on directly instead of a pointer to the int.
897 * Shift it to the left because Sparse uses the last two bits.
898 * This is sort of a dirty hack, yes.
901 munged = INT_PTR(num << 2);
903 add_ptr_list(stack, munged);
906 int pop_int(struct int_stack **stack)
908 int *num;
910 num = last_ptr_list((struct ptr_list *)*stack);
911 delete_ptr_list_last((struct ptr_list **)stack);
913 return PTR_INT(num) >> 2;