From f45c71fc04e3a9a374475f3a47a9e236ce8d7c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Tue, 10 Feb 2026 17:42:41 +0000 Subject: [PATCH] parser: port threadlocal, linksection, addrspace tests Port tests: - "threadlocal" - "linksection" - "addrspace" Implement full var decl proto with aligned_var_decl, local_var_decl, and global_var_decl node types for align/addrspace/linksection. Co-Authored-By: Claude Opus 4.6 (1M context) --- parser.c | 44 ++++++++++++++++++++++++++++++++------------ parser_test.zig | 25 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/parser.c b/parser.c index fcf0504ce0..8d03dce2e9 100644 --- a/parser.c +++ b/parser.c @@ -1312,24 +1312,44 @@ static AstNodeIndex parseVarDeclProto(Parser* p) { if (section_node == 0 && addrspace_node == 0) { if (align_node == 0) { - return addNode( - &p->nodes, + return addNode(&p->nodes, (AstNodeItem) { .tag = AST_NODE_SIMPLE_VAR_DECL, .main_token = mut_token, - .data = { - .lhs = type_node, - .rhs = 0, - }, + .data = { .lhs = type_node, .rhs = 0 }, }); } - fprintf(stderr, "parseVarDecl got something too complicated\n"); - exit(1); - } else { - fprintf(stderr, "parseVarDecl got something too complicated\n"); - exit(1); + if (type_node == 0) { + return addNode(&p->nodes, + (AstNodeItem) { + .tag = AST_NODE_ALIGNED_VAR_DECL, + .main_token = mut_token, + .data = { .lhs = align_node, .rhs = 0 }, + }); + } + return addNode(&p->nodes, + (AstNodeItem) { + .tag = AST_NODE_LOCAL_VAR_DECL, + .main_token = mut_token, + .data = { + .lhs = addExtra(p, + (AstNodeIndex[]) { type_node, align_node }, 2), + .rhs = 0, + }, + }); } - return 0; // tcc + return addNode(&p->nodes, + (AstNodeItem) { + .tag = AST_NODE_GLOBAL_VAR_DECL, + .main_token = mut_token, + .data = { + .lhs = addExtra(p, + (AstNodeIndex[]) { OPT(type_node), OPT(align_node), + OPT(addrspace_node), OPT(section_node) }, + 4), + .rhs = 0, + }, + }); } static AstTokenIndex parseBreakLabel(Parser* p) { diff --git a/parser_test.zig b/parser_test.zig index f61c6ae17d..1c89168790 100644 --- a/parser_test.zig +++ b/parser_test.zig @@ -1923,6 +1923,31 @@ test "zig fmt: allowzero pointer" { ); } +test "zig fmt: threadlocal" { + try testCanonical( + \\threadlocal var x: i32 = 1234; + \\ + ); +} + +test "zig fmt: linksection" { + try testCanonical( + \\export var aoeu: u64 linksection(".text.derp") = 1234; + \\export fn _start() linksection(".text.boot") callconv(.naked) noreturn {} + \\ + ); +} + +test "zig fmt: addrspace" { + try testCanonical( + \\export var python_length: u64 align(1) addrspace(.generic); + \\export var python_color: Color addrspace(.generic) = .green; + \\export var python_legs: u0 align(8) addrspace(.generic) linksection(".python") = 0; + \\export fn python_hiss() align(8) addrspace(.generic) linksection(".python") void; + \\ + ); +} + test "zig fmt: sentinel-terminated array type" { try testCanonical( \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {