From 80fa871f4a087478336692406aaa9ec99eb53754 Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Wed, 12 Jun 2019 23:39:53 -0400 Subject: Add HashMap apis that assert the common case * putNoClobber() for put() * removeAssertDiscard() for remove() --- std/hash_map.zig | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'std') diff --git a/std/hash_map.zig b/std/hash_map.zig index 7336817954..c356f73527 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -183,6 +183,11 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 return putAssumeCapacity(self, key, value); } + /// Calls put() and asserts that no kv pair is clobbered. + pub fn putNoClobber(self: *Self, key: K, value: V) !void { + assert((try self.put(key, value)) == null); + } + pub fn putAssumeCapacity(self: *Self, key: K, value: V) ?KV { assert(self.count() < self.entries.len); self.incrementModificationCount(); @@ -203,6 +208,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 return hm.get(key) != null; } + /// Returns any kv pair that was removed. pub fn remove(hm: *Self, key: K) ?KV { if (hm.entries.len == 0) return null; hm.incrementModificationCount(); @@ -236,6 +242,11 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 return null; } + /// Calls remove(), asserts that a kv pair is removed, and discards it. + pub fn removeAssertDiscard(hm: *Self, key: K) void { + assert(hm.remove(key) != null); + } + pub fn iterator(hm: *const Self) Iterator { return Iterator{ .hm = hm, @@ -250,7 +261,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 try other.initCapacity(self.entries.len); var it = self.iterator(); while (it.next()) |entry| { - assert((try other.put(entry.key, entry.value)) == null); + try other.putNoClobber(entry.key, entry.value); } return other; } @@ -392,8 +403,8 @@ test "basic hash map usage" { testing.expect((try map.put(2, 22)) == null); testing.expect((try map.put(3, 33)) == null); testing.expect((try map.put(4, 44)) == null); - testing.expect((try map.put(5, 55)) == null); + map.putNoClobber(5, 55); testing.expect((try map.put(5, 66)).?.value == 55); testing.expect((try map.put(5, 55)).?.value == 66); @@ -422,6 +433,8 @@ test "basic hash map usage" { testing.expect(rmv1.?.value == 22); testing.expect(map.remove(2) == null); testing.expect(map.get(2) == null); + + map.removeAssertDiscard(3); } test "iterator hash map" { @@ -431,9 +444,9 @@ test "iterator hash map" { var reset_map = AutoHashMap(i32, i32).init(&direct_allocator.allocator); defer reset_map.deinit(); - testing.expect((try reset_map.put(1, 11)) == null); - testing.expect((try reset_map.put(2, 22)) == null); - testing.expect((try reset_map.put(3, 33)) == null); + reset_map.putNoClobber(1, 11); + reset_map.putNoClobber(2, 22); + reset_map.putNoClobber(3, 33); var keys = [_]i32{ 3, -- cgit v1.2.3 From 8a2c2da8051c31f75acd93303e87c5cc039207bc Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 12 Jun 2019 23:48:46 -0600 Subject: Handle putNoClobber errors --- std/hash_map.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'std') diff --git a/std/hash_map.zig b/std/hash_map.zig index c356f73527..cb030eab89 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -404,7 +404,7 @@ test "basic hash map usage" { testing.expect((try map.put(3, 33)) == null); testing.expect((try map.put(4, 44)) == null); - map.putNoClobber(5, 55); + try map.putNoClobber(5, 55); testing.expect((try map.put(5, 66)).?.value == 55); testing.expect((try map.put(5, 55)).?.value == 66); @@ -444,9 +444,9 @@ test "iterator hash map" { var reset_map = AutoHashMap(i32, i32).init(&direct_allocator.allocator); defer reset_map.deinit(); - reset_map.putNoClobber(1, 11); - reset_map.putNoClobber(2, 22); - reset_map.putNoClobber(3, 33); + try reset_map.putNoClobber(1, 11); + try reset_map.putNoClobber(2, 22); + try reset_map.putNoClobber(3, 33); var keys = [_]i32{ 3, -- cgit v1.2.3 From 82ab006e58230b413277fbffe5c304b83a3767e2 Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Thu, 13 Jun 2019 07:26:40 -0400 Subject: HashMap.getValue() --- std/hash_map.zig | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'std') diff --git a/std/hash_map.zig b/std/hash_map.zig index cb030eab89..df7ba740e8 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -204,6 +204,10 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 return hm.internalGet(key); } + pub fn getValue(hm: *const Self, key: K) ?V { + return if (hm.get(key)) |kv| kv.value else null; + } + pub fn contains(hm: *const Self, key: K) bool { return hm.get(key) != null; } @@ -427,12 +431,14 @@ test "basic hash map usage" { testing.expect(map.contains(2)); testing.expect(map.get(2).?.value == 22); + testing.expect(map.getValue(2).? == 22); const rmv1 = map.remove(2); testing.expect(rmv1.?.key == 2); testing.expect(rmv1.?.value == 22); testing.expect(map.remove(2) == null); testing.expect(map.get(2) == null); + testing.expect(map.getValue(2) == null); map.removeAssertDiscard(3); } -- cgit v1.2.3 From 9e8db5b7505a6c2e601c8a94b9d8a4282b5df184 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 12 Jun 2019 21:09:52 -0600 Subject: Remove const on argsAlloc --- std/process.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'std') diff --git a/std/process.zig b/std/process.zig index 410575297e..b39c6e1196 100644 --- a/std/process.zig +++ b/std/process.zig @@ -388,7 +388,7 @@ pub fn args() ArgIterator { } /// Caller must call argsFree on result. -pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { +pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 { if (builtin.os == .wasi) { var count: usize = undefined; var buf_size: usize = undefined; -- cgit v1.2.3 From 7c5ceb0c4cdf5fafadd92b13048bc9878554abc4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 14 Jun 2019 18:45:41 -0400 Subject: standard library integrates with knowledge of stripped debug info --- src/codegen.cpp | 9 ++++++++- src/target.cpp | 4 ++++ src/target.hpp | 1 + std/debug.zig | 11 +++++++---- 4 files changed, 20 insertions(+), 5 deletions(-) (limited to 'std') diff --git a/src/codegen.cpp b/src/codegen.cpp index 88168caff8..cee8c0ec0c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -203,6 +203,10 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget get_target_triple(&g->triple_str, g->zig_target); g->pointer_size_bytes = target_arch_pointer_bit_width(g->zig_target->arch) / 8; + if (!target_has_debug_info(g->zig_target)) { + g->strip_debug_symbols = true; + } + return g; } @@ -248,6 +252,9 @@ void codegen_set_errmsg_color(CodeGen *g, ErrColor err_color) { void codegen_set_strip(CodeGen *g, bool strip) { g->strip_debug_symbols = strip; + if (!target_has_debug_info(g->zig_target)) { + g->strip_debug_symbols = true; + } } void codegen_set_out_name(CodeGen *g, Buf *out_name) { @@ -7514,7 +7521,7 @@ static bool detect_single_threaded(CodeGen *g) { } static bool detect_err_ret_tracing(CodeGen *g) { - return !target_is_wasm(g->zig_target) && + return !g->strip_debug_symbols && g->build_mode != BuildModeFastRelease && g->build_mode != BuildModeSmallRelease; } diff --git a/src/target.cpp b/src/target.cpp index 7862f6d449..1d74304584 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -1585,3 +1585,7 @@ void target_libc_enum(size_t index, ZigTarget *out_target) { out_target->vendor = ZigLLVM_UnknownVendor; out_target->is_native = false; } + +bool target_has_debug_info(const ZigTarget *target) { + return !target_is_wasm(target); +} diff --git a/src/target.hpp b/src/target.hpp index 7fa99bcda8..7fca430df6 100644 --- a/src/target.hpp +++ b/src/target.hpp @@ -177,6 +177,7 @@ bool target_is_musl(const ZigTarget *target); bool target_is_wasm(const ZigTarget *target); bool target_is_single_threaded(const ZigTarget *target); bool target_supports_stack_probing(const ZigTarget *target); +bool target_has_debug_info(const ZigTarget *target); uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch); diff --git a/std/debug.zig b/std/debug.zig index 253f250713..ae9a5227c9 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -85,8 +85,8 @@ fn wantTtyColor() bool { /// TODO multithreaded awareness pub fn dumpCurrentStackTrace(start_addr: ?usize) void { const stderr = getStderrStream() catch return; - if (os.wasi.is_the_target) { - stderr.print("Unable to dump stack trace: unimplemented on WASI\n") catch return; + if (builtin.strip_debug_info) { + stderr.print("Unable to dump stack trace: debug info stripped\n") catch return; return; } const debug_info = getSelfDebugInfo() catch |err| { @@ -151,8 +151,8 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace /// TODO multithreaded awareness pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void { const stderr = getStderrStream() catch return; - if (os.wasi.is_the_target) { - stderr.print("Unable to dump stack trace: unimplemented on WASI\n") catch return; + if (builtin.strip_debug_info) { + stderr.print("Unable to dump stack trace: debug info stripped\n") catch return; return; } const debug_info = getSelfDebugInfo() catch |err| { @@ -223,6 +223,7 @@ pub fn writeStackTrace( debug_info: *DebugInfo, tty_color: bool, ) !void { + if (builtin.strip_debug_info) return error.MissingDebugInfo; var frame_index: usize = 0; var frames_left: usize = std.math.min(stack_trace.index, stack_trace.instruction_addresses.len); @@ -783,6 +784,8 @@ pub const OpenSelfDebugInfoError = error{ }; pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { + if (builtin.strip_debug_info) + return error.MissingDebugInfo; if (windows.is_the_target) { return openSelfDebugInfoWindows(allocator); } -- cgit v1.2.3