sval: extra: type information was being lost in ++/-- operations
[smatch.git] / check_sizeof_pointer.c
blob7879628fb04b06f9b5d8d8a536ecf818e504e65c
1 /*
2 * smatch/check_sizeof_pointer.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
12 static int my_id;
14 static void check_sizeof(struct expression *expr, char *ptr_name)
16 char *name;
17 sval_t sval;
19 if (!expr || expr->type != EXPR_SIZEOF)
20 return;
22 get_value(expr, &sval);
24 expr = strip_expr(expr->cast_expression);
25 name = get_variable_from_expr_complex(expr, NULL);
26 if (!name)
27 return;
29 if (strcmp(ptr_name, name) == 0)
30 sm_msg("warn: was 'sizeof(*%s)' intended?", ptr_name);
31 free_string(name);
34 static void match_call_assignment(struct expression *expr)
36 struct expression *call = strip_expr(expr->right);
37 struct expression *arg;
38 char *ptr_name;
40 if (!is_pointer(expr->left))
41 return;
43 ptr_name = get_variable_from_expr_complex(expr->left, NULL);
44 if (!ptr_name)
45 return;
47 FOR_EACH_PTR(call->args, arg) {
48 check_sizeof(arg, ptr_name);
49 } END_FOR_EACH_PTR(arg);
52 static void check_passes_pointer(char *name, struct expression *call)
54 struct expression *arg;
55 char *ptr_name;
57 FOR_EACH_PTR(call->args, arg) {
58 ptr_name = get_variable_from_expr(arg, NULL);
59 if (!ptr_name)
60 continue;
61 if (strcmp(name, ptr_name) == 0)
62 sm_msg("warn: was 'sizeof(*%s)' intended?", name);
63 free_string(ptr_name);
64 } END_FOR_EACH_PTR(arg);
67 static void match_check_params(struct expression *call)
69 struct expression *arg;
70 struct expression *obj;
71 char *name;
73 FOR_EACH_PTR(call->args, arg) {
74 if (arg->type != EXPR_SIZEOF)
75 continue;
76 obj = strip_expr(arg->cast_expression);
77 if (!is_pointer(obj))
78 continue;
79 name = get_variable_from_expr(obj, NULL);
80 if (!name)
81 continue;
82 check_passes_pointer(name, call);
83 free_string(name);
84 } END_FOR_EACH_PTR(arg);
87 void check_sizeof_pointer(int id)
89 my_id = id;
91 add_hook(&match_call_assignment, CALL_ASSIGNMENT_HOOK);
92 add_hook(&match_check_params, FUNCTION_CALL_HOOK);