nospec: use statement count to mark things as nospec
[smatch.git] / check_sizeof.c
blob6f4f3a6e8f50cd38e74a89f5e82a62650fa1a2b0
1 /*
2 * Copyright (C) 2012 Oracle.
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
18 #include "smatch.h"
20 static int my_id;
22 static void check_pointer(struct expression *expr, char *ptr_name)
24 char *name;
25 sval_t sval;
27 if (!expr || expr->type != EXPR_SIZEOF)
28 return;
30 get_value(expr, &sval);
32 expr = strip_expr(expr->cast_expression);
33 name = expr_to_str(expr);
34 if (!name)
35 return;
37 if (strcmp(ptr_name, name) == 0)
38 sm_warning("was 'sizeof(*%s)' intended?", ptr_name);
39 free_string(name);
42 static void match_call_assignment(struct expression *expr)
44 struct expression *call = strip_expr(expr->right);
45 struct expression *arg;
46 char *ptr_name;
48 if (!is_pointer(expr->left))
49 return;
51 ptr_name = expr_to_str(expr->left);
52 if (!ptr_name)
53 return;
55 FOR_EACH_PTR(call->args, arg) {
56 check_pointer(arg, ptr_name);
57 } END_FOR_EACH_PTR(arg);
59 free_string(ptr_name);
62 static void check_passes_pointer(char *name, struct expression *call)
64 struct expression *arg;
65 char *ptr_name;
67 FOR_EACH_PTR(call->args, arg) {
68 ptr_name = expr_to_var(arg);
69 if (!ptr_name)
70 continue;
71 if (strcmp(name, ptr_name) == 0)
72 sm_warning("was 'sizeof(*%s)' intended?", name);
73 free_string(ptr_name);
74 } END_FOR_EACH_PTR(arg);
77 static void match_check_params(struct expression *call)
79 struct expression *arg;
80 struct expression *obj;
81 char *name;
83 FOR_EACH_PTR(call->args, arg) {
84 if (arg->type != EXPR_SIZEOF)
85 continue;
86 obj = strip_expr(arg->cast_expression);
87 if (!is_pointer(obj))
88 continue;
89 name = expr_to_var(obj);
90 if (!name)
91 continue;
92 check_passes_pointer(name, call);
93 free_string(name);
94 } END_FOR_EACH_PTR(arg);
97 static struct string_list *macro_takes_sizeof_argument;
98 static void check_sizeof_number(struct expression *expr)
100 char *macro, *tmp;
102 if (expr->type != EXPR_VALUE)
103 return;
104 macro = get_macro_name(expr->pos);
105 FOR_EACH_PTR(macro_takes_sizeof_argument, tmp) {
106 if (macro && strcmp(tmp, macro) == 0)
107 return;
108 } END_FOR_EACH_PTR(tmp);
110 sm_warning("sizeof(NUMBER)?");
113 static void match_sizeof(struct expression *expr)
115 check_sizeof_number(expr);
116 if (expr->type == EXPR_PREOP && expr->op == '&')
117 sm_warning("sizeof(&pointer)?");
118 if (expr->type == EXPR_SIZEOF)
119 sm_warning("sizeof(sizeof())?");
120 /* the ilog2() macro is a valid place to check the size of a binop */
121 if (expr->type == EXPR_BINOP && !get_macro_name(expr->pos))
122 sm_warning("taking sizeof binop");
125 static void register_macro_takes_sizeof_argument(void)
127 struct token *token;
128 char *macro;
129 char name[256];
131 snprintf(name, 256, "%s.macro_takes_sizeof_argument", option_project_str);
133 token = get_tokens_file(name);
134 if (!token)
135 return;
136 if (token_type(token) != TOKEN_STREAMBEGIN)
137 return;
138 token = token->next;
139 while (token_type(token) != TOKEN_STREAMEND) {
140 if (token_type(token) != TOKEN_IDENT)
141 return;
142 macro = alloc_string(show_ident(token->ident));
143 add_ptr_list(&macro_takes_sizeof_argument, macro);
144 token = token->next;
146 clear_token_alloc();
149 void check_sizeof(int id)
151 my_id = id;
153 register_macro_takes_sizeof_argument();
154 add_hook(&match_call_assignment, CALL_ASSIGNMENT_HOOK);
155 add_hook(&match_check_params, FUNCTION_CALL_HOOK);
156 add_hook(&match_sizeof, SIZEOF_HOOK);