Report error on missing values for addConfigHeader

This commit is contained in:
MrDmitry 2024-06-08 00:21:06 -07:00 committed by Andrew Kelley
parent 0884a43411
commit 84d1580873
9 changed files with 15 additions and 36 deletions

View File

@ -568,7 +568,7 @@ fn expand_variables_cmake(
} }
const key = contents[curr + 1 .. close_pos]; 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]; const missing = contents[source_offset..curr];
try result.appendSlice(missing); try result.appendSlice(missing);
switch (value) { switch (value) {
@ -623,7 +623,10 @@ fn expand_variables_cmake(
const key_start = open_pos.target + open_var.len; const key_start = open_pos.target + open_var.len;
const key = result.items[key_start..]; 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); result.shrinkRetainingCapacity(result.items.len - key.len - open_var.len);
switch (value) { switch (value) {
.undef, .defined => {}, .undef, .defined => {},
@ -693,8 +696,8 @@ test "expand_variables_cmake simple cases" {
// line with misc content is preserved // line with misc content is preserved
try testReplaceVariables(allocator, "no substitution", "no substitution", values); try testReplaceVariables(allocator, "no substitution", "no substitution", values);
// empty ${} wrapper is removed // empty ${} wrapper leads to an error
try testReplaceVariables(allocator, "${}", "", values); try std.testing.expectError(error.MissingKey, testReplaceVariables(allocator, "${}", "", values));
// empty @ sigils are preserved // empty @ sigils are preserved
try testReplaceVariables(allocator, "@", "@", values); 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);
try testReplaceVariables(allocator, "undef}", "undef}", values); try testReplaceVariables(allocator, "undef}", "undef}", values);
// unknown key is removed // unknown key leads to an error
try testReplaceVariables(allocator, "@bad@", "", values); try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "@bad@", "", values));
try testReplaceVariables(allocator, "${bad}", "", values); try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${bad}", "", values));
} }
test "expand_variables_cmake edge cases" { test "expand_variables_cmake edge cases" {
@ -804,17 +807,17 @@ test "expand_variables_cmake edge cases" {
try testReplaceVariables(allocator, "@dollar@{@string@}", "${text}", values); try testReplaceVariables(allocator, "@dollar@{@string@}", "${text}", values);
// when expanded variables contain invalid characters, they prevent further expansion // when expanded variables contain invalid characters, they prevent further expansion
try testReplaceVariables(allocator, "${${string_var}}", "", values); try std.testing.expectError(error.MissingValue, testReplaceVariables(allocator, "${${string_var}}", "", values));
try 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 // 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);
try testReplaceVariables(allocator, "${string@underscore@proxy}", "string", values); try testReplaceVariables(allocator, "${string@underscore@proxy}", "string", values);
// nested vars are only expanded when ${} is closed // 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${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); try testReplaceVariables(allocator, "${nest${${nest_underscore${underscore}proxy}}proxy}", "nest_underscore_proxy", values);
// invalid characters lead to an error // invalid characters lead to an error
@ -840,5 +843,5 @@ test "expand_variables_cmake escaped characters" {
try testReplaceVariables(allocator, "$\\{string}", "$\\{string}", values); try testReplaceVariables(allocator, "$\\{string}", "$\\{string}", values);
// backslash is skipped when checking for invalid characters, yet it mangles the key // 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));
} }

View File

@ -94,8 +94,6 @@
// test10 // test10
// @noval@@stringval@@trueval@@zeroval@ // @noval@@stringval@@trueval@@zeroval@
// ${} substition
// no substition // no substition
// ${noval} // ${noval}

View File

@ -94,8 +94,6 @@
// test10 // test10
// test10 // test10
// substition
// no substition // no substition
// //

View File

@ -1,4 +1,3 @@
#define VAR
#define AT @ #define AT @
#define ATAT @@ #define ATAT @@
#define ATATAT @@@ #define ATATAT @@@

View File

@ -1,7 +1,3 @@
#define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY #define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY
#define UNDERSCORE UNDERSCORE
#define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY #define NEST_UNDERSCORE_PROXY NEST_UNDERSCORE_PROXY
#define UNDERSCORE UNDERSCORE
#define (empty)

View File

@ -25,11 +25,6 @@
#define @STRING@ #define @STRING@
#define @STRING@ #define @STRING@
// becomes `empty`
#define
#define
#define \@STRING_VAR\@ #define \@STRING_VAR\@
#define \${STRING} #define \${STRING}
#define $\{STRING_VAR} #define $\{STRING_VAR}
#define

View File

@ -1,4 +1,3 @@
#define VAR ${}
#define AT @ #define AT @
#define ATAT @@ #define ATAT @@
#define ATATAT @@@ #define ATATAT @@@

View File

@ -1,7 +1,3 @@
#define NEST_UNDERSCORE_PROXY ${NEST${UNDERSCORE}PROXY} #define NEST_UNDERSCORE_PROXY ${NEST${UNDERSCORE}PROXY}
#define UNDERSCORE @NEST@UNDERSCORE@PROXY@
#define NEST_UNDERSCORE_PROXY ${NEST${${NEST_UNDERSCORE${UNDERSCORE}PROXY}}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}

View File

@ -25,11 +25,6 @@
#define ${STRING_AT} #define ${STRING_AT}
#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}
#define $\{STRING_VAR} #define $\{STRING_VAR}
#define ${STRING_VAR\}