From 84d15808731978637a1093a96c385a92949dff0d Mon Sep 17 00:00:00 2001 From: MrDmitry Date: Sat, 8 Jun 2024 00:21:06 -0700 Subject: [PATCH] Report error on missing values for addConfigHeader --- lib/std/Build/Step/ConfigHeader.zig | 27 ++++++++++--------- test/standalone/cmakedefine/config.h.in | 2 -- test/standalone/cmakedefine/expected_config.h | 2 -- test/standalone/cmakedefine/expected_sigil.h | 1 - test/standalone/cmakedefine/expected_stack.h | 4 --- .../standalone/cmakedefine/expected_wrapper.h | 5 ---- test/standalone/cmakedefine/sigil.h.in | 1 - test/standalone/cmakedefine/stack.h.in | 4 --- test/standalone/cmakedefine/wrapper.h.in | 5 ---- 9 files changed, 15 insertions(+), 36 deletions(-) diff --git a/lib/std/Build/Step/ConfigHeader.zig b/lib/std/Build/Step/ConfigHeader.zig index 212ea605ed19a2bc85ee814c690685163193550d..6390a88da79dfaa269a761e37f0205446e371c65 100644 --- a/lib/std/Build/Step/ConfigHeader.zig +++ b/lib/std/Build/Step/ConfigHeader.zig @@ -568,7 +568,7 @@ fn expand_variables_cmake( } const key = contents[curr + 1 .. close_pos]; - const value = values.get(key) orelse .undef; + const value = values.get(key) orelse return error.MissingValue; const missing = contents[source_offset..curr]; try result.appendSlice(missing); switch (value) { @@ -623,7 +623,10 @@ fn expand_variables_cmake( const key_start = open_pos.target + open_var.len; const key = result.items[key_start..]; - const value = values.get(key) orelse .undef; + if (key.len == 0) { + return error.MissingKey; + } + const value = values.get(key) orelse return error.MissingValue; result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len); switch (value) { .undef, .defined => {}, @@ -693,8 +696,8 @@ test "expand_variables_cmake simple cases" { // line with misc content is preserved try testReplaceVariables(allocator, "no substitution", "no substitution", values); - // empty ${} wrapper is removed - try testReplaceVariables(allocator, "${}", "", values); + // empty ${} wrapper leads to an error + try std.testing.expectError(error.MissingKey, testReplaceVariables(allocator, "${}", "", values)); // empty @ sigils are preserved try testReplaceVariables(allocator, "@", "@", values); @@ -757,9 +760,9 @@ test "expand_variables_cmake simple cases" { try testReplaceVariables(allocator, "undef@", "undef@", values); try testReplaceVariables(allocator, "undef}", "undef}", values); - // unknown key is removed - try testReplaceVariables(allocator, "@bad@", "", values); - try testReplaceVariables(allocator, "${bad}", "", values); + // unknown key leads to an error + try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@bad@", "", values)); + try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${bad}", "", values)); } test "expand_variables_cmake edge cases" { @@ -804,17 +807,17 @@ test "expand_variables_cmake edge cases" { try testReplaceVariables(allocator, "@dollar@{@string@}", "${text}", values); // when expanded variables contain invalid characters, they prevent further expansion - try testReplaceVariables(allocator, "${${string_var}}", "", values); - try testReplaceVariables(allocator, "${@string_var@}", "", values); + try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${${string_var}}", "", values)); + try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${@string_var@}", "", values)); // nested expanded variables are expanded from the inside out try testReplaceVariables(allocator, "${string${underscore}proxy}", "string", values); try testReplaceVariables(allocator, "${string@underscore@proxy}", "string", values); // nested vars are only expanded when ${} is closed - try testReplaceVariables(allocator, "@nest@underscore@proxy@", "underscore", values); + try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@underscore@proxy@", "", values)); try testReplaceVariables(allocator, "${nest${underscore}proxy}", "nest_underscore_proxy", values); - try testReplaceVariables(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "underscore", values); + try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "", values)); try testReplaceVariables(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values); // invalid characters lead to an error @@ -840,5 +843,5 @@ test "expand_variables_cmake escaped characters" { try testReplaceVariables(allocator, "$\\{string}", "$\\{string}", values); // backslash is skipped when checking for invalid characters, yet it mangles the key - try testReplaceVariables(allocator, "${string\\}", "", values); + try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${string\\}", "", values)); } diff --git a/test/standalone/cmakedefine/config.h.in b/test/standalone/cmakedefine/config.h.in index 4f078970c7a52e7bd368ba415ff8fc0d07b48594..f6f973d004fda121ca30e908c454f29d9e7b0070 100644 --- a/test/standalone/cmakedefine/config.h.in +++ b/test/standalone/cmakedefine/config.h.in @@ -94,8 +94,6 @@ // test10 // @noval@@stringval@@trueval@@zeroval@ -// ${} substition - // no substition // ${noval} diff --git a/test/standalone/cmakedefine/expected_config.h b/test/standalone/cmakedefine/expected_config.h index 580f0f11d88bc56b2645ddbcf36a9a73baa023f7..b991727ffff10c52f412d35f94b4437c2f2f565e 100644 --- a/test/standalone/cmakedefine/expected_config.h +++ b/test/standalone/cmakedefine/expected_config.h @@ -94,8 +94,6 @@ // test10 // test10 -// substition - // no substition // diff --git a/test/standalone/cmakedefine/expected_sigil.h b/test/standalone/cmakedefine/expected_sigil.h index 248daaa6ccd0cee1d92420c902eaf7c770248fa2..10108f4d1e2fdd3c4a685a8501596a77291b0c1f 100644 --- a/test/standalone/cmakedefine/expected_sigil.h +++ b/test/standalone/cmakedefine/expected_sigil.h @@ -1,4 +1,3 @@ -#define VAR #define AT @ #define ATAT @@ #define ATATAT @@@ diff --git a/test/standalone/cmakedefine/expected_stack.h b/test/standalone/cmakedefine/expected_stack.h index fb8876f2b983f4ad284895d8c2eaf1eab18d8988..940d68d13e2f6d98ea518ae031a42c3944d4cf7a 100644 --- a/test/standalone/cmakedefine/expected_stack.h +++ b/test/standalone/cmakedefine/expected_stack.h @@ -1,7 +1,3 @@ #define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY -#define UNDERSCORE UNDERSCORE #define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY -#define UNDERSCORE UNDERSCORE - -#define (empty) diff --git a/test/standalone/cmakedefine/expected_wrapper.h b/test/standalone/cmakedefine/expected_wrapper.h index 0fa39ba64e34d3bc9cb0d9a8e1044152c45fc4d8..cbd6aabdcae959adef77256dc958999a41f07d0f 100644 --- a/test/standalone/cmakedefine/expected_wrapper.h +++ b/test/standalone/cmakedefine/expected_wrapper.h @@ -25,11 +25,6 @@ #define @STRING@ #define @STRING@ -// becomes `empty` -#define -#define - #define \@STRING_VAR\@ #define \${STRING} #define $\{STRING_VAR} -#define diff --git a/test/standalone/cmakedefine/sigil.h.in b/test/standalone/cmakedefine/sigil.h.in index 0f64688ba71dccd1e84384b844c1e803f83b9570..10108f4d1e2fdd3c4a685a8501596a77291b0c1f 100644 --- a/test/standalone/cmakedefine/sigil.h.in +++ b/test/standalone/cmakedefine/sigil.h.in @@ -1,4 +1,3 @@ -#define VAR ${} #define AT @ #define ATAT @@ #define ATATAT @@@ diff --git a/test/standalone/cmakedefine/stack.h.in b/test/standalone/cmakedefine/stack.h.in index d4322f5990487e73b46d731ec22ab80eafce2edc..91fdd8a26516634e7cce1779c276ca5e30a0a2a9 100644 --- a/test/standalone/cmakedefine/stack.h.in +++ b/test/standalone/cmakedefine/stack.h.in @@ -1,7 +1,3 @@ #define NEST_UNDERSCORE_PROXY ${NEST${UNDERSCORE}PROXY} -#define UNDERSCORE @NEST@UNDERSCORE@PROXY@ #define NEST_UNDERSCORE_PROXY ${NEST${${NEST_UNDERSCORE${UNDERSCORE}PROXY}}PROXY} -#define UNDERSCORE @NEST@@NEST_UNDERSCORE@UNDERSCORE@PROXY@@PROXY@ - -#define (empty) ${NEST${${AT}UNDERSCORE${AT}}PROXY} diff --git a/test/standalone/cmakedefine/wrapper.h.in b/test/standalone/cmakedefine/wrapper.h.in index c7323867e1681bbba65c3640e8973bdc8600cdf7..d2334fa9b79c624bfb72c4dcae19b38f57e2cc13 100644 --- a/test/standalone/cmakedefine/wrapper.h.in +++ b/test/standalone/cmakedefine/wrapper.h.in @@ -25,11 +25,6 @@ #define ${STRING_AT} #define @STRING_AT@ -// becomes `empty` -#define ${${STRING_VAR}} -#define ${@STRING_VAR@} - #define \@STRING_VAR\@ #define \${STRING_VAR} #define $\{STRING_VAR} -#define ${STRING_VAR\} -- 2.54.0