aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/c.zig6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index b545b64059..19f98d216a 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -34,6 +34,8 @@ pub const CValue = union(enum) {
/// By-value
decl: *Decl,
decl_ref: *Decl,
+ /// Render the slice as an identifier (using fmtIdent)
+ identifier: []const u8,
/// Render these bytes literally.
/// TODO make this a [*:0]const u8 to save memory
bytes: []const u8,
@@ -78,6 +80,7 @@ fn formatIdent(
) !void {
_ = fmt;
_ = options;
+ try writer.writeAll("__"); // Add double underscore to avoid conflicting with C's reserved keywords
for (ident) |c, i| {
switch (c) {
'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c),
@@ -741,7 +744,7 @@ pub const DeclGen = struct {
if (!field_ty.hasCodeGenBits()) continue;
const alignment = entry.value_ptr.abi_align;
- const name: CValue = .{ .bytes = entry.key_ptr.* };
+ const name: CValue = .{ .identifier = entry.key_ptr.* };
try buffer.append(' ');
try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut, alignment);
try buffer.appendSlice(";\n");
@@ -1033,6 +1036,7 @@ pub const DeclGen = struct {
try w.writeByte('&');
return dg.renderDeclName(decl, w);
},
+ .identifier => |ident| return w.print("{}", .{fmtIdent(ident)}),
.bytes => |bytes| return w.writeAll(bytes),
}
}