ranges: initialize the pointer in get_value_ranges()
[smatch.git] / smatch_capped.c
blob391cb50c300a82159764204afbff17d107a2844f
1 /*
2 * smatch/smatch_capped.c
4 * Copyright (C) 2011 Oracle. All rights reserved.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is trying to make a list of the variables which
12 * have capped values. Sometimes we don't know what the
13 * cap is, for example if we are comparing variables but
14 * we don't know the values of the variables. In that
15 * case we only know that our variable is capped and we
16 * sort that information here.
19 #include "smatch.h"
20 #include "smatch_slist.h"
22 static int my_id;
24 STATE(capped);
25 STATE(uncapped);
27 int is_capped(struct expression *expr)
29 long long val;
31 if (expr->type == EXPR_BINOP) {
32 if (expr->op == '&')
33 return 1;
34 if (expr->op == SPECIAL_RIGHTSHIFT)
35 return 1;
36 if (expr->op == '%')
37 return is_capped(expr->right);
38 if (!is_capped(expr->left))
39 return 0;
40 if (expr->op == '/')
41 return 1;
42 if (!is_capped(expr->right))
43 return 0;
44 return 1;
46 if (get_implied_max(expr, &val))
47 return 1;
48 if (get_state_expr(my_id, expr) == &capped)
49 return 1;
50 return 0;
53 void set_param_capped_data(const char *name, struct symbol *sym, char *key, char *value)
55 char fullname[256];
57 if (strncmp(key, "$$", 2))
58 return;
59 snprintf(fullname, 256, "%s%s", name, key + 2);
60 set_state(my_id, fullname, sym, &capped);
63 static void match_condition(struct expression *expr)
65 struct smatch_state *left_true = NULL;
66 struct smatch_state *left_false = NULL;
67 struct smatch_state *right_true = NULL;
68 struct smatch_state *right_false = NULL;
71 if (expr->type != EXPR_COMPARE)
72 return;
74 switch (expr->op) {
75 case '<':
76 case SPECIAL_LTE:
77 case SPECIAL_UNSIGNED_LT:
78 case SPECIAL_UNSIGNED_LTE:
79 left_true = &capped;
80 right_false = &capped;
81 break;
82 case '>':
83 case SPECIAL_GTE:
84 case SPECIAL_UNSIGNED_GT:
85 case SPECIAL_UNSIGNED_GTE:
86 left_false = &capped;
87 right_true = &capped;
88 break;
89 case SPECIAL_EQUAL:
90 left_true = &capped;
91 right_true = &capped;
92 break;
93 case SPECIAL_NOTEQUAL:
94 left_false = &capped;
95 right_false = &capped;
96 break;
98 default:
99 return;
102 set_true_false_states_expr(my_id, expr->right, right_true, right_false);
103 set_true_false_states_expr(my_id, expr->left, left_true, left_false);
106 static void match_min_assign(const char *fn, struct expression *expr, void *unused)
108 set_state_expr(my_id, expr->left, &capped);
111 static void match_assign(struct expression *expr)
113 if (is_capped(expr->right)) {
114 set_state_expr(my_id, expr->left, &capped);
115 } else {
116 if (get_state_expr(my_id, expr->left))
117 set_state_expr(my_id, expr->left, &uncapped);
121 static void match_caller_info(struct expression *expr)
123 struct expression *tmp;
124 char *func;
125 int i;
127 func = get_fnptr_name(expr->fn);
128 if (!func)
129 return;
131 i = 0;
132 FOR_EACH_PTR(expr->args, tmp) {
133 if (is_capped(tmp))
134 sm_msg("info: passes capped_data %s %d '$$'", func, i);
135 i++;
136 } END_FOR_EACH_PTR(tmp);
139 static void struct_member_callback(char *fn, int param, char *printed_name, struct smatch_state *state)
141 if (state != &capped)
142 return;
143 sm_msg("info: passes capped_data '%s' %d '%s'", fn, param, printed_name);
146 void register_capped(int id)
148 my_id = id;
150 add_definition_db_callback(set_param_capped_data, CAPPED_DATA);
151 add_hook(&match_condition, CONDITION_HOOK);
152 add_hook(&match_assign, ASSIGNMENT_HOOK);
153 add_macro_assign_hook("min", &match_min_assign, NULL);
154 add_macro_assign_hook("min_t", &match_min_assign, NULL);
155 if (option_info) {
156 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
157 add_member_info_callback(my_id, struct_member_callback);