comparison: stop caring so much about wrapping (it's rare)
[smatch.git] / smatch.c
blobfe1114cbce40ab53c5e51287a60518c755cb90e9
2 /*
3 * sparse/smatch.c
5 * Copyright (C) 2006 Dan Carpenter.
7 * Licensed under the Open Software License version 1.1
9 */
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <libgen.h>
14 #include "smatch.h"
15 #include "check_list.h"
17 char *option_project_str = (char *)"";
18 enum project_type option_project = PROJ_NONE;
19 char *data_dir;
20 int option_no_data = 0;
21 int option_spammy = 0;
22 int option_info = 0;
23 int option_full_path = 0;
24 int option_param_mapper = 0;
25 int option_call_tree = 0;
26 int option_no_db = 0;
27 int option_debug_related;
28 int option_file_output;
29 int option_time;
30 char *option_datadir_str;
31 FILE *sm_outfd;
33 typedef void (*reg_func) (int id);
34 #define CK(_x) {.name = #_x, .func = &_x},
35 static struct reg_func_info {
36 const char *name;
37 reg_func func;
38 } reg_funcs[] = {
39 #include "check_list.h"
41 #undef CK
42 int num_checks = ARRAY_SIZE(reg_funcs);
44 const char *check_name(unsigned short id)
46 if (id > ARRAY_SIZE(reg_funcs))
47 return "internal";
49 return reg_funcs[id - 1].name;
52 int id_from_name(const char *name)
54 int i;
56 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
57 if (!strcmp(name, reg_funcs[i].name))
58 return i + 1;
60 return 0;
63 static void help(void)
65 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
66 printf("--project=<name> or -p=<name>: project specific tests\n");
67 printf("--spammy: print superfluous crap.\n");
68 printf("--info: print info used to fill smatch_data/.\n");
69 printf("--debug: print lots of debug output.\n");
70 printf("--param-mapper: enable param_mapper output.\n");
71 printf("--no-data: do not use the /smatch_data/ directory.\n");
72 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
73 printf("--full-path: print the full pathname.\n");
74 printf("--debug-implied: print debug output about implications.\n");
75 printf("--no-implied: ignore implications.\n");
76 printf("--assume-loops: assume loops always go through at least once.\n");
77 printf("--known-conditions: don't branch for known conditions.\n");
78 printf("--two-passes: use a two pass system for each function.\n");
79 printf("--file-output: instead of printing stdout, print to \"file.c.smatch_out\".\n");
80 printf("--help: print this helpful message.\n");
81 exit(1);
84 static int match_option(const char *arg, const char *option)
86 char *str;
87 char *tmp;
88 int ret = 0;
90 str = malloc(strlen(option) + 3);
91 snprintf(str, strlen(option) + 3, "--%s", option);
92 tmp = str;
93 while (*tmp) {
94 if (*tmp == '_')
95 *tmp = '-';
96 tmp++;
98 if (!strcmp(arg, str))
99 ret = 1;
100 free(str);
101 return ret;
104 #define OPTION(_x) do { \
105 if (!found && match_option((*argvp)[1], #_x)) { \
106 found = 1; \
107 option_##_x = 1; \
108 (*argvp)[1] = (*argvp)[0]; \
110 } while (0)
112 void parse_args(int *argcp, char ***argvp)
114 while (*argcp >= 2) {
115 int found = 0;
116 if (!strcmp((*argvp)[1], "--help"))
117 help();
119 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
120 option_project_str = (*argvp)[1] + 10;
121 (*argvp)[1] = (*argvp)[0];
122 found = 1;
124 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
125 option_project_str = (*argvp)[1] + 3;
126 (*argvp)[1] = (*argvp)[0];
127 found = 1;
129 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
130 option_datadir_str = (*argvp)[1] + 7;
131 (*argvp)[1] = (*argvp)[0];
132 found = 1;
135 OPTION(spammy);
136 OPTION(info);
137 OPTION(debug);
138 OPTION(debug_implied);
139 OPTION(debug_related);
140 OPTION(no_implied);
141 OPTION(assume_loops);
142 OPTION(known_conditions);
143 OPTION(no_data);
144 OPTION(two_passes);
145 OPTION(full_path);
146 OPTION(param_mapper);
147 OPTION(call_tree);
148 OPTION(file_output);
149 OPTION(time);
150 if (!found)
151 break;
152 (*argcp)--;
153 (*argvp)++;
156 if (!strcmp(option_project_str, "kernel"))
157 option_project = PROJ_KERNEL;
158 if (!strcmp(option_project_str, "wine"))
159 option_project = PROJ_WINE;
162 static char *get_data_dir(char *arg0)
164 char *bin_dir;
165 char *orig;
166 char buf[256];
167 char *dir;
169 if (option_no_data)
170 return NULL;
172 if (option_datadir_str) {
173 if (access(option_datadir_str, R_OK))
174 printf("Warning: %s is not accessible -- ignore.\n",
175 option_datadir_str);
176 else
177 return alloc_string(option_datadir_str);
180 orig = alloc_string(arg0);
181 bin_dir = dirname(orig);
182 strncpy(buf, bin_dir, 254);
183 free_string(orig);
185 buf[255] = '\0';
186 strncat(buf, "/smatch_data/", 254 - strlen(buf));
187 dir = alloc_string(buf);
188 if (!access(dir, R_OK))
189 return dir;
190 free_string(dir);
191 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
192 dir = alloc_string(buf);
193 if (!access(dir, R_OK))
194 return dir;
196 printf("Warning: %s is not accessible.\n", dir);
197 printf("Use --no-data or --data to suppress this message.\n");
198 return NULL;
201 int main(int argc, char **argv)
203 int i;
204 reg_func func;
206 sm_outfd = stdout;
207 parse_args(&argc, &argv);
209 /* this gets set back to zero when we parse the first function */
210 final_pass = 1;
212 data_dir = get_data_dir(argv[0]);
214 create_function_hook_hash();
215 open_smatch_db();
216 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
217 func = reg_funcs[i].func;
218 /* The script IDs start at 1.
219 0 is used for internal stuff. */
220 func(i + 1);
223 smatch(argc, argv);
224 free_string(data_dir);
225 return 0;