diff options
| author | Cody Tapscott <cody+topolarity@tapscott.me> | 2022-01-24 12:00:01 -0700 |
|---|---|---|
| committer | Cody Tapscott <cody+topolarity@tapscott.me> | 2022-01-24 12:00:01 -0700 |
| commit | 52517e86d6c56c3eb705f8de77e5ef3c81fae2a9 (patch) | |
| tree | 604f96be88fd50c830cf61934d26d10a939104a0 /src/codegen/c.zig | |
| parent | 799bd81b080cebd246698f054cb3c243d39ab4f9 (diff) | |
| download | zig-52517e86d6c56c3eb705f8de77e5ef3c81fae2a9.tar.gz zig-52517e86d6c56c3eb705f8de77e5ef3c81fae2a9.zip | |
Avoid identifier conflicts with reserved C keywords
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 6 |
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), } } |
