Skip to content

Commit 4af305b

Browse files
authored
Merge pull request #13891 from Vexu/compile-errors
Organize and implement remaining stage1 compile errors
2 parents 3bf97bf + 014009a commit 4af305b

107 files changed

Lines changed: 578 additions & 533 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/std/zig/parse.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const Parser = struct {
286286
.keyword_comptime => switch (p.token_tags[p.tok_i + 1]) {
287287
.l_brace => {
288288
if (doc_comment) |some| {
289-
try p.warnMsg(.{ .tag = .test_doc_comment, .token = some });
289+
try p.warnMsg(.{ .tag = .comptime_doc_comment, .token = some });
290290
}
291291
const comptime_token = p.nextToken();
292292
const block = p.parseBlock() catch |err| switch (err) {

lib/std/zig/parser_test.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,6 +4210,18 @@ test "zig fmt: remove newlines surrounding doc comment within container decl" {
42104210
);
42114211
}
42124212

4213+
test "zig fmt: invalid else branch statement" {
4214+
try testError(
4215+
\\/// This is a doc comment for a comptime block.
4216+
\\comptime {}
4217+
\\/// This is a doc comment for a test
4218+
\\test "This is my test" {}
4219+
, &[_]Error{
4220+
.comptime_doc_comment,
4221+
.test_doc_comment,
4222+
});
4223+
}
4224+
42134225
test "zig fmt: invalid else branch statement" {
42144226
try testError(
42154227
\\comptime {

src/Compilation.zig

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const clangMain = @import("main.zig").clangMain;
3131
const Module = @import("Module.zig");
3232
const Cache = @import("Cache.zig");
3333
const translate_c = @import("translate_c.zig");
34+
const clang = @import("clang.zig");
3435
const c_codegen = @import("codegen/c.zig");
3536
const ThreadPool = @import("ThreadPool.zig");
3637
const WaitGroup = @import("WaitGroup.zig");
@@ -2749,6 +2750,9 @@ pub fn totalErrorCount(self: *Compilation) usize {
27492750
const decl = module.declPtr(key);
27502751
if (decl.getFileScope().okToReportErrors()) {
27512752
total += 1;
2753+
if (module.cimport_errors.get(key)) |errors| {
2754+
total += errors.len;
2755+
}
27522756
}
27532757
}
27542758
if (module.emit_h) |emit_h| {
@@ -2858,6 +2862,23 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
28582862
// We'll try again once parsing succeeds.
28592863
if (decl.getFileScope().okToReportErrors()) {
28602864
try AllErrors.add(module, &arena, &errors, entry.value_ptr.*.*);
2865+
if (module.cimport_errors.get(entry.key_ptr.*)) |cimport_errors| for (cimport_errors) |c_error| {
2866+
if (c_error.path) |some|
2867+
try errors.append(.{
2868+
.src = .{
2869+
.src_path = try arena_allocator.dupe(u8, std.mem.span(some)),
2870+
.span = .{ .start = c_error.offset, .end = c_error.offset + 1, .main = c_error.offset },
2871+
.msg = try arena_allocator.dupe(u8, std.mem.span(c_error.msg)),
2872+
.line = c_error.line,
2873+
.column = c_error.column,
2874+
.source_line = if (c_error.source_line) |line| try arena_allocator.dupe(u8, std.mem.span(line)) else null,
2875+
},
2876+
})
2877+
else
2878+
try errors.append(.{
2879+
.plain = .{ .msg = try arena_allocator.dupe(u8, std.mem.span(c_error.msg)) },
2880+
});
2881+
};
28612882
}
28622883
}
28632884
}
@@ -3524,7 +3545,7 @@ test "cImport" {
35243545

35253546
const CImportResult = struct {
35263547
out_zig_path: []u8,
3527-
errors: []translate_c.ClangErrMsg,
3548+
errors: []clang.ErrorMsg,
35283549
};
35293550

35303551
/// Caller owns returned memory.
@@ -3599,7 +3620,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
35993620

36003621
const c_headers_dir_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{"include"});
36013622
const c_headers_dir_path_z = try arena.dupeZ(u8, c_headers_dir_path);
3602-
var clang_errors: []translate_c.ClangErrMsg = &[0]translate_c.ClangErrMsg{};
3623+
var clang_errors: []clang.ErrorMsg = &[0]clang.ErrorMsg{};
36033624
var tree = translate_c.translate(
36043625
comp.gpa,
36053626
new_argv.ptr,
@@ -3665,7 +3686,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
36653686
}
36663687
return CImportResult{
36673688
.out_zig_path = out_zig_path,
3668-
.errors = &[0]translate_c.ClangErrMsg{},
3689+
.errors = &[0]clang.ErrorMsg{},
36693690
};
36703691
}
36713692

src/Module.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const target_util = @import("target.zig");
3131
const build_options = @import("build_options");
3232
const Liveness = @import("Liveness.zig");
3333
const isUpDir = @import("introspect.zig").isUpDir;
34+
const clang = @import("clang.zig");
3435

3536
/// General-purpose allocator. Used for both temporary and long-term storage.
3637
gpa: Allocator,
@@ -111,6 +112,9 @@ failed_embed_files: std.AutoArrayHashMapUnmanaged(*EmbedFile, *ErrorMsg) = .{},
111112
/// Using a map here for consistency with the other fields here.
112113
/// The ErrorMsg memory is owned by the `Export`, using Module's general purpose allocator.
113114
failed_exports: std.AutoArrayHashMapUnmanaged(*Export, *ErrorMsg) = .{},
115+
/// If a decl failed due to a cimport error, the corresponding Clang errors
116+
/// are stored here.
117+
cimport_errors: std.AutoArrayHashMapUnmanaged(Decl.Index, []CImportError) = .{},
114118

115119
/// Candidates for deletion. After a semantic analysis update completes, this list
116120
/// contains Decls that need to be deleted if they end up having no references to them.
@@ -172,6 +176,21 @@ reference_table: std.AutoHashMapUnmanaged(Decl.Index, struct {
172176
src: LazySrcLoc,
173177
}) = .{},
174178

179+
pub const CImportError = struct {
180+
offset: u32,
181+
line: u32,
182+
column: u32,
183+
path: ?[*:0]u8,
184+
source_line: ?[*:0]u8,
185+
msg: [*:0]u8,
186+
187+
pub fn deinit(err: CImportError, gpa: Allocator) void {
188+
if (err.path) |some| gpa.free(std.mem.span(some));
189+
if (err.source_line) |some| gpa.free(std.mem.span(some));
190+
gpa.free(std.mem.span(err.msg));
191+
}
192+
};
193+
175194
pub const StringLiteralContext = struct {
176195
bytes: *ArrayListUnmanaged(u8),
177196

@@ -3449,6 +3468,11 @@ pub fn deinit(mod: *Module) void {
34493468
}
34503469
mod.failed_exports.deinit(gpa);
34513470

3471+
for (mod.cimport_errors.values()) |errs| {
3472+
for (errs) |err| err.deinit(gpa);
3473+
}
3474+
mod.cimport_errors.deinit(gpa);
3475+
34523476
mod.compile_log_decls.deinit(gpa);
34533477

34543478
for (mod.decl_exports.values()) |*export_list| {
@@ -5381,6 +5405,9 @@ pub fn clearDecl(
53815405
if (mod.failed_decls.fetchSwapRemove(decl_index)) |kv| {
53825406
kv.value.destroy(gpa);
53835407
}
5408+
if (mod.cimport_errors.fetchSwapRemove(decl_index)) |kv| {
5409+
for (kv.value) |err| err.deinit(gpa);
5410+
}
53845411
if (mod.emit_h) |emit_h| {
53855412
if (emit_h.failed_decls.fetchSwapRemove(decl_index)) |kv| {
53865413
kv.value.destroy(gpa);
@@ -5768,6 +5795,9 @@ fn markOutdatedDecl(mod: *Module, decl_index: Decl.Index) !void {
57685795
if (mod.failed_decls.fetchSwapRemove(decl_index)) |kv| {
57695796
kv.value.destroy(mod.gpa);
57705797
}
5798+
if (mod.cimport_errors.fetchSwapRemove(decl_index)) |kv| {
5799+
for (kv.value) |err| err.deinit(mod.gpa);
5800+
}
57715801
if (decl.has_tv and decl.owns_tv) {
57725802
if (decl.val.castTag(.function)) |payload| {
57735803
const func = payload.data;

0 commit comments

Comments
 (0)