From 6314e6f238758ce84a288e9ed990540232d04656 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 2 Jul 2025 14:25:32 -0700 Subject: [PATCH] compiler: fix a bunch of format strings --- src/Package/Fetch.zig | 2 +- src/arch/riscv64/CodeGen.zig | 48 ++++++------- src/arch/riscv64/Emit.zig | 2 +- src/arch/riscv64/Lower.zig | 2 +- src/codegen/c.zig | 128 ++++++++++++++++------------------- 5 files changed, 86 insertions(+), 96 deletions(-) diff --git a/src/Package/Fetch.zig b/src/Package/Fetch.zig index dea41ed2c0..4f6567e985 100644 --- a/src/Package/Fetch.zig +++ b/src/Package/Fetch.zig @@ -227,7 +227,7 @@ pub const JobQueue = struct { } try buf.writer().print( - \\ pub const build_root = "{q}"; + \\ pub const build_root = "{fq}"; \\ , .{fetch.package_root}); diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig index 0c173e9498..9801dc8054 100644 --- a/src/arch/riscv64/CodeGen.zig +++ b/src/arch/riscv64/CodeGen.zig @@ -435,7 +435,7 @@ const InstTracking = struct { fn trackSpill(inst_tracking: *InstTracking, function: *Func, inst: Air.Inst.Index) !void { try function.freeValue(inst_tracking.short); inst_tracking.reuseFrame(); - tracking_log.debug("%{d} => {} (spilled)", .{ inst, inst_tracking.* }); + tracking_log.debug("%{d} => {f} (spilled)", .{ inst, inst_tracking.* }); } fn verifyMaterialize(inst_tracking: InstTracking, target: InstTracking) void { @@ -1015,7 +1015,7 @@ const FormatTrackingData = struct { }; fn formatTracking(data: FormatTrackingData, writer: *std.io.Writer) std.io.Writer.Error!void { var it = data.func.inst_tracking.iterator(); - while (it.next()) |entry| try writer.print("\n%{d} = {}", .{ entry.key_ptr.*, entry.value_ptr.* }); + while (it.next()) |entry| try writer.print("\n%{d} = {f}", .{ entry.key_ptr.*, entry.value_ptr.* }); } fn fmtTracking(func: *Func) std.fmt.Formatter(FormatTrackingData, formatTracking) { return .{ .data = .{ .func = func } }; @@ -1033,7 +1033,7 @@ fn addInst(func: *Func, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { .pseudo_dbg_epilogue_begin, .pseudo_dead, => false, - }) wip_mir_log.debug("{}", .{func.fmtWipMir(result_index)}); + }) wip_mir_log.debug("{f}", .{func.fmtWipMir(result_index)}); return result_index; } @@ -1287,7 +1287,7 @@ fn genLazy(func: *Func, lazy_sym: link.File.LazySymbol) InnerError!void { switch (Type.fromInterned(lazy_sym.ty).zigTypeTag(zcu)) { .@"enum" => { const enum_ty = Type.fromInterned(lazy_sym.ty); - wip_mir_log.debug("{}.@tagName:", .{enum_ty.fmt(pt)}); + wip_mir_log.debug("{f}.@tagName:", .{enum_ty.fmt(pt)}); const param_regs = abi.Registers.Integer.function_arg_regs; const ret_reg = param_regs[0]; @@ -1369,7 +1369,7 @@ fn genLazy(func: *Func, lazy_sym: link.File.LazySymbol) InnerError!void { }); }, else => return func.fail( - "TODO implement {s} for {}", + "TODO implement {s} for {f}", .{ @tagName(lazy_sym.kind), Type.fromInterned(lazy_sym.ty).fmt(pt) }, ), } @@ -1383,8 +1383,8 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void { for (body) |inst| { if (func.liveness.isUnused(inst) and !func.air.mustLower(inst, ip)) continue; - wip_mir_log.debug("{}", .{func.fmtAir(inst)}); - verbose_tracking_log.debug("{}", .{func.fmtTracking()}); + wip_mir_log.debug("{f}", .{func.fmtAir(inst)}); + verbose_tracking_log.debug("{f}", .{func.fmtTracking()}); const old_air_bookkeeping = func.air_bookkeeping; try func.ensureProcessDeathCapacity(Air.Liveness.bpi); @@ -1674,7 +1674,7 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void { } } } - verbose_tracking_log.debug("{}", .{func.fmtTracking()}); + verbose_tracking_log.debug("{f}", .{func.fmtTracking()}); } fn getValue(func: *Func, value: MCValue, inst: ?Air.Inst.Index) !void { @@ -1891,7 +1891,7 @@ fn splitType(func: *Func, ty: Type) ![2]Type { else => return func.fail("TODO: splitType class {}", .{class}), }; } else if (parts[0].abiSize(zcu) + parts[1].abiSize(zcu) == ty.abiSize(zcu)) return parts; - return func.fail("TODO implement splitType for {}", .{ty.fmt(func.pt)}); + return func.fail("TODO implement splitType for {f}", .{ty.fmt(func.pt)}); } /// Truncates the value in the register in place. @@ -2004,7 +2004,7 @@ fn allocMemPtr(func: *Func, inst: Air.Inst.Index) !FrameIndex { const val_ty = ptr_ty.childType(zcu); return func.allocFrameIndex(FrameAlloc.init(.{ .size = math.cast(u32, val_ty.abiSize(zcu)) orelse { - return func.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(pt)}); + return func.fail("type '{f}' too big to fit into stack frame", .{val_ty.fmt(pt)}); }, .alignment = ptr_ty.ptrAlignment(zcu).max(.@"1"), })); @@ -2144,7 +2144,7 @@ pub fn spillRegisters(func: *Func, comptime registers: []const Register) !void { /// allocated. A second call to `copyToTmpRegister` may return the same register. /// This can have a side effect of spilling instructions to the stack to free up a register. fn copyToTmpRegister(func: *Func, ty: Type, mcv: MCValue) !Register { - log.debug("copyToTmpRegister ty: {}", .{ty.fmt(func.pt)}); + log.debug("copyToTmpRegister ty: {f}", .{ty.fmt(func.pt)}); const reg = try func.register_manager.allocReg(null, func.regTempClassForType(ty)); try func.genSetReg(ty, reg, mcv); return reg; @@ -2229,7 +2229,7 @@ fn airIntCast(func: *Func, inst: Air.Inst.Index) !void { break :result null; // TODO break :result dst_mcv; - } orelse return func.fail("TODO: implement airIntCast from {} to {}", .{ + } orelse return func.fail("TODO: implement airIntCast from {f} to {f}", .{ src_ty.fmt(pt), dst_ty.fmt(pt), }); @@ -2617,7 +2617,7 @@ fn genBinOp( .add_sat, => { if (bit_size != 64 or !is_unsigned) - return func.fail("TODO: genBinOp ty: {}", .{lhs_ty.fmt(pt)}); + return func.fail("TODO: genBinOp ty: {f}", .{lhs_ty.fmt(pt)}); const tmp_reg = try func.copyToTmpRegister(rhs_ty, .{ .register = rhs_reg }); const tmp_lock = func.register_manager.lockRegAssumeUnused(tmp_reg); @@ -4049,7 +4049,7 @@ fn airGetUnionTag(func: *Func, inst: Air.Inst.Index) !void { ); } else { return func.fail( - "TODO implement get_union_tag for ABI larger than 8 bytes and operand {}, tag {}", + "TODO implement get_union_tag for ABI larger than 8 bytes and operand {}, tag {f}", .{ frame_mcv, tag_ty.fmt(pt) }, ); } @@ -4170,7 +4170,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void { switch (scalar_ty.zigTypeTag(zcu)) { .int => if (ty.zigTypeTag(zcu) == .vector) { - return func.fail("TODO implement airAbs for {}", .{ty.fmt(pt)}); + return func.fail("TODO implement airAbs for {f}", .{ty.fmt(pt)}); } else { const int_info = scalar_ty.intInfo(zcu); const int_bits = int_info.bits; @@ -4251,7 +4251,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void { break :result return_mcv; }, - else => return func.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(pt)}), + else => return func.fail("TODO: implement airAbs {f}", .{scalar_ty.fmt(pt)}), } break :result .unreach; @@ -4315,7 +4315,7 @@ fn airByteSwap(func: *Func, inst: Air.Inst.Index) !void { break :result dest_mcv; }, - else => return func.fail("TODO: airByteSwap {}", .{ty.fmt(pt)}), + else => return func.fail("TODO: airByteSwap {f}", .{ty.fmt(pt)}), } }; 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 { else => return func.fail("TODO: airUnaryMath Float {s}", .{@tagName(tag)}), } }, - else => return func.fail("TODO: airUnaryMath ty: {}", .{ty.fmt(pt)}), + else => return func.fail("TODO: airUnaryMath ty: {f}", .{ty.fmt(pt)}), } break :result MCValue{ .register = dst_reg }; @@ -4481,7 +4481,7 @@ fn load(func: *Func, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerErro const zcu = pt.zcu; const dst_ty = ptr_ty.childType(zcu); - log.debug("loading {}:{} into {}", .{ ptr_mcv, ptr_ty.fmt(pt), dst_mcv }); + log.debug("loading {}:{f} into {}", .{ ptr_mcv, ptr_ty.fmt(pt), dst_mcv }); switch (ptr_mcv) { .none, @@ -4534,7 +4534,7 @@ fn airStore(func: *Func, inst: Air.Inst.Index, safety: bool) !void { fn store(func: *Func, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type) !void { const zcu = func.pt.zcu; const src_ty = ptr_ty.childType(zcu); - log.debug("storing {}:{} in {}:{}", .{ src_mcv, src_ty.fmt(func.pt), ptr_mcv, ptr_ty.fmt(func.pt) }); + log.debug("storing {}:{f} in {}:{f}", .{ src_mcv, src_ty.fmt(func.pt), ptr_mcv, ptr_ty.fmt(func.pt) }); switch (ptr_mcv) { .none => unreachable, @@ -7289,7 +7289,7 @@ fn airBitCast(func: *Func, inst: Air.Inst.Index) !void { const bit_size = dst_ty.bitSize(zcu); if (abi_size * 8 <= bit_size) break :result dst_mcv; - return func.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(pt), dst_ty.fmt(pt) }); + return func.fail("TODO: airBitCast {f} to {f}", .{ src_ty.fmt(pt), dst_ty.fmt(pt) }); }; return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); } @@ -8105,7 +8105,7 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { ); break :result .{ .load_frame = .{ .index = frame_index } }; }, - else => return func.fail("TODO: airAggregate {}", .{result_ty.fmt(pt)}), + else => return func.fail("TODO: airAggregate {f}", .{result_ty.fmt(pt)}), } }; @@ -8306,7 +8306,7 @@ fn resolveCallingConventionValues( }; result.return_value = switch (ret_tracking_i) { - else => return func.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(pt), ret_tracking_i }), + else => return func.fail("ty {f} took {} tracking return indices", .{ ret_ty.fmt(pt), ret_tracking_i }), 1 => ret_tracking[0], 2 => InstTracking.init(.{ .register_pair = .{ ret_tracking[0].short.register, ret_tracking[1].short.register, @@ -8361,7 +8361,7 @@ fn resolveCallingConventionValues( else => return func.fail("TODO: C calling convention arg class {}", .{class}), } else { arg.* = switch (arg_mcv_i) { - else => return func.fail("ty {} took {} tracking arg indices", .{ ty.fmt(pt), arg_mcv_i }), + else => return func.fail("ty {f} took {} tracking arg indices", .{ ty.fmt(pt), arg_mcv_i }), 1 => arg_mcv[0], 2 => .{ .register_pair = .{ arg_mcv[0].register, arg_mcv[1].register } }, }; diff --git a/src/arch/riscv64/Emit.zig b/src/arch/riscv64/Emit.zig index 0561eb2019..41875ed789 100644 --- a/src/arch/riscv64/Emit.zig +++ b/src/arch/riscv64/Emit.zig @@ -172,7 +172,7 @@ const Reloc = struct { fn fixupRelocs(emit: *Emit) Error!void { for (emit.relocs.items) |reloc| { - log.debug("target inst: {}", .{emit.lower.mir.instructions.get(reloc.target)}); + log.debug("target inst: {f}", .{emit.lower.mir.instructions.get(reloc.target)}); const target = emit.code_offset_mapping.get(reloc.target) orelse return emit.fail("relocation target not found!", .{}); diff --git a/src/arch/riscv64/Lower.zig b/src/arch/riscv64/Lower.zig index ff3d79ba41..08bb3e2b93 100644 --- a/src/arch/riscv64/Lower.zig +++ b/src/arch/riscv64/Lower.zig @@ -61,7 +61,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index, options: struct { defer lower.result_relocs_len = undefined; const inst = lower.mir.instructions.get(index); - log.debug("lowerMir {}", .{inst}); + log.debug("lowerMir {f}", .{inst}); switch (inst.tag) { else => try lower.generic(inst), .pseudo_dbg_line_column, diff --git a/src/codegen/c.zig b/src/codegen/c.zig index f4ae7996bd..2b2df72a11 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1282,7 +1282,7 @@ pub const DeclGen = struct { } const ai = ty.arrayInfo(zcu); if (ai.elem_type.eql(.u8, zcu)) { - var literal = stringLiteral(writer, ty.arrayLenIncludingSentinel(zcu)); + var literal: StringLiteral = .init(writer, ty.arrayLenIncludingSentinel(zcu)); try literal.start(); var index: usize = 0; while (index < ai.len) : (index += 1) { @@ -1841,7 +1841,7 @@ pub const DeclGen = struct { const ai = ty.arrayInfo(zcu); if (ai.elem_type.eql(.u8, zcu)) { const c_len = ty.arrayLenIncludingSentinel(zcu); - var literal = stringLiteral(writer, c_len); + var literal: StringLiteral = .init(writer, c_len); try literal.start(); var index: u64 = 0; while (index < c_len) : (index += 1) @@ -8143,7 +8143,12 @@ fn compareOperatorC(operator: std.math.CompareOperator) []const u8 { }; } -fn StringLiteral(comptime WriterType: type) type { +const StringLiteral = struct { + len: usize, + cur_len: usize, + start_count: usize, + writer: *std.io.Writer, + // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal, // regardless of the length of the string literal initializing it. Array initializer syntax is // used instead. @@ -8155,81 +8160,66 @@ fn StringLiteral(comptime WriterType: type) type { const max_char_len = 4; const max_literal_len = @min(16380 - max_char_len, 4095); - return struct { - len: u64, - cur_len: u64 = 0, - counting_writer: std.io.CountingWriter(WriterType), + fn init(writer: *std.io.Writer, len: usize) StringLiteral { + return .{ + .cur_len = 0, + .len = len, + .start_count = writer.count, + .writer = writer, + }; + } - pub const Error = if (WriterType == *std.io.Writer) error{WriteFailed} else WriterType.Error; - - const Self = @This(); - - pub fn start(self: *Self) Error!void { - const writer = self.counting_writer.writer(); - if (self.len <= max_string_initializer_len) { - try writer.writeByte('\"'); - } else { - try writer.writeByte('{'); - } + pub fn start(sl: *StringLiteral) std.io.Writer.Error!void { + if (sl.len <= max_string_initializer_len) { + try sl.writer.writeByte('\"'); + } else { + try sl.writer.writeByte('{'); } + } - pub fn end(self: *Self) Error!void { - const writer = self.counting_writer.writer(); - if (self.len <= max_string_initializer_len) { - try writer.writeByte('\"'); - } else { - try writer.writeByte('}'); - } + pub fn end(sl: *StringLiteral) std.io.Writer.Error!void { + if (sl.len <= max_string_initializer_len) { + try sl.writer.writeByte('\"'); + } else { + try sl.writer.writeByte('}'); } + } - fn writeStringLiteralChar(writer: anytype, c: u8) !void { - switch (c) { - 7 => try writer.writeAll("\\a"), - 8 => try writer.writeAll("\\b"), - '\t' => try writer.writeAll("\\t"), - '\n' => try writer.writeAll("\\n"), - 11 => try writer.writeAll("\\v"), - 12 => try writer.writeAll("\\f"), - '\r' => try writer.writeAll("\\r"), - '"', '\'', '?', '\\' => try writer.print("\\{c}", .{c}), - else => switch (c) { - ' '...'~' => try writer.writeByte(c), - else => try writer.print("\\{o:0>3}", .{c}), - }, - } + fn writeStringLiteralChar(sl: *StringLiteral, c: u8) std.io.Writer.Error!void { + switch (c) { + 7 => try sl.writer.writeAll("\\a"), + 8 => try sl.writer.writeAll("\\b"), + '\t' => try sl.writer.writeAll("\\t"), + '\n' => try sl.writer.writeAll("\\n"), + 11 => try sl.writer.writeAll("\\v"), + 12 => try sl.writer.writeAll("\\f"), + '\r' => try sl.writer.writeAll("\\r"), + '"', '\'', '?', '\\' => try sl.writer.print("\\{c}", .{c}), + else => switch (c) { + ' '...'~' => try sl.writer.writeByte(c), + else => try sl.writer.print("\\{o:0>3}", .{c}), + }, } + } - pub fn writeChar(self: *Self, c: u8) Error!void { - const writer = self.counting_writer.writer(); - if (self.len <= max_string_initializer_len) { - if (self.cur_len == 0 and self.counting_writer.bytes_written > 1) - try writer.writeAll("\"\""); + pub fn writeChar(sl: *StringLiteral, c: u8) std.io.Writer.Error!void { + if (sl.len <= max_string_initializer_len) { + if (sl.cur_len == 0 and sl.writer.count - sl.start_count > 1) + try sl.writer.writeAll("\"\""); - const len = self.counting_writer.bytes_written; - try writeStringLiteralChar(writer, c); + const count = sl.writer.count; + try sl.writeStringLiteralChar(c); + const char_len = sl.writer.count - count; + assert(char_len <= max_char_len); + sl.cur_len += char_len; - const char_length = self.counting_writer.bytes_written - len; - assert(char_length <= max_char_len); - self.cur_len += char_length; - - if (self.cur_len >= max_literal_len) self.cur_len = 0; - } else { - if (self.counting_writer.bytes_written > 1) try writer.writeByte(','); - try writer.print("'\\x{x}'", .{c}); - } + if (sl.cur_len >= max_literal_len) sl.cur_len = 0; + } else { + if (sl.writer.count - sl.start_count > 1) try sl.writer.writeByte(','); + try sl.writer.print("'\\x{x}'", .{c}); } - }; -} - -fn stringLiteral( - child_stream: anytype, - len: u64, -) StringLiteral(@TypeOf(child_stream)) { - return .{ - .len = len, - .counting_writer = std.io.countingWriter(child_stream), - }; -} + } +}; const FormatStringContext = struct { str: []const u8, @@ -8237,7 +8227,7 @@ const FormatStringContext = struct { }; fn formatStringLiteral(data: FormatStringContext, writer: *std.io.Writer) std.io.Writer.Error!void { - var literal = stringLiteral(writer, data.str.len + @intFromBool(data.sentinel != null)); + var literal: StringLiteral = .init(writer, data.str.len + @intFromBool(data.sentinel != null)); try literal.start(); for (data.str) |c| try literal.writeChar(c); if (data.sentinel) |sentinel| if (sentinel != 0) try literal.writeChar(sentinel);