╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ fmt.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
306 file_closed = true; 307 308 // Add to set after no longer possible to get error.IsDir. 309 if (try fmt.seen.fetchPut(stat.inode, {})) |_| return; 310 311 const mode: std.zig.Ast.Mode = mode: { 312 if (fmt.force_zon) break :mode .zon; 313 if (mem.endsWith(u8, sub_path, ".zon")) break :mode .zon; 314 break :mode .zig; 315 }; 316 317 var tree = try std.zig.Ast.parse(gpa, source_code, .{ .mode = mode }); 318 defer tree.deinit(gpa); 319 320 if (tree.errors.len != 0) { 321 try std.zig.printAstErrorsToStderr(gpa, io, tree, file_path, fmt.color); 322 fmt.any_error = true; 323 return; 324 } 325 326 if (fmt.check_ast) { 327 if (stat.size > std.zig.max_src_size) 328 return error.FileTooBig; 329 330 switch (mode) { 331 .zig => { 332 var zir = try std.zig.AstGen.generate(gpa, tree); 333 defer zir.deinit(gpa); 334 335 if (zir.hasCompileErrors()) { 336 var wip_errors: std.zig.ErrorBundle.Wip = undefined; 337 try wip_errors.init(gpa); 338 defer wip_errors.deinit(); 339 try wip_errors.addZirErrorMessages(zir, tree, source_code, file_path); 340 var error_bundle = try wip_errors.toOwnedBundle(""); 341 defer error_bundle.deinit(gpa); 342 try error_bundle.renderToStderr(io, .{}, fmt.color); 343 fmt.any_error = true; 344 } 345 }, 346 .zon => { 347 var zoir = try std.zig.ZonGen.generate(gpa, tree, .{}); 348 defer zoir.deinit(gpa); 349 350 if (zoir.hasCompileErrors()) { 351 var wip_errors: std.zig.ErrorBundle.Wip = undefined; 352 try wip_errors.init(gpa); 353 defer wip_errors.deinit(); 354 try wip_errors.addZoirErrorMessages(zoir, tree, source_code, file_path); 355 var error_bundle = try wip_errors.toOwnedBundle(""); 356 defer error_bundle.deinit(gpa); 357 try error_bundle.renderToStderr(io, .{}, fmt.color); 358 fmt.any_error = true; 359 } 360 }, 361 } 362 } 363 364 if (fmt.complexity) { 365 std.log.info("{s}: tokens={d} nodes={d}", .{ file_path, tree.tokens.len, tree.nodes.len }); 366 fmt.total_tokens += tree.tokens.len; 367 fmt.total_nodes += tree.nodes.len; 368 } 369 370 // As a heuristic, we make enough capacity for the same as the input source. 371 fmt.out_buffer.clearRetainingCapacity(); 372 try fmt.out_buffer.ensureTotalCapacity(source_code.len); 373 374 tree.render(gpa, &fmt.out_buffer.writer, .{}) catch |err| switch (err) { 375 error.WriteFailed, error.OutOfMemory => return error.OutOfMemory, 376 }; 377 if (mem.eql(u8, fmt.out_buffer.written(), source_code)) 378 return; 379 380 if (check_mode) { 381 try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); 382 fmt.any_error = true; 383 } else { 384 var af = try dir.createFileAtomic(io, sub_path, .{ .permissions = stat.permissions, .replace = true }); 385 defer af.deinit(io); 386 387 try af.file.writeStreamingAll(io, fmt.out_buffer.written()); 388 try af.replace(io); 389 try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); 390 } 391 } 392 393 /// Provided for debugging/testing purposes; unused by the compiler. 394 pub fn main(init: process.Init) !void { 395 const args = try init.minimal.args.toSlice(init.arena.allocator()); 396 return run(init.gpa, init.arena.allocator(), init.io, args[1..]); 397 } 398