authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-02 14:25:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-07 22:43:52-07:00
log6314e6f238758ce84a288e9ed990540232d04656
treeb31d2a11b6527374a0e73800a23ae8a8cc773f4c
parentd5c97fded5d31114f8fc684938a2de22af929949

compiler: fix a bunch of format strings


5 files changed, 86 insertions(+), 96 deletions(-)

src/Package/Fetch.zig+1-1
...@@ -227,7 +227,7 @@ pub const JobQueue = struct {...@@ -227,7 +227,7 @@ pub const JobQueue = struct {
227 }227 }
228228
229 try buf.writer().print(229 try buf.writer().print(
230 \\ pub const build_root = "{q}";230 \\ pub const build_root = "{fq}";
231 \\231 \\
232 , .{fetch.package_root});232 , .{fetch.package_root});
233233
src/arch/riscv64/CodeGen.zig+24-24
...@@ -435,7 +435,7 @@ const InstTracking = struct {...@@ -435,7 +435,7 @@ const InstTracking = struct {
435 fn trackSpill(inst_tracking: *InstTracking, function: *Func, inst: Air.Inst.Index) !void {435 fn trackSpill(inst_tracking: *InstTracking, function: *Func, inst: Air.Inst.Index) !void {
436 try function.freeValue(inst_tracking.short);436 try function.freeValue(inst_tracking.short);
437 inst_tracking.reuseFrame();437 inst_tracking.reuseFrame();
438 tracking_log.debug("%{d} => {} (spilled)", .{ inst, inst_tracking.* });438 tracking_log.debug("%{d} => {f} (spilled)", .{ inst, inst_tracking.* });
439 }439 }
440440
441 fn verifyMaterialize(inst_tracking: InstTracking, target: InstTracking) void {441 fn verifyMaterialize(inst_tracking: InstTracking, target: InstTracking) void {
...@@ -1015,7 +1015,7 @@ const FormatTrackingData = struct {...@@ -1015,7 +1015,7 @@ const FormatTrackingData = struct {
1015};1015};
1016fn formatTracking(data: FormatTrackingData, writer: *std.io.Writer) std.io.Writer.Error!void {1016fn formatTracking(data: FormatTrackingData, writer: *std.io.Writer) std.io.Writer.Error!void {
1017 var it = data.func.inst_tracking.iterator();1017 var it = data.func.inst_tracking.iterator();
1018 while (it.next()) |entry| try writer.print("\n%{d} = {}", .{ entry.key_ptr.*, entry.value_ptr.* });1018 while (it.next()) |entry| try writer.print("\n%{d} = {f}", .{ entry.key_ptr.*, entry.value_ptr.* });
1019}1019}
1020fn fmtTracking(func: *Func) std.fmt.Formatter(FormatTrackingData, formatTracking) {1020fn fmtTracking(func: *Func) std.fmt.Formatter(FormatTrackingData, formatTracking) {
1021 return .{ .data = .{ .func = func } };1021 return .{ .data = .{ .func = func } };
...@@ -1033,7 +1033,7 @@ fn addInst(func: *Func, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {...@@ -1033,7 +1033,7 @@ fn addInst(func: *Func, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {
1033 .pseudo_dbg_epilogue_begin,1033 .pseudo_dbg_epilogue_begin,
1034 .pseudo_dead,1034 .pseudo_dead,
1035 => false,1035 => false,
1036 }) wip_mir_log.debug("{}", .{func.fmtWipMir(result_index)});1036 }) wip_mir_log.debug("{f}", .{func.fmtWipMir(result_index)});
1037 return result_index;1037 return result_index;
1038}1038}
10391039
...@@ -1287,7 +1287,7 @@ fn genLazy(func: *Func, lazy_sym: link.File.LazySymbol) InnerError!void {...@@ -1287,7 +1287,7 @@ fn genLazy(func: *Func, lazy_sym: link.File.LazySymbol) InnerError!void {
1287 switch (Type.fromInterned(lazy_sym.ty).zigTypeTag(zcu)) {1287 switch (Type.fromInterned(lazy_sym.ty).zigTypeTag(zcu)) {
1288 .@"enum" => {1288 .@"enum" => {
1289 const enum_ty = Type.fromInterned(lazy_sym.ty);1289 const enum_ty = Type.fromInterned(lazy_sym.ty);
1290 wip_mir_log.debug("{}.@tagName:", .{enum_ty.fmt(pt)});1290 wip_mir_log.debug("{f}.@tagName:", .{enum_ty.fmt(pt)});
12911291
1292 const param_regs = abi.Registers.Integer.function_arg_regs;1292 const param_regs = abi.Registers.Integer.function_arg_regs;
1293 const ret_reg = param_regs[0];1293 const ret_reg = param_regs[0];
...@@ -1369,7 +1369,7 @@ fn genLazy(func: *Func, lazy_sym: link.File.LazySymbol) InnerError!void {...@@ -1369,7 +1369,7 @@ fn genLazy(func: *Func, lazy_sym: link.File.LazySymbol) InnerError!void {
1369 });1369 });
1370 },1370 },
1371 else => return func.fail(1371 else => return func.fail(
1372 "TODO implement {s} for {}",1372 "TODO implement {s} for {f}",
1373 .{ @tagName(lazy_sym.kind), Type.fromInterned(lazy_sym.ty).fmt(pt) },1373 .{ @tagName(lazy_sym.kind), Type.fromInterned(lazy_sym.ty).fmt(pt) },
1374 ),1374 ),
1375 }1375 }
...@@ -1383,8 +1383,8 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {...@@ -1383,8 +1383,8 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {
13831383
1384 for (body) |inst| {1384 for (body) |inst| {
1385 if (func.liveness.isUnused(inst) and !func.air.mustLower(inst, ip)) continue;1385 if (func.liveness.isUnused(inst) and !func.air.mustLower(inst, ip)) continue;
1386 wip_mir_log.debug("{}", .{func.fmtAir(inst)});1386 wip_mir_log.debug("{f}", .{func.fmtAir(inst)});
1387 verbose_tracking_log.debug("{}", .{func.fmtTracking()});1387 verbose_tracking_log.debug("{f}", .{func.fmtTracking()});
13881388
1389 const old_air_bookkeeping = func.air_bookkeeping;1389 const old_air_bookkeeping = func.air_bookkeeping;
1390 try func.ensureProcessDeathCapacity(Air.Liveness.bpi);1390 try func.ensureProcessDeathCapacity(Air.Liveness.bpi);
...@@ -1674,7 +1674,7 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {...@@ -1674,7 +1674,7 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {
1674 }1674 }
1675 }1675 }
1676 }1676 }
1677 verbose_tracking_log.debug("{}", .{func.fmtTracking()});1677 verbose_tracking_log.debug("{f}", .{func.fmtTracking()});
1678}1678}
16791679
1680fn getValue(func: *Func, value: MCValue, inst: ?Air.Inst.Index) !void {1680fn getValue(func: *Func, value: MCValue, inst: ?Air.Inst.Index) !void {
...@@ -1891,7 +1891,7 @@ fn splitType(func: *Func, ty: Type) ![2]Type {...@@ -1891,7 +1891,7 @@ fn splitType(func: *Func, ty: Type) ![2]Type {
1891 else => return func.fail("TODO: splitType class {}", .{class}),1891 else => return func.fail("TODO: splitType class {}", .{class}),
1892 };1892 };
1893 } else if (parts[0].abiSize(zcu) + parts[1].abiSize(zcu) == ty.abiSize(zcu)) return parts;1893 } else if (parts[0].abiSize(zcu) + parts[1].abiSize(zcu) == ty.abiSize(zcu)) return parts;
1894 return func.fail("TODO implement splitType for {}", .{ty.fmt(func.pt)});1894 return func.fail("TODO implement splitType for {f}", .{ty.fmt(func.pt)});
1895}1895}
18961896
1897/// Truncates the value in the register in place.1897/// Truncates the value in the register in place.
...@@ -2004,7 +2004,7 @@ fn allocMemPtr(func: *Func, inst: Air.Inst.Index) !FrameIndex {...@@ -2004,7 +2004,7 @@ fn allocMemPtr(func: *Func, inst: Air.Inst.Index) !FrameIndex {
2004 const val_ty = ptr_ty.childType(zcu);2004 const val_ty = ptr_ty.childType(zcu);
2005 return func.allocFrameIndex(FrameAlloc.init(.{2005 return func.allocFrameIndex(FrameAlloc.init(.{
2006 .size = math.cast(u32, val_ty.abiSize(zcu)) orelse {2006 .size = math.cast(u32, val_ty.abiSize(zcu)) orelse {
2007 return func.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(pt)});2007 return func.fail("type '{f}' too big to fit into stack frame", .{val_ty.fmt(pt)});
2008 },2008 },
2009 .alignment = ptr_ty.ptrAlignment(zcu).max(.@"1"),2009 .alignment = ptr_ty.ptrAlignment(zcu).max(.@"1"),
2010 }));2010 }));
...@@ -2144,7 +2144,7 @@ pub fn spillRegisters(func: *Func, comptime registers: []const Register) !void {...@@ -2144,7 +2144,7 @@ pub fn spillRegisters(func: *Func, comptime registers: []const Register) !void {
2144/// allocated. A second call to `copyToTmpRegister` may return the same register.2144/// allocated. A second call to `copyToTmpRegister` may return the same register.
2145/// This can have a side effect of spilling instructions to the stack to free up a register.2145/// This can have a side effect of spilling instructions to the stack to free up a register.
2146fn copyToTmpRegister(func: *Func, ty: Type, mcv: MCValue) !Register {2146fn copyToTmpRegister(func: *Func, ty: Type, mcv: MCValue) !Register {
2147 log.debug("copyToTmpRegister ty: {}", .{ty.fmt(func.pt)});2147 log.debug("copyToTmpRegister ty: {f}", .{ty.fmt(func.pt)});
2148 const reg = try func.register_manager.allocReg(null, func.regTempClassForType(ty));2148 const reg = try func.register_manager.allocReg(null, func.regTempClassForType(ty));
2149 try func.genSetReg(ty, reg, mcv);2149 try func.genSetReg(ty, reg, mcv);
2150 return reg;2150 return reg;
...@@ -2229,7 +2229,7 @@ fn airIntCast(func: *Func, inst: Air.Inst.Index) !void {...@@ -2229,7 +2229,7 @@ fn airIntCast(func: *Func, inst: Air.Inst.Index) !void {
2229 break :result null; // TODO2229 break :result null; // TODO
22302230
2231 break :result dst_mcv;2231 break :result dst_mcv;
2232 } orelse return func.fail("TODO: implement airIntCast from {} to {}", .{2232 } orelse return func.fail("TODO: implement airIntCast from {f} to {f}", .{
2233 src_ty.fmt(pt), dst_ty.fmt(pt),2233 src_ty.fmt(pt), dst_ty.fmt(pt),
2234 });2234 });
22352235
...@@ -2617,7 +2617,7 @@ fn genBinOp(...@@ -2617,7 +2617,7 @@ fn genBinOp(
2617 .add_sat,2617 .add_sat,
2618 => {2618 => {
2619 if (bit_size != 64 or !is_unsigned)2619 if (bit_size != 64 or !is_unsigned)
2620 return func.fail("TODO: genBinOp ty: {}", .{lhs_ty.fmt(pt)});2620 return func.fail("TODO: genBinOp ty: {f}", .{lhs_ty.fmt(pt)});
26212621
2622 const tmp_reg = try func.copyToTmpRegister(rhs_ty, .{ .register = rhs_reg });2622 const tmp_reg = try func.copyToTmpRegister(rhs_ty, .{ .register = rhs_reg });
2623 const tmp_lock = func.register_manager.lockRegAssumeUnused(tmp_reg);2623 const tmp_lock = func.register_manager.lockRegAssumeUnused(tmp_reg);
...@@ -4049,7 +4049,7 @@ fn airGetUnionTag(func: *Func, inst: Air.Inst.Index) !void {...@@ -4049,7 +4049,7 @@ fn airGetUnionTag(func: *Func, inst: Air.Inst.Index) !void {
4049 );4049 );
4050 } else {4050 } else {
4051 return func.fail(4051 return func.fail(
4052 "TODO implement get_union_tag for ABI larger than 8 bytes and operand {}, tag {}",4052 "TODO implement get_union_tag for ABI larger than 8 bytes and operand {}, tag {f}",
4053 .{ frame_mcv, tag_ty.fmt(pt) },4053 .{ frame_mcv, tag_ty.fmt(pt) },
4054 );4054 );
4055 }4055 }
...@@ -4170,7 +4170,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void {...@@ -4170,7 +4170,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void {
41704170
4171 switch (scalar_ty.zigTypeTag(zcu)) {4171 switch (scalar_ty.zigTypeTag(zcu)) {
4172 .int => if (ty.zigTypeTag(zcu) == .vector) {4172 .int => if (ty.zigTypeTag(zcu) == .vector) {
4173 return func.fail("TODO implement airAbs for {}", .{ty.fmt(pt)});4173 return func.fail("TODO implement airAbs for {f}", .{ty.fmt(pt)});
4174 } else {4174 } else {
4175 const int_info = scalar_ty.intInfo(zcu);4175 const int_info = scalar_ty.intInfo(zcu);
4176 const int_bits = int_info.bits;4176 const int_bits = int_info.bits;
...@@ -4251,7 +4251,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void {...@@ -4251,7 +4251,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void {
42514251
4252 break :result return_mcv;4252 break :result return_mcv;
4253 },4253 },
4254 else => return func.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(pt)}),4254 else => return func.fail("TODO: implement airAbs {f}", .{scalar_ty.fmt(pt)}),
4255 }4255 }
42564256
4257 break :result .unreach;4257 break :result .unreach;
...@@ -4315,7 +4315,7 @@ fn airByteSwap(func: *Func, inst: Air.Inst.Index) !void {...@@ -4315,7 +4315,7 @@ fn airByteSwap(func: *Func, inst: Air.Inst.Index) !void {
43154315
4316 break :result dest_mcv;4316 break :result dest_mcv;
4317 },4317 },
4318 else => return func.fail("TODO: airByteSwap {}", .{ty.fmt(pt)}),4318 else => return func.fail("TODO: airByteSwap {f}", .{ty.fmt(pt)}),
4319 }4319 }
4320 };4320 };
4321 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });4321 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
...@@ -4381,7 +4381,7 @@ fn airUnaryMath(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void {...@@ -4381,7 +4381,7 @@ fn airUnaryMath(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void {
4381 else => return func.fail("TODO: airUnaryMath Float {s}", .{@tagName(tag)}),4381 else => return func.fail("TODO: airUnaryMath Float {s}", .{@tagName(tag)}),
4382 }4382 }
4383 },4383 },
4384 else => return func.fail("TODO: airUnaryMath ty: {}", .{ty.fmt(pt)}),4384 else => return func.fail("TODO: airUnaryMath ty: {f}", .{ty.fmt(pt)}),
4385 }4385 }
43864386
4387 break :result MCValue{ .register = dst_reg };4387 break :result MCValue{ .register = dst_reg };
...@@ -4481,7 +4481,7 @@ fn load(func: *Func, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerErro...@@ -4481,7 +4481,7 @@ fn load(func: *Func, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerErro
4481 const zcu = pt.zcu;4481 const zcu = pt.zcu;
4482 const dst_ty = ptr_ty.childType(zcu);4482 const dst_ty = ptr_ty.childType(zcu);
44834483
4484 log.debug("loading {}:{} into {}", .{ ptr_mcv, ptr_ty.fmt(pt), dst_mcv });4484 log.debug("loading {}:{f} into {}", .{ ptr_mcv, ptr_ty.fmt(pt), dst_mcv });
44854485
4486 switch (ptr_mcv) {4486 switch (ptr_mcv) {
4487 .none,4487 .none,
...@@ -4534,7 +4534,7 @@ fn airStore(func: *Func, inst: Air.Inst.Index, safety: bool) !void {...@@ -4534,7 +4534,7 @@ fn airStore(func: *Func, inst: Air.Inst.Index, safety: bool) !void {
4534fn store(func: *Func, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type) !void {4534fn store(func: *Func, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type) !void {
4535 const zcu = func.pt.zcu;4535 const zcu = func.pt.zcu;
4536 const src_ty = ptr_ty.childType(zcu);4536 const src_ty = ptr_ty.childType(zcu);
4537 log.debug("storing {}:{} in {}:{}", .{ src_mcv, src_ty.fmt(func.pt), ptr_mcv, ptr_ty.fmt(func.pt) });4537 log.debug("storing {}:{f} in {}:{f}", .{ src_mcv, src_ty.fmt(func.pt), ptr_mcv, ptr_ty.fmt(func.pt) });
45384538
4539 switch (ptr_mcv) {4539 switch (ptr_mcv) {
4540 .none => unreachable,4540 .none => unreachable,
...@@ -7289,7 +7289,7 @@ fn airBitCast(func: *Func, inst: Air.Inst.Index) !void {...@@ -7289,7 +7289,7 @@ fn airBitCast(func: *Func, inst: Air.Inst.Index) !void {
7289 const bit_size = dst_ty.bitSize(zcu);7289 const bit_size = dst_ty.bitSize(zcu);
7290 if (abi_size * 8 <= bit_size) break :result dst_mcv;7290 if (abi_size * 8 <= bit_size) break :result dst_mcv;
72917291
7292 return func.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(pt), dst_ty.fmt(pt) });7292 return func.fail("TODO: airBitCast {f} to {f}", .{ src_ty.fmt(pt), dst_ty.fmt(pt) });
7293 };7293 };
7294 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });7294 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
7295}7295}
...@@ -8105,7 +8105,7 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void {...@@ -8105,7 +8105,7 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void {
8105 );8105 );
8106 break :result .{ .load_frame = .{ .index = frame_index } };8106 break :result .{ .load_frame = .{ .index = frame_index } };
8107 },8107 },
8108 else => return func.fail("TODO: airAggregate {}", .{result_ty.fmt(pt)}),8108 else => return func.fail("TODO: airAggregate {f}", .{result_ty.fmt(pt)}),
8109 }8109 }
8110 };8110 };
81118111
...@@ -8306,7 +8306,7 @@ fn resolveCallingConventionValues(...@@ -8306,7 +8306,7 @@ fn resolveCallingConventionValues(
8306 };8306 };
83078307
8308 result.return_value = switch (ret_tracking_i) {8308 result.return_value = switch (ret_tracking_i) {
8309 else => return func.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(pt), ret_tracking_i }),8309 else => return func.fail("ty {f} took {} tracking return indices", .{ ret_ty.fmt(pt), ret_tracking_i }),
8310 1 => ret_tracking[0],8310 1 => ret_tracking[0],
8311 2 => InstTracking.init(.{ .register_pair = .{8311 2 => InstTracking.init(.{ .register_pair = .{
8312 ret_tracking[0].short.register, ret_tracking[1].short.register,8312 ret_tracking[0].short.register, ret_tracking[1].short.register,
...@@ -8361,7 +8361,7 @@ fn resolveCallingConventionValues(...@@ -8361,7 +8361,7 @@ fn resolveCallingConventionValues(
8361 else => return func.fail("TODO: C calling convention arg class {}", .{class}),8361 else => return func.fail("TODO: C calling convention arg class {}", .{class}),
8362 } else {8362 } else {
8363 arg.* = switch (arg_mcv_i) {8363 arg.* = switch (arg_mcv_i) {
8364 else => return func.fail("ty {} took {} tracking arg indices", .{ ty.fmt(pt), arg_mcv_i }),8364 else => return func.fail("ty {f} took {} tracking arg indices", .{ ty.fmt(pt), arg_mcv_i }),
8365 1 => arg_mcv[0],8365 1 => arg_mcv[0],
8366 2 => .{ .register_pair = .{ arg_mcv[0].register, arg_mcv[1].register } },8366 2 => .{ .register_pair = .{ arg_mcv[0].register, arg_mcv[1].register } },
8367 };8367 };
src/arch/riscv64/Emit.zig+1-1
...@@ -172,7 +172,7 @@ const Reloc = struct {...@@ -172,7 +172,7 @@ const Reloc = struct {
172172
173fn fixupRelocs(emit: *Emit) Error!void {173fn fixupRelocs(emit: *Emit) Error!void {
174 for (emit.relocs.items) |reloc| {174 for (emit.relocs.items) |reloc| {
175 log.debug("target inst: {}", .{emit.lower.mir.instructions.get(reloc.target)});175 log.debug("target inst: {f}", .{emit.lower.mir.instructions.get(reloc.target)});
176 const target = emit.code_offset_mapping.get(reloc.target) orelse176 const target = emit.code_offset_mapping.get(reloc.target) orelse
177 return emit.fail("relocation target not found!", .{});177 return emit.fail("relocation target not found!", .{});
178178
src/arch/riscv64/Lower.zig+1-1
...@@ -61,7 +61,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index, options: struct {...@@ -61,7 +61,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index, options: struct {
61 defer lower.result_relocs_len = undefined;61 defer lower.result_relocs_len = undefined;
6262
63 const inst = lower.mir.instructions.get(index);63 const inst = lower.mir.instructions.get(index);
64 log.debug("lowerMir {}", .{inst});64 log.debug("lowerMir {f}", .{inst});
65 switch (inst.tag) {65 switch (inst.tag) {
66 else => try lower.generic(inst),66 else => try lower.generic(inst),
67 .pseudo_dbg_line_column,67 .pseudo_dbg_line_column,
src/codegen/c.zig+59-69
...@@ -1282,7 +1282,7 @@ pub const DeclGen = struct {...@@ -1282,7 +1282,7 @@ pub const DeclGen = struct {
1282 }1282 }
1283 const ai = ty.arrayInfo(zcu);1283 const ai = ty.arrayInfo(zcu);
1284 if (ai.elem_type.eql(.u8, zcu)) {1284 if (ai.elem_type.eql(.u8, zcu)) {
1285 var literal = stringLiteral(writer, ty.arrayLenIncludingSentinel(zcu));1285 var literal: StringLiteral = .init(writer, ty.arrayLenIncludingSentinel(zcu));
1286 try literal.start();1286 try literal.start();
1287 var index: usize = 0;1287 var index: usize = 0;
1288 while (index < ai.len) : (index += 1) {1288 while (index < ai.len) : (index += 1) {
...@@ -1841,7 +1841,7 @@ pub const DeclGen = struct {...@@ -1841,7 +1841,7 @@ pub const DeclGen = struct {
1841 const ai = ty.arrayInfo(zcu);1841 const ai = ty.arrayInfo(zcu);
1842 if (ai.elem_type.eql(.u8, zcu)) {1842 if (ai.elem_type.eql(.u8, zcu)) {
1843 const c_len = ty.arrayLenIncludingSentinel(zcu);1843 const c_len = ty.arrayLenIncludingSentinel(zcu);
1844 var literal = stringLiteral(writer, c_len);1844 var literal: StringLiteral = .init(writer, c_len);
1845 try literal.start();1845 try literal.start();
1846 var index: u64 = 0;1846 var index: u64 = 0;
1847 while (index < c_len) : (index += 1)1847 while (index < c_len) : (index += 1)
...@@ -8143,7 +8143,12 @@ fn compareOperatorC(operator: std.math.CompareOperator) []const u8 {...@@ -8143,7 +8143,12 @@ fn compareOperatorC(operator: std.math.CompareOperator) []const u8 {
8143 };8143 };
8144}8144}
81458145
8146fn StringLiteral(comptime WriterType: type) type {8146const StringLiteral = struct {
8147 len: usize,
8148 cur_len: usize,
8149 start_count: usize,
8150 writer: *std.io.Writer,
8151
8147 // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal,8152 // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal,
8148 // regardless of the length of the string literal initializing it. Array initializer syntax is8153 // regardless of the length of the string literal initializing it. Array initializer syntax is
8149 // used instead.8154 // used instead.
...@@ -8155,81 +8160,66 @@ fn StringLiteral(comptime WriterType: type) type {...@@ -8155,81 +8160,66 @@ fn StringLiteral(comptime WriterType: type) type {
8155 const max_char_len = 4;8160 const max_char_len = 4;
8156 const max_literal_len = @min(16380 - max_char_len, 4095);8161 const max_literal_len = @min(16380 - max_char_len, 4095);
81578162
8158 return struct {8163 fn init(writer: *std.io.Writer, len: usize) StringLiteral {
8159 len: u64,8164 return .{
8160 cur_len: u64 = 0,8165 .cur_len = 0,
8161 counting_writer: std.io.CountingWriter(WriterType),8166 .len = len,
81628167 .start_count = writer.count,
8163 pub const Error = if (WriterType == *std.io.Writer) error{WriteFailed} else WriterType.Error;8168 .writer = writer,
81648169 };
8165 const Self = @This();8170 }
81668171
8167 pub fn start(self: *Self) Error!void {8172 pub fn start(sl: *StringLiteral) std.io.Writer.Error!void {
8168 const writer = self.counting_writer.writer();8173 if (sl.len <= max_string_initializer_len) {
8169 if (self.len <= max_string_initializer_len) {8174 try sl.writer.writeByte('\"');
8170 try writer.writeByte('\"');8175 } else {
8171 } else {8176 try sl.writer.writeByte('{');
8172 try writer.writeByte('{');
8173 }
8174 }8177 }
8178 }
81758179
8176 pub fn end(self: *Self) Error!void {8180 pub fn end(sl: *StringLiteral) std.io.Writer.Error!void {
8177 const writer = self.counting_writer.writer();8181 if (sl.len <= max_string_initializer_len) {
8178 if (self.len <= max_string_initializer_len) {8182 try sl.writer.writeByte('\"');
8179 try writer.writeByte('\"');8183 } else {
8180 } else {8184 try sl.writer.writeByte('}');
8181 try writer.writeByte('}');
8182 }
8183 }8185 }
8186 }
81848187
8185 fn writeStringLiteralChar(writer: anytype, c: u8) !void {8188 fn writeStringLiteralChar(sl: *StringLiteral, c: u8) std.io.Writer.Error!void {
8186 switch (c) {8189 switch (c) {
8187 7 => try writer.writeAll("\\a"),8190 7 => try sl.writer.writeAll("\\a"),
8188 8 => try writer.writeAll("\\b"),8191 8 => try sl.writer.writeAll("\\b"),
8189 '\t' => try writer.writeAll("\\t"),8192 '\t' => try sl.writer.writeAll("\\t"),
8190 '\n' => try writer.writeAll("\\n"),8193 '\n' => try sl.writer.writeAll("\\n"),
8191 11 => try writer.writeAll("\\v"),8194 11 => try sl.writer.writeAll("\\v"),
8192 12 => try writer.writeAll("\\f"),8195 12 => try sl.writer.writeAll("\\f"),
8193 '\r' => try writer.writeAll("\\r"),8196 '\r' => try sl.writer.writeAll("\\r"),
8194 '"', '\'', '?', '\\' => try writer.print("\\{c}", .{c}),8197 '"', '\'', '?', '\\' => try sl.writer.print("\\{c}", .{c}),
8195 else => switch (c) {8198 else => switch (c) {
8196 ' '...'~' => try writer.writeByte(c),8199 ' '...'~' => try sl.writer.writeByte(c),
8197 else => try writer.print("\\{o:0>3}", .{c}),8200 else => try sl.writer.print("\\{o:0>3}", .{c}),
8198 },8201 },
8199 }
8200 }8202 }
8203 }
82018204
8202 pub fn writeChar(self: *Self, c: u8) Error!void {8205 pub fn writeChar(sl: *StringLiteral, c: u8) std.io.Writer.Error!void {
8203 const writer = self.counting_writer.writer();8206 if (sl.len <= max_string_initializer_len) {
8204 if (self.len <= max_string_initializer_len) {8207 if (sl.cur_len == 0 and sl.writer.count - sl.start_count > 1)
8205 if (self.cur_len == 0 and self.counting_writer.bytes_written > 1)8208 try sl.writer.writeAll("\"\"");
8206 try writer.writeAll("\"\"");
8207
8208 const len = self.counting_writer.bytes_written;
8209 try writeStringLiteralChar(writer, c);
82108209
8211 const char_length = self.counting_writer.bytes_written - len;8210 const count = sl.writer.count;
8212 assert(char_length <= max_char_len);8211 try sl.writeStringLiteralChar(c);
8213 self.cur_len += char_length;8212 const char_len = sl.writer.count - count;
8213 assert(char_len <= max_char_len);
8214 sl.cur_len += char_len;
82148215
8215 if (self.cur_len >= max_literal_len) self.cur_len = 0;8216 if (sl.cur_len >= max_literal_len) sl.cur_len = 0;
8216 } else {8217 } else {
8217 if (self.counting_writer.bytes_written > 1) try writer.writeByte(',');8218 if (sl.writer.count - sl.start_count > 1) try sl.writer.writeByte(',');
8218 try writer.print("'\\x{x}'", .{c});8219 try sl.writer.print("'\\x{x}'", .{c});
8219 }
8220 }8220 }
8221 };8221 }
8222}8222};
8223
8224fn stringLiteral(
8225 child_stream: anytype,
8226 len: u64,
8227) StringLiteral(@TypeOf(child_stream)) {
8228 return .{
8229 .len = len,
8230 .counting_writer = std.io.countingWriter(child_stream),
8231 };
8232}
82338223
8234const FormatStringContext = struct {8224const FormatStringContext = struct {
8235 str: []const u8,8225 str: []const u8,
...@@ -8237,7 +8227,7 @@ const FormatStringContext = struct {...@@ -8237,7 +8227,7 @@ const FormatStringContext = struct {
8237};8227};
82388228
8239fn formatStringLiteral(data: FormatStringContext, writer: *std.io.Writer) std.io.Writer.Error!void {8229fn formatStringLiteral(data: FormatStringContext, writer: *std.io.Writer) std.io.Writer.Error!void {
8240 var literal = stringLiteral(writer, data.str.len + @intFromBool(data.sentinel != null));8230 var literal: StringLiteral = .init(writer, data.str.len + @intFromBool(data.sentinel != null));
8241 try literal.start();8231 try literal.start();
8242 for (data.str) |c| try literal.writeChar(c);8232 for (data.str) |c| try literal.writeChar(c);
8243 if (data.sentinel) |sentinel| if (sentinel != 0) try literal.writeChar(sentinel);8233 if (data.sentinel) |sentinel| if (sentinel != 0) try literal.writeChar(sentinel);