flow: parse more inline functions
[smatch.git] / smatch.c
blobc6ae26b08ec25f1db9109453e225bdee6a603a76
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 char *option_datadir_str;
40 FILE *sm_outfd;
41 FILE *sql_outfd;
42 FILE *caller_info_fd;
44 typedef void (*reg_func) (int id);
45 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
46 static struct reg_func_info {
47 const char *name;
48 reg_func func;
49 int enabled;
50 } reg_funcs[] = {
51 {NULL, NULL},
52 #include "check_list.h"
54 #undef CK
55 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
57 const char *check_name(unsigned short id)
59 if (id >= ARRAY_SIZE(reg_funcs))
60 return "internal";
62 return reg_funcs[id].name;
65 int id_from_name(const char *name)
67 int i;
69 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
70 if (!strcmp(name, reg_funcs[i].name))
71 return i;
73 return 0;
76 static void show_checks(void)
78 int i;
80 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
81 if (!strncmp(reg_funcs[i].name, "check_", 6))
82 printf("%3d. %s\n", i, reg_funcs[i].name);
84 exit(1);
86 static void enable_check(int i)
88 if (1 <= i && i < ARRAY_SIZE(reg_funcs))
89 reg_funcs[i].enabled = 1;
92 static void enable_checks(const char *s)
94 int n = 0, lo = -1, i;
96 do {
97 switch (*s) {
98 case ',':
99 case '\0':
100 if (lo < 0)
101 enable_check(n);
102 else
103 for (i = lo; i <= n; ++i)
104 enable_check(i);
105 lo = -1;
106 n = 0;
107 break;
108 case '-':
109 lo = n;
110 n = 0;
111 break;
112 case '0' ... '9':
113 n = 10*n + (*s - '0');
114 break;
115 default:
116 fprintf(stderr, "invalid character '%c'\n", *s);
117 exit(1);
119 } while (*s++);
122 static void help(void)
124 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
125 printf("--project=<name> or -p=<name>: project specific tests\n");
126 printf("--spammy: print superfluous crap.\n");
127 printf("--info: print info used to fill smatch_data/.\n");
128 printf("--debug: print lots of debug output.\n");
129 printf("--param-mapper: enable param_mapper output.\n");
130 printf("--no-data: do not use the /smatch_data/ directory.\n");
131 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
132 printf("--full-path: print the full pathname.\n");
133 printf("--debug-implied: print debug output about implications.\n");
134 printf("--assume-loops: assume loops always go through at least once.\n");
135 printf("--two-passes: use a two pass system for each function.\n");
136 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
137 printf("--help: print this helpful message.\n");
138 exit(1);
141 static int match_option(const char *arg, const char *option)
143 char *str;
144 char *tmp;
145 int ret = 0;
147 str = malloc(strlen(option) + 3);
148 snprintf(str, strlen(option) + 3, "--%s", option);
149 tmp = str;
150 while (*tmp) {
151 if (*tmp == '_')
152 *tmp = '-';
153 tmp++;
155 if (!strcmp(arg, str))
156 ret = 1;
157 free(str);
158 return ret;
161 #define OPTION(_x) do { \
162 if (!found && match_option((*argvp)[1], #_x)) { \
163 found = 1; \
164 option_##_x = 1; \
165 (*argvp)[1] = (*argvp)[0]; \
167 } while (0)
169 void parse_args(int *argcp, char ***argvp)
171 while (*argcp >= 2) {
172 int found = 0;
173 if (!strcmp((*argvp)[1], "--help"))
174 help();
176 if (!strcmp((*argvp)[1], "--show-checks"))
177 show_checks();
179 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
180 option_project_str = (*argvp)[1] + 10;
181 (*argvp)[1] = (*argvp)[0];
182 found = 1;
184 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
185 option_project_str = (*argvp)[1] + 3;
186 (*argvp)[1] = (*argvp)[0];
187 found = 1;
189 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
190 option_datadir_str = (*argvp)[1] + 7;
191 (*argvp)[1] = (*argvp)[0];
192 found = 1;
194 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
195 option_debug_check = (*argvp)[1] + 8;
196 (*argvp)[1] = (*argvp)[0];
197 found = 1;
199 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
200 trace_variable = (*argvp)[1] + 8;
201 (*argvp)[1] = (*argvp)[0];
202 found = 1;
204 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
205 enable_checks((*argvp)[1] + 9);
206 option_enable = 1;
207 (*argvp)[1] = (*argvp)[0];
208 found = 1;
211 OPTION(spammy);
212 OPTION(info);
213 OPTION(debug);
214 OPTION(debug_implied);
215 OPTION(debug_related);
216 OPTION(assume_loops);
217 OPTION(no_data);
218 OPTION(two_passes);
219 OPTION(full_path);
220 OPTION(param_mapper);
221 OPTION(call_tree);
222 OPTION(file_output);
223 OPTION(time);
224 OPTION(no_db);
225 if (!found)
226 break;
227 (*argcp)--;
228 (*argvp)++;
231 if (strcmp(option_project_str, "smatch_generic") != 0)
232 option_project = PROJ_UNKNOWN;
233 if (strcmp(option_project_str, "kernel") == 0)
234 option_project = PROJ_KERNEL;
235 if (strcmp(option_project_str, "wine") == 0)
236 option_project = PROJ_WINE;
239 static char *get_data_dir(char *arg0)
241 char *bin_dir;
242 char *orig;
243 char buf[256];
244 char *dir;
246 if (option_no_data)
247 return NULL;
249 if (option_datadir_str) {
250 if (access(option_datadir_str, R_OK))
251 printf("Warning: %s is not accessible -- ignore.\n",
252 option_datadir_str);
253 else
254 return alloc_string(option_datadir_str);
257 strncpy(buf, "smatch_data/", sizeof(buf));
258 dir = alloc_string(buf);
259 if (!access(dir, R_OK))
260 return dir;
262 orig = alloc_string(arg0);
263 bin_dir = dirname(orig);
264 strncpy(buf, bin_dir, 254);
265 free_string(orig);
267 buf[255] = '\0';
268 strncat(buf, "/smatch_data/", 254 - strlen(buf));
269 dir = alloc_string(buf);
270 if (!access(dir, R_OK))
271 return dir;
272 free_string(dir);
273 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
274 dir = alloc_string(buf);
275 if (!access(dir, R_OK))
276 return dir;
278 printf("Warning: %s is not accessible.\n", dir);
279 printf("Use --no-data or --data to suppress this message.\n");
280 return NULL;
283 int main(int argc, char **argv)
285 int i;
286 reg_func func;
288 sm_outfd = stdout;
289 sql_outfd = stdout;
290 caller_info_fd = stdout;
291 parse_args(&argc, &argv);
293 /* this gets set back to zero when we parse the first function */
294 final_pass = 1;
296 data_dir = get_data_dir(argv[0]);
298 allocate_hook_memory();
299 create_function_hook_hash();
300 open_smatch_db();
301 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
302 func = reg_funcs[i].func;
303 /* The script IDs start at 1.
304 0 is used for internal stuff. */
305 if (!option_enable || reg_funcs[i].enabled || !strncmp(reg_funcs[i].name, "register_", 9))
306 func(i);
309 smatch(argc, argv);
310 free_string(data_dir);
311 return 0;