Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit 115550d

Browse files
authored
update to writergate changes
1 parent fe42c2e commit 115550d

File tree

7 files changed

+99
-82
lines changed

7 files changed

+99
-82
lines changed

build.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ pub fn build(b: *std.Build) void {
3030
translate_c_module.addImport("helpers", helpers);
3131
translate_c_module.addImport("c_builtins", c_builtins);
3232

33+
if (target.result.os.tag == .windows) {
34+
translate_c_module.linkSystemLibrary("advapi32", .{});
35+
}
36+
3337
const translate_c_exe = b.addExecutable(.{
3438
.name = "translate-c",
3539
.root_module = translate_c_module,

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
.fingerprint = 0x1c84f6455a54f043, // Changing this has security and trust implications.
77

8-
.minimum_zig_version = "0.15.0-dev.870+710632b45",
8+
.minimum_zig_version = "0.15.0-dev.1145+3ae0ba096",
99

1010
.dependencies = .{
1111
.aro = .{
12-
.url = "git+https://github.com/Vexu/arocc#1916d00fe055af0a33783378d76571cfc9561761",
13-
.hash = "aro-0.0.0-JSD1QuUuJwCtUthXWuwlmnWBMRaoKB_6Uy1nLkrgMQ0A",
12+
.url = "git+https://github.com/Vexu/arocc#c9af66bcba770beb36c0602a59f4adfe58996468",
13+
.hash = "aro-0.0.0-JSD1Qlc4JwBHqaS3T676PyNbYKF0mVWf5h_KEhB9Huvo",
1414
},
1515
},
1616

examples/use_static_lib/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn buildLibfoo(
7272
.root = b.path("libfoo"),
7373
.files = &.{ "add.c", "print.c" },
7474
});
75-
const lib = b.addStaticLibrary(.{
75+
const lib = b.addLibrary(.{
7676
.name = "foo",
7777
.root_module = mod,
7878
});

src/MacroTranslator.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ fn zigifyEscapeSequences(mt: *MacroTranslator, slice: []const u8) ![]const u8 {
387387
break;
388388
}
389389
} else return source;
390-
var bytes = try mt.t.arena.alloc(u8, source.len * 2);
390+
const bytes = try mt.t.arena.alloc(u8, source.len * 2);
391391
var state: enum {
392392
start,
393393
escape,
@@ -497,7 +497,7 @@ fn zigifyEscapeSequences(mt: *MacroTranslator, slice: []const u8) ![]const u8 {
497497
num += c - 'A' + 10;
498498
},
499499
else => {
500-
i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, .{ .fill = '0', .width = 2 });
500+
i += std.fmt.printInt(bytes[i..], num, 16, .lower, .{ .fill = '0', .width = 2 });
501501
num = 0;
502502
if (c == '\\')
503503
state = .escape
@@ -523,7 +523,7 @@ fn zigifyEscapeSequences(mt: *MacroTranslator, slice: []const u8) ![]const u8 {
523523
};
524524
num += c - '0';
525525
} else {
526-
i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, .{ .fill = '0', .width = 2 });
526+
i += std.fmt.printInt(bytes[i..], num, 16, .lower, .{ .fill = '0', .width = 2 });
527527
num = 0;
528528
count = 0;
529529
if (c == '\\')
@@ -537,7 +537,7 @@ fn zigifyEscapeSequences(mt: *MacroTranslator, slice: []const u8) ![]const u8 {
537537
}
538538
}
539539
if (state == .hex or state == .octal) {
540-
i += std.fmt.formatIntBuf(bytes[i..], num, 16, .lower, .{ .fill = '0', .width = 2 });
540+
i += std.fmt.printInt(bytes[i..], num, 16, .lower, .{ .fill = '0', .width = 2 });
541541
}
542542

543543
return bytes[0..i];
@@ -553,10 +553,10 @@ fn escapeUnprintables(mt: *MacroTranslator) ![]const u8 {
553553
const zigified = try mt.zigifyEscapeSequences(slice);
554554
if (std.unicode.utf8ValidateSlice(zigified)) return zigified;
555555

556-
const formatter = std.fmt.fmtSliceEscapeLower(zigified);
557-
const encoded_size = @as(usize, @intCast(std.fmt.count("{s}", .{formatter})));
556+
const formatter = std.ascii.hexEscape(zigified, .lower);
557+
const encoded_size = @as(usize, @intCast(std.fmt.count("{f}", .{formatter})));
558558
const output = try mt.t.arena.alloc(u8, encoded_size);
559-
return std.fmt.bufPrint(output, "{s}", .{formatter}) catch |err| switch (err) {
559+
return std.fmt.bufPrint(output, "{f}", .{formatter}) catch |err| switch (err) {
560560
error.NoSpaceLeft => unreachable,
561561
else => |e| return e,
562562
};
@@ -577,7 +577,7 @@ fn parseCPrimaryExpr(mt: *MacroTranslator, scope: *Scope) ParseError!ZigNode {
577577
} else {
578578
mt.i += 1;
579579

580-
const str = try std.fmt.allocPrint(mt.t.arena, "0x{s}", .{std.fmt.fmtSliceHexLower(slice[1 .. slice.len - 1])});
580+
const str = try std.fmt.allocPrint(mt.t.arena, "0x{x}", .{slice[1 .. slice.len - 1]});
581581
return ZigTag.integer_literal.create(mt.t.arena, str);
582582
}
583583
},

src/Translator.zig

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub const Options = struct {
172172
module_libs: bool,
173173
};
174174

175-
pub fn translate(options: Options) ![]u8 {
175+
pub fn translate(options: Options) mem.Allocator.Error![]u8 {
176176
const gpa = options.gpa;
177177
var arena_allocator = std.heap.ArenaAllocator.init(gpa);
178178
defer arena_allocator.deinit();
@@ -217,32 +217,32 @@ pub fn translate(options: Options) ![]u8 {
217217

218218
try translator.global_scope.processContainerMemberFns();
219219

220-
var buf: std.ArrayList(u8) = .init(gpa);
221-
defer buf.deinit();
220+
var allocating: std.Io.Writer.Allocating = .init(gpa);
221+
defer allocating.deinit();
222222

223223
if (options.module_libs) {
224-
try buf.appendSlice(
224+
allocating.writer.writeAll(
225225
\\pub const __builtin = @import("c_builtins");
226226
\\pub const __helpers = @import("helpers");
227227
\\
228228
\\
229-
);
229+
) catch return error.OutOfMemory;
230230
} else {
231-
try buf.appendSlice(
231+
allocating.writer.writeAll(
232232
\\pub const __builtin = @import("c_builtins.zig");
233233
\\pub const __helpers = @import("helpers.zig");
234234
\\
235235
\\
236-
);
236+
) catch return error.OutOfMemory;
237237
}
238238

239239
var zig_ast = try ast.render(gpa, translator.global_scope.nodes.items);
240240
defer {
241241
gpa.free(zig_ast.source);
242242
zig_ast.deinit(gpa);
243243
}
244-
try zig_ast.renderToArrayList(&buf, .{});
245-
return buf.toOwnedSlice();
244+
zig_ast.render(gpa, &allocating.writer, .{}) catch return error.OutOfMemory;
245+
return allocating.toOwnedSlice();
246246
}
247247

248248
fn prepopulateGlobalNameTable(t: *Translator) !void {
@@ -1006,17 +1006,16 @@ fn transStaticAssert(t: *Translator, scope: *Scope, static_assert: Node.StaticAs
10061006
const str_qt = message.qt(t.tree);
10071007

10081008
const bytes = t.comp.interner.get(str_val.ref()).bytes;
1009-
var buf = std.ArrayList(u8).init(t.gpa);
1010-
defer buf.deinit();
1009+
var allocating: std.Io.Writer.Allocating = .init(t.gpa);
1010+
defer allocating.deinit();
10111011

1012-
try buf.appendSlice("\"static assertion failed \\");
1012+
allocating.writer.writeAll("\"static assertion failed \\") catch return error.OutOfMemory;
10131013

1014-
try buf.ensureUnusedCapacity(bytes.len);
1015-
try aro.Value.printString(bytes, str_qt, t.comp, buf.writer());
1016-
_ = buf.pop(); // printString adds a terminating " so we need to remove it
1017-
try buf.appendSlice("\\\"\"");
1014+
aro.Value.printString(bytes, str_qt, t.comp, &allocating.writer) catch return error.OutOfMemory;
1015+
allocating.writer.end -= 1; // printString adds a terminating " so we need to remove it
1016+
allocating.writer.writeAll("\\\"\"") catch return error.OutOfMemory;
10181017

1019-
break :str try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, buf.items));
1018+
break :str try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
10201019
} else try ZigTag.string_literal.create(t.arena, "\"static assertion failed\"");
10211020

10221021
const assert_node = try ZigTag.static_assert.create(t.arena, .{ .lhs = condition, .rhs = diagnostic });
@@ -1027,11 +1026,11 @@ fn transGlobalAsm(t: *Translator, scope: *Scope, global_asm: Node.SimpleAsm) Err
10271026
const asm_string = t.tree.value_map.get(global_asm.asm_str).?;
10281027
const bytes = t.comp.interner.get(asm_string.ref()).bytes;
10291028

1030-
var buf = std.ArrayList(u8).init(t.gpa);
1031-
defer buf.deinit();
1032-
try aro.Value.printString(bytes, global_asm.asm_str.qt(t.tree), t.comp, buf.writer());
1029+
var allocating: std.Io.Writer.Allocating = try .initCapacity(t.gpa, bytes.len);
1030+
defer allocating.deinit();
1031+
aro.Value.printString(bytes, global_asm.asm_str.qt(t.tree), t.comp, &allocating.writer) catch return error.OutOfMemory;
10331032

1034-
const str_node = try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, buf.items));
1033+
const str_node = try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
10351034

10361035
const asm_node = try ZigTag.asm_simple.create(t.arena, str_node);
10371036
const block = try ZigTag.block_single.create(t.arena, asm_node);
@@ -1045,11 +1044,10 @@ fn transGlobalAsm(t: *Translator, scope: *Scope, global_asm: Node.SimpleAsm) Err
10451044
// ================
10461045

10471046
fn getTypeStr(t: *Translator, qt: QualType) ![]const u8 {
1048-
var buf: std.ArrayListUnmanaged(u8) = .empty;
1049-
defer buf.deinit(t.gpa);
1050-
const w = buf.writer(t.gpa);
1051-
try qt.print(t.comp, w);
1052-
return t.arena.dupe(u8, buf.items);
1047+
var allocating: std.Io.Writer.Allocating = .init(t.gpa);
1048+
defer allocating.deinit();
1049+
qt.print(t.comp, &allocating.writer) catch return error.OutOfMemory;
1050+
return t.arena.dupe(u8, allocating.getWritten());
10531051
}
10541052

10551053
fn transType(t: *Translator, scope: *Scope, qt: QualType, source_loc: TokenIndex) TypeError!ZigNode {
@@ -3353,12 +3351,11 @@ fn transFloatLiteral(
33533351
const val = t.tree.value_map.get(literal_index).?;
33543352
const float_literal = literal_index.get(t.tree).float_literal;
33553353

3356-
var buf: std.ArrayListUnmanaged(u8) = .empty;
3357-
defer buf.deinit(t.gpa);
3358-
const w = buf.writer(t.gpa);
3359-
_ = try val.print(float_literal.qt, t.comp, w);
3354+
var allocating: std.Io.Writer.Allocating = .init(t.gpa);
3355+
defer allocating.deinit();
3356+
_ = val.print(float_literal.qt, t.comp, &allocating.writer) catch return error.OutOfMemory;
33603357

3361-
const float_lit_node = try ZigTag.float_literal.create(t.arena, try t.arena.dupe(u8, buf.items));
3358+
const float_lit_node = try ZigTag.float_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
33623359
if (suppress_as == .no_as) {
33633360
return t.maybeSuppressResult(used, float_lit_node);
33643361
}
@@ -3398,13 +3395,12 @@ fn transNarrowStringLiteral(
33983395
const val = t.tree.value_map.get(expr).?;
33993396

34003397
const bytes = t.comp.interner.get(val.ref()).bytes;
3401-
var buf = std.ArrayList(u8).init(t.gpa);
3402-
defer buf.deinit();
3398+
var allocating: std.Io.Writer.Allocating = try .initCapacity(t.gpa, bytes.len);
3399+
defer allocating.deinit();
34033400

3404-
try buf.ensureUnusedCapacity(bytes.len);
3405-
try aro.Value.printString(bytes, literal.qt, t.comp, buf.writer());
3401+
aro.Value.printString(bytes, literal.qt, t.comp, &allocating.writer) catch return error.OutOfMemory;
34063402

3407-
return ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, buf.items));
3403+
return ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
34083404
}
34093405

34103406
/// Translate a string literal that is initializing an array. In general narrow string
@@ -3887,7 +3883,7 @@ fn createNumberNode(t: *Translator, num: anytype, num_kind: enum { int, float })
38873883

38883884
fn createCharLiteralNode(t: *Translator, narrow: bool, val: u32) TransError!ZigNode {
38893885
return ZigTag.char_literal.create(t.arena, if (narrow)
3890-
try std.fmt.allocPrint(t.arena, "'{'}'", .{std.zig.fmtEscapes(&.{@as(u8, @intCast(val))})})
3886+
try std.fmt.allocPrint(t.arena, "'{f}'", .{std.zig.fmtChar(&.{@as(u8, @intCast(val))})})
38913887
else
38923888
try std.fmt.allocPrint(t.arena, "'\\u{{{x}}}'", .{val}));
38933889
}

src/ast.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ const Context = struct {
866866

867867
fn addTokenFmt(c: *Context, tag: TokenTag, comptime format: []const u8, args: anytype) Allocator.Error!TokenIndex {
868868
const start_index = c.buf.items.len;
869-
try c.buf.writer().print(format ++ " ", args);
869+
try c.buf.print(format ++ " ", args);
870870

871871
try c.tokens.append(c.gpa, .{
872872
.tag = tag,
@@ -883,7 +883,7 @@ const Context = struct {
883883
fn addIdentifier(c: *Context, bytes: []const u8) Allocator.Error!TokenIndex {
884884
if (std.zig.primitives.isPrimitive(bytes))
885885
return c.addTokenFmt(.identifier, "@\"{s}\"", .{bytes});
886-
return c.addTokenFmt(.identifier, "{p}", .{std.zig.fmtId(bytes)});
886+
return c.addTokenFmt(.identifier, "{f}", .{std.zig.fmtId(bytes)});
887887
}
888888

889889
fn listToSpan(c: *Context, list: []const NodeIndex) Allocator.Error!NodeSubRange {
@@ -1212,7 +1212,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
12121212

12131213
const compile_error_tok = try c.addToken(.builtin, "@compileError");
12141214
_ = try c.addToken(.l_paren, "(");
1215-
const err_msg_tok = try c.addTokenFmt(.string_literal, "\"{}\"", .{std.zig.fmtEscapes(payload.mangled)});
1215+
const err_msg_tok = try c.addTokenFmt(.string_literal, "\"{f}\"", .{std.zig.fmtString(payload.mangled)});
12161216
const err_msg = try c.addNode(.{
12171217
.tag = .string_literal,
12181218
.main_token = err_msg_tok,
@@ -2185,7 +2185,7 @@ fn renderContainer(c: *Context, node: Node) !NodeIndex {
21852185
defer c.gpa.free(members);
21862186

21872187
for (payload.fields, 0..) |field, i| {
2188-
const name_tok = try c.addTokenFmt(.identifier, "{p}", .{std.zig.fmtId(field.name)});
2188+
const name_tok = try c.addTokenFmt(.identifier, "{f}", .{std.zig.fmtIdFlags(field.name, .{ .allow_primitive = true })});
21892189
_ = try c.addToken(.colon, ":");
21902190
const type_expr = try renderNode(c, field.type);
21912191

@@ -2282,7 +2282,7 @@ fn renderFieldAccess(c: *Context, lhs: NodeIndex, field_name: []const u8) !NodeI
22822282
.tag = .field_access,
22832283
.main_token = try c.addToken(.period, "."),
22842284
.data = .{ .node_and_token = .{
2285-
lhs, try c.addTokenFmt(.identifier, "{p}", .{std.zig.fmtId(field_name)}),
2285+
lhs, try c.addTokenFmt(.identifier, "{f}", .{std.zig.fmtIdFlags(field_name, .{ .allow_primitive = true })}),
22862286
} },
22872287
});
22882288
}
@@ -2766,7 +2766,7 @@ fn renderVar(c: *Context, node: Node) !NodeIndex {
27662766
_ = try c.addToken(.l_paren, "(");
27672767
const res = try c.addNode(.{
27682768
.tag = .string_literal,
2769-
.main_token = try c.addTokenFmt(.string_literal, "\"{}\"", .{std.zig.fmtEscapes(some)}),
2769+
.main_token = try c.addTokenFmt(.string_literal, "\"{f}\"", .{std.zig.fmtString(some)}),
27702770
.data = undefined,
27712771
});
27722772
_ = try c.addToken(.r_paren, ")");
@@ -2852,7 +2852,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {
28522852
_ = try c.addToken(.l_paren, "(");
28532853
const res = try c.addNode(.{
28542854
.tag = .string_literal,
2855-
.main_token = try c.addTokenFmt(.string_literal, "\"{}\"", .{std.zig.fmtEscapes(some)}),
2855+
.main_token = try c.addTokenFmt(.string_literal, "\"{f}\"", .{std.zig.fmtString(some)}),
28562856
.data = undefined,
28572857
});
28582858
_ = try c.addToken(.r_paren, ")");

0 commit comments

Comments
 (0)