Skip to content

Commit 0f6fb19

Browse files
committed
Generate name for inner anonymous records
1 parent 3a6a280 commit 0f6fb19

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

bindgen/TypeTranslator.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,19 @@ TypeTranslator::addUnionDefinition(clang::RecordDecl *record,
193193
std::string name) {
194194
std::vector<std::shared_ptr<Field>> fields;
195195

196+
int anonIdField = 0;
196197
for (const clang::FieldDecl *field : record->fields()) {
197-
std::string fname = field->getNameAsString();
198198
std::shared_ptr<Type> ftype = translate(field->getType());
199199

200+
std::string fname = field->getNameAsString();
201+
if (fname.empty()) fname = "anonymous_" + std::to_string(anonIdField++);
200202
fields.push_back(std::make_shared<Field>(fname, ftype));
201203
}
202204

203205
uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl());
204206
assert(sizeInBits % 8 == 0);
205207

206-
return ir.addUnion(name, std::move(fields), sizeInBits / 8,
207-
getLocation(record));
208+
return ir.addUnion(std::move(name), std::move(fields), sizeInBits / 8, getLocation(record));
208209
}
209210

210211
std::shared_ptr<TypeDef>
@@ -222,15 +223,17 @@ TypeTranslator::addStructDefinition(clang::RecordDecl *record,
222223
ctx->getASTRecordLayout(record);
223224

224225
bool isBitFieldStruct = false;
226+
int anonIdField = 0;
225227
for (const clang::FieldDecl *field : record->fields()) {
226228
if (field->isBitField()) {
227229
isBitFieldStruct = true;
228230
}
229231
std::shared_ptr<Type> ftype = translate(field->getType());
230232
uint64_t recordOffsetInBits =
231233
recordLayout.getFieldOffset(field->getFieldIndex());
232-
fields.push_back(std::make_shared<Field>(field->getNameAsString(),
233-
ftype, recordOffsetInBits));
234+
std::string fname = field->getNameAsString();
235+
if (fname.empty()) fname = "anonymous_" + std::to_string(anonIdField++);
236+
fields.push_back(std::make_shared<Field>(fname, ftype, recordOffsetInBits));
234237
}
235238

236239
uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl());

0 commit comments

Comments
 (0)