Skip to content

Commit 9275ae5

Browse files
committed
compiletest: Allow escaped " in //@ normalize-* headers
This makes it easier to normalize output containing double-quotes, such as JSON strings.
1 parent 7a08f84 commit 9275ae5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/tools/compiletest/src/header.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,20 +1317,21 @@ fn expand_variables(mut value: String, config: &Config) -> String {
13171317
fn parse_normalize_rule(header: &str) -> Option<(String, String)> {
13181318
// FIXME(#126370): A colon after the header name should be mandatory, but
13191319
// currently is not, and there are many tests that lack the colon.
1320-
// FIXME: Support escaped double-quotes in strings.
13211320
let captures = static_regex!(
13221321
r#"(?x) # (verbose mode regex)
13231322
^
1324-
[^:\s]+:?\s* # (header name followed by optional colon)
1325-
"(?<regex>[^"]*)" # "REGEX"
1326-
\s+->\s+ # ->
1327-
"(?<replacement>[^"]*)" # "REPLACEMENT"
1323+
[^:\s]+:?\s* # (header name followed by optional colon)
1324+
"(?<regex>(?:\\"|[^"])*)" # "REGEX"
1325+
\s+->\s+ # ->
1326+
"(?<replacement>(?:\\"|[^"])*)" # "REPLACEMENT"
13281327
$
13291328
"#
13301329
)
13311330
.captures(header)?;
1331+
// The regex engine will unescape `\"` to `"`.
13321332
let regex = captures["regex"].to_owned();
1333-
let replacement = captures["replacement"].to_owned();
1333+
// Unescape any escaped double-quotes in the replacement string.
1334+
let replacement = captures["replacement"].replace(r#"\""#, r#"""#);
13341335
Some((regex, replacement))
13351336
}
13361337

src/tools/compiletest/src/header/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ fn test_parse_normalize_rule() {
4646
"something (32 bits)",
4747
"something ($WORD bits)",
4848
),
49+
(
50+
r#"normalize-stout-test: "\"json\"key\"" -> "\"json\"value\"""#,
51+
r#"\"json\"key\""#,
52+
r#""json"value""#,
53+
),
4954
];
5055

5156
for &(input, expected_regex, expected_replacement) in good_data {

0 commit comments

Comments
 (0)