Skip to content
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

tools: Add tool for checking size and alignment of C types #20951

Merged
merged 4 commits into from
Aug 6, 2024

Conversation

ehaas
Copy link
Contributor

@ehaas ehaas commented Aug 5, 2024

Requested by @alexrp

Prints _Static_asserts for the size and alignment of all the basic built-in C types.

The output can be run through a compiler for the specified target to verify that Zig's values are the same as those used by a C compiler for the target.

Example output for x86_64-linux-gnu:

_Static_assert(sizeof(char) == 1, "");
_Static_assert(_Alignof(char) == 1, "");

_Static_assert(sizeof(short) == 2, "");
_Static_assert(_Alignof(short) == 2, "");

_Static_assert(sizeof(unsigned short) == 2, "");
_Static_assert(_Alignof(unsigned short) == 2, "");

_Static_assert(sizeof(int) == 4, "");
_Static_assert(_Alignof(int) == 4, "");

_Static_assert(sizeof(unsigned int) == 4, "");
_Static_assert(_Alignof(unsigned int) == 4, "");

_Static_assert(sizeof(long) == 8, "");
_Static_assert(_Alignof(long) == 8, "");

_Static_assert(sizeof(unsigned long) == 8, "");
_Static_assert(_Alignof(unsigned long) == 8, "");

_Static_assert(sizeof(long long) == 8, "");
_Static_assert(_Alignof(long long) == 8, "");

_Static_assert(sizeof(unsigned long long) == 8, "");
_Static_assert(_Alignof(unsigned long long) == 8, "");

_Static_assert(sizeof(float) == 4, "");
_Static_assert(_Alignof(float) == 4, "");

_Static_assert(sizeof(double) == 8, "");
_Static_assert(_Alignof(double) == 8, "");

_Static_assert(sizeof(long double) == 16, "");
_Static_assert(_Alignof(long double) == 16, "");

ehaas added 2 commits August 5, 2024 12:56
Prints _Static_asserts for the size and alignment of all the basic built-in C
types. The output can be run through a compiler for the specified target to
verify that Zig's values are the same as those used by a C compiler for the
target.
@alexrp
Copy link
Member

alexrp commented Aug 6, 2024

Already finding bugs:

zig run generate_c_size_and_align_checks.zig -- sparc64-linux-gnu | sparc64-linux-gnu-gcc -fsyntax-only -xc -
<stdin>:35:1: error: static assertion failed: "_Alignof(long double) == 8"

@alexrp
Copy link
Member

alexrp commented Aug 6, 2024

Another nice addition here would be to test __alignof(T) == target.c_type_preferred_alignment(c_type).

@andrewrk
Copy link
Member

andrewrk commented Aug 6, 2024

Thanks!

@andrewrk andrewrk merged commit de1fb19 into ziglang:master Aug 6, 2024
10 checks passed
@ehaas ehaas deleted the c-size-and-align-check branch August 6, 2024 20:28
@nektro
Copy link
Contributor

nektro commented Aug 6, 2024

a nice followup enhancement would be adding a test step that creates run steps with the output of this program for a matrix of targets that we support

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.

4 participants