Skip to content

Commit eb5ec24

Browse files
authored
Merge pull request diffblue#1736 from hannes-steffenhagen-diffblue/develop_fix-bitfield-pretty-printing
Fix bitfield pretty printing
2 parents 434cc99 + 4f1a67a commit eb5ec24

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
typedef struct {
2+
unsigned char b11 : 1;
3+
unsigned char b22 : 1;
4+
unsigned char b34 : 2;
5+
unsigned char b58 : 4;
6+
} bf_t;
7+
8+
typedef union {
9+
unsigned char val;
10+
bf_t bf;
11+
} union_bf_t;
12+
13+
int main(void)
14+
{
15+
union_bf_t bf;
16+
bf.bf.b11 = 1;
17+
return 0;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CORE
2+
main.c
3+
--show-goto-functions --json-ui
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
BF1\{U8\}\$U8\$\'b11\'
7+
BF1\{U8\}\$U8\$\'b22\'
8+
BF2\{U8\}\$U8\$\'b34\'
9+
BF4\{U8\}\$U8\$\'b58\'
10+
--
11+
--
12+
13+
Checks that type2name generates the right names for structs containing bitfields,
14+
in particular including the width of bitfield fields.
15+

src/ansi-c/type2name.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Author: Daniel Kroening, [email protected]
1616
#include <util/namespace.h>
1717
#include <util/symbol.h>
1818
#include <util/symbol_table.h>
19+
#include <util/pointer_offset_size.h>
20+
#include <util/invariant.h>
1921

2022
typedef std::unordered_map<irep_idt, std::pair<size_t, bool>, irep_id_hash>
2123
symbol_numbert;
@@ -81,6 +83,15 @@ static std::string type2name_symbol(
8183
return result;
8284
}
8385

86+
static std::string pointer_offset_bits_as_string(
87+
const typet &type,
88+
const namespacet &ns)
89+
{
90+
mp_integer bits = pointer_offset_bits(type, ns);
91+
CHECK_RETURN(bits != -1);
92+
return integer2string(bits);
93+
}
94+
8495
static bool parent_is_sym_check=false;
8596
static std::string type2name(
8697
const typet &type,
@@ -115,9 +126,9 @@ static std::string type2name(
115126
else if(type.id()==ID_empty)
116127
result+='V';
117128
else if(type.id()==ID_signedbv)
118-
result+="S" + type.get_string(ID_width);
129+
result+="S" + pointer_offset_bits_as_string(type, ns);
119130
else if(type.id()==ID_unsignedbv)
120-
result+="U" + type.get_string(ID_width);
131+
result+="U" + pointer_offset_bits_as_string(type, ns);
121132
else if(type.id()==ID_bool ||
122133
type.id()==ID_c_bool)
123134
result+='B';
@@ -128,9 +139,9 @@ static std::string type2name(
128139
else if(type.id()==ID_complex)
129140
result+='C';
130141
else if(type.id()==ID_floatbv)
131-
result+="F" + type.get_string(ID_width);
142+
result+="F" + pointer_offset_bits_as_string(type, ns);
132143
else if(type.id()==ID_fixedbv)
133-
result+="X" + type.get_string(ID_width);
144+
result+="X" + pointer_offset_bits_as_string(type, ns);
134145
else if(type.id()==ID_natural)
135146
result+='N';
136147
else if(type.id()==ID_pointer)
@@ -171,7 +182,7 @@ static std::string type2name(
171182
const array_typet &t=to_array_type(type);
172183
mp_integer size;
173184
if(t.size().id()==ID_symbol)
174-
result+="ARR"+t.size().get_string(ID_identifier);
185+
result+="ARR"+id2string(t.size().get(ID_identifier));
175186
else if(to_integer(t.size(), size))
176187
result+="ARR?";
177188
else
@@ -205,7 +216,9 @@ static std::string type2name(
205216
if(it!=components.begin())
206217
result+='|';
207218
result+=type2name(it->type(), ns, symbol_number);
208-
result+="'"+it->get_string(ID_name)+"'";
219+
irep_idt component_name = it->get_name();
220+
CHECK_RETURN(!component_name.empty());
221+
result+="'"+id2string(component_name)+"'";
209222
}
210223
result+=']';
211224
}
@@ -233,7 +246,7 @@ static std::string type2name(
233246
else if(type.id()==ID_incomplete_c_enum)
234247
result +="EN?";
235248
else if(type.id()==ID_c_bit_field)
236-
result+="BF"+type.get_string(ID_size);
249+
result+="BF"+pointer_offset_bits_as_string(type, ns);
237250
else if(type.id()==ID_vector)
238251
result+="VEC"+type.get_string(ID_size);
239252
else

0 commit comments

Comments
 (0)