extra: move get_address_rl() out of get_extra_state()
[smatch.git] / smatch.c
blobbfbac8f3ea61f16b468f0e24f59323444ab72db5
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 enum project_type option_project = PROJ_NONE;
27 char *data_dir;
28 int option_no_data = 0;
29 int option_spammy = 0;
30 int option_info = 0;
31 int option_full_path = 0;
32 int option_param_mapper = 0;
33 int option_call_tree = 0;
34 int option_no_db = 0;
35 int option_enable = 0;
36 int option_debug_related;
37 int option_file_output;
38 int option_time;
39 int option_mem;
40 char *option_datadir_str;
41 FILE *sm_outfd;
42 FILE *sql_outfd;
43 FILE *caller_info_fd;
45 bool __silence_warnings_for_stmt;
47 typedef void (*reg_func) (int id);
48 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
49 static struct reg_func_info {
50 const char *name;
51 reg_func func;
52 int enabled;
53 } reg_funcs[] = {
54 {NULL, NULL},
55 #include "check_list.h"
57 #undef CK
58 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
60 const char *check_name(unsigned short id)
62 if (id >= ARRAY_SIZE(reg_funcs))
63 return "internal";
65 return reg_funcs[id].name;
68 int id_from_name(const char *name)
70 int i;
72 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
73 if (!strcmp(name, reg_funcs[i].name))
74 return i;
76 return 0;
79 static void show_checks(void)
81 int i;
83 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
84 if (!strncmp(reg_funcs[i].name, "check_", 6))
85 printf("%3d. %s\n", i, reg_funcs[i].name);
87 exit(1);
89 static void enable_check(int i)
91 if (1 <= i && i < ARRAY_SIZE(reg_funcs))
92 reg_funcs[i].enabled = 1;
95 static void enable_checks(const char *s)
97 int n = 0, lo = -1, i;
99 do {
100 switch (*s) {
101 case ',':
102 case '\0':
103 if (lo < 0)
104 enable_check(n);
105 else
106 for (i = lo; i <= n; ++i)
107 enable_check(i);
108 lo = -1;
109 n = 0;
110 break;
111 case '-':
112 lo = n;
113 n = 0;
114 break;
115 case '0' ... '9':
116 n = 10*n + (*s - '0');
117 break;
118 default:
119 fprintf(stderr, "invalid character '%c'\n", *s);
120 exit(1);
122 } while (*s++);
125 static void help(void)
127 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
128 printf("--project=<name> or -p=<name>: project specific tests\n");
129 printf("--spammy: print superfluous crap.\n");
130 printf("--info: print info used to fill smatch_data/.\n");
131 printf("--debug: print lots of debug output.\n");
132 printf("--param-mapper: enable param_mapper output.\n");
133 printf("--no-data: do not use the /smatch_data/ directory.\n");
134 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
135 printf("--full-path: print the full pathname.\n");
136 printf("--debug-implied: print debug output about implications.\n");
137 printf("--assume-loops: assume loops always go through at least once.\n");
138 printf("--two-passes: use a two pass system for each function.\n");
139 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
140 printf("--help: print this helpful message.\n");
141 exit(1);
144 static int match_option(const char *arg, const char *option)
146 char *str;
147 char *tmp;
148 int ret = 0;
150 str = malloc(strlen(option) + 3);
151 snprintf(str, strlen(option) + 3, "--%s", option);
152 tmp = str;
153 while (*tmp) {
154 if (*tmp == '_')
155 *tmp = '-';
156 tmp++;
158 if (!strcmp(arg, str))
159 ret = 1;
160 free(str);
161 return ret;
164 #define OPTION(_x) do { \
165 if (!found && match_option((*argvp)[1], #_x)) { \
166 found = 1; \
167 option_##_x = 1; \
168 (*argvp)[1] = (*argvp)[0]; \
170 } while (0)
172 void parse_args(int *argcp, char ***argvp)
174 while (*argcp >= 2) {
175 int found = 0;
176 if (!strcmp((*argvp)[1], "--help"))
177 help();
179 if (!strcmp((*argvp)[1], "--show-checks"))
180 show_checks();
182 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
183 option_project_str = (*argvp)[1] + 10;
184 (*argvp)[1] = (*argvp)[0];
185 found = 1;
187 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
188 option_project_str = (*argvp)[1] + 3;
189 (*argvp)[1] = (*argvp)[0];
190 found = 1;
192 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
193 option_datadir_str = (*argvp)[1] + 7;
194 (*argvp)[1] = (*argvp)[0];
195 found = 1;
197 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
198 option_debug_check = (*argvp)[1] + 8;
199 (*argvp)[1] = (*argvp)[0];
200 found = 1;
202 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
203 trace_variable = (*argvp)[1] + 8;
204 (*argvp)[1] = (*argvp)[0];
205 found = 1;
207 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
208 enable_checks((*argvp)[1] + 9);
209 option_enable = 1;
210 (*argvp)[1] = (*argvp)[0];
211 found = 1;
214 OPTION(spammy);
215 OPTION(info);
216 OPTION(debug);
217 OPTION(debug_implied);
218 OPTION(debug_related);
219 OPTION(assume_loops);
220 OPTION(no_data);
221 OPTION(two_passes);
222 OPTION(full_path);
223 OPTION(param_mapper);
224 OPTION(call_tree);
225 OPTION(file_output);
226 OPTION(time);
227 OPTION(mem);
228 OPTION(no_db);
229 if (!found)
230 break;
231 (*argcp)--;
232 (*argvp)++;
235 if (strcmp(option_project_str, "smatch_generic") != 0)
236 option_project = PROJ_UNKNOWN;
237 if (strcmp(option_project_str, "kernel") == 0)
238 option_project = PROJ_KERNEL;
239 if (strcmp(option_project_str, "wine") == 0)
240 option_project = PROJ_WINE;
243 static char *read_bin_filename(void)
245 char filename[PATH_MAX] = {};
246 char proc[PATH_MAX];
248 pid_t pid = getpid();
249 sprintf(proc, "/proc/%d/exe", pid);
250 if (readlink(proc, filename, PATH_MAX) < 0)
251 return NULL;
252 return alloc_string(filename);
255 static char *get_data_dir(char *arg0)
257 char *bin_dir;
258 char *orig;
259 char buf[256];
260 char *dir;
262 if (option_no_data)
263 return NULL;
265 if (option_datadir_str) {
266 if (access(option_datadir_str, R_OK))
267 printf("Warning: %s is not accessible -- ignore.\n",
268 option_datadir_str);
269 else
270 return alloc_string(option_datadir_str);
273 strncpy(buf, "smatch_data/", sizeof(buf));
274 dir = alloc_string(buf);
275 if (!access(dir, R_OK))
276 return dir;
278 orig = read_bin_filename();
279 if (!orig)
280 orig = alloc_string(arg0);
281 bin_dir = dirname(orig);
282 strncpy(buf, bin_dir, 254);
283 free_string(orig);
285 buf[255] = '\0';
286 strncat(buf, "/smatch_data/", 254 - strlen(buf));
287 dir = alloc_string(buf);
288 if (!access(dir, R_OK))
289 return dir;
290 free_string(dir);
291 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
292 dir = alloc_string(buf);
293 if (!access(dir, R_OK))
294 return dir;
296 printf("Warning: %s is not accessible.\n", dir);
297 printf("Use --no-data or --data to suppress this message.\n");
298 return NULL;
301 int main(int argc, char **argv)
303 int i;
304 reg_func func;
306 sm_outfd = stdout;
307 sql_outfd = stdout;
308 caller_info_fd = stdout;
309 parse_args(&argc, &argv);
311 /* this gets set back to zero when we parse the first function */
312 final_pass = 1;
314 data_dir = get_data_dir(argv[0]);
316 allocate_hook_memory();
317 create_function_hook_hash();
318 open_smatch_db();
319 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
320 func = reg_funcs[i].func;
321 /* The script IDs start at 1.
322 0 is used for internal stuff. */
323 if (!option_enable || reg_funcs[i].enabled || !strncmp(reg_funcs[i].name, "register_", 9))
324 func(i);
327 smatch(argc, argv);
328 free_string(data_dir);
329 return 0;