2 * Copyright (C) 2021 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 * Type promotion with selects doesn't work how you might expect:
20 * long foo = bar ? u_int_type : -12;
21 * The -12 is promoted to unsigned int and the sign is not expanded.
26 #include "smatch_extra.h"
30 static bool is_select(struct expression
*expr
)
32 if (expr
->type
== EXPR_CONDITIONAL
)
34 if (expr
->type
== EXPR_SELECT
)
39 static int is_uint(struct expression
*expr
)
43 type
= get_type(expr
);
44 if (type_positive_bits(type
) == 32)
49 static int is_suspicious_int(struct expression
*expr
)
52 struct range_list
*rl
;
54 type
= get_type(expr
);
55 if (type_positive_bits(type
) != 31)
58 get_absolute_rl(expr
, &rl
);
59 if (!sval_is_negative(rl_min(rl
)))
62 if (expr
->type
== EXPR_BINOP
&& expr
->op
== SPECIAL_LEFTSHIFT
)
68 static void match_assign(struct expression
*expr
)
70 struct expression
*right
, *one
, *two
;
77 right
= strip_expr(expr
->right
);
78 if (!is_select(right
))
81 type
= get_type(expr
->left
);
82 if (type_bits(type
) != 64)
86 one
= right
->cond_true
;
88 one
= right
->conditional
;
89 two
= right
->cond_false
;
91 if (is_uint(one
) && is_suspicious_int(two
)) {
92 name
= expr_to_str(two
);
93 sm_warning("check sign expansion for '%s'", name
);
98 if (is_uint(two
) && is_suspicious_int(one
)) {
99 name
= expr_to_str(one
);
100 sm_warning("check sign expansion for '%s'", name
);
106 void check_select_type(int id
)
109 add_hook(match_assign
, RAW_ASSIGNMENT_HOOK
);