Makefile: use quiet link to link smatch
[smatch.git] / smatch.c
bloba56d097c6092fb773998f0c07be47f7bd765a97d
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 *)"";
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;
42 typedef void (*reg_func) (int id);
43 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
44 static struct reg_func_info {
45 const char *name;
46 reg_func func;
47 int enabled;
48 } reg_funcs[] = {
49 {NULL, NULL},
50 #include "check_list.h"
52 #undef CK
53 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
55 const char *check_name(unsigned short id)
57 if (id >= ARRAY_SIZE(reg_funcs))
58 return "internal";
60 return reg_funcs[id].name;
63 int id_from_name(const char *name)
65 int i;
67 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
68 if (!strcmp(name, reg_funcs[i].name))
69 return i;
71 return 0;
74 static void show_checks(void)
76 int i;
78 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
79 if (!strncmp(reg_funcs[i].name, "check_", 6))
80 printf("%3d. %s\n", i, reg_funcs[i].name);
82 exit(1);
84 static void enable_check(int i)
86 if (1 <= i && i < ARRAY_SIZE(reg_funcs))
87 reg_funcs[i].enabled = 1;
90 static void enable_checks(const char *s)
92 int n = 0, lo = -1, i;
94 do {
95 switch (*s) {
96 case ',':
97 case '\0':
98 if (lo < 0)
99 enable_check(n);
100 else
101 for (i = lo; i <= n; ++i)
102 enable_check(i);
103 lo = -1;
104 n = 0;
105 break;
106 case '-':
107 lo = n;
108 n = 0;
109 break;
110 case '0' ... '9':
111 n = 10*n + (*s - '0');
112 break;
113 default:
114 fprintf(stderr, "invalid character '%c'\n", *s);
115 exit(1);
117 } while (*s++);
120 static void help(void)
122 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
123 printf("--project=<name> or -p=<name>: project specific tests\n");
124 printf("--spammy: print superfluous crap.\n");
125 printf("--info: print info used to fill smatch_data/.\n");
126 printf("--debug: print lots of debug output.\n");
127 printf("--param-mapper: enable param_mapper output.\n");
128 printf("--no-data: do not use the /smatch_data/ directory.\n");
129 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
130 printf("--full-path: print the full pathname.\n");
131 printf("--debug-implied: print debug output about implications.\n");
132 printf("--assume-loops: assume loops always go through at least once.\n");
133 printf("--two-passes: use a two pass system for each function.\n");
134 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
135 printf("--help: print this helpful message.\n");
136 exit(1);
139 static int match_option(const char *arg, const char *option)
141 char *str;
142 char *tmp;
143 int ret = 0;
145 str = malloc(strlen(option) + 3);
146 snprintf(str, strlen(option) + 3, "--%s", option);
147 tmp = str;
148 while (*tmp) {
149 if (*tmp == '_')
150 *tmp = '-';
151 tmp++;
153 if (!strcmp(arg, str))
154 ret = 1;
155 free(str);
156 return ret;
159 #define OPTION(_x) do { \
160 if (!found && match_option((*argvp)[1], #_x)) { \
161 found = 1; \
162 option_##_x = 1; \
163 (*argvp)[1] = (*argvp)[0]; \
165 } while (0)
167 void parse_args(int *argcp, char ***argvp)
169 while (*argcp >= 2) {
170 int found = 0;
171 if (!strcmp((*argvp)[1], "--help"))
172 help();
174 if (!strcmp((*argvp)[1], "--show-checks"))
175 show_checks();
177 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
178 option_project_str = (*argvp)[1] + 10;
179 (*argvp)[1] = (*argvp)[0];
180 found = 1;
182 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
183 option_project_str = (*argvp)[1] + 3;
184 (*argvp)[1] = (*argvp)[0];
185 found = 1;
187 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
188 option_datadir_str = (*argvp)[1] + 7;
189 (*argvp)[1] = (*argvp)[0];
190 found = 1;
192 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
193 option_debug_check = (*argvp)[1] + 8;
194 (*argvp)[1] = (*argvp)[0];
195 found = 1;
197 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
198 trace_variable = (*argvp)[1] + 8;
199 (*argvp)[1] = (*argvp)[0];
200 found = 1;
202 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
203 enable_checks((*argvp)[1] + 9);
204 option_enable = 1;
205 (*argvp)[1] = (*argvp)[0];
206 found = 1;
209 OPTION(spammy);
210 OPTION(info);
211 OPTION(debug);
212 OPTION(debug_implied);
213 OPTION(debug_related);
214 OPTION(assume_loops);
215 OPTION(no_data);
216 OPTION(two_passes);
217 OPTION(full_path);
218 OPTION(param_mapper);
219 OPTION(call_tree);
220 OPTION(file_output);
221 OPTION(time);
222 OPTION(no_db);
223 if (!found)
224 break;
225 (*argcp)--;
226 (*argvp)++;
229 if (!strcmp(option_project_str, "kernel"))
230 option_project = PROJ_KERNEL;
231 if (!strcmp(option_project_str, "wine"))
232 option_project = PROJ_WINE;
235 static char *get_data_dir(char *arg0)
237 char *bin_dir;
238 char *orig;
239 char buf[256];
240 char *dir;
242 if (option_no_data)
243 return NULL;
245 if (option_datadir_str) {
246 if (access(option_datadir_str, R_OK))
247 printf("Warning: %s is not accessible -- ignore.\n",
248 option_datadir_str);
249 else
250 return alloc_string(option_datadir_str);
253 orig = alloc_string(arg0);
254 bin_dir = dirname(orig);
255 strncpy(buf, bin_dir, 254);
256 free_string(orig);
258 buf[255] = '\0';
259 strncat(buf, "/smatch_data/", 254 - strlen(buf));
260 dir = alloc_string(buf);
261 if (!access(dir, R_OK))
262 return dir;
263 free_string(dir);
264 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
265 dir = alloc_string(buf);
266 if (!access(dir, R_OK))
267 return dir;
269 printf("Warning: %s is not accessible.\n", dir);
270 printf("Use --no-data or --data to suppress this message.\n");
271 return NULL;
274 int main(int argc, char **argv)
276 int i;
277 reg_func func;
279 sm_outfd = stdout;
280 parse_args(&argc, &argv);
282 /* this gets set back to zero when we parse the first function */
283 final_pass = 1;
285 data_dir = get_data_dir(argv[0]);
287 allocate_hook_memory();
288 create_function_hook_hash();
289 open_smatch_db();
290 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
291 func = reg_funcs[i].func;
292 /* The script IDs start at 1.
293 0 is used for internal stuff. */
294 if (!option_enable || reg_funcs[i].enabled || !strncmp(reg_funcs[i].name, "register_", 9))
295 func(i);
298 smatch(argc, argv);
299 free_string(data_dir);
300 return 0;