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) {...@@ -34,6 +34,8 @@ pub const CValue = union(enum) {
34 /// By-value34 /// By-value
35 decl: *Decl,35 decl: *Decl,
36 decl_ref: *Decl,36 decl_ref: *Decl,
37 /// Render the slice as an identifier (using fmtIdent)
38 identifier: []const u8,
37 /// Render these bytes literally.39 /// Render these bytes literally.
38 /// TODO make this a [*:0]const u8 to save memory40 /// TODO make this a [*:0]const u8 to save memory
39 bytes: []const u8,41 bytes: []const u8,
...@@ -78,6 +80,7 @@ fn formatIdent(...@@ -78,6 +80,7 @@ fn formatIdent(
78) !void {80) !void {
79 _ = fmt;81 _ = fmt;
80 _ = options;82 _ = options;
83 try writer.writeAll("__"); // Add double underscore to avoid conflicting with C's reserved keywords
81 for (ident) |c, i| {84 for (ident) |c, i| {
82 switch (c) {85 switch (c) {
83 'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c),86 'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c),
...@@ -741,7 +744,7 @@ pub const DeclGen = struct {...@@ -741,7 +744,7 @@ pub const DeclGen = struct {
741 if (!field_ty.hasCodeGenBits()) continue;744 if (!field_ty.hasCodeGenBits()) continue;
742745
743 const alignment = entry.value_ptr.abi_align;746 const alignment = entry.value_ptr.abi_align;
744 const name: CValue = .{ .bytes = entry.key_ptr.* };747 const name: CValue = .{ .identifier = entry.key_ptr.* };
745 try buffer.append(' ');748 try buffer.append(' ');
746 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut, alignment);749 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut, alignment);
747 try buffer.appendSlice(";\n");750 try buffer.appendSlice(";\n");
...@@ -1033,6 +1036,7 @@ pub const DeclGen = struct {...@@ -1033,6 +1036,7 @@ pub const DeclGen = struct {
1033 try w.writeByte('&');1036 try w.writeByte('&');
1034 return dg.renderDeclName(decl, w);1037 return dg.renderDeclName(decl, w);
1035 },1038 },
1039 .identifier => |ident| return w.print("{}", .{fmtIdent(ident)}),
1036 .bytes => |bytes| return w.writeAll(bytes),1040 .bytes => |bytes| return w.writeAll(bytes),
1037 }1041 }
1038 }1042 }