Skip to content

translate-c: fix applying struct alignment to first field #23863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xdBronch
Copy link
Contributor

fixes #23861, fixes #22766

@alexrp alexrp requested a review from Vexu May 12, 2025 10:23
@xdBronch
Copy link
Contributor Author

failures feel obvious in hindsight but i believe stuff was getting cached so i didnt get any failures locally. this will need a check against the fields natural alignment which i wasnt able to figure out before, would appreciate some guidance there.

@mlugg
Copy link
Member

mlugg commented May 12, 2025

Looks like you want clang::ASTContext::getTypeAlign: https://clang.llvm.org/doxygen/classclang_1_1ASTContext.html#afb825a845dced571b094627bfcae7870
We actually seem to have an unused wrapper for this:

zig/src/zig_clang.cpp

Lines 2463 to 2465 in bc3c50c

unsigned ZigClangASTContext_getTypeAlign(const ZigClangASTContext* self, ZigClangQualType T) {
return reinterpret_cast<const clang::ASTContext *>(self)->getTypeAlign(bitcast(T));
}

You'll just want to add a corresponding declaration on the Zig side here:

zig/src/clang.zig

Lines 164 to 167 in bc3c50c

pub const ASTContext = opaque {
pub const getPointerType = ZigClangASTContext_getPointerType;
extern fn ZigClangASTContext_getPointerType(*const ASTContext, T: QualType) QualType;
};

And use that to get the field type's natural alignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

translate-c fails to perserve field alignment c structs have different sizes in zig
2 participants