pub struct SourceMap { /* private fields */ }Expand description
Every file of one translation unit, laid end to end.
Implementations§
Source§impl SourceMap
impl SourceMap
Sourcepub fn add(
&mut self,
name: impl Into<String>,
bytes: impl AsRef<[u8]> + Send + Sync + 'static,
) -> Result<FileId, SourceMapFull>
pub fn add( &mut self, name: impl Into<String>, bytes: impl AsRef<[u8]> + Send + Sync + 'static, ) -> Result<FileId, SourceMapFull>
Adds a file named on the command line.
§Errors
SourceMapFull if the file does not fit in what is left of the coordinate space.
Adds a file whose contents are already shared.
This is the entry point the file system abstraction uses, because it hands out bytes it may also be holding in a cache.
§Errors
SourceMapFull if the file does not fit in what is left of the coordinate space.
Sourcepub fn add_included(
&mut self,
name: impl Into<String>,
bytes: impl AsRef<[u8]> + Send + Sync + 'static,
from: Span,
) -> Result<FileId, SourceMapFull>
pub fn add_included( &mut self, name: impl Into<String>, bytes: impl AsRef<[u8]> + Send + Sync + 'static, from: Span, ) -> Result<FileId, SourceMapFull>
Adds a file reached through the #include at from.
§Errors
SourceMapFull if the file does not fit in what is left of the coordinate space.
Sourcepub fn files(&self) -> &[SourceFile]
pub fn files(&self) -> &[SourceFile]
Every file, in the order they were added.
Sourcepub fn file(&self, id: FileId) -> &SourceFile
pub fn file(&self, id: FileId) -> &SourceFile
The file id names.
§Panics
Panics if id came from a different map. There is one map per compilation, on the
session, so this is a programming error rather than something a caller handles.
Sourcepub fn lookup_file(&self, pos: BytePos) -> Option<FileId>
pub fn lookup_file(&self, pos: BytePos) -> Option<FileId>
Which file pos is in.
Sourcepub fn presumed(&self, pos: BytePos) -> Option<PresumedLoc<'_>>
pub fn presumed(&self, pos: BytePos) -> Option<PresumedLoc<'_>>
Where pos is presented as being, which is SourceMap::lookup with the #line
directives in front of it applied.
This is the answer to give a user: it is what a diagnostic prints, what __FILE__ and
__LINE__ expand to and what a line marker says. SourceMap::lookup is the answer
to use when the bytes are wanted, which is reading the text of a line to draw a caret
under it.
Sourcepub fn presumed_after(&self, at: BytePos) -> Option<PresumedLoc<'_>>
pub fn presumed_after(&self, at: BytePos) -> Option<PresumedLoc<'_>>
Where the line after the one at is on is presented as being.
This is what a #line written at at did, asked after the fact. -E needs it to
write the marker the directive turns into, and asking it here rather than reading the
directive again is what keeps one answer about where anything is.
Sourcepub fn set_presumed(&mut self, at: BytePos, line: u32, name: Option<String>)
pub fn set_presumed(&mut self, at: BytePos, line: u32, name: Option<String>)
Records a #line written at at, which presents the line after it as line, and the
file as name when one was given.
The directive applies from the following line rather than from where it is written,
which is what makes #line 1000 followed by __LINE__ expand to 1000 and not 1001.
A #line with no name leaves the name alone, so the entry inherits whichever one is
already in force.
Nothing happens if at is in no file, or if the directive is the last line of one.
There is nothing after it for the entry to apply to in either case.
Sourcepub fn render_position(&self, pos: BytePos) -> String
pub fn render_position(&self, pos: BytePos) -> String
name:line:column for pos, or <unknown> for a position in no file.
This is the prefix of a rendered diagnostic and the form every editor already knows how to jump to.
Sourcepub fn include_stack(&self, pos: BytePos) -> Vec<Span>
pub fn include_stack(&self, pos: BytePos) -> Vec<Span>
The chain of #include directives that led to pos, innermost first.
Empty for a position in a file named on the command line. This is what the “in file included from” block of a diagnostic is printed from, and reading it out of the map rather than out of a stack the preprocessor keeps means it is still available long after preprocessing has finished.