Skip to content

Commit

Permalink
Compilation: fix regressed assembly diagnostics
Browse files Browse the repository at this point in the history
Regressed by ziglang#17947
  • Loading branch information
jacobly0 committed May 25, 2024
1 parent 793f820 commit c26e77e
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4568,17 +4568,18 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
// We can't know the digest until we do the C compiler invocation,
// so we need a temporary filename.
const out_obj_path = try comp.tmpFilePath(arena, o_basename);
const out_diag_path = if (comp.clang_passthrough_mode)
undefined
else
try std.fmt.allocPrint(arena, "{s}.diag", .{out_obj_path});
var zig_cache_tmp_dir = try comp.local_cache_directory.handle.makeOpenPath("tmp", .{});
defer zig_cache_tmp_dir.close();

const out_dep_path: ?[]const u8 = if (comp.disable_c_depfile or !ext.clangSupportsDepFile())
const out_diag_path = if (comp.clang_passthrough_mode or !ext.clangSupportsDiagnostics())
null
else
try std.fmt.allocPrint(arena, "{s}.diag", .{out_obj_path});
const out_dep_path = if (comp.disable_c_depfile or !ext.clangSupportsDepFile())
null
else
try std.fmt.allocPrint(arena, "{s}.d", .{out_obj_path});

try comp.addCCArgs(arena, &argv, ext, out_dep_path, c_object.src.owner);
try argv.appendSlice(c_object.src.extra_flags);
try argv.appendSlice(c_object.src.cache_exempt_flags);
Expand All @@ -4590,25 +4591,25 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
.pch => argv.appendSliceAssumeCapacity(&.{ "-Xclang", "-emit-pch", "-o", out_obj_path }),
.stdout => argv.appendAssumeCapacity("-E"),
}
if (comp.clang_passthrough_mode) {
if (out_diag_path) |diag_file_path| {
argv.appendSliceAssumeCapacity(&.{ "--serialize-diagnostics", diag_file_path });
} else if (comp.clang_passthrough_mode) {
if (comp.emit_asm != null) {
argv.appendAssumeCapacity("-S");
} else if (comp.emit_llvm_ir != null) {
argv.appendSliceAssumeCapacity(&.{ "-emit-llvm", "-S" });
} else if (comp.emit_llvm_bc != null) {
argv.appendAssumeCapacity("-emit-llvm");
}
} else {
argv.appendSliceAssumeCapacity(&.{ "--serialize-diagnostics", out_diag_path });
}

if (comp.verbose_cc) {
dump_argv(argv.items);
}

// Just to save disk space, we delete the files that are never needed again.
defer if (!comp.clang_passthrough_mode) zig_cache_tmp_dir.deleteFile(std.fs.path.basename(out_diag_path)) catch |err| {
log.warn("failed to delete '{s}': {s}", .{ out_diag_path, @errorName(err) });
defer if (out_diag_path) |diag_file_path| zig_cache_tmp_dir.deleteFile(std.fs.path.basename(diag_file_path)) catch |err| {
log.warn("failed to delete '{s}': {s}", .{ diag_file_path, @errorName(err) });
};
defer if (out_dep_path) |dep_file_path| zig_cache_tmp_dir.deleteFile(std.fs.path.basename(dep_file_path)) catch |err| {
log.warn("failed to delete '{s}': {s}", .{ dep_file_path, @errorName(err) });
Expand Down Expand Up @@ -4647,14 +4648,15 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
};

switch (term) {
.Exited => |code| {
if (code != 0) {
const bundle = CObject.Diag.Bundle.parse(comp.gpa, out_diag_path) catch |err| {
log.err("{}: failed to parse clang diagnostics: {s}", .{ err, stderr });
return comp.failCObj(c_object, "clang exited with code {d}", .{code});
};
return comp.failCObjWithOwnedDiagBundle(c_object, bundle);
}
.Exited => |code| if (code != 0) if (out_diag_path) |diag_file_path| {
const bundle = CObject.Diag.Bundle.parse(comp.gpa, diag_file_path) catch |err| {
log.err("{}: failed to parse clang diagnostics: {s}", .{ err, stderr });
return comp.failCObj(c_object, "clang exited with code {d}", .{code});
};
return comp.failCObjWithOwnedDiagBundle(c_object, bundle);
} else {
log.err("clang failed with stderr: {s}", .{stderr});
return comp.failCObj(c_object, "clang exited with code {d}", .{code});
},
else => {
log.err("clang terminated with stderr: {s}", .{stderr});
Expand Down Expand Up @@ -5110,7 +5112,7 @@ pub fn addCCArgs(
// We don't ever put `-fcolor-diagnostics` or `-fno-color-diagnostics` because in passthrough mode
// we want Clang to infer it, and in normal mode we always want it off, which will be true since
// clang will detect stderr as a pipe rather than a terminal.
if (!comp.clang_passthrough_mode) {
if (!comp.clang_passthrough_mode and ext.clangSupportsDiagnostics()) {
// Make stderr more easily parseable.
try argv.append("-fno-caret-diagnostics");
}
Expand Down Expand Up @@ -5544,6 +5546,7 @@ fn failCObjWithOwnedDiagBundle(
diag_bundle: *CObject.Diag.Bundle,
) SemaError {
@setCold(true);
assert(diag_bundle.diags.len > 0);
{
comp.mutex.lock();
defer comp.mutex.unlock();
Expand Down Expand Up @@ -5619,6 +5622,25 @@ pub const FileExt = enum {
manifest,
unknown,

pub fn clangSupportsDiagnostics(ext: FileExt) bool {
return switch (ext) {
.c, .cpp, .h, .hpp, .hm, .hmm, .m, .mm, .cu, .ll, .bc => true,

.assembly,
.assembly_with_cpp,
.shared_library,
.object,
.static_library,
.zig,
.def,
.rc,
.res,
.manifest,
.unknown,
=> false,
};
}

pub fn clangSupportsDepFile(ext: FileExt) bool {
return switch (ext) {
.c, .cpp, .h, .hpp, .hm, .hmm, .m, .mm, .cu => true,
Expand Down

0 comments on commit c26e77e

Please sign in to comment.