Skip to content

Commit 2f57db1

Browse files
committed
Convert size in bits to size in bytes
1 parent 11d65bd commit 2f57db1

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

bindgen/TypeTranslator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe,
132132
if (typeEquals(tpe, avoid)) {
133133
// This is a type that we want to avoid the usage.
134134
// Êxample: A struct that has a pointer to itself
135-
uint64_t size = ctx->getTypeSize(tpe);
135+
uint64_t sizeInBits = ctx->getTypeSize(tpe);
136+
assert(sizeInBits % 8 == 0);
136137
return std::make_shared<ArrayType>(
137-
std::make_shared<PrimitiveType>("Byte"), size);
138+
std::make_shared<PrimitiveType>("Byte"), sizeInBits / 8);
138139
}
139140

140141
if (tpe->isFunctionPointerType()) {

bindgen/visitor/TreeVisitor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
105105
std::vector<Field *> fields;
106106

107107
for (const clang::FieldDecl *field : record->fields()) {
108-
uint64_t sizeInBytes = astContext->getTypeSize(field->getType()) / 8;
109-
maxSize = std::max(maxSize, sizeInBytes);
108+
uint64_t sizeInBits = astContext->getTypeSize(field->getType());
109+
assert(sizeInBits % 8 == 0);
110+
maxSize = std::max(maxSize, sizeInBits / 8);
110111
std::string fname = field->getNameAsString();
111112
std::shared_ptr<Type> ftype =
112113
typeTranslator.translate(field->getType(), &name);
@@ -152,9 +153,10 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
152153
llvm::errs().flush();
153154
}
154155

156+
uint64_t sizeInBits = astContext->getTypeSize(record->getTypeForDecl());
157+
assert(sizeInBits % 8 == 0);
155158
std::shared_ptr<Type> alias =
156-
ir.addStruct(name, std::move(fields),
157-
astContext->getTypeSize(record->getTypeForDecl()));
159+
ir.addStruct(name, std::move(fields), sizeInBits / 8);
158160

159161
typeTranslator.addAlias("struct " + name, alias);
160162
}

0 commit comments

Comments
 (0)