2 * smatch/smatch_capped.c
4 * Copyright (C) 2011 Oracle. All rights reserved.
6 * Licensed under the Open Software License version 1.1
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.
20 #include "smatch_slist.h"
27 static int is_capped_macro(struct expression
*expr
)
31 name
= get_macro_name(expr
->pos
);
35 if (strcmp(name
, "min") == 0)
37 if (strcmp(name
, "MIN") == 0)
39 if (strcmp(name
, "min_t") == 0)
45 int is_capped(struct expression
*expr
)
49 expr
= strip_expr(expr
);
50 while (expr
&& expr
->type
== EXPR_POSTOP
) {
51 expr
= strip_expr(expr
->unop
);
56 if (get_hard_max(expr
, &dummy
))
59 if (is_capped_macro(expr
))
62 if (expr
->type
== EXPR_BINOP
) {
65 if (expr
->op
== SPECIAL_RIGHTSHIFT
)
68 return is_capped(expr
->right
);
69 if (!is_capped(expr
->left
))
73 if (!is_capped(expr
->right
))
77 if (get_state_expr(my_id
, expr
) == &capped
)
82 void set_param_capped_data(const char *name
, struct symbol
*sym
, char *key
, char *value
)
86 if (strncmp(key
, "$$", 2))
88 snprintf(fullname
, 256, "%s%s", name
, key
+ 2);
89 set_state(my_id
, fullname
, sym
, &capped
);
92 static void match_condition(struct expression
*expr
)
94 struct smatch_state
*left_true
= NULL
;
95 struct smatch_state
*left_false
= NULL
;
96 struct smatch_state
*right_true
= NULL
;
97 struct smatch_state
*right_false
= NULL
;
100 if (expr
->type
!= EXPR_COMPARE
)
106 case SPECIAL_UNSIGNED_LT
:
107 case SPECIAL_UNSIGNED_LTE
:
109 right_false
= &capped
;
113 case SPECIAL_UNSIGNED_GT
:
114 case SPECIAL_UNSIGNED_GTE
:
115 left_false
= &capped
;
116 right_true
= &capped
;
120 right_true
= &capped
;
122 case SPECIAL_NOTEQUAL
:
123 left_false
= &capped
;
124 right_false
= &capped
;
131 set_true_false_states_expr(my_id
, expr
->right
, right_true
, right_false
);
132 set_true_false_states_expr(my_id
, expr
->left
, left_true
, left_false
);
135 static void match_assign(struct expression
*expr
)
137 if (is_capped(expr
->right
)) {
138 set_state_expr(my_id
, expr
->left
, &capped
);
140 if (get_state_expr(my_id
, expr
->left
))
141 set_state_expr(my_id
, expr
->left
, &uncapped
);
145 static void match_caller_info(struct expression
*expr
)
147 struct expression
*tmp
;
151 FOR_EACH_PTR(expr
->args
, tmp
) {
153 sql_insert_caller_info(expr
, CAPPED_DATA
, i
, "$$", "1");
155 } END_FOR_EACH_PTR(tmp
);
158 static void struct_member_callback(struct expression
*call
, int param
, char *printed_name
, struct smatch_state
*state
)
160 if (state
!= &capped
)
162 sql_insert_caller_info(call
, CAPPED_DATA
, param
, printed_name
, "1");
165 void register_capped(int id
)
169 add_definition_db_callback(set_param_capped_data
, CAPPED_DATA
);
170 add_hook(&match_condition
, CONDITION_HOOK
);
171 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
173 add_hook(&match_caller_info
, FUNCTION_CALL_HOOK
);
174 add_member_info_callback(my_id
, struct_member_callback
);