Balance parentheses in rustc command parsing
Dependencies
- [2]
L6QJNN62Parse rustc self-profile data using `analyzeme`
Change contents
- edit in src/annotations/self_profile.rs at line 81
}fn count_parens(source: &str) -> i32 {source.chars().fold(0, |count, character| match character {'(' => count + 1,')' => count - 1,_ => count,})}fn balance_parens<'a>(source: &'a str, iterator: &mut impl Iterator<Item = &'a str>) -> String {let mut buffer = source.to_string();// If there are more closing parentheses than opening, there's a bug// (most likely from this argument parsing code)let mut open_parens = Self::count_parens(&buffer);assert!(open_parens >= 0,"Got too many closing parentheses while parsing argument: `{source}`");while open_parens > 0 {let next_chunk = iterator.next().expect(&format!("Ran out of arguments while searching for closing parenthesis: `{buffer}`"));buffer.push_str(next_chunk);open_parens += Self::count_parens(next_chunk);}buffer - replacement in src/annotations/self_profile.rs at line 123
while let Some(chunk) = arg_chunks.next() {while let Some(initial_chunk) = arg_chunks.next() {// Need to handle cases such as `--check-cfg cfg(feature, values("default", "simd"))`// To do this, consume more chunks until the parentheses are balancedlet owned_chunk = Self::balance_parens(initial_chunk, &mut arg_chunks);let chunk = owned_chunk.as_str(); - replacement in src/annotations/self_profile.rs at line 139
panic!("Found multiple unexpected arguments, unable to parse filename. First: `{previous_filename}` Second: `{chunk}`")panic!("Found multiple unexpected arguments, unable to parse filename. First: `{previous_filename}` Second: `{chunk}`"); - replacement in src/annotations/self_profile.rs at line 145
(name.strip_prefix(prefix).unwrap(), value)(name.strip_prefix(prefix).unwrap(), value.to_string()) - replacement in src/annotations/self_profile.rs at line 151
(arg, value)(arg, Self::balance_parens(value, &mut arg_chunks)) - edit in src/annotations/self_profile.rs at line 267
// .map(|arg| {// // Can't split on commas if inside (parentheses)// // Example: `--check-cfg cfg(feature, values("default", "simd"))`// let mut split_points = Vec::new();// let mut open_parens = 0;// for (index, character) in arg.chars().enumerate() {// match character {// '(' => open_parens += 1,// ')' => open_parens -= 1,// ',' => {// if open_parens == 0 {// split_points.push(index)// }// }// _ => (),// }// }// })