2 * Copyright (C) 2012 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 #include "smatch_function_hashtable.h"
23 DEFINE_STRING_HASHTABLE_STATIC(unconstant_macros
);
25 static int does_inc_dec(struct expression
*expr
)
27 if (expr
->type
== EXPR_PREOP
|| expr
->type
== EXPR_POSTOP
) {
28 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
30 return does_inc_dec(expr
->unop
);
35 static int expr_equiv_no_inc_dec(struct expression
*one
, struct expression
*two
)
37 if (does_inc_dec(one
) || does_inc_dec(two
))
39 return expr_equiv(one
, two
);
42 static int inconsistent_check(struct expression
*left
, struct expression
*right
)
46 if (get_value(left
->left
, &sval
)) {
47 if (get_value(right
->left
, &sval
))
48 return expr_equiv_no_inc_dec(left
->right
, right
->right
);
49 if (get_value(right
->right
, &sval
))
50 return expr_equiv_no_inc_dec(left
->right
, right
->left
);
53 if (get_value(left
->right
, &sval
)) {
54 if (get_value(right
->left
, &sval
))
55 return expr_equiv_no_inc_dec(left
->left
, right
->right
);
56 if (get_value(right
->right
, &sval
))
57 return expr_equiv_no_inc_dec(left
->left
, right
->left
);
64 static void check_or(struct expression
*expr
)
66 struct expression
*left
, *right
;
68 left
= strip_expr(expr
->left
);
69 right
= strip_expr(expr
->right
);
71 if (left
->type
!= EXPR_COMPARE
||
72 left
->op
!= SPECIAL_NOTEQUAL
)
74 if (right
->type
!= EXPR_COMPARE
||
75 right
->op
!= SPECIAL_NOTEQUAL
)
77 if (!inconsistent_check(left
, right
))
80 sm_msg("warn: was && intended here instead of ||?");
83 static void check_and(struct expression
*expr
)
85 struct expression
*left
, *right
;
87 left
= strip_expr(expr
->left
);
88 right
= strip_expr(expr
->right
);
90 if (left
->type
!= EXPR_COMPARE
||
91 left
->op
!= SPECIAL_EQUAL
)
93 if (right
->type
!= EXPR_COMPARE
||
94 right
->op
!= SPECIAL_EQUAL
)
96 if (!inconsistent_check(left
, right
))
99 sm_msg("warn: was || intended here instead of &&?");
102 static void match_logic(struct expression
*expr
)
104 if (expr
->type
!= EXPR_LOGICAL
)
107 if (expr
->op
== SPECIAL_LOGICAL_OR
)
109 if (expr
->op
== SPECIAL_LOGICAL_AND
)
113 static int is_unconstant_macro(struct expression
*expr
)
117 macro
= get_macro_name(expr
->pos
);
120 if (search_unconstant_macros(unconstant_macros
, macro
))
125 static void match_condition(struct expression
*expr
)
129 if (expr
->type
!= EXPR_BINOP
)
131 if (expr
->op
== '|') {
132 if (get_value(expr
->left
, &sval
) || get_value(expr
->right
, &sval
))
133 sm_msg("warn: suspicious bitop condition");
140 if (get_macro_name(expr
->pos
))
142 if (is_unconstant_macro(expr
->left
) || is_unconstant_macro(expr
->right
))
145 if ((get_value(expr
->left
, &sval
) && sval
.value
== 0) ||
146 (get_value(expr
->right
, &sval
) && sval
.value
== 0))
147 sm_msg("warn: bitwise AND condition is false here");
150 static void match_binop(struct expression
*expr
)
152 sval_t left
, right
, sval
;
156 if (!get_value(expr
, &sval
) || sval
.value
!= 0)
158 if (get_macro_name(expr
->pos
))
160 if (!get_value(expr
->left
, &left
) || !get_value(expr
->right
, &right
))
162 sm_msg("warn: odd binop '0x%llx & 0x%llx'", left
.uvalue
, right
.uvalue
);
165 void check_or_vs_and(int id
)
169 unconstant_macros
= create_function_hashtable(100);
170 load_strings("unconstant_macros", unconstant_macros
);
172 add_hook(&match_logic
, LOGIC_HOOK
);
173 add_hook(&match_condition
, CONDITION_HOOK
);
175 add_hook(&match_binop
, BINOP_HOOK
);