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.

checkLiteralMacro

Attempt to translate literal as the name of the simple macro it was expanded from.

Translator.checkLiteralMacro
fn checkLiteralMacro(t: *Translator, tok: TokenIndex, used: ResultUsed) !?ZigNode

File

lib/compiler/translate-c/Translator.zig:3470

Code

fn checkLiteralMacro(t: *Translator, tok: TokenIndex, used: ResultUsed) !?ZigNode {
    if (!t.keep_macro_literals) return null;
    const expansion_locs = t.pp.expansionSlice(tok);
    if (expansion_locs.len == 0) return null;

    const last_expand = expansion_locs[0];
    const source = t.comp.getSource(last_expand.id);
    var tokenizer: aro.Tokenizer = .{
        .buf = source.buf,
        .langopts = t.comp.langopts,
        .source = last_expand.id,
        .index = last_expand.byte_offset,
        .splice_locs = &.{},
    };
    const name_tok = tokenizer.next();
    if (!name_tok.id.isMacroIdentifier()) return null;

    const name = t.pp.tokSlice(name_tok);
    if (t.global_scope.containsNow(name)) return null;
    const macro = t.pp.defines.get(name) orelse return null;
    if (macro.is_func) return null;
    if (macro.isBuiltin()) return null;

    var tok_count: u8 = 0;
    for (macro.tokens) |macro_tok| {
        switch (macro_tok.id) {
            .invalid => continue,
            .whitespace => continue,
            .comment => continue,
            .macro_ws => continue,
            else => {
                if (tok_count != 0) return null;
                tok_count += 1;
            },
        }
    }

    if (t.checkTranslatableMacro(macro.tokens, macro.params) != null) return null;

    const ident = try ZigTag.identifier.create(t.arena, name);
    return try t.maybeSuppressResult(used, ident);
}