Make smatch installable.
[smatch.git] / smatch.c
blob48c3838ed2d056adba7c81e1079b33ff853c96c9
1 /*
2 * sparse/smatch.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <libgen.h>
13 #include "smatch.h"
15 char *data_dir;
16 int option_no_data = 0;
17 int option_spammy = 0;
19 typedef void (*reg_func) (int id);
20 void register_smatch_extra(int id);
21 void register_smatch_ignore(int id);
22 void register_implications(int id);
23 void register_function_hooks(int id);
24 void register_modification_hooks(int id);
25 void register_containers(int id);
26 void check_debug(int id);
27 void check_assigned_expr(int id);
28 void check_null_deref(int id);
29 void check_overflow(int id);
30 void check_locking(int id);
31 void check_memory(int id);
32 void check_frees_argument(int id);
33 void check_puts_argument(int id);
34 void check_leaks(int id);
35 void check_type(int id);
36 void check_allocation_funcs(int id);
37 void check_err_ptr(int id);
38 void check_err_ptr_deref(int id);
39 void check_balanced(int id);
40 void check_deref_check(int id);
41 void check_hold_dev(int id);
42 /* <- your test goes here */
43 /* void register_template(int id); */
45 static const reg_func reg_funcs[] = {
46 &register_smatch_extra, /* smatch_extra always has to be first */
47 &register_smatch_ignore,
48 &check_debug,
49 &check_assigned_expr,
51 &check_null_deref,
52 &check_overflow,
53 &check_locking,
54 &check_memory,
55 &check_type,
56 &check_allocation_funcs,
57 &check_leaks,
58 &check_frees_argument,
59 &check_puts_argument,
60 &check_err_ptr,
61 &check_err_ptr_deref,
62 &check_balanced,
63 &check_deref_check,
64 &check_hold_dev,
66 /* <- your test goes here */
67 /* &register_template, */
69 &register_modification_hooks,
70 &register_containers,
71 &register_implications, /* implications always has to be last */
72 NULL
75 struct smatch_state *default_state[sizeof(reg_funcs)/sizeof(reg_func)];
77 static void help(void)
79 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
80 printf("--debug: print lots of debug output.\n");
81 printf("--debug-implied: print debug output about implications.\n");
82 printf("--oom <num>: number of mB memory to use before giving up.\n");
83 printf("--no-implied: ignore implications.\n");
84 printf("--assume-loops: assume loops always go through at least once.\n");
85 printf("--known-conditions: don't branch for known conditions.\n");
86 printf("--no-data: do not use the /smatch_data/ directory.\n");
87 printf("--spammy: print superfluous crap.\n");
88 printf("--two-passes: use a two pass system for each function.\n");
89 printf("--help: print this helpfull message.\n");
90 exit(1);
93 void parse_args(int *argcp, char ***argvp)
95 while(*argcp >= 2) {
96 if (!strcmp((*argvp)[1], "--debug")) {
97 debug_states = 1;
98 (*argvp)[1] = (*argvp)[0];
99 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
100 debug_implied_states = 1;
101 (*argvp)[1] = (*argvp)[0];
102 } else if (!strcmp((*argvp)[1], "--oom")) {
103 option_oom_kb = atoi((*argvp)[2]) * 1000;
104 (*argvp)[2] = (*argvp)[0];
105 (*argcp)--;
106 (*argvp)++;
107 } else if (!strcmp((*argvp)[1], "--no-implied")) {
108 option_no_implied = 1;
109 (*argvp)[1] = (*argvp)[0];
110 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
111 option_assume_loops = 1;
112 (*argvp)[1] = (*argvp)[0];
113 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
114 option_known_conditions = 1;
115 (*argvp)[1] = (*argvp)[0];
116 } else if (!strcmp((*argvp)[1], "--no-data")) {
117 option_no_data = 1;
118 (*argvp)[1] = (*argvp)[0];
119 } else if (!strcmp((*argvp)[1], "--spammy")) {
120 option_spammy = 1;
121 (*argvp)[1] = (*argvp)[0];
122 } else if (!strcmp((*argvp)[1], "--two-passes")) {
123 option_two_passes = 1;
124 (*argvp)[1] = (*argvp)[0];
125 } else if (!strcmp((*argvp)[1], "--help")) {
126 help();
127 } else {
128 break;
130 (*argcp)--;
131 (*argvp)++;
135 static char *get_data_dir(char *arg0)
137 char *bin_dir;
138 char buf[256];
139 char *dir;
141 if (option_no_data) {
142 return NULL;
144 bin_dir = dirname(alloc_string(arg0));
145 strncpy(buf, bin_dir, 254);
146 buf[255] = '\0';
147 strncat(buf, "/smatch_data/", 254 - strlen(buf));
148 dir = alloc_string(buf);
149 if (!access(dir, R_OK))
150 return dir;
151 printf("Warning: %s is not accessible.\n", dir);
152 printf("Use --no-data to suppress this message.\n");
153 return NULL;
156 int main(int argc, char **argv)
158 int i;
159 reg_func func;
161 parse_args(&argc, &argv);
163 data_dir = get_data_dir(argv[0]);
165 /* The script IDs start at 1.
166 0 is used for internal stuff. */
167 create_function_hash();
168 for(i = 0; (func = reg_funcs[i]); i++){
169 func(i + 1);
171 register_function_hooks(-1);
173 smatch(argc, argv);
174 free_string(data_dir);
175 return 0;