Skip to content

Commit ff6a7d1

Browse files
committed
keep typedefs when dumping c with --use-system-headers
1 parent 126d582 commit ff6a7d1

9 files changed

+166
-18
lines changed

src/ansi-c/ansi_c_convert_type.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ Function: ansi_c_convert_typet::write
253253

254254
void ansi_c_convert_typet::write(typet &type)
255255
{
256+
irep_idt _typedef = type.get(ID_typedef);
256257
type.clear();
258+
type.set(ID_typedef,_typedef);
257259

258260
// first, do "other"
259261

src/ansi-c/ansi_c_declaration.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ typet ansi_c_declarationt::full_type(
138138
}
139139

140140
*p=type();
141+
142+
// retain typedef for dump-c
143+
if(get_is_typedef())
144+
result.set(ID_typedef,declarator.get_name());
141145

142146
return result;
143147
}
@@ -169,7 +173,7 @@ void ansi_c_declarationt::to_symbol(
169173
symbol.is_macro=get_is_typedef() || get_is_enum_constant();
170174
symbol.is_parameter=get_is_parameter();
171175
symbol.is_weak=get_is_weak();
172-
176+
173177
// is it a function?
174178

175179
if(symbol.type.id()==ID_code && !symbol.is_type)

src/ansi-c/c_typecheck_base.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Author: Daniel Kroening, [email protected]
1717
#include "type2name.h"
1818
#include "c_storage_spec.h"
1919

20+
#include <iostream>
21+
2022
/*******************************************************************\
2123
2224
Function: c_typecheck_baset::to_string
@@ -96,7 +98,10 @@ void c_typecheck_baset::typecheck_symbol(symbolt &symbol)
9698
bool is_function=symbol.type.id()==ID_code;
9799

98100
const typet &final_type=follow(symbol.type);
99-
101+
// std::cout << "BASENAME: " << symbol.base_name << std::endl;
102+
// std::cout << "TYPE: " << symbol.type.pretty() << std::endl;
103+
// std::cout << "FINAL: " << final_type.pretty() << std::endl;
104+
100105
// set a few flags
101106
symbol.is_lvalue=!symbol.is_type && !symbol.is_macro;
102107

@@ -127,13 +132,23 @@ void c_typecheck_baset::typecheck_symbol(symbolt &symbol)
127132
(final_type.id()==ID_struct ||
128133
final_type.id()==ID_incomplete_struct))
129134
{
130-
symbol.pretty_name="struct "+id2string(symbol.base_name);
135+
if(symbol.type.find(ID_typedef).is_not_nil())
136+
symbol.pretty_name=symbol.type.get(ID_typedef);
137+
else
138+
symbol.pretty_name="struct "+id2string(symbol.base_name);
131139
}
132140
else if(symbol.is_type &&
133141
(final_type.id()==ID_union ||
134142
final_type.id()==ID_incomplete_union))
135143
{
136-
symbol.pretty_name="union "+id2string(symbol.base_name);
144+
if(symbol.type.find(ID_typedef).is_not_nil())
145+
{
146+
symbol.pretty_name=symbol.type.get(ID_typedef);
147+
symbol_table.symbols.find(symbol.type.get(ID_identifier))->second.pretty_name=symbol.type.get(ID_typedef);
148+
symbol_table.symbols.find(symbol.type.get(ID_identifier))->second.type.set(ID_typedef,symbol.pretty_name);
149+
}
150+
else
151+
symbol.pretty_name="union "+id2string(symbol.base_name);
137152
}
138153
else if(symbol.is_type &&
139154
(final_type.id()==ID_c_enum ||
@@ -148,6 +163,7 @@ void c_typecheck_baset::typecheck_symbol(symbolt &symbol)
148163

149164
// see if we have it already
150165
symbol_tablet::symbolst::iterator old_it=symbol_table.symbols.find(symbol.name);
166+
151167

152168
if(old_it==symbol_table.symbols.end())
153169
{

src/ansi-c/c_typecheck_type.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Author: Daniel Kroening, [email protected]
2323
#include "type2name.h"
2424
#include "ansi_c_convert_type.h"
2525

26+
#include <iostream>
27+
2628
/*******************************************************************\
2729
2830
Function: c_typecheck_baset::typecheck_type
@@ -52,12 +54,14 @@ void c_typecheck_baset::typecheck_type(typet &type)
5254
c_qualifiers+=c_qualifierst(type.subtype());
5355
bool packed=type.get_bool(ID_C_packed);
5456
exprt alignment=static_cast<const exprt &>(type.find(ID_C_alignment));
57+
irept _typedef=type.find(ID_typedef);
5558

5659
type.swap(type.subtype());
5760

5861
c_qualifiers.write(type);
5962
if(packed) type.set(ID_C_packed, true);
6063
if(alignment.is_not_nil()) type.add(ID_C_alignment, alignment);
64+
if(_typedef.is_not_nil()) type.add(ID_typedef, _typedef);
6165

6266
return; // done
6367
}
@@ -720,25 +724,32 @@ void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
720724
irep_idt identifier;
721725

722726
bool have_body=type.find(ID_components).is_not_nil();
723-
727+
724728
if(type.find(ID_tag).is_nil())
725729
{
726730
// Anonymous? Must come with body.
727731
assert(have_body);
728732

733+
// std::cout << "__TYPE: " << type.pretty() << std::endl;
734+
729735
// produce symbol
730736
symbolt compound_symbol;
731737
compound_symbol.is_type=true;
732738
compound_symbol.type=type;
733739
compound_symbol.location=type.source_location();
740+
if(compound_symbol.type.find(ID_typedef).is_not_nil())
741+
compound_symbol.pretty_name=compound_symbol.type.get(ID_typedef);
734742

735743
typecheck_compound_body(to_struct_union_type(compound_symbol.type));
736744

737745
std::string typestr=type2name(compound_symbol.type);
738746
compound_symbol.base_name="#anon-"+typestr;
739747
compound_symbol.name="tag-#anon#"+typestr;
740748
identifier=compound_symbol.name;
741-
749+
750+
// std::cout << "TYPEDEF: " << compound_symbol.type.get_string(ID_typedef) << std::endl;
751+
// std::cout << "BASENAME: " << id2string(compound_symbol.base_name) << std::endl;
752+
742753
// We might already have the same anonymous union/struct,
743754
// and this is simply ok. Note that the C standard treats
744755
// these as different types.

src/ansi-c/expr2c.cpp

+28-4
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,12 @@ std::string expr2ct::convert_rec(
365365
{
366366
const struct_typet &struct_type=to_struct_type(src);
367367

368-
std::string dest=q+"struct";
368+
std::string dest=q;
369+
370+
if(src.find(ID_typedef).is_not_nil())
371+
dest+="typedef struct";
372+
else
373+
dest+="struct";
369374

370375
const irep_idt &tag=struct_type.get_tag();
371376
if(tag!="") dest+=" "+id2string(tag);
@@ -383,7 +388,10 @@ std::string expr2ct::convert_rec(
383388

384389
dest+=" }";
385390

386-
dest+=d;
391+
if(src.find(ID_typedef).is_not_nil())
392+
dest+=src.get_string(ID_typedef);
393+
else
394+
dest+=d;
387395

388396
return dest;
389397
}
@@ -401,7 +409,12 @@ std::string expr2ct::convert_rec(
401409
{
402410
const union_typet &union_type=to_union_type(src);
403411

404-
std::string dest=q+"union";
412+
std::string dest=q;
413+
414+
if(src.find(ID_typedef).is_not_nil())
415+
dest+="typedef union";
416+
else
417+
dest+="union";
405418

406419
const irep_idt &tag=union_type.get_tag();
407420
if(tag!="") dest+=" "+id2string(tag);
@@ -419,7 +432,10 @@ std::string expr2ct::convert_rec(
419432

420433
dest+=" }";
421434

422-
dest+=d;
435+
if(src.find(ID_typedef).is_not_nil())
436+
dest+=src.get_string(ID_typedef);
437+
else
438+
dest+=d;
423439

424440
return dest;
425441
}
@@ -547,6 +563,8 @@ std::string expr2ct::convert_rec(
547563

548564
if(followed.id()==ID_struct)
549565
{
566+
if(followed.find(ID_typedef).is_not_nil())
567+
return followed.get_string(ID_typedef)+d;
550568
std::string dest=q+"struct";
551569
const irep_idt &tag=to_struct_type(followed).get_tag();
552570
if(tag!="") dest+=" "+id2string(tag);
@@ -555,6 +573,8 @@ std::string expr2ct::convert_rec(
555573
}
556574
else if(followed.id()==ID_union)
557575
{
576+
if(followed.find(ID_typedef).is_not_nil())
577+
return followed.get_string(ID_typedef)+d;
558578
std::string dest=q+"union";
559579
const irep_idt &tag=to_union_type(followed).get_tag();
560580
if(tag!="") dest+=" "+id2string(tag);
@@ -566,6 +586,8 @@ std::string expr2ct::convert_rec(
566586
}
567587
else if(src.id()==ID_struct_tag)
568588
{
589+
if(src.find(ID_typedef).is_not_nil())
590+
return src.get_string(ID_typedef)+d;
569591
const struct_tag_typet &struct_tag_type=
570592
to_struct_tag_type(src);
571593

@@ -578,6 +600,8 @@ std::string expr2ct::convert_rec(
578600
}
579601
else if(src.id()==ID_union_tag)
580602
{
603+
if(src.find(ID_typedef).is_not_nil())
604+
return src.get_string(ID_typedef)+d;
581605
const union_tag_typet &union_tag_type=
582606
to_union_tag_type(src);
583607

0 commit comments

Comments
 (0)