Commit 7e548af8b1

Techatrix <techatrix@mailbox.org>
2025-02-19 09:03:56
std.Build.Step.ConfigHeader: allow using autoconf values multiple times
1 parent c390f55
Changed files (3)
lib
std
Build
test
standalone
config_header
lib/std/Build/Step/ConfigHeader.zig
@@ -264,8 +264,11 @@ fn render_autoconf(
     values: std.StringArrayHashMap(Value),
     src_path: []const u8,
 ) !void {
-    var values_copy = try values.clone();
-    defer values_copy.deinit();
+    const build = step.owner;
+    const allocator = build.allocator;
+
+    var is_used: std.DynamicBitSetUnmanaged = try .initEmpty(allocator, values.count());
+    defer is_used.deinit(allocator);
 
     var any_errors = false;
     var line_index: u32 = 0;
@@ -284,18 +287,20 @@ fn render_autoconf(
             continue;
         }
         const name = it.rest();
-        const kv = values_copy.fetchSwapRemove(name) orelse {
+        const index = values.getIndex(name) orelse {
             try step.addError("{s}:{d}: error: unspecified config header value: '{s}'", .{
                 src_path, line_index + 1, name,
             });
             any_errors = true;
             continue;
         };
-        try renderValueC(output, name, kv.value);
+        is_used.set(index);
+        try renderValueC(output, name, values.values()[index]);
     }
 
-    for (values_copy.keys()) |name| {
-        try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, name });
+    var unused_value_it = is_used.iterator(.{ .kind = .unset });
+    while (unused_value_it.next()) |index| {
+        try step.addError("{s}: error: config header value unused: '{s}'", .{ src_path, values.keys()[index] });
         any_errors = true;
     }
 
test/standalone/config_header/config.h
@@ -12,3 +12,6 @@ int foo();
 #define SOME_ENUM_LITERAL test
 #define SOME_STRING "test"
 
+// Used twice
+#define SOME_TRUE 1
+
test/standalone/config_header/config.h.in
@@ -10,3 +10,6 @@ int foo();
 #undef SOME_TEN
 #undef SOME_ENUM_LITERAL
 #undef SOME_STRING
+
+// Used twice
+#undef SOME_TRUE