Balance parentheses in rustc command parsing

finchie
May 9, 2024, 7:35 AM
IU3B2YBIUC3YJYN7XV3UND6CONSUX25SYMZD24K4GUS65I3XCPVAC

Dependencies

  • [2] L6QJNN62 Parse rustc self-profile data using `analyzeme`

Change contents

  • edit in src/annotations/self_profile.rs at line 81
    [2.2520]
    [2.2520]
    }
    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
    [2.2876][2.2876:2928]()
    while let Some(chunk) = arg_chunks.next() {
    [2.2876]
    [2.2928]
    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 balanced
    let 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
    [2.3352][2.3352:3492]()
    panic!("Found multiple unexpected arguments, unable to parse filename. First: `{previous_filename}` Second: `{chunk}`")
    [2.3352]
    [2.3492]
    panic!("Found multiple unexpected arguments, unable to parse filename. First: `{previous_filename}` Second: `{chunk}`");
  • replacement in src/annotations/self_profile.rs at line 145
    [2.3641][2.3641:3701]()
    (name.strip_prefix(prefix).unwrap(), value)
    [2.3641]
    [2.3701]
    (name.strip_prefix(prefix).unwrap(), value.to_string())
  • replacement in src/annotations/self_profile.rs at line 151
    [2.3890][2.3890:3919]()
    (arg, value)
    [2.3890]
    [2.3919]
    (arg, Self::balance_parens(value, &mut arg_chunks))
  • edit in src/annotations/self_profile.rs at line 267
    [2.7972]
    [2.7972]
    // .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)
    // }
    // }
    // _ => (),
    // }
    // }
    // })