Skip to content

Commit d60c100

Browse files
authored
Merge pull request #20865 from ehaas/aro-translate-c-static-assert
Aro translate c: Render error diagnostics properly and ignore _Static_assert decls during translation
2 parents f7cebf2 + 699e103 commit d60c100

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/compiler/aro_translate_c.zig

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ pub fn translate(
123123
var tree = try pp.parse();
124124
defer tree.deinit();
125125

126-
if (driver.comp.diagnostics.errors != 0) {
127-
return error.SemanticAnalyzeFail;
126+
// Workaround for https://github.com/Vexu/arocc/issues/603
127+
for (comp.diagnostics.list.items) |msg| {
128+
if (msg.kind == .@"error" or msg.kind == .@"fatal error") return error.ParsingFailed;
128129
}
129130

130131
const mapper = tree.comp.string_interner.getFastTypeMapper(tree.comp.gpa) catch tree.comp.string_interner.getSlowTypeMapper();
@@ -227,6 +228,7 @@ fn prepopulateGlobalNameTable(c: *Context) !void {
227228
const decl_name = c.tree.tokSlice(data.decl.name);
228229
try c.global_names.put(c.gpa, decl_name, {});
229230
},
231+
.static_assert => {},
230232
else => unreachable,
231233
}
232234
}
@@ -304,6 +306,7 @@ fn transDecl(c: *Context, scope: *Scope, decl: NodeIndex) !void {
304306
=> {
305307
try transVarDecl(c, decl, null);
306308
},
309+
.static_assert => try warn(c, &c.global_scope.base, 0, "ignoring _Static_assert declaration", .{}),
307310
else => unreachable,
308311
}
309312
}
@@ -1622,6 +1625,33 @@ test "Macro matching" {
16221625
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((volatile const void)(X))", "DISCARD");
16231626
}
16241627

1628+
/// Renders errors and fatal errors + associated notes (e.g. "expanded from here"); does not render warnings or associated notes
1629+
/// Terminates with exit code 1
1630+
fn renderErrorsAndExit(comp: *aro.Compilation) noreturn {
1631+
defer std.process.exit(1);
1632+
1633+
var writer = aro.Diagnostics.defaultMsgWriter(std.io.tty.detectConfig(std.io.getStdErr()));
1634+
defer writer.deinit(); // writer deinit must run *before* exit so that stderr is flushed
1635+
1636+
var saw_error = false;
1637+
for (comp.diagnostics.list.items) |msg| {
1638+
switch (msg.kind) {
1639+
.@"error", .@"fatal error" => {
1640+
saw_error = true;
1641+
aro.Diagnostics.renderMessage(comp, &writer, msg);
1642+
},
1643+
.warning => saw_error = false,
1644+
.note => {
1645+
if (saw_error) {
1646+
aro.Diagnostics.renderMessage(comp, &writer, msg);
1647+
}
1648+
},
1649+
.off => {},
1650+
.default => unreachable,
1651+
}
1652+
}
1653+
}
1654+
16251655
pub fn main() !void {
16261656
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
16271657
defer arena_instance.deinit();
@@ -1636,12 +1666,9 @@ pub fn main() !void {
16361666
defer aro_comp.deinit();
16371667

16381668
var tree = translate(gpa, &aro_comp, args) catch |err| switch (err) {
1639-
error.SemanticAnalyzeFail, error.FatalError => {
1640-
aro.Diagnostics.render(&aro_comp, std.io.tty.detectConfig(std.io.getStdErr()));
1641-
std.process.exit(1);
1642-
},
1669+
error.ParsingFailed, error.FatalError => renderErrorsAndExit(&aro_comp),
16431670
error.OutOfMemory => return error.OutOfMemory,
1644-
error.StreamTooLong => std.zig.fatal("StreamTooLong?", .{}),
1671+
error.StreamTooLong => std.zig.fatal("An input file was larger than 4GiB", .{}),
16451672
};
16461673
defer tree.deinit(gpa);
16471674

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_Static_assert(1 == 1, "");
2+
3+
// translate-c
4+
// target=x86_64-linux
5+
// c_frontend=aro
6+
//
7+
// tmp.c:1:1: warning: ignoring _Static_assert declaration

0 commit comments

Comments
 (0)