arocc
arocc copied to clipboard
Attributes before declarations applied incorrectly.
for the following struct
__declspec(align(2)) typedef struct {
int a;
#ifdef MSVC
} X;
ty.requestedAlignment() returns null.
$ 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?
Maybe because the attributes are before
typedefin 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;
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, "");