Vexu / arocc

A C compiler written in Zig.
http://aro.vexu.eu/
MIT License
1.11k stars 57 forks source link

Issue a warning on gnu void pointer arithmetic #776

Closed wrongnull closed 4 days ago

wrongnull commented 5 days ago

Closes https://github.com/Vexu/arocc/issues/759 In this request I implemented the Type.isPtrTo function but only for the .pointer type specifier. I'm not sure if it is right approach to do that. Maybe other specifiers should be considered as well. I'm open for the conversation.

ehaas commented 5 days ago

It's not a bad idea but we already have a specialized function for it; you can just use ty.isVoidStar()

wrongnull commented 4 days ago

It's not a bad idea but we already have a specialized function for it; you can just use ty.isVoidStar()

I agree. Implementation of such a function is out of this pull requests scope

ehaas commented 4 days ago

We'll also need a check in the .add branch of the switch where the current check is, to handle things like ptr + 1 or 1 + ptr. In the .sub branch, I just realized we only need to check for a.ty.isVoidStar() because you can only subtract a pointer from a same-type pointer. So there's no valid subtraction where b is void * and a is not. But for addition either one could be void *.

One final area to add the checks would be for .plus_plus and .minus_minus in the suffixExpr and unExpr functions to handle the case of ptr++/ptr--/++ptr/--ptr when ptr is void *.

wrongnull commented 4 days ago

We'll also need a check in the .add branch of the switch where the current check is, to handle things like ptr + 1 or 1 + ptr. In the .sub branch, I just realized we only need to check for a.ty.isVoidStar() because you can only subtract a pointer from a same-type pointer. So there's no valid subtraction where b is void * and a is not. But for addition either one could be void *.

One final area to add the checks would be for .plus_plus and .minus_minus in the suffixExpr and unExpr functions to handle the case of ptr++/ptr--/++ptr/--ptr when ptr is void *.

done