Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

preprocess

preprocess.preprocess
pub fn preprocess(
    comp: *aro.Compilation,
    writer: *std.Io.Writer,
    /// Expects argv[0] to be the command name
    argv: []const []const u8,
    maybe_dependencies: ?*Dependencies,
) PreprocessError!void

File

lib/compiler/resinator/preprocess.zig:10

Code

pub fn preprocess(
    comp: *aro.Compilation,
    writer: *std.Io.Writer,
    /// Expects argv[0] to be the command name
    argv: []const []const u8,
    maybe_dependencies: ?*Dependencies,
) PreprocessError!void {
    var driver: aro.Driver = .{ .comp = comp, .diagnostics = comp.diagnostics, .aro_name = "arocc" };
    defer driver.deinit();

    var macro_buf: std.ArrayList(u8) = .empty;
    defer macro_buf.deinit(comp.gpa);

    var discard_buffer: [64]u8 = undefined;
    var discarding: std.Io.Writer.Discarding = .init(&discard_buffer);
    _ = driver.parseArgs(&discarding.writer, &macro_buf, argv) catch |err| switch (err) {
        error.FatalError => return error.ArgError,
        error.OutOfMemory => |e| return e,
        error.WriteFailed => unreachable,
    };
    try comp.initSearchPath(driver.includes.items, false);

    if (hasAnyErrors(comp)) return error.ArgError;

    // .include_system_defines gives us things like _WIN32
    const builtin_macros = comp.generateBuiltinMacros(.include_system_defines) catch |err| switch (err) {
        error.FatalError => return error.GeneratedSourceError,
        else => |e| return e,
    };
    const user_macros = comp.addSourceFromBuffer("<command line>", macro_buf.items) catch |err| switch (err) {
        error.FatalError => return error.GeneratedSourceError,
        else => |e| return e,
    };
    const source = driver.inputs.items[0];

    if (hasAnyErrors(comp)) return error.GeneratedSourceError;

    comp.generated_buf.items.len = 0;
    var pp = aro.Preprocessor.init(comp, .{ .base_file = source.id }) catch |err| switch (err) {
        error.FatalError => return error.GeneratedSourceError,
        error.OutOfMemory => |e| return e,
    };
    defer pp.deinit();

    if (comp.langopts.ms_extensions) {
        comp.ms_cwd_source_id = source.id;
    }

    pp.preserve_whitespace = true;
    pp.linemarkers = .line_directives;

    pp.preprocessSources(.{ .main = source, .builtin = builtin_macros, .command_line = user_macros }) catch |err| switch (err) {
        error.FatalError => return error.PreprocessError,
        else => |e| return e,
    };

    if (hasAnyErrors(comp)) return error.PreprocessError;

    try pp.prettyPrintTokens(writer, .result_only);

    if (maybe_dependencies) |dependencies| {
        for (comp.sources.values()) |comp_source| {
            if (comp_source.id == builtin_macros.id or comp_source.id == user_macros.id) continue;
            if (comp_source.id.index == .unused or comp_source.id.index == .generated) continue;
            const duped_path = try dependencies.allocator.dupe(u8, comp_source.path);
            errdefer dependencies.allocator.free(duped_path);
            try dependencies.list.append(dependencies.allocator, duped_path);
        }
    }
}