dma_on_stack: &foo means it's an error too
[smatch.git] / smatch.c
blobae54d74707ff5a0b5a681b447f77bb056be6d1ce
1 /*
2 * sparse/smatch.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <libgen.h>
13 #include "smatch.h"
15 char *option_project_str = (char *)"";
16 enum project_type option_project = PROJ_NONE;
17 char *data_dir;
18 int option_no_data = 0;
19 int option_spammy = 0;
20 int option_full_path = 0;
22 typedef void (*reg_func) (int id);
23 void register_smatch_extra(int id);
24 void register_smatch_ignore(int id);
25 void register_implications(int id);
26 void register_function_hooks(int id);
27 void register_modification_hooks(int id);
28 void register_containers(int id);
29 void check_debug(int id);
30 void check_assigned_expr(int id);
31 void check_null_deref(int id);
32 void check_overflow(int id);
33 void check_locking(int id);
34 void check_memory(int id);
35 void check_frees_argument(int id);
36 void check_puts_argument(int id);
37 void check_leaks(int id);
38 void check_type(int id);
39 void check_allocation_funcs(int id);
40 void check_err_ptr(int id);
41 void check_err_ptr_deref(int id);
42 void check_balanced(int id);
43 void check_deref_check(int id);
44 void check_hold_dev(int id);
45 void check_redundant_null_check(int id);
46 void check_signed(int id);
47 void check_precedence(int id);
48 void check_format_string(int id);
49 void check_unused_ret(int id);
50 void check_dma_on_stack(int id);
51 /* <- your test goes here */
53 /* may as well put wine scripts all together */
54 void check_wine(int id);
55 void check_wine_filehandles(int id);
56 void check_wine_WtoA(int id);
58 /* void register_template(int id); */
60 #define CK(_x) {.name = #_x, .func = &_x}
61 static struct reg_func_info {
62 const char *name;
63 reg_func func;
64 } reg_funcs[] = {
65 CK(register_smatch_extra), /* smatch_extra always has to be first */
66 CK(register_smatch_ignore),
67 CK(check_debug),
68 CK(check_assigned_expr),
70 CK(check_null_deref),
71 CK(check_overflow),
72 CK(check_memory),
73 CK(check_type),
74 CK(check_allocation_funcs),
75 CK(check_leaks),
76 CK(check_frees_argument),
77 CK(check_balanced),
78 CK(check_deref_check),
79 CK(check_redundant_null_check),
80 CK(check_signed),
81 CK(check_precedence),
82 CK(check_format_string),
83 CK(check_unused_ret),
84 CK(check_dma_on_stack),
86 /* <- your test goes here */
87 /* CK(register_template), */
89 /* kernel specific */
90 CK(check_locking),
91 CK(check_puts_argument),
92 CK(check_err_ptr),
93 CK(check_err_ptr_deref),
94 CK(check_hold_dev),
96 /* wine specific stuff */
97 CK(check_wine),
98 CK(check_wine_filehandles),
99 CK(check_wine_WtoA),
101 CK(register_modification_hooks),
102 CK(register_containers),
103 CK(register_implications), /* implications always has to be last */
106 const char *check_name(unsigned short id)
108 if (id > ARRAY_SIZE(reg_funcs)) {
109 return "internal";
111 return reg_funcs[id - 1].name;
114 int id_from_name(const char *name)
116 int i;
118 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
119 if (!strcmp(name, reg_funcs[i].name))
120 return i + 1;
122 return 0;
125 struct smatch_state *default_state[ARRAY_SIZE(reg_funcs)];
127 static void help(void)
129 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
130 printf("--project=<name> or -p=<name>: project specific tests\n");
131 printf("--spammy: print superfluous crap.\n");
132 printf("--debug: print lots of debug output.\n");
133 printf("--no-data: do not use the /smatch_data/ directory.\n");
134 printf("--full-path: print the full pathname.\n");
135 printf("--debug-implied: print debug output about implications.\n");
136 printf("--oom <num>: number of mB memory to use before giving up.\n");
137 printf("--no-implied: ignore implications.\n");
138 printf("--assume-loops: assume loops always go through at least once.\n");
139 printf("--known-conditions: don't branch for known conditions.\n");
140 printf("--two-passes: use a two pass system for each function.\n");
141 printf("--help: print this helpfull message.\n");
142 exit(1);
145 void parse_args(int *argcp, char ***argvp)
147 while(*argcp >= 2) {
148 if (!strncmp((*argvp)[1], "--project=", 10)) {
149 option_project_str = (*argvp)[1] + 10;
150 (*argvp)[1] = (*argvp)[0];
151 } else if (!strncmp((*argvp)[1], "-p=", 3)) {
152 option_project_str = (*argvp)[1] + 3;
153 (*argvp)[1] = (*argvp)[0];
154 } else if (!strcmp((*argvp)[1], "--debug")) {
155 debug_states = 1;
156 (*argvp)[1] = (*argvp)[0];
157 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
158 debug_implied_states = 1;
159 (*argvp)[1] = (*argvp)[0];
160 } else if (!strcmp((*argvp)[1], "--oom")) {
161 option_oom_kb = atoi((*argvp)[2]) * 1000;
162 (*argvp)[2] = (*argvp)[0];
163 (*argcp)--;
164 (*argvp)++;
165 } else if (!strcmp((*argvp)[1], "--no-implied")) {
166 option_no_implied = 1;
167 (*argvp)[1] = (*argvp)[0];
168 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
169 option_assume_loops = 1;
170 (*argvp)[1] = (*argvp)[0];
171 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
172 option_known_conditions = 1;
173 (*argvp)[1] = (*argvp)[0];
174 } else if (!strcmp((*argvp)[1], "--no-data")) {
175 option_no_data = 1;
176 (*argvp)[1] = (*argvp)[0];
177 } else if (!strcmp((*argvp)[1], "--spammy")) {
178 option_spammy = 1;
179 (*argvp)[1] = (*argvp)[0];
180 } else if (!strcmp((*argvp)[1], "--two-passes")) {
181 option_two_passes = 1;
182 (*argvp)[1] = (*argvp)[0];
183 } else if (!strcmp((*argvp)[1], "--full-path")) {
184 option_full_path = 1;
185 (*argvp)[1] = (*argvp)[0];
186 } else if (!strcmp((*argvp)[1], "--help")) {
187 help();
188 } else {
189 break;
191 (*argcp)--;
192 (*argvp)++;
195 if (!strcmp(option_project_str, "kernel"))
196 option_project = PROJ_KERNEL;
197 if (!strcmp(option_project_str, "wine"))
198 option_project = PROJ_WINE;
201 static char *get_data_dir(char *arg0)
203 char *bin_dir;
204 char buf[256];
205 char *dir;
207 if (option_no_data) {
208 return NULL;
210 bin_dir = dirname(alloc_string(arg0));
211 strncpy(buf, bin_dir, 254);
212 buf[255] = '\0';
213 strncat(buf, "/smatch_data/", 254 - strlen(buf));
214 dir = alloc_string(buf);
215 if (!access(dir, R_OK))
216 return dir;
217 printf("Warning: %s is not accessible.\n", dir);
218 printf("Use --no-data to suppress this message.\n");
219 return NULL;
222 int main(int argc, char **argv)
224 int i;
225 reg_func func;
227 parse_args(&argc, &argv);
229 data_dir = get_data_dir(argv[0]);
231 create_function_hash();
232 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
233 func = reg_funcs[i].func;
234 /* The script IDs start at 1.
235 0 is used for internal stuff. */
236 func(i + 1);
238 register_function_hooks(-1);
240 smatch(argc, argv);
241 free_string(data_dir);
242 return 0;