smatch: make the --enable/disable options take strings
[smatch.git] / smatch.c
blob8640defd715ac8009be3751b99e760210fddf85e
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 "check_list.h"
24 char *option_debug_check = (char *)"";
25 char *option_project_str = (char *)"smatch_generic";
26 static char *option_db_file = (char *)"smatch_db.sqlite";
27 enum project_type option_project = PROJ_NONE;
28 char *bin_dir;
29 char *data_dir;
30 int option_no_data = 0;
31 int option_spammy = 0;
32 int option_info = 0;
33 int option_full_path = 0;
34 int option_param_mapper = 0;
35 int option_call_tree = 0;
36 int option_no_db = 0;
37 int option_enable = 0;
38 int option_disable = 0;
39 int option_debug_related;
40 int option_file_output;
41 int option_time;
42 int option_mem;
43 char *option_datadir_str;
44 FILE *sm_outfd;
45 FILE *sql_outfd;
46 FILE *caller_info_fd;
48 bool __silence_warnings_for_stmt;
50 typedef void (*reg_func) (int id);
51 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
52 static struct reg_func_info {
53 const char *name;
54 reg_func func;
55 int enabled;
56 } reg_funcs[] = {
57 {NULL, NULL},
58 #include "check_list.h"
60 #undef CK
61 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
63 const char *check_name(unsigned short id)
65 if (id >= ARRAY_SIZE(reg_funcs))
66 return "internal";
68 return reg_funcs[id].name;
71 int id_from_name(const char *name)
73 int i;
75 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
76 if (!strcmp(name, reg_funcs[i].name))
77 return i;
79 return 0;
82 static void show_checks(void)
84 int i;
86 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
87 if (!strncmp(reg_funcs[i].name, "check_", 6))
88 printf("%3d. %s\n", i, reg_funcs[i].name);
90 exit(1);
93 static void enable_disable_checks(char *s, bool enable)
95 char buf[128];
96 char *next;
97 int i;
99 do {
100 next = strchr(s, ',');
101 if (next) {
102 *next = '\0';
103 next++;
105 if (*s == '\0')
106 return;
107 if (strncmp(s, "check_", 6) == 0)
108 snprintf(buf, sizeof(buf), "%s", s);
109 else
110 snprintf(buf, sizeof(buf), "check_%s", s);
113 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
114 if (strcmp(reg_funcs[i].name, buf) == 0) {
115 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
116 break;
120 if (i == ARRAY_SIZE(reg_funcs)) {
121 printf("error: '%s' not found", s);
122 exit(1);
124 } while ((s = next));
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("--info: print info used to fill smatch_data/.\n");
133 printf("--debug: print lots of debug output.\n");
134 printf("--param-mapper: enable param_mapper output.\n");
135 printf("--no-data: do not use the /smatch_data/ directory.\n");
136 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
137 printf("--full-path: print the full pathname.\n");
138 printf("--debug-implied: print debug output about implications.\n");
139 printf("--assume-loops: assume loops always go through at least once.\n");
140 printf("--two-passes: use a two pass system for each function.\n");
141 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
142 printf("--help: print this helpful message.\n");
143 exit(1);
146 static int match_option(const char *arg, const char *option)
148 char *str;
149 char *tmp;
150 int ret = 0;
152 str = malloc(strlen(option) + 3);
153 snprintf(str, strlen(option) + 3, "--%s", option);
154 tmp = str;
155 while (*tmp) {
156 if (*tmp == '_')
157 *tmp = '-';
158 tmp++;
160 if (!strcmp(arg, str))
161 ret = 1;
162 free(str);
163 return ret;
166 #define OPTION(_x) do { \
167 if (!found && match_option((*argvp)[1], #_x)) { \
168 found = 1; \
169 option_##_x = 1; \
170 (*argvp)[1] = (*argvp)[0]; \
172 } while (0)
174 void parse_args(int *argcp, char ***argvp)
176 while (*argcp >= 2) {
177 int found = 0;
178 if (!strcmp((*argvp)[1], "--help"))
179 help();
181 if (!strcmp((*argvp)[1], "--show-checks"))
182 show_checks();
184 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
185 option_project_str = (*argvp)[1] + 10;
186 (*argvp)[1] = (*argvp)[0];
187 found = 1;
189 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
190 option_project_str = (*argvp)[1] + 3;
191 (*argvp)[1] = (*argvp)[0];
192 found = 1;
194 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
195 option_db_file = (*argvp)[1] + 10;
196 (*argvp)[1] = (*argvp)[0];
197 found = 1;
199 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
200 option_datadir_str = (*argvp)[1] + 7;
201 (*argvp)[1] = (*argvp)[0];
202 found = 1;
204 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
205 option_debug_check = (*argvp)[1] + 8;
206 (*argvp)[1] = (*argvp)[0];
207 found = 1;
209 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
210 trace_variable = (*argvp)[1] + 8;
211 (*argvp)[1] = (*argvp)[0];
212 found = 1;
214 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
215 enable_disable_checks((*argvp)[1] + 9, 1);
216 option_enable = 1;
217 (*argvp)[1] = (*argvp)[0];
218 found = 1;
220 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
221 enable_disable_checks((*argvp)[1] + 10, 0);
222 option_enable = 1;
223 option_disable = 1;
224 (*argvp)[1] = (*argvp)[0];
225 found = 1;
228 OPTION(spammy);
229 OPTION(info);
230 OPTION(debug);
231 OPTION(debug_implied);
232 OPTION(debug_related);
233 OPTION(assume_loops);
234 OPTION(no_data);
235 OPTION(two_passes);
236 OPTION(full_path);
237 OPTION(param_mapper);
238 OPTION(call_tree);
239 OPTION(file_output);
240 OPTION(time);
241 OPTION(mem);
242 OPTION(no_db);
243 if (!found)
244 break;
245 (*argcp)--;
246 (*argvp)++;
249 if (strcmp(option_project_str, "smatch_generic") != 0)
250 option_project = PROJ_UNKNOWN;
251 if (strcmp(option_project_str, "kernel") == 0)
252 option_project = PROJ_KERNEL;
253 if (strcmp(option_project_str, "wine") == 0)
254 option_project = PROJ_WINE;
257 static char *read_bin_filename(void)
259 char filename[PATH_MAX] = {};
260 char proc[PATH_MAX];
262 pid_t pid = getpid();
263 sprintf(proc, "/proc/%d/exe", pid);
264 if (readlink(proc, filename, PATH_MAX) < 0)
265 return NULL;
266 return alloc_string(filename);
269 static char *get_bin_dir(char *arg0)
271 char *orig;
273 orig = read_bin_filename();
274 if (!orig)
275 orig = alloc_string(arg0);
276 return dirname(orig);
279 static char *get_data_dir(char *arg0)
281 char buf[256];
282 char *dir;
284 if (option_no_data)
285 return NULL;
287 if (option_datadir_str) {
288 if (access(option_datadir_str, R_OK))
289 printf("Warning: %s is not accessible -- ignore.\n",
290 option_datadir_str);
291 else
292 return alloc_string(option_datadir_str);
295 strncpy(buf, "smatch_data/", sizeof(buf));
296 dir = alloc_string(buf);
297 if (!access(dir, R_OK))
298 return dir;
300 strncpy(buf, bin_dir, 254);
302 buf[255] = '\0';
303 strncat(buf, "/smatch_data/", 254 - strlen(buf));
304 dir = alloc_string(buf);
305 if (!access(dir, R_OK))
306 return dir;
307 free_string(dir);
308 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
309 dir = alloc_string(buf);
310 if (!access(dir, R_OK))
311 return dir;
313 printf("Warning: %s is not accessible.\n", dir);
314 printf("Use --no-data or --data to suppress this message.\n");
315 return NULL;
318 int main(int argc, char **argv)
320 int i;
321 reg_func func;
323 sm_outfd = stdout;
324 sql_outfd = stdout;
325 caller_info_fd = stdout;
326 parse_args(&argc, &argv);
328 /* this gets set back to zero when we parse the first function */
329 final_pass = 1;
331 bin_dir = get_bin_dir(argv[0]);
332 data_dir = get_data_dir(argv[0]);
334 allocate_hook_memory();
335 create_function_hook_hash();
336 open_smatch_db(option_db_file);
337 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
338 func = reg_funcs[i].func;
339 /* The script IDs start at 1.
340 0 is used for internal stuff. */
341 if (!option_enable || reg_funcs[i].enabled == 1 ||
342 (option_disable && reg_funcs[i].enabled != -1) ||
343 strncmp(reg_funcs[i].name, "register_", 9) == 0)
344 func(i);
347 smatch(argc, argv);
348 free_string(data_dir);
349 return 0;