unreachable: silence "not actually initialized" false positives
[smatch.git] / smatch_type.c
blob51a892a5cd42646a735823e496a70dc32c85de71
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 || (sym->type != SYM_PTR && sym->type != SYM_ARRAY))
189 return NULL;
190 return get_real_base_type(sym);
193 static struct symbol *fake_pointer_sym(struct expression *expr)
195 struct symbol *sym;
196 struct symbol *base;
198 sym = alloc_symbol(expr->pos, SYM_PTR);
199 expr = expr->unop;
200 base = get_type(expr);
201 if (!base)
202 return NULL;
203 sym->ctype.base_type = base;
204 return sym;
207 struct symbol *get_type(struct expression *expr)
209 if (!expr)
210 return NULL;
211 expr = strip_parens(expr);
213 switch (expr->type) {
214 case EXPR_STRING:
215 return &string_ctype;
216 case EXPR_SYMBOL:
217 return get_type_symbol(expr);
218 case EXPR_DEREF:
219 return get_symbol_from_deref(expr);
220 case EXPR_PREOP:
221 case EXPR_POSTOP:
222 if (expr->op == '&')
223 return fake_pointer_sym(expr);
224 if (expr->op == '*')
225 return get_pointer_type(expr->unop);
226 return get_type(expr->unop);
227 case EXPR_ASSIGNMENT:
228 return get_type(expr->left);
229 case EXPR_CAST:
230 case EXPR_FORCE_CAST:
231 case EXPR_IMPLIED_CAST:
232 return get_real_base_type(expr->cast_type);
233 case EXPR_COMPARE:
234 case EXPR_BINOP:
235 return get_binop_type(expr);
236 case EXPR_CALL:
237 return get_return_type(expr);
238 case EXPR_STATEMENT:
239 return get_expr_stmt_type(expr->statement);
240 case EXPR_CONDITIONAL:
241 case EXPR_SELECT:
242 return get_select_type(expr);
243 case EXPR_SIZEOF:
244 return &ulong_ctype;
245 case EXPR_LOGICAL:
246 return &int_ctype;
247 default:
248 // sm_msg("unhandled type %d", expr->type);
249 return expr->ctype;
251 return NULL;
254 int type_unsigned(struct symbol *base_type)
256 if (!base_type)
257 return 0;
258 if (base_type->ctype.modifiers & MOD_UNSIGNED)
259 return 1;
260 return 0;
263 int type_signed(struct symbol *base_type)
265 if (!base_type)
266 return 0;
267 if (base_type->ctype.modifiers & MOD_UNSIGNED)
268 return 0;
269 return 1;
272 int expr_unsigned(struct expression *expr)
274 struct symbol *sym;
276 sym = get_type(expr);
277 if (!sym)
278 return 0;
279 if (type_unsigned(sym))
280 return 1;
281 return 0;
284 int returns_unsigned(struct symbol *sym)
286 if (!sym)
287 return 0;
288 sym = get_base_type(sym);
289 if (!sym || sym->type != SYM_FN)
290 return 0;
291 sym = get_base_type(sym);
292 return type_unsigned(sym);
295 int is_pointer(struct expression *expr)
297 struct symbol *sym;
299 sym = get_type(expr);
300 if (!sym)
301 return 0;
302 if (sym == &string_ctype)
303 return 0;
304 if (sym->type == SYM_PTR)
305 return 1;
306 return 0;
309 int returns_pointer(struct symbol *sym)
311 if (!sym)
312 return 0;
313 sym = get_base_type(sym);
314 if (!sym || sym->type != SYM_FN)
315 return 0;
316 sym = get_base_type(sym);
317 if (sym->type == SYM_PTR)
318 return 1;
319 return 0;
322 sval_t sval_type_max(struct symbol *base_type)
324 sval_t ret;
326 ret.value = (~0ULL) >> 1;
327 ret.type = base_type;
329 if (!base_type || !type_bits(base_type))
330 return ret;
332 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
333 return ret;
336 sval_t sval_type_min(struct symbol *base_type)
338 sval_t ret;
340 if (!base_type || !type_bits(base_type))
341 base_type = &llong_ctype;
342 ret.type = base_type;
344 if (type_unsigned(base_type)) {
345 ret.value = 0;
346 return ret;
349 ret.value = (~0ULL) << type_positive_bits(base_type);
351 return ret;
354 int nr_bits(struct expression *expr)
356 struct symbol *type;
358 type = get_type(expr);
359 if (!type)
360 return 0;
361 return type_bits(type);
364 int is_void_pointer(struct expression *expr)
366 struct symbol *type;
368 type = get_type(expr);
369 if (!type || type->type != SYM_PTR)
370 return 0;
371 type = get_real_base_type(type);
372 if (type == &void_ctype)
373 return 1;
374 return 0;
377 int is_char_pointer(struct expression *expr)
379 struct symbol *type;
381 type = get_type(expr);
382 if (!type || type->type != SYM_PTR)
383 return 0;
384 type = get_real_base_type(type);
385 if (type == &char_ctype)
386 return 1;
387 return 0;
390 int is_string(struct expression *expr)
392 expr = strip_expr(expr);
393 if (!expr || expr->type != EXPR_STRING)
394 return 0;
395 if (expr->string)
396 return 1;
397 return 0;
400 int is_static(struct expression *expr)
402 char *name;
403 struct symbol *sym;
404 int ret = 0;
406 name = expr_to_str_sym(expr, &sym);
407 if (!name || !sym)
408 goto free;
410 if (sym->ctype.modifiers & MOD_STATIC)
411 ret = 1;
412 free:
413 free_string(name);
414 return ret;
417 int types_equiv(struct symbol *one, struct symbol *two)
419 if (!one && !two)
420 return 1;
421 if (!one || !two)
422 return 0;
423 if (one->type != two->type)
424 return 0;
425 if (one->type == SYM_PTR)
426 return types_equiv(get_real_base_type(one), get_real_base_type(two));
427 if (type_positive_bits(one) != type_positive_bits(two))
428 return 0;
429 return 1;
432 int fn_static(void)
434 return !!(cur_func_sym->ctype.modifiers & MOD_STATIC);
437 const char *global_static()
439 if (cur_func_sym->ctype.modifiers & MOD_STATIC)
440 return "static";
441 else
442 return "global";
445 struct symbol *cur_func_return_type(void)
447 struct symbol *sym;
449 sym = get_real_base_type(cur_func_sym);
450 if (!sym || sym->type != SYM_FN)
451 return NULL;
452 sym = get_real_base_type(sym);
453 return sym;
456 struct symbol *get_arg_type(struct expression *fn, int arg)
458 struct symbol *fn_type;
459 struct symbol *tmp;
460 struct symbol *arg_type;
461 int i;
463 fn_type = get_type(fn);
464 if (!fn_type)
465 return NULL;
466 if (fn_type->type == SYM_PTR)
467 fn_type = get_real_base_type(fn_type);
468 if (fn_type->type != SYM_FN)
469 return NULL;
471 i = 0;
472 FOR_EACH_PTR(fn_type->arguments, tmp) {
473 arg_type = get_real_base_type(tmp);
474 if (i == arg) {
475 return arg_type;
477 i++;
478 } END_FOR_EACH_PTR(tmp);
480 return NULL;
483 static struct symbol *get_member_from_string(struct symbol_list *symbol_list, char *name)
485 struct symbol *tmp, *sub;
486 int chunk_len;
488 if (strncmp(name, ".", 1) == 0)
489 name += 1;
490 if (strncmp(name, "->", 2) == 0)
491 name += 2;
493 FOR_EACH_PTR(symbol_list, tmp) {
494 if (!tmp->ident) {
495 sub = get_real_base_type(tmp);
496 sub = get_member_from_string(sub->symbol_list, name);
497 if (sub)
498 return sub;
499 continue;
502 if (strcmp(tmp->ident->name, name) == 0)
503 return tmp;
505 chunk_len = strlen(tmp->ident->name);
506 if (strncmp(tmp->ident->name, name, chunk_len) == 0 &&
507 (name[chunk_len] == '.' || name[chunk_len] == '-')) {
508 sub = get_real_base_type(tmp);
509 return get_member_from_string(sub->symbol_list, name + chunk_len);
512 } END_FOR_EACH_PTR(tmp);
514 return NULL;
517 struct symbol *get_member_type_from_key(struct expression *expr, char *key)
519 struct symbol *sym;
520 char *name;
522 if (strcmp(key, "$$") == 0)
523 return get_type(expr);
525 if (strcmp(key, "*$$") == 0) {
526 sym = get_type(expr);
527 if (!sym || sym->type != SYM_PTR)
528 return NULL;
529 return get_real_base_type(sym);
532 name = expr_to_str_sym(expr, &sym);
533 free_string(name);
534 if (!sym)
535 return NULL;
536 sym = get_real_base_type(sym);
537 if (sym->type == SYM_PTR)
538 sym = get_real_base_type(sym);
540 key = key + 2;
541 sym = get_member_from_string(sym->symbol_list, key);
542 if (!sym)
543 return NULL;
544 return get_real_base_type(sym);
547 int is_struct(struct expression *expr)
549 struct symbol *type;
551 type = get_type(expr);
552 if (type && type->type == SYM_STRUCT)
553 return 1;
554 return 0;