aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-07-02 21:11:45 -0400
committerGitHub <noreply@github.com>2021-07-02 21:11:45 -0400
commitbb98620c10992884d58ae97a3a30dd9e26735fea (patch)
tree463cb67f3551c397aae17dff5594cd2fd2037ca0 /src/main.zig
parentd84b386f6034278c8a9e8c3d2b0975ac541584aa (diff)
parenta6bf68ccf985787eeac33a97e362d043987905c4 (diff)
downloadzig-bb98620c10992884d58ae97a3a30dd9e26735fea.tar.gz
zig-bb98620c10992884d58ae97a3a30dd9e26735fea.zip
Merge pull request #9219 from ziglang/unreachable-code
move "unreachable code" error from stage1 to stage2
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig50
1 files changed, 42 insertions, 8 deletions
diff --git a/src/main.zig b/src/main.zig
index 902d893169..37044d0b99 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -233,7 +233,7 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
} else if (mem.eql(u8, cmd, "build")) {
return cmdBuild(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "fmt")) {
- return cmdFmt(gpa, cmd_args);
+ return cmdFmt(gpa, arena, cmd_args);
} else if (mem.eql(u8, cmd, "libc")) {
return cmdLibC(gpa, cmd_args);
} else if (mem.eql(u8, cmd, "init-exe")) {
@@ -350,9 +350,11 @@ const usage_build_generic =
\\ -funwind-tables Always produce unwind table entries for all functions
\\ -fno-unwind-tables Never produce unwind table entries
\\ -fLLVM Force using LLVM as the codegen backend
- \\ -fno-LLVM Prevent using LLVM as a codegen backend
+ \\ -fno-LLVM Prevent using LLVM as the codegen backend
\\ -fClang Force using Clang as the C/C++ compilation backend
\\ -fno-Clang Prevent using Clang as the C/C++ compilation backend
+ \\ -fstage1 Force using bootstrap compiler as the codegen backend
+ \\ -fno-stage1 Prevent using bootstrap compiler as the codegen backend
\\ --strip Omit debug symbols
\\ --single-threaded Code assumes it is only used single-threaded
\\ -ofmt=[mode] Override target object format
@@ -602,6 +604,7 @@ fn buildOutputType(
var use_llvm: ?bool = null;
var use_lld: ?bool = null;
var use_clang: ?bool = null;
+ var use_stage1: ?bool = null;
var link_eh_frame_hdr = false;
var link_emit_relocs = false;
var each_lib_rpath: ?bool = null;
@@ -975,6 +978,10 @@ fn buildOutputType(
use_clang = true;
} else if (mem.eql(u8, arg, "-fno-Clang")) {
use_clang = false;
+ } else if (mem.eql(u8, arg, "-fstage1")) {
+ use_stage1 = true;
+ } else if (mem.eql(u8, arg, "-fno-stage1")) {
+ use_stage1 = false;
} else if (mem.eql(u8, arg, "-rdynamic")) {
rdynamic = true;
} else if (mem.eql(u8, arg, "-fsoname")) {
@@ -2020,6 +2027,7 @@ fn buildOutputType(
.use_llvm = use_llvm,
.use_lld = use_lld,
.use_clang = use_clang,
+ .use_stage1 = use_stage1,
.rdynamic = rdynamic,
.linker_script = linker_script,
.version_script = version_script,
@@ -3031,12 +3039,13 @@ const Fmt = struct {
check_ast: bool,
color: Color,
gpa: *Allocator,
+ arena: *Allocator,
out_buffer: std.ArrayList(u8),
const SeenMap = std.AutoHashMap(fs.File.INode, void);
};
-pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
+pub fn cmdFmt(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !void {
var color: Color = .auto;
var stdin_flag: bool = false;
var check_flag: bool = false;
@@ -3094,7 +3103,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
defer tree.deinit(gpa);
for (tree.errors) |parse_error| {
- try printErrMsgToStdErr(gpa, parse_error, tree, "<stdin>", color);
+ try printErrMsgToStdErr(gpa, arena, parse_error, tree, "<stdin>", color);
}
var has_ast_error = false;
if (check_ast_flag) {
@@ -3162,6 +3171,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
var fmt = Fmt{
.gpa = gpa,
+ .arena = arena,
.seen = Fmt.SeenMap.init(gpa),
.any_error = false,
.check_ast = check_ast_flag,
@@ -3285,7 +3295,7 @@ fn fmtPathFile(
defer tree.deinit(fmt.gpa);
for (tree.errors) |parse_error| {
- try printErrMsgToStdErr(fmt.gpa, parse_error, tree, file_path, fmt.color);
+ try printErrMsgToStdErr(fmt.gpa, fmt.arena, parse_error, tree, file_path, fmt.color);
}
if (tree.errors.len != 0) {
fmt.any_error = true;
@@ -3366,12 +3376,14 @@ fn fmtPathFile(
fn printErrMsgToStdErr(
gpa: *mem.Allocator,
+ arena: *mem.Allocator,
parse_error: ast.Error,
tree: ast.Tree,
path: []const u8,
color: Color,
) !void {
const lok_token = parse_error.token;
+ const token_tags = tree.tokens.items(.tag);
const start_loc = tree.tokenLocation(0, lok_token);
const source_line = tree.source[start_loc.line_start..start_loc.line_end];
@@ -3381,6 +3393,27 @@ fn printErrMsgToStdErr(
try tree.renderError(parse_error, writer);
const text = text_buf.items;
+ var notes_buffer: [1]Compilation.AllErrors.Message = undefined;
+ var notes_len: usize = 0;
+
+ if (token_tags[parse_error.token] == .invalid) {
+ const bad_off = @intCast(u32, tree.tokenSlice(parse_error.token).len);
+ const byte_offset = @intCast(u32, start_loc.line_start) + bad_off;
+ notes_buffer[notes_len] = .{
+ .src = .{
+ .src_path = path,
+ .msg = try std.fmt.allocPrint(arena, "invalid byte: '{'}'", .{
+ std.zig.fmtEscapes(tree.source[byte_offset..][0..1]),
+ }),
+ .byte_offset = byte_offset,
+ .line = @intCast(u32, start_loc.line),
+ .column = @intCast(u32, start_loc.column) + bad_off,
+ .source_line = source_line,
+ },
+ };
+ notes_len += 1;
+ }
+
const message: Compilation.AllErrors.Message = .{
.src = .{
.src_path = path,
@@ -3389,6 +3422,7 @@ fn printErrMsgToStdErr(
.line = @intCast(u32, start_loc.line),
.column = @intCast(u32, start_loc.column),
.source_line = source_line,
+ .notes = notes_buffer[0..notes_len],
},
};
@@ -3915,7 +3949,7 @@ pub fn cmdAstCheck(
defer file.tree.deinit(gpa);
for (file.tree.errors) |parse_error| {
- try printErrMsgToStdErr(gpa, parse_error, file.tree, file.sub_file_path, color);
+ try printErrMsgToStdErr(gpa, arena, parse_error, file.tree, file.sub_file_path, color);
}
if (file.tree.errors.len != 0) {
process.exit(1);
@@ -4041,7 +4075,7 @@ pub fn cmdChangelist(
defer file.tree.deinit(gpa);
for (file.tree.errors) |parse_error| {
- try printErrMsgToStdErr(gpa, parse_error, file.tree, old_source_file, .auto);
+ try printErrMsgToStdErr(gpa, arena, parse_error, file.tree, old_source_file, .auto);
}
if (file.tree.errors.len != 0) {
process.exit(1);
@@ -4080,7 +4114,7 @@ pub fn cmdChangelist(
defer new_tree.deinit(gpa);
for (new_tree.errors) |parse_error| {
- try printErrMsgToStdErr(gpa, parse_error, new_tree, new_source_file, .auto);
+ try printErrMsgToStdErr(gpa, arena, parse_error, new_tree, new_source_file, .auto);
}
if (new_tree.errors.len != 0) {
process.exit(1);