Skip to content

Commit a88c5ac

Browse files
ehaasVexu
authored andcommitted
Attribute: implement alloc_align
1 parent 94ba1fb commit a88c5ac

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/aro/Attribute.zig

+14-1
Original file line numberDiff line numberDiff line change
@@ -945,8 +945,21 @@ pub fn applyFunctionAttributes(p: *Parser, ty: Type, attr_buf_start: usize) !Typ
945945
try ignoredAttrErr(p, tok, attr.tag, "functions that do not return pointers");
946946
}
947947
},
948+
.alloc_align => {
949+
if (base_ty.returnType().isPtr()) {
950+
const params = base_ty.params();
951+
if (attr.args.alloc_align.position == 0 or attr.args.alloc_align.position > params.len) {
952+
try p.errExtra(.attribute_param_out_of_bounds, tok, .{ .attr_arg_count = .{ .attribute = .alloc_align, .expected = 1 } });
953+
} else if (!params[attr.args.alloc_align.position - 1].ty.isInt()) {
954+
try p.errTok(.alloc_align_required_int_param, tok);
955+
} else {
956+
try p.attr_application_buf.append(p.gpa, attr);
957+
}
958+
} else {
959+
try p.errTok(.alloc_align_requires_ptr_return, tok);
960+
}
961+
},
948962
.access,
949-
.alloc_align,
950963
.alloc_size,
951964
.artificial,
952965
.assume_aligned,

src/aro/Diagnostics/messages.def

+14
Original file line numberDiff line numberDiff line change
@@ -2566,3 +2566,17 @@ packed_member_address
25662566
.opt = W("address-of-packed-member")
25672567
.kind = .warning
25682568
.extra = .str
2569+
2570+
alloc_align_requires_ptr_return
2571+
.msg = "'alloc_align' attribute only applies to return values that are pointers"
2572+
.opt = W("ignored-attributes")
2573+
.kind = .warning
2574+
2575+
attribute_param_out_of_bounds
2576+
.msg = "'{s}' attribute parameter {d} is out of bounds"
2577+
.kind = .@"error"
2578+
.extra = .attr_arg_count
2579+
2580+
alloc_align_required_int_param
2581+
.msg = "'alloc_align' attribute argument may only refer to a function parameter of integer type"
2582+
.kind = .@"error"

test/cases/alloc_align attribute.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
__attribute__((alloc_align(0))) int* foo1(int x, double y);
2+
__attribute__((alloc_align(1))) int* foo2(int x, double y);
3+
__attribute__((alloc_align(2))) int* foo3(int x, double y);
4+
__attribute__((alloc_align(3))) int* foo4(int x, double y);
5+
__attribute__((alloc_align(1))) int foo5(int x, double y);
6+
7+
#define EXPECTED_ERRORS \
8+
"alloc_align attribute.c:1:16: error: 'alloc_align' attribute parameter 1 is out of bounds" \
9+
"alloc_align attribute.c:3:16: error: 'alloc_align' attribute argument may only refer to a function parameter of integer type" \
10+
"alloc_align attribute.c:4:16: error: 'alloc_align' attribute parameter 1 is out of bounds" \
11+
"alloc_align attribute.c:5:16: warning: 'alloc_align' attribute only applies to return values that are pointers [-Wignored-attributes]" \
12+

0 commit comments

Comments
 (0)