diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-08-16 18:22:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-16 18:22:24 +0200 |
| commit | c83d3c32815c13d9fa2e881ed97b7163030cd8cc (patch) | |
| tree | b427373656728960e894f6ea9fca4d67a88785fb /src/libsyntax/source_map.rs | |
| parent | db3bae01708b41f7fd9bb75830cd9163b8b10e48 (diff) | |
| parent | 14bc998df9f15042342ac8e649a4adadf17a65f8 (diff) | |
| download | rust-c83d3c32815c13d9fa2e881ed97b7163030cd8cc.tar.gz rust-c83d3c32815c13d9fa2e881ed97b7163030cd8cc.zip | |
Rollup merge of #63525 - matklad:centraliza-file-loading, r=petrochenkov
Make sure that all file loading happens via SourceMap That way, callers don't need to repeat "let's add this to sm manually for tracking dependencies" trick. It should make it easier to switch to using `FileLoader` for binary files in the future as well cc #62948 r? @petrochenkov
Diffstat (limited to 'src/libsyntax/source_map.rs')
| -rw-r--r-- | src/libsyntax/source_map.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libsyntax/source_map.rs b/src/libsyntax/source_map.rs index 940687cb5d4..7190cfd72a9 100644 --- a/src/libsyntax/source_map.rs +++ b/src/libsyntax/source_map.rs @@ -171,6 +171,26 @@ impl SourceMap { Ok(self.new_source_file(filename, src)) } + /// Loads source file as a binary blob. + /// + /// Unlike `load_file`, guarantees that no normalization like BOM-removal + /// takes place. + pub fn load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> { + // Ideally, this should use `self.file_loader`, but it can't + // deal with binary files yet. + let bytes = fs::read(path)?; + + // We need to add file to the `SourceMap`, so that it is present + // in dep-info. There's also an edge case that file might be both + // loaded as a binary via `include_bytes!` and as proper `SourceFile` + // via `mod`, so we try to use real file contents and not just an + // empty string. + let text = std::str::from_utf8(&bytes).unwrap_or("") + .to_string(); + self.new_source_file(path.to_owned().into(), text); + Ok(bytes) + } + pub fn files(&self) -> MappedLockGuard<'_, Vec<Lrc<SourceFile>>> { LockGuard::map(self.files.borrow(), |files| &mut files.source_files) } |
