new_source_file no longer enters duplicate files, expand_include_bytes includes the source if it can convert bytes to string

This commit is contained in:
Matthew Russo 2018-10-30 10:10:42 -04:00
parent 906deae079
commit 6ee4d3cafc
2 changed files with 40 additions and 19 deletions

View File

@ -182,9 +182,12 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::Toke
DummyResult::expr(sp) DummyResult::expr(sp)
} }
Ok(..) => { Ok(..) => {
// Add this input file to the code map to make it available as let src = match String::from_utf8(bytes.clone()) {
// dependency information, but don't enter it's contents Ok(contents) => contents,
cx.source_map().new_source_file(file.into(), String::new()); Err(..) => "".to_string()
};
cx.source_map().new_source_file(file.into(), src);
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes)))) base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes))))
} }

View File

@ -110,11 +110,19 @@ pub struct StableSourceFileId(u128);
impl StableSourceFileId { impl StableSourceFileId {
pub fn new(source_file: &SourceFile) -> StableSourceFileId { pub fn new(source_file: &SourceFile) -> StableSourceFileId {
StableFilemapId::new_from_pieces(&source_file.name,
source_file.name_was_remapped,
source_file.unmapped_path.as_ref())
}
pub fn new_from_pieces(name: &FileName,
name_was_remapped: bool,
unmapped_path: Option<&FileName>) -> StableFilemapId {
let mut hasher = StableHasher::new(); let mut hasher = StableHasher::new();
source_file.name.hash(&mut hasher); name.hash(&mut hasher);
source_file.name_was_remapped.hash(&mut hasher); name_was_remapped.hash(&mut hasher);
source_file.unmapped_path.hash(&mut hasher); unmapped_path.hash(&mut hasher);
StableSourceFileId(hasher.finish()) StableSourceFileId(hasher.finish())
} }
@ -208,7 +216,8 @@ impl SourceMap {
} }
/// Creates a new source_file. /// Creates a new source_file.
/// This does not ensure that only one SourceFile exists per file name. /// If a file already exists in the source_map with the same id, that file is returned
/// unmodified
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> { pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
let start_pos = self.next_start_pos(); let start_pos = self.next_start_pos();
@ -226,6 +235,14 @@ impl SourceMap {
}, },
other => (other, false), other => (other, false),
}; };
let file_id = StableFilemapId::new_from_pieces(&filename,
was_remapped,
Some(&unmapped_path));
return match self.source_file_by_stable_id(file_id) {
Some(lrc_sf) => lrc_sf,
None => {
let source_file = Lrc::new(SourceFile::new( let source_file = Lrc::new(SourceFile::new(
filename, filename,
was_remapped, was_remapped,
@ -237,11 +254,12 @@ impl SourceMap {
let mut files = self.files.borrow_mut(); let mut files = self.files.borrow_mut();
files.source_files.push(source_file.clone()); files.source_files.push(source_file.clone());
files.stable_id_to_source_file.insert(StableSourceFileId::new(&source_file), files.stable_id_to_source_file.insert(file_id, source_file.clone());
source_file.clone());
source_file source_file
} }
}
}
/// Allocates a new SourceFile representing a source file from an external /// Allocates a new SourceFile representing a source file from an external
/// crate. The source code of such an "imported source_file" is not available, /// crate. The source code of such an "imported source_file" is not available,