user_data2: mark sscanf() output as user data
[smatch.git] / smatch_type.c
blob7a294824f86326f3047fb460b748bb50a988b7fa
1 /*
2 * Copyright (C) 2009 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 * The idea here is that you have an expression and you
20 * want to know what the type is for that.
23 #include "smatch.h"
25 struct symbol *get_real_base_type(struct symbol *sym)
27 struct symbol *ret;
29 if (!sym)
30 return NULL;
31 ret = get_base_type(sym);
32 if (ret && ret->type == SYM_RESTRICT)
33 return get_real_base_type(ret);
34 return ret;
37 int type_bits(struct symbol *type)
39 if (!type)
40 return 0;
41 if (type->type == SYM_PTR) /* Sparse doesn't set this for &pointers */
42 return bits_in_pointer;
43 if (!type->examined)
44 examine_symbol_type(type);
45 return type->bit_size;
48 int type_bytes(struct symbol *type)
50 int bits = type_bits(type);
52 if (bits < 0)
53 return 0;
54 return bits_to_bytes(bits);
57 int type_positive_bits(struct symbol *type)
59 if (!type)
60 return 0;
61 if (type_unsigned(type))
62 return type_bits(type);
63 return type_bits(type) - 1;
66 static struct symbol *get_binop_type(struct expression *expr)
68 struct symbol *left, *right;
70 left = get_type(expr->left);
71 right = get_type(expr->right);
73 if (!left || !right)
74 return NULL;
76 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
77 return left;
78 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
79 return right;
81 if (expr->op == SPECIAL_LEFTSHIFT ||
82 expr->op == SPECIAL_RIGHTSHIFT) {
83 if (type_positive_bits(left) < 31)
84 return &int_ctype;
85 return left;
88 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
89 return &int_ctype;
91 if (type_positive_bits(left) > type_positive_bits(right))
92 return left;
93 return right;
96 static struct symbol *get_type_symbol(struct expression *expr)
98 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
99 return NULL;
101 return get_real_base_type(expr->symbol);
104 static struct symbol *get_member_symbol(struct symbol_list *symbol_list, struct ident *member)
106 struct symbol *tmp, *sub;
108 FOR_EACH_PTR(symbol_list, tmp) {
109 if (!tmp->ident) {
110 sub = get_real_base_type(tmp);
111 sub = get_member_symbol(sub->symbol_list, member);
112 if (sub)
113 return sub;
114 continue;
116 if (tmp->ident == member)
117 return tmp;
118 } END_FOR_EACH_PTR(tmp);
120 return NULL;
123 static struct symbol *get_symbol_from_deref(struct expression *expr)
125 struct ident *member;
126 struct symbol *sym;
128 if (!expr || expr->type != EXPR_DEREF)
129 return NULL;
131 member = expr->member;
132 sym = get_type(expr->deref);
133 if (!sym) {
134 // sm_msg("could not find struct type");
135 return NULL;
137 if (sym->type == SYM_PTR)
138 sym = get_real_base_type(sym);
139 sym = get_member_symbol(sym->symbol_list, member);
140 if (!sym)
141 return NULL;
142 return get_real_base_type(sym);
145 static struct symbol *get_return_type(struct expression *expr)
147 struct symbol *tmp;
149 tmp = get_type(expr->fn);
150 if (!tmp)
151 return NULL;
152 return get_real_base_type(tmp);
155 static struct symbol *get_expr_stmt_type(struct statement *stmt)
157 if (stmt->type != STMT_COMPOUND)
158 return NULL;
159 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
160 if (!stmt || stmt->type != STMT_EXPRESSION)
161 return NULL;
162 return get_type(stmt->expression);
165 static struct symbol *get_select_type(struct expression *expr)
167 struct symbol *one, *two;
169 one = get_type(expr->cond_true);
170 two = get_type(expr->cond_false);
171 if (!one || !two)
172 return NULL;
174 * This is a hack. If the types are not equiv then we
175 * really don't know the type. But I think guessing is
176 * probably Ok here.
178 if (type_positive_bits(one) > type_positive_bits(two))
179 return one;
180 return two;
183 struct symbol *get_pointer_type(struct expression *expr)
185 struct symbol *sym;
187 sym = get_type(expr);
188 if (!sym)
189 return NULL;
190 if (sym->type == SYM_NODE) {
191 sym = get_real_base_type(sym);
192 if (!sym)
193 return NULL;
195 if (sym->type != SYM_PTR && sym->type != SYM_ARRAY)
196 return NULL;
197 return get_real_base_type(sym);
200 static struct symbol *fake_pointer_sym(struct expression *expr)
202 struct symbol *sym;
203 struct symbol *base;
205 sym = alloc_symbol(expr->pos, SYM_PTR);
206 expr = expr->unop;
207 base = get_type(expr);
208 if (!base)
209 return NULL;
210 sym->ctype.base_type = base;
211 return sym;
214 struct symbol *get_type(struct expression *expr)
216 if (!expr)
217 return NULL;
218 expr = strip_parens(expr);
220 switch (expr->type) {
221 case EXPR_STRING:
222 return &string_ctype;
223 case EXPR_SYMBOL:
224 return get_type_symbol(expr);
225 case EXPR_DEREF:
226 return get_symbol_from_deref(expr);
227 case EXPR_PREOP:
228 case EXPR_POSTOP:
229 if (expr->op == '&')
230 return fake_pointer_sym(expr);
231 if (expr->op == '*')
232 return get_pointer_type(expr->unop);
233 return get_type(expr->unop);
234 case EXPR_ASSIGNMENT:
235 return get_type(expr->left);
236 case EXPR_CAST:
237 case EXPR_FORCE_CAST:
238 case EXPR_IMPLIED_CAST:
239 return get_real_base_type(expr->cast_type);
240 case EXPR_COMPARE:
241 case EXPR_BINOP:
242 return get_binop_type(expr);
243 case EXPR_CALL:
244 return get_return_type(expr);
245 case EXPR_STATEMENT:
246 return get_expr_stmt_type(expr->statement);
247 case EXPR_CONDITIONAL:
248 case EXPR_SELECT:
249 return get_select_type(expr);
250 case EXPR_SIZEOF:
251 return &ulong_ctype;
252 case EXPR_LOGICAL:
253 return &int_ctype;
254 default:
255 // sm_msg("unhandled type %d", expr->type);
256 return expr->ctype;
258 return NULL;
261 int type_unsigned(struct symbol *base_type)
263 if (!base_type)
264 return 0;
265 if (base_type->ctype.modifiers & MOD_UNSIGNED)
266 return 1;
267 return 0;
270 int type_signed(struct symbol *base_type)
272 if (!base_type)
273 return 0;
274 if (base_type->ctype.modifiers & MOD_SIGNED)
275 return 1;
276 return 0;
279 int expr_unsigned(struct expression *expr)
281 struct symbol *sym;
283 sym = get_type(expr);
284 if (!sym)
285 return 0;
286 if (type_unsigned(sym))
287 return 1;
288 return 0;
291 int expr_signed(struct expression *expr)
293 struct symbol *sym;
295 sym = get_type(expr);
296 if (!sym)
297 return 0;
298 if (type_signed(sym))
299 return 1;
300 return 0;
303 int returns_unsigned(struct symbol *sym)
305 if (!sym)
306 return 0;
307 sym = get_base_type(sym);
308 if (!sym || sym->type != SYM_FN)
309 return 0;
310 sym = get_base_type(sym);
311 return type_unsigned(sym);
314 int is_pointer(struct expression *expr)
316 struct symbol *sym;
318 sym = get_type(expr);
319 if (!sym)
320 return 0;
321 if (sym == &string_ctype)
322 return 0;
323 if (sym->type == SYM_PTR)
324 return 1;
325 return 0;
328 int returns_pointer(struct symbol *sym)
330 if (!sym)
331 return 0;
332 sym = get_base_type(sym);
333 if (!sym || sym->type != SYM_FN)
334 return 0;
335 sym = get_base_type(sym);
336 if (sym->type == SYM_PTR)
337 return 1;
338 return 0;
341 sval_t sval_type_max(struct symbol *base_type)
343 sval_t ret;
345 ret.value = (~0ULL) >> 1;
346 ret.type = base_type;
348 if (!base_type || !type_bits(base_type))
349 return ret;
351 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
352 return ret;
355 sval_t sval_type_min(struct symbol *base_type)
357 sval_t ret;
359 if (!base_type || !type_bits(base_type))
360 base_type = &llong_ctype;
361 ret.type = base_type;
363 if (type_unsigned(base_type)) {
364 ret.value = 0;
365 return ret;
368 ret.value = (~0ULL) << type_positive_bits(base_type);
370 return ret;
373 int nr_bits(struct expression *expr)
375 struct symbol *type;
377 type = get_type(expr);
378 if (!type)
379 return 0;
380 return type_bits(type);
383 int is_void_pointer(struct expression *expr)
385 struct symbol *type;
387 type = get_type(expr);
388 if (!type || type->type != SYM_PTR)
389 return 0;
390 type = get_real_base_type(type);
391 if (type == &void_ctype)
392 return 1;
393 return 0;
396 int is_char_pointer(struct expression *expr)
398 struct symbol *type;
400 type = get_type(expr);
401 if (!type || type->type != SYM_PTR)
402 return 0;
403 type = get_real_base_type(type);
404 if (type == &char_ctype)
405 return 1;
406 return 0;
409 int is_string(struct expression *expr)
411 expr = strip_expr(expr);
412 if (!expr || expr->type != EXPR_STRING)
413 return 0;
414 if (expr->string)
415 return 1;
416 return 0;
419 int is_static(struct expression *expr)
421 char *name;
422 struct symbol *sym;
423 int ret = 0;
425 name = expr_to_str_sym(expr, &sym);
426 if (!name || !sym)
427 goto free;
429 if (sym->ctype.modifiers & MOD_STATIC)
430 ret = 1;
431 free:
432 free_string(name);
433 return ret;
436 int types_equiv(struct symbol *one, struct symbol *two)
438 if (!one && !two)
439 return 1;
440 if (!one || !two)
441 return 0;
442 if (one->type != two->type)
443 return 0;
444 if (one->type == SYM_PTR)
445 return types_equiv(get_real_base_type(one), get_real_base_type(two));
446 if (type_positive_bits(one) != type_positive_bits(two))
447 return 0;
448 return 1;
451 int fn_static(void)
453 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
456 const char *global_static(void)
458 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
459 return "static";
460 else
461 return "global";
464 struct symbol *cur_func_return_type(void)
466 struct symbol *sym;
468 sym = get_real_base_type(cur_func_sym);
469 if (!sym || sym->type != SYM_FN)
470 return NULL;
471 sym = get_real_base_type(sym);
472 return sym;
475 struct symbol *get_arg_type(struct expression *fn, int arg)
477 struct symbol *fn_type;
478 struct symbol *tmp;
479 struct symbol *arg_type;
480 int i;
482 fn_type = get_type(fn);
483 if (!fn_type)
484 return NULL;
485 if (fn_type->type == SYM_PTR)
486 fn_type = get_real_base_type(fn_type);
487 if (fn_type->type != SYM_FN)
488 return NULL;
490 i = 0;
491 FOR_EACH_PTR(fn_type->arguments, tmp) {
492 arg_type = get_real_base_type(tmp);
493 if (i == arg) {
494 return arg_type;
496 i++;
497 } END_FOR_EACH_PTR(tmp);
499 return NULL;
502 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, char *name)
504 struct symbol *tmp, *sub;
505 int chunk_len;
507 if (strncmp(name, ".", 1) == 0)
508 name += 1;
509 if (strncmp(name, "->", 2) == 0)
510 name += 2;
512 FOR_EACH_PTR(symbol_list, tmp) {
513 if (!tmp->ident) {
514 sub = get_real_base_type(tmp);
515 sub = get_member_from_string(sub->symbol_list, name);
516 if (sub)
517 return sub;
518 continue;
521 if (strcmp(tmp->ident->name, name) == 0)
522 return tmp;
524 chunk_len = strlen(tmp->ident->name);
525 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
526 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
527 sub = get_real_base_type(tmp);
528 return get_member_from_string(sub->symbol_list, name + chunk_len);
531 } END_FOR_EACH_PTR(tmp);
533 return NULL;
536 struct symbol *get_member_type_from_key(struct expression *expr, char *key)
538 struct symbol *sym;
539 char *name;
541 if (strcmp(key, "$") == 0)
542 return get_type(expr);
544 if (strcmp(key, "*$") == 0) {
545 sym = get_type(expr);
546 if (!sym || sym->type != SYM_PTR)
547 return NULL;
548 return get_real_base_type(sym);
551 name = expr_to_str_sym(expr, &sym);
552 free_string(name);
553 if (!sym)
554 return NULL;
555 sym = get_real_base_type(sym);
556 if (sym->type == SYM_PTR)
557 sym = get_real_base_type(sym);
559 key = key + 1;
560 sym = get_member_from_string(sym->symbol_list, key);
561 if (!sym)
562 return NULL;
563 return get_real_base_type(sym);
566 int is_struct(struct expression *expr)
568 struct symbol *type;
570 type = get_type(expr);
571 if (type && type->type == SYM_STRUCT)
572 return 1;
573 return 0;
576 static struct {
577 struct symbol *sym;
578 const char *name;
579 } base_types[] = {
580 {&bool_ctype, "bool"},
581 {&void_ctype, "void"},
582 {&type_ctype, "type"},
583 {&char_ctype, "char"},
584 {&schar_ctype, "schar"},
585 {&uchar_ctype, "uchar"},
586 {&short_ctype, "short"},
587 {&sshort_ctype, "sshort"},
588 {&ushort_ctype, "ushort"},
589 {&int_ctype, "int"},
590 {&sint_ctype, "sint"},
591 {&uint_ctype, "uint"},
592 {&long_ctype, "long"},
593 {&slong_ctype, "slong"},
594 {&ulong_ctype, "ulong"},
595 {&llong_ctype, "llong"},
596 {&sllong_ctype, "sllong"},
597 {&ullong_ctype, "ullong"},
598 {&lllong_ctype, "lllong"},
599 {&slllong_ctype, "slllong"},
600 {&ulllong_ctype, "ulllong"},
601 {&float_ctype, "float"},
602 {&double_ctype, "double"},
603 {&ldouble_ctype, "ldouble"},
604 {&string_ctype, "string"},
605 {&ptr_ctype, "ptr"},
606 {&lazy_ptr_ctype, "lazy_ptr"},
607 {&incomplete_ctype, "incomplete"},
608 {&label_ctype, "label"},
609 {&bad_ctype, "bad"},
610 {&null_ctype, "null"},
613 static const char *base_type_str(struct symbol *sym)
615 int i;
617 for (i = 0; i < ARRAY_SIZE(base_types); i++) {
618 if (sym == base_types[i].sym)
619 return base_types[i].name;
621 return "<unknown>";
624 static int type_str_helper(char *buf, int size, struct symbol *type)
626 int n;
628 if (!type)
629 return snprintf(buf, size, "<unknown>");
631 if (type->type == SYM_BASETYPE) {
632 return snprintf(buf, size, base_type_str(type));
633 } else if (type->type == SYM_PTR) {
634 type = get_real_base_type(type);
635 n = type_str_helper(buf, size, type);
636 if (n > size)
637 return n;
638 return n + snprintf(buf + n, size - n, "*");
639 } else if (type->type == SYM_STRUCT) {
640 return snprintf(buf, size, "struct %s", type->ident ? type->ident->name : "");
641 } else if (type->type == SYM_UNION) {
642 if (type->ident)
643 return snprintf(buf, size, "union %s", type->ident->name);
644 else
645 return snprintf(buf, size, "anonymous union");
646 } else if (type->type == SYM_FN) {
647 struct symbol *arg, *return_type, *arg_type;
648 int i;
650 return_type = get_real_base_type(type);
651 n = type_str_helper(buf, size, return_type);
652 if (n > size)
653 return n;
654 n += snprintf(buf + n, size - n, "(*)(");
655 if (n > size)
656 return n;
658 i = 0;
659 FOR_EACH_PTR(type->arguments, arg) {
660 if (i++)
661 n += snprintf(buf + n, size - n, ", ");
662 if (n > size)
663 return n;
664 arg_type = get_real_base_type(arg);
665 n += type_str_helper(buf + n, size - n, arg_type);
666 if (n > size)
667 return n;
668 } END_FOR_EACH_PTR(arg);
670 return n + snprintf(buf + n, size - n, ")");
671 } else if (type->type == SYM_NODE) {
672 n = snprintf(buf, size, "node {");
673 if (n > size)
674 return n;
675 type = get_real_base_type(type);
676 n += type_str_helper(buf + n, size - n, type);
677 if (n > size)
678 return n;
679 return n + snprintf(buf + n, size - n, "}");
680 } else {
681 return snprintf(buf, size, "<type %d>", type->type);
685 char *type_to_str(struct symbol *type)
687 static char buf[256];
689 buf[0] = '\0';
690 type_str_helper(buf, sizeof(buf), type);
691 return buf;