Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve il2cpp line mapping parser #595

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 49 additions & 4 deletions symbolic-il2cpp/src/line_mapping/from_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl ObjectLineMapping {
}

/// An Il2cpp `source_info` record.
#[derive(Debug, PartialEq, Eq)]
struct SourceInfo<'data> {
/// The C++ source line the `source_info` was parsed from.
cpp_line: u32,
Expand Down Expand Up @@ -115,16 +116,19 @@ impl<'data> Iterator for SourceInfos<'data> {
type Item = SourceInfo<'data>;

fn next(&mut self) -> Option<Self::Item> {
for (cpp_line, cpp_src_line) in &mut self.lines {
while let Some((_, cpp_src_line)) = self.lines.next() {
match parse_line(cpp_src_line) {
None => continue,
Some((cs_file, cs_line)) => {
let next_non_comment_line_nr = self.lines.find_map(|(nr, src_line)| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is find_map defined as being "short circuiting"? It would be bad if it exhausted the whole iterator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it stops as soon as it finds the first Some return value. It's equivalent to filter_map(…).next().

(!src_line.trim_start().starts_with("//")).then(|| nr)
})?;
return Some(SourceInfo {
cpp_line: (cpp_line + 1) as u32,
cpp_line: (next_non_comment_line_nr + 1) as u32,
cs_file,
cs_line,
})
});
}
None => continue,
}
}
None
Expand All @@ -143,3 +147,44 @@ fn parse_line(line: &str) -> Option<(&str, u32)> {
let line = line.parse().ok()?;
Some((file, line))
}

#[cfg(test)]
mod tests {
use super::{SourceInfo, SourceInfos};

#[test]
fn simple() {
let cpp_source = b"
Lorem ipsum dolor sit amet
//<source_info:main.cs:17>
// some
// more
// comments
actual source code";

let source_infos: Vec<_> = SourceInfos::new(cpp_source).collect();

assert_eq!(
source_infos,
vec![SourceInfo {
cpp_line: 7,
cs_file: "main.cs",
cs_line: 17
}]
)
}

#[test]
fn broken() {
let cpp_source = b"
Lorem ipsum dolor sit amet
//<source_info:main.cs:17>
// some
// more
// comments";

// Since there is no non-comment line for the source info to attach to,
// no source infos should be returned.
assert_eq!(SourceInfos::new(cpp_source).count(), 0);
}
}