comparison: select the caller_info
[smatch.git] / smatch_project.c
blob8b66d9d4c97fff620dae1f39e9d94f1c31ae7a65
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * This file is only for very generic stuff, that is reusable
20 * between projects. If you need something special create a
21 * check_your_project.c.
25 #include "smatch.h"
26 #include "smatch_extra.h"
27 #include "smatch_function_hashtable.h"
29 static DEFINE_HASHTABLE_INSERT(insert_func, char, int);
30 static DEFINE_HASHTABLE_SEARCH(search_func, char, int);
31 static struct hashtable *skipped_funcs;
32 static struct hashtable *silenced_funcs;
33 static struct hashtable *no_inline_funcs;
35 static unsigned long skipped;
37 void set_function_skipped(void)
39 skipped = true;
42 int is_skipped_function(void)
44 return skipped;
47 static void match_function_def(struct symbol *sym)
49 char *func;
51 func = get_function();
52 if (!func)
53 return;
54 if (search_func(skipped_funcs, func))
55 set_function_skipped();
59 * A silenced function will still be processed and potentially appear in info
60 * output, but not regular checks.
62 int is_silenced_function(void)
64 char *func;
66 if (is_skipped_function())
67 return 1;
69 func = get_function();
70 if (!func)
71 return 0;
72 if (search_func(silenced_funcs, func))
73 return 1;
74 return 0;
77 int is_no_inline_function(const char *function)
79 if (search_func(no_inline_funcs, (char *)function))
80 return 1;
81 return 0;
84 static void register_no_return_funcs(void)
86 struct token *token;
87 const char *func;
88 char name[256];
90 snprintf(name, 256, "%s.no_return_funcs", option_project_str);
92 token = get_tokens_file(name);
93 if (!token)
94 return;
95 if (token_type(token) != TOKEN_STREAMBEGIN)
96 return;
97 token = token->next;
98 while (token_type(token) != TOKEN_STREAMEND) {
99 if (token_type(token) != TOKEN_IDENT)
100 return;
101 func = show_ident(token->ident);
102 add_function_hook(func, &__match_nullify_path_hook, NULL);
103 token = token->next;
105 clear_token_alloc();
108 static void register_ignored_macros(void)
110 struct token *token;
111 char *macro;
112 char name[256];
114 if (option_project == PROJ_NONE)
115 strcpy(name, "ignored_macros");
116 else
117 snprintf(name, 256, "%s.ignored_macros", option_project_str);
119 token = get_tokens_file(name);
120 if (!token)
121 return;
122 if (token_type(token) != TOKEN_STREAMBEGIN)
123 return;
124 token = token->next;
125 while (token_type(token) != TOKEN_STREAMEND) {
126 if (token_type(token) != TOKEN_IDENT)
127 return;
128 macro = alloc_string(show_ident(token->ident));
129 add_ptr_list(&__ignored_macros, macro);
130 token = token->next;
132 clear_token_alloc();
135 static void register_skipped_functions(void)
137 struct token *token;
138 char *func;
139 char name[256];
141 skipped_funcs = create_function_hashtable(500);
143 if (option_project == PROJ_NONE)
144 return;
146 snprintf(name, 256, "%s.skipped_functions", option_project_str);
148 token = get_tokens_file(name);
149 if (!token)
150 return;
151 if (token_type(token) != TOKEN_STREAMBEGIN)
152 return;
153 token = token->next;
154 while (token_type(token) != TOKEN_STREAMEND) {
155 if (token_type(token) != TOKEN_IDENT)
156 return;
157 func = alloc_string(show_ident(token->ident));
158 insert_func(skipped_funcs, func, INT_PTR(1));
159 token = token->next;
161 clear_token_alloc();
164 static void register_silenced_functions(void)
166 struct token *token;
167 char *func;
168 char name[256];
170 silenced_funcs = create_function_hashtable(500);
172 if (option_project == PROJ_NONE)
173 return;
175 snprintf(name, 256, "%s.silenced_functions", option_project_str);
177 token = get_tokens_file(name);
178 if (!token)
179 return;
180 if (token_type(token) != TOKEN_STREAMBEGIN)
181 return;
182 token = token->next;
183 while (token_type(token) != TOKEN_STREAMEND) {
184 if (token_type(token) != TOKEN_IDENT)
185 return;
186 func = alloc_string(show_ident(token->ident));
187 insert_func(silenced_funcs, func, INT_PTR(1));
188 token = token->next;
190 clear_token_alloc();
193 static void register_no_inline_functions(void)
195 struct token *token;
196 char *func;
197 char name[256];
199 no_inline_funcs = create_function_hashtable(500);
201 if (option_project == PROJ_NONE)
202 return;
204 snprintf(name, 256, "%s.no_inline_functions", option_project_str);
206 token = get_tokens_file(name);
207 if (!token)
208 return;
209 if (token_type(token) != TOKEN_STREAMBEGIN)
210 return;
211 token = token->next;
212 while (token_type(token) != TOKEN_STREAMEND) {
213 if (token_type(token) != TOKEN_IDENT)
214 return;
215 func = alloc_string(show_ident(token->ident));
216 insert_func(no_inline_funcs, func, INT_PTR(1));
217 token = token->next;
219 clear_token_alloc();
222 void register_project(int id)
224 add_hook(&match_function_def, FUNC_DEF_HOOK);
225 add_function_data(&skipped);
226 register_no_return_funcs();
227 register_ignored_macros();
228 register_skipped_functions();
229 register_silenced_functions();
230 register_no_inline_functions();