Merge git://git.kernel.org/pub/scm/devel/sparse/sparse into devel
[smatch.git] / smatch.c
blobe3c0e90e9e55d4f5663264e790c7a1eb0ef17527
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_print_names;
35 int option_info = 0;
36 int option_full_path = 0;
37 int option_call_tree = 0;
38 int option_no_db = 0;
39 int option_enable = 0;
40 int option_disable = 0;
41 int option_file_output;
42 int option_time;
43 int option_time_stmt;
44 int option_mem;
45 char *option_datadir_str;
46 int option_fatal_checks;
47 int option_succeed;
49 FILE *sm_outfd;
50 FILE *sql_outfd;
51 FILE *caller_info_fd;
53 int sm_nr_errors;
54 int sm_nr_checks;
56 bool __silence_warnings_for_stmt;
58 typedef void (*reg_func) (int id);
59 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
60 static struct reg_func_info {
61 const char *name;
62 reg_func func;
63 int enabled;
64 } reg_funcs[] = {
65 {NULL, NULL},
66 #include "check_list.h"
68 #undef CK
69 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
71 const char *check_name(unsigned short id)
73 if (id >= ARRAY_SIZE(reg_funcs))
74 return "internal";
76 return reg_funcs[id].name;
79 int id_from_name(const char *name)
81 int i;
83 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
84 if (!strcmp(name, reg_funcs[i].name))
85 return i;
87 return 0;
90 static void show_checks(void)
92 int i;
94 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
95 if (!strncmp(reg_funcs[i].name, "check_", 6))
96 printf("%3d. %s\n", i, reg_funcs[i].name);
98 exit(0);
101 static void enable_disable_checks(char *s, bool enable)
103 char buf[128];
104 char *next;
105 int i;
107 do {
108 next = strchr(s, ',');
109 if (next) {
110 *next = '\0';
111 next++;
113 if (*s == '\0')
114 return;
115 if (strncmp(s, "check_", 6) == 0)
116 snprintf(buf, sizeof(buf), "%s", s);
117 else
118 snprintf(buf, sizeof(buf), "check_%s", s);
121 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
122 if (strcmp(reg_funcs[i].name, buf) == 0) {
123 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
124 break;
128 if (i == ARRAY_SIZE(reg_funcs))
129 sm_fatal("'%s' not found", s);
131 } while ((s = next));
134 static void help(void)
136 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
137 printf("--project=<name> or -p=<name>: project specific tests\n");
138 printf("--succeed: don't exit with an error\n");
139 printf("--spammy: print superfluous crap.\n");
140 printf("--info: print info used to fill smatch_data/.\n");
141 printf("--debug: print lots of debug output.\n");
142 printf("--no-data: do not use the /smatch_data/ directory.\n");
143 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
144 printf("--full-path: print the full pathname.\n");
145 printf("--debug-implied: print debug output about implications.\n");
146 printf("--assume-loops: assume loops always go through at least once.\n");
147 printf("--two-passes: use a two pass system for each function.\n");
148 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
149 printf("--fatal-checks: check output is treated as an error.\n");
150 printf("--help: print this helpful message.\n");
151 exit(1);
154 static int match_option(const char *arg, const char *option)
156 char *str;
157 char *tmp;
158 int ret = 0;
160 str = malloc(strlen(option) + 3);
161 snprintf(str, strlen(option) + 3, "--%s", option);
162 tmp = str;
163 while (*tmp) {
164 if (*tmp == '_')
165 *tmp = '-';
166 tmp++;
168 if (!strcmp(arg, str))
169 ret = 1;
170 free(str);
171 return ret;
174 #define OPTION(_x) do { \
175 if (!found && match_option((*argvp)[1], #_x)) { \
176 found = 1; \
177 option_##_x = 1; \
178 (*argvp)[1] = (*argvp)[0]; \
180 } while (0)
182 void parse_args(int *argcp, char ***argvp)
184 while (*argcp >= 2) {
185 int found = 0;
186 if (!strcmp((*argvp)[1], "--help"))
187 help();
189 if (!strcmp((*argvp)[1], "--show-checks"))
190 show_checks();
192 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
193 option_project_str = (*argvp)[1] + 10;
194 (*argvp)[1] = (*argvp)[0];
195 found = 1;
197 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
198 option_project_str = (*argvp)[1] + 3;
199 (*argvp)[1] = (*argvp)[0];
200 found = 1;
202 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
203 option_db_file = (*argvp)[1] + 10;
204 (*argvp)[1] = (*argvp)[0];
205 found = 1;
207 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
208 option_datadir_str = (*argvp)[1] + 7;
209 (*argvp)[1] = (*argvp)[0];
210 found = 1;
212 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
213 option_debug_check = (*argvp)[1] + 8;
214 (*argvp)[1] = (*argvp)[0];
215 found = 1;
217 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
218 trace_variable = (*argvp)[1] + 8;
219 (*argvp)[1] = (*argvp)[0];
220 found = 1;
222 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
223 enable_disable_checks((*argvp)[1] + 9, 1);
224 option_enable = 1;
225 (*argvp)[1] = (*argvp)[0];
226 found = 1;
228 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
229 enable_disable_checks((*argvp)[1] + 10, 0);
230 option_enable = 1;
231 option_disable = 1;
232 (*argvp)[1] = (*argvp)[0];
233 found = 1;
236 OPTION(fatal_checks);
237 OPTION(spammy);
238 OPTION(info);
239 OPTION(debug);
240 OPTION(assume_loops);
241 OPTION(no_data);
242 OPTION(two_passes);
243 OPTION(full_path);
244 OPTION(call_tree);
245 OPTION(file_output);
246 OPTION(time);
247 OPTION(time_stmt);
248 OPTION(mem);
249 OPTION(no_db);
250 OPTION(succeed);
251 OPTION(print_names);
252 if (!found)
253 break;
254 (*argcp)--;
255 (*argvp)++;
258 if (strcmp(option_project_str, "smatch_generic") != 0)
259 option_project = PROJ_UNKNOWN;
261 if (strcmp(option_project_str, "kernel") == 0)
262 option_project = PROJ_KERNEL;
263 else if (strcmp(option_project_str, "wine") == 0)
264 option_project = PROJ_WINE;
265 else if (strcmp(option_project_str, "illumos_kernel") == 0)
266 option_project = PROJ_ILLUMOS_KERNEL;
267 else if (strcmp(option_project_str, "illumos_user") == 0)
268 option_project = PROJ_ILLUMOS_USER;
271 static char *read_bin_filename(void)
273 char filename[PATH_MAX] = {};
274 char proc[PATH_MAX];
276 pid_t pid = getpid();
277 sprintf(proc, "/proc/%d/exe", pid);
278 if (readlink(proc, filename, PATH_MAX) < 0)
279 return NULL;
280 return alloc_string(filename);
283 static char *get_bin_dir(char *arg0)
285 char *orig;
287 orig = read_bin_filename();
288 if (!orig)
289 orig = alloc_string(arg0);
290 return dirname(orig);
293 static char *get_data_dir(char *arg0)
295 char buf[256];
296 char *dir;
298 if (option_no_data)
299 return NULL;
301 if (option_datadir_str) {
302 if (access(option_datadir_str, R_OK))
303 sm_warning("%s is not accessible -- ignored.",
304 option_datadir_str);
305 else
306 return alloc_string(option_datadir_str);
309 strncpy(buf, "smatch_data/", sizeof(buf));
310 dir = alloc_string(buf);
311 if (!access(dir, R_OK))
312 return dir;
314 strncpy(buf, bin_dir, 254);
316 buf[255] = '\0';
317 strncat(buf, "/smatch_data/", 254 - strlen(buf));
318 dir = alloc_string(buf);
319 if (!access(dir, R_OK))
320 return dir;
321 free_string(dir);
322 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
323 dir = alloc_string(buf);
324 if (!access(dir, R_OK))
325 return dir;
327 sm_warning("%s is not accessible.", dir);
328 sm_warning("Use --no-data or --data to suppress this message.");
329 return NULL;
332 int main(int argc, char **argv)
334 struct string_list *filelist = NULL;
335 int i;
336 reg_func func;
338 sm_outfd = stdout;
339 sql_outfd = stdout;
340 caller_info_fd = stdout;
342 parse_args(&argc, &argv);
344 if (argc < 2)
345 help();
347 /* this gets set back to zero when we parse the first function */
348 final_pass = 1;
350 bin_dir = get_bin_dir(argv[0]);
351 data_dir = get_data_dir(argv[0]);
353 allocate_hook_memory();
354 allocate_dynamic_states_array(num_checks);
355 allocate_tracker_array(num_checks);
356 create_function_hook_hash();
357 open_smatch_db(option_db_file);
358 sparse_initialize(argc, argv, &filelist);
359 alloc_valid_ptr_rl();
361 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
362 func = reg_funcs[i].func;
363 /* The script IDs start at 1.
364 0 is used for internal stuff. */
365 if (!option_enable || reg_funcs[i].enabled == 1 ||
366 (option_disable && reg_funcs[i].enabled != -1) ||
367 strncmp(reg_funcs[i].name, "register_", 9) == 0)
368 func(i);
371 smatch(filelist);
372 free_string(data_dir);
374 if (option_succeed)
375 return 0;
376 if (sm_nr_errors > 0)
377 return 1;
378 if (sm_nr_checks > 0 && option_fatal_checks)
379 return 1;
380 return 0;