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.

handleLineCommand

source_mapping.handleLineCommand
pub fn handleLineCommand(allocator: Allocator, line_command: []const u8, current_mapping: *CurrentMapping) error

File

lib/compiler/resinator/source_mapping.zig:513

Code

pub fn handleLineCommand(allocator: Allocator, line_command: []const u8, current_mapping: *CurrentMapping) error{ OutOfMemory, InvalidLineCommand }!void {
    // TODO: Are there other whitespace characters that should be included?
    var tokenizer = std.mem.tokenizeAny(u8, line_command, " \t");
    const line_directive = tokenizer.next() orelse return error.InvalidLineCommand; // #line
    if (!std.mem.eql(u8, line_directive, "#line")) return error.InvalidLineCommand;
    const linenum_str = tokenizer.next() orelse return error.InvalidLineCommand;
    const linenum = std.fmt.parseUnsigned(usize, linenum_str, 10) catch return error.InvalidLineCommand;
    if (linenum == 0) return error.InvalidLineCommand;

    var filename_literal = tokenizer.rest();
    while (filename_literal.len > 0 and std.ascii.isWhitespace(filename_literal[filename_literal.len - 1])) {
        filename_literal.len -= 1;
    }
    if (filename_literal.len < 2) return error.InvalidLineCommand;
    const is_quoted = filename_literal[0] == '"' and filename_literal[filename_literal.len - 1] == '"';
    if (!is_quoted) return error.InvalidLineCommand;
    const unquoted_filename = filename_literal[1 .. filename_literal.len - 1];

    // Ignore <builtin> and <command line>
    if (std.mem.eql(u8, unquoted_filename, "<builtin>") or std.mem.eql(u8, unquoted_filename, "<command line>")) return;

    const filename = parseFilename(allocator, unquoted_filename) catch |err| switch (err) {
        error.OutOfMemory => |e| return e,
        else => return error.InvalidLineCommand,
    };
    defer allocator.free(filename);

    // \x00 bytes in the filename is incompatible with how StringTable works
    if (std.mem.indexOfScalar(u8, filename, '\x00') != null) return error.InvalidLineCommand;

    current_mapping.line_num = linenum;
    current_mapping.filename.clearRetainingCapacity();
    try current_mapping.filename.appendSlice(allocator, filename);
    current_mapping.pending = true;
    current_mapping.ignore_contents = std.ascii.endsWithIgnoreCase(filename, ".c") or std.ascii.endsWithIgnoreCase(filename, ".h");
}