Rename smatch_extra_helper.c to smatch_ranges.c
[smatch.git] / smatch.c
blobb3dcd2d90fba9eb87729294943ba8be3fb4c3b65
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;
18 typedef void (*reg_func) (int id);
19 void register_smatch_extra(int id);
20 void register_smatch_ignore(int id);
21 void register_implications(int id);
22 void register_function_hooks(int id);
23 void check_null_deref(int id);
24 void check_overflow(int id);
25 void check_locking(int id);
26 void check_memory(int id);
27 void check_frees_argument(int id);
28 void check_puts_argument(int id);
29 void check_leaks(int id);
30 void check_type(int id);
31 void check_allocation_funcs(int id);
32 void check_err_ptr(int id);
33 void check_err_ptr_deref(int id);
34 /* <- your test goes here */
35 /* void register_template(int id); */
37 static const reg_func reg_funcs[] = {
38 &register_smatch_extra, /* smatch_extra always has to be first */
39 &register_smatch_ignore,
40 &check_null_deref,
41 &check_overflow,
42 &check_locking,
43 &check_memory,
44 // &check_type,
45 // &check_allocation_funcs,
46 // &check_leaks,
47 // &check_frees_argument,
48 // &check_puts_argument,
49 // &check_err_ptr,
50 &check_err_ptr_deref,
52 /* <- your test goes here */
53 /* &register_template, */
55 &register_implications, /* implications always has to be last */
56 NULL
59 static void help(void)
61 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
62 printf("--debug: print lots of debug output.\n");
63 printf("--debug-implied: print debug output about implications.\n");
64 printf("--no-implied: ignore implications.\n");
65 printf("--assume-loops: assume loops always go through at least once.\n");
66 printf("--known-conditions: don't branch for known conditions.\n");
67 printf("--no-data: do not use the /smatch_data/ directory.\n");
68 printf("--help: print this helpfull message.\n");
69 exit(1);
72 void parse_args(int *argcp, char ***argvp)
74 while(*argcp >= 2) {
75 if (!strcmp((*argvp)[1], "--debug")) {
76 debug_states = 1;
77 (*argvp)[1] = (*argvp)[0];
78 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
79 debug_implied_states = 1;
80 (*argvp)[1] = (*argvp)[0];
81 } else if (!strcmp((*argvp)[1], "--no-implied")) {
82 option_no_implied = 1;
83 (*argvp)[1] = (*argvp)[0];
84 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
85 option_assume_loops = 1;
86 (*argvp)[1] = (*argvp)[0];
87 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
88 option_known_conditions = 1;
89 (*argvp)[1] = (*argvp)[0];
90 } else if (!strcmp((*argvp)[1], "--no-data")) {
91 option_no_data = 1;
92 (*argvp)[1] = (*argvp)[0];
93 } else if (!strcmp((*argvp)[1], "--help")) {
94 help();
95 } else {
96 break;
98 (*argcp)--;
99 (*argvp)++;
103 static char *get_data_dir(char *arg0)
105 char *bin_dir;
106 char buf[256];
107 char *dir;
109 if (option_no_data) {
110 return NULL;
112 bin_dir = dirname(alloc_string(arg0));
113 strncpy(buf, bin_dir, 254);
114 buf[255] = '\0';
115 strncat(buf, "/smatch_data/", 254 - strlen(buf));
116 dir = alloc_string(buf);
117 if (!access(dir, R_OK))
118 return dir;
119 printf("Warning: %s is not accessible.\n", dir);
120 printf("Use --no-data to suppress this message.\n");
121 return NULL;
124 int main(int argc, char **argv)
126 int i;
127 reg_func func;
129 parse_args(&argc, &argv);
131 data_dir = get_data_dir(argv[0]);
133 /* The script IDs start at 1.
134 0 is used for internal stuff. */
135 create_function_hash();
136 for(i = 0; (func = reg_funcs[i]); i++){
137 func(i + 1);
139 register_function_hooks(-1);
141 smatch(argc, argv);
142 free_string(data_dir);
143 return 0;