Skip to content

Commit dd8f7cd

Browse files
committed
Moved FileMap construction to it's own constructor.
The rationale is that BOM stripping is needed for lazy source loading for external crates, and duplication can be avoided by moving the corresponding functionality to libsyntax_pos.
1 parent 3d2cff0 commit dd8f7cd

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

src/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libsyntax/codemap.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ use std::rc::Rc;
2727

2828
use std::env;
2929
use std::fs;
30-
use std::hash::Hasher;
3130
use std::io::{self, Read};
3231
use errors::CodeMapper;
3332

34-
use rustc_data_structures::stable_hasher::StableHasher;
35-
3633
/// Return the span itself if it doesn't come from a macro expansion,
3734
/// otherwise return the call site span up to the `enclosing_sp` by
3835
/// following the `expn_info` chain.
@@ -161,34 +158,13 @@ impl CodeMap {
161158

162159
/// Creates a new filemap without setting its line information. If you don't
163160
/// intend to set the line information yourself, you should use new_filemap_and_lines.
164-
pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> {
161+
pub fn new_filemap(&self, filename: FileName, src: String) -> Rc<FileMap> {
165162
let start_pos = self.next_start_pos();
166163
let mut files = self.files.borrow_mut();
167164

168-
// Remove utf-8 BOM if any.
169-
if src.starts_with("\u{feff}") {
170-
src.drain(..3);
171-
}
172-
173-
let end_pos = start_pos + src.len();
174-
175165
let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
176-
177-
let mut hasher: StableHasher<u128> = StableHasher::new();
178-
hasher.write(src.as_bytes());
179-
let src_hash = hasher.finish();
180-
181-
let filemap = Rc::new(FileMap {
182-
name: filename,
183-
name_was_remapped: was_remapped,
184-
crate_of_origin: 0,
185-
src: Some(Rc::new(src)),
186-
src_hash: src_hash,
187-
start_pos: Pos::from_usize(start_pos),
188-
end_pos: Pos::from_usize(end_pos),
189-
lines: RefCell::new(Vec::new()),
190-
multibyte_chars: RefCell::new(Vec::new()),
191-
});
166+
let filemap =
167+
Rc::new(FileMap::new(filename, was_remapped, src, Pos::from_usize(start_pos)));
192168

193169
files.push(filemap.clone());
194170

src/libsyntax_pos/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ crate-type = ["dylib"]
1010

1111
[dependencies]
1212
serialize = { path = "../libserialize" }
13+
rustc_data_structures = { path = "../librustc_data_structures" }

src/libsyntax_pos/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ use std::ops::{Add, Sub};
3838
use std::rc::Rc;
3939
use std::cmp;
4040
use std::fmt;
41+
use std::hash::Hasher;
42+
43+
use rustc_data_structures::stable_hasher::StableHasher;
44+
45+
extern crate rustc_data_structures;
4146

4247
use serialize::{Encodable, Decodable, Encoder, Decoder};
4348

@@ -522,6 +527,31 @@ impl fmt::Debug for FileMap {
522527
}
523528

524529
impl FileMap {
530+
pub fn new(name: FileName,
531+
name_was_remapped: bool,
532+
mut src: String,
533+
start_pos: BytePos) -> FileMap {
534+
remove_bom(&mut src);
535+
536+
let mut hasher: StableHasher<u128> = StableHasher::new();
537+
hasher.write(src.as_bytes());
538+
let src_hash = hasher.finish();
539+
540+
let end_pos = start_pos.to_usize() + src.len();
541+
542+
FileMap {
543+
name: name,
544+
name_was_remapped: name_was_remapped,
545+
crate_of_origin: 0,
546+
src: Some(Rc::new(src)),
547+
src_hash: src_hash,
548+
start_pos: start_pos,
549+
end_pos: Pos::from_usize(end_pos),
550+
lines: RefCell::new(Vec::new()),
551+
multibyte_chars: RefCell::new(Vec::new()),
552+
}
553+
}
554+
525555
/// EFFECT: register a start-of-line offset in the
526556
/// table of line-beginnings.
527557
/// UNCHECKED INVARIANT: these offsets must be added in the right
@@ -621,6 +651,13 @@ impl FileMap {
621651
}
622652
}
623653

654+
/// Remove utf-8 BOM if any.
655+
fn remove_bom(src: &mut String) {
656+
if src.starts_with("\u{feff}") {
657+
src.drain(..3);
658+
}
659+
}
660+
624661
// _____________________________________________________________________________
625662
// Pos, BytePos, CharPos
626663
//

0 commit comments

Comments
 (0)