From c1ec772654fc15afe408bab790360d65bf4272db Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Wed, 27 Mar 2024 23:46:47 -0600 Subject: [PATCH] zig fmt --- bootstrap/Compilation.zig | 57 ++++++++++++++++--------------------- bootstrap/library.zig | 4 +-- bootstrap/linker/linker.zig | 8 +++--- bootstrap/linker/lld.zig | 9 +++--- bootstrap/main.zig | 4 +-- build/test_runner.zig | 12 ++++---- 6 files changed, 41 insertions(+), 53 deletions(-) diff --git a/bootstrap/Compilation.zig b/bootstrap/Compilation.zig index 8cc6cf2..65eba9f 100644 --- a/bootstrap/Compilation.zig +++ b/bootstrap/Compilation.zig @@ -176,7 +176,7 @@ const CSourceKind = enum { cpp, }; -fn compileMusl(context: *const Context) MuslContext{ +fn compileMusl(context: *const Context) MuslContext { const musl = try MuslContext.init(context); var exists = true; var dir = std.fs.cwd().openDir(musl.global_cache_dir, .{}) catch b: { @@ -272,13 +272,13 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 _ = kind; // autofix var argument_index: usize = 0; _ = &argument_index; - const Mode = enum{ + const Mode = enum { object, link, }; var out_path: ?[]const u8 = null; var out_mode: ?Mode = null; - const Extension = enum{ + const Extension = enum { c, cpp, assembly, @@ -286,15 +286,15 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 static_library, shared_library, }; - const CSourceFile = struct{ + const CSourceFile = struct { path: []const u8, extension: Extension, }; - const DebugInfo = enum{ + const DebugInfo = enum { yes, no, }; - const LinkArch = enum{ + const LinkArch = enum { arm64, }; var debug_info: ?DebugInfo = null; @@ -314,21 +314,14 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 if (data_structures.last_byte(argument, '.')) |dot_index| { const extension_string = argument[dot_index..]; const extension: Extension = - if (byte_equal(extension_string, ".c")) .c - else if (byte_equal(extension_string, ".cpp") or byte_equal(extension_string, ".cxx") or byte_equal(extension_string, ".cc")) .cpp - else if (byte_equal(extension_string, ".S")) .assembly - else if (byte_equal(extension_string, ".o")) .object - else if (byte_equal(extension_string, ".a")) .static_library - else if (byte_equal(extension_string, ".so") or - byte_equal(extension_string, ".dll") or - byte_equal(extension_string, ".dylib") or - byte_equal(extension_string, ".tbd") - ) .shared_library - else { - try write(.panic, argument); - try write(.panic, "\n"); - @panic("Unable to recognize extension for the file above"); - }; + if (byte_equal(extension_string, ".c")) .c else if (byte_equal(extension_string, ".cpp") or byte_equal(extension_string, ".cxx") or byte_equal(extension_string, ".cc")) .cpp else if (byte_equal(extension_string, ".S")) .assembly else if (byte_equal(extension_string, ".o")) .object else if (byte_equal(extension_string, ".a")) .static_library else if (byte_equal(extension_string, ".so") or + byte_equal(extension_string, ".dll") or + byte_equal(extension_string, ".dylib") or + byte_equal(extension_string, ".tbd")) .shared_library else { + try write(.panic, argument); + try write(.panic, "\n"); + @panic("Unable to recognize extension for the file above"); + }; switch (extension) { .c, .cpp, .assembly => { try c_source_files.append(context.my_allocator, .{ @@ -464,7 +457,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 const wl_arg = argument["-Wl,".len..]; if (data_structures.first_byte(wl_arg, ',')) |comma_index| { const key = wl_arg[0..comma_index]; - const value = wl_arg[comma_index + 1..]; + const value = wl_arg[comma_index + 1 ..]; try ld_argv.append(context.my_allocator, key); try ld_argv.append(context.my_allocator, value); } else { @@ -486,7 +479,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 try list.append_slice(context.my_allocator, argument); try list.append(context.my_allocator, '\n'); - try std.fs.cwd().writeFile(try std.fmt.allocPrint(context.allocator, "{s}/dev/nativity/nat/unhandled_arg_{}", .{home_dir, std.time.milliTimestamp()}), list.slice()); + try std.fs.cwd().writeFile(try std.fmt.allocPrint(context.allocator, "{s}/dev/nativity/nat/unhandled_arg_{}", .{ home_dir, std.time.milliTimestamp() }), list.slice()); } try write(.panic, "unhandled argument: '"); try write(.panic, argument); @@ -533,15 +526,13 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 try argv.append(context.my_allocator, "-fno-builtin"); } - if (link_libcpp) { // include paths } const link_libc = c_source_file.extension == .c; - if (link_libc) { - } + if (link_libc) {} const link_libunwind = false; if (link_libunwind) { @@ -592,11 +583,11 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 break :blk target_triple_buffer.slice(); }; - try argv.append_slice(context.my_allocator, &.{"-target", target_triple}); + try argv.append_slice(context.my_allocator, &.{ "-target", target_triple }); const object_path = switch (mode) { .object => out_path.?, - .link => try std.mem.concat(context.allocator, u8, &.{if (out_path) |op| op else "a.o", ".o"}), + .link => try std.mem.concat(context.allocator, u8, &.{ if (out_path) |op| op else "a.o", ".o" }), }; try link_objects.append(context.my_allocator, .{ @@ -623,12 +614,12 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 else => &.{}, }; for (libc_framework_dirs) |framework_dir| { - try argv.append_slice(context.my_allocator, &.{"-iframework", framework_dir}); + try argv.append_slice(context.my_allocator, &.{ "-iframework", framework_dir }); } const framework_dirs = &[_][]const u8{}; for (framework_dirs) |framework_dir| { - try argv.append_slice(context.my_allocator, &.{"-F", framework_dir}); + try argv.append_slice(context.my_allocator, &.{ "-F", framework_dir }); } // TODO: c headers dir @@ -653,7 +644,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 }; for (libc_include_dirs) |include_dir| { - try argv.append_slice(context.my_allocator, &.{"-isystem", include_dir}); + try argv.append_slice(context.my_allocator, &.{ "-isystem", include_dir }); } // TODO: cpu model @@ -727,7 +718,7 @@ pub fn compileCSourceFile(context: *const Context, arguments: []const []const u8 // TODO: extra flags // TODO: cache exempt flags - try argv.append_slice(context.my_allocator, &.{"-c", "-o", object_path}); + try argv.append_slice(context.my_allocator, &.{ "-c", "-o", object_path }); // TODO: emit ASM/LLVM IR const debug_clang_args = false; @@ -15669,7 +15660,7 @@ pub const Unit = struct { o_file, }); - var arguments = [_][]const u8{ "-c", c_source_file, "-o", object_path, "-g", "-fno-stack-protector"}; + var arguments = [_][]const u8{ "-c", c_source_file, "-o", object_path, "-g", "-fno-stack-protector" }; try compileCSourceFile(context, &arguments, .c); object_files.append_with_capacity(.{ .path = object_path, diff --git a/bootstrap/library.zig b/bootstrap/library.zig index 2341fc3..01e6a8e 100644 --- a/bootstrap/library.zig +++ b/bootstrap/library.zig @@ -736,7 +736,7 @@ pub fn starts_with_slice(bytes: []const u8, slice: []const u8) bool { pub fn ends_with_slice(bytes: []const u8, slice: []const u8) bool { if (slice.len <= bytes.len) { - if (byte_equal(bytes[bytes.len - slice.len..], slice)) { + if (byte_equal(bytes[bytes.len - slice.len ..], slice)) { return true; } } @@ -749,7 +749,7 @@ pub fn first_byte(bytes: []const u8, byte: u8) ?usize { if (b == byte) { return i; } - } + } return null; } diff --git a/bootstrap/linker/linker.zig b/bootstrap/linker/linker.zig index fd78ed1..2705cf7 100644 --- a/bootstrap/linker/linker.zig +++ b/bootstrap/linker/linker.zig @@ -1,7 +1,7 @@ const Compilation = @import("../Compilation.zig"); const Context = Compilation.Context; const lld = @import("lld.zig"); -pub const Options = struct{ +pub const Options = struct { backend: Backend = .lld, output_file_path: []const u8, objects: []const Object, @@ -11,15 +11,15 @@ pub const Options = struct{ link_libcpp: bool, }; -const Backend = enum{ +const Backend = enum { lld, }; -pub const Object = struct{ +pub const Object = struct { path: []const u8, }; -pub const Library = struct{ +pub const Library = struct { path: []const u8, }; diff --git a/bootstrap/linker/lld.zig b/bootstrap/linker/lld.zig index 188b476..f5a536c 100644 --- a/bootstrap/linker/lld.zig +++ b/bootstrap/linker/lld.zig @@ -59,7 +59,7 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void .linux => { if (options.link_libcpp) { assert(options.link_libc); - try argv.append(context.my_allocator, "/usr/lib/libstdc++.so" ); + try argv.append(context.my_allocator, "/usr/lib/libstdc++.so"); } if (options.link_libc) { @@ -80,9 +80,8 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void else => @compileError("OS not supported"), } - for (options.libraries) |lib| { - try argv.append(context.my_allocator, try std.mem.concat(context.allocator, u8, &.{"-l", lib.path})); + try argv.append(context.my_allocator, try std.mem.concat(context.allocator, u8, &.{ "-l", lib.path })); } const argv_zero_terminated = try Compilation.argsCopyZ(context.allocator, argv.slice()); @@ -92,8 +91,8 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void var stderr_ptr: [*]const u8 = undefined; var stderr_len: usize = 0; const result = switch (@import("builtin").os.tag) { - .linux => NativityLLDLinkELF (argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len), - .macos => NativityLLDLinkMachO (argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len), + .linux => NativityLLDLinkELF(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len), + .macos => NativityLLDLinkMachO(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len), .windows => NativityLLDLinkCOFF(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len), else => @compileError("OS not supported"), }; diff --git a/bootstrap/main.zig b/bootstrap/main.zig index 9a7e189..2221750 100644 --- a/bootstrap/main.zig +++ b/bootstrap/main.zig @@ -66,8 +66,8 @@ pub fn main() !void { argument_list.appendSlice(arg) catch {}; argument_list.append(' ') catch {}; } - argument_list.append('\n') catch {}; - std.fs.cwd().writeFile(std.fmt.allocPrint(std.heap.page_allocator, "{s}/dev/nativity/nat/invocation_log_{}", .{home_dir, timestamp}) catch unreachable, argument_list.items) catch {}; + argument_list.append('\n') catch {}; + std.fs.cwd().writeFile(std.fmt.allocPrint(std.heap.page_allocator, "{s}/dev/nativity/nat/invocation_log_{}", .{ home_dir, timestamp }) catch unreachable, argument_list.items) catch {}; } if (arguments.len <= 1) { diff --git a/build/test_runner.zig b/build/test_runner.zig index ad27617..698e05d 100644 --- a/build/test_runner.zig +++ b/build/test_runner.zig @@ -136,7 +136,7 @@ fn runStandaloneTests(allocator: Allocator) !void { .allocator = allocator, // TODO: delete -main_source_file? .argv = &.{ "zig-out/bin/nat", "exe", "-main_source_file", source_file_path }, - .max_output_bytes = std.math.maxInt(u64), + .max_output_bytes = std.math.maxInt(u64), }); ran_compilation_count += 1; @@ -225,7 +225,7 @@ fn runBuildTests(allocator: Allocator) !void { .allocator = allocator, // TODO: delete -main_source_file? .argv = &.{ compiler_realpath, "build" }, - .max_output_bytes = std.math.maxInt(u64), + .max_output_bytes = std.math.maxInt(u64), }); ran_compilation_count += 1; @@ -413,9 +413,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void { const ninja = try std.ChildProcess.run(.{ .allocator = allocator, // TODO: delete -main_source_file? - .argv = &.{ - "ninja" - }, + .argv = &.{"ninja"}, .max_output_bytes = std.math.maxInt(u64), }); const ninja_result: TestError!bool = switch (ninja.term) { @@ -446,7 +444,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void { .allocator = allocator, // TODO: delete -main_source_file? .argv = &.{ - try std.mem.concat(allocator, u8, &.{ "./", cc_entry.name}), + try std.mem.concat(allocator, u8, &.{ "./", cc_entry.name }), }, .max_output_bytes = std.math.maxInt(u64), }); @@ -475,7 +473,7 @@ fn runCmakeTests(allocator: Allocator, dir_path: []const u8) !void { std.debug.print("[TEST {s}]\n", .{if (success) "\x1b[32mOK\x1b[0m" else "\x1b[31mFAILED\x1b[0m"}); }, - else => std.debug.panic("Entry {s} is a {s}", .{cc_entry.name, @tagName(cc_entry.kind)}), + else => std.debug.panic("Entry {s} is a {s}", .{ cc_entry.name, @tagName(cc_entry.kind) }), } try std.posix.chdir(cc_dir_path);