Skip to content

Commit

Permalink
cleaner output from zig build when there are compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Jul 8, 2018
1 parent d8295c1 commit ced3aae
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
62 changes: 41 additions & 21 deletions std/debug/index.zig
Expand Up @@ -156,7 +156,7 @@ pub fn writeStackTrace(stack_trace: *const builtin.StackTrace, out_stream: var,
frame_index = (frame_index + 1) % stack_trace.instruction_addresses.len;
}) {
const return_address = stack_trace.instruction_addresses[frame_index];
try printSourceAtAddress(debug_info, out_stream, return_address);
try printSourceAtAddress(debug_info, out_stream, return_address, tty_color);
}
}

Expand Down Expand Up @@ -189,13 +189,11 @@ pub fn writeCurrentStackTrace(out_stream: var, allocator: *mem.Allocator, debug_
}
},
}
try printSourceAtAddress(debug_info, out_stream, return_address);
try printSourceAtAddress(debug_info, out_stream, return_address, tty_color);
}
}

fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: usize) !void {
const ptr_hex = "0x{x}";

fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: usize, tty_color: bool) !void {
switch (builtin.os) {
builtin.Os.windows => return error.UnsupportedDebugInfo,
builtin.Os.macosx => {
Expand All @@ -209,36 +207,58 @@ fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address: us
.address = address,
};
const symbol = debug_info.symbol_table.search(address) orelse &unknown;
try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n", symbol.name, address);
try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++ DIM ++ "0x{x}" ++ " in ??? (???)" ++ RESET ++ "\n", symbol.name, address);
},
else => {
const compile_unit = findCompileUnit(debug_info, address) catch {
try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", address);
if (tty_color) {
try out_stream.print("???:?:?: " ++ DIM ++ "0x{x} in ??? (???)" ++ RESET ++ "\n ???\n\n", address);
} else {
try out_stream.print("???:?:?: 0x{x} in ??? (???)\n ???\n\n", address);
}
return;
};
const compile_unit_name = try compile_unit.die.getAttrString(debug_info, DW.AT_name);
if (getLineNumberInfo(debug_info, compile_unit, address - 1)) |line_info| {
defer line_info.deinit();
try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n", line_info.file_name, line_info.line, line_info.column, address, compile_unit_name);
if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) {
if (line_info.column == 0) {
try out_stream.write("\n");
} else {
{
var col_i: usize = 1;
while (col_i < line_info.column) : (col_i += 1) {
try out_stream.writeByte(' ');
if (tty_color) {
try out_stream.print(
WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ "0x{x} in ??? ({})" ++ RESET ++ "\n",
line_info.file_name,
line_info.line,
line_info.column,
address,
compile_unit_name,
);
if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) {
if (line_info.column == 0) {
try out_stream.write("\n");
} else {
{
var col_i: usize = 1;
while (col_i < line_info.column) : (col_i += 1) {
try out_stream.writeByte(' ');
}
}
try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");
}
try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");
} else |err| switch (err) {
error.EndOfFile => {},
else => return err,
}
} else |err| switch (err) {
error.EndOfFile => {},
else => return err,
} else {
try out_stream.print(
"{}:{}:{}: 0x{x} in ??? ({})\n",
line_info.file_name,
line_info.line,
line_info.column,
address,
compile_unit_name,
);
}
} else |err| switch (err) {
error.MissingDebugInfo, error.InvalidDebugInfo => {
try out_stream.print(ptr_hex ++ " in ??? ({})\n", address, compile_unit_name);
try out_stream.print("0x{x} in ??? ({})\n", address, compile_unit_name);
},
else => return err,
}
Expand Down
9 changes: 6 additions & 3 deletions std/special/build_runner.zig
Expand Up @@ -122,10 +122,13 @@ pub fn main() !void {
return usageAndErr(&builder, true, try stderr_stream);

builder.make(targets.toSliceConst()) catch |err| {
if (err == error.InvalidStepName) {
return usageAndErr(&builder, true, try stderr_stream);
switch (err) {
error.InvalidStepName => {
return usageAndErr(&builder, true, try stderr_stream);
},
error.UncleanExit => os.exit(1),
else => return err,
}
return err;
};
}

Expand Down

0 comments on commit ced3aae

Please sign in to comment.