arocc icon indicating copy to clipboard operation
arocc copied to clipboard

Attributes before declarations applied incorrectly.

Open TwoClocks opened this issue 3 years ago • 3 comments

for the following struct

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

ty.requestedAlignment() returns null.

TwoClocks avatar Aug 04 '22 07:08 TwoClocks

$ 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 avatar Aug 04 '22 08:08 Vexu

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 avatar Aug 04 '22 16:08 Vexu

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, "");

TwoClocks avatar Oct 14 '22 19:10 TwoClocks