diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2023-03-05 02:35:32 -0500 |
|---|---|---|
| committer | Jacob Young <jacobly0@users.noreply.github.com> | 2023-03-05 02:59:02 -0500 |
| commit | 33fa25ba4470bf000280a94f0376988b05918b75 (patch) | |
| tree | f3267a88de5aa5161634aaaabd1b7072855d6483 /src/codegen/c.zig | |
| parent | 0b0298aff27a31a7f45828d96d95adfdde61a085 (diff) | |
| download | zig-33fa25ba4470bf000280a94f0376988b05918b75.tar.gz zig-33fa25ba4470bf000280a94f0376988b05918b75.zip | |
CBE: ensure uniqueness of more internal identifiers
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 5e64823a0d..f1761ed80d 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1841,30 +1841,21 @@ pub const DeclGen = struct { dg.module.markDeclAlive(decl); if (dg.module.decl_exports.get(decl_index)) |exports| { - return writer.writeAll(exports.items[export_index].options.name); + try writer.writeAll(exports.items[export_index].options.name); } else if (decl.isExtern()) { - return writer.writeAll(mem.sliceTo(decl.name, 0)); - } else if (dg.module.test_functions.get(decl_index)) |_| { - const gpa = dg.gpa; - const name = try decl.getFullyQualifiedName(dg.module); - defer gpa.free(name); - return writer.print("{}_{d}", .{ fmtIdent(name), @enumToInt(decl_index) }); + try writer.writeAll(mem.sliceTo(decl.name, 0)); } else { - const gpa = dg.gpa; - const name = try decl.getFullyQualifiedName(dg.module); - defer gpa.free(name); - - // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), expand - // to 3x the length of its input - if (name.len > 1365) { - var hash = ident_hasher_init; - hash.update(name); - const ident_hash = hash.finalInt(); - try writer.writeAll("zig_D_"); - return std.fmt.formatIntValue(ident_hash, "x", .{}, writer); - } else { - return writer.print("{}", .{fmtIdent(name)}); - } + // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), + // expand to 3x the length of its input, but let's cut it off at a much shorter limit. + var name: [100]u8 = undefined; + var name_stream = std.io.fixedBufferStream(&name); + decl.renderFullyQualifiedName(dg.module, name_stream.writer()) catch |err| switch (err) { + error.NoSpaceLeft => {}, + }; + try writer.print("{}__{d}", .{ + fmtIdent(name_stream.getWritten()), + @enumToInt(decl_index), + }); } } |
