nobody likes my std.process.cleanExit idea

it's appropriate for the self-hosted compiler though, so this commit
moves it from std lib to stage2 src code.
This commit is contained in:
Andrew Kelley 2020-09-23 20:52:33 -07:00
parent b08fd0e8fc
commit 7bbd152dcc
2 changed files with 19 additions and 19 deletions

View File

@ -19,19 +19,6 @@ pub const exit = os.exit;
pub const changeCurDir = os.chdir; pub const changeCurDir = os.chdir;
pub const changeCurDirC = os.chdirC; pub const changeCurDirC = os.chdirC;
/// Indicate that we are now terminating with a successful exit code.
/// In debug builds, this is a no-op, so that the calling code's
/// cleanup mechanisms are tested and so that external tools that
/// check for resource leaks can be accurate. In release builds, this
/// calls exit(0), and does not return.
pub fn cleanExit() void {
if (builtin.mode == .Debug) {
return;
} else {
exit(0);
}
}
/// The result is a slice of `out_buffer`, from index `0`. /// The result is a slice of `out_buffer`, from index `0`.
pub fn getCwd(out_buffer: []u8) ![]u8 { pub fn getCwd(out_buffer: []u8) ![]u8 {
return os.getcwd(out_buffer); return os.getcwd(out_buffer);

View File

@ -437,7 +437,7 @@ pub fn buildOutputType(
if (mem.startsWith(u8, arg, "-")) { if (mem.startsWith(u8, arg, "-")) {
if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
try io.getStdOut().writeAll(usage_build_generic); try io.getStdOut().writeAll(usage_build_generic);
return process.cleanExit(); return cleanExit();
} else if (mem.eql(u8, arg, "--")) { } else if (mem.eql(u8, arg, "--")) {
if (arg_mode == .run) { if (arg_mode == .run) {
runtime_args_start = i + 1; runtime_args_start = i + 1;
@ -1506,7 +1506,7 @@ pub fn buildOutputType(
else => process.exit(1), else => process.exit(1),
} }
if (!watch) if (!watch)
return process.cleanExit(); return cleanExit();
}, },
else => {}, else => {},
} }
@ -1667,7 +1667,7 @@ pub fn cmdLibC(gpa: *Allocator, args: []const []const u8) !void {
if (mem.eql(u8, arg, "--help")) { if (mem.eql(u8, arg, "--help")) {
const stdout = io.getStdOut().writer(); const stdout = io.getStdOut().writer();
try stdout.writeAll(usage_libc); try stdout.writeAll(usage_libc);
return process.cleanExit(); return cleanExit();
} else { } else {
fatal("unrecognized parameter: '{}'", .{arg}); fatal("unrecognized parameter: '{}'", .{arg});
} }
@ -1724,7 +1724,7 @@ pub fn cmdInit(
if (mem.startsWith(u8, arg, "-")) { if (mem.startsWith(u8, arg, "-")) {
if (mem.eql(u8, arg, "--help")) { if (mem.eql(u8, arg, "--help")) {
try io.getStdOut().writeAll(usage_init); try io.getStdOut().writeAll(usage_init);
return process.cleanExit(); return cleanExit();
} else { } else {
fatal("unrecognized parameter: '{}'", .{arg}); fatal("unrecognized parameter: '{}'", .{arg});
} }
@ -2012,7 +2012,7 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
const term = try child.spawnAndWait(); const term = try child.spawnAndWait();
switch (term) { switch (term) {
.Exited => |code| { .Exited => |code| {
if (code == 0) return process.cleanExit(); if (code == 0) return cleanExit();
try cmd.writer().print("failed with exit code {}:\n", .{code}); try cmd.writer().print("failed with exit code {}:\n", .{code});
}, },
else => { else => {
@ -2073,7 +2073,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
if (mem.eql(u8, arg, "--help")) { if (mem.eql(u8, arg, "--help")) {
const stdout = io.getStdOut().outStream(); const stdout = io.getStdOut().outStream();
try stdout.writeAll(usage_fmt); try stdout.writeAll(usage_fmt);
return process.cleanExit(); return cleanExit();
} else if (mem.eql(u8, arg, "--color")) { } else if (mem.eql(u8, arg, "--color")) {
if (i + 1 >= args.len) { if (i + 1 >= args.len) {
fatal("expected [auto|on|off] after --color", .{}); fatal("expected [auto|on|off] after --color", .{});
@ -2804,3 +2804,16 @@ fn detectNativeTargetInfo(gpa: *Allocator, cross_target: std.zig.CrossTarget) !s
} }
return info; return info;
} }
/// Indicate that we are now terminating with a successful exit code.
/// In debug builds, this is a no-op, so that the calling code's
/// cleanup mechanisms are tested and so that external tools that
/// check for resource leaks can be accurate. In release builds, this
/// calls exit(0), and does not return.
pub fn cleanExit() void {
if (std.builtin.mode == .Debug) {
return;
} else {
process.exit(0);
}
}