assigned_expr: fix memory leak
[smatch.git] / smatch.c
blobe77901d0e6d6660b86d20b0dc7f21de2f0e75428
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;
27 typedef void (*reg_func) (int id);
28 #define CK(_x) {.name = #_x, .func = &_x},
29 static struct reg_func_info {
30 const char *name;
31 reg_func func;
32 } reg_funcs[] = {
33 #include "check_list.h"
35 #undef CK
36 int num_checks = ARRAY_SIZE(reg_funcs);
38 const char *check_name(unsigned short id)
40 if (id > ARRAY_SIZE(reg_funcs)) {
41 return "internal";
43 return reg_funcs[id - 1].name;
46 int id_from_name(const char *name)
48 int i;
50 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
51 if (!strcmp(name, reg_funcs[i].name))
52 return i + 1;
54 return 0;
57 static void help(void)
59 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
60 printf("--project=<name> or -p=<name>: project specific tests\n");
61 printf("--spammy: print superfluous crap.\n");
62 printf("--info: print info used to fill smatch_data/.\n");
63 printf("--debug: print lots of debug output.\n");
64 printf("--param-mapper: enable param_mapper output.\n");
65 printf("--no-data: do not use the /smatch_data/ directory.\n");
66 printf("--full-path: print the full pathname.\n");
67 printf("--debug-implied: print debug output about implications.\n");
68 printf("--oom <num>: number of mB memory to use before giving up.\n");
69 printf("--no-implied: ignore implications.\n");
70 printf("--assume-loops: assume loops always go through at least once.\n");
71 printf("--known-conditions: don't branch for known conditions.\n");
72 printf("--two-passes: use a two pass system for each function.\n");
73 printf("--help: print this helpfull message.\n");
74 exit(1);
77 static int match_option(const char *arg, const char *option)
79 char *str;
80 char *tmp;
81 int ret = 0;
83 str = malloc(strlen(option) + 3);
84 snprintf(str, strlen(option) + 3, "--%s", option);
85 tmp = str;
86 while (*tmp) {
87 if (*tmp == '_')
88 *tmp = '-';
89 tmp++;
91 if (!strcmp(arg, str))
92 ret = 1;
93 free(str);
94 return ret;
97 #define OPTION(_x) do { \
98 if (!found && match_option((*argvp)[1], #_x)) { \
99 found = 1; \
100 option_##_x = 1; \
101 (*argvp)[1] = (*argvp)[0]; \
103 } while (0)
105 void parse_args(int *argcp, char ***argvp)
107 while(*argcp >= 2) {
108 int found = 0;
109 if (!strcmp((*argvp)[1], "--help")) {
110 help();
112 if (!found && !strncmp((*argvp)[1], "--project=", 10)) {
113 option_project_str = (*argvp)[1] + 10;
114 (*argvp)[1] = (*argvp)[0];
115 found = 1;
117 if (!found && !strncmp((*argvp)[1], "-p=", 3)) {
118 option_project_str = (*argvp)[1] + 3;
119 (*argvp)[1] = (*argvp)[0];
120 found = 1;
122 if (!found && !strcmp((*argvp)[1], "--oom")) {
123 option_oom_kb = atoi((*argvp)[2]) * 1000;
124 (*argvp)[2] = (*argvp)[0];
125 (*argcp)--;
126 (*argvp)++;
128 OPTION(spammy);
129 OPTION(info);
130 OPTION(debug);
131 OPTION(debug_implied);
132 OPTION(no_implied);
133 OPTION(assume_loops);
134 OPTION(known_conditions);
135 OPTION(no_data);
136 OPTION(two_passes);
137 OPTION(full_path);
138 OPTION(param_mapper);
139 OPTION(call_tree);
140 if (!found)
141 break;
142 (*argcp)--;
143 (*argvp)++;
146 if (!strcmp(option_project_str, "kernel"))
147 option_project = PROJ_KERNEL;
148 if (!strcmp(option_project_str, "wine"))
149 option_project = PROJ_WINE;
152 static char *get_data_dir(char *arg0)
154 char *bin_dir;
155 char buf[256];
156 char *dir;
158 if (option_no_data) {
159 return NULL;
161 bin_dir = dirname(alloc_string(arg0));
162 strncpy(buf, bin_dir, 254);
163 buf[255] = '\0';
164 strncat(buf, "/smatch_data/", 254 - strlen(buf));
165 dir = alloc_string(buf);
166 if (!access(dir, R_OK))
167 return dir;
168 free_string(dir);
169 snprintf(buf, 254, "%s/smatch_data/", SMATCHDATADIR);
170 dir = alloc_string(buf);
171 if (!access(dir, R_OK))
172 return dir;
173 printf("Warning: %s is not accessible.\n", dir);
174 printf("Use --no-data to suppress this message.\n");
175 return NULL;
178 int main(int argc, char **argv)
180 int i;
181 reg_func func;
183 parse_args(&argc, &argv);
185 data_dir = get_data_dir(argv[0]);
187 create_function_hook_hash();
188 for(i = 0; i < ARRAY_SIZE(reg_funcs); i++){
189 func = reg_funcs[i].func;
190 /* The script IDs start at 1.
191 0 is used for internal stuff. */
192 func(i + 1);
194 register_function_hooks(-1);
196 smatch(argc, argv);
197 free_string(data_dir);
198 return 0;