Skip to main content

SourceMap

Struct SourceMap 

Source
pub struct SourceMap { /* private fields */ }
Expand description

Every file of one translation unit, laid end to end.

Implementations§

Source§

impl SourceMap

Source

pub fn new() -> SourceMap

An empty map.

Source

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.

Source

pub fn add_shared( &mut self, name: impl Into<String>, bytes: SourceBytes, included_from: Option<Span>, ) -> Result<FileId, SourceMapFull>

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.

Source

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.

Source

pub fn files(&self) -> &[SourceFile]

Every file, in the order they were added.

Source

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.

Source

pub fn lookup_file(&self, pos: BytePos) -> Option<FileId>

Which file pos is in.

Source

pub fn lookup(&self, pos: BytePos) -> Option<Loc>

The file, line and column of pos.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn used(&self) -> BytePos

How much of the coordinate space is used, which is where the next file will start.

Trait Implementations§

Source§

impl Debug for SourceMap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SourceMap

Source§

fn default() -> SourceMap

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.