From d7b9bbecaf6ba3ee924d7744ea720c67bc8f0a1b Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Mon, 29 Jul 2024 09:33:21 -0700 Subject: [PATCH 1/3] aro_translate_c: Render errors properly The error count is not set until the diagnostics are actually rendered --- lib/compiler/aro_translate_c.zig | 37 ++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/compiler/aro_translate_c.zig b/lib/compiler/aro_translate_c.zig index ef41911d18f0..3a08f1ee59ca 100644 --- a/lib/compiler/aro_translate_c.zig +++ b/lib/compiler/aro_translate_c.zig @@ -123,8 +123,9 @@ pub fn translate( var tree = try pp.parse(); defer tree.deinit(); - if (driver.comp.diagnostics.errors != 0) { - return error.SemanticAnalyzeFail; + // Workaround for https://github.com/Vexu/arocc/issues/603 + for (comp.diagnostics.list.items) |msg| { + if (msg.kind == .@"error" or msg.kind == .@"fatal error") return error.ParsingFailed; } const mapper = tree.comp.string_interner.getFastTypeMapper(tree.comp.gpa) catch tree.comp.string_interner.getSlowTypeMapper(); @@ -1622,6 +1623,33 @@ test "Macro matching" { try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((volatile const void)(X))", "DISCARD"); } +/// Renders errors and fatal errors + associated notes (e.g. "expanded from here"); does not render warnings or associated notes +/// Terminates with exit code 1 +fn renderErrorsAndExit(comp: *aro.Compilation) noreturn { + defer std.process.exit(1); + + var writer = aro.Diagnostics.defaultMsgWriter(std.io.tty.detectConfig(std.io.getStdErr())); + defer writer.deinit(); // writer deinit must run *before* exit so that stderr is flushed + + var saw_error = false; + for (comp.diagnostics.list.items) |msg| { + switch (msg.kind) { + .@"error", .@"fatal error" => { + saw_error = true; + aro.Diagnostics.renderMessage(comp, &writer, msg); + }, + .warning => saw_error = false, + .note => { + if (saw_error) { + aro.Diagnostics.renderMessage(comp, &writer, msg); + } + }, + .off => {}, + .default => unreachable, + } + } +} + pub fn main() !void { var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena_instance.deinit(); @@ -1636,10 +1664,7 @@ pub fn main() !void { defer aro_comp.deinit(); var tree = translate(gpa, &aro_comp, args) catch |err| switch (err) { - error.SemanticAnalyzeFail, error.FatalError => { - aro.Diagnostics.render(&aro_comp, std.io.tty.detectConfig(std.io.getStdErr())); - std.process.exit(1); - }, + error.ParsingFailed, error.FatalError => renderErrorsAndExit(&aro_comp), error.OutOfMemory => return error.OutOfMemory, error.StreamTooLong => std.zig.fatal("StreamTooLong?", .{}), }; From 5d8e56c2ebf22cc1320aa26d8ed6877bd03036cb Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Mon, 29 Jul 2024 10:12:33 -0700 Subject: [PATCH 2/3] aro_translate_c: do not translate _Static_assert declarations This does not completely ignore static asserts - they are validated by aro during parsing; any failures will render an error and non-zero exit code. Emit a warning comment that _Static_asserts are not translated - this matches the behavior of the existing clang-based translate-c. Aro currently does not store source locations for _Static_assert declarations so I've hard-coded token index 0 for now. --- lib/compiler/aro_translate_c.zig | 2 ++ test/cases/translate_c/_Static_assert.c | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 test/cases/translate_c/_Static_assert.c diff --git a/lib/compiler/aro_translate_c.zig b/lib/compiler/aro_translate_c.zig index 3a08f1ee59ca..372734ace9eb 100644 --- a/lib/compiler/aro_translate_c.zig +++ b/lib/compiler/aro_translate_c.zig @@ -228,6 +228,7 @@ fn prepopulateGlobalNameTable(c: *Context) !void { const decl_name = c.tree.tokSlice(data.decl.name); try c.global_names.put(c.gpa, decl_name, {}); }, + .static_assert => {}, else => unreachable, } } @@ -305,6 +306,7 @@ fn transDecl(c: *Context, scope: *Scope, decl: NodeIndex) !void { => { try transVarDecl(c, decl, null); }, + .static_assert => try warn(c, &c.global_scope.base, 0, "ignoring _Static_assert declaration", .{}), else => unreachable, } } diff --git a/test/cases/translate_c/_Static_assert.c b/test/cases/translate_c/_Static_assert.c new file mode 100644 index 000000000000..8ecc639e9dbb --- /dev/null +++ b/test/cases/translate_c/_Static_assert.c @@ -0,0 +1,7 @@ +_Static_assert(1 == 1, ""); + +// translate-c +// target=x86_64-linux +// c_frontend=aro +// +// tmp.c:1:1: warning: ignoring _Static_assert declaration From 699e10371781a16f6096ea1517acb7d085b2d42f Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Mon, 29 Jul 2024 10:23:28 -0700 Subject: [PATCH 3/3] aro_translate_c: Add a more helpful error message for error.StreamTooLong --- lib/compiler/aro_translate_c.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compiler/aro_translate_c.zig b/lib/compiler/aro_translate_c.zig index 372734ace9eb..02c7547ce58c 100644 --- a/lib/compiler/aro_translate_c.zig +++ b/lib/compiler/aro_translate_c.zig @@ -1668,7 +1668,7 @@ pub fn main() !void { var tree = translate(gpa, &aro_comp, args) catch |err| switch (err) { error.ParsingFailed, error.FatalError => renderErrorsAndExit(&aro_comp), error.OutOfMemory => return error.OutOfMemory, - error.StreamTooLong => std.zig.fatal("StreamTooLong?", .{}), + error.StreamTooLong => std.zig.fatal("An input file was larger than 4GiB", .{}), }; defer tree.deinit(gpa);