Skip to content

Commit

Permalink
Parser: canonicalize record type in offsetofMemberDesignator
Browse files Browse the repository at this point in the history
Closes #342
  • Loading branch information
Vexu committed Jul 25, 2022
1 parent 365d3b5 commit dde0bbb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5565,7 +5565,8 @@ fn offsetofMemberDesignator(p: *Parser, base_ty: Type) Error!Result {
const base_node = try p.addNode(.{ .tag = .default_init_expr, .ty = base_ty, .data = undefined });

var bit_offset = Value.int(0);
var lhs = try p.fieldAccessExtra(base_node, base_ty, base_field_name, false);
const base_record_ty = base_ty.canonicalize(.standard);
var lhs = try p.fieldAccessExtra(base_node, base_record_ty, base_field_name, false);

while (true) switch (p.tok_ids[p.tok_i]) {
.period => {
Expand All @@ -5578,7 +5579,8 @@ fn offsetofMemberDesignator(p: *Parser, base_ty: Type) Error!Result {
return error.ParsingFailed;
}
try p.validateFieldAccess(lhs.ty, lhs.ty, field_name_tok, field_name);
lhs = try p.fieldAccessExtra(lhs.node, lhs.ty, field_name, false);
const record_ty = lhs.ty.canonicalize(.standard);
lhs = try p.fieldAccessExtra(lhs.node, record_ty, field_name, false);
},
.l_bracket => {
const l_bracket_tok = p.tok_i;
Expand Down
5 changes: 5 additions & 0 deletions test/cases/offsetof.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ struct A{
int b = __builtin_offsetof(struct A, b);
int b = __builtin_offsetof(struct A, a[1]);

struct Foo {
char a;
} __attribute__((packed));
_Static_assert(__builtin_offsetof(struct Foo, a) == 0, "field Foo.a wrong bit offset");

#define EXPECTED_ERRORS "offsetof.c:1:28: error: offsetof requires struct or union type, 'int' invalid" \
"offsetof.c:3:28: error: offsetof of incomplete type 'struct A'" \
"offsetof.c:7:38: error: no member named 'b' in 'struct A'" \
Expand Down

0 comments on commit dde0bbb

Please sign in to comment.