flow: don't print duplicate "unreachable code" warnings
[smatch.git] / smatch_capped.c
blobc7831a7e9b7a7ee369adab40981e27b86c833d4c
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 static int is_capped_macro(struct expression *expr)
29 char *name;
31 name = get_macro_name(expr->pos);
32 if (!name)
33 return 0;
35 if (strcmp(name, "min") == 0)
36 return 1;
37 if (strcmp(name, "MIN") == 0)
38 return 1;
39 if (strcmp(name, "min_t") == 0)
40 return 1;
42 return 0;
45 int is_capped(struct expression *expr)
47 expr = strip_expr(expr);
48 if (!expr)
49 return 0;
51 if (is_capped_macro(expr))
52 return 1;
54 if (expr->type == EXPR_BINOP) {
55 if (expr->op == '&')
56 return 1;
57 if (expr->op == SPECIAL_RIGHTSHIFT)
58 return 1;
59 if (expr->op == '%')
60 return is_capped(expr->right);
61 if (!is_capped(expr->left))
62 return 0;
63 if (expr->op == '/')
64 return 1;
65 if (!is_capped(expr->right))
66 return 0;
67 return 1;
69 if (get_state_expr(my_id, expr) == &capped)
70 return 1;
71 return 0;
74 void set_param_capped_data(const char *name, struct symbol *sym, char *key, char *value)
76 char fullname[256];
78 if (strncmp(key, "$$", 2))
79 return;
80 snprintf(fullname, 256, "%s%s", name, key + 2);
81 set_state(my_id, fullname, sym, &capped);
84 static void match_condition(struct expression *expr)
86 struct smatch_state *left_true = NULL;
87 struct smatch_state *left_false = NULL;
88 struct smatch_state *right_true = NULL;
89 struct smatch_state *right_false = NULL;
92 if (expr->type != EXPR_COMPARE)
93 return;
95 switch (expr->op) {
96 case '<':
97 case SPECIAL_LTE:
98 case SPECIAL_UNSIGNED_LT:
99 case SPECIAL_UNSIGNED_LTE:
100 left_true = &capped;
101 right_false = &capped;
102 break;
103 case '>':
104 case SPECIAL_GTE:
105 case SPECIAL_UNSIGNED_GT:
106 case SPECIAL_UNSIGNED_GTE:
107 left_false = &capped;
108 right_true = &capped;
109 break;
110 case SPECIAL_EQUAL:
111 left_true = &capped;
112 right_true = &capped;
113 break;
114 case SPECIAL_NOTEQUAL:
115 left_false = &capped;
116 right_false = &capped;
117 break;
119 default:
120 return;
123 set_true_false_states_expr(my_id, expr->right, right_true, right_false);
124 set_true_false_states_expr(my_id, expr->left, left_true, left_false);
127 static void match_assign(struct expression *expr)
129 if (is_capped(expr->right)) {
130 set_state_expr(my_id, expr->left, &capped);
131 } else {
132 if (get_state_expr(my_id, expr->left))
133 set_state_expr(my_id, expr->left, &uncapped);
137 static void match_caller_info(struct expression *expr)
139 struct expression *tmp;
140 int i;
142 i = 0;
143 FOR_EACH_PTR(expr->args, tmp) {
144 if (is_capped(tmp))
145 sql_insert_caller_info(expr, CAPPED_DATA, i, "$$", "1");
146 i++;
147 } END_FOR_EACH_PTR(tmp);
150 static void struct_member_callback(struct expression *call, int param, char *printed_name, struct smatch_state *state)
152 if (state != &capped)
153 return;
154 sql_insert_caller_info(call, CAPPED_DATA, param, printed_name, "1");
157 void register_capped(int id)
159 my_id = id;
161 add_definition_db_callback(set_param_capped_data, CAPPED_DATA);
162 add_hook(&match_condition, CONDITION_HOOK);
163 add_hook(&match_assign, ASSIGNMENT_HOOK);
165 add_hook(&match_caller_info, FUNCTION_CALL_HOOK);
166 add_member_info_callback(my_id, struct_member_callback);