xref: /illumos-gate/usr/src/tools/smatch/src/check_return.c (revision 1e56f352c1c208679012bca47d552e127f5b1072)
1 /*
2  * Copyright (C) 2010 Dan Carpenter.
3  *
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.
8  *
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.
13  *
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
16  */
17 
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 
21 static int my_id;
22 
23 static void must_check(const char *fn, struct expression *expr, void *data)
24 {
25 	struct statement *stmt;
26 
27 	stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
28 	if (stmt->type == STMT_EXPRESSION && stmt->expression == expr)
29 		sm_warning("unchecked '%s'", fn);
30 }
31 
32 static void register_must_check_funcs(void)
33 {
34 	struct token *token;
35 	const char *func;
36 	static char name[256];
37 
38 
39 	snprintf(name, 256, "%s.must_check_funcs", option_project_str);
40 	name[255] = '\0';
41 	token = get_tokens_file(name);
42 	if (!token)
43 		return;
44 	if (token_type(token) != TOKEN_STREAMBEGIN)
45 		return;
46 	token = token->next;
47 	while (token_type(token) != TOKEN_STREAMEND) {
48 		if (token_type(token) != TOKEN_IDENT)
49 			return;
50 		func = show_ident(token->ident);
51 		add_function_hook(func, &must_check, NULL);
52 		token = token->next;
53 	}
54 	clear_token_alloc();
55 }
56 
57 void check_return(int id)
58 {
59 	my_id = id;
60 	register_must_check_funcs();
61 }
62