Skip to content

Commit

Permalink
Attribute: allow fallthrough to precede closing braces
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas authored and Vexu committed Jan 2, 2025
1 parent 46da345 commit 9ab16c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/aro/Attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1037,13 +1037,20 @@ pub fn applyStatementAttributes(p: *Parser, expr_start: TokenIndex, attr_buf_sta
const toks = p.attr_buf.items(.tok)[attr_buf_start..];
p.attr_application_buf.items.len = 0;
for (attrs, toks) |attr, tok| switch (attr.tag) {
.fallthrough => if (p.tok_ids[p.tok_i] != .keyword_case and p.tok_ids[p.tok_i] != .keyword_default) {
// TODO: this condition is not completely correct; the last statement of a compound
// statement is also valid if it precedes a switch label (so intervening '}' are ok,
// but only if they close a compound statement)
try p.errTok(.invalid_fallthrough, expr_start);
} else {
try p.attr_application_buf.append(p.gpa, attr);
.fallthrough => {
for (p.tok_ids[p.tok_i..]) |tok_id| {
switch (tok_id) {
.keyword_case, .keyword_default, .eof => {
try p.attr_application_buf.append(p.gpa, attr);
break;
},
.r_brace => {},
else => {
try p.errTok(.invalid_fallthrough, expr_start);
break;
},
}
}
},
else => try p.errStr(.cannot_apply_attribute_to_statement, tok, @tagName(attr.tag)),
};
Expand Down
15 changes: 15 additions & 0 deletions test/cases/compound stmt fallthrough.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
int main(void) {
int x = 5;
switch (x) {
case 1: {
__attribute__((fallthrough));
}
case 2: {
__attribute__((fallthrough));
}
default: {

}
}
return 0;
}

0 comments on commit 9ab16c6

Please sign in to comment.