From bac8bafa2573149b4f096eb81afbc4b084fc131a Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Wed, 3 Apr 2024 14:34:42 -0700 Subject: [PATCH] analyze: apply_rewrites: return new struct FileRewrite instead of a tuple --- c2rust-analyze/src/rewrite/apply.rs | 26 +++++++++++++++++++------- c2rust-analyze/src/rewrite/mod.rs | 6 +++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/c2rust-analyze/src/rewrite/apply.rs b/c2rust-analyze/src/rewrite/apply.rs index 24be2bc614..bd9d9d4ad9 100644 --- a/c2rust-analyze/src/rewrite/apply.rs +++ b/c2rust-analyze/src/rewrite/apply.rs @@ -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, +} + /// 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` 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)> { +) -> HashMap { let (rts, errs) = RewriteTree::build(rws); for (span, rw, err) in errs { eprintln!( @@ -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]; while !rts.is_empty() { let file = source_map.lookup_source_file(rts[0].span.lo()); @@ -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)] diff --git a/c2rust-analyze/src/rewrite/mod.rs b/c2rust-analyze/src/rewrite/mod.rs index 0ab8422c22..e6eb35ebcf 100644 --- a/c2rust-analyze/src/rewrite/mod.rs +++ b/c2rust-analyze/src/rewrite/mod.rs @@ -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.