@@ -14,6 +14,7 @@ use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
14
14
use crate :: header:: cfg:: parse_cfg_name_directive;
15
15
use crate :: header:: cfg:: MatchOutcome ;
16
16
use crate :: header:: needs:: CachedNeedsConditions ;
17
+ use crate :: util:: static_regex;
17
18
use crate :: { extract_cdb_version, extract_gdb_version} ;
18
19
19
20
mod cfg;
@@ -1186,11 +1187,11 @@ impl Config {
1186
1187
}
1187
1188
}
1188
1189
1189
- fn parse_custom_normalization ( & self , mut line : & str , prefix : & str ) -> Option < ( String , String ) > {
1190
+ fn parse_custom_normalization ( & self , line : & str , prefix : & str ) -> Option < ( String , String ) > {
1190
1191
if parse_cfg_name_directive ( self , line, prefix) . outcome == MatchOutcome :: Match {
1191
- let from = parse_normalization_string ( & mut line) ? ;
1192
- let to = parse_normalization_string ( & mut line) ? ;
1193
- Some ( ( from , to ) )
1192
+ let ( regex , replacement ) = parse_normalize_rule ( line)
1193
+ . unwrap_or_else ( || panic ! ( "couldn't parse custom normalization rule: `{ line}`" ) ) ;
1194
+ Some ( ( regex , replacement ) )
1194
1195
} else {
1195
1196
None
1196
1197
}
@@ -1311,24 +1312,29 @@ fn expand_variables(mut value: String, config: &Config) -> String {
1311
1312
value
1312
1313
}
1313
1314
1314
- /// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
1315
- /// variable after the end of the quoted string.
1316
- ///
1317
- /// # Examples
1318
- ///
1319
- /// ```
1320
- /// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
1321
- /// let first = parse_normalization_string(&mut s);
1322
- /// assert_eq!(first, Some("something (32 bits)".to_owned()));
1323
- /// assert_eq!(s, " -> \"something ($WORD bits)\".");
1315
+ /// Parses the regex and replacement values of a `//@ normalize-*` header,
1316
+ /// in the format:
1317
+ /// ```text
1318
+ /// normalize-*: "REGEX" -> "REPLACEMENT"
1324
1319
/// ```
1325
- fn parse_normalization_string ( line : & mut & str ) -> Option < String > {
1326
- // FIXME support escapes in strings.
1327
- let begin = line. find ( '"' ) ? + 1 ;
1328
- let end = line[ begin..] . find ( '"' ) ? + begin;
1329
- let result = line[ begin..end] . to_owned ( ) ;
1330
- * line = & line[ end + 1 ..] ;
1331
- Some ( result)
1320
+ fn parse_normalize_rule ( header : & str ) -> Option < ( String , String ) > {
1321
+ // FIXME(#126370): A colon after the header name should be mandatory, but
1322
+ // currently is not, and there are many tests that lack the colon.
1323
+ // FIXME: Support escaped double-quotes in strings.
1324
+ let captures = static_regex ! (
1325
+ r#"(?x) # (verbose mode regex)
1326
+ ^
1327
+ [^:\s]+:?\s* # (header name followed by optional colon)
1328
+ "(?<regex>[^"]*)" # "REGEX"
1329
+ \s+->\s+ # ->
1330
+ "(?<replacement>[^"]*)" # "REPLACEMENT"
1331
+ $
1332
+ "#
1333
+ )
1334
+ . captures ( header) ?;
1335
+ let regex = captures[ "regex" ] . to_owned ( ) ;
1336
+ let replacement = captures[ "replacement" ] . to_owned ( ) ;
1337
+ Some ( ( regex, replacement) )
1332
1338
}
1333
1339
1334
1340
pub fn extract_llvm_version ( version : & str ) -> Option < u32 > {
0 commit comments