Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 10 additions & 85 deletions lib/compiler/resinator/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const cvtres = @import("cvtres.zig");
const hasDisjointCodePage = @import("disjoint_code_page.zig").hasDisjointCodePage;
const fmtResourceType = @import("res.zig").NameOrOrdinal.fmtResourceType;
const aro = @import("aro");
const compiler_util = @import("../util.zig");

pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
Expand Down Expand Up @@ -671,7 +672,11 @@ const ErrorHandler = union(enum) {
) !void {
switch (self.*) {
.server => |*server| {
var error_bundle = try aroDiagnosticsToErrorBundle(allocator, fail_msg, comp);
var error_bundle = try compiler_util.aroDiagnosticsToErrorBundle(
comp.diagnostics,
allocator,
fail_msg,
);
defer error_bundle.deinit(allocator);

try server.serveErrorBundle(error_bundle);
Expand Down Expand Up @@ -753,7 +758,7 @@ fn cliDiagnosticsToErrorBundle(
switch (err_details.type) {
.err => {
if (cur_err) |err| {
try flushErrorMessageIntoBundle(&bundle, err, cur_notes.items);
try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
}
cur_err = .{
.msg = try bundle.addString(err_details.msg.items),
Expand All @@ -771,7 +776,7 @@ fn cliDiagnosticsToErrorBundle(
}
}
if (cur_err) |err| {
try flushErrorMessageIntoBundle(&bundle, err, cur_notes.items);
try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
}

return try bundle.toOwnedBundle("");
Expand Down Expand Up @@ -840,7 +845,7 @@ fn diagnosticsToErrorBundle(
switch (err_details.type) {
.err => {
if (cur_err) |err| {
try flushErrorMessageIntoBundle(&bundle, err, cur_notes.items);
try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
}
cur_err = .{
.msg = try bundle.addString(msg_buf.written()),
Expand All @@ -859,20 +864,12 @@ fn diagnosticsToErrorBundle(
}
}
if (cur_err) |err| {
try flushErrorMessageIntoBundle(&bundle, err, cur_notes.items);
try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
}

return try bundle.toOwnedBundle("");
}

fn flushErrorMessageIntoBundle(wip: *ErrorBundle.Wip, msg: ErrorBundle.ErrorMessage, notes: []const ErrorBundle.ErrorMessage) !void {
try wip.addRootErrorMessage(msg);
const notes_start = try wip.reserveNotes(@intCast(notes.len));
for (notes_start.., notes) |i, note| {
wip.extra.items[i] = @intFromEnum(wip.addErrorMessageAssumeCapacity(note));
}
}

fn errorStringToErrorBundle(allocator: std.mem.Allocator, comptime format: []const u8, args: anytype) !ErrorBundle {
@branchHint(.cold);
var bundle: ErrorBundle.Wip = undefined;
Expand All @@ -883,75 +880,3 @@ fn errorStringToErrorBundle(allocator: std.mem.Allocator, comptime format: []con
});
return try bundle.toOwnedBundle("");
}

fn aroDiagnosticsToErrorBundle(
gpa: std.mem.Allocator,
fail_msg: []const u8,
comp: *aro.Compilation,
) !ErrorBundle {
@branchHint(.cold);

var bundle: ErrorBundle.Wip = undefined;
try bundle.init(gpa);
errdefer bundle.deinit();

try bundle.addRootErrorMessage(.{
.msg = try bundle.addString(fail_msg),
});

var cur_err: ?ErrorBundle.ErrorMessage = null;
var cur_notes: std.ArrayList(ErrorBundle.ErrorMessage) = .empty;
defer cur_notes.deinit(gpa);
for (comp.diagnostics.output.to_list.messages.items) |msg| {
switch (msg.kind) {
// Clear the current error so that notes don't bleed into unassociated errors
.off, .warning => {
cur_err = null;
continue;
},
.note => if (cur_err == null) continue,
.@"fatal error", .@"error" => {},
}

const src_loc = src_loc: {
if (msg.location) |location| {
break :src_loc try bundle.addSourceLocation(.{
.src_path = try bundle.addString(location.path),
.line = location.line_no - 1, // 1-based -> 0-based
.column = location.col - 1, // 1-based -> 0-based
.span_start = location.width,
.span_main = location.width,
.span_end = location.width,
.source_line = try bundle.addString(location.line),
});
}
break :src_loc ErrorBundle.SourceLocationIndex.none;
};

switch (msg.kind) {
.@"fatal error", .@"error" => {
if (cur_err) |err| {
try flushErrorMessageIntoBundle(&bundle, err, cur_notes.items);
}
cur_err = .{
.msg = try bundle.addString(msg.text),
.src_loc = src_loc,
};
cur_notes.clearRetainingCapacity();
},
.note => {
cur_err.?.notes_len += 1;
try cur_notes.append(gpa, .{
.msg = try bundle.addString(msg.text),
.src_loc = src_loc,
});
},
.off, .warning => unreachable,
}
}
if (cur_err) |err| {
try flushErrorMessageIntoBundle(&bundle, err, cur_notes.items);
}

return try bundle.toOwnedBundle("");
}
15 changes: 1 addition & 14 deletions lib/compiler/std-docs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,7 @@ fn buildWasmBinary(
}
},
.error_bundle => {
const EbHdr = std.zig.Server.Message.ErrorBundle;
const eb_hdr = @as(*align(1) const EbHdr, @ptrCast(body));
const extra_bytes =
body[@sizeOf(EbHdr)..][0 .. @sizeOf(u32) * eb_hdr.extra_len];
const string_bytes =
body[@sizeOf(EbHdr) + extra_bytes.len ..][0..eb_hdr.string_bytes_len];
// TODO: use @ptrCast when the compiler supports it
const unaligned_extra = std.mem.bytesAsSlice(u32, extra_bytes);
const extra_array = try arena.alloc(u32, unaligned_extra.len);
@memcpy(extra_array, unaligned_extra);
result_error_bundle = .{
.string_bytes = try arena.dupe(u8, string_bytes),
.extra = extra_array,
};
result_error_bundle = try std.zig.Server.allocErrorBundle(arena, body);
},
.emit_digest => {
const EmitDigest = std.zig.Server.Message.EmitDigest;
Expand Down
67 changes: 57 additions & 10 deletions lib/compiler/translate-c/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const assert = std.debug.assert;
const mem = std.mem;
const process = std.process;
const aro = @import("aro");
const compiler_util = @import("../util.zig");
const Translator = @import("Translator.zig");

const fast_exit = @import("builtin").mode != .Debug;
Expand All @@ -13,24 +14,33 @@ pub fn main() u8 {
const gpa = general_purpose_allocator.allocator();
defer _ = general_purpose_allocator.deinit();

var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();
const arena = arena_instance.allocator();

const args = process.argsAlloc(arena) catch {
var args = process.argsAlloc(arena) catch {
std.debug.print("ran out of memory allocating arguments\n", .{});
if (fast_exit) process.exit(1);
return 1;
};

var zig_integration = false;
if (args.len > 1 and std.mem.eql(u8, args[1], "--zig-integration")) {
zig_integration = true;
}

var stderr_buf: [1024]u8 = undefined;
var stderr = std.fs.File.stderr().writer(&stderr_buf);
var diagnostics: aro.Diagnostics = .{
.output = .{ .to_writer = .{
var diagnostics: aro.Diagnostics = switch (zig_integration) {
false => .{ .output = .{ .to_writer = .{
.color = .detect(stderr.file),
.writer = &stderr.interface,
} },
} } },
true => .{ .output = .{ .to_list = .{
.arena = .init(gpa),
} } },
};
defer diagnostics.deinit();

var comp = aro.Compilation.initDefault(gpa, arena, &diagnostics, std.fs.cwd()) catch |err| switch (err) {
error.OutOfMemory => {
Expand All @@ -47,13 +57,22 @@ pub fn main() u8 {
var toolchain: aro.Toolchain = .{ .driver = &driver, .filesystem = .{ .real = comp.cwd } };
defer toolchain.deinit();

translate(&driver, &toolchain, args) catch |err| switch (err) {
translate(&driver, &toolchain, args, zig_integration) catch |err| switch (err) {
error.OutOfMemory => {
std.debug.print("ran out of memory translating\n", .{});
if (fast_exit) process.exit(1);
return 1;
},
error.FatalError => {
error.FatalError => if (zig_integration) {
serveErrorBundle(arena, &diagnostics) catch |bundle_err| {
std.debug.print("unable to serve error bundle: {}\n", .{bundle_err});
if (fast_exit) process.exit(1);
return 1;
};

if (fast_exit) process.exit(0);
return 0;
} else {
if (fast_exit) process.exit(1);
return 1;
},
Expand All @@ -63,10 +82,27 @@ pub fn main() u8 {
return 1;
},
};

assert(comp.diagnostics.errors == 0 or !zig_integration);
if (fast_exit) process.exit(@intFromBool(comp.diagnostics.errors != 0));
return @intFromBool(comp.diagnostics.errors != 0);
}

fn serveErrorBundle(arena: std.mem.Allocator, diagnostics: *const aro.Diagnostics) !void {
const error_bundle = try compiler_util.aroDiagnosticsToErrorBundle(
diagnostics,
arena,
"translation failure",
);
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
var server: std.zig.Server = .{
.out = &stdout_writer.interface,
.in = undefined,
};
try server.serveErrorBundle(error_bundle);
}

pub const usage =
\\Usage {s}: [options] file [CC options]
\\
Expand All @@ -79,7 +115,7 @@ pub const usage =
\\
;

fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8, zig_integration: bool) !void {
const gpa = d.comp.gpa;

const aro_args = args: {
Expand All @@ -99,6 +135,9 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
try stdout.interface.writeAll("0.0.0-dev\n");
try stdout.interface.flush();
return;
} else if (mem.eql(u8, arg, "--zig-integration")) {
if (i != 1 or !zig_integration)
return d.fatal("--zig-integration must be the first argument", .{});
} else {
i += 1;
}
Expand All @@ -116,6 +155,14 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
return d.fatal("user provided macro source exceeded max size", .{});
}

const has_output_file = if (d.output_name) |path|
!std.mem.eql(u8, path, "-")
else
false;
if (zig_integration and !has_output_file) {
return d.fatal("--zig-integration requires specifying an output file", .{});
}

const content = try macro_buf.toOwnedSlice(gpa);
errdefer gpa.free(content);

Expand Down Expand Up @@ -160,7 +207,7 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
defer c_tree.deinit();

if (d.diagnostics.errors != 0) {
if (fast_exit) process.exit(1);
if (fast_exit and !zig_integration) process.exit(1);
return error.FatalError;
}

Expand Down Expand Up @@ -212,7 +259,7 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
if (out_writer.err) |write_err|
return d.fatal("failed to write result to '{s}': {s}", .{ out_file_path, aro.Driver.errorDescription(write_err) });

if (fast_exit) process.exit(0);
if (fast_exit and !zig_integration) process.exit(0);
}

test {
Expand Down
Loading
Loading