authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-17 12:18:51+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-08-17 18:50:10-04:00
logf0374fe3f04925a6e686077c2ffcb51b8eafc926
tree04156c9a729b5029a3682ff25fef088156addfdd
parent9e6318a4ea042e3fab7a1b2347600cde3d804e1a

Compilation: simplify `totalErrorCount`

This function now has to allocate anyway to resolve references, so we may as well just build the error bundle and check its length. Also remove some unnecessary calls of this function for efficiency.

1 files changed, 8 insertions(+), 91 deletions(-)

src/Compilation.zig+8-91
......@@ -2300,7 +2300,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void {
23002300 zcu.intern_pool.dumpGenericInstances(gpa);
23012301 }
23022302
2303 if (comp.config.is_test and try comp.totalErrorCount() == 0) {
2303 if (comp.config.is_test) {
23042304 // The `test_functions` decl has been intentionally postponed until now,
23052305 // at which point we must populate it with the list of test functions that
23062306 // have been discovered and not filtered out.
......@@ -2394,6 +2394,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void {
23942394 }
23952395
23962396 try flush(comp, arena, .main, main_progress_node);
2397
23972398 if (try comp.totalErrorCount() != 0) return;
23982399
23992400 // Failure here only means an unnecessary cache miss.
......@@ -2411,7 +2412,6 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void {
24112412 },
24122413 .incremental => {
24132414 try flush(comp, arena, .main, main_progress_node);
2414 if (try comp.totalErrorCount() != 0) return;
24152415 },
24162416 }
24172417}
......@@ -3047,93 +3047,6 @@ fn addBuf(list: *std.ArrayList(std.posix.iovec_const), buf: []const u8) void {
30473047 list.appendAssumeCapacity(.{ .base = buf.ptr, .len = buf.len });
30483048}
30493049
3050/// This function is temporally single-threaded.
3051pub fn totalErrorCount(comp: *Compilation) Allocator.Error!u32 {
3052 var total: usize =
3053 comp.misc_failures.count() +
3054 @intFromBool(comp.alloc_failure_occurred) +
3055 comp.lld_errors.items.len;
3056
3057 for (comp.failed_c_objects.values()) |bundle| {
3058 total += bundle.diags.len;
3059 }
3060
3061 for (comp.failed_win32_resources.values()) |errs| {
3062 total += errs.errorMessageCount();
3063 }
3064
3065 if (comp.module) |zcu| {
3066 const ip = &zcu.intern_pool;
3067
3068 var all_references: ?std.AutoHashMapUnmanaged(InternPool.AnalUnit, ?Zcu.ResolvedReference) = null;
3069 defer if (all_references) |*a| a.deinit(zcu.gpa);
3070
3071 total += zcu.failed_exports.count();
3072 total += zcu.failed_embed_files.count();
3073
3074 for (zcu.failed_files.keys(), zcu.failed_files.values()) |file, error_msg| {
3075 if (error_msg) |_| {
3076 total += 1;
3077 } else {
3078 assert(file.zir_loaded);
3079 const payload_index = file.zir.extra[@intFromEnum(Zir.ExtraIndex.compile_errors)];
3080 assert(payload_index != 0);
3081 const header = file.zir.extraData(Zir.Inst.CompileErrors, payload_index);
3082 total += header.data.items_len;
3083 }
3084 }
3085
3086 // Skip errors for Decls within files that failed parsing.
3087 // When a parse error is introduced, we keep all the semantic analysis for
3088 // the previous parse success, including compile errors, but we cannot
3089 // emit them until the file succeeds parsing.
3090 for (zcu.failed_analysis.keys()) |anal_unit| {
3091 if (comp.incremental) {
3092 if (all_references == null) {
3093 all_references = try zcu.resolveReferences();
3094 }
3095 if (!all_references.?.contains(anal_unit)) continue;
3096 }
3097 const file_index = switch (anal_unit.unwrap()) {
3098 .cau => |cau| zcu.namespacePtr(ip.getCau(cau).namespace).file_scope,
3099 .func => |ip_index| (zcu.funcInfo(ip_index).zir_body_inst.resolveFull(ip) orelse continue).file,
3100 };
3101 if (zcu.fileByIndex(file_index).okToReportErrors()) {
3102 total += 1;
3103 if (zcu.cimport_errors.get(anal_unit)) |errors| {
3104 total += errors.errorMessageCount();
3105 }
3106 }
3107 }
3108
3109 for (zcu.failed_codegen.keys()) |nav| {
3110 if (zcu.navFileScope(nav).okToReportErrors()) {
3111 total += 1;
3112 }
3113 }
3114
3115 if (zcu.intern_pool.global_error_set.getNamesFromMainThread().len > zcu.error_limit) {
3116 total += 1;
3117 }
3118 }
3119
3120 // The "no entry point found" error only counts if there are no semantic analysis errors.
3121 if (total == 0) {
3122 total += @intFromBool(comp.link_error_flags.no_entry_point_found);
3123 }
3124 total += @intFromBool(comp.link_error_flags.missing_libc);
3125 total += comp.link_errors.items.len;
3126
3127 // Compile log errors only count if there are no other errors.
3128 if (total == 0) {
3129 if (comp.module) |zcu| {
3130 total += @intFromBool(zcu.compile_log_sources.count() != 0);
3131 }
3132 }
3133
3134 return @intCast(total);
3135}
3136
31373050/// This function is temporally single-threaded.
31383051pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
31393052 const gpa = comp.gpa;
......@@ -3357,8 +3270,6 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33573270 }
33583271 }
33593272
3360 assert(try comp.totalErrorCount() == bundle.root_list.items.len);
3361
33623273 if (comp.module) |zcu| {
33633274 if (comp.incremental and bundle.root_list.items.len == 0) {
33643275 const should_have_error = for (zcu.transitive_failed_analysis.keys()) |failed_unit| {
......@@ -3377,6 +3288,12 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33773288 return bundle.toOwnedBundle(compile_log_text);
33783289}
33793290
3291fn totalErrorCount(comp: *Compilation) !u32 {
3292 var errors = try comp.getAllErrorsAlloc();
3293 defer errors.deinit(comp.gpa);
3294 return errors.errorMessageCount();
3295}
3296
33803297pub const ErrorNoteHashContext = struct {
33813298 eb: *const ErrorBundle.Wip,
33823299