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

Attributes before declarations applied incorrectly. #367

Open
Tracked by #387
TwoClocks opened this issue Aug 4, 2022 · 3 comments
Open
Tracked by #387

Attributes before declarations applied incorrectly. #367

TwoClocks opened this issue Aug 4, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@TwoClocks
Copy link
Contributor

for the following struct

__declspec(align(2)) typedef struct {
    int a;
#ifdef MSVC
} X;

ty.requestedAlignment() returns null.

@Vexu
Copy link
Owner

Vexu commented Aug 4, 2022

$ arocc a.c --verbose-ast --target=x86_64-windows-msvc
struct_decl_two: 'struct (anonymous struct at a.c:1:30)'
  record_field_decl: 'int'
   name: a

typedef: 'attributed(struct (anonymous struct at a.c:1:30))'
 attr: aligned alignment: Attribute.Alignment{ .node = Tree.NodeIndex.none, .requested = 2 }
 name: X

Looks fine to me?

@Vexu
Copy link
Owner

Vexu commented Aug 4, 2022

Maybe because the attributes are before typedef in the MSVC version?

That would be it, the same happens if you put the __attribute__ before the typedef:

__attribute__((aligned(2))) typedef struct { int a; } X;

@Vexu Vexu added the bug Something isn't working label Aug 4, 2022
@Vexu Vexu changed the title MSVC isn't parsing __declspec? Attributes before declarations applied incorrectly. Aug 4, 2022
ehaas added a commit to ehaas/arocc that referenced this issue Aug 5, 2022
@TwoClocks
Copy link
Contributor Author

TwoClocks commented Oct 14, 2022

So for GCC/Clang it looks like if the attribute is before the typedef, it's applied to the type, if it's after then it's a requested attribute to the struct, and the layout logic decides what to do with it :

__attribute__((aligned(2))) typedef struct {
  int a;
} X;

typedef struct {
  int a;
} __attribute__((aligned(2))) Y;

_Static_assert(sizeof(X) == 4, "");
_Static_assert(_Alignof(X) == 2, "");

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

For MSVC, it seems like either before, or after, it's applied to the struct and the layout code decides what to do with it:

__declspec(align(2))  typedef struct {
  int a;
} X;

typedef struct {
  int a;
} __declspec(align(2)) Y;

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants