From c1cf158729f4d726639a5695754957f9f45f89da Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Sat, 5 Feb 2022 09:09:55 -0700 Subject: [PATCH] Replace argvCmd with std.mem.join --- lib/std/build/RunStep.zig | 13 +------------ src/main.zig | 27 ++++++++------------------- test/tests.zig | 13 +------------ 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/lib/std/build/RunStep.zig b/lib/std/build/RunStep.zig index 6bd1bda952..e8544921d9 100644 --- a/lib/std/build/RunStep.zig +++ b/lib/std/build/RunStep.zig @@ -138,17 +138,6 @@ pub fn setEnvironmentVariable(self: *RunStep, key: []const u8, value: []const u8 ) catch unreachable; } -fn argvCmd(allocator: Allocator, argv: []const []const u8) ![]u8 { - var cmd = std.ArrayList(u8).init(allocator); - defer cmd.deinit(); - for (argv[0 .. argv.len - 1]) |arg| { - try cmd.appendSlice(arg); - try cmd.append(' '); - } - try cmd.appendSlice(argv[argv.len - 1]); - return cmd.toOwnedSlice(); -} - pub fn expectStdErrEqual(self: *RunStep, bytes: []const u8) void { self.stderr_action = .{ .expect_exact = self.builder.dupe(bytes) }; } @@ -189,7 +178,7 @@ fn make(step: *Step) !void { const argv = argv_list.items; if (!std.process.can_spawn) { - const cmd = try argvCmd(self.builder.allocator, argv); + const cmd = try std.mem.join(self.builder.allocator, " ", argv); std.debug.print("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd }); self.builder.allocator.free(cmd); return ExecError.ExecNotSupported; diff --git a/src/main.zig b/src/main.zig index d6688081f1..6a1416cd6b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2881,7 +2881,7 @@ fn runOrTest( // execv releases the locks; no need to destroy the Compilation here. const err = std.process.execv(gpa, argv.items); try warnAboutForeignBinaries(gpa, arena, arg_mode, target_info, link_libc); - const cmd = try argvCmd(arena, argv.items); + const cmd = try std.mem.join(arena, " ", argv.items); fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd }); } else if (std.process.can_spawn) { const child = try std.ChildProcess.init(argv.items, gpa); @@ -2900,7 +2900,7 @@ fn runOrTest( const term = child.spawnAndWait() catch |err| { try warnAboutForeignBinaries(gpa, arena, arg_mode, target_info, link_libc); - const cmd = try argvCmd(arena, argv.items); + const cmd = try std.mem.join(arena, " ", argv.items); fatal("the following command failed with '{s}':\n{s}", .{ @errorName(err), cmd }); }; switch (arg_mode) { @@ -2931,12 +2931,12 @@ fn runOrTest( if (code == 0) { if (!watch) return cleanExit(); } else { - const cmd = try argvCmd(arena, argv.items); + const cmd = try std.mem.join(arena, " ", argv.items); fatal("the following test command failed with exit code {d}:\n{s}", .{ code, cmd }); } }, else => { - const cmd = try argvCmd(arena, argv.items); + const cmd = try std.mem.join(arena, " ", argv.items); fatal("the following test command crashed:\n{s}", .{cmd}); }, } @@ -2944,7 +2944,7 @@ fn runOrTest( else => unreachable, } } else { - const cmd = try argvCmd(arena, argv.items); + const cmd = try std.mem.join(arena, " ", argv.items); fatal("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd }); } } @@ -3573,32 +3573,21 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi if (prominent_compile_errors) { fatal("the build command failed with exit code {d}", .{code}); } else { - const cmd = try argvCmd(arena, child_argv); + const cmd = try std.mem.join(arena, " ", child_argv); fatal("the following build command failed with exit code {d}:\n{s}", .{ code, cmd }); } }, else => { - const cmd = try argvCmd(arena, child_argv); + const cmd = try std.mem.join(arena, " ", child_argv); fatal("the following build command crashed:\n{s}", .{cmd}); }, } } else { - const cmd = try argvCmd(arena, child_argv); + const cmd = try std.mem.join(arena, " ", child_argv); fatal("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd }); } } -fn argvCmd(allocator: Allocator, argv: []const []const u8) ![]u8 { - var cmd = std.ArrayList(u8).init(allocator); - defer cmd.deinit(); - for (argv[0 .. argv.len - 1]) |arg| { - try cmd.appendSlice(arg); - try cmd.append(' '); - } - try cmd.appendSlice(argv[argv.len - 1]); - return cmd.toOwnedSlice(); -} - fn readSourceFileToEndAlloc( allocator: mem.Allocator, input: *const fs.File, diff --git a/test/tests.zig b/test/tests.zig index bd6c82cbb7..5b15da2bcb 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -28,17 +28,6 @@ pub const TranslateCContext = @import("src/translate_c.zig").TranslateCContext; pub const RunTranslatedCContext = @import("src/run_translated_c.zig").RunTranslatedCContext; pub const CompareOutputContext = @import("src/compare_output.zig").CompareOutputContext; -fn argvCmd(allocator: Allocator, argv: []const []const u8) ![]u8 { - var cmd = std.ArrayList(u8).init(allocator); - defer cmd.deinit(); - for (argv[0 .. argv.len - 1]) |arg| { - try cmd.appendSlice(arg); - try cmd.append(' '); - } - try cmd.appendSlice(argv[argv.len - 1]); - return cmd.toOwnedSlice(); -} - const TestTarget = struct { target: CrossTarget = @as(CrossTarget, .{}), mode: std.builtin.Mode = .Debug, @@ -736,7 +725,7 @@ pub const StackTracesContext = struct { std.debug.print("Test {d}/{d} {s}...", .{ self.test_index + 1, self.context.test_index, self.name }); if (!std.process.can_spawn) { - const cmd = try argvCmd(b.allocator, args.items); + const cmd = try std.mem.join(b.allocator, " ", args.items); std.debug.print("the following command cannot be executed ({s} does not support spawning a child process):\n{s}", .{ @tagName(builtin.os.tag), cmd }); b.allocator.free(cmd); return ExecError.ExecNotSupported;