authorgravatar for cody+topolarity@tapscott.meCody Tapscott <cody+topolarity@tapscott.me> 2022-01-24 12:00:01-07:00
committergravatar for cody+topolarity@tapscott.meCody Tapscott <cody+topolarity@tapscott.me> 2022-01-24 12:00:01-07:00
log52517e86d6c56c3eb705f8de77e5ef3c81fae2a9
tree604f96be88fd50c830cf61934d26d10a939104a0
parent799bd81b080cebd246698f054cb3c243d39ab4f9

Avoid identifier conflicts with reserved C keywords


1 files changed, 5 insertions(+), 1 deletions(-)

src/codegen/c.zig+5-1
......@@ -34,6 +34,8 @@ pub const CValue = union(enum) {
3434 /// By-value
3535 decl: *Decl,
3636 decl_ref: *Decl,
37 /// Render the slice as an identifier (using fmtIdent)
38 identifier: []const u8,
3739 /// Render these bytes literally.
3840 /// TODO make this a [*:0]const u8 to save memory
3941 bytes: []const u8,
......@@ -78,6 +80,7 @@ fn formatIdent(
7880) !void {
7981 _ = fmt;
8082 _ = options;
83 try writer.writeAll("__"); // Add double underscore to avoid conflicting with C's reserved keywords
8184 for (ident) |c, i| {
8285 switch (c) {
8386 'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c),
......@@ -741,7 +744,7 @@ pub const DeclGen = struct {
741744 if (!field_ty.hasCodeGenBits()) continue;
742745
743746 const alignment = entry.value_ptr.abi_align;
744 const name: CValue = .{ .bytes = entry.key_ptr.* };
747 const name: CValue = .{ .identifier = entry.key_ptr.* };
745748 try buffer.append(' ');
746749 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut, alignment);
747750 try buffer.appendSlice(";\n");
......@@ -1033,6 +1036,7 @@ pub const DeclGen = struct {
10331036 try w.writeByte('&');
10341037 return dg.renderDeclName(decl, w);
10351038 },
1039 .identifier => |ident| return w.print("{}", .{fmtIdent(ident)}),
10361040 .bytes => |bytes| return w.writeAll(bytes),
10371041 }
10381042 }