authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-01-22 15:18:39+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-01-22 15:46:58+01:00
logac004e1bf13d00804c30f55d479b554774798307
tree53f6540c14b043db95aed562945e66d0d9493dc5
parentfc5ae1c4096fa814b97671525db17c0b4f0b786d

stage1: Allow nameless test blocks

Nameless blocks are never filtered, the test prefix is still applied.

6 files changed, 17 insertions(+), 12 deletions(-)

lib/std/zig/ast.zig+1-1
...@@ -3231,7 +3231,7 @@ pub const Node = struct {...@@ -3231,7 +3231,7 @@ pub const Node = struct {
3231 base: Node = Node{ .tag = .TestDecl },3231 base: Node = Node{ .tag = .TestDecl },
3232 doc_comments: ?*DocComment,3232 doc_comments: ?*DocComment,
3233 test_token: TokenIndex,3233 test_token: TokenIndex,
3234 name: *Node,3234 name: ?*Node,
3235 body_node: *Node,3235 body_node: *Node,
32363236
3237 pub fn iterate(self: *const TestDecl, index: usize) ?*Node {3237 pub fn iterate(self: *const TestDecl, index: usize) ?*Node {
lib/std/zig/parse.zig+1-3
...@@ -366,9 +366,7 @@ const Parser = struct {...@@ -366,9 +366,7 @@ const Parser = struct {
366 /// TestDecl <- KEYWORD_test STRINGLITERALSINGLE Block366 /// TestDecl <- KEYWORD_test STRINGLITERALSINGLE Block
367 fn parseTestDecl(p: *Parser) !?*Node {367 fn parseTestDecl(p: *Parser) !?*Node {
368 const test_token = p.eatToken(.Keyword_test) orelse return null;368 const test_token = p.eatToken(.Keyword_test) orelse return null;
369 const name_node = try p.expectNode(parseStringLiteralSingle, .{369 const name_node = try p.parseStringLiteralSingle();
370 .ExpectedStringLiteral = .{ .token = p.tok_i },
371 });
372 const block_node = (try p.parseBlock(null)) orelse {370 const block_node = (try p.parseBlock(null)) orelse {
373 try p.errors.append(p.gpa, .{ .ExpectedLBrace = .{ .token = p.tok_i } });371 try p.errors.append(p.gpa, .{ .ExpectedLBrace = .{ .token = p.tok_i } });
374 return error.ParseError;372 return error.ParseError;
lib/std/zig/render.zig+2-1
...@@ -228,7 +228,8 @@ fn renderContainerDecl(allocator: *mem.Allocator, ais: anytype, tree: *ast.Tree,...@@ -228,7 +228,8 @@ fn renderContainerDecl(allocator: *mem.Allocator, ais: anytype, tree: *ast.Tree,
228228
229 try renderDocComments(tree, ais, test_decl, test_decl.doc_comments);229 try renderDocComments(tree, ais, test_decl, test_decl.doc_comments);
230 try renderToken(tree, ais, test_decl.test_token, .Space);230 try renderToken(tree, ais, test_decl.test_token, .Space);
231 try renderExpression(allocator, ais, tree, test_decl.name, .Space);231 if (test_decl.name) |name|
232 try renderExpression(allocator, ais, tree, name, .Space);
232 try renderExpression(allocator, ais, tree, test_decl.body_node, space);233 try renderExpression(allocator, ais, tree, test_decl.body_node, space);
233 },234 },
234235
src/stage1/all_types.hpp+1
...@@ -797,6 +797,7 @@ struct AstNodeVariableDeclaration {...@@ -797,6 +797,7 @@ struct AstNodeVariableDeclaration {
797};797};
798798
799struct AstNodeTestDecl {799struct AstNodeTestDecl {
800 // nullptr if the test declaration has no name
800 Buf *name;801 Buf *name;
801802
802 AstNode *body;803 AstNode *body;
src/stage1/analyze.cpp+10-5
...@@ -3871,13 +3871,18 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope...@@ -3871,13 +3871,18 @@ static void preview_test_decl(CodeGen *g, AstNode *node, ScopeDecls *decls_scope
3871 return;3871 return;
38723872
3873 Buf *decl_name_buf = node->data.test_decl.name;3873 Buf *decl_name_buf = node->data.test_decl.name;
3874 Buf *test_name;
38743875
3875 Buf *test_name = g->test_name_prefix ?3876 if (decl_name_buf != nullptr) {
3876 buf_sprintf("%s%s", buf_ptr(g->test_name_prefix), buf_ptr(decl_name_buf)) : decl_name_buf;3877 test_name = g->test_name_prefix ?
3878 buf_sprintf("%s%s", buf_ptr(g->test_name_prefix), buf_ptr(decl_name_buf)) : decl_name_buf;
38773879
3878 if (g->test_filter != nullptr && buf_len(test_name) > 0 &&3880 if (g->test_filter != nullptr && strstr(buf_ptr(test_name), buf_ptr(g->test_filter)) == nullptr) {
3879 strstr(buf_ptr(test_name), buf_ptr(g->test_filter)) == nullptr) {3881 return;
3880 return;3882 }
3883 } else {
3884 // Unnamed test blocks are always executed.
3885 test_name = buf_sprintf("%s", g->test_name_prefix ? buf_ptr(g->test_name_prefix) : "");
3881 }3886 }
38823887
3883 TldFn *tld_fn = heap::c_allocator.create<TldFn>();3888 TldFn *tld_fn = heap::c_allocator.create<TldFn>();
src/stage1/parser.cpp+2-2
...@@ -658,10 +658,10 @@ static AstNode *ast_parse_test_decl(ParseContext *pc) {...@@ -658,10 +658,10 @@ static AstNode *ast_parse_test_decl(ParseContext *pc) {
658 if (test == nullptr)658 if (test == nullptr)
659 return nullptr;659 return nullptr;
660660
661 Token *name = expect_token(pc, TokenIdStringLiteral);661 Token *name = eat_token_if(pc, TokenIdStringLiteral);
662 AstNode *block = ast_expect(pc, ast_parse_block);662 AstNode *block = ast_expect(pc, ast_parse_block);
663 AstNode *res = ast_create_node(pc, NodeTypeTestDecl, test);663 AstNode *res = ast_create_node(pc, NodeTypeTestDecl, test);
664 res->data.test_decl.name = token_buf(name);664 res->data.test_decl.name = name ? token_buf(name) : nullptr;
665 res->data.test_decl.body = block;665 res->data.test_decl.body = block;
666 return res;666 return res;
667}667}