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());