Skip to content

Commit

Permalink
analyze: apply_rewrites: return new struct FileRewrite instead of a t…
Browse files Browse the repository at this point in the history
…uple
  • Loading branch information
spernsteiner committed Apr 3, 2024
1 parent a3fbdb5 commit bac8baf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
26 changes: 19 additions & 7 deletions c2rust-analyze/src/rewrite/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,20 @@ impl LineMapBuilder {
}
}

pub struct FileRewrite {
/// The rewritten source code for this file.
pub new_src: String,
/// For each input line in the original source code, this gives the line number within
/// `new_src` of the first output line that contains some part of the input line.
pub line_map: Vec<usize>,
}

/// Apply rewrites `rws` to the source files covered by their `Span`s. Returns a map giving the
/// rewritten source code for each file that contains at least one rewritten `Span`. The
/// `Vec<usize>` is a line map: for each line of the original source code, it gives the line number
/// in the output `String` of the first output line that contains some part of the input line.
/// rewritten source code for each file that contains at least one rewritten `Span`.
pub fn apply_rewrites(
source_map: &SourceMap,
rws: Vec<(Span, Rewrite)>,
) -> HashMap<FileName, (String, Vec<usize>)> {
) -> HashMap<FileName, FileRewrite> {
let (rts, errs) = RewriteTree::build(rws);
for (span, rw, err) in errs {
eprintln!(
Expand All @@ -643,7 +649,7 @@ pub fn apply_rewrites(
);
}

let mut new_src = HashMap::new();
let mut file_rewrites = HashMap::new();
let mut rts = &rts as &[RewriteTree<Span>];
while !rts.is_empty() {
let file = source_map.lookup_source_file(rts[0].span.lo());
Expand Down Expand Up @@ -677,10 +683,16 @@ pub fn apply_rewrites(
let file_span = Span::new(file.start_pos, file.end_pos, SyntaxContext::root(), None);
sink.emit_span_with_rewrites(file_span, file_rts).unwrap();

new_src.insert(file.name.clone(), (buf, line_map.finish()));
file_rewrites.insert(
file.name.clone(),
FileRewrite {
new_src: buf,
line_map: line_map.finish(),
},
);
}

new_src
file_rewrites
}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions c2rust-analyze/src/rewrite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ pub fn apply_rewrites(
};

let new_src = apply::apply_rewrites(tcx.sess.source_map(), rewrites);
for (filename, (src, line_map)) in new_src {
for (filename, file_rw) in new_src {
let annotations = annotations.remove(&filename).unwrap_or(Vec::new());
let src = add_annotations(src, Some(&line_map), annotations);
emit(filename, src);
let new_src = add_annotations(file_rw.new_src, Some(&file_rw.line_map), annotations);
emit(filename, new_src);
}

// Also emit files that have annotations but no rewrites.
Expand Down

0 comments on commit bac8baf

Please sign in to comment.