Skip to content

Commit

Permalink
run zig fmt on some of the codebase
Browse files Browse the repository at this point in the history
See #1003
  • Loading branch information
andrewrk committed May 27, 2018
1 parent 118d41e commit b184ae5
Show file tree
Hide file tree
Showing 28 changed files with 913 additions and 845 deletions.
182 changes: 117 additions & 65 deletions doc/docgen.zig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/guess_number/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn main() !void {

while (true) {
try stdout.print("\nGuess a number between 1 and 100: ");
var line_buf : [20]u8 = undefined;
var line_buf: [20]u8 = undefined;

const line_len = io.readLine(line_buf[0..]) catch |err| switch (err) {
error.InputTooLong => {
Expand Down
3 changes: 1 addition & 2 deletions example/hello_world/hello_libc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const c = @cImport({
const msg = c"Hello, world!\n";

export fn main(argc: c_int, argv: &&u8) c_int {
if (c.printf(msg) != c_int(c.strlen(msg)))
return -1;
if (c.printf(msg) != c_int(c.strlen(msg))) return -1;

return 0;
}
4 changes: 1 addition & 3 deletions example/mix_o_files/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ pub fn build(b: &Builder) void {
const obj = b.addObject("base64", "base64.zig");

const exe = b.addCExecutable("test");
exe.addCompileFlags([][]const u8 {
"-std=c99",
});
exe.addCompileFlags([][]const u8{"-std=c99"});
exe.addSourceFile("test.c");
exe.addObject(obj);

Expand Down
4 changes: 1 addition & 3 deletions example/shared_library/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ pub fn build(b: &Builder) void {
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));

const exe = b.addCExecutable("test");
exe.addCompileFlags([][]const u8 {
"-std=c99",
});
exe.addCompileFlags([][]const u8{"-std=c99"});
exe.addSourceFile("test.c");
exe.linkLibrary(lib);

Expand Down
4 changes: 1 addition & 3 deletions src-self-hosted/introspect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ pub fn resolveZigLibDir(allocator: &mem.Allocator) ![]u8 {
\\Unable to find zig lib directory: {}.
\\Reinstall Zig or use --zig-install-prefix.
\\
,
@errorName(err)
);
, @errorName(err));

return error.ZigLibDirNotFound;
};
Expand Down
1 change: 0 additions & 1 deletion src-self-hosted/ir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,4 @@ pub const Instruction = struct {
ArgType,
Export,
};

};
169 changes: 102 additions & 67 deletions src-self-hosted/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const usage =
\\ zen Print zen of zig and exit
\\
\\
;
;

const Command = struct {
name: []const u8,
Expand All @@ -63,22 +63,61 @@ pub fn main() !void {
os.exit(1);
}

const commands = []Command {
Command { .name = "build", .exec = cmdBuild },
Command { .name = "build-exe", .exec = cmdBuildExe },
Command { .name = "build-lib", .exec = cmdBuildLib },
Command { .name = "build-obj", .exec = cmdBuildObj },
Command { .name = "fmt", .exec = cmdFmt },
Command { .name = "run", .exec = cmdRun },
Command { .name = "targets", .exec = cmdTargets },
Command { .name = "test", .exec = cmdTest },
Command { .name = "translate-c", .exec = cmdTranslateC },
Command { .name = "version", .exec = cmdVersion },
Command { .name = "zen", .exec = cmdZen },
const commands = []Command{
Command{
.name = "build",
.exec = cmdBuild,
},
Command{
.name = "build-exe",
.exec = cmdBuildExe,
},
Command{
.name = "build-lib",
.exec = cmdBuildLib,
},
Command{
.name = "build-obj",
.exec = cmdBuildObj,
},
Command{
.name = "fmt",
.exec = cmdFmt,
},
Command{
.name = "run",
.exec = cmdRun,
},
Command{
.name = "targets",
.exec = cmdTargets,
},
Command{
.name = "test",
.exec = cmdTest,
},
Command{
.name = "translate-c",
.exec = cmdTranslateC,
},
Command{
.name = "version",
.exec = cmdVersion,
},
Command{
.name = "zen",
.exec = cmdZen,
},

// undocumented commands
Command { .name = "help", .exec = cmdHelp },
Command { .name = "internal", .exec = cmdInternal },
Command{
.name = "help",
.exec = cmdHelp,
},
Command{
.name = "internal",
.exec = cmdInternal,
},
};

for (commands) |command| {
Expand Down Expand Up @@ -120,9 +159,9 @@ const usage_build =
\\ --verbose-cimport Enable compiler debug output for C imports
\\
\\
;
;

const args_build_spec = []Flag {
const args_build_spec = []Flag{
Flag.Bool("--help"),
Flag.Bool("--init"),
Flag.Arg1("--build-file"),
Expand All @@ -148,7 +187,7 @@ const missing_build_file =
\\
\\See: `zig build --help` or `zig help` for more options.
\\
;
;

fn cmdBuild(allocator: &Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_build_spec, args);
Expand Down Expand Up @@ -317,15 +356,23 @@ const usage_build_generic =
\\ --ver-patch [ver] Dynamic library semver patch version
\\
\\
;
;

const args_build_generic = []Flag {
const args_build_generic = []Flag{
Flag.Bool("--help"),
Flag.Option("--color", []const []const u8 { "auto", "off", "on" }),
Flag.Option("--color", []const []const u8{
"auto",
"off",
"on",
}),

Flag.ArgMergeN("--assembly", 1),
Flag.Arg1("--cache-dir"),
Flag.Option("--emit", []const []const u8 { "asm", "bin", "llvm-ir" }),
Flag.Option("--emit", []const []const u8{
"asm",
"bin",
"llvm-ir",
}),
Flag.Bool("--enable-timing-info"),
Flag.Arg1("--libc-include-dir"),
Flag.Arg1("--name"),
Expand Down Expand Up @@ -471,7 +518,7 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo
os.exit(1);
};

const asm_a= flags.many("assembly");
const asm_a = flags.many("assembly");
const obj_a = flags.many("object");
if (in_file == null and (obj_a == null or (??obj_a).len == 0) and (asm_a == null or (??asm_a).len == 0)) {
try stderr.write("Expected source file argument or at least one --object or --assembly argument\n");
Expand All @@ -493,17 +540,16 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo
const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1);
defer allocator.free(zig_lib_dir);

var module =
try Module.create(
allocator,
root_name,
zig_root_source_file,
Target.Native,
out_type,
build_mode,
zig_lib_dir,
full_cache_dir
);
var module = try Module.create(
allocator,
root_name,
zig_root_source_file,
Target.Native,
out_type,
build_mode,
zig_lib_dir,
full_cache_dir,
);
defer module.destroy();

module.version_major = try std.fmt.parseUnsigned(u32, flags.single("ver-major") ?? "0", 10);
Expand Down Expand Up @@ -588,10 +634,10 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo
}

if (flags.single("mmacosx-version-min")) |ver| {
module.darwin_version_min = Module.DarwinVersionMin { .MacOS = ver };
module.darwin_version_min = Module.DarwinVersionMin{ .MacOS = ver };
}
if (flags.single("mios-version-min")) |ver| {
module.darwin_version_min = Module.DarwinVersionMin { .Ios = ver };
module.darwin_version_min = Module.DarwinVersionMin{ .Ios = ver };
}

module.emit_file_type = emit_type;
Expand Down Expand Up @@ -639,11 +685,9 @@ const usage_fmt =
\\ --help Print this help and exit
\\
\\
;
;

const args_fmt_spec = []Flag {
Flag.Bool("--help"),
};
const args_fmt_spec = []Flag{Flag.Bool("--help")};

fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_fmt_spec, args);
Expand Down Expand Up @@ -675,7 +719,6 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
};
defer tree.deinit();


var error_it = tree.errors.iterator(0);
while (error_it.next()) |parse_error| {
const token = tree.tokens.at(parse_error.loc());
Expand Down Expand Up @@ -721,8 +764,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
inline while (i < @memberCount(builtin.Arch)) : (i += 1) {
comptime const arch_tag = @memberName(builtin.Arch, i);
// NOTE: Cannot use empty string, see #918.
comptime const native_str =
if (comptime mem.eql(u8, arch_tag, @tagName(builtin.arch))) " (native)\n" else "\n";
comptime const native_str = if (comptime mem.eql(u8, arch_tag, @tagName(builtin.arch))) " (native)\n" else "\n";

try stdout.print(" {}{}", arch_tag, native_str);
}
Expand All @@ -735,8 +777,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
inline while (i < @memberCount(builtin.Os)) : (i += 1) {
comptime const os_tag = @memberName(builtin.Os, i);
// NOTE: Cannot use empty string, see #918.
comptime const native_str =
if (comptime mem.eql(u8, os_tag, @tagName(builtin.os))) " (native)\n" else "\n";
comptime const native_str = if (comptime mem.eql(u8, os_tag, @tagName(builtin.os))) " (native)\n" else "\n";

try stdout.print(" {}{}", os_tag, native_str);
}
Expand All @@ -749,8 +790,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
inline while (i < @memberCount(builtin.Environ)) : (i += 1) {
comptime const environ_tag = @memberName(builtin.Environ, i);
// NOTE: Cannot use empty string, see #918.
comptime const native_str =
if (comptime mem.eql(u8, environ_tag, @tagName(builtin.environ))) " (native)\n" else "\n";
comptime const native_str = if (comptime mem.eql(u8, environ_tag, @tagName(builtin.environ))) " (native)\n" else "\n";

try stdout.print(" {}{}", environ_tag, native_str);
}
Expand All @@ -772,12 +812,9 @@ const usage_test =
\\ --help Print this help and exit
\\
\\
;

const args_test_spec = []Flag {
Flag.Bool("--help"),
};
;

const args_test_spec = []Flag{Flag.Bool("--help")};

fn cmdTest(allocator: &Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_build_spec, args);
Expand Down Expand Up @@ -810,21 +847,18 @@ const usage_run =
\\ --help Print this help and exit
\\
\\
;

const args_run_spec = []Flag {
Flag.Bool("--help"),
};
;

const args_run_spec = []Flag{Flag.Bool("--help")};

fn cmdRun(allocator: &Allocator, args: []const []const u8) !void {
var compile_args = args;
var runtime_args: []const []const u8 = []const []const u8 {};
var runtime_args: []const []const u8 = []const []const u8{};

for (args) |argv, i| {
if (mem.eql(u8, argv, "--")) {
compile_args = args[0..i];
runtime_args = args[i+1..];
runtime_args = args[i + 1..];
break;
}
}
Expand Down Expand Up @@ -858,9 +892,9 @@ const usage_translate_c =
\\ --output [path] Output file to write generated zig file (default: stdout)
\\
\\
;
;

const args_translate_c_spec = []Flag {
const args_translate_c_spec = []Flag{
Flag.Bool("--help"),
Flag.Bool("--enable-timing-info"),
Flag.Arg1("--libc-include-dir"),
Expand Down Expand Up @@ -934,7 +968,7 @@ const info_zen =
\\ * Together we serve end users.
\\
\\
;
;

fn cmdZen(allocator: &Allocator, args: []const []const u8) !void {
try stdout.write(info_zen);
Expand All @@ -949,17 +983,18 @@ const usage_internal =
\\ build-info Print static compiler build-info
\\
\\
;
;

fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void {
if (args.len == 0) {
try stderr.write(usage_internal);
os.exit(1);
}

const sub_commands = []Command {
Command { .name = "build-info", .exec = cmdInternalBuildInfo },
};
const sub_commands = []Command{Command{
.name = "build-info",
.exec = cmdInternalBuildInfo,
}};

for (sub_commands) |sub_command| {
if (mem.eql(u8, sub_command.name, args[0])) {
Expand All @@ -983,7 +1018,7 @@ fn cmdInternalBuildInfo(allocator: &Allocator, args: []const []const u8) !void {
\\ZIG_C_HEADER_FILES {}
\\ZIG_DIA_GUIDS_LIB {}
\\
,
,
std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR),
std.cstr.toSliceConst(c.ZIG_CXX_COMPILER),
std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE),
Expand Down
3 changes: 1 addition & 2 deletions src-self-hosted/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ pub const Target = union(enum) {

pub fn isDarwin(self: &const Target) bool {
return switch (self.getOs()) {
builtin.Os.ios,
builtin.Os.macosx => true,
builtin.Os.ios, builtin.Os.macosx => true,
else => false,
};
}
Expand Down
Loading

0 comments on commit b184ae5

Please sign in to comment.