flow: tweak the ->pos setting for fake assignments
[smatch.git] / smatch.c
blob2e4a59baf1b33f55befc3e33e6cb031589dcbfd0
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_print_returns = 0;
26 int option_call_tree = 0;
27 int option_no_db = 0;
28 char *option_datadir_str;
30 typedef void (*reg_func) (int id);
31 #define CK(_x) {.name = #_x, .func = &_x},
32 static struct reg_func_info {
33 const char *name;
34 reg_func func;
35 } reg_funcs[] = {
36 #include "check_list.h"
38 #undef CK
39 int num_checks = ARRAY_SIZE(reg_funcs);
41 const char *check_name(unsigned short id)
43 if (id > ARRAY_SIZE(reg_funcs)) {
44 return "internal";
46 return reg_funcs[id - 1].name;
49 int id_from_name(const char *name)
51 int i;
53 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
54 if (!strcmp(name, reg_funcs[i].name))
55 return i + 1;
57 return 0;
60 static void help(void)
62 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
63 printf("--project=<name> or -p=<name>: project specific tests\n");
64 printf("--spammy: print superfluous crap.\n");
65 printf("--info: print info used to fill smatch_data/.\n");
66 printf("--debug: print lots of debug output.\n");
67 printf("--param-mapper: enable param_mapper output.\n");
68 printf("--no-data: do not use the /smatch_data/ directory.\n");
69 printf("--data=<dir>: overwrite path to default smatch data directory.\n");
70 printf("--full-path: print the full pathname.\n");
71 printf("--debug-implied: print debug output about implications.\n");
72 printf("--no-implied: ignore implications.\n");
73 printf("--assume-loops: assume loops always go through at least once.\n");
74 printf("--known-conditions: don't branch for known conditions.\n");
75 printf("--two-passes: use a two pass system for each function.\n");
76 printf("--help: print this helpfull message.\n");
77 exit(1);
80 static int match_option(const char *arg, const char *option)
82 char *str;
83 char *tmp;
84 int ret = 0;
86 str = malloc(strlen(option) + 3);
87 snprintf(str, strlen(option) + 3, "--%s", option);
88 tmp = str;
89 while (*tmp) {
90 if (*tmp == '_')
91 *tmp = '-';
92 tmp++;
94 if (!strcmp(arg, str))
95 ret = 1;
96 free(str);
97 return ret;
100 #define OPTION(_x) do { \
101 if (!found && match_option((*argvp)[1], #_x)) { \
102 found = 1; \
103 option_##_x = 1; \
104 (*argvp)[1] = (*argvp)[0]; \
106 } while (0)
108 void parse_args(int *argcp, char ***argvp)
110 while(*argcp >= 2) {
111 int found = 0;
112 if (!strcmp((*argvp)[1], "--help")) {
113 help();
115 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
116 option_project_str = (*argvp)[1] + 10;
117 (*argvp)[1] = (*argvp)[0];
118 found = 1;
120 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
121 option_project_str = (*argvp)[1] + 3;
122 (*argvp)[1] = (*argvp)[0];
123 found = 1;
125 if (!found && !strncmp((*argvp)[1], "--data=", 7)) {
126 option_datadir_str = (*argvp)[1] + 7;
127 (*argvp)[1] = (*argvp)[0];
128 found = 1;
131 OPTION(spammy);
132 OPTION(info);
133 OPTION(debug);
134 OPTION(debug_implied);
135 OPTION(no_implied);
136 OPTION(assume_loops);
137 OPTION(known_conditions);
138 OPTION(no_data);
139 OPTION(two_passes);
140 OPTION(full_path);
141 OPTION(param_mapper);
142 OPTION(print_returns);
143 OPTION(call_tree);
144 if (!found)
145 break;
146 (*argcp)--;
147 (*argvp)++;
150 if (!strcmp(option_project_str, "kernel"))
151 option_project = PROJ_KERNEL;
152 if (!strcmp(option_project_str, "wine"))
153 option_project = PROJ_WINE;
156 static char *get_data_dir(char *arg0)
158 char *bin_dir;
159 char *orig;
160 char buf[256];
161 char *dir;
163 if (option_no_data) {
164 return NULL;
167 if (option_datadir_str) {
168 if (access(option_datadir_str, R_OK))
169 printf("Warning: %s is not accessible -- ignore.\n",
170 option_datadir_str);
171 else
172 return alloc_string(option_datadir_str);
175 orig = alloc_string(arg0);
176 bin_dir = dirname(orig);
177 strncpy(buf, bin_dir, 254);
178 free_string(orig);
180 buf[255] = '\0';
181 strncat(buf, "/smatch_data/", 254 - strlen(buf));
182 dir = alloc_string(buf);
183 if (!access(dir, R_OK))
184 return dir;
185 free_string(dir);
186 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
187 dir = alloc_string(buf);
188 if (!access(dir, R_OK))
189 return dir;
191 printf("Warning: %s is not accessible.\n", dir);
192 printf("Use --no-data or --data to suppress this message.\n");
193 return NULL;
196 int main(int argc, char **argv)
198 int i;
199 reg_func func;
201 parse_args(&argc, &argv);
203 data_dir = get_data_dir(argv[0]);
205 create_function_hook_hash();
206 open_smatch_db();
207 for (i = 0; i < ARRAY_SIZE(reg_funcs); i++) {
208 func = reg_funcs[i].func;
209 /* The script IDs start at 1.
210 0 is used for internal stuff. */
211 func(i + 1);
213 register_function_hooks(-1);
215 smatch(argc, argv);
216 free_string(data_dir);
217 return 0;