Skip to content

Commit 38784f2

Browse files
committed
Handle anonymous structs inside records
1 parent 188081d commit 38784f2

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,10 +2837,6 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
28372837
if (auto it = inner_structs_.find(ID); it != inner_structs_.end()) {
28382838
return it->second;
28392839
}
2840-
if (auto *record = clang::dyn_cast<clang::RecordDecl>(decl);
2841-
record && !record->getIdentifier()) {
2842-
return ID;
2843-
}
28442840
return std::regex_replace(Mapper::ToString(decl), std::regex("::"), "_");
28452841
}
28462842

cpp2rust/converter/mapper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,17 @@ std::string ToString(clang::QualType qual_type) {
734734
return normalizeTranslationRule(type);
735735
}
736736

737+
static std::string synthesizeAnonRecordName(const clang::RecordDecl *record) {
738+
std::string parent_name;
739+
if (auto *parent =
740+
clang::dyn_cast<clang::RecordDecl>(record->getDeclContext())) {
741+
parent_name =
742+
parent->getIdentifier() ? parent->getIdentifier()->getName().str();
743+
: synthesizeAnonRecordName(parent);
744+
}
745+
return std::format("{}_anon_{}", parent_name, GetAnonIndex(record));
746+
}
747+
737748
std::string ToString(const clang::NamedDecl *decl) {
738749
if (auto *record = clang::dyn_cast<clang::RecordDecl>(decl);
739750
record && !record->getIdentifier()) {

0 commit comments

Comments
 (0)