Skip to content

Commit

Permalink
Parser: allow labels at end of block in C23
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Nov 6, 2023
1 parent 97c3de2 commit 08bde2c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,12 @@ const messages = struct {
const kind = .@"error";
const extra = .str;
};
pub const label_compound_end = struct {
const msg = "label at end of compound statement is a C2x extension";
const opt = "c2x-extensions";
const kind = .warning;
const suppress_version = .c2x;
};
};

list: std.ArrayListUnmanaged(Message) = .{},
Expand Down
14 changes: 11 additions & 3 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4294,7 +4294,7 @@ fn labeledStmt(p: *Parser) Error!?NodeIndex {

var labeled_stmt = Tree.Node{
.tag = .labeled_stmt,
.data = .{ .decl = .{ .name = name_tok, .node = try p.stmt() } },
.data = .{ .decl = .{ .name = name_tok, .node = try p.labelableStmt() } },
};
labeled_stmt.ty = try Attribute.applyLabelAttributes(p, labeled_stmt.ty, attr_buf_top);
return try p.addNode(labeled_stmt);
Expand Down Expand Up @@ -4341,7 +4341,7 @@ fn labeledStmt(p: *Parser) Error!?NodeIndex {
try p.errStr(.case_not_in_switch, case, "case");
}

const s = try p.stmt();
const s = try p.labelableStmt();
if (second_item) |some| return try p.addNode(.{
.tag = .case_range_stmt,
.data = .{ .if3 = .{ .cond = s, .body = (try p.addList(&.{ first_item.node, some.node })).start } },
Expand All @@ -4351,7 +4351,7 @@ fn labeledStmt(p: *Parser) Error!?NodeIndex {
});
} else if (p.eatToken(.keyword_default)) |default| {
_ = try p.expectToken(.colon);
const s = try p.stmt();
const s = try p.labelableStmt();
const node = try p.addNode(.{
.tag = .default_stmt,
.data = .{ .un = s },
Expand All @@ -4370,6 +4370,14 @@ fn labeledStmt(p: *Parser) Error!?NodeIndex {
} else return null;
}

fn labelableStmt(p: *Parser) Error!NodeIndex {
if (p.tok_ids[p.tok_i] == .r_brace) {
try p.err(.label_compound_end);
return p.addNode(.{ .tag = .null_stmt, .data = undefined });
}
return p.stmt();
}

const StmtExprState = struct {
last_expr_tok: TokenIndex = 0,
last_expr_res: Result = .{ .ty = .{ .specifier = .void } },
Expand Down

0 comments on commit 08bde2c

Please sign in to comment.