comparison: remove any "+- 0" operations
[smatch.git] / smatch.c
bloba167dec6b0bf9bde267d80013bd760288b9072f2
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 = (char *)"";
26 char *option_project_str = (char *)"smatch_generic";
27 static char *option_db_file = (char *)"smatch_db.sqlite";
28 enum project_type option_project = PROJ_NONE;
29 char *bin_dir;
30 char *data_dir;
31 int option_no_data = 0;
32 int option_spammy = 0;
33 int option_print_names;
34 int option_info = 0;
35 int option_full_path = 0;
36 int option_call_tree = 0;
37 int option_no_db = 0;
38 int option_enable = 0;
39 int option_disable = 0;
40 int option_file_output;
41 int option_time;
42 int option_mem;
43 char *option_datadir_str;
44 int option_fatal_checks;
45 int option_succeed;
47 FILE *sm_outfd;
48 FILE *sql_outfd;
49 FILE *caller_info_fd;
51 int sm_nr_errors;
52 int sm_nr_checks;
54 bool __silence_warnings_for_stmt;
56 typedef void (*reg_func) (int id);
57 #define CK(_x) {.name = #_x, .func = &_x, .enabled = 0},
58 static struct reg_func_info {
59 const char *name;
60 reg_func func;
61 int enabled;
62 } reg_funcs[] = {
63 {NULL, NULL},
64 #include "check_list.h"
66 #undef CK
67 int num_checks = ARRAY_SIZE(reg_funcs) - 1;
69 const char *check_name(unsigned short id)
71 if (id >= ARRAY_SIZE(reg_funcs))
72 return "internal";
74 return reg_funcs[id].name;
77 int id_from_name(const char *name)
79 int i;
81 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
82 if (!strcmp(name, reg_funcs[i].name))
83 return i;
85 return 0;
88 static void show_checks(void)
90 int i;
92 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
93 if (!strncmp(reg_funcs[i].name, "check_", 6))
94 printf("%3d. %s\n", i, reg_funcs[i].name);
96 exit(0);
99 static void enable_disable_checks(char *s, bool enable)
101 char buf[128];
102 char *next;
103 int i;
105 do {
106 next = strchr(s, ',');
107 if (next) {
108 *next = '\0';
109 next++;
111 if (*s == '\0')
112 return;
113 if (strncmp(s, "check_", 6) == 0)
114 snprintf(buf, sizeof(buf), "%s", s);
115 else
116 snprintf(buf, sizeof(buf), "check_%s", s);
119 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
120 if (strcmp(reg_funcs[i].name, buf) == 0) {
121 reg_funcs[i].enabled = (enable == true) ? 1 : -1;
122 break;
126 if (i == ARRAY_SIZE(reg_funcs))
127 sm_fatal("'%s' not found", s);
129 } while ((s = next));
132 static void help(void)
134 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
135 printf("--project=<name> or -p=<name>: project specific tests\n");
136 printf("--succeed: don't exit with an error\n");
137 printf("--spammy: print superfluous crap.\n");
138 printf("--info: print info used to fill smatch_data/.\n");
139 printf("--debug: print lots of debug output.\n");
140 printf("--no-data: do not use the /smatch_data/ directory.\n");
141 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
142 printf("--full-path: print the full pathname.\n");
143 printf("--debug-implied: print debug output about implications.\n");
144 printf("--assume-loops: assume loops always go through at least once.\n");
145 printf("--two-passes: use a two pass system for each function.\n");
146 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
147 printf("--fatal-checks: check output is treated as an error.\n");
148 printf("--help: print this helpful message.\n");
149 exit(1);
152 static int match_option(const char *arg, const char *option)
154 char *str;
155 char *tmp;
156 int ret = 0;
158 str = malloc(strlen(option) + 3);
159 snprintf(str, strlen(option) + 3, "--%s", option);
160 tmp = str;
161 while (*tmp) {
162 if (*tmp == '_')
163 *tmp = '-';
164 tmp++;
166 if (!strcmp(arg, str))
167 ret = 1;
168 free(str);
169 return ret;
172 #define OPTION(_x) do { \
173 if (!found && match_option((*argvp)[1], #_x)) { \
174 found = 1; \
175 option_##_x = 1; \
176 (*argvp)[1] = (*argvp)[0]; \
178 } while (0)
180 void parse_args(int *argcp, char ***argvp)
182 while (*argcp >= 2) {
183 int found = 0;
184 if (!strcmp((*argvp)[1], "--help"))
185 help();
187 if (!strcmp((*argvp)[1], "--show-checks"))
188 show_checks();
190 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
191 option_project_str = (*argvp)[1] + 10;
192 (*argvp)[1] = (*argvp)[0];
193 found = 1;
195 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
196 option_project_str = (*argvp)[1] + 3;
197 (*argvp)[1] = (*argvp)[0];
198 found = 1;
200 if (!found && !strncmp((*argvp)[1], "--db-file=", 10)) {
201 option_db_file = (*argvp)[1] + 10;
202 (*argvp)[1] = (*argvp)[0];
203 found = 1;
205 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
206 option_datadir_str = (*argvp)[1] + 7;
207 (*argvp)[1] = (*argvp)[0];
208 found = 1;
210 if (!found && !strncmp((*argvp)[1], "--debug=", 8)) {
211 option_debug_check = (*argvp)[1] + 8;
212 (*argvp)[1] = (*argvp)[0];
213 found = 1;
215 if (!found && strncmp((*argvp)[1], "--trace=", 8) == 0) {
216 trace_variable = (*argvp)[1] + 8;
217 (*argvp)[1] = (*argvp)[0];
218 found = 1;
220 if (!found && strncmp((*argvp)[1], "--enable=", 9) == 0) {
221 enable_disable_checks((*argvp)[1] + 9, 1);
222 option_enable = 1;
223 (*argvp)[1] = (*argvp)[0];
224 found = 1;
226 if (!found && strncmp((*argvp)[1], "--disable=", 10) == 0) {
227 enable_disable_checks((*argvp)[1] + 10, 0);
228 option_enable = 1;
229 option_disable = 1;
230 (*argvp)[1] = (*argvp)[0];
231 found = 1;
234 OPTION(fatal_checks);
235 OPTION(spammy);
236 OPTION(info);
237 OPTION(debug);
238 OPTION(assume_loops);
239 OPTION(no_data);
240 OPTION(two_passes);
241 OPTION(full_path);
242 OPTION(call_tree);
243 OPTION(file_output);
244 OPTION(time);
245 OPTION(mem);
246 OPTION(no_db);
247 OPTION(succeed);
248 OPTION(print_names);
249 if (!found)
250 break;
251 (*argcp)--;
252 (*argvp)++;
255 if (strcmp(option_project_str, "smatch_generic") != 0)
256 option_project = PROJ_UNKNOWN;
258 if (strcmp(option_project_str, "kernel") == 0)
259 option_project = PROJ_KERNEL;
260 else if (strcmp(option_project_str, "wine") == 0)
261 option_project = PROJ_WINE;
262 else if (strcmp(option_project_str, "illumos_kernel") == 0)
263 option_project = PROJ_ILLUMOS_KERNEL;
264 else if (strcmp(option_project_str, "illumos_user") == 0)
265 option_project = PROJ_ILLUMOS_USER;
268 static char *read_bin_filename(void)
270 char filename[PATH_MAX] = {};
271 char proc[PATH_MAX];
273 pid_t pid = getpid();
274 sprintf(proc, "/proc/%d/exe", pid);
275 if (readlink(proc, filename, PATH_MAX) < 0)
276 return NULL;
277 return alloc_string(filename);
280 static char *get_bin_dir(char *arg0)
282 char *orig;
284 orig = read_bin_filename();
285 if (!orig)
286 orig = alloc_string(arg0);
287 return dirname(orig);
290 static char *get_data_dir(char *arg0)
292 char buf[256];
293 char *dir;
295 if (option_no_data)
296 return NULL;
298 if (option_datadir_str) {
299 if (access(option_datadir_str, R_OK))
300 sm_warning("%s is not accessible -- ignored.",
301 option_datadir_str);
302 else
303 return alloc_string(option_datadir_str);
306 strncpy(buf, "smatch_data/", sizeof(buf));
307 dir = alloc_string(buf);
308 if (!access(dir, R_OK))
309 return dir;
311 strncpy(buf, bin_dir, 254);
313 buf[255] = '\0';
314 strncat(buf, "/smatch_data/", 254 - strlen(buf));
315 dir = alloc_string(buf);
316 if (!access(dir, R_OK))
317 return dir;
318 free_string(dir);
319 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
320 dir = alloc_string(buf);
321 if (!access(dir, R_OK))
322 return dir;
324 sm_warning("%s is not accessible.", dir);
325 sm_warning("Use --no-data or --data to suppress this message.");
326 return NULL;
329 int main(int argc, char **argv)
331 struct string_list *filelist = NULL;
332 int i;
333 reg_func func;
335 sm_outfd = stdout;
336 sql_outfd = stdout;
337 caller_info_fd = stdout;
339 parse_args(&argc, &argv);
341 if (argc < 2)
342 help();
344 /* this gets set back to zero when we parse the first function */
345 final_pass = 1;
347 bin_dir = get_bin_dir(argv[0]);
348 data_dir = get_data_dir(argv[0]);
350 allocate_hook_memory();
351 allocate_dynamic_states_array(num_checks);
352 allocate_tracker_array(num_checks);
353 create_function_hook_hash();
354 open_smatch_db(option_db_file);
355 sparse_initialize(argc, argv, &filelist);
356 alloc_valid_ptr_rl();
358 for (i = 1; i < ARRAY_SIZE(reg_funcs); i++) {
359 func = reg_funcs[i].func;
360 /* The script IDs start at 1.
361 0 is used for internal stuff. */
362 if (!option_enable || reg_funcs[i].enabled == 1 ||
363 (option_disable && reg_funcs[i].enabled != -1) ||
364 strncmp(reg_funcs[i].name, "register_", 9) == 0)
365 func(i);
368 smatch(filelist);
369 free_string(data_dir);
371 if (option_succeed)
372 return 0;
373 if (sm_nr_errors > 0)
374 return 1;
375 if (sm_nr_checks > 0 && option_fatal_checks)
376 return 1;
377 return 0;