kernel_user_data: use a param/key table for simple_strtoul()
[smatch.git] / smatch.c
blob84ddce32eaed4aefd51238a1bdbfbd2e21d184ee
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
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <libgen.h>
21 #include "smatch.h"
22 #include "smatch_slist.h"
23 #include "check_list.h"
25 char *option_debug_check;
26 char *option_debug_var;
27 char *option_project_str = (char *)"smatch_generic";
28 static char *option_db_file = (char *)"smatch_db.sqlite";
29 enum project_type option_project = PROJ_NONE;
30 char *bin_dir;
31 char *data_dir;
32 int option_no_data = 0;
33 int option_spammy = 0;
34 int option_pedantic;
35 int option_print_names;
36 int option_info = 0;
37 int option_full_path = 0;
38 int option_call_tree = 0;
39 int option_no_db = 0;
40 int option_enable = 0;
41 int option_disable = 0;
42 int option_file_output;
43 int option_time;
44 int option_time_stmt;
45 int option_mem;
46 char *option_datadir_str;
47 int option_fatal_checks;
48 int option_succeed;
49 int SMATCH_EXTRA;
51 FILE *sm_outfd;
52 FILE *sql_outfd;
53 FILE *caller_info_fd;
55 int sm_nr_errors;
56 int sm_nr_checks;
57 int __cur_check_id;
59 bool __silence_warnings_for_stmt;
61 typedef void (*reg_func) (int id);
62 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
63 static struct reg_func_info {
64 const char *name;
65 reg_func func;
66 int enabled;
67 } reg_funcs[] = {
68 {"internal", NULL},
69 #include "check_list.h"
71 #undef CK
72 int num_checks = ARRAY_SIZE(reg_funcs);
74 const char *check_name(unsigned short id)
76 if (id >= ARRAY_SIZE(reg_funcs))
77 return "internal";
79 return reg_funcs[id].name;
82 int id_from_name(const char *name)
84 int i;
86 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
87 if (!strcmp(name, reg_funcs[i].name))
88 return i;
90 return 0;
93 static void show_checks(void)
95 int i;
97 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
98 if (!strncmp(reg_funcs[i].name, "check_", 6))
99 printf("%3d. %s\n", i, reg_funcs[i].name);
101 exit(0);
104 static void enable_disable_checks(char *s, bool enable)
106 char buf[128];
107 char *next;
108 int i;
110 do {
111 next = strchr(s, ',');
112 if (next) {
113 *next = '\0';
114 next++;
116 if (*s == '\0')
117 return;
118 if (strncmp(s, "check_", 6) == 0)
119 snprintf(buf, sizeof(buf), "%s", s);
120 else
121 snprintf(buf, sizeof(buf), "check_%s", s);
124 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
125 if (strcmp(reg_funcs[i].name, buf) == 0) {
126 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
127 break;
131 if (i == ARRAY_SIZE(reg_funcs))
132 sm_fatal("'%s' not found", s);
134 } while ((s = next));
137 static void help(void)
139 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
140 printf("--project=<name> or -p=<name>: project specific tests\n");
141 printf("--succeed: don't exit with an error\n");
142 printf("--spammy: print superfluous crap.\n");
143 printf("--pedantic: intended for reviewing new drivers.\n");
144 printf("--info: print info used to fill smatch_data/.\n");
145 printf("--debug: print lots of debug output.\n");
146 printf("--no-data: do not use the /smatch_data/ directory.\n");
147 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
148 printf("--full-path: print the full pathname.\n");
149 printf("--debug-implied: print debug output about implications.\n");
150 printf("--assume-loops: assume loops always go through at least once.\n");
151 printf("--two-passes: use a two pass system for each function.\n");
152 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
153 printf("--fatal-checks: check output is treated as an error.\n");
154 printf("--help: print this helpful message.\n");
155 exit(1);
158 static int match_option(const char *arg, const char *option)
160 char *str;
161 char *tmp;
162 int ret = 0;
164 str = malloc(strlen(option) + 3);
165 snprintf(str, strlen(option) + 3, "--%s", option);
166 tmp = str;
167 while (*tmp) {
168 if (*tmp == '_')
169 *tmp = '-';
170 tmp++;
172 if (!strcmp(arg, str))
173 ret = 1;
174 free(str);
175 return ret;
178 #define OPTION(_x) do { \
179 if (!found && match_option((*argvp)[1], #_x)) { \
180 found = 1; \
181 option_##_x = 1; \
182 (*argvp)[1] = (*argvp)[0]; \
184 } while (0)
186 void parse_args(int *argcp, char ***argvp)
188 while (*argcp >= 2) {
189 int found = 0;
190 if (!strcmp((*argvp)[1], "--help"))
191 help();
193 if (!strcmp((*argvp)[1], "--show-checks"))
194 show_checks();
196 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
197 option_project_str = (*argvp)[1] + 10;
198 (*argvp)[1] = (*argvp)[0];
199 found = 1;
201 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
202 option_project_str = (*argvp)[1] + 3;
203 (*argvp)[1] = (*argvp)[0];
204 found = 1;
206 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
207 option_db_file = (*argvp)[1] + 10;
208 (*argvp)[1] = (*argvp)[0];
209 found = 1;
211 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
212 option_datadir_str = (*argvp)[1] + 7;
213 (*argvp)[1] = (*argvp)[0];
214 found = 1;
216 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
217 option_debug_check = (*argvp)[1] + 8;
218 (*argvp)[1] = (*argvp)[0];
219 found = 1;
221 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
222 trace_variable = (*argvp)[1] + 8;
223 (*argvp)[1] = (*argvp)[0];
224 found = 1;
226 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
227 enable_disable_checks((*argvp)[1] + 9, 1);
228 option_enable = 1;
229 (*argvp)[1] = (*argvp)[0];
230 found = 1;
232 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
233 enable_disable_checks((*argvp)[1] + 10, 0);
234 option_enable = 1;
235 option_disable = 1;
236 (*argvp)[1] = (*argvp)[0];
237 found = 1;
240 OPTION(fatal_checks);
241 OPTION(spammy);
242 OPTION(pedantic);
243 OPTION(info);
244 OPTION(debug);
245 OPTION(assume_loops);
246 OPTION(no_data);
247 OPTION(two_passes);
248 OPTION(full_path);
249 OPTION(call_tree);
250 OPTION(file_output);
251 OPTION(time);
252 OPTION(time_stmt);
253 OPTION(mem);
254 OPTION(no_db);
255 OPTION(succeed);
256 OPTION(print_names);
257 if (!found)
258 break;
259 (*argcp)--;
260 (*argvp)++;
263 if (strcmp(option_project_str, "smatch_generic") != 0)
264 option_project = PROJ_UNKNOWN;
266 if (strcmp(option_project_str, "kernel") == 0)
267 option_project = PROJ_KERNEL;
268 else if (strcmp(option_project_str, "wine") == 0)
269 option_project = PROJ_WINE;
270 else if (strcmp(option_project_str, "illumos_kernel") == 0)
271 option_project = PROJ_ILLUMOS_KERNEL;
272 else if (strcmp(option_project_str, "illumos_user") == 0)
273 option_project = PROJ_ILLUMOS_USER;
276 static char *read_bin_filename(void)
278 char filename[PATH_MAX] = {};
279 char proc[PATH_MAX];
281 pid_t pid = getpid();
282 sprintf(proc, "/proc/%d/exe", pid);
283 if (readlink(proc, filename, PATH_MAX) < 0)
284 return NULL;
285 return alloc_string(filename);
288 static char *get_bin_dir(char *arg0)
290 char *orig;
292 orig = read_bin_filename();
293 if (!orig)
294 orig = alloc_string(arg0);
295 return dirname(orig);
298 static char *get_data_dir(char *arg0)
300 char buf[256];
301 char *dir;
303 if (option_no_data)
304 return NULL;
306 if (option_datadir_str) {
307 if (access(option_datadir_str, R_OK))
308 sm_warning("%s is not accessible -- ignored.",
309 option_datadir_str);
310 else
311 return alloc_string(option_datadir_str);
314 strncpy(buf, "smatch_data/", sizeof(buf));
315 dir = alloc_string(buf);
316 if (!access(dir, R_OK))
317 return dir;
319 strncpy(buf, bin_dir, 254);
321 buf[255] = '\0';
322 strncat(buf, "/smatch_data/", 254 - strlen(buf));
323 dir = alloc_string(buf);
324 if (!access(dir, R_OK))
325 return dir;
326 free_string(dir);
327 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
328 dir = alloc_string(buf);
329 if (!access(dir, R_OK))
330 return dir;
332 sm_warning("%s is not accessible.", dir);
333 sm_warning("Use --no-data or --data to suppress this message.");
334 return NULL;
337 int main(int argc, char **argv)
339 struct string_list *filelist = NULL;
340 int i;
341 reg_func func;
343 sm_outfd = stdout;
344 sql_outfd = stdout;
345 caller_info_fd = stdout;
347 parse_args(&argc, &argv);
349 if (argc < 2)
350 help();
352 /* this gets set back to zero when we parse the first function */
353 final_pass = 1;
355 bin_dir = get_bin_dir(argv[0]);
356 data_dir = get_data_dir(argv[0]);
358 allocate_hook_memory();
359 allocate_dynamic_states_array(num_checks);
360 allocate_tracker_array(num_checks);
361 create_function_hook_hash();
362 open_smatch_db(option_db_file);
363 sparse_initialize(argc, argv, &filelist);
364 alloc_valid_ptr_rl();
365 SMATCH_EXTRA = id_from_name("register_smatch_extra");
366 allocate_modification_hooks();
368 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
369 __cur_check_id = i;
370 func = reg_funcs[i].func;
371 /* The script IDs start at 1.
372 0 is used for internal stuff. */
373 if (!option_enable || reg_funcs[i].enabled == 1 ||
374 (option_disable && reg_funcs[i].enabled != -1) ||
375 strncmp(reg_funcs[i].name, "register_", 9) == 0)
376 func(i);
378 __cur_check_id = 0;
380 smatch(filelist);
381 free_string(data_dir);
383 if (option_succeed)
384 return 0;
385 if (sm_nr_errors > 0)
386 return 1;
387 if (sm_nr_checks > 0 && option_fatal_checks)
388 return 1;
389 return 0;