Skip to content

Commit 6ee4d3c

Browse files
author
Matthew Russo
committed
new_source_file no longer enters duplicate files, expand_include_bytes includes the source if it can convert bytes to string
1 parent 906deae commit 6ee4d3c

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/libsyntax/ext/source_util.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,12 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::Toke
182182
DummyResult::expr(sp)
183183
}
184184
Ok(..) => {
185-
// Add this input file to the code map to make it available as
186-
// dependency information, but don't enter it's contents
187-
cx.source_map().new_source_file(file.into(), String::new());
185+
let src = match String::from_utf8(bytes.clone()) {
186+
Ok(contents) => contents,
187+
Err(..) => "".to_string()
188+
};
189+
190+
cx.source_map().new_source_file(file.into(), src);
188191

189192
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes))))
190193
}

src/libsyntax/source_map.rs

+34-16
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,19 @@ pub struct StableSourceFileId(u128);
110110

111111
impl StableSourceFileId {
112112
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
113+
StableFilemapId::new_from_pieces(&source_file.name,
114+
source_file.name_was_remapped,
115+
source_file.unmapped_path.as_ref())
116+
}
117+
118+
pub fn new_from_pieces(name: &FileName,
119+
name_was_remapped: bool,
120+
unmapped_path: Option<&FileName>) -> StableFilemapId {
113121
let mut hasher = StableHasher::new();
114122

115-
source_file.name.hash(&mut hasher);
116-
source_file.name_was_remapped.hash(&mut hasher);
117-
source_file.unmapped_path.hash(&mut hasher);
123+
name.hash(&mut hasher);
124+
name_was_remapped.hash(&mut hasher);
125+
unmapped_path.hash(&mut hasher);
118126

119127
StableSourceFileId(hasher.finish())
120128
}
@@ -208,7 +216,8 @@ impl SourceMap {
208216
}
209217

210218
/// Creates a new source_file.
211-
/// This does not ensure that only one SourceFile exists per file name.
219+
/// If a file already exists in the source_map with the same id, that file is returned
220+
/// unmodified
212221
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
213222
let start_pos = self.next_start_pos();
214223

@@ -226,21 +235,30 @@ impl SourceMap {
226235
},
227236
other => (other, false),
228237
};
229-
let source_file = Lrc::new(SourceFile::new(
230-
filename,
231-
was_remapped,
232-
unmapped_path,
233-
src,
234-
Pos::from_usize(start_pos),
235-
));
236238

237-
let mut files = self.files.borrow_mut();
239+
let file_id = StableFilemapId::new_from_pieces(&filename,
240+
was_remapped,
241+
Some(&unmapped_path));
238242

239-
files.source_files.push(source_file.clone());
240-
files.stable_id_to_source_file.insert(StableSourceFileId::new(&source_file),
241-
source_file.clone());
243+
return match self.source_file_by_stable_id(file_id) {
244+
Some(lrc_sf) => lrc_sf,
245+
None => {
246+
let source_file = Lrc::new(SourceFile::new(
247+
filename,
248+
was_remapped,
249+
unmapped_path,
250+
src,
251+
Pos::from_usize(start_pos),
252+
));
242253

243-
source_file
254+
let mut files = self.files.borrow_mut();
255+
256+
files.source_files.push(source_file.clone());
257+
files.stable_id_to_source_file.insert(file_id, source_file.clone());
258+
259+
source_file
260+
}
261+
}
244262
}
245263

246264
/// Allocates a new SourceFile representing a source file from an external

0 commit comments

Comments
 (0)