replace %defer with errdefer

See #632

now we have 1 less sigil
This commit is contained in:
Andrew Kelley
2018-01-23 23:08:09 -05:00
parent ad2527d47a
commit b3a6faf13e
21 changed files with 87 additions and 89 deletions

View File

@@ -30,14 +30,14 @@ pub const BufMap = struct {
pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void {
if (self.hash_map.get(key)) |entry| {
const value_copy = try self.copy(value);
%defer self.free(value_copy);
errdefer self.free(value_copy);
_ = try self.hash_map.put(key, value_copy);
self.free(entry.value);
} else {
const key_copy = try self.copy(key);
%defer self.free(key_copy);
errdefer self.free(key_copy);
const value_copy = try self.copy(value);
%defer self.free(value_copy);
errdefer self.free(value_copy);
_ = try self.hash_map.put(key_copy, value_copy);
}
}

View File

@@ -27,7 +27,7 @@ pub const BufSet = struct {
pub fn put(self: &BufSet, key: []const u8) -> %void {
if (self.hash_map.get(key) == null) {
const key_copy = try self.copy(key);
%defer self.free(key_copy);
errdefer self.free(key_copy);
_ = try self.hash_map.put(key_copy, {});
}
}

View File

@@ -71,7 +71,7 @@ pub const NullTerminated2DArray = struct {
byte_count += index_size;
const buf = try allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count);
%defer allocator.free(buf);
errdefer allocator.free(buf);
var write_index = index_size;
const index_buf = ([]?&u8)(buf);

View File

@@ -248,10 +248,10 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) -> %&ElfStackTrace {
.compile_unit_list = ArrayList(CompileUnit).init(allocator),
};
st.self_exe_file = try os.openSelfExe();
%defer st.self_exe_file.close();
errdefer st.self_exe_file.close();
try st.elf.openFile(allocator, &st.self_exe_file);
%defer st.elf.close();
errdefer st.elf.close();
st.debug_info = (try st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;
st.debug_abbrev = (try st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;
@@ -524,7 +524,7 @@ const LineNumberProgram = struct {
return error.InvalidDebugInfo;
} else self.include_dirs[file_entry.dir_index];
const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name);
%defer self.file_entries.allocator.free(file_name);
errdefer self.file_entries.allocator.free(file_name);
return LineInfo {
.line = if (self.prev_line >= 0) usize(self.prev_line) else 0,
.column = self.prev_column,
@@ -563,7 +563,7 @@ fn getString(st: &ElfStackTrace, offset: u64) -> %[]u8 {
fn readAllocBytes(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %[]u8 {
const buf = try global_allocator.alloc(u8, size);
%defer global_allocator.free(buf);
errdefer global_allocator.free(buf);
if ((try in_stream.read(buf)) < size) return error.EndOfFile;
return buf;
}

View File

@@ -183,7 +183,7 @@ pub const Elf = struct {
try elf.in_file.seekTo(elf.section_header_offset);
elf.section_headers = try elf.allocator.alloc(SectionHeader, sh_entry_count);
%defer elf.allocator.free(elf.section_headers);
errdefer elf.allocator.free(elf.section_headers);
if (elf.is_64) {
if (sh_entry_size != 64) return error.InvalidFormat;

View File

@@ -550,7 +550,7 @@ pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len
const size = try file.getEndPos();
const buf = try allocator.alloc(u8, size + extra_len);
%defer allocator.free(buf);
errdefer allocator.free(buf);
var adapter = FileInStream.init(&file);
try adapter.stream.readNoEof(buf[0..size]);

View File

@@ -440,7 +440,7 @@ pub fn join(allocator: &Allocator, sep: u8, strings: ...) -> %[]u8 {
}
const buf = try allocator.alloc(u8, total_strings_len);
%defer allocator.free(buf);
errdefer allocator.free(buf);
var buf_index: usize = 0;
comptime var string_i = 0;

View File

@@ -76,7 +76,7 @@ pub const ChildProcess = struct {
/// On success must call deinit.
pub fn init(argv: []const []const u8, allocator: &mem.Allocator) -> %&ChildProcess {
const child = try allocator.create(ChildProcess);
%defer allocator.destroy(child);
errdefer allocator.destroy(child);
*child = ChildProcess {
.allocator = allocator,
@@ -336,13 +336,13 @@ pub const ChildProcess = struct {
install_SIGCHLD_handler();
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined;
%defer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
errdefer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined;
%defer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); };
errdefer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); };
const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined;
%defer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); };
errdefer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); };
const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
const dev_null_fd = if (any_ignore)
@@ -367,7 +367,7 @@ pub const ChildProcess = struct {
// This pipe is used to communicate errors between the time of fork
// and execve from the child process to the parent process.
const err_pipe = try makePipe();
%defer destroyPipe(err_pipe);
errdefer destroyPipe(err_pipe);
block_SIGCHLD();
const pid_result = posix.fork();
@@ -479,7 +479,7 @@ pub const ChildProcess = struct {
g_hChildStd_IN_Rd = null;
},
}
%defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr); };
errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr); };
var g_hChildStd_OUT_Rd: ?windows.HANDLE = null;
var g_hChildStd_OUT_Wr: ?windows.HANDLE = null;
@@ -497,7 +497,7 @@ pub const ChildProcess = struct {
g_hChildStd_OUT_Wr = null;
},
}
%defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr); };
errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr); };
var g_hChildStd_ERR_Rd: ?windows.HANDLE = null;
var g_hChildStd_ERR_Wr: ?windows.HANDLE = null;
@@ -515,7 +515,7 @@ pub const ChildProcess = struct {
g_hChildStd_ERR_Wr = null;
},
}
%defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); };
errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); };
const cmd_line = try windowsCreateCommandLine(self.allocator, self.argv);
defer self.allocator.free(cmd_line);
@@ -722,7 +722,7 @@ fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const S
var rd_h: windows.HANDLE = undefined;
var wr_h: windows.HANDLE = undefined;
try windowsMakePipe(&rd_h, &wr_h, sattr);
%defer windowsDestroyPipe(rd_h, wr_h);
errdefer windowsDestroyPipe(rd_h, wr_h);
try windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0);
*rd = rd_h;
*wr = wr_h;
@@ -732,7 +732,7 @@ fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const
var rd_h: windows.HANDLE = undefined;
var wr_h: windows.HANDLE = undefined;
try windowsMakePipe(&rd_h, &wr_h, sattr);
%defer windowsDestroyPipe(rd_h, wr_h);
errdefer windowsDestroyPipe(rd_h, wr_h);
try windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0);
*rd = rd_h;
*wr = wr_h;

View File

@@ -311,7 +311,7 @@ pub fn createNullDelimitedEnvMap(allocator: &Allocator, env_map: &const BufMap)
const envp_count = env_map.count();
const envp_buf = try allocator.alloc(?&u8, envp_count + 1);
mem.set(?&u8, envp_buf, null);
%defer freeNullDelimitedEnvMap(allocator, envp_buf);
errdefer freeNullDelimitedEnvMap(allocator, envp_buf);
{
var it = env_map.iterator();
var i: usize = 0;
@@ -421,7 +421,7 @@ pub var posix_environ_raw: []&u8 = undefined;
/// Caller must free result when done.
pub fn getEnvMap(allocator: &Allocator) -> %BufMap {
var result = BufMap.init(allocator);
%defer result.deinit();
errdefer result.deinit();
if (is_windows) {
const ptr = windows.GetEnvironmentStringsA() ?? return error.OutOfMemory;
@@ -489,7 +489,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 {
defer allocator.free(key_with_null);
var buf = try allocator.alloc(u8, 256);
%defer allocator.free(buf);
errdefer allocator.free(buf);
while (true) {
const windows_buf_len = try math.cast(windows.DWORD, buf.len);
@@ -521,7 +521,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {
switch (builtin.os) {
Os.windows => {
var buf = try allocator.alloc(u8, 256);
%defer allocator.free(buf);
errdefer allocator.free(buf);
while (true) {
const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);
@@ -543,7 +543,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {
},
else => {
var buf = try allocator.alloc(u8, 1024);
%defer allocator.free(buf);
errdefer allocator.free(buf);
while (true) {
const err = posix.getErrno(posix.getcwd(buf.ptr, buf.len));
if (err == posix.ERANGE) {
@@ -724,7 +724,7 @@ pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: [
var out_file = try io.File.openWriteMode(tmp_path, mode, allocator);
defer out_file.close();
%defer _ = deleteFile(allocator, tmp_path);
errdefer _ = deleteFile(allocator, tmp_path);
var in_file = try io.File.openRead(source_path, allocator);
defer in_file.close();
@@ -1074,7 +1074,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
path_buf[pathname.len] = 0;
var result_buf = try allocator.alloc(u8, 1024);
%defer allocator.free(result_buf);
errdefer allocator.free(result_buf);
while (true) {
const ret_val = posix.readlink(path_buf.ptr, result_buf.ptr, result_buf.len);
const err = posix.getErrno(ret_val);
@@ -1443,7 +1443,7 @@ pub fn argsAlloc(allocator: &mem.Allocator) -> %[]const []u8 {
const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len);
const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len);
const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes);
%defer allocator.free(buf);
errdefer allocator.free(buf);
const result_slice_list = ([][]u8)(buf[0..slice_list_bytes]);
const result_contents = buf[slice_list_bytes..];
@@ -1556,7 +1556,7 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
},
Os.windows => {
var out_path = try Buffer.initSize(allocator, 0xff);
%defer out_path.deinit();
errdefer out_path.deinit();
while (true) {
const dword_len = try math.cast(windows.DWORD, out_path.len());
const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len);
@@ -1579,7 +1579,7 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
const ret1 = c._NSGetExecutablePath(undefined, &u32_len);
assert(ret1 != 0);
const bytes = try allocator.alloc(u8, u32_len);
%defer allocator.free(bytes);
errdefer allocator.free(bytes);
const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len);
assert(ret2 == 0);
return bytes;
@@ -1598,13 +1598,13 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 {
// This path cannot be opened, but it's valid for determining the directory
// the executable was in when it was run.
const full_exe_path = try readLink(allocator, "/proc/self/exe");
%defer allocator.free(full_exe_path);
errdefer allocator.free(full_exe_path);
const dir = path.dirname(full_exe_path);
return allocator.shrink(u8, full_exe_path, dir.len);
},
Os.windows, Os.macosx, Os.ios => {
const self_exe_path = try selfExePath(allocator);
%defer allocator.free(self_exe_path);
errdefer allocator.free(self_exe_path);
const dirname = os.path.dirname(self_exe_path);
return allocator.shrink(u8, self_exe_path, dirname.len);
},

View File

@@ -468,7 +468,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8
}
have_drive_kind = parsed_cwd.kind;
}
%defer allocator.free(result);
errdefer allocator.free(result);
// Now we know the disk designator to use, if any, and what kind it is. And our result
// is big enough to append all the paths to.
@@ -551,7 +551,7 @@ pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) -> %[]u8 {
mem.copy(u8, result, cwd);
result_index += cwd.len;
}
%defer allocator.free(result);
errdefer allocator.free(result);
for (paths[first_index..]) |p, i| {
var it = mem.split(p, "/");
@@ -943,7 +943,7 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8)
}
const up_index_end = up_count * "..\\".len;
const result = try allocator.alloc(u8, up_index_end + to_rest.len);
%defer allocator.free(result);
errdefer allocator.free(result);
var result_index: usize = 0;
while (result_index < up_index_end) {
@@ -993,7 +993,7 @@ pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) ->
}
const up_index_end = up_count * "../".len;
const result = try allocator.alloc(u8, up_index_end + to_rest.len);
%defer allocator.free(result);
errdefer allocator.free(result);
var result_index: usize = 0;
while (result_index < up_index_end) {
@@ -1100,7 +1100,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
}
defer os.close(h_file);
var buf = try allocator.alloc(u8, 256);
%defer allocator.free(buf);
errdefer allocator.free(buf);
while (true) {
const buf_len = math.cast(windows.DWORD, buf.len) catch return error.NameTooLong;
const result = windows.GetFinalPathNameByHandleA(h_file, buf.ptr, buf_len, windows.VOLUME_NAME_DOS);
@@ -1144,7 +1144,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
defer allocator.free(pathname_buf);
const result_buf = try allocator.alloc(u8, posix.PATH_MAX);
%defer allocator.free(result_buf);
errdefer allocator.free(result_buf);
mem.copy(u8, pathname_buf, pathname);
pathname_buf[pathname.len] = 0;

View File

@@ -133,7 +133,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)
break :x bytes_needed;
};
const result = try allocator.alloc(u8, bytes_needed);
%defer allocator.free(result);
errdefer allocator.free(result);
var it = env_map.iterator();
var i: usize = 0;