Skip to content

Commit

Permalink
Attribute: do not apply vector_size to enums
Browse files Browse the repository at this point in the history
clang silently ignores the attribute; GCC issues an error
diagnostic

Fixes #658
  • Loading branch information
ehaas authored and Vexu committed Apr 4, 2024
1 parent 011809e commit 9f31df3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 deletions.
10 changes: 6 additions & 4 deletions src/aro/Attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1049,11 +1049,13 @@ fn applyTransparentUnion(attr: Attribute, p: *Parser, tok: TokenIndex, ty: Type)
}

fn applyVectorSize(attr: Attribute, p: *Parser, tok: TokenIndex, ty: *Type) !void {
if (!(ty.isInt() or ty.isFloat()) or !ty.isReal()) {
const orig_ty = try p.typeStr(ty.*);
ty.* = Type.invalid;
return p.errStr(.invalid_vec_elem_ty, tok, orig_ty);
const is_enum = ty.is(.@"enum");
if (!(ty.isInt() or ty.isFloat()) or !ty.isReal() or (is_enum and p.comp.langopts.emulate == .gcc)) {
try p.errStr(.invalid_vec_elem_ty, tok, try p.typeStr(ty.*));
return error.ParsingFailed;
}
if (is_enum) return;

const vec_bytes = attr.args.vector_size.bytes;
const ty_size = ty.sizeof(p.comp).?;
if (vec_bytes % ty_size != 0) {
Expand Down
3 changes: 0 additions & 3 deletions test/cases/ast/vectors.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
typedef: 'invalid'
name: invalid1

typedef: 'float'
name: invalid2

Expand Down
42 changes: 42 additions & 0 deletions test/cases/enum attributes clang.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//aro-args --emulate=clang
enum E {
is_deprecated __attribute__((deprecated)),
is_deprecated_with_msg __attribute__((deprecated("I am deprecated"))),
is_unavailable __attribute__((unavailable)),
is_unavailable_with_msg __attribute__((unavailable("I am not available"))),
newval,
};

void foo(void) {
int a = newval;
a = is_deprecated;
a = is_deprecated_with_msg;
a = is_unavailable;
a = is_unavailable_with_msg;
}

enum __attribute__((aligned(16))) Attributed {
Val,
};
_Static_assert(_Alignof(enum Attributed) == 16, "enum align");

enum Trailing {
Foo
} __attribute__((aligned(32)));
_Static_assert(_Alignof(enum Trailing) == 32, "enum align");

enum __attribute__((vector_size(32))) VectorSize1;

enum __attribute__((vector_size(32))) VectorSize2 {
A
};


#define EXPECTED_ERRORS "enum attributes clang.c:12:7: warning: 'is_deprecated' is deprecated [-Wdeprecated-declarations]" \
"enum attributes clang.c:3:33: note: 'is_deprecated' has been explicitly marked deprecated here" \
"enum attributes clang.c:13:7: warning: 'is_deprecated_with_msg' is deprecated: I am deprecated [-Wdeprecated-declarations]" \
"enum attributes clang.c:4:42: note: 'is_deprecated_with_msg' has been explicitly marked deprecated here" \
"enum attributes clang.c:14:7: error: 'is_unavailable' is unavailable" \
"enum attributes clang.c:5:34: note: 'is_unavailable' has been explicitly marked unavailable here" \
"enum attributes clang.c:15:7: error: 'is_unavailable_with_msg' is unavailable: I am not available" \
"enum attributes clang.c:6:44: note: 'is_unavailable_with_msg' has been explicitly marked unavailable here" \
8 changes: 8 additions & 0 deletions test/cases/enum attributes gcc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//aro-args --emulate=gcc

enum __attribute__((vector_size(32))) VectorSize2 {
A
};

#define EXPECTED_ERRORS "enum attributes gcc.c:3:21: error: invalid vector element type 'enum VectorSize2'" \

34 changes: 0 additions & 34 deletions test/cases/enum attributes.c

This file was deleted.

0 comments on commit 9f31df3

Please sign in to comment.