-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix translation of signed array indices #4079
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
Conversation
travisstaloch
commented
Jan 5, 2020
- closes translate-c doesn't cast signed array index #4075
You only need the cast if the index is signed or has more bits than |
I thought there might need to be some checks like this. Not sure how to do this right now. I'm guessing the result of Here is the line in question: |
Thanks! Now that #4053 is merged, would you mind adding a "Run Translated C" test case? file: This kind of test is better at catching whether translated-c code will actually turn into viable zig code. |
@andrewrk Should the tests be moved to test/run_translated_c.zig and removed from test/translate_c.zig or copied and changed? |
For your pull request, I think it would be nice to copy&change, because it's also nice to see the input/output as a test case. |
I've managed to get this working with the checks for signedness and width. switch (ZigClangType_getTypeClass(ty)) {
.Builtin => {
const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty);
switch (ZigClangBuiltinType_getKind(builtin_ty)) {
.Char_U,
.UChar,
.Char_S,
.SChar,
=> return 8,
.UInt128,
.Int128,
// Should we add more types here such as:
// .ULongLong,
// .LongLong,
=> return 128,
else => return 0,
}
unreachable;
}, https://github.com/ziglang/zig/blob/master/src-self-hosted/translate_c.zig#L3043 |
Would I be able to somehow get access to the QualType's TypeInfo? There seems to be public Or am I looking at the wrong docs here? |
"You only need the cast if the index is signed or has more bits than usize" |
Consider that the width of an integer type in bytes depends on the target, and when translating from C to Zig, generally you are translating target-agnostic code to target-agnostic code. So if you have to find out how many bytes an integer is exactly, it's a bit of a code smell. There might be a related function that I already made for a different purpose, which selects a type based on another type, but overrides the signedness. Would that help? |
I think that would help. I'm working with a |
zig/src-self-hosted/translate_c.zig Lines 3097 to 3105 in 8c640b3
|
Thanks for pointing out So rather than checking the width, I'm now planning to create a |
Mind rebasing while you're at it? This will make sure your new code doesn't regress the newly added test cases. |
Thousand dollar question, what about |
I used to Also I used |
(@as([*]E, &array) - 1).* |
That's my point, should this PR generate that if the index is not unsigned?
When you're dealing with pointers using a Also, if you want to keep the PR tidy don't merge |
Apologies for the messy commits. I did rebase but ended up with conflicts and must have taken a wrong turn. Ended up needing
I'll update to |
I will clean up that nasty merge commit. Maybe it will be easier to leave as is for now and make a fresh PR if accepted. |