cbe: port to new std.io.BufferedWriter API

when rebasing I gave up on the conflicts of src/link/C.zig and copied
the file from origin/master which was 710632b45cd7a7081af82af418eb3e405f7aa35e
This commit is contained in:
Jacob Young 2025-04-21 23:28:33 -04:00 committed by Andrew Kelley
parent 4dc3a9444c
commit fceec91f77
15 changed files with 2052 additions and 1958 deletions

View File

@ -1790,7 +1790,7 @@ const ElfDumper = struct {
.p32 => @sizeOf(u32),
.p64 => @sizeOf(u64),
};
try br.discard(num * ptr_size);
_ = try br.discard(.limited(num * ptr_size));
const strtab = br.bufferContents();
assert(ctx.symtab.len == 0);
@ -2569,7 +2569,7 @@ const WasmDumper = struct {
if (!flags.passive) try parseDumpInit(step, br, bw);
const size = try br.takeLeb128(u32);
try bw.print("size {d}\n", .{size});
try br.discard(size); // we do not care about the content of the segments
_ = try br.discard(.limited(size)); // we do not care about the content of the segments
}
},
else => unreachable,

View File

@ -304,8 +304,8 @@ pub fn dumpHexFallible(bw: *std.io.BufferedWriter, ttyconf: std.io.tty.Config, b
test dumpHexFallible {
const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 };
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var bw = aw.init(std.testing.allocator);
defer aw.deinit();
try dumpHexFallible(&bw, .no_color, bytes);
const expected = try std.fmt.allocPrint(std.testing.allocator,

View File

@ -856,6 +856,7 @@ pub fn count(comptime fmt: []const u8, args: anytype) usize {
pub fn allocPrint(gpa: Allocator, comptime fmt: []const u8, args: anytype) Allocator.Error![]u8 {
var aw: std.io.AllocatingWriter = undefined;
try aw.initCapacity(gpa, fmt.len);
defer aw.deinit();
aw.buffered_writer.print(fmt, args) catch |err| switch (err) {
error.WriteFailed => return error.OutOfMemory,
};
@ -870,6 +871,7 @@ pub fn allocPrintSentinel(
) Allocator.Error![:sentinel]u8 {
var aw: std.io.AllocatingWriter = undefined;
try aw.initCapacity(gpa, fmt.len);
defer aw.deinit();
aw.buffered_writer.print(fmt, args) catch |err| switch (err) {
error.WriteFailed => return error.OutOfMemory,
};

View File

@ -934,7 +934,7 @@ pub fn writeFileAll(self: File, in_file: File, options: BufferedWriter.WriteFile
var buffer: [2000]u8 = undefined;
var bw = file_writer.interface().buffered(&buffer);
bw.writeFileAll(in_file, options) catch |err| switch (err) {
error.WriteFailed => if (file_writer.err) |_| unreachable else |e| return e,
error.WriteFailed => return file_writer.err.?,
else => |e| return e,
};
}
@ -1232,7 +1232,7 @@ pub const Reader = struct {
pub const Writer = struct {
file: File,
err: WriteError!void = {},
err: ?WriteError = null,
mode: Writer.Mode = .positional,
pos: u64 = 0,
sendfile_err: ?SendfileError = null,

View File

@ -73,7 +73,7 @@ pub fn readVecAll(br: *BufferedReader, data: [][]u8) Reader.Error!void {
defer data[index] = untruncated;
truncate += try br.readVec(data[index..]);
}
while (index < data.len and truncate <= data[index].len) {
while (index < data.len and truncate >= data[index].len) {
truncate -= data[index].len;
index += 1;
}

View File

@ -150,7 +150,7 @@ pub fn writeVecAll(bw: *BufferedWriter, data: [][]const u8) Writer.Error!void {
defer data[index] = untruncated;
truncate += try bw.writeVec(data[index..]);
}
while (index < data.len and truncate <= data[index].len) {
while (index < data.len and truncate >= data[index].len) {
truncate -= data[index].len;
index += 1;
}

View File

@ -177,8 +177,8 @@ pub const ReadAllocError = std.mem.Allocator.Error || ShortError;
pub fn readAlloc(r: Reader, gpa: std.mem.Allocator, max_size: usize) ReadAllocError![]u8 {
const readFn = r.vtable.read;
var aw: std.io.AllocatingWriter = undefined;
errdefer aw.deinit();
aw.init(gpa);
errdefer aw.deinit();
var remaining = max_size;
while (remaining > 0) {
const n = readFn(r.context, &aw.buffered_writer, .limited(remaining)) catch |err| switch (err) {

View File

@ -1053,8 +1053,8 @@ fn expectSerializeEqual(
options: SerializeOptions,
) !void {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
const bw = aw.init(std.testing.allocator);
defer aw.deinit();
try serialize(value, options, bw);
try std.testing.expectEqualStrings(expected, aw.getWritten());
@ -1155,8 +1155,8 @@ test "std.zon stringify whitespace, high level API" {
test "std.zon stringify whitespace, low level API" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
for ([2]bool{ true, false }) |whitespace| {
s.options = .{ .whitespace = whitespace };
@ -1512,8 +1512,8 @@ test "std.zon stringify whitespace, low level API" {
test "std.zon stringify utf8 codepoints" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
// Printable ASCII
try s.int('a');
@ -1622,8 +1622,8 @@ test "std.zon stringify utf8 codepoints" {
test "std.zon stringify strings" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
// Minimal case
try s.string("abc⚡\n");
@ -1692,8 +1692,8 @@ test "std.zon stringify strings" {
test "std.zon stringify multiline strings" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
inline for (.{ true, false }) |whitespace| {
s.options.whitespace = whitespace;
@ -1912,8 +1912,8 @@ test "std.zon stringify skip default fields" {
test "std.zon depth limits" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
const bw = aw.init(std.testing.allocator);
defer aw.deinit();
const Recurse = struct { r: []const @This() };
@ -2173,8 +2173,8 @@ test "std.zon stringify primitives" {
test "std.zon stringify ident" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
try expectSerializeEqual(".{ .a = 0 }", .{ .a = 0 }, .{});
try s.ident("a");
@ -2220,8 +2220,8 @@ test "std.zon stringify ident" {
test "std.zon stringify as tuple" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
// Tuples
try s.tuple(.{ 1, 2 }, .{});
@ -2241,8 +2241,8 @@ test "std.zon stringify as tuple" {
test "std.zon stringify as float" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
// Comptime float
try s.float(2.5);
@ -2345,8 +2345,8 @@ test "std.zon pointers" {
test "std.zon tuple/struct field" {
var aw: std.io.AllocatingWriter = undefined;
defer aw.deinit();
var s: Serializer = .{ .writer = aw.init(std.testing.allocator) };
defer aw.deinit();
// Test on structs
{

View File

@ -37371,6 +37371,7 @@ fn notePathToComptimeAllocPtr(sema: *Sema, msg: *Zcu.ErrorMsg, src: LazySrcLoc,
var second_path_aw: std.io.AllocatingWriter = undefined;
second_path_aw.init(arena);
defer second_path_aw.deinit();
const inter_name = try std.fmt.allocPrint(arena, "v{d}", .{intermediate_value_count});
const deriv_start = @import("print_value.zig").printPtrDerivation(
derivation,

View File

@ -2857,7 +2857,7 @@ pub fn loadZirCacheBody(gpa: Allocator, header: Zir.Header, cache_file: std.fs.F
var cache_fr = cache_file.reader();
var cache_br = cache_fr.interface().unbuffered();
cache_br.readVecAll(&vecs) catch |err| switch (err) {
error.ReadFailed => if (cache_fr.err) |_| unreachable else |e| return e,
error.ReadFailed => return cache_fr.err.?,
error.EndOfStream => return error.UnexpectedFileSize,
};
if (data_has_safety_tag) {
@ -2912,7 +2912,7 @@ pub fn saveZirCache(gpa: Allocator, cache_file: std.fs.File, stat: std.fs.File.S
var cache_fw = cache_file.writer();
var cache_bw = cache_fw.interface().unbuffered();
cache_bw.writeVecAll(&vecs) catch |err| switch (err) {
error.WriteFailed => if (cache_fw.err) |_| unreachable else |e| return e,
error.WriteFailed => return cache_fw.err.?,
};
}
@ -2943,7 +2943,7 @@ pub fn saveZoirCache(cache_file: std.fs.File, stat: std.fs.File.Stat, zoir: Zoir
var cache_fw = cache_file.writer();
var cache_bw = cache_fw.interface().unbuffered();
cache_bw.writeVecAll(&vecs) catch |err| switch (err) {
error.WriteFailed => if (cache_fw.err) |_| unreachable else |e| return e,
error.WriteFailed => return cache_fw.err.?,
};
}
@ -2986,7 +2986,7 @@ pub fn loadZoirCacheBody(gpa: Allocator, header: Zoir.Header, cache_file: std.fs
var cache_fr = cache_file.reader();
var cache_br = cache_fr.interface().unbuffered();
cache_br.readVecAll(&vecs) catch |err| switch (err) {
error.ReadFailed => if (cache_fr.err) |_| unreachable else |e| return e,
error.ReadFailed => return cache_fr.err.?,
error.EndOfStream => return error.UnexpectedFileSize,
};
return zoir;

View File

@ -254,7 +254,7 @@ pub fn updateFile(
var source_fr = source_file.reader();
var source_br = source_fr.interface().unbuffered();
source_br.readSlice(source) catch |err| switch (err) {
error.ReadFailed => if (source_fr.err) |_| unreachable else |e| return e,
error.ReadFailed => return source_fr.err.?,
error.EndOfStream => return error.UnexpectedEndOfFile,
};
@ -2443,7 +2443,7 @@ fn updateEmbedFileInner(
var fr = file.reader();
var br = fr.interface().unbuffered();
br.readSlice(bytes[0..size]) catch |err| switch (err) {
error.ReadFailed => if (fr.err) |_| unreachable else |e| return e,
error.ReadFailed => return fr.err.?,
error.EndOfStream => return error.UnexpectedEof,
};
bytes[size] = 0;

File diff suppressed because it is too large Load Diff

View File

@ -209,7 +209,7 @@ pub fn getStandardDefineAbbrev(ctype: CType) ?[]const u8 {
};
}
pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *const Pool) @TypeOf(writer).Error!void {
pub fn renderLiteralPrefix(ctype: CType, bw: *std.io.BufferedWriter, kind: Kind, pool: *const Pool) std.io.Writer.Error!void {
switch (ctype.info(pool)) {
.basic => |basic_info| switch (basic_info) {
.void => unreachable,
@ -224,7 +224,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.uintptr_t,
.intptr_t,
=> switch (kind) {
else => try writer.print("({s})", .{@tagName(basic_info)}),
else => try bw.print("({s})", .{@tagName(basic_info)}),
.global => {},
},
.int,
@ -246,7 +246,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.int32_t,
.uint64_t,
.int64_t,
=> try writer.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}),
=> try bw.print("{s}_C(", .{ctype.getStandardDefineAbbrev().?}),
.zig_u128,
.zig_i128,
.zig_f16,
@ -255,7 +255,7 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.zig_f80,
.zig_f128,
.zig_c_longdouble,
=> try writer.print("zig_{s}_{s}(", .{
=> try bw.print("zig_{s}_{s}(", .{
switch (kind) {
else => "make",
.global => "init",
@ -265,12 +265,12 @@ pub fn renderLiteralPrefix(ctype: CType, writer: anytype, kind: Kind, pool: *con
.va_list => unreachable,
_ => unreachable,
},
.array, .vector => try writer.writeByte('{'),
.array, .vector => try bw.writeByte('{'),
else => unreachable,
}
}
pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @TypeOf(writer).Error!void {
pub fn renderLiteralSuffix(ctype: CType, bw: *std.io.BufferedWriter, pool: *const Pool) std.io.Writer.Error!void {
switch (ctype.info(pool)) {
.basic => |basic_info| switch (basic_info) {
.void => unreachable,
@ -280,20 +280,20 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty
.short,
.int,
=> {},
.long => try writer.writeByte('l'),
.@"long long" => try writer.writeAll("ll"),
.long => try bw.writeByte('l'),
.@"long long" => try bw.writeAll("ll"),
.@"unsigned char",
.@"unsigned short",
.@"unsigned int",
=> try writer.writeByte('u'),
=> try bw.writeByte('u'),
.@"unsigned long",
.size_t,
.uintptr_t,
=> try writer.writeAll("ul"),
.@"unsigned long long" => try writer.writeAll("ull"),
.float => try writer.writeByte('f'),
=> try bw.writeAll("ul"),
.@"unsigned long long" => try bw.writeAll("ull"),
.float => try bw.writeByte('f'),
.double => {},
.@"long double" => try writer.writeByte('l'),
.@"long double" => try bw.writeByte('l'),
.bool,
.ptrdiff_t,
.intptr_t,
@ -314,11 +314,11 @@ pub fn renderLiteralSuffix(ctype: CType, writer: anytype, pool: *const Pool) @Ty
.zig_f80,
.zig_f128,
.zig_c_longdouble,
=> try writer.writeByte(')'),
=> try bw.writeByte(')'),
.va_list => unreachable,
_ => unreachable,
},
.array, .vector => try writer.writeByte('}'),
.array, .vector => try bw.writeByte('}'),
else => unreachable,
}
}
@ -940,15 +940,14 @@ pub const Pool = struct {
const FormatData = struct { string: String, pool: *const Pool };
fn format(
data: FormatData,
bw: *std.io.BufferedWriter,
comptime fmt_str: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
) @TypeOf(writer).Error!void {
) std.io.Writer.Error!void {
if (fmt_str.len > 0) @compileError("invalid format string '" ++ fmt_str ++ "'");
if (data.string.toSlice(data.pool)) |slice|
try writer.writeAll(slice)
try bw.writeAll(slice)
else
try writer.print("f{d}", .{@intFromEnum(data.string.index)});
try bw.print("f{d}", .{@intFromEnum(data.string.index)});
}
pub fn fmt(str: String, pool: *const Pool) std.fmt.Formatter(format) {
return .{ .data = .{ .string = str, .pool = pool } };
@ -2890,7 +2889,7 @@ pub const Pool = struct {
comptime fmt_str: []const u8,
fmt_args: anytype,
) !String {
try pool.string_bytes.writer(allocator).print(fmt_str, fmt_args);
try pool.string_bytes.print(allocator, fmt_str, fmt_args);
return pool.trailingString(allocator);
}

View File

@ -1262,6 +1262,7 @@ const NavGen = struct {
fn resolveTypeName(self: *NavGen, ty: Type) Allocator.Error![]const u8 {
var aw: std.io.AllocatingWriter = undefined;
aw.init(self.gpa);
defer aw.deinit();
ty.print(&aw.buffered_writer, self.pt) catch return error.OutOfMemory;
return aw.toOwnedSlice();
}

View File

@ -1038,7 +1038,7 @@ pub const File = struct {
var fr = file.reader();
var br = fr.interface().unbuffered();
br.readSlice(buf) catch |err| switch (err) {
error.ReadFailed => if (fr.err) |_| unreachable else |e| return e,
error.ReadFailed => return fr.err.?,
error.EndOfStream => return error.UnexpectedEndOfFile,
};
var ld_script = try LdScript.parse(gpa, diags, path, buf);
@ -2107,7 +2107,7 @@ fn resolvePathInputLib(
br.readSlice(ld_script_bytes.items) catch |err| switch (err) {
error.ReadFailed => fatal("failed to read '{f'}': {s}", .{
test_path,
@errorName(if (fr.err) |_| unreachable else |e| e),
@errorName(fr.err.?),
}),
error.EndOfStream => break :ok,
};
@ -2124,7 +2124,7 @@ fn resolvePathInputLib(
fatal("{f}: linker script too big", .{test_path});
try ld_script_bytes.resize(gpa, size);
br.readSlice(ld_script_bytes.items[@intCast(fr.pos)..]) catch |err| switch (err) {
error.ReadFailed => if (fr.err) |_| unreachable else |e| fatal("failed to read {f}: {s}", .{ test_path, @errorName(e) }),
error.ReadFailed => fatal("failed to read {f}: {s}", .{ test_path, @errorName(fr.err.?) }),
error.EndOfStream => fatal("failed to read {f}: unexpected end of file", .{test_path}),
};
var diags: Diags = .init(gpa);