diff options
| -rw-r--r-- | core/root.zig | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/core/root.zig b/core/root.zig index a0cba5ec..ae6fc454 100644 --- a/core/root.zig +++ b/core/root.zig @@ -443,3 +443,153 @@ test { // and the rest) is gui.zig's. if (comptime @import("term_options").terminal and platform == .gui) _ = @import("dev").gui; } + +/// The container-level `var`s core/ may hold (at the top of a file, in a +/// container, a function's struct's static), by file, each line's names +/// with why none is a core's state. Nothing else of a core is a process +/// global (docs: the reorganisation's R2): what a step reads lives in the +/// core's memory, which the rollback copy saves and the digest covers. A +/// `var` in a test block is the test's and needs no line. +const allowed_vars = [_]struct { []const u8, []const u8, []const u8 }{ + .{ "Addon.zig", "libs found found_words found_text found_file found_head nfound scanned heads nheads heads_changed", "the process's addon libraries and the start's scan of them" }, + .{ "Addon.zig", "marked marked_len", "the crash mark set around a library's load" }, + .{ "Kind.zig", "current", "the kind call in progress, put back at its end (threadlocal)" }, + .{ "Kind.zig", "breach", "Debug: the first call over budget, for the addon test tool" }, + .{ "Kind.zig", "drawer heap seed_base", "set once by the host at start" }, + .{ "Kind.zig", "ready", "this process's draws done, each keyed by its core, pane and serial" }, + .{ "Names.zig", "dp_rows dp_trace", "the scorer's scratch, written before it is read in one call" }, + .{ "Turn.zig", "yield_depth", "this thread's nesting of yields (threadlocal)" }, + .{ "c_heap.zig", "allocators allocators_len allocators_lock last", "the C heap's registry of allocators and its lookup's memo" }, + .{ "c_heap.zig", "test_allocator stateless", "its tests'" }, + .{ "config.zig", "other_build addon_sources", "the docs check's data, a test's" }, + .{ "dump.zig", "terminal_gpa slim_terminals join_files", "a dump's or a load's mode, set and put back around it" }, + .{ "dump.zig", "empty", "the empty undo a slim dump points a file at for one call" }, + .{ "edit.zig", "drawing", "a frame is being drawn, set and put back around it" }, + .{ "fs.zig", "last_reads last_read", "the host's last whole-file reads, read by its watch alone" }, + .{ "memory.zig", "pardes_bits pardes_region frame_bits frame_region lsp_bits lsp_region tree_sitter_bits tree_sitter_region image_bits image_region addon_bits addon_region host_bits host_region lsp_query_buffer lsp_query_busy pins_lock pins pin_count put_off", "the process's memory regions themselves" }, + .{ "replica.zig", "gpa", "zstd's allocator for its C heap, a test's to set" }, + .{ "replica.zig", "live_peers", "the session's clients, for its panic handler" }, + .{ "replica.zig", "core tasks", "a test host's" }, + .{ "rollback.zig", "fill", "a test's fill byte for new allocations, set at start" }, + .{ "root.zig", "turn", "the process's turn: the lock steps and 9P tasks take" }, + .{ "sam_edit.zig", "opened", "a test fixture's" }, + .{ "selection_pipe.zig", "lock procs len stopped next next_token", "the process's running pipes and their stop tokens (the session's alone)" }, + .{ "syntax.zig", "specs diff_pieces diff_clock", "memos of pure functions: grammars made lazily, a hunk's styles by its bytes" }, + .{ "syntax.zig", "syntax_allocator syntax_started", "tree-sitter's allocator, set once at start" }, + .{ "syntax.zig", "handed handed_exts handed_n", "the languages an editor library's user hands in" }, + .{ "syntax.zig", "held_trees", "the trees of the core being stepped, switched per step" }, + .{ "tape.zig", "on", "the tape of the core being stepped, switched per step" }, + .{ "walk.zig", "in_blob blobs_out", "a walk's context, set and put back (threadlocal)" }, + .{ "walk.zig", "state bytes", "a type's table, made once" }, + .{ "term/TerminalImages.zig", "started", "the PNG resize has its allocator, set at start" }, +}; + +/// The container-level `var`s of a Zig source, each `name` and `line`: +/// a comment, a string, a character and a `\\` line are not code, and a +/// test block is skipped whole. +const Var = struct { name: []const u8, line: usize }; + +fn containerVars(src: []const u8, gpa: std.mem.Allocator, out: *std.ArrayList(Var)) !void { + // Each open brace's: a container's ('c'), a test's ('t'), a block's. + var open: std.ArrayList(u8) = .empty; + defer open.deinit(gpa); + var tests: usize = 0; + var line: usize = 1; + var i: usize = 0; + while (i < src.len) { + const c = src[i]; + if (c == '\n') line += 1; + if ((c == '/' or c == '\\') and i + 1 < src.len and src[i + 1] == c) { + i = std.mem.indexOfScalarPos(u8, src, i, '\n') orelse src.len; + } else if (c == '"' or c == '\'') { + i += 1; + while (i < src.len and src[i] != c) : (i += 1) if (src[i] == '\\') { + i += 1; + }; + i += 1; + } else if (c == '{') { + const kind = braceKind(src[0..i]); + if (kind == 't') tests += 1; + try open.append(gpa, kind); + i += 1; + } else if (c == '}') { + if ((open.pop() orelse 0) == 't') tests -= 1; + i += 1; + } else if (std.ascii.isAlphabetic(c) or c == '_') { + const end = wordEnd(src, i); + if (tests == 0 and std.mem.eql(u8, src[i..end], "var") and (open.items.len == 0 or open.getLast() == 'c')) { + var at = end; + while (at < src.len and std.ascii.isWhitespace(src[at])) at += 1; + try out.append(gpa, .{ .name = src[at..wordEnd(src, at)], .line = line }); + } + i = end; + } else i += 1; + } +} + +fn wordEnd(src: []const u8, from: usize) usize { + var i = from; + while (i < src.len and (std.ascii.isAlphanumeric(src[i]) or src[i] == '_')) i += 1; + return i; +} + +/// What the brace after `before` opens: a container's body (`struct {`, +/// `union(enum) {`, `enum(u8) {`), a test's (`test "name" {`, `test {`), +/// or a block. +fn braceKind(before: []const u8) u8 { + var j = before.len; + while (j > 0 and std.ascii.isWhitespace(before[j - 1])) j -= 1; + if (j > 0 and before[j - 1] == ')') { + var depth: usize = 0; + while (j > 0) { + j -= 1; + if (before[j] == ')') depth += 1; + if (before[j] == '(') depth -= 1; + if (depth == 0) break; + } + while (j > 0 and std.ascii.isWhitespace(before[j - 1])) j -= 1; + } else if (j > 0 and before[j - 1] == '"') { + j -= 1; + while (j > 0 and before[j - 1] != '"') j -= 1; + if (j > 0) j -= 1; + while (j > 0 and std.ascii.isWhitespace(before[j - 1])) j -= 1; + } + const end = j; + while (j > 0 and (std.ascii.isAlphanumeric(before[j - 1]) or before[j - 1] == '_')) j -= 1; + const word = before[j..end]; + for ([_][]const u8{ "struct", "union", "enum", "opaque" }) |w| if (std.mem.eql(u8, word, w)) return 'c'; + return if (std.mem.eql(u8, word, "test")) 't' else 'b'; +} + +test "nothing of a core is a process global: every container-level var in core/ is one the list allows, with its reason" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var dir = try std.Io.Dir.cwd().openDir(io, "core", .{ .iterate = true }); + defer dir.close(io); + var walker = try dir.walk(gpa); + defer walker.deinit(); + var found: std.ArrayList(Var) = .empty; + defer found.deinit(gpa); + var files: usize = 0; + var unlisted: usize = 0; + while (try walker.next(io)) |e| { + if (e.kind != .file or !std.mem.endsWith(u8, e.basename, ".zig")) continue; + files += 1; + const src = try e.dir.readFileAlloc(io, e.basename, gpa, .limited(16 << 20)); + defer gpa.free(src); + found.clearRetainingCapacity(); + try containerVars(src, gpa, &found); + for (found.items) |v| { + var listed = false; + for (allowed_vars) |a| if (std.mem.eql(u8, a[0], e.path)) { + var names = std.mem.tokenizeScalar(u8, a[1], ' '); + while (names.next()) |n| listed = listed or std.mem.eql(u8, n, v.name); + }; + if (listed) continue; + std.debug.print("core/{s}:{d}: `var {s}` is a process global: the core's state goes in its memory (a field), or the var on root.zig's allowed_vars with why it is not a core's\n", .{ e.path, v.line, v.name }); + unlisted += 1; + } + } + try std.testing.expect(files > 100); + try std.testing.expectEqual(@as(usize, 0), unlisted); +} |
