diff --git a/lib/build_runner.zig b/lib/build_runner.zig deleted file mode 100644 index 071b56a71cc053e121fd31108c333adb8d6364cb..0000000000000000000000000000000000000000 --- a/lib/build_runner.zig +++ /dev/null @@ -1,1273 +0,0 @@ -const root = @import("@build"); -const std = @import("std"); -const builtin = @import("builtin"); -const assert = std.debug.assert; -const io = std.io; -const fmt = std.fmt; -const mem = std.mem; -const process = std.process; -const ArrayList = std.ArrayList; -const File = std.fs.File; -const Step = std.Build.Step; - -pub const dependencies = @import("@dependencies"); - -pub fn main() !void { - // Here we use an ArenaAllocator backed by a page allocator because a build is a short-lived, - // one shot program. We don't need to waste time freeing memory and finding places to squish - // bytes into. So we free everything all at once at the very end. - var single_threaded_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); - defer single_threaded_arena.deinit(); - - var thread_safe_arena: std.heap.ThreadSafeAllocator = .{ - .child_allocator = single_threaded_arena.allocator(), - }; - const arena = thread_safe_arena.allocator(); - - const args = try process.argsAlloc(arena); - - // skip my own exe name - var arg_idx: usize = 1; - - const zig_exe = nextArg(args, &arg_idx) orelse { - std.debug.print("Expected path to zig compiler\n", .{}); - return error.InvalidArgs; - }; - const build_root = nextArg(args, &arg_idx) orelse { - std.debug.print("Expected build root directory path\n", .{}); - return error.InvalidArgs; - }; - const cache_root = nextArg(args, &arg_idx) orelse { - std.debug.print("Expected cache root directory path\n", .{}); - return error.InvalidArgs; - }; - const global_cache_root = nextArg(args, &arg_idx) orelse { - std.debug.print("Expected global cache root directory path\n", .{}); - return error.InvalidArgs; - }; - - const build_root_directory: std.Build.Cache.Directory = .{ - .path = build_root, - .handle = try std.fs.cwd().openDir(build_root, .{}), - }; - - const local_cache_directory: std.Build.Cache.Directory = .{ - .path = cache_root, - .handle = try std.fs.cwd().makeOpenPath(cache_root, .{}), - }; - - const global_cache_directory: std.Build.Cache.Directory = .{ - .path = global_cache_root, - .handle = try std.fs.cwd().makeOpenPath(global_cache_root, .{}), - }; - - var graph: std.Build.Graph = .{ - .arena = arena, - .cache = .{ - .gpa = arena, - .manifest_dir = try local_cache_directory.handle.makeOpenPath("h", .{}), - }, - .zig_exe = zig_exe, - .env_map = try process.getEnvMap(arena), - .global_cache_root = global_cache_directory, - }; - - graph.cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() }); - graph.cache.addPrefix(build_root_directory); - graph.cache.addPrefix(local_cache_directory); - graph.cache.addPrefix(global_cache_directory); - graph.cache.hash.addBytes(builtin.zig_version_string); - - const builder = try std.Build.create( - &graph, - build_root_directory, - local_cache_directory, - dependencies.root_deps, - ); - - var targets = ArrayList([]const u8).init(arena); - var debug_log_scopes = ArrayList([]const u8).init(arena); - var thread_pool_options: std.Thread.Pool.Options = .{ .allocator = arena }; - - var install_prefix: ?[]const u8 = null; - var dir_list = std.Build.DirList{}; - var summary: ?Summary = null; - var max_rss: u64 = 0; - var skip_oom_steps: bool = false; - var color: Color = .auto; - var seed: u32 = 0; - var prominent_compile_errors: bool = false; - var help_menu: bool = false; - var steps_menu: bool = false; - var output_tmp_nonce: ?[16]u8 = null; - - while (nextArg(args, &arg_idx)) |arg| { - if (mem.startsWith(u8, arg, "-Z")) { - if (arg.len != 18) fatalWithHint("bad argument: '{s}'", .{arg}); - output_tmp_nonce = arg[2..18].*; - } else if (mem.startsWith(u8, arg, "-D")) { - const option_contents = arg[2..]; - if (option_contents.len == 0) - fatalWithHint("expected option name after '-D'", .{}); - if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| { - const option_name = option_contents[0..name_end]; - const option_value = option_contents[name_end + 1 ..]; - if (try builder.addUserInputOption(option_name, option_value)) - fatal(" access the help menu with 'zig build -h'", .{}); - } else { - if (try builder.addUserInputFlag(option_contents)) - fatal(" access the help menu with 'zig build -h'", .{}); - } - } else if (mem.startsWith(u8, arg, "-")) { - if (mem.eql(u8, arg, "--verbose")) { - builder.verbose = true; - } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { - help_menu = true; - } else if (mem.eql(u8, arg, "-p") or mem.eql(u8, arg, "--prefix")) { - install_prefix = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "-l") or mem.eql(u8, arg, "--list-steps")) { - steps_menu = true; - } else if (mem.startsWith(u8, arg, "-fsys=")) { - const name = arg["-fsys=".len..]; - graph.system_library_options.put(arena, name, .user_enabled) catch @panic("OOM"); - } else if (mem.startsWith(u8, arg, "-fno-sys=")) { - const name = arg["-fno-sys=".len..]; - graph.system_library_options.put(arena, name, .user_disabled) catch @panic("OOM"); - } else if (mem.eql(u8, arg, "--release")) { - builder.release_mode = .any; - } else if (mem.startsWith(u8, arg, "--release=")) { - const text = arg["--release=".len..]; - builder.release_mode = std.meta.stringToEnum(std.Build.ReleaseMode, text) orelse { - fatalWithHint("expected [off|any|fast|safe|small] in '{s}', found '{s}'", .{ - arg, text, - }); - }; - } else if (mem.eql(u8, arg, "--host-target")) { - graph.host_query_options.arch_os_abi = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--host-cpu")) { - graph.host_query_options.cpu_features = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--host-dynamic-linker")) { - graph.host_query_options.dynamic_linker = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--prefix-lib-dir")) { - dir_list.lib_dir = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--prefix-exe-dir")) { - dir_list.exe_dir = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--prefix-include-dir")) { - dir_list.include_dir = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--sysroot")) { - builder.sysroot = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--maxrss")) { - const max_rss_text = nextArgOrFatal(args, &arg_idx); - max_rss = std.fmt.parseIntSizeSuffix(max_rss_text, 10) catch |err| { - std.debug.print("invalid byte size: '{s}': {s}\n", .{ - max_rss_text, @errorName(err), - }); - process.exit(1); - }; - } else if (mem.eql(u8, arg, "--skip-oom-steps")) { - skip_oom_steps = true; - } else if (mem.eql(u8, arg, "--search-prefix")) { - const search_prefix = nextArgOrFatal(args, &arg_idx); - builder.addSearchPrefix(search_prefix); - } else if (mem.eql(u8, arg, "--libc")) { - builder.libc_file = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--color")) { - const next_arg = nextArg(args, &arg_idx) orelse - fatalWithHint("expected [auto|on|off] after '{s}'", .{arg}); - color = std.meta.stringToEnum(Color, next_arg) orelse { - fatalWithHint("expected [auto|on|off] after '{s}', found '{s}'", .{ - arg, next_arg, - }); - }; - } else if (mem.eql(u8, arg, "--summary")) { - const next_arg = nextArg(args, &arg_idx) orelse - fatalWithHint("expected [all|failures|none] after '{s}'", .{arg}); - summary = std.meta.stringToEnum(Summary, next_arg) orelse { - fatalWithHint("expected [all|failures|none] after '{s}', found '{s}'", .{ - arg, next_arg, - }); - }; - } else if (mem.eql(u8, arg, "--zig-lib-dir")) { - builder.zig_lib_dir = .{ .cwd_relative = nextArgOrFatal(args, &arg_idx) }; - } else if (mem.eql(u8, arg, "--seed")) { - const next_arg = nextArg(args, &arg_idx) orelse - fatalWithHint("expected u32 after '{s}'", .{arg}); - seed = std.fmt.parseUnsigned(u32, next_arg, 0) catch |err| { - fatal("unable to parse seed '{s}' as 32-bit integer: {s}\n", .{ - next_arg, @errorName(err), - }); - }; - } else if (mem.eql(u8, arg, "--debug-log")) { - const next_arg = nextArgOrFatal(args, &arg_idx); - try debug_log_scopes.append(next_arg); - } else if (mem.eql(u8, arg, "--debug-pkg-config")) { - builder.debug_pkg_config = true; - } else if (mem.eql(u8, arg, "--debug-compile-errors")) { - builder.debug_compile_errors = true; - } else if (mem.eql(u8, arg, "--system")) { - // The usage text shows another argument after this parameter - // but it is handled by the parent process. The build runner - // only sees this flag. - graph.system_package_mode = true; - } else if (mem.eql(u8, arg, "--glibc-runtimes")) { - builder.glibc_runtimes_dir = nextArgOrFatal(args, &arg_idx); - } else if (mem.eql(u8, arg, "--verbose-link")) { - builder.verbose_link = true; - } else if (mem.eql(u8, arg, "--verbose-air")) { - builder.verbose_air = true; - } else if (mem.eql(u8, arg, "--verbose-llvm-ir")) { - builder.verbose_llvm_ir = "-"; - } else if (mem.startsWith(u8, arg, "--verbose-llvm-ir=")) { - builder.verbose_llvm_ir = arg["--verbose-llvm-ir=".len..]; - } else if (mem.eql(u8, arg, "--verbose-llvm-bc=")) { - builder.verbose_llvm_bc = arg["--verbose-llvm-bc=".len..]; - } else if (mem.eql(u8, arg, "--verbose-cimport")) { - builder.verbose_cimport = true; - } else if (mem.eql(u8, arg, "--verbose-cc")) { - builder.verbose_cc = true; - } else if (mem.eql(u8, arg, "--verbose-llvm-cpu-features")) { - builder.verbose_llvm_cpu_features = true; - } else if (mem.eql(u8, arg, "--prominent-compile-errors")) { - prominent_compile_errors = true; - } else if (mem.eql(u8, arg, "-fwine")) { - builder.enable_wine = true; - } else if (mem.eql(u8, arg, "-fno-wine")) { - builder.enable_wine = false; - } else if (mem.eql(u8, arg, "-fqemu")) { - builder.enable_qemu = true; - } else if (mem.eql(u8, arg, "-fno-qemu")) { - builder.enable_qemu = false; - } else if (mem.eql(u8, arg, "-fwasmtime")) { - builder.enable_wasmtime = true; - } else if (mem.eql(u8, arg, "-fno-wasmtime")) { - builder.enable_wasmtime = false; - } else if (mem.eql(u8, arg, "-frosetta")) { - builder.enable_rosetta = true; - } else if (mem.eql(u8, arg, "-fno-rosetta")) { - builder.enable_rosetta = false; - } else if (mem.eql(u8, arg, "-fdarling")) { - builder.enable_darling = true; - } else if (mem.eql(u8, arg, "-fno-darling")) { - builder.enable_darling = false; - } else if (mem.eql(u8, arg, "-freference-trace")) { - builder.reference_trace = 256; - } else if (mem.startsWith(u8, arg, "-freference-trace=")) { - const num = arg["-freference-trace=".len..]; - builder.reference_trace = std.fmt.parseUnsigned(u32, num, 10) catch |err| { - std.debug.print("unable to parse reference_trace count '{s}': {s}", .{ num, @errorName(err) }); - process.exit(1); - }; - } else if (mem.eql(u8, arg, "-fno-reference-trace")) { - builder.reference_trace = null; - } else if (mem.startsWith(u8, arg, "-j")) { - const num = arg["-j".len..]; - const n_jobs = std.fmt.parseUnsigned(u32, num, 10) catch |err| { - std.debug.print("unable to parse jobs count '{s}': {s}", .{ - num, @errorName(err), - }); - process.exit(1); - }; - if (n_jobs < 1) { - std.debug.print("number of jobs must be at least 1\n", .{}); - process.exit(1); - } - thread_pool_options.n_jobs = n_jobs; - } else if (mem.eql(u8, arg, "--")) { - builder.args = argsRest(args, arg_idx); - break; - } else { - fatalWithHint("unrecognized argument: '{s}'", .{arg}); - } - } else { - try targets.append(arg); - } - } - - const host_query = std.Build.parseTargetQuery(graph.host_query_options) catch |err| switch (err) { - error.ParseFailed => process.exit(1), - }; - builder.host = .{ - .query = .{}, - .result = try std.zig.system.resolveTargetQuery(host_query), - }; - - const stderr = std.io.getStdErr(); - const ttyconf = get_tty_conf(color, stderr); - switch (ttyconf) { - .no_color => try graph.env_map.put("NO_COLOR", "1"), - .escape_codes => try graph.env_map.put("YES_COLOR", "1"), - .windows_api => {}, - } - - var progress: std.Progress = .{ .dont_print_on_dumb = true }; - const main_progress_node = progress.start("", 0); - - builder.debug_log_scopes = debug_log_scopes.items; - builder.resolveInstallPrefix(install_prefix, dir_list); - { - var prog_node = main_progress_node.start("user build.zig logic", 0); - defer prog_node.end(); - try builder.runBuild(root); - } - - if (graph.needed_lazy_dependencies.entries.len != 0) { - var buffer: std.ArrayListUnmanaged(u8) = .{}; - for (graph.needed_lazy_dependencies.keys()) |k| { - try buffer.appendSlice(arena, k); - try buffer.append(arena, '\n'); - } - const s = std.fs.path.sep_str; - const tmp_sub_path = "tmp" ++ s ++ (output_tmp_nonce orelse fatal("missing -Z arg", .{})); - local_cache_directory.handle.writeFile2(.{ - .sub_path = tmp_sub_path, - .data = buffer.items, - .flags = .{ .exclusive = true }, - }) catch |err| { - fatal("unable to write configuration results to '{}{s}': {s}", .{ - local_cache_directory, tmp_sub_path, @errorName(err), - }); - }; - process.exit(3); // Indicate configure phase failed with meaningful stdout. - } - - if (builder.validateUserInputDidItFail()) { - fatal(" access the help menu with 'zig build -h'", .{}); - } - - validateSystemLibraryOptions(builder); - - const stdout_writer = io.getStdOut().writer(); - - if (help_menu) - return usage(builder, stdout_writer); - - if (steps_menu) - return steps(builder, stdout_writer); - - var run: Run = .{ - .max_rss = max_rss, - .max_rss_is_default = false, - .max_rss_mutex = .{}, - .skip_oom_steps = skip_oom_steps, - .memory_blocked_steps = std.ArrayList(*Step).init(arena), - .prominent_compile_errors = prominent_compile_errors, - - .claimed_rss = 0, - .summary = summary, - .ttyconf = ttyconf, - .stderr = stderr, - }; - - if (run.max_rss == 0) { - run.max_rss = process.totalSystemMemory() catch std.math.maxInt(u64); - run.max_rss_is_default = true; - } - - runStepNames( - arena, - builder, - targets.items, - main_progress_node, - thread_pool_options, - &run, - seed, - ) catch |err| switch (err) { - error.UncleanExit => process.exit(1), - else => return err, - }; -} - -const Run = struct { - max_rss: u64, - max_rss_is_default: bool, - max_rss_mutex: std.Thread.Mutex, - skip_oom_steps: bool, - memory_blocked_steps: std.ArrayList(*Step), - prominent_compile_errors: bool, - - claimed_rss: usize, - summary: ?Summary, - ttyconf: std.io.tty.Config, - stderr: File, -}; - -fn runStepNames( - arena: std.mem.Allocator, - b: *std.Build, - step_names: []const []const u8, - parent_prog_node: *std.Progress.Node, - thread_pool_options: std.Thread.Pool.Options, - run: *Run, - seed: u32, -) !void { - const gpa = b.allocator; - var step_stack: std.AutoArrayHashMapUnmanaged(*Step, void) = .{}; - defer step_stack.deinit(gpa); - - if (step_names.len == 0) { - try step_stack.put(gpa, b.default_step, {}); - } else { - try step_stack.ensureUnusedCapacity(gpa, step_names.len); - for (0..step_names.len) |i| { - const step_name = step_names[step_names.len - i - 1]; - const s = b.top_level_steps.get(step_name) orelse { - std.debug.print("no step named '{s}'\n access the help menu with 'zig build -h'\n", .{step_name}); - process.exit(1); - }; - step_stack.putAssumeCapacity(&s.step, {}); - } - } - - const starting_steps = try arena.dupe(*Step, step_stack.keys()); - - var rng = std.Random.DefaultPrng.init(seed); - const rand = rng.random(); - rand.shuffle(*Step, starting_steps); - - for (starting_steps) |s| { - constructGraphAndCheckForDependencyLoop(b, s, &step_stack, rand) catch |err| switch (err) { - error.DependencyLoopDetected => return error.UncleanExit, - else => |e| return e, - }; - } - - { - // Check that we have enough memory to complete the build. - var any_problems = false; - for (step_stack.keys()) |s| { - if (s.max_rss == 0) continue; - if (s.max_rss > run.max_rss) { - if (run.skip_oom_steps) { - s.state = .skipped_oom; - } else { - std.debug.print("{s}{s}: this step declares an upper bound of {d} bytes of memory, exceeding the available {d} bytes of memory\n", .{ - s.owner.dep_prefix, s.name, s.max_rss, run.max_rss, - }); - any_problems = true; - } - } - } - if (any_problems) { - if (run.max_rss_is_default) { - std.debug.print("note: use --maxrss to override the default", .{}); - } - return error.UncleanExit; - } - } - - var thread_pool: std.Thread.Pool = undefined; - try thread_pool.init(thread_pool_options); - defer thread_pool.deinit(); - - { - defer parent_prog_node.end(); - - var step_prog = parent_prog_node.start("steps", step_stack.count()); - defer step_prog.end(); - - var wait_group: std.Thread.WaitGroup = .{}; - defer wait_group.wait(); - - // Here we spawn the initial set of tasks with a nice heuristic - - // dependency order. Each worker when it finishes a step will then - // check whether it should run any dependants. - const steps_slice = step_stack.keys(); - for (0..steps_slice.len) |i| { - const step = steps_slice[steps_slice.len - i - 1]; - if (step.state == .skipped_oom) continue; - - wait_group.start(); - thread_pool.spawn(workerMakeOneStep, .{ - &wait_group, &thread_pool, b, step, &step_prog, run, - }) catch @panic("OOM"); - } - } - assert(run.memory_blocked_steps.items.len == 0); - - var test_skip_count: usize = 0; - var test_fail_count: usize = 0; - var test_pass_count: usize = 0; - var test_leak_count: usize = 0; - var test_count: usize = 0; - - var success_count: usize = 0; - var skipped_count: usize = 0; - var failure_count: usize = 0; - var pending_count: usize = 0; - var total_compile_errors: usize = 0; - var compile_error_steps: std.ArrayListUnmanaged(*Step) = .{}; - defer compile_error_steps.deinit(gpa); - - for (step_stack.keys()) |s| { - test_fail_count += s.test_results.fail_count; - test_skip_count += s.test_results.skip_count; - test_leak_count += s.test_results.leak_count; - test_pass_count += s.test_results.passCount(); - test_count += s.test_results.test_count; - - switch (s.state) { - .precheck_unstarted => unreachable, - .precheck_started => unreachable, - .running => unreachable, - .precheck_done => { - // precheck_done is equivalent to dependency_failure in the case of - // transitive dependencies. For example: - // A -> B -> C (failure) - // B will be marked as dependency_failure, while A may never be queued, and thus - // remain in the initial state of precheck_done. - s.state = .dependency_failure; - pending_count += 1; - }, - .dependency_failure => pending_count += 1, - .success => success_count += 1, - .skipped, .skipped_oom => skipped_count += 1, - .failure => { - failure_count += 1; - const compile_errors_len = s.result_error_bundle.errorMessageCount(); - if (compile_errors_len > 0) { - total_compile_errors += compile_errors_len; - try compile_error_steps.append(gpa, s); - } - }, - } - } - - // A proper command line application defaults to silently succeeding. - // The user may request verbose mode if they have a different preference. - if (failure_count == 0 and run.summary != Summary.all) return cleanExit(); - - const ttyconf = run.ttyconf; - const stderr = run.stderr; - - if (run.summary != Summary.none) { - const total_count = success_count + failure_count + pending_count + skipped_count; - ttyconf.setColor(stderr, .cyan) catch {}; - stderr.writeAll("Build Summary:") catch {}; - ttyconf.setColor(stderr, .reset) catch {}; - stderr.writer().print(" {d}/{d} steps succeeded", .{ success_count, total_count }) catch {}; - if (skipped_count > 0) stderr.writer().print("; {d} skipped", .{skipped_count}) catch {}; - if (failure_count > 0) stderr.writer().print("; {d} failed", .{failure_count}) catch {}; - - if (test_count > 0) stderr.writer().print("; {d}/{d} tests passed", .{ test_pass_count, test_count }) catch {}; - if (test_skip_count > 0) stderr.writer().print("; {d} skipped", .{test_skip_count}) catch {}; - if (test_fail_count > 0) stderr.writer().print("; {d} failed", .{test_fail_count}) catch {}; - if (test_leak_count > 0) stderr.writer().print("; {d} leaked", .{test_leak_count}) catch {}; - - if (run.summary == null) { - ttyconf.setColor(stderr, .dim) catch {}; - stderr.writeAll(" (disable with --summary none)") catch {}; - ttyconf.setColor(stderr, .reset) catch {}; - } - stderr.writeAll("\n") catch {}; - const failures_only = run.summary != Summary.all; - - // Print a fancy tree with build results. - var print_node: PrintNode = .{ .parent = null }; - if (step_names.len == 0) { - print_node.last = true; - printTreeStep(b, b.default_step, run, stderr, ttyconf, &print_node, &step_stack, failures_only) catch {}; - } else { - const last_index = if (!failures_only) b.top_level_steps.count() else blk: { - var i: usize = step_names.len; - while (i > 0) { - i -= 1; - if (b.top_level_steps.get(step_names[i]).?.step.state != .success) break :blk i; - } - break :blk b.top_level_steps.count(); - }; - for (step_names, 0..) |step_name, i| { - const tls = b.top_level_steps.get(step_name).?; - print_node.last = i + 1 == last_index; - printTreeStep(b, &tls.step, run, stderr, ttyconf, &print_node, &step_stack, failures_only) catch {}; - } - } - } - - if (failure_count == 0) return cleanExit(); - - // Finally, render compile errors at the bottom of the terminal. - // We use a separate compile_error_steps array list because step_stack is destructively - // mutated in printTreeStep above. - if (run.prominent_compile_errors and total_compile_errors > 0) { - for (compile_error_steps.items) |s| { - if (s.result_error_bundle.errorMessageCount() > 0) { - s.result_error_bundle.renderToStdErr(renderOptions(ttyconf)); - } - } - - // Signal to parent process that we have printed compile errors. The - // parent process may choose to omit the "following command failed" - // line in this case. - process.exit(2); - } - - process.exit(1); -} - -const PrintNode = struct { - parent: ?*PrintNode, - last: bool = false, -}; - -fn printPrefix(node: *PrintNode, stderr: File, ttyconf: std.io.tty.Config) !void { - const parent = node.parent orelse return; - if (parent.parent == null) return; - try printPrefix(parent, stderr, ttyconf); - if (parent.last) { - try stderr.writeAll(" "); - } else { - try stderr.writeAll(switch (ttyconf) { - .no_color, .windows_api => "| ", - .escape_codes => "\x1B\x28\x30\x78\x1B\x28\x42 ", // │ - }); - } -} - -fn printChildNodePrefix(stderr: File, ttyconf: std.io.tty.Config) !void { - try stderr.writeAll(switch (ttyconf) { - .no_color, .windows_api => "+- ", - .escape_codes => "\x1B\x28\x30\x6d\x71\x1B\x28\x42 ", // └─ - }); -} - -fn printStepStatus( - s: *Step, - stderr: File, - ttyconf: std.io.tty.Config, - run: *const Run, -) !void { - switch (s.state) { - .precheck_unstarted => unreachable, - .precheck_started => unreachable, - .precheck_done => unreachable, - .running => unreachable, - - .dependency_failure => { - try ttyconf.setColor(stderr, .dim); - try stderr.writeAll(" transitive failure\n"); - try ttyconf.setColor(stderr, .reset); - }, - - .success => { - try ttyconf.setColor(stderr, .green); - if (s.result_cached) { - try stderr.writeAll(" cached"); - } else if (s.test_results.test_count > 0) { - const pass_count = s.test_results.passCount(); - try stderr.writer().print(" {d} passed", .{pass_count}); - if (s.test_results.skip_count > 0) { - try ttyconf.setColor(stderr, .yellow); - try stderr.writer().print(" {d} skipped", .{s.test_results.skip_count}); - } - } else { - try stderr.writeAll(" success"); - } - try ttyconf.setColor(stderr, .reset); - if (s.result_duration_ns) |ns| { - try ttyconf.setColor(stderr, .dim); - if (ns >= std.time.ns_per_min) { - try stderr.writer().print(" {d}m", .{ns / std.time.ns_per_min}); - } else if (ns >= std.time.ns_per_s) { - try stderr.writer().print(" {d}s", .{ns / std.time.ns_per_s}); - } else if (ns >= std.time.ns_per_ms) { - try stderr.writer().print(" {d}ms", .{ns / std.time.ns_per_ms}); - } else if (ns >= std.time.ns_per_us) { - try stderr.writer().print(" {d}us", .{ns / std.time.ns_per_us}); - } else { - try stderr.writer().print(" {d}ns", .{ns}); - } - try ttyconf.setColor(stderr, .reset); - } - if (s.result_peak_rss != 0) { - const rss = s.result_peak_rss; - try ttyconf.setColor(stderr, .dim); - if (rss >= 1000_000_000) { - try stderr.writer().print(" MaxRSS:{d}G", .{rss / 1000_000_000}); - } else if (rss >= 1000_000) { - try stderr.writer().print(" MaxRSS:{d}M", .{rss / 1000_000}); - } else if (rss >= 1000) { - try stderr.writer().print(" MaxRSS:{d}K", .{rss / 1000}); - } else { - try stderr.writer().print(" MaxRSS:{d}B", .{rss}); - } - try ttyconf.setColor(stderr, .reset); - } - try stderr.writeAll("\n"); - }, - .skipped, .skipped_oom => |skip| { - try ttyconf.setColor(stderr, .yellow); - try stderr.writeAll(" skipped"); - if (skip == .skipped_oom) { - try stderr.writeAll(" (not enough memory)"); - try ttyconf.setColor(stderr, .dim); - try stderr.writer().print(" upper bound of {d} exceeded runner limit ({d})", .{ s.max_rss, run.max_rss }); - try ttyconf.setColor(stderr, .yellow); - } - try stderr.writeAll("\n"); - try ttyconf.setColor(stderr, .reset); - }, - .failure => try printStepFailure(s, stderr, ttyconf), - } -} - -fn printStepFailure( - s: *Step, - stderr: File, - ttyconf: std.io.tty.Config, -) !void { - if (s.result_error_bundle.errorMessageCount() > 0) { - try ttyconf.setColor(stderr, .red); - try stderr.writer().print(" {d} errors\n", .{ - s.result_error_bundle.errorMessageCount(), - }); - try ttyconf.setColor(stderr, .reset); - } else if (!s.test_results.isSuccess()) { - try stderr.writer().print(" {d}/{d} passed", .{ - s.test_results.passCount(), s.test_results.test_count, - }); - if (s.test_results.fail_count > 0) { - try stderr.writeAll(", "); - try ttyconf.setColor(stderr, .red); - try stderr.writer().print("{d} failed", .{ - s.test_results.fail_count, - }); - try ttyconf.setColor(stderr, .reset); - } - if (s.test_results.skip_count > 0) { - try stderr.writeAll(", "); - try ttyconf.setColor(stderr, .yellow); - try stderr.writer().print("{d} skipped", .{ - s.test_results.skip_count, - }); - try ttyconf.setColor(stderr, .reset); - } - if (s.test_results.leak_count > 0) { - try stderr.writeAll(", "); - try ttyconf.setColor(stderr, .red); - try stderr.writer().print("{d} leaked", .{ - s.test_results.leak_count, - }); - try ttyconf.setColor(stderr, .reset); - } - try stderr.writeAll("\n"); - } else if (s.result_error_msgs.items.len > 0) { - try ttyconf.setColor(stderr, .red); - try stderr.writeAll(" failure\n"); - try ttyconf.setColor(stderr, .reset); - } else { - assert(s.result_stderr.len > 0); - try ttyconf.setColor(stderr, .red); - try stderr.writeAll(" stderr\n"); - try ttyconf.setColor(stderr, .reset); - } -} - -fn printTreeStep( - b: *std.Build, - s: *Step, - run: *const Run, - stderr: File, - ttyconf: std.io.tty.Config, - parent_node: *PrintNode, - step_stack: *std.AutoArrayHashMapUnmanaged(*Step, void), - failures_only: bool, -) !void { - const first = step_stack.swapRemove(s); - if (failures_only and s.state == .success) return; - try printPrefix(parent_node, stderr, ttyconf); - - if (!first) try ttyconf.setColor(stderr, .dim); - if (parent_node.parent != null) { - if (parent_node.last) { - try printChildNodePrefix(stderr, ttyconf); - } else { - try stderr.writeAll(switch (ttyconf) { - .no_color, .windows_api => "+- ", - .escape_codes => "\x1B\x28\x30\x74\x71\x1B\x28\x42 ", // ├─ - }); - } - } - - // dep_prefix omitted here because it is redundant with the tree. - try stderr.writeAll(s.name); - - if (first) { - try printStepStatus(s, stderr, ttyconf, run); - - const last_index = if (!failures_only) s.dependencies.items.len -| 1 else blk: { - var i: usize = s.dependencies.items.len; - while (i > 0) { - i -= 1; - if (s.dependencies.items[i].state != .success) break :blk i; - } - break :blk s.dependencies.items.len -| 1; - }; - for (s.dependencies.items, 0..) |dep, i| { - var print_node: PrintNode = .{ - .parent = parent_node, - .last = i == last_index, - }; - try printTreeStep(b, dep, run, stderr, ttyconf, &print_node, step_stack, failures_only); - } - } else { - if (s.dependencies.items.len == 0) { - try stderr.writeAll(" (reused)\n"); - } else { - try stderr.writer().print(" (+{d} more reused dependencies)\n", .{ - s.dependencies.items.len, - }); - } - try ttyconf.setColor(stderr, .reset); - } -} - -/// Traverse the dependency graph depth-first and make it undirected by having -/// steps know their dependants (they only know dependencies at start). -/// Along the way, check that there is no dependency loop, and record the steps -/// in traversal order in `step_stack`. -/// Each step has its dependencies traversed in random order, this accomplishes -/// two things: -/// - `step_stack` will be in randomized-depth-first order, so the build runner -/// spawns steps in a random (but optimized) order -/// - each step's `dependants` list is also filled in a random order, so that -/// when it finishes executing in `workerMakeOneStep`, it spawns next steps -/// to run in random order -fn constructGraphAndCheckForDependencyLoop( - b: *std.Build, - s: *Step, - step_stack: *std.AutoArrayHashMapUnmanaged(*Step, void), - rand: std.Random, -) !void { - switch (s.state) { - .precheck_started => { - std.debug.print("dependency loop detected:\n {s}\n", .{s.name}); - return error.DependencyLoopDetected; - }, - .precheck_unstarted => { - s.state = .precheck_started; - - try step_stack.ensureUnusedCapacity(b.allocator, s.dependencies.items.len); - - // We dupe to avoid shuffling the steps in the summary, it depends - // on s.dependencies' order. - const deps = b.allocator.dupe(*Step, s.dependencies.items) catch @panic("OOM"); - rand.shuffle(*Step, deps); - - for (deps) |dep| { - try step_stack.put(b.allocator, dep, {}); - try dep.dependants.append(b.allocator, s); - constructGraphAndCheckForDependencyLoop(b, dep, step_stack, rand) catch |err| { - if (err == error.DependencyLoopDetected) { - std.debug.print(" {s}\n", .{s.name}); - } - return err; - }; - } - - s.state = .precheck_done; - }, - .precheck_done => {}, - - // These don't happen until we actually run the step graph. - .dependency_failure => unreachable, - .running => unreachable, - .success => unreachable, - .failure => unreachable, - .skipped => unreachable, - .skipped_oom => unreachable, - } -} - -fn workerMakeOneStep( - wg: *std.Thread.WaitGroup, - thread_pool: *std.Thread.Pool, - b: *std.Build, - s: *Step, - prog_node: *std.Progress.Node, - run: *Run, -) void { - defer wg.finish(); - - // First, check the conditions for running this step. If they are not met, - // then we return without doing the step, relying on another worker to - // queue this step up again when dependencies are met. - for (s.dependencies.items) |dep| { - switch (@atomicLoad(Step.State, &dep.state, .SeqCst)) { - .success, .skipped => continue, - .failure, .dependency_failure, .skipped_oom => { - @atomicStore(Step.State, &s.state, .dependency_failure, .SeqCst); - return; - }, - .precheck_done, .running => { - // dependency is not finished yet. - return; - }, - .precheck_unstarted => unreachable, - .precheck_started => unreachable, - } - } - - if (s.max_rss != 0) { - run.max_rss_mutex.lock(); - defer run.max_rss_mutex.unlock(); - - // Avoid running steps twice. - if (s.state != .precheck_done) { - // Another worker got the job. - return; - } - - const new_claimed_rss = run.claimed_rss + s.max_rss; - if (new_claimed_rss > run.max_rss) { - // Running this step right now could possibly exceed the allotted RSS. - // Add this step to the queue of memory-blocked steps. - run.memory_blocked_steps.append(s) catch @panic("OOM"); - return; - } - - run.claimed_rss = new_claimed_rss; - s.state = .running; - } else { - // Avoid running steps twice. - if (@cmpxchgStrong(Step.State, &s.state, .precheck_done, .running, .SeqCst, .SeqCst) != null) { - // Another worker got the job. - return; - } - } - - var sub_prog_node = prog_node.start(s.name, 0); - sub_prog_node.activate(); - defer sub_prog_node.end(); - - const make_result = s.make(&sub_prog_node); - - // No matter the result, we want to display error/warning messages. - const show_compile_errors = !run.prominent_compile_errors and - s.result_error_bundle.errorMessageCount() > 0; - const show_error_msgs = s.result_error_msgs.items.len > 0; - const show_stderr = s.result_stderr.len > 0; - - if (show_error_msgs or show_compile_errors or show_stderr) { - sub_prog_node.context.lock_stderr(); - defer sub_prog_node.context.unlock_stderr(); - - printErrorMessages(b, s, run) catch {}; - } - - handle_result: { - if (make_result) |_| { - @atomicStore(Step.State, &s.state, .success, .SeqCst); - } else |err| switch (err) { - error.MakeFailed => { - @atomicStore(Step.State, &s.state, .failure, .SeqCst); - break :handle_result; - }, - error.MakeSkipped => @atomicStore(Step.State, &s.state, .skipped, .SeqCst), - } - - // Successful completion of a step, so we queue up its dependants as well. - for (s.dependants.items) |dep| { - wg.start(); - thread_pool.spawn(workerMakeOneStep, .{ - wg, thread_pool, b, dep, prog_node, run, - }) catch @panic("OOM"); - } - } - - // If this is a step that claims resources, we must now queue up other - // steps that are waiting for resources. - if (s.max_rss != 0) { - run.max_rss_mutex.lock(); - defer run.max_rss_mutex.unlock(); - - // Give the memory back to the scheduler. - run.claimed_rss -= s.max_rss; - // Avoid kicking off too many tasks that we already know will not have - // enough resources. - var remaining = run.max_rss - run.claimed_rss; - var i: usize = 0; - var j: usize = 0; - while (j < run.memory_blocked_steps.items.len) : (j += 1) { - const dep = run.memory_blocked_steps.items[j]; - assert(dep.max_rss != 0); - if (dep.max_rss <= remaining) { - remaining -= dep.max_rss; - - wg.start(); - thread_pool.spawn(workerMakeOneStep, .{ - wg, thread_pool, b, dep, prog_node, run, - }) catch @panic("OOM"); - } else { - run.memory_blocked_steps.items[i] = dep; - i += 1; - } - } - run.memory_blocked_steps.shrinkRetainingCapacity(i); - } -} - -fn printErrorMessages(b: *std.Build, failing_step: *Step, run: *const Run) !void { - const gpa = b.allocator; - const stderr = run.stderr; - const ttyconf = run.ttyconf; - - // Provide context for where these error messages are coming from by - // printing the corresponding Step subtree. - - var step_stack: std.ArrayListUnmanaged(*Step) = .{}; - defer step_stack.deinit(gpa); - try step_stack.append(gpa, failing_step); - while (step_stack.items[step_stack.items.len - 1].dependants.items.len != 0) { - try step_stack.append(gpa, step_stack.items[step_stack.items.len - 1].dependants.items[0]); - } - - // Now, `step_stack` has the subtree that we want to print, in reverse order. - try ttyconf.setColor(stderr, .dim); - var indent: usize = 0; - while (step_stack.popOrNull()) |s| : (indent += 1) { - if (indent > 0) { - try stderr.writer().writeByteNTimes(' ', (indent - 1) * 3); - try printChildNodePrefix(stderr, ttyconf); - } - - try stderr.writeAll(s.name); - - if (s == failing_step) { - try printStepFailure(s, stderr, ttyconf); - } else { - try stderr.writeAll("\n"); - } - } - try ttyconf.setColor(stderr, .reset); - - if (failing_step.result_stderr.len > 0) { - try stderr.writeAll(failing_step.result_stderr); - if (!mem.endsWith(u8, failing_step.result_stderr, "\n")) { - try stderr.writeAll("\n"); - } - } - - if (!run.prominent_compile_errors and failing_step.result_error_bundle.errorMessageCount() > 0) - try failing_step.result_error_bundle.renderToWriter(renderOptions(ttyconf), stderr.writer()); - - for (failing_step.result_error_msgs.items) |msg| { - try ttyconf.setColor(stderr, .red); - try stderr.writeAll("error: "); - try ttyconf.setColor(stderr, .reset); - try stderr.writeAll(msg); - try stderr.writeAll("\n"); - } -} - -fn steps(builder: *std.Build, out_stream: anytype) !void { - const allocator = builder.allocator; - for (builder.top_level_steps.values()) |top_level_step| { - const name = if (&top_level_step.step == builder.default_step) - try fmt.allocPrint(allocator, "{s} (default)", .{top_level_step.step.name}) - else - top_level_step.step.name; - try out_stream.print(" {s:<28} {s}\n", .{ name, top_level_step.description }); - } -} - -fn usage(b: *std.Build, out_stream: anytype) !void { - try out_stream.print( - \\Usage: {s} build [steps] [options] - \\ - \\Steps: - \\ - , .{b.graph.zig_exe}); - try steps(b, out_stream); - - try out_stream.writeAll( - \\ - \\General Options: - \\ -p, --prefix [path] Where to install files (default: zig-out) - \\ --prefix-lib-dir [path] Where to install libraries - \\ --prefix-exe-dir [path] Where to install executables - \\ --prefix-include-dir [path] Where to install C header files - \\ - \\ --release[=mode] Request release mode, optionally specifying a - \\ preferred optimization mode: fast, safe, small - \\ - \\ -fdarling, -fno-darling Integration with system-installed Darling to - \\ execute macOS programs on Linux hosts - \\ (default: no) - \\ -fqemu, -fno-qemu Integration with system-installed QEMU to execute - \\ foreign-architecture programs on Linux hosts - \\ (default: no) - \\ --glibc-runtimes [path] Enhances QEMU integration by providing glibc built - \\ for multiple foreign architectures, allowing - \\ execution of non-native programs that link with glibc. - \\ -frosetta, -fno-rosetta Rely on Rosetta to execute x86_64 programs on - \\ ARM64 macOS hosts. (default: no) - \\ -fwasmtime, -fno-wasmtime Integration with system-installed wasmtime to - \\ execute WASI binaries. (default: no) - \\ -fwine, -fno-wine Integration with system-installed Wine to execute - \\ Windows programs on Linux hosts. (default: no) - \\ - \\ -h, --help Print this help and exit - \\ -l, --list-steps Print available steps - \\ --verbose Print commands before executing them - \\ --color [auto|off|on] Enable or disable colored error messages - \\ --prominent-compile-errors Buffer compile errors and display at end - \\ --summary [mode] Control the printing of the build summary - \\ all Print the build summary in its entirety - \\ failures (Default) Only print failed steps - \\ none Do not print the build summary - \\ -j Limit concurrent jobs (default is to use all CPU cores) - \\ --maxrss Limit memory usage (default is to use available memory) - \\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss - \\ --fetch Exit after fetching dependency tree - \\ - \\Project-Specific Options: - \\ - ); - - const arena = b.allocator; - if (b.available_options_list.items.len == 0) { - try out_stream.print(" (none)\n", .{}); - } else { - for (b.available_options_list.items) |option| { - const name = try fmt.allocPrint(arena, " -D{s}=[{s}]", .{ - option.name, - @tagName(option.type_id), - }); - try out_stream.print("{s:<30} {s}\n", .{ name, option.description }); - if (option.enum_options) |enum_options| { - const padding = " " ** 33; - try out_stream.writeAll(padding ++ "Supported Values:\n"); - for (enum_options) |enum_option| { - try out_stream.print(padding ++ " {s}\n", .{enum_option}); - } - } - } - } - - try out_stream.writeAll( - \\ - \\System Integration Options: - \\ --search-prefix [path] Add a path to look for binaries, libraries, headers - \\ --sysroot [path] Set the system root directory (usually /) - \\ --libc [file] Provide a file which specifies libc paths - \\ - \\ --host-target [triple] Use the provided target as the host - \\ --host-cpu [cpu] Use the provided CPU as the host - \\ --host-dynamic-linker [path] Use the provided dynamic linker as the host - \\ - \\ --system [pkgdir] Disable package fetching; enable all integrations - \\ -fsys=[name] Enable a system integration - \\ -fno-sys=[name] Disable a system integration - \\ - \\ Available System Integrations: Enabled: - \\ - ); - if (b.graph.system_library_options.entries.len == 0) { - try out_stream.writeAll(" (none) -\n"); - } else { - for (b.graph.system_library_options.keys(), b.graph.system_library_options.values()) |k, v| { - const status = switch (v) { - .declared_enabled => "yes", - .declared_disabled => "no", - .user_enabled, .user_disabled => unreachable, // already emitted error - }; - try out_stream.print(" {s:<43} {s}\n", .{ k, status }); - } - } - - try out_stream.writeAll( - \\ - \\Advanced Options: - \\ -freference-trace[=num] How many lines of reference trace should be shown per compile error - \\ -fno-reference-trace Disable reference trace - \\ --build-file [file] Override path to build.zig - \\ --cache-dir [path] Override path to local Zig cache directory - \\ --global-cache-dir [path] Override path to global Zig cache directory - \\ --zig-lib-dir [arg] Override path to Zig lib directory - \\ --build-runner [file] Override path to build runner - \\ --seed [integer] For shuffling dependency traversal order (default: random) - \\ --debug-log [scope] Enable debugging the compiler - \\ --debug-pkg-config Fail if unknown pkg-config flags encountered - \\ --verbose-link Enable compiler debug output for linking - \\ --verbose-air Enable compiler debug output for Zig AIR - \\ --verbose-llvm-ir[=file] Enable compiler debug output for LLVM IR - \\ --verbose-llvm-bc=[file] Enable compiler debug output for LLVM BC - \\ --verbose-cimport Enable compiler debug output for C imports - \\ --verbose-cc Enable compiler debug output for C compilation - \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features - \\ - ); -} - -fn nextArg(args: [][:0]const u8, idx: *usize) ?[:0]const u8 { - if (idx.* >= args.len) return null; - defer idx.* += 1; - return args[idx.*]; -} - -fn nextArgOrFatal(args: [][:0]const u8, idx: *usize) [:0]const u8 { - return nextArg(args, idx) orelse { - std.debug.print("expected argument after '{s}'\n access the help menu with 'zig build -h'\n", .{args[idx.*]}); - process.exit(1); - }; -} - -fn argsRest(args: [][:0]const u8, idx: usize) ?[][:0]const u8 { - if (idx >= args.len) return null; - return args[idx..]; -} - -fn cleanExit() void { - // Perhaps in the future there could be an Advanced Options flag such as - // --debug-build-runner-leaks which would make this function return instead - // of calling exit. - process.exit(0); -} - -const Color = enum { auto, off, on }; -const Summary = enum { all, failures, none }; - -fn get_tty_conf(color: Color, stderr: File) std.io.tty.Config { - return switch (color) { - .auto => std.io.tty.detectConfig(stderr), - .on => .escape_codes, - .off => .no_color, - }; -} - -fn renderOptions(ttyconf: std.io.tty.Config) std.zig.ErrorBundle.RenderOptions { - return .{ - .ttyconf = ttyconf, - .include_source_line = ttyconf != .no_color, - .include_reference_trace = ttyconf != .no_color, - }; -} - -fn fatalWithHint(comptime f: []const u8, args: anytype) noreturn { - std.debug.print(f ++ "\n access the help menu with 'zig build -h'\n", args); - process.exit(1); -} - -fn fatal(comptime f: []const u8, args: anytype) noreturn { - std.debug.print(f ++ "\n", args); - process.exit(1); -} - -fn validateSystemLibraryOptions(b: *std.Build) void { - var bad = false; - for (b.graph.system_library_options.keys(), b.graph.system_library_options.values()) |k, v| { - switch (v) { - .user_disabled, .user_enabled => { - // The user tried to enable or disable a system library integration, but - // the build script did not recognize that option. - std.debug.print("system library name not recognized by build script: '{s}'\n", .{k}); - bad = true; - }, - .declared_disabled, .declared_enabled => {}, - } - } - if (bad) { - std.debug.print(" access the help menu with 'zig build -h'\n", .{}); - process.exit(1); - } -} diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig new file mode 100644 index 0000000000000000000000000000000000000000..071b56a71cc053e121fd31108c333adb8d6364cb --- /dev/null +++ b/lib/compiler/build_runner.zig @@ -0,0 +1,1273 @@ +const root = @import("@build"); +const std = @import("std"); +const builtin = @import("builtin"); +const assert = std.debug.assert; +const io = std.io; +const fmt = std.fmt; +const mem = std.mem; +const process = std.process; +const ArrayList = std.ArrayList; +const File = std.fs.File; +const Step = std.Build.Step; + +pub const dependencies = @import("@dependencies"); + +pub fn main() !void { + // Here we use an ArenaAllocator backed by a page allocator because a build is a short-lived, + // one shot program. We don't need to waste time freeing memory and finding places to squish + // bytes into. So we free everything all at once at the very end. + var single_threaded_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer single_threaded_arena.deinit(); + + var thread_safe_arena: std.heap.ThreadSafeAllocator = .{ + .child_allocator = single_threaded_arena.allocator(), + }; + const arena = thread_safe_arena.allocator(); + + const args = try process.argsAlloc(arena); + + // skip my own exe name + var arg_idx: usize = 1; + + const zig_exe = nextArg(args, &arg_idx) orelse { + std.debug.print("Expected path to zig compiler\n", .{}); + return error.InvalidArgs; + }; + const build_root = nextArg(args, &arg_idx) orelse { + std.debug.print("Expected build root directory path\n", .{}); + return error.InvalidArgs; + }; + const cache_root = nextArg(args, &arg_idx) orelse { + std.debug.print("Expected cache root directory path\n", .{}); + return error.InvalidArgs; + }; + const global_cache_root = nextArg(args, &arg_idx) orelse { + std.debug.print("Expected global cache root directory path\n", .{}); + return error.InvalidArgs; + }; + + const build_root_directory: std.Build.Cache.Directory = .{ + .path = build_root, + .handle = try std.fs.cwd().openDir(build_root, .{}), + }; + + const local_cache_directory: std.Build.Cache.Directory = .{ + .path = cache_root, + .handle = try std.fs.cwd().makeOpenPath(cache_root, .{}), + }; + + const global_cache_directory: std.Build.Cache.Directory = .{ + .path = global_cache_root, + .handle = try std.fs.cwd().makeOpenPath(global_cache_root, .{}), + }; + + var graph: std.Build.Graph = .{ + .arena = arena, + .cache = .{ + .gpa = arena, + .manifest_dir = try local_cache_directory.handle.makeOpenPath("h", .{}), + }, + .zig_exe = zig_exe, + .env_map = try process.getEnvMap(arena), + .global_cache_root = global_cache_directory, + }; + + graph.cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() }); + graph.cache.addPrefix(build_root_directory); + graph.cache.addPrefix(local_cache_directory); + graph.cache.addPrefix(global_cache_directory); + graph.cache.hash.addBytes(builtin.zig_version_string); + + const builder = try std.Build.create( + &graph, + build_root_directory, + local_cache_directory, + dependencies.root_deps, + ); + + var targets = ArrayList([]const u8).init(arena); + var debug_log_scopes = ArrayList([]const u8).init(arena); + var thread_pool_options: std.Thread.Pool.Options = .{ .allocator = arena }; + + var install_prefix: ?[]const u8 = null; + var dir_list = std.Build.DirList{}; + var summary: ?Summary = null; + var max_rss: u64 = 0; + var skip_oom_steps: bool = false; + var color: Color = .auto; + var seed: u32 = 0; + var prominent_compile_errors: bool = false; + var help_menu: bool = false; + var steps_menu: bool = false; + var output_tmp_nonce: ?[16]u8 = null; + + while (nextArg(args, &arg_idx)) |arg| { + if (mem.startsWith(u8, arg, "-Z")) { + if (arg.len != 18) fatalWithHint("bad argument: '{s}'", .{arg}); + output_tmp_nonce = arg[2..18].*; + } else if (mem.startsWith(u8, arg, "-D")) { + const option_contents = arg[2..]; + if (option_contents.len == 0) + fatalWithHint("expected option name after '-D'", .{}); + if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| { + const option_name = option_contents[0..name_end]; + const option_value = option_contents[name_end + 1 ..]; + if (try builder.addUserInputOption(option_name, option_value)) + fatal(" access the help menu with 'zig build -h'", .{}); + } else { + if (try builder.addUserInputFlag(option_contents)) + fatal(" access the help menu with 'zig build -h'", .{}); + } + } else if (mem.startsWith(u8, arg, "-")) { + if (mem.eql(u8, arg, "--verbose")) { + builder.verbose = true; + } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { + help_menu = true; + } else if (mem.eql(u8, arg, "-p") or mem.eql(u8, arg, "--prefix")) { + install_prefix = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "-l") or mem.eql(u8, arg, "--list-steps")) { + steps_menu = true; + } else if (mem.startsWith(u8, arg, "-fsys=")) { + const name = arg["-fsys=".len..]; + graph.system_library_options.put(arena, name, .user_enabled) catch @panic("OOM"); + } else if (mem.startsWith(u8, arg, "-fno-sys=")) { + const name = arg["-fno-sys=".len..]; + graph.system_library_options.put(arena, name, .user_disabled) catch @panic("OOM"); + } else if (mem.eql(u8, arg, "--release")) { + builder.release_mode = .any; + } else if (mem.startsWith(u8, arg, "--release=")) { + const text = arg["--release=".len..]; + builder.release_mode = std.meta.stringToEnum(std.Build.ReleaseMode, text) orelse { + fatalWithHint("expected [off|any|fast|safe|small] in '{s}', found '{s}'", .{ + arg, text, + }); + }; + } else if (mem.eql(u8, arg, "--host-target")) { + graph.host_query_options.arch_os_abi = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--host-cpu")) { + graph.host_query_options.cpu_features = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--host-dynamic-linker")) { + graph.host_query_options.dynamic_linker = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--prefix-lib-dir")) { + dir_list.lib_dir = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--prefix-exe-dir")) { + dir_list.exe_dir = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--prefix-include-dir")) { + dir_list.include_dir = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--sysroot")) { + builder.sysroot = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--maxrss")) { + const max_rss_text = nextArgOrFatal(args, &arg_idx); + max_rss = std.fmt.parseIntSizeSuffix(max_rss_text, 10) catch |err| { + std.debug.print("invalid byte size: '{s}': {s}\n", .{ + max_rss_text, @errorName(err), + }); + process.exit(1); + }; + } else if (mem.eql(u8, arg, "--skip-oom-steps")) { + skip_oom_steps = true; + } else if (mem.eql(u8, arg, "--search-prefix")) { + const search_prefix = nextArgOrFatal(args, &arg_idx); + builder.addSearchPrefix(search_prefix); + } else if (mem.eql(u8, arg, "--libc")) { + builder.libc_file = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--color")) { + const next_arg = nextArg(args, &arg_idx) orelse + fatalWithHint("expected [auto|on|off] after '{s}'", .{arg}); + color = std.meta.stringToEnum(Color, next_arg) orelse { + fatalWithHint("expected [auto|on|off] after '{s}', found '{s}'", .{ + arg, next_arg, + }); + }; + } else if (mem.eql(u8, arg, "--summary")) { + const next_arg = nextArg(args, &arg_idx) orelse + fatalWithHint("expected [all|failures|none] after '{s}'", .{arg}); + summary = std.meta.stringToEnum(Summary, next_arg) orelse { + fatalWithHint("expected [all|failures|none] after '{s}', found '{s}'", .{ + arg, next_arg, + }); + }; + } else if (mem.eql(u8, arg, "--zig-lib-dir")) { + builder.zig_lib_dir = .{ .cwd_relative = nextArgOrFatal(args, &arg_idx) }; + } else if (mem.eql(u8, arg, "--seed")) { + const next_arg = nextArg(args, &arg_idx) orelse + fatalWithHint("expected u32 after '{s}'", .{arg}); + seed = std.fmt.parseUnsigned(u32, next_arg, 0) catch |err| { + fatal("unable to parse seed '{s}' as 32-bit integer: {s}\n", .{ + next_arg, @errorName(err), + }); + }; + } else if (mem.eql(u8, arg, "--debug-log")) { + const next_arg = nextArgOrFatal(args, &arg_idx); + try debug_log_scopes.append(next_arg); + } else if (mem.eql(u8, arg, "--debug-pkg-config")) { + builder.debug_pkg_config = true; + } else if (mem.eql(u8, arg, "--debug-compile-errors")) { + builder.debug_compile_errors = true; + } else if (mem.eql(u8, arg, "--system")) { + // The usage text shows another argument after this parameter + // but it is handled by the parent process. The build runner + // only sees this flag. + graph.system_package_mode = true; + } else if (mem.eql(u8, arg, "--glibc-runtimes")) { + builder.glibc_runtimes_dir = nextArgOrFatal(args, &arg_idx); + } else if (mem.eql(u8, arg, "--verbose-link")) { + builder.verbose_link = true; + } else if (mem.eql(u8, arg, "--verbose-air")) { + builder.verbose_air = true; + } else if (mem.eql(u8, arg, "--verbose-llvm-ir")) { + builder.verbose_llvm_ir = "-"; + } else if (mem.startsWith(u8, arg, "--verbose-llvm-ir=")) { + builder.verbose_llvm_ir = arg["--verbose-llvm-ir=".len..]; + } else if (mem.eql(u8, arg, "--verbose-llvm-bc=")) { + builder.verbose_llvm_bc = arg["--verbose-llvm-bc=".len..]; + } else if (mem.eql(u8, arg, "--verbose-cimport")) { + builder.verbose_cimport = true; + } else if (mem.eql(u8, arg, "--verbose-cc")) { + builder.verbose_cc = true; + } else if (mem.eql(u8, arg, "--verbose-llvm-cpu-features")) { + builder.verbose_llvm_cpu_features = true; + } else if (mem.eql(u8, arg, "--prominent-compile-errors")) { + prominent_compile_errors = true; + } else if (mem.eql(u8, arg, "-fwine")) { + builder.enable_wine = true; + } else if (mem.eql(u8, arg, "-fno-wine")) { + builder.enable_wine = false; + } else if (mem.eql(u8, arg, "-fqemu")) { + builder.enable_qemu = true; + } else if (mem.eql(u8, arg, "-fno-qemu")) { + builder.enable_qemu = false; + } else if (mem.eql(u8, arg, "-fwasmtime")) { + builder.enable_wasmtime = true; + } else if (mem.eql(u8, arg, "-fno-wasmtime")) { + builder.enable_wasmtime = false; + } else if (mem.eql(u8, arg, "-frosetta")) { + builder.enable_rosetta = true; + } else if (mem.eql(u8, arg, "-fno-rosetta")) { + builder.enable_rosetta = false; + } else if (mem.eql(u8, arg, "-fdarling")) { + builder.enable_darling = true; + } else if (mem.eql(u8, arg, "-fno-darling")) { + builder.enable_darling = false; + } else if (mem.eql(u8, arg, "-freference-trace")) { + builder.reference_trace = 256; + } else if (mem.startsWith(u8, arg, "-freference-trace=")) { + const num = arg["-freference-trace=".len..]; + builder.reference_trace = std.fmt.parseUnsigned(u32, num, 10) catch |err| { + std.debug.print("unable to parse reference_trace count '{s}': {s}", .{ num, @errorName(err) }); + process.exit(1); + }; + } else if (mem.eql(u8, arg, "-fno-reference-trace")) { + builder.reference_trace = null; + } else if (mem.startsWith(u8, arg, "-j")) { + const num = arg["-j".len..]; + const n_jobs = std.fmt.parseUnsigned(u32, num, 10) catch |err| { + std.debug.print("unable to parse jobs count '{s}': {s}", .{ + num, @errorName(err), + }); + process.exit(1); + }; + if (n_jobs < 1) { + std.debug.print("number of jobs must be at least 1\n", .{}); + process.exit(1); + } + thread_pool_options.n_jobs = n_jobs; + } else if (mem.eql(u8, arg, "--")) { + builder.args = argsRest(args, arg_idx); + break; + } else { + fatalWithHint("unrecognized argument: '{s}'", .{arg}); + } + } else { + try targets.append(arg); + } + } + + const host_query = std.Build.parseTargetQuery(graph.host_query_options) catch |err| switch (err) { + error.ParseFailed => process.exit(1), + }; + builder.host = .{ + .query = .{}, + .result = try std.zig.system.resolveTargetQuery(host_query), + }; + + const stderr = std.io.getStdErr(); + const ttyconf = get_tty_conf(color, stderr); + switch (ttyconf) { + .no_color => try graph.env_map.put("NO_COLOR", "1"), + .escape_codes => try graph.env_map.put("YES_COLOR", "1"), + .windows_api => {}, + } + + var progress: std.Progress = .{ .dont_print_on_dumb = true }; + const main_progress_node = progress.start("", 0); + + builder.debug_log_scopes = debug_log_scopes.items; + builder.resolveInstallPrefix(install_prefix, dir_list); + { + var prog_node = main_progress_node.start("user build.zig logic", 0); + defer prog_node.end(); + try builder.runBuild(root); + } + + if (graph.needed_lazy_dependencies.entries.len != 0) { + var buffer: std.ArrayListUnmanaged(u8) = .{}; + for (graph.needed_lazy_dependencies.keys()) |k| { + try buffer.appendSlice(arena, k); + try buffer.append(arena, '\n'); + } + const s = std.fs.path.sep_str; + const tmp_sub_path = "tmp" ++ s ++ (output_tmp_nonce orelse fatal("missing -Z arg", .{})); + local_cache_directory.handle.writeFile2(.{ + .sub_path = tmp_sub_path, + .data = buffer.items, + .flags = .{ .exclusive = true }, + }) catch |err| { + fatal("unable to write configuration results to '{}{s}': {s}", .{ + local_cache_directory, tmp_sub_path, @errorName(err), + }); + }; + process.exit(3); // Indicate configure phase failed with meaningful stdout. + } + + if (builder.validateUserInputDidItFail()) { + fatal(" access the help menu with 'zig build -h'", .{}); + } + + validateSystemLibraryOptions(builder); + + const stdout_writer = io.getStdOut().writer(); + + if (help_menu) + return usage(builder, stdout_writer); + + if (steps_menu) + return steps(builder, stdout_writer); + + var run: Run = .{ + .max_rss = max_rss, + .max_rss_is_default = false, + .max_rss_mutex = .{}, + .skip_oom_steps = skip_oom_steps, + .memory_blocked_steps = std.ArrayList(*Step).init(arena), + .prominent_compile_errors = prominent_compile_errors, + + .claimed_rss = 0, + .summary = summary, + .ttyconf = ttyconf, + .stderr = stderr, + }; + + if (run.max_rss == 0) { + run.max_rss = process.totalSystemMemory() catch std.math.maxInt(u64); + run.max_rss_is_default = true; + } + + runStepNames( + arena, + builder, + targets.items, + main_progress_node, + thread_pool_options, + &run, + seed, + ) catch |err| switch (err) { + error.UncleanExit => process.exit(1), + else => return err, + }; +} + +const Run = struct { + max_rss: u64, + max_rss_is_default: bool, + max_rss_mutex: std.Thread.Mutex, + skip_oom_steps: bool, + memory_blocked_steps: std.ArrayList(*Step), + prominent_compile_errors: bool, + + claimed_rss: usize, + summary: ?Summary, + ttyconf: std.io.tty.Config, + stderr: File, +}; + +fn runStepNames( + arena: std.mem.Allocator, + b: *std.Build, + step_names: []const []const u8, + parent_prog_node: *std.Progress.Node, + thread_pool_options: std.Thread.Pool.Options, + run: *Run, + seed: u32, +) !void { + const gpa = b.allocator; + var step_stack: std.AutoArrayHashMapUnmanaged(*Step, void) = .{}; + defer step_stack.deinit(gpa); + + if (step_names.len == 0) { + try step_stack.put(gpa, b.default_step, {}); + } else { + try step_stack.ensureUnusedCapacity(gpa, step_names.len); + for (0..step_names.len) |i| { + const step_name = step_names[step_names.len - i - 1]; + const s = b.top_level_steps.get(step_name) orelse { + std.debug.print("no step named '{s}'\n access the help menu with 'zig build -h'\n", .{step_name}); + process.exit(1); + }; + step_stack.putAssumeCapacity(&s.step, {}); + } + } + + const starting_steps = try arena.dupe(*Step, step_stack.keys()); + + var rng = std.Random.DefaultPrng.init(seed); + const rand = rng.random(); + rand.shuffle(*Step, starting_steps); + + for (starting_steps) |s| { + constructGraphAndCheckForDependencyLoop(b, s, &step_stack, rand) catch |err| switch (err) { + error.DependencyLoopDetected => return error.UncleanExit, + else => |e| return e, + }; + } + + { + // Check that we have enough memory to complete the build. + var any_problems = false; + for (step_stack.keys()) |s| { + if (s.max_rss == 0) continue; + if (s.max_rss > run.max_rss) { + if (run.skip_oom_steps) { + s.state = .skipped_oom; + } else { + std.debug.print("{s}{s}: this step declares an upper bound of {d} bytes of memory, exceeding the available {d} bytes of memory\n", .{ + s.owner.dep_prefix, s.name, s.max_rss, run.max_rss, + }); + any_problems = true; + } + } + } + if (any_problems) { + if (run.max_rss_is_default) { + std.debug.print("note: use --maxrss to override the default", .{}); + } + return error.UncleanExit; + } + } + + var thread_pool: std.Thread.Pool = undefined; + try thread_pool.init(thread_pool_options); + defer thread_pool.deinit(); + + { + defer parent_prog_node.end(); + + var step_prog = parent_prog_node.start("steps", step_stack.count()); + defer step_prog.end(); + + var wait_group: std.Thread.WaitGroup = .{}; + defer wait_group.wait(); + + // Here we spawn the initial set of tasks with a nice heuristic - + // dependency order. Each worker when it finishes a step will then + // check whether it should run any dependants. + const steps_slice = step_stack.keys(); + for (0..steps_slice.len) |i| { + const step = steps_slice[steps_slice.len - i - 1]; + if (step.state == .skipped_oom) continue; + + wait_group.start(); + thread_pool.spawn(workerMakeOneStep, .{ + &wait_group, &thread_pool, b, step, &step_prog, run, + }) catch @panic("OOM"); + } + } + assert(run.memory_blocked_steps.items.len == 0); + + var test_skip_count: usize = 0; + var test_fail_count: usize = 0; + var test_pass_count: usize = 0; + var test_leak_count: usize = 0; + var test_count: usize = 0; + + var success_count: usize = 0; + var skipped_count: usize = 0; + var failure_count: usize = 0; + var pending_count: usize = 0; + var total_compile_errors: usize = 0; + var compile_error_steps: std.ArrayListUnmanaged(*Step) = .{}; + defer compile_error_steps.deinit(gpa); + + for (step_stack.keys()) |s| { + test_fail_count += s.test_results.fail_count; + test_skip_count += s.test_results.skip_count; + test_leak_count += s.test_results.leak_count; + test_pass_count += s.test_results.passCount(); + test_count += s.test_results.test_count; + + switch (s.state) { + .precheck_unstarted => unreachable, + .precheck_started => unreachable, + .running => unreachable, + .precheck_done => { + // precheck_done is equivalent to dependency_failure in the case of + // transitive dependencies. For example: + // A -> B -> C (failure) + // B will be marked as dependency_failure, while A may never be queued, and thus + // remain in the initial state of precheck_done. + s.state = .dependency_failure; + pending_count += 1; + }, + .dependency_failure => pending_count += 1, + .success => success_count += 1, + .skipped, .skipped_oom => skipped_count += 1, + .failure => { + failure_count += 1; + const compile_errors_len = s.result_error_bundle.errorMessageCount(); + if (compile_errors_len > 0) { + total_compile_errors += compile_errors_len; + try compile_error_steps.append(gpa, s); + } + }, + } + } + + // A proper command line application defaults to silently succeeding. + // The user may request verbose mode if they have a different preference. + if (failure_count == 0 and run.summary != Summary.all) return cleanExit(); + + const ttyconf = run.ttyconf; + const stderr = run.stderr; + + if (run.summary != Summary.none) { + const total_count = success_count + failure_count + pending_count + skipped_count; + ttyconf.setColor(stderr, .cyan) catch {}; + stderr.writeAll("Build Summary:") catch {}; + ttyconf.setColor(stderr, .reset) catch {}; + stderr.writer().print(" {d}/{d} steps succeeded", .{ success_count, total_count }) catch {}; + if (skipped_count > 0) stderr.writer().print("; {d} skipped", .{skipped_count}) catch {}; + if (failure_count > 0) stderr.writer().print("; {d} failed", .{failure_count}) catch {}; + + if (test_count > 0) stderr.writer().print("; {d}/{d} tests passed", .{ test_pass_count, test_count }) catch {}; + if (test_skip_count > 0) stderr.writer().print("; {d} skipped", .{test_skip_count}) catch {}; + if (test_fail_count > 0) stderr.writer().print("; {d} failed", .{test_fail_count}) catch {}; + if (test_leak_count > 0) stderr.writer().print("; {d} leaked", .{test_leak_count}) catch {}; + + if (run.summary == null) { + ttyconf.setColor(stderr, .dim) catch {}; + stderr.writeAll(" (disable with --summary none)") catch {}; + ttyconf.setColor(stderr, .reset) catch {}; + } + stderr.writeAll("\n") catch {}; + const failures_only = run.summary != Summary.all; + + // Print a fancy tree with build results. + var print_node: PrintNode = .{ .parent = null }; + if (step_names.len == 0) { + print_node.last = true; + printTreeStep(b, b.default_step, run, stderr, ttyconf, &print_node, &step_stack, failures_only) catch {}; + } else { + const last_index = if (!failures_only) b.top_level_steps.count() else blk: { + var i: usize = step_names.len; + while (i > 0) { + i -= 1; + if (b.top_level_steps.get(step_names[i]).?.step.state != .success) break :blk i; + } + break :blk b.top_level_steps.count(); + }; + for (step_names, 0..) |step_name, i| { + const tls = b.top_level_steps.get(step_name).?; + print_node.last = i + 1 == last_index; + printTreeStep(b, &tls.step, run, stderr, ttyconf, &print_node, &step_stack, failures_only) catch {}; + } + } + } + + if (failure_count == 0) return cleanExit(); + + // Finally, render compile errors at the bottom of the terminal. + // We use a separate compile_error_steps array list because step_stack is destructively + // mutated in printTreeStep above. + if (run.prominent_compile_errors and total_compile_errors > 0) { + for (compile_error_steps.items) |s| { + if (s.result_error_bundle.errorMessageCount() > 0) { + s.result_error_bundle.renderToStdErr(renderOptions(ttyconf)); + } + } + + // Signal to parent process that we have printed compile errors. The + // parent process may choose to omit the "following command failed" + // line in this case. + process.exit(2); + } + + process.exit(1); +} + +const PrintNode = struct { + parent: ?*PrintNode, + last: bool = false, +}; + +fn printPrefix(node: *PrintNode, stderr: File, ttyconf: std.io.tty.Config) !void { + const parent = node.parent orelse return; + if (parent.parent == null) return; + try printPrefix(parent, stderr, ttyconf); + if (parent.last) { + try stderr.writeAll(" "); + } else { + try stderr.writeAll(switch (ttyconf) { + .no_color, .windows_api => "| ", + .escape_codes => "\x1B\x28\x30\x78\x1B\x28\x42 ", // │ + }); + } +} + +fn printChildNodePrefix(stderr: File, ttyconf: std.io.tty.Config) !void { + try stderr.writeAll(switch (ttyconf) { + .no_color, .windows_api => "+- ", + .escape_codes => "\x1B\x28\x30\x6d\x71\x1B\x28\x42 ", // └─ + }); +} + +fn printStepStatus( + s: *Step, + stderr: File, + ttyconf: std.io.tty.Config, + run: *const Run, +) !void { + switch (s.state) { + .precheck_unstarted => unreachable, + .precheck_started => unreachable, + .precheck_done => unreachable, + .running => unreachable, + + .dependency_failure => { + try ttyconf.setColor(stderr, .dim); + try stderr.writeAll(" transitive failure\n"); + try ttyconf.setColor(stderr, .reset); + }, + + .success => { + try ttyconf.setColor(stderr, .green); + if (s.result_cached) { + try stderr.writeAll(" cached"); + } else if (s.test_results.test_count > 0) { + const pass_count = s.test_results.passCount(); + try stderr.writer().print(" {d} passed", .{pass_count}); + if (s.test_results.skip_count > 0) { + try ttyconf.setColor(stderr, .yellow); + try stderr.writer().print(" {d} skipped", .{s.test_results.skip_count}); + } + } else { + try stderr.writeAll(" success"); + } + try ttyconf.setColor(stderr, .reset); + if (s.result_duration_ns) |ns| { + try ttyconf.setColor(stderr, .dim); + if (ns >= std.time.ns_per_min) { + try stderr.writer().print(" {d}m", .{ns / std.time.ns_per_min}); + } else if (ns >= std.time.ns_per_s) { + try stderr.writer().print(" {d}s", .{ns / std.time.ns_per_s}); + } else if (ns >= std.time.ns_per_ms) { + try stderr.writer().print(" {d}ms", .{ns / std.time.ns_per_ms}); + } else if (ns >= std.time.ns_per_us) { + try stderr.writer().print(" {d}us", .{ns / std.time.ns_per_us}); + } else { + try stderr.writer().print(" {d}ns", .{ns}); + } + try ttyconf.setColor(stderr, .reset); + } + if (s.result_peak_rss != 0) { + const rss = s.result_peak_rss; + try ttyconf.setColor(stderr, .dim); + if (rss >= 1000_000_000) { + try stderr.writer().print(" MaxRSS:{d}G", .{rss / 1000_000_000}); + } else if (rss >= 1000_000) { + try stderr.writer().print(" MaxRSS:{d}M", .{rss / 1000_000}); + } else if (rss >= 1000) { + try stderr.writer().print(" MaxRSS:{d}K", .{rss / 1000}); + } else { + try stderr.writer().print(" MaxRSS:{d}B", .{rss}); + } + try ttyconf.setColor(stderr, .reset); + } + try stderr.writeAll("\n"); + }, + .skipped, .skipped_oom => |skip| { + try ttyconf.setColor(stderr, .yellow); + try stderr.writeAll(" skipped"); + if (skip == .skipped_oom) { + try stderr.writeAll(" (not enough memory)"); + try ttyconf.setColor(stderr, .dim); + try stderr.writer().print(" upper bound of {d} exceeded runner limit ({d})", .{ s.max_rss, run.max_rss }); + try ttyconf.setColor(stderr, .yellow); + } + try stderr.writeAll("\n"); + try ttyconf.setColor(stderr, .reset); + }, + .failure => try printStepFailure(s, stderr, ttyconf), + } +} + +fn printStepFailure( + s: *Step, + stderr: File, + ttyconf: std.io.tty.Config, +) !void { + if (s.result_error_bundle.errorMessageCount() > 0) { + try ttyconf.setColor(stderr, .red); + try stderr.writer().print(" {d} errors\n", .{ + s.result_error_bundle.errorMessageCount(), + }); + try ttyconf.setColor(stderr, .reset); + } else if (!s.test_results.isSuccess()) { + try stderr.writer().print(" {d}/{d} passed", .{ + s.test_results.passCount(), s.test_results.test_count, + }); + if (s.test_results.fail_count > 0) { + try stderr.writeAll(", "); + try ttyconf.setColor(stderr, .red); + try stderr.writer().print("{d} failed", .{ + s.test_results.fail_count, + }); + try ttyconf.setColor(stderr, .reset); + } + if (s.test_results.skip_count > 0) { + try stderr.writeAll(", "); + try ttyconf.setColor(stderr, .yellow); + try stderr.writer().print("{d} skipped", .{ + s.test_results.skip_count, + }); + try ttyconf.setColor(stderr, .reset); + } + if (s.test_results.leak_count > 0) { + try stderr.writeAll(", "); + try ttyconf.setColor(stderr, .red); + try stderr.writer().print("{d} leaked", .{ + s.test_results.leak_count, + }); + try ttyconf.setColor(stderr, .reset); + } + try stderr.writeAll("\n"); + } else if (s.result_error_msgs.items.len > 0) { + try ttyconf.setColor(stderr, .red); + try stderr.writeAll(" failure\n"); + try ttyconf.setColor(stderr, .reset); + } else { + assert(s.result_stderr.len > 0); + try ttyconf.setColor(stderr, .red); + try stderr.writeAll(" stderr\n"); + try ttyconf.setColor(stderr, .reset); + } +} + +fn printTreeStep( + b: *std.Build, + s: *Step, + run: *const Run, + stderr: File, + ttyconf: std.io.tty.Config, + parent_node: *PrintNode, + step_stack: *std.AutoArrayHashMapUnmanaged(*Step, void), + failures_only: bool, +) !void { + const first = step_stack.swapRemove(s); + if (failures_only and s.state == .success) return; + try printPrefix(parent_node, stderr, ttyconf); + + if (!first) try ttyconf.setColor(stderr, .dim); + if (parent_node.parent != null) { + if (parent_node.last) { + try printChildNodePrefix(stderr, ttyconf); + } else { + try stderr.writeAll(switch (ttyconf) { + .no_color, .windows_api => "+- ", + .escape_codes => "\x1B\x28\x30\x74\x71\x1B\x28\x42 ", // ├─ + }); + } + } + + // dep_prefix omitted here because it is redundant with the tree. + try stderr.writeAll(s.name); + + if (first) { + try printStepStatus(s, stderr, ttyconf, run); + + const last_index = if (!failures_only) s.dependencies.items.len -| 1 else blk: { + var i: usize = s.dependencies.items.len; + while (i > 0) { + i -= 1; + if (s.dependencies.items[i].state != .success) break :blk i; + } + break :blk s.dependencies.items.len -| 1; + }; + for (s.dependencies.items, 0..) |dep, i| { + var print_node: PrintNode = .{ + .parent = parent_node, + .last = i == last_index, + }; + try printTreeStep(b, dep, run, stderr, ttyconf, &print_node, step_stack, failures_only); + } + } else { + if (s.dependencies.items.len == 0) { + try stderr.writeAll(" (reused)\n"); + } else { + try stderr.writer().print(" (+{d} more reused dependencies)\n", .{ + s.dependencies.items.len, + }); + } + try ttyconf.setColor(stderr, .reset); + } +} + +/// Traverse the dependency graph depth-first and make it undirected by having +/// steps know their dependants (they only know dependencies at start). +/// Along the way, check that there is no dependency loop, and record the steps +/// in traversal order in `step_stack`. +/// Each step has its dependencies traversed in random order, this accomplishes +/// two things: +/// - `step_stack` will be in randomized-depth-first order, so the build runner +/// spawns steps in a random (but optimized) order +/// - each step's `dependants` list is also filled in a random order, so that +/// when it finishes executing in `workerMakeOneStep`, it spawns next steps +/// to run in random order +fn constructGraphAndCheckForDependencyLoop( + b: *std.Build, + s: *Step, + step_stack: *std.AutoArrayHashMapUnmanaged(*Step, void), + rand: std.Random, +) !void { + switch (s.state) { + .precheck_started => { + std.debug.print("dependency loop detected:\n {s}\n", .{s.name}); + return error.DependencyLoopDetected; + }, + .precheck_unstarted => { + s.state = .precheck_started; + + try step_stack.ensureUnusedCapacity(b.allocator, s.dependencies.items.len); + + // We dupe to avoid shuffling the steps in the summary, it depends + // on s.dependencies' order. + const deps = b.allocator.dupe(*Step, s.dependencies.items) catch @panic("OOM"); + rand.shuffle(*Step, deps); + + for (deps) |dep| { + try step_stack.put(b.allocator, dep, {}); + try dep.dependants.append(b.allocator, s); + constructGraphAndCheckForDependencyLoop(b, dep, step_stack, rand) catch |err| { + if (err == error.DependencyLoopDetected) { + std.debug.print(" {s}\n", .{s.name}); + } + return err; + }; + } + + s.state = .precheck_done; + }, + .precheck_done => {}, + + // These don't happen until we actually run the step graph. + .dependency_failure => unreachable, + .running => unreachable, + .success => unreachable, + .failure => unreachable, + .skipped => unreachable, + .skipped_oom => unreachable, + } +} + +fn workerMakeOneStep( + wg: *std.Thread.WaitGroup, + thread_pool: *std.Thread.Pool, + b: *std.Build, + s: *Step, + prog_node: *std.Progress.Node, + run: *Run, +) void { + defer wg.finish(); + + // First, check the conditions for running this step. If they are not met, + // then we return without doing the step, relying on another worker to + // queue this step up again when dependencies are met. + for (s.dependencies.items) |dep| { + switch (@atomicLoad(Step.State, &dep.state, .SeqCst)) { + .success, .skipped => continue, + .failure, .dependency_failure, .skipped_oom => { + @atomicStore(Step.State, &s.state, .dependency_failure, .SeqCst); + return; + }, + .precheck_done, .running => { + // dependency is not finished yet. + return; + }, + .precheck_unstarted => unreachable, + .precheck_started => unreachable, + } + } + + if (s.max_rss != 0) { + run.max_rss_mutex.lock(); + defer run.max_rss_mutex.unlock(); + + // Avoid running steps twice. + if (s.state != .precheck_done) { + // Another worker got the job. + return; + } + + const new_claimed_rss = run.claimed_rss + s.max_rss; + if (new_claimed_rss > run.max_rss) { + // Running this step right now could possibly exceed the allotted RSS. + // Add this step to the queue of memory-blocked steps. + run.memory_blocked_steps.append(s) catch @panic("OOM"); + return; + } + + run.claimed_rss = new_claimed_rss; + s.state = .running; + } else { + // Avoid running steps twice. + if (@cmpxchgStrong(Step.State, &s.state, .precheck_done, .running, .SeqCst, .SeqCst) != null) { + // Another worker got the job. + return; + } + } + + var sub_prog_node = prog_node.start(s.name, 0); + sub_prog_node.activate(); + defer sub_prog_node.end(); + + const make_result = s.make(&sub_prog_node); + + // No matter the result, we want to display error/warning messages. + const show_compile_errors = !run.prominent_compile_errors and + s.result_error_bundle.errorMessageCount() > 0; + const show_error_msgs = s.result_error_msgs.items.len > 0; + const show_stderr = s.result_stderr.len > 0; + + if (show_error_msgs or show_compile_errors or show_stderr) { + sub_prog_node.context.lock_stderr(); + defer sub_prog_node.context.unlock_stderr(); + + printErrorMessages(b, s, run) catch {}; + } + + handle_result: { + if (make_result) |_| { + @atomicStore(Step.State, &s.state, .success, .SeqCst); + } else |err| switch (err) { + error.MakeFailed => { + @atomicStore(Step.State, &s.state, .failure, .SeqCst); + break :handle_result; + }, + error.MakeSkipped => @atomicStore(Step.State, &s.state, .skipped, .SeqCst), + } + + // Successful completion of a step, so we queue up its dependants as well. + for (s.dependants.items) |dep| { + wg.start(); + thread_pool.spawn(workerMakeOneStep, .{ + wg, thread_pool, b, dep, prog_node, run, + }) catch @panic("OOM"); + } + } + + // If this is a step that claims resources, we must now queue up other + // steps that are waiting for resources. + if (s.max_rss != 0) { + run.max_rss_mutex.lock(); + defer run.max_rss_mutex.unlock(); + + // Give the memory back to the scheduler. + run.claimed_rss -= s.max_rss; + // Avoid kicking off too many tasks that we already know will not have + // enough resources. + var remaining = run.max_rss - run.claimed_rss; + var i: usize = 0; + var j: usize = 0; + while (j < run.memory_blocked_steps.items.len) : (j += 1) { + const dep = run.memory_blocked_steps.items[j]; + assert(dep.max_rss != 0); + if (dep.max_rss <= remaining) { + remaining -= dep.max_rss; + + wg.start(); + thread_pool.spawn(workerMakeOneStep, .{ + wg, thread_pool, b, dep, prog_node, run, + }) catch @panic("OOM"); + } else { + run.memory_blocked_steps.items[i] = dep; + i += 1; + } + } + run.memory_blocked_steps.shrinkRetainingCapacity(i); + } +} + +fn printErrorMessages(b: *std.Build, failing_step: *Step, run: *const Run) !void { + const gpa = b.allocator; + const stderr = run.stderr; + const ttyconf = run.ttyconf; + + // Provide context for where these error messages are coming from by + // printing the corresponding Step subtree. + + var step_stack: std.ArrayListUnmanaged(*Step) = .{}; + defer step_stack.deinit(gpa); + try step_stack.append(gpa, failing_step); + while (step_stack.items[step_stack.items.len - 1].dependants.items.len != 0) { + try step_stack.append(gpa, step_stack.items[step_stack.items.len - 1].dependants.items[0]); + } + + // Now, `step_stack` has the subtree that we want to print, in reverse order. + try ttyconf.setColor(stderr, .dim); + var indent: usize = 0; + while (step_stack.popOrNull()) |s| : (indent += 1) { + if (indent > 0) { + try stderr.writer().writeByteNTimes(' ', (indent - 1) * 3); + try printChildNodePrefix(stderr, ttyconf); + } + + try stderr.writeAll(s.name); + + if (s == failing_step) { + try printStepFailure(s, stderr, ttyconf); + } else { + try stderr.writeAll("\n"); + } + } + try ttyconf.setColor(stderr, .reset); + + if (failing_step.result_stderr.len > 0) { + try stderr.writeAll(failing_step.result_stderr); + if (!mem.endsWith(u8, failing_step.result_stderr, "\n")) { + try stderr.writeAll("\n"); + } + } + + if (!run.prominent_compile_errors and failing_step.result_error_bundle.errorMessageCount() > 0) + try failing_step.result_error_bundle.renderToWriter(renderOptions(ttyconf), stderr.writer()); + + for (failing_step.result_error_msgs.items) |msg| { + try ttyconf.setColor(stderr, .red); + try stderr.writeAll("error: "); + try ttyconf.setColor(stderr, .reset); + try stderr.writeAll(msg); + try stderr.writeAll("\n"); + } +} + +fn steps(builder: *std.Build, out_stream: anytype) !void { + const allocator = builder.allocator; + for (builder.top_level_steps.values()) |top_level_step| { + const name = if (&top_level_step.step == builder.default_step) + try fmt.allocPrint(allocator, "{s} (default)", .{top_level_step.step.name}) + else + top_level_step.step.name; + try out_stream.print(" {s:<28} {s}\n", .{ name, top_level_step.description }); + } +} + +fn usage(b: *std.Build, out_stream: anytype) !void { + try out_stream.print( + \\Usage: {s} build [steps] [options] + \\ + \\Steps: + \\ + , .{b.graph.zig_exe}); + try steps(b, out_stream); + + try out_stream.writeAll( + \\ + \\General Options: + \\ -p, --prefix [path] Where to install files (default: zig-out) + \\ --prefix-lib-dir [path] Where to install libraries + \\ --prefix-exe-dir [path] Where to install executables + \\ --prefix-include-dir [path] Where to install C header files + \\ + \\ --release[=mode] Request release mode, optionally specifying a + \\ preferred optimization mode: fast, safe, small + \\ + \\ -fdarling, -fno-darling Integration with system-installed Darling to + \\ execute macOS programs on Linux hosts + \\ (default: no) + \\ -fqemu, -fno-qemu Integration with system-installed QEMU to execute + \\ foreign-architecture programs on Linux hosts + \\ (default: no) + \\ --glibc-runtimes [path] Enhances QEMU integration by providing glibc built + \\ for multiple foreign architectures, allowing + \\ execution of non-native programs that link with glibc. + \\ -frosetta, -fno-rosetta Rely on Rosetta to execute x86_64 programs on + \\ ARM64 macOS hosts. (default: no) + \\ -fwasmtime, -fno-wasmtime Integration with system-installed wasmtime to + \\ execute WASI binaries. (default: no) + \\ -fwine, -fno-wine Integration with system-installed Wine to execute + \\ Windows programs on Linux hosts. (default: no) + \\ + \\ -h, --help Print this help and exit + \\ -l, --list-steps Print available steps + \\ --verbose Print commands before executing them + \\ --color [auto|off|on] Enable or disable colored error messages + \\ --prominent-compile-errors Buffer compile errors and display at end + \\ --summary [mode] Control the printing of the build summary + \\ all Print the build summary in its entirety + \\ failures (Default) Only print failed steps + \\ none Do not print the build summary + \\ -j Limit concurrent jobs (default is to use all CPU cores) + \\ --maxrss Limit memory usage (default is to use available memory) + \\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss + \\ --fetch Exit after fetching dependency tree + \\ + \\Project-Specific Options: + \\ + ); + + const arena = b.allocator; + if (b.available_options_list.items.len == 0) { + try out_stream.print(" (none)\n", .{}); + } else { + for (b.available_options_list.items) |option| { + const name = try fmt.allocPrint(arena, " -D{s}=[{s}]", .{ + option.name, + @tagName(option.type_id), + }); + try out_stream.print("{s:<30} {s}\n", .{ name, option.description }); + if (option.enum_options) |enum_options| { + const padding = " " ** 33; + try out_stream.writeAll(padding ++ "Supported Values:\n"); + for (enum_options) |enum_option| { + try out_stream.print(padding ++ " {s}\n", .{enum_option}); + } + } + } + } + + try out_stream.writeAll( + \\ + \\System Integration Options: + \\ --search-prefix [path] Add a path to look for binaries, libraries, headers + \\ --sysroot [path] Set the system root directory (usually /) + \\ --libc [file] Provide a file which specifies libc paths + \\ + \\ --host-target [triple] Use the provided target as the host + \\ --host-cpu [cpu] Use the provided CPU as the host + \\ --host-dynamic-linker [path] Use the provided dynamic linker as the host + \\ + \\ --system [pkgdir] Disable package fetching; enable all integrations + \\ -fsys=[name] Enable a system integration + \\ -fno-sys=[name] Disable a system integration + \\ + \\ Available System Integrations: Enabled: + \\ + ); + if (b.graph.system_library_options.entries.len == 0) { + try out_stream.writeAll(" (none) -\n"); + } else { + for (b.graph.system_library_options.keys(), b.graph.system_library_options.values()) |k, v| { + const status = switch (v) { + .declared_enabled => "yes", + .declared_disabled => "no", + .user_enabled, .user_disabled => unreachable, // already emitted error + }; + try out_stream.print(" {s:<43} {s}\n", .{ k, status }); + } + } + + try out_stream.writeAll( + \\ + \\Advanced Options: + \\ -freference-trace[=num] How many lines of reference trace should be shown per compile error + \\ -fno-reference-trace Disable reference trace + \\ --build-file [file] Override path to build.zig + \\ --cache-dir [path] Override path to local Zig cache directory + \\ --global-cache-dir [path] Override path to global Zig cache directory + \\ --zig-lib-dir [arg] Override path to Zig lib directory + \\ --build-runner [file] Override path to build runner + \\ --seed [integer] For shuffling dependency traversal order (default: random) + \\ --debug-log [scope] Enable debugging the compiler + \\ --debug-pkg-config Fail if unknown pkg-config flags encountered + \\ --verbose-link Enable compiler debug output for linking + \\ --verbose-air Enable compiler debug output for Zig AIR + \\ --verbose-llvm-ir[=file] Enable compiler debug output for LLVM IR + \\ --verbose-llvm-bc=[file] Enable compiler debug output for LLVM BC + \\ --verbose-cimport Enable compiler debug output for C imports + \\ --verbose-cc Enable compiler debug output for C compilation + \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features + \\ + ); +} + +fn nextArg(args: [][:0]const u8, idx: *usize) ?[:0]const u8 { + if (idx.* >= args.len) return null; + defer idx.* += 1; + return args[idx.*]; +} + +fn nextArgOrFatal(args: [][:0]const u8, idx: *usize) [:0]const u8 { + return nextArg(args, idx) orelse { + std.debug.print("expected argument after '{s}'\n access the help menu with 'zig build -h'\n", .{args[idx.*]}); + process.exit(1); + }; +} + +fn argsRest(args: [][:0]const u8, idx: usize) ?[][:0]const u8 { + if (idx >= args.len) return null; + return args[idx..]; +} + +fn cleanExit() void { + // Perhaps in the future there could be an Advanced Options flag such as + // --debug-build-runner-leaks which would make this function return instead + // of calling exit. + process.exit(0); +} + +const Color = enum { auto, off, on }; +const Summary = enum { all, failures, none }; + +fn get_tty_conf(color: Color, stderr: File) std.io.tty.Config { + return switch (color) { + .auto => std.io.tty.detectConfig(stderr), + .on => .escape_codes, + .off => .no_color, + }; +} + +fn renderOptions(ttyconf: std.io.tty.Config) std.zig.ErrorBundle.RenderOptions { + return .{ + .ttyconf = ttyconf, + .include_source_line = ttyconf != .no_color, + .include_reference_trace = ttyconf != .no_color, + }; +} + +fn fatalWithHint(comptime f: []const u8, args: anytype) noreturn { + std.debug.print(f ++ "\n access the help menu with 'zig build -h'\n", args); + process.exit(1); +} + +fn fatal(comptime f: []const u8, args: anytype) noreturn { + std.debug.print(f ++ "\n", args); + process.exit(1); +} + +fn validateSystemLibraryOptions(b: *std.Build) void { + var bad = false; + for (b.graph.system_library_options.keys(), b.graph.system_library_options.values()) |k, v| { + switch (v) { + .user_disabled, .user_enabled => { + // The user tried to enable or disable a system library integration, but + // the build script did not recognize that option. + std.debug.print("system library name not recognized by build script: '{s}'\n", .{k}); + bad = true; + }, + .declared_disabled, .declared_enabled => {}, + } + } + if (bad) { + std.debug.print(" access the help menu with 'zig build -h'\n", .{}); + process.exit(1); + } +} diff --git a/lib/compiler/fmt.zig b/lib/compiler/fmt.zig new file mode 100644 index 0000000000000000000000000000000000000000..2fc04b7935a76e7a2f9acbc3e64e2ca81448a219 --- /dev/null +++ b/lib/compiler/fmt.zig @@ -0,0 +1,342 @@ +const std = @import("std"); +const mem = std.mem; +const fs = std.fs; +const process = std.process; +const Allocator = std.mem.Allocator; +const warn = std.log.warn; +const Color = std.zig.Color; + +const usage_fmt = + \\Usage: zig fmt [file]... + \\ + \\ Formats the input files and modifies them in-place. + \\ Arguments can be files or directories, which are searched + \\ recursively. + \\ + \\Options: + \\ -h, --help Print this help and exit + \\ --color [auto|off|on] Enable or disable colored error messages + \\ --stdin Format code from stdin; output to stdout + \\ --check List non-conforming files and exit with an error + \\ if the list is non-empty + \\ --ast-check Run zig ast-check on every file + \\ --exclude [file] Exclude file or directory from formatting + \\ + \\ +; + +const Fmt = struct { + seen: SeenMap, + any_error: bool, + check_ast: bool, + color: Color, + gpa: Allocator, + arena: Allocator, + out_buffer: std.ArrayList(u8), + + const SeenMap = std.AutoHashMap(fs.File.INode, void); +}; + +pub fn main() !void { + var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena_instance.deinit(); + const arena = arena_instance.allocator(); + const gpa = arena; + + const args = try process.argsAlloc(arena); + + var color: Color = .auto; + var stdin_flag: bool = false; + var check_flag: bool = false; + var check_ast_flag: bool = false; + var input_files = std.ArrayList([]const u8).init(gpa); + defer input_files.deinit(); + var excluded_files = std.ArrayList([]const u8).init(gpa); + defer excluded_files.deinit(); + + { + var i: usize = 1; + while (i < args.len) : (i += 1) { + const arg = args[i]; + if (mem.startsWith(u8, arg, "-")) { + if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { + const stdout = std.io.getStdOut().writer(); + try stdout.writeAll(usage_fmt); + return process.cleanExit(); + } else if (mem.eql(u8, arg, "--color")) { + if (i + 1 >= args.len) { + fatal("expected [auto|on|off] after --color", .{}); + } + i += 1; + const next_arg = args[i]; + color = std.meta.stringToEnum(Color, next_arg) orelse { + fatal("expected [auto|on|off] after --color, found '{s}'", .{next_arg}); + }; + } else if (mem.eql(u8, arg, "--stdin")) { + stdin_flag = true; + } else if (mem.eql(u8, arg, "--check")) { + check_flag = true; + } else if (mem.eql(u8, arg, "--ast-check")) { + check_ast_flag = true; + } else if (mem.eql(u8, arg, "--exclude")) { + if (i + 1 >= args.len) { + fatal("expected parameter after --exclude", .{}); + } + i += 1; + const next_arg = args[i]; + try excluded_files.append(next_arg); + } else { + fatal("unrecognized parameter: '{s}'", .{arg}); + } + } else { + try input_files.append(arg); + } + } + } + + if (stdin_flag) { + if (input_files.items.len != 0) { + fatal("cannot use --stdin with positional arguments", .{}); + } + + const stdin = std.io.getStdIn(); + const source_code = std.zig.readSourceFileToEndAlloc(gpa, stdin, null) catch |err| { + fatal("unable to read stdin: {}", .{err}); + }; + defer gpa.free(source_code); + + var tree = std.zig.Ast.parse(gpa, source_code, .zig) catch |err| { + fatal("error parsing stdin: {}", .{err}); + }; + defer tree.deinit(gpa); + + if (check_ast_flag) { + var zir = try std.zig.AstGen.generate(gpa, tree); + + if (zir.hasCompileErrors()) { + var wip_errors: std.zig.ErrorBundle.Wip = undefined; + try wip_errors.init(gpa); + defer wip_errors.deinit(); + try wip_errors.addZirErrorMessages(zir, tree, source_code, ""); + var error_bundle = try wip_errors.toOwnedBundle(""); + defer error_bundle.deinit(gpa); + error_bundle.renderToStdErr(color.renderOptions()); + process.exit(2); + } + } else if (tree.errors.len != 0) { + try std.zig.printAstErrorsToStderr(gpa, tree, "", color); + process.exit(2); + } + const formatted = try tree.render(gpa); + defer gpa.free(formatted); + + if (check_flag) { + const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code)); + process.exit(code); + } + + return std.io.getStdOut().writeAll(formatted); + } + + if (input_files.items.len == 0) { + fatal("expected at least one source file argument", .{}); + } + + var fmt = Fmt{ + .gpa = gpa, + .arena = arena, + .seen = Fmt.SeenMap.init(gpa), + .any_error = false, + .check_ast = check_ast_flag, + .color = color, + .out_buffer = std.ArrayList(u8).init(gpa), + }; + defer fmt.seen.deinit(); + defer fmt.out_buffer.deinit(); + + // Mark any excluded files/directories as already seen, + // so that they are skipped later during actual processing + for (excluded_files.items) |file_path| { + const stat = fs.cwd().statFile(file_path) catch |err| switch (err) { + error.FileNotFound => continue, + // On Windows, statFile does not work for directories + error.IsDir => dir: { + var dir = try fs.cwd().openDir(file_path, .{}); + defer dir.close(); + break :dir try dir.stat(); + }, + else => |e| return e, + }; + try fmt.seen.put(stat.inode, {}); + } + + for (input_files.items) |file_path| { + try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path); + } + if (fmt.any_error) { + process.exit(1); + } +} + +const FmtError = error{ + SystemResources, + OperationAborted, + IoPending, + BrokenPipe, + Unexpected, + WouldBlock, + FileClosed, + DestinationAddressRequired, + DiskQuota, + FileTooBig, + InputOutput, + NoSpaceLeft, + AccessDenied, + OutOfMemory, + RenameAcrossMountPoints, + ReadOnlyFileSystem, + LinkQuotaExceeded, + FileBusy, + EndOfStream, + Unseekable, + NotOpenForWriting, + UnsupportedEncoding, + ConnectionResetByPeer, + SocketNotConnected, + LockViolation, + NetNameDeleted, + InvalidArgument, +} || fs.File.OpenError; + +fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void { + fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) { + error.IsDir, error.AccessDenied => return fmtPathDir(fmt, file_path, check_mode, dir, sub_path), + else => { + warn("unable to format '{s}': {s}", .{ file_path, @errorName(err) }); + fmt.any_error = true; + return; + }, + }; +} + +fn fmtPathDir( + fmt: *Fmt, + file_path: []const u8, + check_mode: bool, + parent_dir: fs.Dir, + parent_sub_path: []const u8, +) FmtError!void { + var dir = try parent_dir.openDir(parent_sub_path, .{ .iterate = true }); + defer dir.close(); + + const stat = try dir.stat(); + if (try fmt.seen.fetchPut(stat.inode, {})) |_| return; + + var dir_it = dir.iterate(); + while (try dir_it.next()) |entry| { + const is_dir = entry.kind == .directory; + + if (is_dir and (mem.eql(u8, entry.name, "zig-cache") or mem.eql(u8, entry.name, "zig-out"))) continue; + + if (is_dir or entry.kind == .file and (mem.endsWith(u8, entry.name, ".zig") or mem.endsWith(u8, entry.name, ".zon"))) { + const full_path = try fs.path.join(fmt.gpa, &[_][]const u8{ file_path, entry.name }); + defer fmt.gpa.free(full_path); + + if (is_dir) { + try fmtPathDir(fmt, full_path, check_mode, dir, entry.name); + } else { + fmtPathFile(fmt, full_path, check_mode, dir, entry.name) catch |err| { + warn("unable to format '{s}': {s}", .{ full_path, @errorName(err) }); + fmt.any_error = true; + return; + }; + } + } + } +} + +fn fmtPathFile( + fmt: *Fmt, + file_path: []const u8, + check_mode: bool, + dir: fs.Dir, + sub_path: []const u8, +) FmtError!void { + const source_file = try dir.openFile(sub_path, .{}); + var file_closed = false; + errdefer if (!file_closed) source_file.close(); + + const stat = try source_file.stat(); + + if (stat.kind == .directory) + return error.IsDir; + + const gpa = fmt.gpa; + const source_code = try std.zig.readSourceFileToEndAlloc( + gpa, + source_file, + std.math.cast(usize, stat.size) orelse return error.FileTooBig, + ); + defer gpa.free(source_code); + + source_file.close(); + file_closed = true; + + // Add to set after no longer possible to get error.IsDir. + if (try fmt.seen.fetchPut(stat.inode, {})) |_| return; + + var tree = try std.zig.Ast.parse(gpa, source_code, .zig); + defer tree.deinit(gpa); + + if (tree.errors.len != 0) { + try std.zig.printAstErrorsToStderr(gpa, tree, file_path, fmt.color); + fmt.any_error = true; + return; + } + + if (fmt.check_ast) { + if (stat.size > std.zig.max_src_size) + return error.FileTooBig; + + var zir = try std.zig.AstGen.generate(gpa, tree); + defer zir.deinit(gpa); + + if (zir.hasCompileErrors()) { + var wip_errors: std.zig.ErrorBundle.Wip = undefined; + try wip_errors.init(gpa); + defer wip_errors.deinit(); + try wip_errors.addZirErrorMessages(zir, tree, source_code, file_path); + var error_bundle = try wip_errors.toOwnedBundle(""); + defer error_bundle.deinit(gpa); + error_bundle.renderToStdErr(fmt.color.renderOptions()); + fmt.any_error = true; + } + } + + // As a heuristic, we make enough capacity for the same as the input source. + fmt.out_buffer.shrinkRetainingCapacity(0); + try fmt.out_buffer.ensureTotalCapacity(source_code.len); + + try tree.renderToArrayList(&fmt.out_buffer, .{}); + if (mem.eql(u8, fmt.out_buffer.items, source_code)) + return; + + if (check_mode) { + const stdout = std.io.getStdOut().writer(); + try stdout.print("{s}\n", .{file_path}); + fmt.any_error = true; + } else { + var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode }); + defer af.deinit(); + + try af.file.writeAll(fmt.out_buffer.items); + try af.finish(); + const stdout = std.io.getStdOut().writer(); + try stdout.print("{s}\n", .{file_path}); + } +} + +fn fatal(comptime format: []const u8, args: anytype) noreturn { + std.log.err(format, args); + process.exit(1); +} diff --git a/lib/compiler/reduce.zig b/lib/compiler/reduce.zig new file mode 100644 index 0000000000000000000000000000000000000000..1b40856ffe557fa77b366a8033778e18922cb064 --- /dev/null +++ b/lib/compiler/reduce.zig @@ -0,0 +1,426 @@ +const std = @import("std"); +const mem = std.mem; +const Allocator = std.mem.Allocator; +const assert = std.debug.assert; +const Ast = std.zig.Ast; +const Walk = @import("reduce/Walk.zig"); +const AstGen = std.zig.AstGen; +const Zir = std.zig.Zir; + +const usage = + \\zig reduce [options] ./checker root_source_file.zig [-- [argv]] + \\ + \\root_source_file.zig is relative to --main-mod-path. + \\ + \\checker: + \\ An executable that communicates interestingness by returning these exit codes: + \\ exit(0): interesting + \\ exit(1): unknown (infinite loop or other mishap) + \\ exit(other): not interesting + \\ + \\options: + \\ --seed [integer] Override the random seed. Defaults to 0 + \\ --skip-smoke-test Skip interestingness check smoke test + \\ --mod [name]:[deps]:[src] Make a module available for dependency under the given name + \\ deps: [dep],[dep],... + \\ dep: [[import=]name] + \\ --deps [dep],[dep],... Set dependency names for the root package + \\ dep: [[import=]name] + \\ --main-mod-path Set the directory of the root module + \\ + \\argv: + \\ Forwarded directly to the interestingness script. + \\ +; + +const Interestingness = enum { interesting, unknown, boring }; + +// Roadmap: +// - add thread pool +// - add support for parsing the module flags +// - more fancy transformations +// - @import inlining of modules +// - removing statements or blocks of code +// - replacing operands of `and` and `or` with `true` and `false` +// - replacing if conditions with `true` and `false` +// - reduce flags sent to the compiler +// - integrate with the build system? + +pub fn main() !void { + var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena_instance.deinit(); + const arena = arena_instance.allocator(); + + var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .{}; + const gpa = general_purpose_allocator.allocator(); + + const args = try std.process.argsAlloc(arena); + + var opt_checker_path: ?[]const u8 = null; + var opt_root_source_file_path: ?[]const u8 = null; + var argv: []const []const u8 = &.{}; + var seed: u32 = 0; + var skip_smoke_test = false; + + { + var i: usize = 1; + while (i < args.len) : (i += 1) { + const arg = args[i]; + if (mem.startsWith(u8, arg, "-")) { + if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { + const stdout = std.io.getStdOut().writer(); + try stdout.writeAll(usage); + return std.process.cleanExit(); + } else if (mem.eql(u8, arg, "--")) { + argv = args[i + 1 ..]; + break; + } else if (mem.eql(u8, arg, "--skip-smoke-test")) { + skip_smoke_test = true; + } else if (mem.eql(u8, arg, "--main-mod-path")) { + @panic("TODO: implement --main-mod-path"); + } else if (mem.eql(u8, arg, "--mod")) { + @panic("TODO: implement --mod"); + } else if (mem.eql(u8, arg, "--deps")) { + @panic("TODO: implement --deps"); + } else if (mem.eql(u8, arg, "--seed")) { + i += 1; + if (i >= args.len) fatal("expected 32-bit integer after {s}", .{arg}); + const next_arg = args[i]; + seed = std.fmt.parseUnsigned(u32, next_arg, 0) catch |err| { + fatal("unable to parse seed '{s}' as 32-bit integer: {s}", .{ + next_arg, @errorName(err), + }); + }; + } else { + fatal("unrecognized parameter: '{s}'", .{arg}); + } + } else if (opt_checker_path == null) { + opt_checker_path = arg; + } else if (opt_root_source_file_path == null) { + opt_root_source_file_path = arg; + } else { + fatal("unexpected extra parameter: '{s}'", .{arg}); + } + } + } + + const checker_path = opt_checker_path orelse + fatal("missing interestingness checker argument; see -h for usage", .{}); + const root_source_file_path = opt_root_source_file_path orelse + fatal("missing root source file path argument; see -h for usage", .{}); + + var interestingness_argv: std.ArrayListUnmanaged([]const u8) = .{}; + try interestingness_argv.ensureUnusedCapacity(arena, argv.len + 1); + interestingness_argv.appendAssumeCapacity(checker_path); + interestingness_argv.appendSliceAssumeCapacity(argv); + + var rendered = std.ArrayList(u8).init(gpa); + defer rendered.deinit(); + + var astgen_input = std.ArrayList(u8).init(gpa); + defer astgen_input.deinit(); + + var tree = try parse(gpa, root_source_file_path); + defer { + gpa.free(tree.source); + tree.deinit(gpa); + } + + if (!skip_smoke_test) { + std.debug.print("smoke testing the interestingness check...\n", .{}); + switch (try runCheck(arena, interestingness_argv.items)) { + .interesting => {}, + .boring, .unknown => |t| { + fatal("interestingness check returned {s} for unmodified input\n", .{ + @tagName(t), + }); + }, + } + } + + var fixups: Ast.Fixups = .{}; + defer fixups.deinit(gpa); + + var more_fixups: Ast.Fixups = .{}; + defer more_fixups.deinit(gpa); + + var rng = std.Random.DefaultPrng.init(seed); + + // 1. Walk the AST of the source file looking for independent + // reductions and collecting them all into an array list. + // 2. Randomize the list of transformations. A future enhancement will add + // priority weights to the sorting but for now they are completely + // shuffled. + // 3. Apply a subset consisting of 1/2 of the transformations and check for + // interestingness. + // 4. If not interesting, half the subset size again and check again. + // 5. Repeat until the subset size is 1, then march the transformation + // index forward by 1 with each non-interesting attempt. + // + // At any point if a subset of transformations succeeds in producing an interesting + // result, restart the whole process, reparsing the AST and re-generating the list + // of all possible transformations and shuffling it again. + + var transformations = std.ArrayList(Walk.Transformation).init(gpa); + defer transformations.deinit(); + try Walk.findTransformations(arena, &tree, &transformations); + sortTransformations(transformations.items, rng.random()); + + fresh: while (transformations.items.len > 0) { + std.debug.print("found {d} possible transformations\n", .{ + transformations.items.len, + }); + var subset_size: usize = transformations.items.len; + var start_index: usize = 0; + + while (start_index < transformations.items.len) { + const prev_subset_size = subset_size; + subset_size = @max(1, subset_size * 3 / 4); + if (prev_subset_size > 1 and subset_size == 1) + start_index = 0; + + const this_set = transformations.items[start_index..][0..subset_size]; + std.debug.print("trying {d} random transformations: ", .{subset_size}); + for (this_set[0..@min(this_set.len, 20)]) |t| { + std.debug.print("{s} ", .{@tagName(t)}); + } + std.debug.print("\n", .{}); + try transformationsToFixups(gpa, arena, root_source_file_path, this_set, &fixups); + + rendered.clearRetainingCapacity(); + try tree.renderToArrayList(&rendered, fixups); + + // The transformations we applied may have resulted in unused locals, + // in which case we would like to add the respective discards. + { + try astgen_input.resize(rendered.items.len); + @memcpy(astgen_input.items, rendered.items); + try astgen_input.append(0); + const source_with_null = astgen_input.items[0 .. astgen_input.items.len - 1 :0]; + var astgen_tree = try Ast.parse(gpa, source_with_null, .zig); + defer astgen_tree.deinit(gpa); + if (astgen_tree.errors.len != 0) { + @panic("syntax errors occurred"); + } + var zir = try AstGen.generate(gpa, astgen_tree); + defer zir.deinit(gpa); + + if (zir.hasCompileErrors()) { + more_fixups.clearRetainingCapacity(); + const payload_index = zir.extra[@intFromEnum(Zir.ExtraIndex.compile_errors)]; + assert(payload_index != 0); + const header = zir.extraData(Zir.Inst.CompileErrors, payload_index); + var extra_index = header.end; + for (0..header.data.items_len) |_| { + const item = zir.extraData(Zir.Inst.CompileErrors.Item, extra_index); + extra_index = item.end; + const msg = zir.nullTerminatedString(item.data.msg); + if (mem.eql(u8, msg, "unused local constant") or + mem.eql(u8, msg, "unused local variable") or + mem.eql(u8, msg, "unused function parameter") or + mem.eql(u8, msg, "unused capture")) + { + const ident_token = item.data.token; + try more_fixups.unused_var_decls.put(gpa, ident_token, {}); + } else { + std.debug.print("found other ZIR error: '{s}'\n", .{msg}); + } + } + if (more_fixups.count() != 0) { + rendered.clearRetainingCapacity(); + try astgen_tree.renderToArrayList(&rendered, more_fixups); + } + } + } + + try std.fs.cwd().writeFile(root_source_file_path, rendered.items); + // std.debug.print("trying this code:\n{s}\n", .{rendered.items}); + + const interestingness = try runCheck(arena, interestingness_argv.items); + std.debug.print("{d} random transformations: {s}. {d}/{d}\n", .{ + subset_size, @tagName(interestingness), start_index, transformations.items.len, + }); + switch (interestingness) { + .interesting => { + const new_tree = try parse(gpa, root_source_file_path); + gpa.free(tree.source); + tree.deinit(gpa); + tree = new_tree; + + try Walk.findTransformations(arena, &tree, &transformations); + sortTransformations(transformations.items, rng.random()); + + continue :fresh; + }, + .unknown, .boring => { + // Continue to try the next set of transformations. + // If we tested only one transformation, move on to the next one. + if (subset_size == 1) { + start_index += 1; + } else { + start_index += subset_size; + if (start_index + subset_size > transformations.items.len) { + start_index = 0; + } + } + }, + } + } + std.debug.print("all {d} remaining transformations are uninteresting\n", .{ + transformations.items.len, + }); + + // Revert the source back to not be transformed. + fixups.clearRetainingCapacity(); + rendered.clearRetainingCapacity(); + try tree.renderToArrayList(&rendered, fixups); + try std.fs.cwd().writeFile(root_source_file_path, rendered.items); + + return std.process.cleanExit(); + } + std.debug.print("no more transformations found\n", .{}); + return std.process.cleanExit(); +} + +fn sortTransformations(transformations: []Walk.Transformation, rng: std.Random) void { + rng.shuffle(Walk.Transformation, transformations); + // Stable sort based on priority to keep randomness as the secondary sort. + // TODO: introduce transformation priorities + // std.mem.sort(transformations); +} + +fn termToInteresting(term: std.process.Child.Term) Interestingness { + return switch (term) { + .Exited => |code| switch (code) { + 0 => .interesting, + 1 => .unknown, + else => .boring, + }, + else => b: { + std.debug.print("interestingness check aborted unexpectedly\n", .{}); + break :b .boring; + }, + }; +} + +fn runCheck(arena: std.mem.Allocator, argv: []const []const u8) !Interestingness { + const result = try std.process.Child.run(.{ + .allocator = arena, + .argv = argv, + }); + if (result.stderr.len != 0) + std.debug.print("{s}", .{result.stderr}); + return termToInteresting(result.term); +} + +fn transformationsToFixups( + gpa: Allocator, + arena: Allocator, + root_source_file_path: []const u8, + transforms: []const Walk.Transformation, + fixups: *Ast.Fixups, +) !void { + fixups.clearRetainingCapacity(); + + for (transforms) |t| switch (t) { + .gut_function => |fn_decl_node| { + try fixups.gut_functions.put(gpa, fn_decl_node, {}); + }, + .delete_node => |decl_node| { + try fixups.omit_nodes.put(gpa, decl_node, {}); + }, + .delete_var_decl => |delete_var_decl| { + try fixups.omit_nodes.put(gpa, delete_var_decl.var_decl_node, {}); + for (delete_var_decl.references.items) |ident_node| { + try fixups.replace_nodes_with_string.put(gpa, ident_node, "undefined"); + } + }, + .replace_with_undef => |node| { + try fixups.replace_nodes_with_string.put(gpa, node, "undefined"); + }, + .replace_with_true => |node| { + try fixups.replace_nodes_with_string.put(gpa, node, "true"); + }, + .replace_with_false => |node| { + try fixups.replace_nodes_with_string.put(gpa, node, "false"); + }, + .replace_node => |r| { + try fixups.replace_nodes_with_node.put(gpa, r.to_replace, r.replacement); + }, + .inline_imported_file => |inline_imported_file| { + const full_imported_path = try std.fs.path.join(gpa, &.{ + std.fs.path.dirname(root_source_file_path) orelse ".", + inline_imported_file.imported_string, + }); + defer gpa.free(full_imported_path); + var other_file_ast = try parse(gpa, full_imported_path); + defer { + gpa.free(other_file_ast.source); + other_file_ast.deinit(gpa); + } + + var inlined_fixups: Ast.Fixups = .{}; + defer inlined_fixups.deinit(gpa); + if (std.fs.path.dirname(inline_imported_file.imported_string)) |dirname| { + inlined_fixups.rebase_imported_paths = dirname; + } + for (inline_imported_file.in_scope_names.keys()) |name| { + // This name needs to be mangled in order to not cause an + // ambiguous reference error. + var i: u32 = 2; + const mangled = while (true) : (i += 1) { + const mangled = try std.fmt.allocPrint(gpa, "{s}{d}", .{ name, i }); + if (!inline_imported_file.in_scope_names.contains(mangled)) + break mangled; + gpa.free(mangled); + }; + try inlined_fixups.rename_identifiers.put(gpa, name, mangled); + } + defer { + for (inlined_fixups.rename_identifiers.values()) |v| { + gpa.free(v); + } + } + + var other_source = std.ArrayList(u8).init(gpa); + defer other_source.deinit(); + try other_source.appendSlice("struct {\n"); + try other_file_ast.renderToArrayList(&other_source, inlined_fixups); + try other_source.appendSlice("}"); + + try fixups.replace_nodes_with_string.put( + gpa, + inline_imported_file.builtin_call_node, + try arena.dupe(u8, other_source.items), + ); + }, + }; +} + +fn parse(gpa: Allocator, file_path: []const u8) !Ast { + const source_code = std.fs.cwd().readFileAllocOptions( + gpa, + file_path, + std.math.maxInt(u32), + null, + 1, + 0, + ) catch |err| { + fatal("unable to open '{s}': {s}", .{ file_path, @errorName(err) }); + }; + errdefer gpa.free(source_code); + + var tree = try Ast.parse(gpa, source_code, .zig); + errdefer tree.deinit(gpa); + + if (tree.errors.len != 0) { + @panic("syntax errors occurred"); + } + + return tree; +} + +fn fatal(comptime format: []const u8, args: anytype) noreturn { + std.log.err(format, args); + std.process.exit(1); +} diff --git a/lib/compiler/reduce/Walk.zig b/lib/compiler/reduce/Walk.zig new file mode 100644 index 0000000000000000000000000000000000000000..572243d82970c3dc3cfe5a1f26db279a13bba559 --- /dev/null +++ b/lib/compiler/reduce/Walk.zig @@ -0,0 +1,1102 @@ +const std = @import("std"); +const Ast = std.zig.Ast; +const Walk = @This(); +const assert = std.debug.assert; +const BuiltinFn = std.zig.BuiltinFn; + +ast: *const Ast, +transformations: *std.ArrayList(Transformation), +unreferenced_globals: std.StringArrayHashMapUnmanaged(Ast.Node.Index), +in_scope_names: std.StringArrayHashMapUnmanaged(u32), +replace_names: std.StringArrayHashMapUnmanaged(u32), +gpa: std.mem.Allocator, +arena: std.mem.Allocator, + +pub const Transformation = union(enum) { + /// Replace the fn decl AST Node with one whose body is only `@trap()` with + /// discarded parameters. + gut_function: Ast.Node.Index, + /// Omit a global declaration. + delete_node: Ast.Node.Index, + /// Delete a local variable declaration and replace all of its references + /// with `undefined`. + delete_var_decl: struct { + var_decl_node: Ast.Node.Index, + /// Identifier nodes that reference the variable. + references: std.ArrayListUnmanaged(Ast.Node.Index), + }, + /// Replace an expression with `undefined`. + replace_with_undef: Ast.Node.Index, + /// Replace an expression with `true`. + replace_with_true: Ast.Node.Index, + /// Replace an expression with `false`. + replace_with_false: Ast.Node.Index, + /// Replace a node with another node. + replace_node: struct { + to_replace: Ast.Node.Index, + replacement: Ast.Node.Index, + }, + /// Replace an `@import` with the imported file contents wrapped in a struct. + inline_imported_file: InlineImportedFile, + + pub const InlineImportedFile = struct { + builtin_call_node: Ast.Node.Index, + imported_string: []const u8, + /// Identifier names that must be renamed in the inlined code or else + /// will cause ambiguous reference errors. + in_scope_names: std.StringArrayHashMapUnmanaged(void), + }; +}; + +pub const Error = error{OutOfMemory}; + +/// The result will be priority shuffled. +pub fn findTransformations( + arena: std.mem.Allocator, + ast: *const Ast, + transformations: *std.ArrayList(Transformation), +) !void { + transformations.clearRetainingCapacity(); + + var walk: Walk = .{ + .ast = ast, + .transformations = transformations, + .gpa = transformations.allocator, + .arena = arena, + .unreferenced_globals = .{}, + .in_scope_names = .{}, + .replace_names = .{}, + }; + defer { + walk.unreferenced_globals.deinit(walk.gpa); + walk.in_scope_names.deinit(walk.gpa); + walk.replace_names.deinit(walk.gpa); + } + + try walkMembers(&walk, walk.ast.rootDecls()); + + const unreferenced_globals = walk.unreferenced_globals.values(); + try transformations.ensureUnusedCapacity(unreferenced_globals.len); + for (unreferenced_globals) |node| { + transformations.appendAssumeCapacity(.{ .delete_node = node }); + } +} + +fn walkMembers(w: *Walk, members: []const Ast.Node.Index) Error!void { + // First we scan for globals so that we can delete them while walking. + try scanDecls(w, members, .add); + + for (members) |member| { + try walkMember(w, member); + } + + try scanDecls(w, members, .remove); +} + +const ScanDeclsAction = enum { add, remove }; + +fn scanDecls(w: *Walk, members: []const Ast.Node.Index, action: ScanDeclsAction) Error!void { + const ast = w.ast; + const gpa = w.gpa; + const node_tags = ast.nodes.items(.tag); + const main_tokens = ast.nodes.items(.main_token); + const token_tags = ast.tokens.items(.tag); + + for (members) |member_node| { + const name_token = switch (node_tags[member_node]) { + .global_var_decl, + .local_var_decl, + .simple_var_decl, + .aligned_var_decl, + => main_tokens[member_node] + 1, + + .fn_proto_simple, + .fn_proto_multi, + .fn_proto_one, + .fn_proto, + .fn_decl, + => main_tokens[member_node] + 1, + + else => continue, + }; + + assert(token_tags[name_token] == .identifier); + const name_bytes = ast.tokenSlice(name_token); + + switch (action) { + .add => { + try w.unreferenced_globals.put(gpa, name_bytes, member_node); + + const gop = try w.in_scope_names.getOrPut(gpa, name_bytes); + if (!gop.found_existing) gop.value_ptr.* = 0; + gop.value_ptr.* += 1; + }, + .remove => { + const entry = w.in_scope_names.getEntry(name_bytes).?; + if (entry.value_ptr.* <= 1) { + assert(w.in_scope_names.swapRemove(name_bytes)); + } else { + entry.value_ptr.* -= 1; + } + }, + } + } +} + +fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { + const ast = w.ast; + const datas = ast.nodes.items(.data); + switch (ast.nodes.items(.tag)[decl]) { + .fn_decl => { + const fn_proto = datas[decl].lhs; + try walkExpression(w, fn_proto); + const body_node = datas[decl].rhs; + if (!isFnBodyGutted(ast, body_node)) { + w.replace_names.clearRetainingCapacity(); + try w.transformations.append(.{ .gut_function = decl }); + try walkExpression(w, body_node); + } + }, + .fn_proto_simple, + .fn_proto_multi, + .fn_proto_one, + .fn_proto, + => { + try walkExpression(w, decl); + }, + + .@"usingnamespace" => { + try w.transformations.append(.{ .delete_node = decl }); + const expr = datas[decl].lhs; + try walkExpression(w, expr); + }, + + .global_var_decl, + .local_var_decl, + .simple_var_decl, + .aligned_var_decl, + => try walkGlobalVarDecl(w, decl, ast.fullVarDecl(decl).?), + + .test_decl => { + try w.transformations.append(.{ .delete_node = decl }); + try walkExpression(w, datas[decl].rhs); + }, + + .container_field_init, + .container_field_align, + .container_field, + => { + try w.transformations.append(.{ .delete_node = decl }); + try walkContainerField(w, ast.fullContainerField(decl).?); + }, + + .@"comptime" => { + try w.transformations.append(.{ .delete_node = decl }); + try walkExpression(w, decl); + }, + + .root => unreachable, + else => unreachable, + } +} + +fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void { + const ast = w.ast; + const token_tags = ast.tokens.items(.tag); + const main_tokens = ast.nodes.items(.main_token); + const node_tags = ast.nodes.items(.tag); + const datas = ast.nodes.items(.data); + switch (node_tags[node]) { + .identifier => { + const name_ident = main_tokens[node]; + assert(token_tags[name_ident] == .identifier); + const name_bytes = ast.tokenSlice(name_ident); + _ = w.unreferenced_globals.swapRemove(name_bytes); + if (w.replace_names.get(name_bytes)) |index| { + try w.transformations.items[index].delete_var_decl.references.append(w.arena, node); + } + }, + + .number_literal, + .char_literal, + .unreachable_literal, + .anyframe_literal, + .string_literal, + => {}, + + .multiline_string_literal => {}, + + .error_value => {}, + + .block_two, + .block_two_semicolon, + => { + const statements = [2]Ast.Node.Index{ datas[node].lhs, datas[node].rhs }; + if (datas[node].lhs == 0) { + return walkBlock(w, node, statements[0..0]); + } else if (datas[node].rhs == 0) { + return walkBlock(w, node, statements[0..1]); + } else { + return walkBlock(w, node, statements[0..2]); + } + }, + .block, + .block_semicolon, + => { + const statements = ast.extra_data[datas[node].lhs..datas[node].rhs]; + return walkBlock(w, node, statements); + }, + + .@"errdefer" => { + const expr = datas[node].rhs; + return walkExpression(w, expr); + }, + + .@"defer" => { + const expr = datas[node].rhs; + return walkExpression(w, expr); + }, + .@"comptime", .@"nosuspend" => { + const block = datas[node].lhs; + return walkExpression(w, block); + }, + + .@"suspend" => { + const body = datas[node].lhs; + return walkExpression(w, body); + }, + + .@"catch" => { + try walkExpression(w, datas[node].lhs); // target + try walkExpression(w, datas[node].rhs); // fallback + }, + + .field_access => { + const field_access = datas[node]; + try walkExpression(w, field_access.lhs); + }, + + .error_union, + .switch_range, + => { + const infix = datas[node]; + try walkExpression(w, infix.lhs); + return walkExpression(w, infix.rhs); + }, + .for_range => { + const infix = datas[node]; + try walkExpression(w, infix.lhs); + if (infix.rhs != 0) { + return walkExpression(w, infix.rhs); + } + }, + + .add, + .add_wrap, + .add_sat, + .array_cat, + .array_mult, + .assign, + .assign_bit_and, + .assign_bit_or, + .assign_shl, + .assign_shl_sat, + .assign_shr, + .assign_bit_xor, + .assign_div, + .assign_sub, + .assign_sub_wrap, + .assign_sub_sat, + .assign_mod, + .assign_add, + .assign_add_wrap, + .assign_add_sat, + .assign_mul, + .assign_mul_wrap, + .assign_mul_sat, + .bang_equal, + .bit_and, + .bit_or, + .shl, + .shl_sat, + .shr, + .bit_xor, + .bool_and, + .bool_or, + .div, + .equal_equal, + .greater_or_equal, + .greater_than, + .less_or_equal, + .less_than, + .merge_error_sets, + .mod, + .mul, + .mul_wrap, + .mul_sat, + .sub, + .sub_wrap, + .sub_sat, + .@"orelse", + => { + const infix = datas[node]; + try walkExpression(w, infix.lhs); + try walkExpression(w, infix.rhs); + }, + + .assign_destructure => { + const lhs_count = ast.extra_data[datas[node].lhs]; + assert(lhs_count > 1); + const lhs_exprs = ast.extra_data[datas[node].lhs + 1 ..][0..lhs_count]; + const rhs = datas[node].rhs; + + for (lhs_exprs) |lhs_node| { + switch (node_tags[lhs_node]) { + .global_var_decl, + .local_var_decl, + .simple_var_decl, + .aligned_var_decl, + => try walkLocalVarDecl(w, ast.fullVarDecl(lhs_node).?), + + else => try walkExpression(w, lhs_node), + } + } + return walkExpression(w, rhs); + }, + + .bit_not, + .bool_not, + .negation, + .negation_wrap, + .optional_type, + .address_of, + => { + return walkExpression(w, datas[node].lhs); + }, + + .@"try", + .@"resume", + .@"await", + => { + return walkExpression(w, datas[node].lhs); + }, + + .array_type, + .array_type_sentinel, + => {}, + + .ptr_type_aligned, + .ptr_type_sentinel, + .ptr_type, + .ptr_type_bit_range, + => {}, + + .array_init_one, + .array_init_one_comma, + .array_init_dot_two, + .array_init_dot_two_comma, + .array_init_dot, + .array_init_dot_comma, + .array_init, + .array_init_comma, + => { + var elements: [2]Ast.Node.Index = undefined; + return walkArrayInit(w, ast.fullArrayInit(&elements, node).?); + }, + + .struct_init_one, + .struct_init_one_comma, + .struct_init_dot_two, + .struct_init_dot_two_comma, + .struct_init_dot, + .struct_init_dot_comma, + .struct_init, + .struct_init_comma, + => { + var buf: [2]Ast.Node.Index = undefined; + return walkStructInit(w, node, ast.fullStructInit(&buf, node).?); + }, + + .call_one, + .call_one_comma, + .async_call_one, + .async_call_one_comma, + .call, + .call_comma, + .async_call, + .async_call_comma, + => { + var buf: [1]Ast.Node.Index = undefined; + return walkCall(w, ast.fullCall(&buf, node).?); + }, + + .array_access => { + const suffix = datas[node]; + try walkExpression(w, suffix.lhs); + try walkExpression(w, suffix.rhs); + }, + + .slice_open, .slice, .slice_sentinel => return walkSlice(w, node, ast.fullSlice(node).?), + + .deref => { + try walkExpression(w, datas[node].lhs); + }, + + .unwrap_optional => { + try walkExpression(w, datas[node].lhs); + }, + + .@"break" => { + const label_token = datas[node].lhs; + const target = datas[node].rhs; + if (label_token == 0 and target == 0) { + // no expressions + } else if (label_token == 0 and target != 0) { + try walkExpression(w, target); + } else if (label_token != 0 and target == 0) { + try walkIdentifier(w, label_token); + } else if (label_token != 0 and target != 0) { + try walkExpression(w, target); + } + }, + + .@"continue" => { + const label = datas[node].lhs; + if (label != 0) { + return walkIdentifier(w, label); // label + } + }, + + .@"return" => { + if (datas[node].lhs != 0) { + try walkExpression(w, datas[node].lhs); + } + }, + + .grouped_expression => { + try walkExpression(w, datas[node].lhs); + }, + + .container_decl, + .container_decl_trailing, + .container_decl_arg, + .container_decl_arg_trailing, + .container_decl_two, + .container_decl_two_trailing, + .tagged_union, + .tagged_union_trailing, + .tagged_union_enum_tag, + .tagged_union_enum_tag_trailing, + .tagged_union_two, + .tagged_union_two_trailing, + => { + var buf: [2]Ast.Node.Index = undefined; + return walkContainerDecl(w, node, ast.fullContainerDecl(&buf, node).?); + }, + + .error_set_decl => { + const error_token = main_tokens[node]; + const lbrace = error_token + 1; + const rbrace = datas[node].rhs; + + var i = lbrace + 1; + while (i < rbrace) : (i += 1) { + switch (token_tags[i]) { + .doc_comment => unreachable, // TODO + .identifier => try walkIdentifier(w, i), + .comma => {}, + else => unreachable, + } + } + }, + + .builtin_call_two, .builtin_call_two_comma => { + if (datas[node].lhs == 0) { + return walkBuiltinCall(w, node, &.{}); + } else if (datas[node].rhs == 0) { + return walkBuiltinCall(w, node, &.{datas[node].lhs}); + } else { + return walkBuiltinCall(w, node, &.{ datas[node].lhs, datas[node].rhs }); + } + }, + .builtin_call, .builtin_call_comma => { + const params = ast.extra_data[datas[node].lhs..datas[node].rhs]; + return walkBuiltinCall(w, node, params); + }, + + .fn_proto_simple, + .fn_proto_multi, + .fn_proto_one, + .fn_proto, + => { + var buf: [1]Ast.Node.Index = undefined; + return walkFnProto(w, ast.fullFnProto(&buf, node).?); + }, + + .anyframe_type => { + if (datas[node].rhs != 0) { + return walkExpression(w, datas[node].rhs); + } + }, + + .@"switch", + .switch_comma, + => { + const condition = datas[node].lhs; + const extra = ast.extraData(datas[node].rhs, Ast.Node.SubRange); + const cases = ast.extra_data[extra.start..extra.end]; + + try walkExpression(w, condition); // condition expression + try walkExpressions(w, cases); + }, + + .switch_case_one, + .switch_case_inline_one, + .switch_case, + .switch_case_inline, + => return walkSwitchCase(w, ast.fullSwitchCase(node).?), + + .while_simple, + .while_cont, + .@"while", + => return walkWhile(w, node, ast.fullWhile(node).?), + + .for_simple, + .@"for", + => return walkFor(w, ast.fullFor(node).?), + + .if_simple, + .@"if", + => return walkIf(w, node, ast.fullIf(node).?), + + .asm_simple, + .@"asm", + => return walkAsm(w, ast.fullAsm(node).?), + + .enum_literal => { + return walkIdentifier(w, main_tokens[node]); // name + }, + + .fn_decl => unreachable, + .container_field => unreachable, + .container_field_init => unreachable, + .container_field_align => unreachable, + .root => unreachable, + .global_var_decl => unreachable, + .local_var_decl => unreachable, + .simple_var_decl => unreachable, + .aligned_var_decl => unreachable, + .@"usingnamespace" => unreachable, + .test_decl => unreachable, + .asm_output => unreachable, + .asm_input => unreachable, + } +} + +fn walkGlobalVarDecl(w: *Walk, decl_node: Ast.Node.Index, var_decl: Ast.full.VarDecl) Error!void { + _ = decl_node; + + if (var_decl.ast.type_node != 0) { + try walkExpression(w, var_decl.ast.type_node); + } + + if (var_decl.ast.align_node != 0) { + try walkExpression(w, var_decl.ast.align_node); + } + + if (var_decl.ast.addrspace_node != 0) { + try walkExpression(w, var_decl.ast.addrspace_node); + } + + if (var_decl.ast.section_node != 0) { + try walkExpression(w, var_decl.ast.section_node); + } + + if (var_decl.ast.init_node != 0) { + if (!isUndefinedIdent(w.ast, var_decl.ast.init_node)) { + try w.transformations.append(.{ .replace_with_undef = var_decl.ast.init_node }); + } + try walkExpression(w, var_decl.ast.init_node); + } +} + +fn walkLocalVarDecl(w: *Walk, var_decl: Ast.full.VarDecl) Error!void { + try walkIdentifierNew(w, var_decl.ast.mut_token + 1); // name + + if (var_decl.ast.type_node != 0) { + try walkExpression(w, var_decl.ast.type_node); + } + + if (var_decl.ast.align_node != 0) { + try walkExpression(w, var_decl.ast.align_node); + } + + if (var_decl.ast.addrspace_node != 0) { + try walkExpression(w, var_decl.ast.addrspace_node); + } + + if (var_decl.ast.section_node != 0) { + try walkExpression(w, var_decl.ast.section_node); + } + + if (var_decl.ast.init_node != 0) { + if (!isUndefinedIdent(w.ast, var_decl.ast.init_node)) { + try w.transformations.append(.{ .replace_with_undef = var_decl.ast.init_node }); + } + try walkExpression(w, var_decl.ast.init_node); + } +} + +fn walkContainerField(w: *Walk, field: Ast.full.ContainerField) Error!void { + if (field.ast.type_expr != 0) { + try walkExpression(w, field.ast.type_expr); // type + } + if (field.ast.align_expr != 0) { + try walkExpression(w, field.ast.align_expr); // alignment + } + if (field.ast.value_expr != 0) { + try walkExpression(w, field.ast.value_expr); // value + } +} + +fn walkBlock( + w: *Walk, + block_node: Ast.Node.Index, + statements: []const Ast.Node.Index, +) Error!void { + _ = block_node; + const ast = w.ast; + const node_tags = ast.nodes.items(.tag); + + for (statements) |stmt| { + switch (node_tags[stmt]) { + .global_var_decl, + .local_var_decl, + .simple_var_decl, + .aligned_var_decl, + => { + const var_decl = ast.fullVarDecl(stmt).?; + if (var_decl.ast.init_node != 0 and + isUndefinedIdent(w.ast, var_decl.ast.init_node)) + { + try w.transformations.append(.{ .delete_var_decl = .{ + .var_decl_node = stmt, + .references = .{}, + } }); + const name_tok = var_decl.ast.mut_token + 1; + const name_bytes = ast.tokenSlice(name_tok); + try w.replace_names.put(w.gpa, name_bytes, @intCast(w.transformations.items.len - 1)); + } else { + try walkLocalVarDecl(w, var_decl); + } + }, + + else => { + switch (categorizeStmt(ast, stmt)) { + // Don't try to remove `_ = foo;` discards; those are handled separately. + .discard_identifier => {}, + // definitely try to remove `_ = undefined;` though. + .discard_undefined, .trap_call, .other => { + try w.transformations.append(.{ .delete_node = stmt }); + }, + } + try walkExpression(w, stmt); + }, + } + } +} + +fn walkArrayType(w: *Walk, array_type: Ast.full.ArrayType) Error!void { + try walkExpression(w, array_type.ast.elem_count); + if (array_type.ast.sentinel != 0) { + try walkExpression(w, array_type.ast.sentinel); + } + return walkExpression(w, array_type.ast.elem_type); +} + +fn walkArrayInit(w: *Walk, array_init: Ast.full.ArrayInit) Error!void { + if (array_init.ast.type_expr != 0) { + try walkExpression(w, array_init.ast.type_expr); // T + } + for (array_init.ast.elements) |elem_init| { + try walkExpression(w, elem_init); + } +} + +fn walkStructInit( + w: *Walk, + struct_node: Ast.Node.Index, + struct_init: Ast.full.StructInit, +) Error!void { + _ = struct_node; + if (struct_init.ast.type_expr != 0) { + try walkExpression(w, struct_init.ast.type_expr); // T + } + for (struct_init.ast.fields) |field_init| { + try walkExpression(w, field_init); + } +} + +fn walkCall(w: *Walk, call: Ast.full.Call) Error!void { + try walkExpression(w, call.ast.fn_expr); + try walkParamList(w, call.ast.params); +} + +fn walkSlice( + w: *Walk, + slice_node: Ast.Node.Index, + slice: Ast.full.Slice, +) Error!void { + _ = slice_node; + try walkExpression(w, slice.ast.sliced); + try walkExpression(w, slice.ast.start); + if (slice.ast.end != 0) { + try walkExpression(w, slice.ast.end); + } + if (slice.ast.sentinel != 0) { + try walkExpression(w, slice.ast.sentinel); + } +} + +fn walkIdentifier(w: *Walk, name_ident: Ast.TokenIndex) Error!void { + const ast = w.ast; + const token_tags = ast.tokens.items(.tag); + assert(token_tags[name_ident] == .identifier); + const name_bytes = ast.tokenSlice(name_ident); + _ = w.unreferenced_globals.swapRemove(name_bytes); +} + +fn walkIdentifierNew(w: *Walk, name_ident: Ast.TokenIndex) Error!void { + _ = w; + _ = name_ident; +} + +fn walkContainerDecl( + w: *Walk, + container_decl_node: Ast.Node.Index, + container_decl: Ast.full.ContainerDecl, +) Error!void { + _ = container_decl_node; + if (container_decl.ast.arg != 0) { + try walkExpression(w, container_decl.ast.arg); + } + try walkMembers(w, container_decl.ast.members); +} + +fn walkBuiltinCall( + w: *Walk, + call_node: Ast.Node.Index, + params: []const Ast.Node.Index, +) Error!void { + const ast = w.ast; + const main_tokens = ast.nodes.items(.main_token); + const builtin_token = main_tokens[call_node]; + const builtin_name = ast.tokenSlice(builtin_token); + const info = BuiltinFn.list.get(builtin_name).?; + switch (info.tag) { + .import => { + const operand_node = params[0]; + const str_lit_token = main_tokens[operand_node]; + const token_bytes = ast.tokenSlice(str_lit_token); + if (std.mem.endsWith(u8, token_bytes, ".zig\"")) { + const imported_string = std.zig.string_literal.parseAlloc(w.arena, token_bytes) catch + unreachable; + try w.transformations.append(.{ .inline_imported_file = .{ + .builtin_call_node = call_node, + .imported_string = imported_string, + .in_scope_names = try std.StringArrayHashMapUnmanaged(void).init( + w.arena, + w.in_scope_names.keys(), + &.{}, + ), + } }); + } + }, + else => {}, + } + for (params) |param_node| { + try walkExpression(w, param_node); + } +} + +fn walkFnProto(w: *Walk, fn_proto: Ast.full.FnProto) Error!void { + const ast = w.ast; + + { + var it = fn_proto.iterate(ast); + while (it.next()) |param| { + if (param.type_expr != 0) { + try walkExpression(w, param.type_expr); + } + } + } + + if (fn_proto.ast.align_expr != 0) { + try walkExpression(w, fn_proto.ast.align_expr); + } + + if (fn_proto.ast.addrspace_expr != 0) { + try walkExpression(w, fn_proto.ast.addrspace_expr); + } + + if (fn_proto.ast.section_expr != 0) { + try walkExpression(w, fn_proto.ast.section_expr); + } + + if (fn_proto.ast.callconv_expr != 0) { + try walkExpression(w, fn_proto.ast.callconv_expr); + } + + try walkExpression(w, fn_proto.ast.return_type); +} + +fn walkExpressions(w: *Walk, expressions: []const Ast.Node.Index) Error!void { + for (expressions) |expression| { + try walkExpression(w, expression); + } +} + +fn walkSwitchCase(w: *Walk, switch_case: Ast.full.SwitchCase) Error!void { + for (switch_case.ast.values) |value_expr| { + try walkExpression(w, value_expr); + } + try walkExpression(w, switch_case.ast.target_expr); +} + +fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) Error!void { + assert(while_node.ast.cond_expr != 0); + assert(while_node.ast.then_expr != 0); + + // Perform these transformations in this priority order: + // 1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already. + // 2. If the `then` block is empty, replace the condition with `if (false)` if it is not already. + // 3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression. + // 4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression. + if (!isTrueIdent(w.ast, while_node.ast.cond_expr) and + (while_node.ast.else_expr == 0 or isEmptyBlock(w.ast, while_node.ast.else_expr))) + { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_with_true = while_node.ast.cond_expr }); + } else if (!isFalseIdent(w.ast, while_node.ast.cond_expr) and isEmptyBlock(w.ast, while_node.ast.then_expr)) { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_with_false = while_node.ast.cond_expr }); + } else if (isTrueIdent(w.ast, while_node.ast.cond_expr)) { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_node = .{ + .to_replace = node_index, + .replacement = while_node.ast.then_expr, + } }); + } else if (isFalseIdent(w.ast, while_node.ast.cond_expr)) { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_node = .{ + .to_replace = node_index, + .replacement = while_node.ast.else_expr, + } }); + } + + try walkExpression(w, while_node.ast.cond_expr); // condition + + if (while_node.ast.cont_expr != 0) { + try walkExpression(w, while_node.ast.cont_expr); + } + + if (while_node.ast.then_expr != 0) { + try walkExpression(w, while_node.ast.then_expr); + } + if (while_node.ast.else_expr != 0) { + try walkExpression(w, while_node.ast.else_expr); + } +} + +fn walkFor(w: *Walk, for_node: Ast.full.For) Error!void { + try walkParamList(w, for_node.ast.inputs); + if (for_node.ast.then_expr != 0) { + try walkExpression(w, for_node.ast.then_expr); + } + if (for_node.ast.else_expr != 0) { + try walkExpression(w, for_node.ast.else_expr); + } +} + +fn walkIf(w: *Walk, node_index: Ast.Node.Index, if_node: Ast.full.If) Error!void { + assert(if_node.ast.cond_expr != 0); + assert(if_node.ast.then_expr != 0); + + // Perform these transformations in this priority order: + // 1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already. + // 2. If the `then` block is empty, replace the condition with `if (false)` if it is not already. + // 3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression. + // 4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression. + if (!isTrueIdent(w.ast, if_node.ast.cond_expr) and + (if_node.ast.else_expr == 0 or isEmptyBlock(w.ast, if_node.ast.else_expr))) + { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_with_true = if_node.ast.cond_expr }); + } else if (!isFalseIdent(w.ast, if_node.ast.cond_expr) and isEmptyBlock(w.ast, if_node.ast.then_expr)) { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_with_false = if_node.ast.cond_expr }); + } else if (isTrueIdent(w.ast, if_node.ast.cond_expr)) { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_node = .{ + .to_replace = node_index, + .replacement = if_node.ast.then_expr, + } }); + } else if (isFalseIdent(w.ast, if_node.ast.cond_expr)) { + try w.transformations.ensureUnusedCapacity(1); + w.transformations.appendAssumeCapacity(.{ .replace_node = .{ + .to_replace = node_index, + .replacement = if_node.ast.else_expr, + } }); + } + + try walkExpression(w, if_node.ast.cond_expr); // condition + + if (if_node.ast.then_expr != 0) { + try walkExpression(w, if_node.ast.then_expr); + } + if (if_node.ast.else_expr != 0) { + try walkExpression(w, if_node.ast.else_expr); + } +} + +fn walkAsm(w: *Walk, asm_node: Ast.full.Asm) Error!void { + try walkExpression(w, asm_node.ast.template); + for (asm_node.ast.items) |item| { + try walkExpression(w, item); + } +} + +fn walkParamList(w: *Walk, params: []const Ast.Node.Index) Error!void { + for (params) |param_node| { + try walkExpression(w, param_node); + } +} + +/// Check if it is already gutted (i.e. its body replaced with `@trap()`). +fn isFnBodyGutted(ast: *const Ast, body_node: Ast.Node.Index) bool { + // skip over discards + const node_tags = ast.nodes.items(.tag); + const datas = ast.nodes.items(.data); + var statements_buf: [2]Ast.Node.Index = undefined; + const statements = switch (node_tags[body_node]) { + .block_two, + .block_two_semicolon, + => blk: { + statements_buf[0..2].* = .{ datas[body_node].lhs, datas[body_node].rhs }; + break :blk if (datas[body_node].lhs == 0) + statements_buf[0..0] + else if (datas[body_node].rhs == 0) + statements_buf[0..1] + else + statements_buf[0..2]; + }, + + .block, + .block_semicolon, + => ast.extra_data[datas[body_node].lhs..datas[body_node].rhs], + + else => return false, + }; + var i: usize = 0; + while (i < statements.len) : (i += 1) { + switch (categorizeStmt(ast, statements[i])) { + .discard_identifier => continue, + .trap_call => return i + 1 == statements.len, + else => return false, + } + } + return false; +} + +const StmtCategory = enum { + discard_undefined, + discard_identifier, + trap_call, + other, +}; + +fn categorizeStmt(ast: *const Ast, stmt: Ast.Node.Index) StmtCategory { + const node_tags = ast.nodes.items(.tag); + const datas = ast.nodes.items(.data); + const main_tokens = ast.nodes.items(.main_token); + switch (node_tags[stmt]) { + .builtin_call_two, .builtin_call_two_comma => { + if (datas[stmt].lhs == 0) { + return categorizeBuiltinCall(ast, main_tokens[stmt], &.{}); + } else if (datas[stmt].rhs == 0) { + return categorizeBuiltinCall(ast, main_tokens[stmt], &.{datas[stmt].lhs}); + } else { + return categorizeBuiltinCall(ast, main_tokens[stmt], &.{ datas[stmt].lhs, datas[stmt].rhs }); + } + }, + .builtin_call, .builtin_call_comma => { + const params = ast.extra_data[datas[stmt].lhs..datas[stmt].rhs]; + return categorizeBuiltinCall(ast, main_tokens[stmt], params); + }, + .assign => { + const infix = datas[stmt]; + if (isDiscardIdent(ast, infix.lhs) and node_tags[infix.rhs] == .identifier) { + const name_bytes = ast.tokenSlice(main_tokens[infix.rhs]); + if (std.mem.eql(u8, name_bytes, "undefined")) { + return .discard_undefined; + } else { + return .discard_identifier; + } + } + return .other; + }, + else => return .other, + } +} + +fn categorizeBuiltinCall( + ast: *const Ast, + builtin_token: Ast.TokenIndex, + params: []const Ast.Node.Index, +) StmtCategory { + if (params.len != 0) return .other; + const name_bytes = ast.tokenSlice(builtin_token); + if (std.mem.eql(u8, name_bytes, "@trap")) + return .trap_call; + return .other; +} + +fn isDiscardIdent(ast: *const Ast, node: Ast.Node.Index) bool { + return isMatchingIdent(ast, node, "_"); +} + +fn isUndefinedIdent(ast: *const Ast, node: Ast.Node.Index) bool { + return isMatchingIdent(ast, node, "undefined"); +} + +fn isTrueIdent(ast: *const Ast, node: Ast.Node.Index) bool { + return isMatchingIdent(ast, node, "true"); +} + +fn isFalseIdent(ast: *const Ast, node: Ast.Node.Index) bool { + return isMatchingIdent(ast, node, "false"); +} + +fn isMatchingIdent(ast: *const Ast, node: Ast.Node.Index, string: []const u8) bool { + const node_tags = ast.nodes.items(.tag); + const main_tokens = ast.nodes.items(.main_token); + switch (node_tags[node]) { + .identifier => { + const token_index = main_tokens[node]; + const name_bytes = ast.tokenSlice(token_index); + return std.mem.eql(u8, name_bytes, string); + }, + else => return false, + } +} + +fn isEmptyBlock(ast: *const Ast, node: Ast.Node.Index) bool { + const node_tags = ast.nodes.items(.tag); + const node_data = ast.nodes.items(.data); + switch (node_tags[node]) { + .block_two => { + return node_data[node].lhs == 0 and node_data[node].rhs == 0; + }, + else => return false, + } +} diff --git a/lib/compiler/test_runner.zig b/lib/compiler/test_runner.zig new file mode 100644 index 0000000000000000000000000000000000000000..e11d187d2a1ce7ba00d5ce2c03415d33febfde72 --- /dev/null +++ b/lib/compiler/test_runner.zig @@ -0,0 +1,249 @@ +//! Default test runner for unit tests. +const std = @import("std"); +const io = std.io; +const builtin = @import("builtin"); + +pub const std_options = .{ + .logFn = log, +}; + +var log_err_count: usize = 0; +var cmdline_buffer: [4096]u8 = undefined; +var fba = std.heap.FixedBufferAllocator.init(&cmdline_buffer); + +pub fn main() void { + if (builtin.zig_backend == .stage2_aarch64) { + return mainSimple() catch @panic("test failure"); + } + + const args = std.process.argsAlloc(fba.allocator()) catch + @panic("unable to parse command line args"); + + var listen = false; + + for (args[1..]) |arg| { + if (std.mem.eql(u8, arg, "--listen=-")) { + listen = true; + } else { + @panic("unrecognized command line argument"); + } + } + + if (listen) { + return mainServer() catch @panic("internal test runner failure"); + } else { + return mainTerminal(); + } +} + +fn mainServer() !void { + var server = try std.zig.Server.init(.{ + .gpa = fba.allocator(), + .in = std.io.getStdIn(), + .out = std.io.getStdOut(), + .zig_version = builtin.zig_version_string, + }); + defer server.deinit(); + + while (true) { + const hdr = try server.receiveMessage(); + switch (hdr.tag) { + .exit => { + return std.process.exit(0); + }, + .query_test_metadata => { + std.testing.allocator_instance = .{}; + defer if (std.testing.allocator_instance.deinit() == .leak) { + @panic("internal test runner memory leak"); + }; + + var string_bytes: std.ArrayListUnmanaged(u8) = .{}; + defer string_bytes.deinit(std.testing.allocator); + try string_bytes.append(std.testing.allocator, 0); // Reserve 0 for null. + + const test_fns = builtin.test_functions; + const names = try std.testing.allocator.alloc(u32, test_fns.len); + defer std.testing.allocator.free(names); + const expected_panic_msgs = try std.testing.allocator.alloc(u32, test_fns.len); + defer std.testing.allocator.free(expected_panic_msgs); + + for (test_fns, names, expected_panic_msgs) |test_fn, *name, *expected_panic_msg| { + name.* = @as(u32, @intCast(string_bytes.items.len)); + try string_bytes.ensureUnusedCapacity(std.testing.allocator, test_fn.name.len + 1); + string_bytes.appendSliceAssumeCapacity(test_fn.name); + string_bytes.appendAssumeCapacity(0); + expected_panic_msg.* = 0; + } + + try server.serveTestMetadata(.{ + .names = names, + .expected_panic_msgs = expected_panic_msgs, + .string_bytes = string_bytes.items, + }); + }, + + .run_test => { + std.testing.allocator_instance = .{}; + log_err_count = 0; + const index = try server.receiveBody_u32(); + const test_fn = builtin.test_functions[index]; + var fail = false; + var skip = false; + var leak = false; + test_fn.func() catch |err| switch (err) { + error.SkipZigTest => skip = true, + else => { + fail = true; + if (@errorReturnTrace()) |trace| { + std.debug.dumpStackTrace(trace.*); + } + }, + }; + leak = std.testing.allocator_instance.deinit() == .leak; + try server.serveTestResults(.{ + .index = index, + .flags = .{ + .fail = fail, + .skip = skip, + .leak = leak, + .log_err_count = std.math.lossyCast(std.meta.FieldType( + std.zig.Server.Message.TestResults.Flags, + .log_err_count, + ), log_err_count), + }, + }); + }, + + else => { + std.debug.print("unsupported message: {x}", .{@intFromEnum(hdr.tag)}); + std.process.exit(1); + }, + } + } +} + +fn mainTerminal() void { + const test_fn_list = builtin.test_functions; + var ok_count: usize = 0; + var skip_count: usize = 0; + var fail_count: usize = 0; + var progress = std.Progress{ + .dont_print_on_dumb = true, + }; + const root_node = progress.start("Test", test_fn_list.len); + const have_tty = progress.terminal != null and + (progress.supports_ansi_escape_codes or progress.is_windows_terminal); + + var async_frame_buffer: []align(builtin.target.stackAlignment()) u8 = undefined; + // TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly + // ignores the alignment of the slice. + async_frame_buffer = &[_]u8{}; + + var leaks: usize = 0; + for (test_fn_list, 0..) |test_fn, i| { + std.testing.allocator_instance = .{}; + defer { + if (std.testing.allocator_instance.deinit() == .leak) { + leaks += 1; + } + } + std.testing.log_level = .warn; + + var test_node = root_node.start(test_fn.name, 0); + test_node.activate(); + progress.refresh(); + if (!have_tty) { + std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name }); + } + if (test_fn.func()) |_| { + ok_count += 1; + test_node.end(); + if (!have_tty) std.debug.print("OK\n", .{}); + } else |err| switch (err) { + error.SkipZigTest => { + skip_count += 1; + progress.log("SKIP\n", .{}); + test_node.end(); + }, + else => { + fail_count += 1; + progress.log("FAIL ({s})\n", .{@errorName(err)}); + if (@errorReturnTrace()) |trace| { + std.debug.dumpStackTrace(trace.*); + } + test_node.end(); + }, + } + } + root_node.end(); + if (ok_count == test_fn_list.len) { + std.debug.print("All {d} tests passed.\n", .{ok_count}); + } else { + std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count }); + } + if (log_err_count != 0) { + std.debug.print("{d} errors were logged.\n", .{log_err_count}); + } + if (leaks != 0) { + std.debug.print("{d} tests leaked memory.\n", .{leaks}); + } + if (leaks != 0 or log_err_count != 0 or fail_count != 0) { + std.process.exit(1); + } +} + +pub fn log( + comptime message_level: std.log.Level, + comptime scope: @Type(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + if (@intFromEnum(message_level) <= @intFromEnum(std.log.Level.err)) { + log_err_count +|= 1; + } + if (@intFromEnum(message_level) <= @intFromEnum(std.testing.log_level)) { + std.debug.print( + "[" ++ @tagName(scope) ++ "] (" ++ @tagName(message_level) ++ "): " ++ format ++ "\n", + args, + ); + } +} + +/// Simpler main(), exercising fewer language features, so that +/// work-in-progress backends can handle it. +pub fn mainSimple() anyerror!void { + const enable_print = false; + const print_all = false; + + var passed: u64 = 0; + var skipped: u64 = 0; + var failed: u64 = 0; + const stderr = if (enable_print) std.io.getStdErr() else {}; + for (builtin.test_functions) |test_fn| { + if (enable_print and print_all) { + stderr.writeAll(test_fn.name) catch {}; + stderr.writeAll("... ") catch {}; + } + test_fn.func() catch |err| { + if (enable_print and !print_all) { + stderr.writeAll(test_fn.name) catch {}; + stderr.writeAll("... ") catch {}; + } + if (err != error.SkipZigTest) { + if (enable_print) stderr.writeAll("FAIL\n") catch {}; + failed += 1; + if (!enable_print) return err; + continue; + } + if (enable_print) stderr.writeAll("SKIP\n") catch {}; + skipped += 1; + continue; + }; + if (enable_print and print_all) stderr.writeAll("PASS\n") catch {}; + passed += 1; + } + if (enable_print) { + stderr.writer().print("{} passed, {} skipped, {} failed\n", .{ passed, skipped, failed }) catch {}; + if (failed != 0) std.process.exit(1); + } +} diff --git a/lib/std/zig/fmt.zig b/lib/std/zig/fmt.zig deleted file mode 100644 index 2fc04b7935a76e7a2f9acbc3e64e2ca81448a219..0000000000000000000000000000000000000000 --- a/lib/std/zig/fmt.zig +++ /dev/null @@ -1,342 +0,0 @@ -const std = @import("std"); -const mem = std.mem; -const fs = std.fs; -const process = std.process; -const Allocator = std.mem.Allocator; -const warn = std.log.warn; -const Color = std.zig.Color; - -const usage_fmt = - \\Usage: zig fmt [file]... - \\ - \\ Formats the input files and modifies them in-place. - \\ Arguments can be files or directories, which are searched - \\ recursively. - \\ - \\Options: - \\ -h, --help Print this help and exit - \\ --color [auto|off|on] Enable or disable colored error messages - \\ --stdin Format code from stdin; output to stdout - \\ --check List non-conforming files and exit with an error - \\ if the list is non-empty - \\ --ast-check Run zig ast-check on every file - \\ --exclude [file] Exclude file or directory from formatting - \\ - \\ -; - -const Fmt = struct { - seen: SeenMap, - any_error: bool, - check_ast: bool, - color: Color, - gpa: Allocator, - arena: Allocator, - out_buffer: std.ArrayList(u8), - - const SeenMap = std.AutoHashMap(fs.File.INode, void); -}; - -pub fn main() !void { - var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); - defer arena_instance.deinit(); - const arena = arena_instance.allocator(); - const gpa = arena; - - const args = try process.argsAlloc(arena); - - var color: Color = .auto; - var stdin_flag: bool = false; - var check_flag: bool = false; - var check_ast_flag: bool = false; - var input_files = std.ArrayList([]const u8).init(gpa); - defer input_files.deinit(); - var excluded_files = std.ArrayList([]const u8).init(gpa); - defer excluded_files.deinit(); - - { - var i: usize = 1; - while (i < args.len) : (i += 1) { - const arg = args[i]; - if (mem.startsWith(u8, arg, "-")) { - if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { - const stdout = std.io.getStdOut().writer(); - try stdout.writeAll(usage_fmt); - return process.cleanExit(); - } else if (mem.eql(u8, arg, "--color")) { - if (i + 1 >= args.len) { - fatal("expected [auto|on|off] after --color", .{}); - } - i += 1; - const next_arg = args[i]; - color = std.meta.stringToEnum(Color, next_arg) orelse { - fatal("expected [auto|on|off] after --color, found '{s}'", .{next_arg}); - }; - } else if (mem.eql(u8, arg, "--stdin")) { - stdin_flag = true; - } else if (mem.eql(u8, arg, "--check")) { - check_flag = true; - } else if (mem.eql(u8, arg, "--ast-check")) { - check_ast_flag = true; - } else if (mem.eql(u8, arg, "--exclude")) { - if (i + 1 >= args.len) { - fatal("expected parameter after --exclude", .{}); - } - i += 1; - const next_arg = args[i]; - try excluded_files.append(next_arg); - } else { - fatal("unrecognized parameter: '{s}'", .{arg}); - } - } else { - try input_files.append(arg); - } - } - } - - if (stdin_flag) { - if (input_files.items.len != 0) { - fatal("cannot use --stdin with positional arguments", .{}); - } - - const stdin = std.io.getStdIn(); - const source_code = std.zig.readSourceFileToEndAlloc(gpa, stdin, null) catch |err| { - fatal("unable to read stdin: {}", .{err}); - }; - defer gpa.free(source_code); - - var tree = std.zig.Ast.parse(gpa, source_code, .zig) catch |err| { - fatal("error parsing stdin: {}", .{err}); - }; - defer tree.deinit(gpa); - - if (check_ast_flag) { - var zir = try std.zig.AstGen.generate(gpa, tree); - - if (zir.hasCompileErrors()) { - var wip_errors: std.zig.ErrorBundle.Wip = undefined; - try wip_errors.init(gpa); - defer wip_errors.deinit(); - try wip_errors.addZirErrorMessages(zir, tree, source_code, ""); - var error_bundle = try wip_errors.toOwnedBundle(""); - defer error_bundle.deinit(gpa); - error_bundle.renderToStdErr(color.renderOptions()); - process.exit(2); - } - } else if (tree.errors.len != 0) { - try std.zig.printAstErrorsToStderr(gpa, tree, "", color); - process.exit(2); - } - const formatted = try tree.render(gpa); - defer gpa.free(formatted); - - if (check_flag) { - const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code)); - process.exit(code); - } - - return std.io.getStdOut().writeAll(formatted); - } - - if (input_files.items.len == 0) { - fatal("expected at least one source file argument", .{}); - } - - var fmt = Fmt{ - .gpa = gpa, - .arena = arena, - .seen = Fmt.SeenMap.init(gpa), - .any_error = false, - .check_ast = check_ast_flag, - .color = color, - .out_buffer = std.ArrayList(u8).init(gpa), - }; - defer fmt.seen.deinit(); - defer fmt.out_buffer.deinit(); - - // Mark any excluded files/directories as already seen, - // so that they are skipped later during actual processing - for (excluded_files.items) |file_path| { - const stat = fs.cwd().statFile(file_path) catch |err| switch (err) { - error.FileNotFound => continue, - // On Windows, statFile does not work for directories - error.IsDir => dir: { - var dir = try fs.cwd().openDir(file_path, .{}); - defer dir.close(); - break :dir try dir.stat(); - }, - else => |e| return e, - }; - try fmt.seen.put(stat.inode, {}); - } - - for (input_files.items) |file_path| { - try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path); - } - if (fmt.any_error) { - process.exit(1); - } -} - -const FmtError = error{ - SystemResources, - OperationAborted, - IoPending, - BrokenPipe, - Unexpected, - WouldBlock, - FileClosed, - DestinationAddressRequired, - DiskQuota, - FileTooBig, - InputOutput, - NoSpaceLeft, - AccessDenied, - OutOfMemory, - RenameAcrossMountPoints, - ReadOnlyFileSystem, - LinkQuotaExceeded, - FileBusy, - EndOfStream, - Unseekable, - NotOpenForWriting, - UnsupportedEncoding, - ConnectionResetByPeer, - SocketNotConnected, - LockViolation, - NetNameDeleted, - InvalidArgument, -} || fs.File.OpenError; - -fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void { - fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) { - error.IsDir, error.AccessDenied => return fmtPathDir(fmt, file_path, check_mode, dir, sub_path), - else => { - warn("unable to format '{s}': {s}", .{ file_path, @errorName(err) }); - fmt.any_error = true; - return; - }, - }; -} - -fn fmtPathDir( - fmt: *Fmt, - file_path: []const u8, - check_mode: bool, - parent_dir: fs.Dir, - parent_sub_path: []const u8, -) FmtError!void { - var dir = try parent_dir.openDir(parent_sub_path, .{ .iterate = true }); - defer dir.close(); - - const stat = try dir.stat(); - if (try fmt.seen.fetchPut(stat.inode, {})) |_| return; - - var dir_it = dir.iterate(); - while (try dir_it.next()) |entry| { - const is_dir = entry.kind == .directory; - - if (is_dir and (mem.eql(u8, entry.name, "zig-cache") or mem.eql(u8, entry.name, "zig-out"))) continue; - - if (is_dir or entry.kind == .file and (mem.endsWith(u8, entry.name, ".zig") or mem.endsWith(u8, entry.name, ".zon"))) { - const full_path = try fs.path.join(fmt.gpa, &[_][]const u8{ file_path, entry.name }); - defer fmt.gpa.free(full_path); - - if (is_dir) { - try fmtPathDir(fmt, full_path, check_mode, dir, entry.name); - } else { - fmtPathFile(fmt, full_path, check_mode, dir, entry.name) catch |err| { - warn("unable to format '{s}': {s}", .{ full_path, @errorName(err) }); - fmt.any_error = true; - return; - }; - } - } - } -} - -fn fmtPathFile( - fmt: *Fmt, - file_path: []const u8, - check_mode: bool, - dir: fs.Dir, - sub_path: []const u8, -) FmtError!void { - const source_file = try dir.openFile(sub_path, .{}); - var file_closed = false; - errdefer if (!file_closed) source_file.close(); - - const stat = try source_file.stat(); - - if (stat.kind == .directory) - return error.IsDir; - - const gpa = fmt.gpa; - const source_code = try std.zig.readSourceFileToEndAlloc( - gpa, - source_file, - std.math.cast(usize, stat.size) orelse return error.FileTooBig, - ); - defer gpa.free(source_code); - - source_file.close(); - file_closed = true; - - // Add to set after no longer possible to get error.IsDir. - if (try fmt.seen.fetchPut(stat.inode, {})) |_| return; - - var tree = try std.zig.Ast.parse(gpa, source_code, .zig); - defer tree.deinit(gpa); - - if (tree.errors.len != 0) { - try std.zig.printAstErrorsToStderr(gpa, tree, file_path, fmt.color); - fmt.any_error = true; - return; - } - - if (fmt.check_ast) { - if (stat.size > std.zig.max_src_size) - return error.FileTooBig; - - var zir = try std.zig.AstGen.generate(gpa, tree); - defer zir.deinit(gpa); - - if (zir.hasCompileErrors()) { - var wip_errors: std.zig.ErrorBundle.Wip = undefined; - try wip_errors.init(gpa); - defer wip_errors.deinit(); - try wip_errors.addZirErrorMessages(zir, tree, source_code, file_path); - var error_bundle = try wip_errors.toOwnedBundle(""); - defer error_bundle.deinit(gpa); - error_bundle.renderToStdErr(fmt.color.renderOptions()); - fmt.any_error = true; - } - } - - // As a heuristic, we make enough capacity for the same as the input source. - fmt.out_buffer.shrinkRetainingCapacity(0); - try fmt.out_buffer.ensureTotalCapacity(source_code.len); - - try tree.renderToArrayList(&fmt.out_buffer, .{}); - if (mem.eql(u8, fmt.out_buffer.items, source_code)) - return; - - if (check_mode) { - const stdout = std.io.getStdOut().writer(); - try stdout.print("{s}\n", .{file_path}); - fmt.any_error = true; - } else { - var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode }); - defer af.deinit(); - - try af.file.writeAll(fmt.out_buffer.items); - try af.finish(); - const stdout = std.io.getStdOut().writer(); - try stdout.print("{s}\n", .{file_path}); - } -} - -fn fatal(comptime format: []const u8, args: anytype) noreturn { - std.log.err(format, args); - process.exit(1); -} diff --git a/lib/std/zig/reduce.zig b/lib/std/zig/reduce.zig deleted file mode 100644 index 1b40856ffe557fa77b366a8033778e18922cb064..0000000000000000000000000000000000000000 --- a/lib/std/zig/reduce.zig +++ /dev/null @@ -1,426 +0,0 @@ -const std = @import("std"); -const mem = std.mem; -const Allocator = std.mem.Allocator; -const assert = std.debug.assert; -const Ast = std.zig.Ast; -const Walk = @import("reduce/Walk.zig"); -const AstGen = std.zig.AstGen; -const Zir = std.zig.Zir; - -const usage = - \\zig reduce [options] ./checker root_source_file.zig [-- [argv]] - \\ - \\root_source_file.zig is relative to --main-mod-path. - \\ - \\checker: - \\ An executable that communicates interestingness by returning these exit codes: - \\ exit(0): interesting - \\ exit(1): unknown (infinite loop or other mishap) - \\ exit(other): not interesting - \\ - \\options: - \\ --seed [integer] Override the random seed. Defaults to 0 - \\ --skip-smoke-test Skip interestingness check smoke test - \\ --mod [name]:[deps]:[src] Make a module available for dependency under the given name - \\ deps: [dep],[dep],... - \\ dep: [[import=]name] - \\ --deps [dep],[dep],... Set dependency names for the root package - \\ dep: [[import=]name] - \\ --main-mod-path Set the directory of the root module - \\ - \\argv: - \\ Forwarded directly to the interestingness script. - \\ -; - -const Interestingness = enum { interesting, unknown, boring }; - -// Roadmap: -// - add thread pool -// - add support for parsing the module flags -// - more fancy transformations -// - @import inlining of modules -// - removing statements or blocks of code -// - replacing operands of `and` and `or` with `true` and `false` -// - replacing if conditions with `true` and `false` -// - reduce flags sent to the compiler -// - integrate with the build system? - -pub fn main() !void { - var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); - defer arena_instance.deinit(); - const arena = arena_instance.allocator(); - - var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .{}; - const gpa = general_purpose_allocator.allocator(); - - const args = try std.process.argsAlloc(arena); - - var opt_checker_path: ?[]const u8 = null; - var opt_root_source_file_path: ?[]const u8 = null; - var argv: []const []const u8 = &.{}; - var seed: u32 = 0; - var skip_smoke_test = false; - - { - var i: usize = 1; - while (i < args.len) : (i += 1) { - const arg = args[i]; - if (mem.startsWith(u8, arg, "-")) { - if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { - const stdout = std.io.getStdOut().writer(); - try stdout.writeAll(usage); - return std.process.cleanExit(); - } else if (mem.eql(u8, arg, "--")) { - argv = args[i + 1 ..]; - break; - } else if (mem.eql(u8, arg, "--skip-smoke-test")) { - skip_smoke_test = true; - } else if (mem.eql(u8, arg, "--main-mod-path")) { - @panic("TODO: implement --main-mod-path"); - } else if (mem.eql(u8, arg, "--mod")) { - @panic("TODO: implement --mod"); - } else if (mem.eql(u8, arg, "--deps")) { - @panic("TODO: implement --deps"); - } else if (mem.eql(u8, arg, "--seed")) { - i += 1; - if (i >= args.len) fatal("expected 32-bit integer after {s}", .{arg}); - const next_arg = args[i]; - seed = std.fmt.parseUnsigned(u32, next_arg, 0) catch |err| { - fatal("unable to parse seed '{s}' as 32-bit integer: {s}", .{ - next_arg, @errorName(err), - }); - }; - } else { - fatal("unrecognized parameter: '{s}'", .{arg}); - } - } else if (opt_checker_path == null) { - opt_checker_path = arg; - } else if (opt_root_source_file_path == null) { - opt_root_source_file_path = arg; - } else { - fatal("unexpected extra parameter: '{s}'", .{arg}); - } - } - } - - const checker_path = opt_checker_path orelse - fatal("missing interestingness checker argument; see -h for usage", .{}); - const root_source_file_path = opt_root_source_file_path orelse - fatal("missing root source file path argument; see -h for usage", .{}); - - var interestingness_argv: std.ArrayListUnmanaged([]const u8) = .{}; - try interestingness_argv.ensureUnusedCapacity(arena, argv.len + 1); - interestingness_argv.appendAssumeCapacity(checker_path); - interestingness_argv.appendSliceAssumeCapacity(argv); - - var rendered = std.ArrayList(u8).init(gpa); - defer rendered.deinit(); - - var astgen_input = std.ArrayList(u8).init(gpa); - defer astgen_input.deinit(); - - var tree = try parse(gpa, root_source_file_path); - defer { - gpa.free(tree.source); - tree.deinit(gpa); - } - - if (!skip_smoke_test) { - std.debug.print("smoke testing the interestingness check...\n", .{}); - switch (try runCheck(arena, interestingness_argv.items)) { - .interesting => {}, - .boring, .unknown => |t| { - fatal("interestingness check returned {s} for unmodified input\n", .{ - @tagName(t), - }); - }, - } - } - - var fixups: Ast.Fixups = .{}; - defer fixups.deinit(gpa); - - var more_fixups: Ast.Fixups = .{}; - defer more_fixups.deinit(gpa); - - var rng = std.Random.DefaultPrng.init(seed); - - // 1. Walk the AST of the source file looking for independent - // reductions and collecting them all into an array list. - // 2. Randomize the list of transformations. A future enhancement will add - // priority weights to the sorting but for now they are completely - // shuffled. - // 3. Apply a subset consisting of 1/2 of the transformations and check for - // interestingness. - // 4. If not interesting, half the subset size again and check again. - // 5. Repeat until the subset size is 1, then march the transformation - // index forward by 1 with each non-interesting attempt. - // - // At any point if a subset of transformations succeeds in producing an interesting - // result, restart the whole process, reparsing the AST and re-generating the list - // of all possible transformations and shuffling it again. - - var transformations = std.ArrayList(Walk.Transformation).init(gpa); - defer transformations.deinit(); - try Walk.findTransformations(arena, &tree, &transformations); - sortTransformations(transformations.items, rng.random()); - - fresh: while (transformations.items.len > 0) { - std.debug.print("found {d} possible transformations\n", .{ - transformations.items.len, - }); - var subset_size: usize = transformations.items.len; - var start_index: usize = 0; - - while (start_index < transformations.items.len) { - const prev_subset_size = subset_size; - subset_size = @max(1, subset_size * 3 / 4); - if (prev_subset_size > 1 and subset_size == 1) - start_index = 0; - - const this_set = transformations.items[start_index..][0..subset_size]; - std.debug.print("trying {d} random transformations: ", .{subset_size}); - for (this_set[0..@min(this_set.len, 20)]) |t| { - std.debug.print("{s} ", .{@tagName(t)}); - } - std.debug.print("\n", .{}); - try transformationsToFixups(gpa, arena, root_source_file_path, this_set, &fixups); - - rendered.clearRetainingCapacity(); - try tree.renderToArrayList(&rendered, fixups); - - // The transformations we applied may have resulted in unused locals, - // in which case we would like to add the respective discards. - { - try astgen_input.resize(rendered.items.len); - @memcpy(astgen_input.items, rendered.items); - try astgen_input.append(0); - const source_with_null = astgen_input.items[0 .. astgen_input.items.len - 1 :0]; - var astgen_tree = try Ast.parse(gpa, source_with_null, .zig); - defer astgen_tree.deinit(gpa); - if (astgen_tree.errors.len != 0) { - @panic("syntax errors occurred"); - } - var zir = try AstGen.generate(gpa, astgen_tree); - defer zir.deinit(gpa); - - if (zir.hasCompileErrors()) { - more_fixups.clearRetainingCapacity(); - const payload_index = zir.extra[@intFromEnum(Zir.ExtraIndex.compile_errors)]; - assert(payload_index != 0); - const header = zir.extraData(Zir.Inst.CompileErrors, payload_index); - var extra_index = header.end; - for (0..header.data.items_len) |_| { - const item = zir.extraData(Zir.Inst.CompileErrors.Item, extra_index); - extra_index = item.end; - const msg = zir.nullTerminatedString(item.data.msg); - if (mem.eql(u8, msg, "unused local constant") or - mem.eql(u8, msg, "unused local variable") or - mem.eql(u8, msg, "unused function parameter") or - mem.eql(u8, msg, "unused capture")) - { - const ident_token = item.data.token; - try more_fixups.unused_var_decls.put(gpa, ident_token, {}); - } else { - std.debug.print("found other ZIR error: '{s}'\n", .{msg}); - } - } - if (more_fixups.count() != 0) { - rendered.clearRetainingCapacity(); - try astgen_tree.renderToArrayList(&rendered, more_fixups); - } - } - } - - try std.fs.cwd().writeFile(root_source_file_path, rendered.items); - // std.debug.print("trying this code:\n{s}\n", .{rendered.items}); - - const interestingness = try runCheck(arena, interestingness_argv.items); - std.debug.print("{d} random transformations: {s}. {d}/{d}\n", .{ - subset_size, @tagName(interestingness), start_index, transformations.items.len, - }); - switch (interestingness) { - .interesting => { - const new_tree = try parse(gpa, root_source_file_path); - gpa.free(tree.source); - tree.deinit(gpa); - tree = new_tree; - - try Walk.findTransformations(arena, &tree, &transformations); - sortTransformations(transformations.items, rng.random()); - - continue :fresh; - }, - .unknown, .boring => { - // Continue to try the next set of transformations. - // If we tested only one transformation, move on to the next one. - if (subset_size == 1) { - start_index += 1; - } else { - start_index += subset_size; - if (start_index + subset_size > transformations.items.len) { - start_index = 0; - } - } - }, - } - } - std.debug.print("all {d} remaining transformations are uninteresting\n", .{ - transformations.items.len, - }); - - // Revert the source back to not be transformed. - fixups.clearRetainingCapacity(); - rendered.clearRetainingCapacity(); - try tree.renderToArrayList(&rendered, fixups); - try std.fs.cwd().writeFile(root_source_file_path, rendered.items); - - return std.process.cleanExit(); - } - std.debug.print("no more transformations found\n", .{}); - return std.process.cleanExit(); -} - -fn sortTransformations(transformations: []Walk.Transformation, rng: std.Random) void { - rng.shuffle(Walk.Transformation, transformations); - // Stable sort based on priority to keep randomness as the secondary sort. - // TODO: introduce transformation priorities - // std.mem.sort(transformations); -} - -fn termToInteresting(term: std.process.Child.Term) Interestingness { - return switch (term) { - .Exited => |code| switch (code) { - 0 => .interesting, - 1 => .unknown, - else => .boring, - }, - else => b: { - std.debug.print("interestingness check aborted unexpectedly\n", .{}); - break :b .boring; - }, - }; -} - -fn runCheck(arena: std.mem.Allocator, argv: []const []const u8) !Interestingness { - const result = try std.process.Child.run(.{ - .allocator = arena, - .argv = argv, - }); - if (result.stderr.len != 0) - std.debug.print("{s}", .{result.stderr}); - return termToInteresting(result.term); -} - -fn transformationsToFixups( - gpa: Allocator, - arena: Allocator, - root_source_file_path: []const u8, - transforms: []const Walk.Transformation, - fixups: *Ast.Fixups, -) !void { - fixups.clearRetainingCapacity(); - - for (transforms) |t| switch (t) { - .gut_function => |fn_decl_node| { - try fixups.gut_functions.put(gpa, fn_decl_node, {}); - }, - .delete_node => |decl_node| { - try fixups.omit_nodes.put(gpa, decl_node, {}); - }, - .delete_var_decl => |delete_var_decl| { - try fixups.omit_nodes.put(gpa, delete_var_decl.var_decl_node, {}); - for (delete_var_decl.references.items) |ident_node| { - try fixups.replace_nodes_with_string.put(gpa, ident_node, "undefined"); - } - }, - .replace_with_undef => |node| { - try fixups.replace_nodes_with_string.put(gpa, node, "undefined"); - }, - .replace_with_true => |node| { - try fixups.replace_nodes_with_string.put(gpa, node, "true"); - }, - .replace_with_false => |node| { - try fixups.replace_nodes_with_string.put(gpa, node, "false"); - }, - .replace_node => |r| { - try fixups.replace_nodes_with_node.put(gpa, r.to_replace, r.replacement); - }, - .inline_imported_file => |inline_imported_file| { - const full_imported_path = try std.fs.path.join(gpa, &.{ - std.fs.path.dirname(root_source_file_path) orelse ".", - inline_imported_file.imported_string, - }); - defer gpa.free(full_imported_path); - var other_file_ast = try parse(gpa, full_imported_path); - defer { - gpa.free(other_file_ast.source); - other_file_ast.deinit(gpa); - } - - var inlined_fixups: Ast.Fixups = .{}; - defer inlined_fixups.deinit(gpa); - if (std.fs.path.dirname(inline_imported_file.imported_string)) |dirname| { - inlined_fixups.rebase_imported_paths = dirname; - } - for (inline_imported_file.in_scope_names.keys()) |name| { - // This name needs to be mangled in order to not cause an - // ambiguous reference error. - var i: u32 = 2; - const mangled = while (true) : (i += 1) { - const mangled = try std.fmt.allocPrint(gpa, "{s}{d}", .{ name, i }); - if (!inline_imported_file.in_scope_names.contains(mangled)) - break mangled; - gpa.free(mangled); - }; - try inlined_fixups.rename_identifiers.put(gpa, name, mangled); - } - defer { - for (inlined_fixups.rename_identifiers.values()) |v| { - gpa.free(v); - } - } - - var other_source = std.ArrayList(u8).init(gpa); - defer other_source.deinit(); - try other_source.appendSlice("struct {\n"); - try other_file_ast.renderToArrayList(&other_source, inlined_fixups); - try other_source.appendSlice("}"); - - try fixups.replace_nodes_with_string.put( - gpa, - inline_imported_file.builtin_call_node, - try arena.dupe(u8, other_source.items), - ); - }, - }; -} - -fn parse(gpa: Allocator, file_path: []const u8) !Ast { - const source_code = std.fs.cwd().readFileAllocOptions( - gpa, - file_path, - std.math.maxInt(u32), - null, - 1, - 0, - ) catch |err| { - fatal("unable to open '{s}': {s}", .{ file_path, @errorName(err) }); - }; - errdefer gpa.free(source_code); - - var tree = try Ast.parse(gpa, source_code, .zig); - errdefer tree.deinit(gpa); - - if (tree.errors.len != 0) { - @panic("syntax errors occurred"); - } - - return tree; -} - -fn fatal(comptime format: []const u8, args: anytype) noreturn { - std.log.err(format, args); - std.process.exit(1); -} diff --git a/lib/std/zig/reduce/Walk.zig b/lib/std/zig/reduce/Walk.zig deleted file mode 100644 index 572243d82970c3dc3cfe5a1f26db279a13bba559..0000000000000000000000000000000000000000 --- a/lib/std/zig/reduce/Walk.zig +++ /dev/null @@ -1,1102 +0,0 @@ -const std = @import("std"); -const Ast = std.zig.Ast; -const Walk = @This(); -const assert = std.debug.assert; -const BuiltinFn = std.zig.BuiltinFn; - -ast: *const Ast, -transformations: *std.ArrayList(Transformation), -unreferenced_globals: std.StringArrayHashMapUnmanaged(Ast.Node.Index), -in_scope_names: std.StringArrayHashMapUnmanaged(u32), -replace_names: std.StringArrayHashMapUnmanaged(u32), -gpa: std.mem.Allocator, -arena: std.mem.Allocator, - -pub const Transformation = union(enum) { - /// Replace the fn decl AST Node with one whose body is only `@trap()` with - /// discarded parameters. - gut_function: Ast.Node.Index, - /// Omit a global declaration. - delete_node: Ast.Node.Index, - /// Delete a local variable declaration and replace all of its references - /// with `undefined`. - delete_var_decl: struct { - var_decl_node: Ast.Node.Index, - /// Identifier nodes that reference the variable. - references: std.ArrayListUnmanaged(Ast.Node.Index), - }, - /// Replace an expression with `undefined`. - replace_with_undef: Ast.Node.Index, - /// Replace an expression with `true`. - replace_with_true: Ast.Node.Index, - /// Replace an expression with `false`. - replace_with_false: Ast.Node.Index, - /// Replace a node with another node. - replace_node: struct { - to_replace: Ast.Node.Index, - replacement: Ast.Node.Index, - }, - /// Replace an `@import` with the imported file contents wrapped in a struct. - inline_imported_file: InlineImportedFile, - - pub const InlineImportedFile = struct { - builtin_call_node: Ast.Node.Index, - imported_string: []const u8, - /// Identifier names that must be renamed in the inlined code or else - /// will cause ambiguous reference errors. - in_scope_names: std.StringArrayHashMapUnmanaged(void), - }; -}; - -pub const Error = error{OutOfMemory}; - -/// The result will be priority shuffled. -pub fn findTransformations( - arena: std.mem.Allocator, - ast: *const Ast, - transformations: *std.ArrayList(Transformation), -) !void { - transformations.clearRetainingCapacity(); - - var walk: Walk = .{ - .ast = ast, - .transformations = transformations, - .gpa = transformations.allocator, - .arena = arena, - .unreferenced_globals = .{}, - .in_scope_names = .{}, - .replace_names = .{}, - }; - defer { - walk.unreferenced_globals.deinit(walk.gpa); - walk.in_scope_names.deinit(walk.gpa); - walk.replace_names.deinit(walk.gpa); - } - - try walkMembers(&walk, walk.ast.rootDecls()); - - const unreferenced_globals = walk.unreferenced_globals.values(); - try transformations.ensureUnusedCapacity(unreferenced_globals.len); - for (unreferenced_globals) |node| { - transformations.appendAssumeCapacity(.{ .delete_node = node }); - } -} - -fn walkMembers(w: *Walk, members: []const Ast.Node.Index) Error!void { - // First we scan for globals so that we can delete them while walking. - try scanDecls(w, members, .add); - - for (members) |member| { - try walkMember(w, member); - } - - try scanDecls(w, members, .remove); -} - -const ScanDeclsAction = enum { add, remove }; - -fn scanDecls(w: *Walk, members: []const Ast.Node.Index, action: ScanDeclsAction) Error!void { - const ast = w.ast; - const gpa = w.gpa; - const node_tags = ast.nodes.items(.tag); - const main_tokens = ast.nodes.items(.main_token); - const token_tags = ast.tokens.items(.tag); - - for (members) |member_node| { - const name_token = switch (node_tags[member_node]) { - .global_var_decl, - .local_var_decl, - .simple_var_decl, - .aligned_var_decl, - => main_tokens[member_node] + 1, - - .fn_proto_simple, - .fn_proto_multi, - .fn_proto_one, - .fn_proto, - .fn_decl, - => main_tokens[member_node] + 1, - - else => continue, - }; - - assert(token_tags[name_token] == .identifier); - const name_bytes = ast.tokenSlice(name_token); - - switch (action) { - .add => { - try w.unreferenced_globals.put(gpa, name_bytes, member_node); - - const gop = try w.in_scope_names.getOrPut(gpa, name_bytes); - if (!gop.found_existing) gop.value_ptr.* = 0; - gop.value_ptr.* += 1; - }, - .remove => { - const entry = w.in_scope_names.getEntry(name_bytes).?; - if (entry.value_ptr.* <= 1) { - assert(w.in_scope_names.swapRemove(name_bytes)); - } else { - entry.value_ptr.* -= 1; - } - }, - } - } -} - -fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { - const ast = w.ast; - const datas = ast.nodes.items(.data); - switch (ast.nodes.items(.tag)[decl]) { - .fn_decl => { - const fn_proto = datas[decl].lhs; - try walkExpression(w, fn_proto); - const body_node = datas[decl].rhs; - if (!isFnBodyGutted(ast, body_node)) { - w.replace_names.clearRetainingCapacity(); - try w.transformations.append(.{ .gut_function = decl }); - try walkExpression(w, body_node); - } - }, - .fn_proto_simple, - .fn_proto_multi, - .fn_proto_one, - .fn_proto, - => { - try walkExpression(w, decl); - }, - - .@"usingnamespace" => { - try w.transformations.append(.{ .delete_node = decl }); - const expr = datas[decl].lhs; - try walkExpression(w, expr); - }, - - .global_var_decl, - .local_var_decl, - .simple_var_decl, - .aligned_var_decl, - => try walkGlobalVarDecl(w, decl, ast.fullVarDecl(decl).?), - - .test_decl => { - try w.transformations.append(.{ .delete_node = decl }); - try walkExpression(w, datas[decl].rhs); - }, - - .container_field_init, - .container_field_align, - .container_field, - => { - try w.transformations.append(.{ .delete_node = decl }); - try walkContainerField(w, ast.fullContainerField(decl).?); - }, - - .@"comptime" => { - try w.transformations.append(.{ .delete_node = decl }); - try walkExpression(w, decl); - }, - - .root => unreachable, - else => unreachable, - } -} - -fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void { - const ast = w.ast; - const token_tags = ast.tokens.items(.tag); - const main_tokens = ast.nodes.items(.main_token); - const node_tags = ast.nodes.items(.tag); - const datas = ast.nodes.items(.data); - switch (node_tags[node]) { - .identifier => { - const name_ident = main_tokens[node]; - assert(token_tags[name_ident] == .identifier); - const name_bytes = ast.tokenSlice(name_ident); - _ = w.unreferenced_globals.swapRemove(name_bytes); - if (w.replace_names.get(name_bytes)) |index| { - try w.transformations.items[index].delete_var_decl.references.append(w.arena, node); - } - }, - - .number_literal, - .char_literal, - .unreachable_literal, - .anyframe_literal, - .string_literal, - => {}, - - .multiline_string_literal => {}, - - .error_value => {}, - - .block_two, - .block_two_semicolon, - => { - const statements = [2]Ast.Node.Index{ datas[node].lhs, datas[node].rhs }; - if (datas[node].lhs == 0) { - return walkBlock(w, node, statements[0..0]); - } else if (datas[node].rhs == 0) { - return walkBlock(w, node, statements[0..1]); - } else { - return walkBlock(w, node, statements[0..2]); - } - }, - .block, - .block_semicolon, - => { - const statements = ast.extra_data[datas[node].lhs..datas[node].rhs]; - return walkBlock(w, node, statements); - }, - - .@"errdefer" => { - const expr = datas[node].rhs; - return walkExpression(w, expr); - }, - - .@"defer" => { - const expr = datas[node].rhs; - return walkExpression(w, expr); - }, - .@"comptime", .@"nosuspend" => { - const block = datas[node].lhs; - return walkExpression(w, block); - }, - - .@"suspend" => { - const body = datas[node].lhs; - return walkExpression(w, body); - }, - - .@"catch" => { - try walkExpression(w, datas[node].lhs); // target - try walkExpression(w, datas[node].rhs); // fallback - }, - - .field_access => { - const field_access = datas[node]; - try walkExpression(w, field_access.lhs); - }, - - .error_union, - .switch_range, - => { - const infix = datas[node]; - try walkExpression(w, infix.lhs); - return walkExpression(w, infix.rhs); - }, - .for_range => { - const infix = datas[node]; - try walkExpression(w, infix.lhs); - if (infix.rhs != 0) { - return walkExpression(w, infix.rhs); - } - }, - - .add, - .add_wrap, - .add_sat, - .array_cat, - .array_mult, - .assign, - .assign_bit_and, - .assign_bit_or, - .assign_shl, - .assign_shl_sat, - .assign_shr, - .assign_bit_xor, - .assign_div, - .assign_sub, - .assign_sub_wrap, - .assign_sub_sat, - .assign_mod, - .assign_add, - .assign_add_wrap, - .assign_add_sat, - .assign_mul, - .assign_mul_wrap, - .assign_mul_sat, - .bang_equal, - .bit_and, - .bit_or, - .shl, - .shl_sat, - .shr, - .bit_xor, - .bool_and, - .bool_or, - .div, - .equal_equal, - .greater_or_equal, - .greater_than, - .less_or_equal, - .less_than, - .merge_error_sets, - .mod, - .mul, - .mul_wrap, - .mul_sat, - .sub, - .sub_wrap, - .sub_sat, - .@"orelse", - => { - const infix = datas[node]; - try walkExpression(w, infix.lhs); - try walkExpression(w, infix.rhs); - }, - - .assign_destructure => { - const lhs_count = ast.extra_data[datas[node].lhs]; - assert(lhs_count > 1); - const lhs_exprs = ast.extra_data[datas[node].lhs + 1 ..][0..lhs_count]; - const rhs = datas[node].rhs; - - for (lhs_exprs) |lhs_node| { - switch (node_tags[lhs_node]) { - .global_var_decl, - .local_var_decl, - .simple_var_decl, - .aligned_var_decl, - => try walkLocalVarDecl(w, ast.fullVarDecl(lhs_node).?), - - else => try walkExpression(w, lhs_node), - } - } - return walkExpression(w, rhs); - }, - - .bit_not, - .bool_not, - .negation, - .negation_wrap, - .optional_type, - .address_of, - => { - return walkExpression(w, datas[node].lhs); - }, - - .@"try", - .@"resume", - .@"await", - => { - return walkExpression(w, datas[node].lhs); - }, - - .array_type, - .array_type_sentinel, - => {}, - - .ptr_type_aligned, - .ptr_type_sentinel, - .ptr_type, - .ptr_type_bit_range, - => {}, - - .array_init_one, - .array_init_one_comma, - .array_init_dot_two, - .array_init_dot_two_comma, - .array_init_dot, - .array_init_dot_comma, - .array_init, - .array_init_comma, - => { - var elements: [2]Ast.Node.Index = undefined; - return walkArrayInit(w, ast.fullArrayInit(&elements, node).?); - }, - - .struct_init_one, - .struct_init_one_comma, - .struct_init_dot_two, - .struct_init_dot_two_comma, - .struct_init_dot, - .struct_init_dot_comma, - .struct_init, - .struct_init_comma, - => { - var buf: [2]Ast.Node.Index = undefined; - return walkStructInit(w, node, ast.fullStructInit(&buf, node).?); - }, - - .call_one, - .call_one_comma, - .async_call_one, - .async_call_one_comma, - .call, - .call_comma, - .async_call, - .async_call_comma, - => { - var buf: [1]Ast.Node.Index = undefined; - return walkCall(w, ast.fullCall(&buf, node).?); - }, - - .array_access => { - const suffix = datas[node]; - try walkExpression(w, suffix.lhs); - try walkExpression(w, suffix.rhs); - }, - - .slice_open, .slice, .slice_sentinel => return walkSlice(w, node, ast.fullSlice(node).?), - - .deref => { - try walkExpression(w, datas[node].lhs); - }, - - .unwrap_optional => { - try walkExpression(w, datas[node].lhs); - }, - - .@"break" => { - const label_token = datas[node].lhs; - const target = datas[node].rhs; - if (label_token == 0 and target == 0) { - // no expressions - } else if (label_token == 0 and target != 0) { - try walkExpression(w, target); - } else if (label_token != 0 and target == 0) { - try walkIdentifier(w, label_token); - } else if (label_token != 0 and target != 0) { - try walkExpression(w, target); - } - }, - - .@"continue" => { - const label = datas[node].lhs; - if (label != 0) { - return walkIdentifier(w, label); // label - } - }, - - .@"return" => { - if (datas[node].lhs != 0) { - try walkExpression(w, datas[node].lhs); - } - }, - - .grouped_expression => { - try walkExpression(w, datas[node].lhs); - }, - - .container_decl, - .container_decl_trailing, - .container_decl_arg, - .container_decl_arg_trailing, - .container_decl_two, - .container_decl_two_trailing, - .tagged_union, - .tagged_union_trailing, - .tagged_union_enum_tag, - .tagged_union_enum_tag_trailing, - .tagged_union_two, - .tagged_union_two_trailing, - => { - var buf: [2]Ast.Node.Index = undefined; - return walkContainerDecl(w, node, ast.fullContainerDecl(&buf, node).?); - }, - - .error_set_decl => { - const error_token = main_tokens[node]; - const lbrace = error_token + 1; - const rbrace = datas[node].rhs; - - var i = lbrace + 1; - while (i < rbrace) : (i += 1) { - switch (token_tags[i]) { - .doc_comment => unreachable, // TODO - .identifier => try walkIdentifier(w, i), - .comma => {}, - else => unreachable, - } - } - }, - - .builtin_call_two, .builtin_call_two_comma => { - if (datas[node].lhs == 0) { - return walkBuiltinCall(w, node, &.{}); - } else if (datas[node].rhs == 0) { - return walkBuiltinCall(w, node, &.{datas[node].lhs}); - } else { - return walkBuiltinCall(w, node, &.{ datas[node].lhs, datas[node].rhs }); - } - }, - .builtin_call, .builtin_call_comma => { - const params = ast.extra_data[datas[node].lhs..datas[node].rhs]; - return walkBuiltinCall(w, node, params); - }, - - .fn_proto_simple, - .fn_proto_multi, - .fn_proto_one, - .fn_proto, - => { - var buf: [1]Ast.Node.Index = undefined; - return walkFnProto(w, ast.fullFnProto(&buf, node).?); - }, - - .anyframe_type => { - if (datas[node].rhs != 0) { - return walkExpression(w, datas[node].rhs); - } - }, - - .@"switch", - .switch_comma, - => { - const condition = datas[node].lhs; - const extra = ast.extraData(datas[node].rhs, Ast.Node.SubRange); - const cases = ast.extra_data[extra.start..extra.end]; - - try walkExpression(w, condition); // condition expression - try walkExpressions(w, cases); - }, - - .switch_case_one, - .switch_case_inline_one, - .switch_case, - .switch_case_inline, - => return walkSwitchCase(w, ast.fullSwitchCase(node).?), - - .while_simple, - .while_cont, - .@"while", - => return walkWhile(w, node, ast.fullWhile(node).?), - - .for_simple, - .@"for", - => return walkFor(w, ast.fullFor(node).?), - - .if_simple, - .@"if", - => return walkIf(w, node, ast.fullIf(node).?), - - .asm_simple, - .@"asm", - => return walkAsm(w, ast.fullAsm(node).?), - - .enum_literal => { - return walkIdentifier(w, main_tokens[node]); // name - }, - - .fn_decl => unreachable, - .container_field => unreachable, - .container_field_init => unreachable, - .container_field_align => unreachable, - .root => unreachable, - .global_var_decl => unreachable, - .local_var_decl => unreachable, - .simple_var_decl => unreachable, - .aligned_var_decl => unreachable, - .@"usingnamespace" => unreachable, - .test_decl => unreachable, - .asm_output => unreachable, - .asm_input => unreachable, - } -} - -fn walkGlobalVarDecl(w: *Walk, decl_node: Ast.Node.Index, var_decl: Ast.full.VarDecl) Error!void { - _ = decl_node; - - if (var_decl.ast.type_node != 0) { - try walkExpression(w, var_decl.ast.type_node); - } - - if (var_decl.ast.align_node != 0) { - try walkExpression(w, var_decl.ast.align_node); - } - - if (var_decl.ast.addrspace_node != 0) { - try walkExpression(w, var_decl.ast.addrspace_node); - } - - if (var_decl.ast.section_node != 0) { - try walkExpression(w, var_decl.ast.section_node); - } - - if (var_decl.ast.init_node != 0) { - if (!isUndefinedIdent(w.ast, var_decl.ast.init_node)) { - try w.transformations.append(.{ .replace_with_undef = var_decl.ast.init_node }); - } - try walkExpression(w, var_decl.ast.init_node); - } -} - -fn walkLocalVarDecl(w: *Walk, var_decl: Ast.full.VarDecl) Error!void { - try walkIdentifierNew(w, var_decl.ast.mut_token + 1); // name - - if (var_decl.ast.type_node != 0) { - try walkExpression(w, var_decl.ast.type_node); - } - - if (var_decl.ast.align_node != 0) { - try walkExpression(w, var_decl.ast.align_node); - } - - if (var_decl.ast.addrspace_node != 0) { - try walkExpression(w, var_decl.ast.addrspace_node); - } - - if (var_decl.ast.section_node != 0) { - try walkExpression(w, var_decl.ast.section_node); - } - - if (var_decl.ast.init_node != 0) { - if (!isUndefinedIdent(w.ast, var_decl.ast.init_node)) { - try w.transformations.append(.{ .replace_with_undef = var_decl.ast.init_node }); - } - try walkExpression(w, var_decl.ast.init_node); - } -} - -fn walkContainerField(w: *Walk, field: Ast.full.ContainerField) Error!void { - if (field.ast.type_expr != 0) { - try walkExpression(w, field.ast.type_expr); // type - } - if (field.ast.align_expr != 0) { - try walkExpression(w, field.ast.align_expr); // alignment - } - if (field.ast.value_expr != 0) { - try walkExpression(w, field.ast.value_expr); // value - } -} - -fn walkBlock( - w: *Walk, - block_node: Ast.Node.Index, - statements: []const Ast.Node.Index, -) Error!void { - _ = block_node; - const ast = w.ast; - const node_tags = ast.nodes.items(.tag); - - for (statements) |stmt| { - switch (node_tags[stmt]) { - .global_var_decl, - .local_var_decl, - .simple_var_decl, - .aligned_var_decl, - => { - const var_decl = ast.fullVarDecl(stmt).?; - if (var_decl.ast.init_node != 0 and - isUndefinedIdent(w.ast, var_decl.ast.init_node)) - { - try w.transformations.append(.{ .delete_var_decl = .{ - .var_decl_node = stmt, - .references = .{}, - } }); - const name_tok = var_decl.ast.mut_token + 1; - const name_bytes = ast.tokenSlice(name_tok); - try w.replace_names.put(w.gpa, name_bytes, @intCast(w.transformations.items.len - 1)); - } else { - try walkLocalVarDecl(w, var_decl); - } - }, - - else => { - switch (categorizeStmt(ast, stmt)) { - // Don't try to remove `_ = foo;` discards; those are handled separately. - .discard_identifier => {}, - // definitely try to remove `_ = undefined;` though. - .discard_undefined, .trap_call, .other => { - try w.transformations.append(.{ .delete_node = stmt }); - }, - } - try walkExpression(w, stmt); - }, - } - } -} - -fn walkArrayType(w: *Walk, array_type: Ast.full.ArrayType) Error!void { - try walkExpression(w, array_type.ast.elem_count); - if (array_type.ast.sentinel != 0) { - try walkExpression(w, array_type.ast.sentinel); - } - return walkExpression(w, array_type.ast.elem_type); -} - -fn walkArrayInit(w: *Walk, array_init: Ast.full.ArrayInit) Error!void { - if (array_init.ast.type_expr != 0) { - try walkExpression(w, array_init.ast.type_expr); // T - } - for (array_init.ast.elements) |elem_init| { - try walkExpression(w, elem_init); - } -} - -fn walkStructInit( - w: *Walk, - struct_node: Ast.Node.Index, - struct_init: Ast.full.StructInit, -) Error!void { - _ = struct_node; - if (struct_init.ast.type_expr != 0) { - try walkExpression(w, struct_init.ast.type_expr); // T - } - for (struct_init.ast.fields) |field_init| { - try walkExpression(w, field_init); - } -} - -fn walkCall(w: *Walk, call: Ast.full.Call) Error!void { - try walkExpression(w, call.ast.fn_expr); - try walkParamList(w, call.ast.params); -} - -fn walkSlice( - w: *Walk, - slice_node: Ast.Node.Index, - slice: Ast.full.Slice, -) Error!void { - _ = slice_node; - try walkExpression(w, slice.ast.sliced); - try walkExpression(w, slice.ast.start); - if (slice.ast.end != 0) { - try walkExpression(w, slice.ast.end); - } - if (slice.ast.sentinel != 0) { - try walkExpression(w, slice.ast.sentinel); - } -} - -fn walkIdentifier(w: *Walk, name_ident: Ast.TokenIndex) Error!void { - const ast = w.ast; - const token_tags = ast.tokens.items(.tag); - assert(token_tags[name_ident] == .identifier); - const name_bytes = ast.tokenSlice(name_ident); - _ = w.unreferenced_globals.swapRemove(name_bytes); -} - -fn walkIdentifierNew(w: *Walk, name_ident: Ast.TokenIndex) Error!void { - _ = w; - _ = name_ident; -} - -fn walkContainerDecl( - w: *Walk, - container_decl_node: Ast.Node.Index, - container_decl: Ast.full.ContainerDecl, -) Error!void { - _ = container_decl_node; - if (container_decl.ast.arg != 0) { - try walkExpression(w, container_decl.ast.arg); - } - try walkMembers(w, container_decl.ast.members); -} - -fn walkBuiltinCall( - w: *Walk, - call_node: Ast.Node.Index, - params: []const Ast.Node.Index, -) Error!void { - const ast = w.ast; - const main_tokens = ast.nodes.items(.main_token); - const builtin_token = main_tokens[call_node]; - const builtin_name = ast.tokenSlice(builtin_token); - const info = BuiltinFn.list.get(builtin_name).?; - switch (info.tag) { - .import => { - const operand_node = params[0]; - const str_lit_token = main_tokens[operand_node]; - const token_bytes = ast.tokenSlice(str_lit_token); - if (std.mem.endsWith(u8, token_bytes, ".zig\"")) { - const imported_string = std.zig.string_literal.parseAlloc(w.arena, token_bytes) catch - unreachable; - try w.transformations.append(.{ .inline_imported_file = .{ - .builtin_call_node = call_node, - .imported_string = imported_string, - .in_scope_names = try std.StringArrayHashMapUnmanaged(void).init( - w.arena, - w.in_scope_names.keys(), - &.{}, - ), - } }); - } - }, - else => {}, - } - for (params) |param_node| { - try walkExpression(w, param_node); - } -} - -fn walkFnProto(w: *Walk, fn_proto: Ast.full.FnProto) Error!void { - const ast = w.ast; - - { - var it = fn_proto.iterate(ast); - while (it.next()) |param| { - if (param.type_expr != 0) { - try walkExpression(w, param.type_expr); - } - } - } - - if (fn_proto.ast.align_expr != 0) { - try walkExpression(w, fn_proto.ast.align_expr); - } - - if (fn_proto.ast.addrspace_expr != 0) { - try walkExpression(w, fn_proto.ast.addrspace_expr); - } - - if (fn_proto.ast.section_expr != 0) { - try walkExpression(w, fn_proto.ast.section_expr); - } - - if (fn_proto.ast.callconv_expr != 0) { - try walkExpression(w, fn_proto.ast.callconv_expr); - } - - try walkExpression(w, fn_proto.ast.return_type); -} - -fn walkExpressions(w: *Walk, expressions: []const Ast.Node.Index) Error!void { - for (expressions) |expression| { - try walkExpression(w, expression); - } -} - -fn walkSwitchCase(w: *Walk, switch_case: Ast.full.SwitchCase) Error!void { - for (switch_case.ast.values) |value_expr| { - try walkExpression(w, value_expr); - } - try walkExpression(w, switch_case.ast.target_expr); -} - -fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) Error!void { - assert(while_node.ast.cond_expr != 0); - assert(while_node.ast.then_expr != 0); - - // Perform these transformations in this priority order: - // 1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already. - // 2. If the `then` block is empty, replace the condition with `if (false)` if it is not already. - // 3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression. - // 4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression. - if (!isTrueIdent(w.ast, while_node.ast.cond_expr) and - (while_node.ast.else_expr == 0 or isEmptyBlock(w.ast, while_node.ast.else_expr))) - { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_with_true = while_node.ast.cond_expr }); - } else if (!isFalseIdent(w.ast, while_node.ast.cond_expr) and isEmptyBlock(w.ast, while_node.ast.then_expr)) { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_with_false = while_node.ast.cond_expr }); - } else if (isTrueIdent(w.ast, while_node.ast.cond_expr)) { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_node = .{ - .to_replace = node_index, - .replacement = while_node.ast.then_expr, - } }); - } else if (isFalseIdent(w.ast, while_node.ast.cond_expr)) { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_node = .{ - .to_replace = node_index, - .replacement = while_node.ast.else_expr, - } }); - } - - try walkExpression(w, while_node.ast.cond_expr); // condition - - if (while_node.ast.cont_expr != 0) { - try walkExpression(w, while_node.ast.cont_expr); - } - - if (while_node.ast.then_expr != 0) { - try walkExpression(w, while_node.ast.then_expr); - } - if (while_node.ast.else_expr != 0) { - try walkExpression(w, while_node.ast.else_expr); - } -} - -fn walkFor(w: *Walk, for_node: Ast.full.For) Error!void { - try walkParamList(w, for_node.ast.inputs); - if (for_node.ast.then_expr != 0) { - try walkExpression(w, for_node.ast.then_expr); - } - if (for_node.ast.else_expr != 0) { - try walkExpression(w, for_node.ast.else_expr); - } -} - -fn walkIf(w: *Walk, node_index: Ast.Node.Index, if_node: Ast.full.If) Error!void { - assert(if_node.ast.cond_expr != 0); - assert(if_node.ast.then_expr != 0); - - // Perform these transformations in this priority order: - // 1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already. - // 2. If the `then` block is empty, replace the condition with `if (false)` if it is not already. - // 3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression. - // 4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression. - if (!isTrueIdent(w.ast, if_node.ast.cond_expr) and - (if_node.ast.else_expr == 0 or isEmptyBlock(w.ast, if_node.ast.else_expr))) - { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_with_true = if_node.ast.cond_expr }); - } else if (!isFalseIdent(w.ast, if_node.ast.cond_expr) and isEmptyBlock(w.ast, if_node.ast.then_expr)) { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_with_false = if_node.ast.cond_expr }); - } else if (isTrueIdent(w.ast, if_node.ast.cond_expr)) { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_node = .{ - .to_replace = node_index, - .replacement = if_node.ast.then_expr, - } }); - } else if (isFalseIdent(w.ast, if_node.ast.cond_expr)) { - try w.transformations.ensureUnusedCapacity(1); - w.transformations.appendAssumeCapacity(.{ .replace_node = .{ - .to_replace = node_index, - .replacement = if_node.ast.else_expr, - } }); - } - - try walkExpression(w, if_node.ast.cond_expr); // condition - - if (if_node.ast.then_expr != 0) { - try walkExpression(w, if_node.ast.then_expr); - } - if (if_node.ast.else_expr != 0) { - try walkExpression(w, if_node.ast.else_expr); - } -} - -fn walkAsm(w: *Walk, asm_node: Ast.full.Asm) Error!void { - try walkExpression(w, asm_node.ast.template); - for (asm_node.ast.items) |item| { - try walkExpression(w, item); - } -} - -fn walkParamList(w: *Walk, params: []const Ast.Node.Index) Error!void { - for (params) |param_node| { - try walkExpression(w, param_node); - } -} - -/// Check if it is already gutted (i.e. its body replaced with `@trap()`). -fn isFnBodyGutted(ast: *const Ast, body_node: Ast.Node.Index) bool { - // skip over discards - const node_tags = ast.nodes.items(.tag); - const datas = ast.nodes.items(.data); - var statements_buf: [2]Ast.Node.Index = undefined; - const statements = switch (node_tags[body_node]) { - .block_two, - .block_two_semicolon, - => blk: { - statements_buf[0..2].* = .{ datas[body_node].lhs, datas[body_node].rhs }; - break :blk if (datas[body_node].lhs == 0) - statements_buf[0..0] - else if (datas[body_node].rhs == 0) - statements_buf[0..1] - else - statements_buf[0..2]; - }, - - .block, - .block_semicolon, - => ast.extra_data[datas[body_node].lhs..datas[body_node].rhs], - - else => return false, - }; - var i: usize = 0; - while (i < statements.len) : (i += 1) { - switch (categorizeStmt(ast, statements[i])) { - .discard_identifier => continue, - .trap_call => return i + 1 == statements.len, - else => return false, - } - } - return false; -} - -const StmtCategory = enum { - discard_undefined, - discard_identifier, - trap_call, - other, -}; - -fn categorizeStmt(ast: *const Ast, stmt: Ast.Node.Index) StmtCategory { - const node_tags = ast.nodes.items(.tag); - const datas = ast.nodes.items(.data); - const main_tokens = ast.nodes.items(.main_token); - switch (node_tags[stmt]) { - .builtin_call_two, .builtin_call_two_comma => { - if (datas[stmt].lhs == 0) { - return categorizeBuiltinCall(ast, main_tokens[stmt], &.{}); - } else if (datas[stmt].rhs == 0) { - return categorizeBuiltinCall(ast, main_tokens[stmt], &.{datas[stmt].lhs}); - } else { - return categorizeBuiltinCall(ast, main_tokens[stmt], &.{ datas[stmt].lhs, datas[stmt].rhs }); - } - }, - .builtin_call, .builtin_call_comma => { - const params = ast.extra_data[datas[stmt].lhs..datas[stmt].rhs]; - return categorizeBuiltinCall(ast, main_tokens[stmt], params); - }, - .assign => { - const infix = datas[stmt]; - if (isDiscardIdent(ast, infix.lhs) and node_tags[infix.rhs] == .identifier) { - const name_bytes = ast.tokenSlice(main_tokens[infix.rhs]); - if (std.mem.eql(u8, name_bytes, "undefined")) { - return .discard_undefined; - } else { - return .discard_identifier; - } - } - return .other; - }, - else => return .other, - } -} - -fn categorizeBuiltinCall( - ast: *const Ast, - builtin_token: Ast.TokenIndex, - params: []const Ast.Node.Index, -) StmtCategory { - if (params.len != 0) return .other; - const name_bytes = ast.tokenSlice(builtin_token); - if (std.mem.eql(u8, name_bytes, "@trap")) - return .trap_call; - return .other; -} - -fn isDiscardIdent(ast: *const Ast, node: Ast.Node.Index) bool { - return isMatchingIdent(ast, node, "_"); -} - -fn isUndefinedIdent(ast: *const Ast, node: Ast.Node.Index) bool { - return isMatchingIdent(ast, node, "undefined"); -} - -fn isTrueIdent(ast: *const Ast, node: Ast.Node.Index) bool { - return isMatchingIdent(ast, node, "true"); -} - -fn isFalseIdent(ast: *const Ast, node: Ast.Node.Index) bool { - return isMatchingIdent(ast, node, "false"); -} - -fn isMatchingIdent(ast: *const Ast, node: Ast.Node.Index, string: []const u8) bool { - const node_tags = ast.nodes.items(.tag); - const main_tokens = ast.nodes.items(.main_token); - switch (node_tags[node]) { - .identifier => { - const token_index = main_tokens[node]; - const name_bytes = ast.tokenSlice(token_index); - return std.mem.eql(u8, name_bytes, string); - }, - else => return false, - } -} - -fn isEmptyBlock(ast: *const Ast, node: Ast.Node.Index) bool { - const node_tags = ast.nodes.items(.tag); - const node_data = ast.nodes.items(.data); - switch (node_tags[node]) { - .block_two => { - return node_data[node].lhs == 0 and node_data[node].rhs == 0; - }, - else => return false, - } -} diff --git a/lib/test_runner.zig b/lib/test_runner.zig deleted file mode 100644 index e11d187d2a1ce7ba00d5ce2c03415d33febfde72..0000000000000000000000000000000000000000 --- a/lib/test_runner.zig +++ /dev/null @@ -1,249 +0,0 @@ -//! Default test runner for unit tests. -const std = @import("std"); -const io = std.io; -const builtin = @import("builtin"); - -pub const std_options = .{ - .logFn = log, -}; - -var log_err_count: usize = 0; -var cmdline_buffer: [4096]u8 = undefined; -var fba = std.heap.FixedBufferAllocator.init(&cmdline_buffer); - -pub fn main() void { - if (builtin.zig_backend == .stage2_aarch64) { - return mainSimple() catch @panic("test failure"); - } - - const args = std.process.argsAlloc(fba.allocator()) catch - @panic("unable to parse command line args"); - - var listen = false; - - for (args[1..]) |arg| { - if (std.mem.eql(u8, arg, "--listen=-")) { - listen = true; - } else { - @panic("unrecognized command line argument"); - } - } - - if (listen) { - return mainServer() catch @panic("internal test runner failure"); - } else { - return mainTerminal(); - } -} - -fn mainServer() !void { - var server = try std.zig.Server.init(.{ - .gpa = fba.allocator(), - .in = std.io.getStdIn(), - .out = std.io.getStdOut(), - .zig_version = builtin.zig_version_string, - }); - defer server.deinit(); - - while (true) { - const hdr = try server.receiveMessage(); - switch (hdr.tag) { - .exit => { - return std.process.exit(0); - }, - .query_test_metadata => { - std.testing.allocator_instance = .{}; - defer if (std.testing.allocator_instance.deinit() == .leak) { - @panic("internal test runner memory leak"); - }; - - var string_bytes: std.ArrayListUnmanaged(u8) = .{}; - defer string_bytes.deinit(std.testing.allocator); - try string_bytes.append(std.testing.allocator, 0); // Reserve 0 for null. - - const test_fns = builtin.test_functions; - const names = try std.testing.allocator.alloc(u32, test_fns.len); - defer std.testing.allocator.free(names); - const expected_panic_msgs = try std.testing.allocator.alloc(u32, test_fns.len); - defer std.testing.allocator.free(expected_panic_msgs); - - for (test_fns, names, expected_panic_msgs) |test_fn, *name, *expected_panic_msg| { - name.* = @as(u32, @intCast(string_bytes.items.len)); - try string_bytes.ensureUnusedCapacity(std.testing.allocator, test_fn.name.len + 1); - string_bytes.appendSliceAssumeCapacity(test_fn.name); - string_bytes.appendAssumeCapacity(0); - expected_panic_msg.* = 0; - } - - try server.serveTestMetadata(.{ - .names = names, - .expected_panic_msgs = expected_panic_msgs, - .string_bytes = string_bytes.items, - }); - }, - - .run_test => { - std.testing.allocator_instance = .{}; - log_err_count = 0; - const index = try server.receiveBody_u32(); - const test_fn = builtin.test_functions[index]; - var fail = false; - var skip = false; - var leak = false; - test_fn.func() catch |err| switch (err) { - error.SkipZigTest => skip = true, - else => { - fail = true; - if (@errorReturnTrace()) |trace| { - std.debug.dumpStackTrace(trace.*); - } - }, - }; - leak = std.testing.allocator_instance.deinit() == .leak; - try server.serveTestResults(.{ - .index = index, - .flags = .{ - .fail = fail, - .skip = skip, - .leak = leak, - .log_err_count = std.math.lossyCast(std.meta.FieldType( - std.zig.Server.Message.TestResults.Flags, - .log_err_count, - ), log_err_count), - }, - }); - }, - - else => { - std.debug.print("unsupported message: {x}", .{@intFromEnum(hdr.tag)}); - std.process.exit(1); - }, - } - } -} - -fn mainTerminal() void { - const test_fn_list = builtin.test_functions; - var ok_count: usize = 0; - var skip_count: usize = 0; - var fail_count: usize = 0; - var progress = std.Progress{ - .dont_print_on_dumb = true, - }; - const root_node = progress.start("Test", test_fn_list.len); - const have_tty = progress.terminal != null and - (progress.supports_ansi_escape_codes or progress.is_windows_terminal); - - var async_frame_buffer: []align(builtin.target.stackAlignment()) u8 = undefined; - // TODO this is on the next line (using `undefined` above) because otherwise zig incorrectly - // ignores the alignment of the slice. - async_frame_buffer = &[_]u8{}; - - var leaks: usize = 0; - for (test_fn_list, 0..) |test_fn, i| { - std.testing.allocator_instance = .{}; - defer { - if (std.testing.allocator_instance.deinit() == .leak) { - leaks += 1; - } - } - std.testing.log_level = .warn; - - var test_node = root_node.start(test_fn.name, 0); - test_node.activate(); - progress.refresh(); - if (!have_tty) { - std.debug.print("{d}/{d} {s}... ", .{ i + 1, test_fn_list.len, test_fn.name }); - } - if (test_fn.func()) |_| { - ok_count += 1; - test_node.end(); - if (!have_tty) std.debug.print("OK\n", .{}); - } else |err| switch (err) { - error.SkipZigTest => { - skip_count += 1; - progress.log("SKIP\n", .{}); - test_node.end(); - }, - else => { - fail_count += 1; - progress.log("FAIL ({s})\n", .{@errorName(err)}); - if (@errorReturnTrace()) |trace| { - std.debug.dumpStackTrace(trace.*); - } - test_node.end(); - }, - } - } - root_node.end(); - if (ok_count == test_fn_list.len) { - std.debug.print("All {d} tests passed.\n", .{ok_count}); - } else { - std.debug.print("{d} passed; {d} skipped; {d} failed.\n", .{ ok_count, skip_count, fail_count }); - } - if (log_err_count != 0) { - std.debug.print("{d} errors were logged.\n", .{log_err_count}); - } - if (leaks != 0) { - std.debug.print("{d} tests leaked memory.\n", .{leaks}); - } - if (leaks != 0 or log_err_count != 0 or fail_count != 0) { - std.process.exit(1); - } -} - -pub fn log( - comptime message_level: std.log.Level, - comptime scope: @Type(.EnumLiteral), - comptime format: []const u8, - args: anytype, -) void { - if (@intFromEnum(message_level) <= @intFromEnum(std.log.Level.err)) { - log_err_count +|= 1; - } - if (@intFromEnum(message_level) <= @intFromEnum(std.testing.log_level)) { - std.debug.print( - "[" ++ @tagName(scope) ++ "] (" ++ @tagName(message_level) ++ "): " ++ format ++ "\n", - args, - ); - } -} - -/// Simpler main(), exercising fewer language features, so that -/// work-in-progress backends can handle it. -pub fn mainSimple() anyerror!void { - const enable_print = false; - const print_all = false; - - var passed: u64 = 0; - var skipped: u64 = 0; - var failed: u64 = 0; - const stderr = if (enable_print) std.io.getStdErr() else {}; - for (builtin.test_functions) |test_fn| { - if (enable_print and print_all) { - stderr.writeAll(test_fn.name) catch {}; - stderr.writeAll("... ") catch {}; - } - test_fn.func() catch |err| { - if (enable_print and !print_all) { - stderr.writeAll(test_fn.name) catch {}; - stderr.writeAll("... ") catch {}; - } - if (err != error.SkipZigTest) { - if (enable_print) stderr.writeAll("FAIL\n") catch {}; - failed += 1; - if (!enable_print) return err; - continue; - } - if (enable_print) stderr.writeAll("SKIP\n") catch {}; - skipped += 1; - continue; - }; - if (enable_print and print_all) stderr.writeAll("PASS\n") catch {}; - passed += 1; - } - if (enable_print) { - stderr.writer().print("{} passed, {} skipped, {} failed\n", .{ passed, skipped, failed }) catch {}; - if (failed != 0) std.process.exit(1); - } -} diff --git a/src/main.zig b/src/main.zig index b80070fd353850a1b9531f66d499bea6d18e2e71..668bc61a778d78b7697e2ef2ffaff13b25b42fc5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2739,6 +2739,7 @@ fn buildOutputType( .paths = .{ .root = .{ .root_dir = zig_lib_directory, + .sub_path = "compiler", }, .root_src_path = "test_runner.zig", }, @@ -5385,7 +5386,10 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { }, .root_src_path = fs.path.basename(runner), } else .{ - .root = .{ .root_dir = zig_lib_directory }, + .root = .{ + .root_dir = zig_lib_directory, + .sub_path = "compiler", + }, .root_src_path = "build_runner.zig", }; @@ -5767,7 +5771,7 @@ fn jitCmd( const main_mod_paths: Package.Module.CreateOptions.Paths = .{ .root = .{ .root_dir = zig_lib_directory, - .sub_path = "std/zig", + .sub_path = "compiler", }, .root_src_path = root_src_path, };