From 0f6fb19c5e1d3988e65a970175e598f4fdafce7e Mon Sep 17 00:00:00 2001 From: Liudmila Kornilova Date: Tue, 29 Jan 2019 19:03:29 +0300 Subject: [PATCH] Generate name for inner anonymous records --- bindgen/TypeTranslator.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bindgen/TypeTranslator.cpp b/bindgen/TypeTranslator.cpp index 553e39a..d16f0eb 100644 --- a/bindgen/TypeTranslator.cpp +++ b/bindgen/TypeTranslator.cpp @@ -193,18 +193,19 @@ TypeTranslator::addUnionDefinition(clang::RecordDecl *record, std::string name) { std::vector> fields; + int anonIdField = 0; for (const clang::FieldDecl *field : record->fields()) { - std::string fname = field->getNameAsString(); std::shared_ptr ftype = translate(field->getType()); + std::string fname = field->getNameAsString(); + if (fname.empty()) fname = "anonymous_" + std::to_string(anonIdField++); fields.push_back(std::make_shared(fname, ftype)); } uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl()); assert(sizeInBits % 8 == 0); - return ir.addUnion(name, std::move(fields), sizeInBits / 8, - getLocation(record)); + return ir.addUnion(std::move(name), std::move(fields), sizeInBits / 8, getLocation(record)); } std::shared_ptr @@ -222,6 +223,7 @@ TypeTranslator::addStructDefinition(clang::RecordDecl *record, ctx->getASTRecordLayout(record); bool isBitFieldStruct = false; + int anonIdField = 0; for (const clang::FieldDecl *field : record->fields()) { if (field->isBitField()) { isBitFieldStruct = true; @@ -229,8 +231,9 @@ TypeTranslator::addStructDefinition(clang::RecordDecl *record, std::shared_ptr ftype = translate(field->getType()); uint64_t recordOffsetInBits = recordLayout.getFieldOffset(field->getFieldIndex()); - fields.push_back(std::make_shared(field->getNameAsString(), - ftype, recordOffsetInBits)); + std::string fname = field->getNameAsString(); + if (fname.empty()) fname = "anonymous_" + std::to_string(anonIdField++); + fields.push_back(std::make_shared(fname, ftype, recordOffsetInBits)); } uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl());