Commit 131b328c10

Tw <weii.tan>
2023-04-26 11:21:56
translate-c: deduplicate global declaration
Fix #15456 Signed-off-by: Tw <weii.tan>
1 parent 363269a
Changed files (1)
src/translate_c.zig
@@ -4195,8 +4195,11 @@ fn maybeSuppressResult(c: *Context, used: ResultUsed, result: Node) TransError!N
 }
 
 fn addTopLevelDecl(c: *Context, name: []const u8, decl_node: Node) !void {
-    try c.global_scope.sym_table.put(name, decl_node);
-    try c.global_scope.nodes.append(decl_node);
+    const gop = try c.global_scope.sym_table.getOrPut(name);
+    if (!gop.found_existing) {
+        gop.value_ptr.* = decl_node;
+        try c.global_scope.nodes.append(decl_node);
+    }
 }
 
 fn transQualTypeInitializedStringLiteral(c: *Context, elem_ty: Node, string_lit: *const clang.StringLiteral) TypeError!Node {