authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-01 20:29:35-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-01 20:29:35-05:00
loga3f741e9b8df564f070df0f05ada4875e9118b18
tree11030ab3ec82bc326e8a6e8f437e481f0099cde4
parent365a6124d98966cb5ae1fadaec3e05b1a19c93e7
signature Commit is signed but in an unrecognized format.

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 files changed, 4 insertions(+), 21 deletions(-)

src-self-hosted/translate_c.zig+4-21
......@@ -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) {