aboutsummaryrefslogtreecommitdiff
path: root/lib/compiler/util.zig
blob: 05570abc3bfd9cfc1c9c3afaf602ee0b615bb78b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Utilities shared between compiler sub-commands
const std = @import("std");
const aro = @import("aro");
const ErrorBundle = std.zig.ErrorBundle;

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

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

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

    var cur_err: ?ErrorBundle.ErrorMessage = null;
    var cur_notes: std.ArrayList(ErrorBundle.ErrorMessage) = .empty;
    defer cur_notes.deinit(gpa);
    for (d.output.to_list.messages.items) |msg| {
        switch (msg.kind) {
            .off, .warning => {
                // Emit any pending error and clear everything so that notes don't bleed into unassociated errors
                if (cur_err) |err| {
                    try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
                    cur_err = null;
                }
                cur_notes.clearRetainingCapacity();
                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 bundle.addRootErrorMessageWithNotes(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 bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
    }

    return try bundle.toOwnedBundle("");
}