Skip to content

Commit c6584f3

Browse files
committed
Fix dump-c output involving typedef names
Follow-up to 99067de, which made expr2c prefer typedef names over original type expressions.
1 parent 340a5ff commit c6584f3

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
typedef long int off_t;
2+
typedef signed char smallint;
3+
4+
typedef struct dumper_t dumper_t;
5+
typedef struct FS FS;
6+
7+
dumper_t * alloc_dumper(void);
8+
9+
typedef struct dumper_t
10+
{
11+
off_t dump_skip;
12+
signed int dump_length;
13+
smallint dump_vflag;
14+
FS *fshead;
15+
} dumper_t;
16+
17+
18+
typedef struct FS
19+
{
20+
struct FS *nextfs;
21+
signed int bcnt;
22+
} FS;
23+
24+
dumper_t * alloc_dumper(void)
25+
{
26+
return (void*) 0;
27+
}
28+
29+
int main() {
30+
alloc_dumper();
31+
return 0;
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.c
3+
--dump-c
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring

src/goto-instrument/dump_c.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ void dump_ct::operator()(std::ostream &os)
285285
os << std::endl;
286286
}
287287

288+
// global typedefs
289+
for(const auto &td : typedef_map)
290+
os << td.second << std::endl;
291+
if(!typedef_map.empty())
292+
os << std::endl;
293+
288294
if(!func_decl_stream.str().empty())
289295
os << func_decl_stream.str() << std::endl;
290296
if(!compound_body_stream.str().empty())
@@ -519,6 +525,8 @@ void dump_ct::convert_compound(
519525
assert(false);
520526

521527
struct_body << ";" << std::endl;
528+
529+
collect_typedefs(comp.type());
522530
}
523531

524532
os << type_to_string(unresolved);
@@ -915,6 +923,54 @@ void dump_ct::cleanup_decl(
915923

916924
/*******************************************************************\
917925
926+
Function: dump_ct::collect_typedefs
927+
928+
Inputs:
929+
930+
Outputs:
931+
932+
Purpose:
933+
934+
\*******************************************************************/
935+
936+
void dump_ct::collect_typedefs(const typet &type)
937+
{
938+
if(type.id()==ID_code)
939+
{
940+
const code_typet &code_type=to_code_type(type);
941+
942+
collect_typedefs(code_type.return_type());
943+
for(const auto &param : code_type.parameters())
944+
collect_typedefs(param.type());
945+
}
946+
else if(type.id()==ID_pointer)
947+
{
948+
collect_typedefs(type.subtype());
949+
}
950+
951+
const irep_idt &typedef_str=type.get(ID_C_typedef);
952+
953+
if(!typedef_str.empty())
954+
{
955+
std::pair<typedef_mapt::iterator, bool> entry=
956+
typedef_map.insert({typedef_str, ""});
957+
958+
if(entry.second)
959+
{
960+
typet t=type;
961+
t.remove(ID_C_typedef);
962+
963+
std::ostringstream oss;
964+
oss << "typedef " << type_to_string(t) << " "
965+
<< typedef_str << ';';
966+
967+
entry.first->second=oss.str();
968+
}
969+
}
970+
}
971+
972+
/*******************************************************************\
973+
918974
Function: dump_ct::convert_global_variables
919975
920976
Inputs:
@@ -935,6 +991,9 @@ void dump_ct::convert_global_variable(
935991
!converted_global.insert(symbol.name).second)
936992
return;
937993

994+
if(func.empty() || symbol.is_extern)
995+
collect_typedefs(symbol.type);
996+
938997
code_declt d(symbol.symbol_expr());
939998

940999
find_symbols_sett syms;
@@ -1062,6 +1121,8 @@ void dump_ct::convert_function_declaration(
10621121
if(symbol.name!=goto_functionst::entry_point() &&
10631122
symbol.name!=ID_main)
10641123
{
1124+
collect_typedefs(symbol.type);
1125+
10651126
os_decl << "// " << symbol.name << std::endl;
10661127
os_decl << "// " << symbol.location << std::endl;
10671128
os_decl << make_decl(symbol.name, symbol.type) << ";" << std::endl;

src/goto-instrument/dump_c_class.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class dump_ct
5959
declared_enum_constants_mapt;
6060
declared_enum_constants_mapt declared_enum_constants;
6161

62+
typedef std::map<irep_idt, std::string> typedef_mapt;
63+
typedef_mapt typedef_map;
64+
6265
void init_system_library_map();
6366

6467
std::string type_to_string(const typet &type);
@@ -85,6 +88,8 @@ class dump_ct
8588
return d_str.substr(0, d_str.size()-1);
8689
}
8790

91+
void collect_typedefs(const typet &type);
92+
8893
void convert_compound_declaration(
8994
const symbolt &symbol,
9095
std::ostream &os_body);

src/goto-instrument/goto_program2code.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,8 @@ void goto_program2codet::cleanup_code(
18181818

18191819
add_local_types(code.op0().type());
18201820

1821+
code.op0().type().remove(ID_C_typedef);
1822+
18211823
return;
18221824
}
18231825
else if(code.get_statement()==ID_function_call)

0 commit comments

Comments
 (0)