authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-07 19:20:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-07 19:20:15-07:00
log73403d897caec40eff16226abb54098fa6623954
treecb59ce2d7005096b8c9267f347f5ea2b20b49e83
parent75cecef63ccaffda32b7ec2dd2d6cc3cb0b3d23a

stage2: add --debug-compile-errors CLI option

This is useful when debugging stage2 and getting an unexpected compile error, or when rendering a compile error crashes the compiler.

4 files changed, 20 insertions(+), 3 deletions(-)

src/Compilation.zig+5-2
...@@ -82,6 +82,9 @@ disable_c_depfile: bool,...@@ -82,6 +82,9 @@ disable_c_depfile: bool,
82time_report: bool,82time_report: bool,
83stack_report: bool,83stack_report: bool,
84unwind_tables: bool,84unwind_tables: bool,
85test_evented_io: bool,
86debug_compiler_runtime_libs: bool,
87debug_compile_errors: bool,
8588
86c_source_files: []const CSourceFile,89c_source_files: []const CSourceFile,
87clang_argv: []const []const u8,90clang_argv: []const []const u8,
...@@ -138,8 +141,6 @@ mutex: std.Thread.Mutex = .{},...@@ -138,8 +141,6 @@ mutex: std.Thread.Mutex = .{},
138141
139test_filter: ?[]const u8,142test_filter: ?[]const u8,
140test_name_prefix: ?[]const u8,143test_name_prefix: ?[]const u8,
141test_evented_io: bool,
142debug_compiler_runtime_libs: bool,
143144
144emit_asm: ?EmitLoc,145emit_asm: ?EmitLoc,
145emit_llvm_ir: ?EmitLoc,146emit_llvm_ir: ?EmitLoc,
...@@ -727,6 +728,7 @@ pub const InitOptions = struct {...@@ -727,6 +728,7 @@ pub const InitOptions = struct {
727 is_test: bool = false,728 is_test: bool = false,
728 test_evented_io: bool = false,729 test_evented_io: bool = false,
729 debug_compiler_runtime_libs: bool = false,730 debug_compiler_runtime_libs: bool = false,
731 debug_compile_errors: bool = false,
730 /// Normally when you create a `Compilation`, Zig will automatically build732 /// Normally when you create a `Compilation`, Zig will automatically build
731 /// and link in required dependencies, such as compiler-rt and libc. When733 /// and link in required dependencies, such as compiler-rt and libc. When
732 /// building such dependencies themselves, this flag must be set to avoid734 /// building such dependencies themselves, this flag must be set to avoid
...@@ -1471,6 +1473,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1471,6 +1473,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1471 .test_name_prefix = options.test_name_prefix,1473 .test_name_prefix = options.test_name_prefix,
1472 .test_evented_io = options.test_evented_io,1474 .test_evented_io = options.test_evented_io,
1473 .debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,1475 .debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,
1476 .debug_compile_errors = options.debug_compile_errors,
1474 .work_queue_wait_group = undefined,1477 .work_queue_wait_group = undefined,
1475 .astgen_wait_group = undefined,1478 .astgen_wait_group = undefined,
1476 };1479 };
src/Sema.zig+9
...@@ -1213,6 +1213,15 @@ pub fn fail(...@@ -1213,6 +1213,15 @@ pub fn fail(
1213fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {1213fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
1214 @setCold(true);1214 @setCold(true);
12151215
1216 if (crash_report.is_enabled and sema.mod.comp.debug_compile_errors) {
1217 std.debug.print("compile error during Sema: {s}, src: {s}:{}\n", .{
1218 err_msg.msg,
1219 err_msg.src_loc.file_scope.sub_file_path,
1220 err_msg.src_loc.lazy,
1221 });
1222 crash_report.compilerPanic("unexpected compile error occurred", null);
1223 }
1224
1216 const mod = sema.mod;1225 const mod = sema.mod;
12171226
1218 {1227 {
src/crash_report.zig+1-1
...@@ -151,7 +151,7 @@ fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void {...@@ -151,7 +151,7 @@ fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void {
151 try decl.renderFullyQualifiedDebugName(stream);151 try decl.renderFullyQualifiedDebugName(stream);
152}152}
153153
154fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {154pub fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {
155 PanicSwitch.preDispatch();155 PanicSwitch.preDispatch();
156 @setCold(true);156 @setCold(true);
157 const ret_addr = @returnAddress();157 const ret_addr = @returnAddress();
src/main.zig+5
...@@ -433,6 +433,7 @@ const usage_build_generic =...@@ -433,6 +433,7 @@ const usage_build_generic =
433 \\ --verbose-cimport Enable compiler debug output for C imports433 \\ --verbose-cimport Enable compiler debug output for C imports
434 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features434 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
435 \\ --debug-log [scope] Enable printing debug/info log messages for scope435 \\ --debug-log [scope] Enable printing debug/info log messages for scope
436 \\ --debug-compile-errors Crash with helpful diagnostics at the first compile error
436 \\437 \\
437;438;
438439
...@@ -549,6 +550,7 @@ fn buildOutputType(...@@ -549,6 +550,7 @@ fn buildOutputType(
549 var single_threaded = false;550 var single_threaded = false;
550 var function_sections = false;551 var function_sections = false;
551 var watch = false;552 var watch = false;
553 var debug_compile_errors = false;
552 var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");554 var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
553 var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");555 var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
554 var verbose_air = false;556 var verbose_air = false;
...@@ -1078,6 +1080,8 @@ fn buildOutputType(...@@ -1078,6 +1080,8 @@ fn buildOutputType(
1078 linker_allow_shlib_undefined = false;1080 linker_allow_shlib_undefined = false;
1079 } else if (mem.eql(u8, arg, "-Bsymbolic")) {1081 } else if (mem.eql(u8, arg, "-Bsymbolic")) {
1080 linker_bind_global_refs_locally = true;1082 linker_bind_global_refs_locally = true;
1083 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
1084 debug_compile_errors = true;
1081 } else if (mem.eql(u8, arg, "--verbose-link")) {1085 } else if (mem.eql(u8, arg, "--verbose-link")) {
1082 verbose_link = true;1086 verbose_link = true;
1083 } else if (mem.eql(u8, arg, "--verbose-cc")) {1087 } else if (mem.eql(u8, arg, "--verbose-cc")) {
...@@ -2134,6 +2138,7 @@ fn buildOutputType(...@@ -2134,6 +2138,7 @@ fn buildOutputType(
2134 .disable_lld_caching = !have_enable_cache,2138 .disable_lld_caching = !have_enable_cache,
2135 .subsystem = subsystem,2139 .subsystem = subsystem,
2136 .wasi_exec_model = wasi_exec_model,2140 .wasi_exec_model = wasi_exec_model,
2141 .debug_compile_errors = debug_compile_errors,
2137 }) catch |err| {2142 }) catch |err| {
2138 fatal("unable to create compilation: {s}", .{@errorName(err)});2143 fatal("unable to create compilation: {s}", .{@errorName(err)});
2139 };2144 };