Skip to content

Commit

Permalink
stage2: make @compilelog work with 2 in the same decl
Browse files Browse the repository at this point in the history
  • Loading branch information
g-w1 committed Nov 27, 2020
1 parent 844c53f commit 3a7c08e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,9 @@ pub fn totalErrorCount(self: *Compilation) usize {
total += module.failed_decls.items().len +
module.failed_exports.items().len +
module.failed_files.items().len;
for (module.compile_log_decls.items()) |entry| {
total += entry.value.items.len;
}
}

// The "no entry point found" error only counts if there are no other errors.
Expand Down Expand Up @@ -1200,6 +1203,15 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
const source = try decl.scope.getSource(module);
try AllErrors.add(&arena, &errors, decl.scope.subFilePath(), source, err_msg.*);
}
for (module.compile_log_decls.items()) |entry| {
const decl = entry.key;
const path = decl.scope.subFilePath();
const source = try decl.scope.getSource(module);
for (entry.value.items) |src_loc| {
const err_msg = ErrorMsg{ .byte_offset = src_loc, .msg = "found compile log statement" };
try AllErrors.add(&arena, &errors, path, source, err_msg);
}
}
}

if (errors.items.len == 0 and self.link_error_flags.no_entry_point_found) {
Expand Down
58 changes: 58 additions & 0 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ decl_table: std.ArrayHashMapUnmanaged(Scope.NameHash, *Decl, Scope.name_hash_has
/// Note that a Decl can succeed but the Fn it represents can fail. In this case,
/// a Decl can have a failed_decls entry but have analysis status of success.
failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *Compilation.ErrorMsg) = .{},
/// A Decl can have multiple compileLogs, but only one error, so we map a Decl to a the src locs of all the compileLogs
compile_log_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ArrayListUnmanaged(usize)) = .{},
/// Using a map here for consistency with the other fields here.
/// The ErrorMsg memory is owned by the `Scope`, using Module's general purpose allocator.
failed_files: std.AutoArrayHashMapUnmanaged(*Scope, *Compilation.ErrorMsg) = .{},
Expand Down Expand Up @@ -857,6 +859,12 @@ pub fn deinit(self: *Module) void {
}
self.failed_exports.deinit(gpa);

for (self.compile_log_decls.items()) |entry| {
entry.value.deinit(gpa);
gpa.destroy(entry.value);
}
self.compile_log_decls.deinit(gpa);

for (self.decl_exports.items()) |entry| {
const export_list = entry.value;
gpa.free(export_list);
Expand Down Expand Up @@ -2941,6 +2949,56 @@ pub fn failNode(
return self.fail(scope, src, format, args);
}

fn addCompileLog(self: *Module, decl: *Decl, src: usize) error{OutOfMemory}!void {
var res = self.compile_log_decls.get(decl);
if (res) |value| {
try value.append(self.gpa, src);
} else {
// TODO does it have to be this complicated. why doesn't ArrayListUnmanaged have initCapacityAlloc that returns a heap allocated ArrayList?
var new_list = try self.gpa.create(ArrayListUnmanaged(usize));
const new_memory = try self.gpa.alloc(usize, 1);
new_list.items.ptr = new_memory.ptr;
new_list.items.len = 0;
new_list.capacity = new_memory.len;
new_list.appendAssumeCapacity(src);
try self.compile_log_decls.put(self.gpa, decl, new_list);
}
}
pub fn failCompileLog(
self: *Module,
scope: *Scope,
src: usize,
) InnerError!void { // we do void because we dont actually want to fail because analysis should continue
switch (scope.tag) {
.decl => {
const decl = scope.cast(Scope.DeclAnalysis).?.decl;
try self.addCompileLog(decl, src);
},
.block => {
const block = scope.cast(Scope.Block).?;
try self.addCompileLog(block.decl, src);
},
.gen_zir => {
const gen_zir = scope.cast(Scope.GenZIR).?;
try self.addCompileLog(gen_zir.decl, src);
},
.local_val => {
const gen_zir = scope.cast(Scope.LocalVal).?.gen_zir;
try self.addCompileLog(gen_zir.decl, src);
},
.local_ptr => {
const gen_zir = scope.cast(Scope.LocalPtr).?.gen_zir;
try self.addCompileLog(gen_zir.decl, src);
},
.zir_module => {
// TODO is this actually unreachable?
unreachable;
// const zir_module = scope.cast(Scope.ZIRModule).?;
},
.file => unreachable,
.container => unreachable,
}
}
fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *Compilation.ErrorMsg) InnerError {
{
errdefer err_msg.destroy(self.gpa);
Expand Down
7 changes: 5 additions & 2 deletions src/zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,13 @@ pub const Inst = struct {
.slice_start,
.import,
.switch_range,
.compilelog,
=> false,

.@"break",
.breakvoid,
.condbr,
.compileerror,
.compilelog,
.@"return",
.returnvoid,
.unreach_nocheck,
Expand Down Expand Up @@ -714,7 +714,10 @@ pub const Inst = struct {
positionals: struct {
to_log: []*Inst,
},
kw_args: struct {},
kw_args: struct {
/// If we have seen it already so don't make another error
seen: bool = false,
},
};

pub const Const = struct {
Expand Down
6 changes: 3 additions & 3 deletions src/zir_sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ fn analyzeInstCompileLog(mod: *Module, scope: *Scope, inst: *zir.Inst.CompileLog
if (i != inst.positionals.to_log.len - 1) std.debug.print(", ", .{});
}
std.debug.print("\n", .{});
switch (mod.fail(scope, inst.base.src, "found compile log statement", .{})) {
error.AnalysisFail => {},
else => |e| return e,
if (!inst.kw_args.seen) {
inst.kw_args.seen = true; // so that we do not give multiple compile errors if it gets evaled twice
try mod.failCompileLog(scope, inst.base.src);
}
return mod.constVoid(scope, inst.base.src);
}
Expand Down

0 comments on commit 3a7c08e

Please sign in to comment.