Skip to content

Commit 24e9d5f

Browse files
authored
Merge pull request diffblue#7270 from tautschnig/bugfixes/bitfield-side-effect
Assignments to bit-fields yield results of bit-field type
2 parents 8e92409 + 623cde5 commit 24e9d5f

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

regression/cbmc/Bitfields4/main.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
4+
struct S0
5+
{
6+
signed int f1 : 2;
7+
};
8+
9+
int main(int argc, char *argv[])
10+
{
11+
struct S0 l_4590 = {1};
12+
uint32_t g_307 = ++l_4590.f1;
13+
assert(g_307 == 4294967294u);
14+
15+
l_4590 = (struct S0){1};
16+
uint32_t g_308 = l_4590.f1 += 1;
17+
assert(g_308 == 4294967294u);
18+
19+
uint32_t g_309 = l_4590.f1 = 62378u;
20+
assert(g_309 == 4294967294u);
21+
}

regression/cbmc/Bitfields4/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/expr2c.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,8 +1834,12 @@ std::string expr2ct::convert_constant(
18341834
{
18351835
const auto width = to_bitvector_type(type).get_width();
18361836

1837-
mp_integer int_value =
1838-
bvrep2integer(value, width, type.id() == ID_signedbv);
1837+
mp_integer int_value = bvrep2integer(
1838+
value,
1839+
width,
1840+
type.id() == ID_signedbv ||
1841+
(type.id() == ID_c_bit_field &&
1842+
to_c_bit_field_type(type).underlying_type().id() == ID_signedbv));
18391843

18401844
const irep_idt &c_type =
18411845
type.id() == ID_c_bit_field

src/goto-programs/goto_clean_expr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,12 @@ void goto_convertt::clean_expr(
392392
exprt new_lhs = skip_typecast(lhs);
393393
exprt new_rhs = typecast_exprt::conditional_cast(
394394
side_effect_assign.rhs(), new_lhs.type());
395-
code_assignt assignment(std::move(new_lhs), std::move(new_rhs));
395+
code_assignt assignment(std::move(new_lhs), new_rhs);
396396
assignment.add_source_location()=expr.source_location();
397397
convert_assign(assignment, dest, mode);
398398

399399
if(result_is_used)
400-
expr = must_use_rhs ? side_effect_assign.rhs() : lhs;
400+
expr = must_use_rhs ? new_rhs : lhs;
401401
else
402402
expr.make_nil();
403403
return;

src/goto-programs/goto_convert_side_effect.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void goto_convertt::remove_assignment(
4949
if(statement==ID_assign)
5050
{
5151
auto &old_assignment = to_side_effect_expr_assign(expr);
52+
exprt new_lhs = skip_typecast(old_assignment.lhs());
5253

5354
if(
5455
result_is_used && !address_taken &&
@@ -57,10 +58,10 @@ void goto_convertt::remove_assignment(
5758
if(!old_assignment.rhs().is_constant())
5859
make_temp_symbol(old_assignment.rhs(), "assign", dest, mode);
5960

60-
replacement_expr_opt = old_assignment.rhs();
61+
replacement_expr_opt =
62+
typecast_exprt::conditional_cast(old_assignment.rhs(), new_lhs.type());
6163
}
6264

63-
exprt new_lhs = skip_typecast(old_assignment.lhs());
6465
exprt new_rhs =
6566
typecast_exprt::conditional_cast(old_assignment.rhs(), new_lhs.type());
6667
code_assignt new_assignment(std::move(new_lhs), std::move(new_rhs));
@@ -115,6 +116,7 @@ void goto_convertt::remove_assignment(
115116
}
116117

117118
const binary_exprt &binary_expr = to_binary_expr(expr);
119+
exprt new_lhs = skip_typecast(binary_expr.op0());
118120
const typet &op0_type = binary_expr.op0().type();
119121

120122
PRECONDITION(
@@ -129,10 +131,10 @@ void goto_convertt::remove_assignment(
129131
assignment_lhs_needs_temporary(binary_expr.op0()))
130132
{
131133
make_temp_symbol(rhs, "assign", dest, mode);
132-
replacement_expr_opt = rhs;
134+
replacement_expr_opt =
135+
typecast_exprt::conditional_cast(rhs, new_lhs.type());
133136
}
134137

135-
exprt new_lhs = skip_typecast(binary_expr.op0());
136138
rhs = typecast_exprt::conditional_cast(rhs, new_lhs.type());
137139
rhs.add_source_location() = expr.source_location();
138140
code_assignt assignment(new_lhs, rhs);

0 commit comments

Comments
 (0)