small clean up.
[smatch.git] / smatch.c
blob265414650f976c9d4fd85b5d0c301c898c5e1dbb
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_initializer_deref(int id);
41 void check_deref_check(int id);
42 void check_bitwise_or(int id);
43 /* <- your test goes here */
44 /* void register_template(int id); */
46 static const reg_func reg_funcs[] = {
47 &register_smatch_extra, /* smatch_extra always has to be first */
48 &register_smatch_ignore,
49 &check_debug,
50 &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_initializer_deref,
64 &check_deref_check,
65 &check_bitwise_or,
67 /* <- your test goes here */
68 /* &register_template, */
70 &register_modification_hooks,
71 &register_containers,
72 &register_implications, /* implications always has to be last */
73 NULL
76 struct smatch_state *default_state[sizeof(reg_funcs)/sizeof(reg_func)];
78 static void help(void)
80 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
81 printf("--debug: print lots of debug output.\n");
82 printf("--debug-implied: print debug output about implications.\n");
83 printf("--oom <num>: number of mB memory to use before giving up.\n");
84 printf("--no-implied: ignore implications.\n");
85 printf("--assume-loops: assume loops always go through at least once.\n");
86 printf("--known-conditions: don't branch for known conditions.\n");
87 printf("--no-data: do not use the /smatch_data/ directory.\n");
88 printf("--spammy: print superfluous crap.\n");
89 printf("--two-passes: use a two pass system for each function.\n");
90 printf("--help: print this helpfull message.\n");
91 exit(1);
94 void parse_args(int *argcp, char ***argvp)
96 while(*argcp >= 2) {
97 if (!strcmp((*argvp)[1], "--debug")) {
98 debug_states = 1;
99 (*argvp)[1] = (*argvp)[0];
100 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
101 debug_implied_states = 1;
102 (*argvp)[1] = (*argvp)[0];
103 } else if (!strcmp((*argvp)[1], "--oom")) {
104 option_oom_kb = atoi((*argvp)[2]) * 1000;
105 (*argvp)[2] = (*argvp)[0];
106 (*argcp)--;
107 (*argvp)++;
108 } else if (!strcmp((*argvp)[1], "--no-implied")) {
109 option_no_implied = 1;
110 (*argvp)[1] = (*argvp)[0];
111 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
112 option_assume_loops = 1;
113 (*argvp)[1] = (*argvp)[0];
114 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
115 option_known_conditions = 1;
116 (*argvp)[1] = (*argvp)[0];
117 } else if (!strcmp((*argvp)[1], "--no-data")) {
118 option_no_data = 1;
119 (*argvp)[1] = (*argvp)[0];
120 } else if (!strcmp((*argvp)[1], "--spammy")) {
121 option_spammy = 1;
122 (*argvp)[1] = (*argvp)[0];
123 } else if (!strcmp((*argvp)[1], "--two-passes")) {
124 option_two_passes = 1;
125 (*argvp)[1] = (*argvp)[0];
126 } else if (!strcmp((*argvp)[1], "--help")) {
127 help();
128 } else {
129 break;
131 (*argcp)--;
132 (*argvp)++;
136 static char *get_data_dir(char *arg0)
138 char *bin_dir;
139 char buf[256];
140 char *dir;
142 if (option_no_data) {
143 return NULL;
145 bin_dir = dirname(alloc_string(arg0));
146 strncpy(buf, bin_dir, 254);
147 buf[255] = '\0';
148 strncat(buf, "/smatch_data/", 254 - strlen(buf));
149 dir = alloc_string(buf);
150 if (!access(dir, R_OK))
151 return dir;
152 printf("Warning: %s is not accessible.\n", dir);
153 printf("Use --no-data to suppress this message.\n");
154 return NULL;
157 int main(int argc, char **argv)
159 int i;
160 reg_func func;
162 parse_args(&argc, &argv);
164 data_dir = get_data_dir(argv[0]);
166 /* The script IDs start at 1.
167 0 is used for internal stuff. */
168 create_function_hash();
169 for(i = 0; (func = reg_funcs[i]); i++){
170 func(i + 1);
172 register_function_hooks(-1);
174 smatch(argc, argv);
175 free_string(data_dir);
176 return 0;