Skip to content

Commit

Permalink
Attribute: implement alloc_align
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Dec 7, 2024
1 parent 2963249 commit a3cde8d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/aro/Attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,21 @@ pub fn applyFunctionAttributes(p: *Parser, ty: Type, attr_buf_start: usize) !Typ
try ignoredAttrErr(p, tok, attr.tag, "functions that do not return pointers");
}
},
.alloc_align => {
if (base_ty.returnType().isPtr()) {
const params = base_ty.params();
if (attr.args.alloc_align.position == 0 or attr.args.alloc_align.position > params.len) {
try p.errExtra(.attribute_param_out_of_bounds, tok, .{ .attr_arg_count = .{ .attribute = .alloc_align, .expected = 1 } });
} else if (!params[attr.args.alloc_align.position - 1].ty.isInt()) {
try p.errTok(.alloc_align_required_int_param, tok);
} else {
try p.attr_application_buf.append(p.gpa, attr);
}
} else {
try p.errTok(.alloc_align_requires_ptr_return, tok);
}
},
.access,
.alloc_align,
.alloc_size,
.artificial,
.assume_aligned,
Expand Down
14 changes: 14 additions & 0 deletions src/aro/Diagnostics/messages.def
Original file line number Diff line number Diff line change
Expand Up @@ -2566,3 +2566,17 @@ packed_member_address
.opt = W("address-of-packed-member")
.kind = .warning
.extra = .str

alloc_align_requires_ptr_return
.msg = "'alloc_align' attribute only applies to return values that are pointers"
.opt = W("ignored-attributes")
.kind = .warning

attribute_param_out_of_bounds
.msg = "'{s}' attribute parameter {d} is out of bounds"
.kind = .@"error"
.extra = .attr_arg_count

alloc_align_required_int_param
.msg = "'alloc_align' attribute argument may only refer to a function parameter of integer type"
.kind = .@"error"
12 changes: 12 additions & 0 deletions test/cases/alloc_align attribute.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
__attribute__((alloc_align(0))) int* foo1(int x, double y);
__attribute__((alloc_align(1))) int* foo2(int x, double y);
__attribute__((alloc_align(2))) int* foo3(int x, double y);
__attribute__((alloc_align(3))) int* foo4(int x, double y);
__attribute__((alloc_align(1))) int foo5(int x, double y);

#define EXPECTED_ERRORS \
"alloc_align attribute.c:1:16: error: 'alloc_align' attribute parameter 1 is out of bounds" \
"alloc_align attribute.c:3:16: error: 'alloc_align' attribute argument may only refer to a function parameter of integer type" \
"alloc_align attribute.c:4:16: error: 'alloc_align' attribute parameter 1 is out of bounds" \
"alloc_align attribute.c:5:16: warning: 'alloc_align' attribute only applies to return values that are pointers [-Wignored-attributes]" \

0 comments on commit a3cde8d

Please sign in to comment.