*new* assign_vs_compare: if ((x = <constant>)) {
[smatch.git] / smatch_capped.c
blob4b5d6e9cf889988007ed0e0b8248057d5804c5dc
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)
32 return 0;
34 if (expr->type == EXPR_BINOP) {
35 if (expr->op == '&')
36 return 1;
37 if (expr->op == SPECIAL_RIGHTSHIFT)
38 return 1;
39 if (expr->op == '%')
40 return is_capped(expr->right);
41 if (!is_capped(expr->left))
42 return 0;
43 if (expr->op == '/')
44 return 1;
45 if (!is_capped(expr->right))
46 return 0;
47 return 1;
49 if (get_implied_max(expr, &val))
50 return 1;
51 if (get_state_expr(my_id, expr) == &capped)
52 return 1;
53 return 0;
56 void set_param_capped_data(const char *name, struct symbol *sym, char *key, char *value)
58 char fullname[256];
60 if (strncmp(key, "$$", 2))
61 return;
62 snprintf(fullname, 256, "%s%s", name, key + 2);
63 set_state(my_id, fullname, sym, &capped);
66 static void match_condition(struct expression *expr)
68 struct smatch_state *left_true = NULL;
69 struct smatch_state *left_false = NULL;
70 struct smatch_state *right_true = NULL;
71 struct smatch_state *right_false = NULL;
74 if (expr->type != EXPR_COMPARE)
75 return;
77 switch (expr->op) {
78 case '<':
79 case SPECIAL_LTE:
80 case SPECIAL_UNSIGNED_LT:
81 case SPECIAL_UNSIGNED_LTE:
82 left_true = &capped;
83 right_false = &capped;
84 break;
85 case '>':
86 case SPECIAL_GTE:
87 case SPECIAL_UNSIGNED_GT:
88 case SPECIAL_UNSIGNED_GTE:
89 left_false = &capped;
90 right_true = &capped;
91 break;
92 case SPECIAL_EQUAL:
93 left_true = &capped;
94 right_true = &capped;
95 break;
96 case SPECIAL_NOTEQUAL:
97 left_false = &capped;
98 right_false = &capped;
99 break;
101 default:
102 return;
105 set_true_false_states_expr(my_id, expr->right, right_true, right_false);
106 set_true_false_states_expr(my_id, expr->left, left_true, left_false);
109 static void match_min_assign(const char *fn, struct expression *expr, void *unused)
111 set_state_expr(my_id, expr->left, &capped);
114 static void match_assign(struct expression *expr)
116 if (is_capped(expr->right)) {
117 set_state_expr(my_id, expr->left, &capped);
118 } else {
119 if (get_state_expr(my_id, expr->left))
120 set_state_expr(my_id, expr->left, &uncapped);
124 static void match_caller_info(struct expression *expr)
126 struct expression *tmp;
127 char *func;
128 int i;
130 func = get_fnptr_name(expr->fn);
131 if (!func)
132 return;
134 i = 0;
135 FOR_EACH_PTR(expr->args, tmp) {
136 if (is_capped(tmp))
137 sm_msg("info: passes capped_data %s %d '$$' %s", func,
138 i, is_static(expr->fn) ? "static" : "global");
139 i++;
140 } END_FOR_EACH_PTR(tmp);
143 static void struct_member_callback(char *fn, char *global_static, int param, char *printed_name, struct smatch_state *state)
145 if (state != &capped)
146 return;
147 sm_msg("info: passes capped_data '%s' %d '%s' %s", fn, param, printed_name, global_static);
150 void register_capped(int id)
152 my_id = id;
154 add_definition_db_callback(set_param_capped_data, CAPPED_DATA);
155 add_hook(&match_condition, CONDITION_HOOK);
156 add_hook(&match_assign, ASSIGNMENT_HOOK);
157 add_macro_assign_hook("min", &match_min_assign, NULL);
158 add_macro_assign_hook("min_t", &match_min_assign, NULL);
159 if (option_info) {
160 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
161 add_member_info_callback(my_id, struct_member_callback);