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
22 static void check_pointer(struct expression
*expr
, char *ptr_name
)
27 if (!expr
|| expr
->type
!= EXPR_SIZEOF
)
30 get_value(expr
, &sval
);
32 expr
= strip_expr(expr
->cast_expression
);
33 name
= expr_to_str(expr
);
37 if (strcmp(ptr_name
, name
) == 0)
38 sm_msg("warn: was 'sizeof(*%s)' intended?", ptr_name
);
42 static void match_call_assignment(struct expression
*expr
)
44 struct expression
*call
= strip_expr(expr
->right
);
45 struct expression
*arg
;
48 if (!is_pointer(expr
->left
))
51 ptr_name
= expr_to_str(expr
->left
);
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
;
67 FOR_EACH_PTR(call
->args
, arg
) {
68 ptr_name
= expr_to_var(arg
);
71 if (strcmp(name
, ptr_name
) == 0)
72 sm_msg("warn: 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
;
83 FOR_EACH_PTR(call
->args
, arg
) {
84 if (arg
->type
!= EXPR_SIZEOF
)
86 obj
= strip_expr(arg
->cast_expression
);
89 name
= expr_to_var(obj
);
92 check_passes_pointer(name
, call
);
94 } END_FOR_EACH_PTR(arg
);
97 static struct string_list
*macro_takes_sizeof_argument
;
98 static void check_sizeof_number(struct expression
*expr
)
102 if (expr
->type
!= EXPR_VALUE
)
104 macro
= get_macro_name(expr
->pos
);
105 FOR_EACH_PTR(macro_takes_sizeof_argument
, tmp
) {
106 if (macro
&& strcmp(tmp
, macro
) == 0)
108 } END_FOR_EACH_PTR(tmp
);
110 sm_msg("warn: 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_msg("warn: sizeof(&pointer)?");
118 if (expr
->type
== EXPR_SIZEOF
)
119 sm_msg("warn: sizoef(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_msg("warn: taking sizeof binop");
125 static void register_macro_takes_sizeof_argument(void)
131 snprintf(name
, 256, "%s.macro_takes_sizeof_argument", option_project_str
);
133 token
= get_tokens_file(name
);
136 if (token_type(token
) != TOKEN_STREAMBEGIN
)
139 while (token_type(token
) != TOKEN_STREAMEND
) {
140 if (token_type(token
) != TOKEN_IDENT
)
142 macro
= alloc_string(show_ident(token
->ident
));
143 add_ptr_list(¯o_takes_sizeof_argument
, macro
);
149 void check_sizeof(int 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
);