core: make fake cur_slists stackable
[smatch.git] / check_stack.c
blobfa13147ce9b2f789e2a5de3f884b23f8054c768f
1 /*
2 * smatch/check_stack.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * The kernel has a small stack so putting huge structs and arrays on the
12 * stack is a bug.
16 #include "smatch.h"
18 static int my_id;
20 static int total_size;
21 static int max_size;
22 static int max_lineno;
23 static int max_func_pos;
24 static int complained;
26 static void scope_end(int size)
28 total_size -= size;
31 static void match_declarations(struct symbol *sym)
33 struct symbol *base;
34 const char *name;
36 base = get_base_type(sym);
37 if (sym->ctype.modifiers & MOD_STATIC)
38 return;
39 name = sym->ident->name;
40 total_size += base->bit_size;
41 if (total_size > max_size) {
42 max_size = total_size;
43 max_lineno = get_lineno();
44 max_func_pos = get_func_pos();
46 if (base->bit_size >= 500 * 8) {
47 complained = 1;
48 sm_msg("warn: '%s' puts %d bytes on stack", name, base->bit_size / 8);
50 add_scope_hook((scope_hook *)&scope_end, (void *)base->bit_size);
53 static void match_end_func(struct symbol *sym)
55 if ((max_size >= 500 * 8) && !complained) {
56 sm_printf("%s +%d %s(%d) ", get_filename(), max_lineno, get_function(), max_func_pos);
57 sm_printf("warn: function puts %d bytes on stack\n", max_size / 8);
59 total_size = 0;
60 complained = 0;
61 max_size = 0;
62 max_lineno = 0;
63 max_func_pos = 0;
66 void check_stack(int id)
68 if (option_project != PROJ_KERNEL)
69 return;
71 my_id = id;
72 add_hook(&match_declarations, DECLARATION_HOOK);
73 add_hook(&match_end_func, END_FUNC_HOOK);