Skip to content

Commit 84d1580

Browse files
MrDmitryandrewrk
authored andcommitted
Report error on missing values for addConfigHeader
1 parent 0884a43 commit 84d1580

File tree

9 files changed

+15
-36
lines changed

9 files changed

+15
-36
lines changed

lib/std/Build/Step/ConfigHeader.zig

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ fn expand_variables_cmake(
568568
}
569569

570570
const key = contents[curr + 1 .. close_pos];
571-
const value = values.get(key) orelse .undef;
571+
const value = values.get(key) orelse return error.MissingValue;
572572
const missing = contents[source_offset..curr];
573573
try result.appendSlice(missing);
574574
switch (value) {
@@ -623,7 +623,10 @@ fn expand_variables_cmake(
623623

624624
const key_start = open_pos.target + open_var.len;
625625
const key = result.items[key_start..];
626-
const value = values.get(key) orelse .undef;
626+
if (key.len == 0) {
627+
return error.MissingKey;
628+
}
629+
const value = values.get(key) orelse return error.MissingValue;
627630
result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
628631
switch (value) {
629632
.undef, .defined => {},
@@ -693,8 +696,8 @@ test "expand_variables_cmake simple cases" {
693696
// line with misc content is preserved
694697
try testReplaceVariables(allocator, "no substitution", "no substitution", values);
695698

696-
// empty ${} wrapper is removed
697-
try testReplaceVariables(allocator, "${}", "", values);
699+
// empty ${} wrapper leads to an error
700+
try std.testing.expectError(error.MissingKey, testReplaceVariables(allocator, "${}", "", values));
698701

699702
// empty @ sigils are preserved
700703
try testReplaceVariables(allocator, "@", "@", values);
@@ -757,9 +760,9 @@ test "expand_variables_cmake simple cases" {
757760
try testReplaceVariables(allocator, "undef@", "undef@", values);
758761
try testReplaceVariables(allocator, "undef}", "undef}", values);
759762

760-
// unknown key is removed
761-
try testReplaceVariables(allocator, "@bad@", "", values);
762-
try testReplaceVariables(allocator, "${bad}", "", values);
763+
// unknown key leads to an error
764+
try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@bad@", "", values));
765+
try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${bad}", "", values));
763766
}
764767

765768
test "expand_variables_cmake edge cases" {
@@ -804,17 +807,17 @@ test "expand_variables_cmake edge cases" {
804807
try testReplaceVariables(allocator, "@dollar@{@string@}", "${text}", values);
805808

806809
// when expanded variables contain invalid characters, they prevent further expansion
807-
try testReplaceVariables(allocator, "${${string_var}}", "", values);
808-
try testReplaceVariables(allocator, "${@string_var@}", "", values);
810+
try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${${string_var}}", "", values));
811+
try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${@string_var@}", "", values));
809812

810813
// nested expanded variables are expanded from the inside out
811814
try testReplaceVariables(allocator, "${string${underscore}proxy}", "string", values);
812815
try testReplaceVariables(allocator, "${string@underscore@proxy}", "string", values);
813816

814817
// nested vars are only expanded when ${} is closed
815-
try testReplaceVariables(allocator, "@nest@underscore@proxy@", "underscore", values);
818+
try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@underscore@proxy@", "", values));
816819
try testReplaceVariables(allocator, "${nest${underscore}proxy}", "nest_underscore_proxy", values);
817-
try testReplaceVariables(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "underscore", values);
820+
try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@nest@@nest_underscore@underscore@proxy@@proxy@", "", values));
818821
try testReplaceVariables(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values);
819822

820823
// invalid characters lead to an error
@@ -840,5 +843,5 @@ test "expand_variables_cmake escaped characters" {
840843
try testReplaceVariables(allocator, "$\\{string}", "$\\{string}", values);
841844

842845
// backslash is skipped when checking for invalid characters, yet it mangles the key
843-
try testReplaceVariables(allocator, "${string\\}", "", values);
846+
try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${string\\}", "", values));
844847
}

test/standalone/cmakedefine/config.h.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@
9494
// test10
9595
// @noval@@stringval@@trueval@@zeroval@
9696

97-
// ${} substition
98-
9997
// no substition
10098
// ${noval}
10199

test/standalone/cmakedefine/expected_config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@
9494
// test10
9595
// test10
9696

97-
// substition
98-
9997
// no substition
10098
//
10199

test/standalone/cmakedefine/expected_sigil.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define VAR
21
#define AT @
32
#define ATAT @@
43
#define ATATAT @@@
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
#define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY
2-
#define UNDERSCORE UNDERSCORE
32

43
#define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY
5-
#define UNDERSCORE UNDERSCORE
6-
7-
#define (empty)

test/standalone/cmakedefine/expected_wrapper.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
#define @STRING@
2626
#define @STRING@
2727

28-
// becomes `empty`
29-
#define
30-
#define
31-
3228
#define \@STRING_VAR\@
3329
#define \${STRING}
3430
#define $\{STRING_VAR}
35-
#define

test/standalone/cmakedefine/sigil.h.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define VAR ${}
21
#define AT @
32
#define ATAT @@
43
#define ATATAT @@@
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
#define NEST_UNDERSCORE_PROXY ${NEST${UNDERSCORE}PROXY}
2-
#define UNDERSCORE @NEST@UNDERSCORE@PROXY@
32

43
#define NEST_UNDERSCORE_PROXY ${NEST${${NEST_UNDERSCORE${UNDERSCORE}PROXY}}PROXY}
5-
#define UNDERSCORE @NEST@@NEST_UNDERSCORE@UNDERSCORE@PROXY@@PROXY@
6-
7-
#define (empty) ${NEST${${AT}UNDERSCORE${AT}}PROXY}

test/standalone/cmakedefine/wrapper.h.in

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
#define ${STRING_AT}
2626
#define @STRING_AT@
2727

28-
// becomes `empty`
29-
#define ${${STRING_VAR}}
30-
#define ${@STRING_VAR@}
31-
3228
#define \@STRING_VAR\@
3329
#define \${STRING_VAR}
3430
#define $\{STRING_VAR}
35-
#define ${STRING_VAR\}

0 commit comments

Comments
 (0)