Skip to content

Commit a3f741e

Browse files
committed
translate-c: avoid producing duplicate macro errors
This input file, for example, would produce duplicate identifiers in the translated Zig code: ``` #define bar err( #define bar err( ```
1 parent 365a612 commit a3f741e

File tree

1 file changed

+4
-21
lines changed

1 file changed

+4
-21
lines changed

src-self-hosted/translate_c.zig

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,6 @@ const Scope = struct {
137137
};
138138
}
139139

140-
/// Given the desired name, return a name that does not shadow anything from outer scopes.
141-
/// Does not insert the returned name into the scope.
142-
/// Will allow `name` to be one of the preprocessed decl or macro names, but will not
143-
/// choose a mangled name that matches one of those.
144-
fn makeMangledName(scope: *Root, name: []const u8) ![]const u8 {
145-
if (!scope.containsNow(name)) {
146-
_ = try scope.context.global_names.put(name, {});
147-
return name;
148-
}
149-
var proposed_name = name;
150-
while (scope.contains(proposed_name)) {
151-
proposed_name = try std.fmt.allocPrint(scope.context.a(), "{}_{}", .{
152-
name,
153-
scope.context.getMangle(),
154-
});
155-
}
156-
_ = try scope.context.global_names.put(proposed_name, {});
157-
return proposed_name;
158-
}
159-
160140
/// Check if the global scope contains this name, without looking into the "future", e.g.
161141
/// ignore the preprocessed decl and macro names.
162142
fn containsNow(scope: *Root, name: []const u8) bool {
@@ -4330,7 +4310,10 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
43304310
const name = try c.str(raw_name);
43314311
// TODO https://github.com/ziglang/zig/issues/3756
43324312
// TODO https://github.com/ziglang/zig/issues/1802
4333-
const mangled_name = try scope.makeMangledName(name);
4313+
const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.a(), "_{}", .{name}) else name;
4314+
if (scope.containsNow(mangled_name)) {
4315+
continue;
4316+
}
43344317

43354318
const begin_c = ZigClangSourceManager_getCharacterData(c.source_manager, begin_loc);
43364319
ctok.tokenizeCMacro(c, begin_loc, mangled_name, &tok_list, begin_c) catch |err| switch (err) {

0 commit comments

Comments
 (0)