Skip to main content

Preprocessor

Struct Preprocessor 

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

Translation phase 4 over one file.

Holds the macro table and the conditional stack, so a single instance processes a whole translation unit and the definitions a header makes are visible after it.

Implementations§

Source§

impl Preprocessor

Source

pub fn new() -> Preprocessor

A preprocessor with an empty macro table.

Source

pub fn with_prefix_map(map: PrefixMap) -> Preprocessor

The same, with __FILE__ and __BASE_FILE__ rewritten by map.

Handed in at construction rather than set afterwards because it is fixed for the whole translation unit: the command line cannot change its mind halfway through a file, and a __FILE__ that answered differently at the top of a header than at the bottom would be a worse bug than not having the flag.

Source

pub fn macros(&self) -> &MacroTable

The macros defined so far.

Source

pub fn macros_mut(&mut self) -> &mut MacroTable

The macro table, for the driver to seed with -D and the predefined set.

Source

pub fn diagnostics(&self) -> &[Diagnostic]

Everything reported so far.

Source

pub fn take_diagnostics(&mut self) -> Vec<Diagnostic>

Takes the diagnostics, leaving the preprocessor able to carry on.

Source

pub fn dependencies(&self) -> &[Dependency]

Every file an #include found, in the order they were first reached.

What the -M family writes into a make rule. The source file itself is not in here, since nothing included it, and the caller that knows its name puts it first.

Source

pub fn line_directives(&self) -> &[LineDirective]

The #line directives seen, in the order they appeared.

Each one is also applied, to the source map, as it is read. This is the record of them rather than the mechanism: what a caller wants it for is reporting on the directives themselves, and asking the map is how to find out where anything is.

Source

pub fn predefine( &mut self, target: &TargetInfo, opts: &Predef, cx: &mut Context<'_>, ) -> Result<(), SourceMapFull>

Defines the predefined macro set, and then -D and -U from the command line.

Called before Preprocessor::run, because a predefined macro is a macro like any other by the time the source file is read. The set arrives as two synthetic files rather than as a list of definitions, so a diagnostic about one of them points at <built-in> or <command-line> the way GCC’s does, and so that -dM has something to print. The reasoning is in crate::predef.

§Errors

When the source map has no room left for the two synthetic files.

Source

pub fn preinclude( &mut self, files: &[Preinclude], out: &mut Vec<Tok>, cx: &mut Context<'_>, ) -> Result<(), SourceMapFull>

Reads the files -imacros and -include named, before the source file is opened.

Called between Preprocessor::predefine and Preprocessor::run, with the tokens the -include files produce going in front of the ones the source file produces. That is what the flags mean: the definitions arrive before the first line of the source, so a header the source has no #include for is nevertheless in scope throughout it.

Every -imacros file is read before every -include file, whatever order the command line wrote them in, which is GCC’s behaviour and is measured rather than read: two command lines with the two flags the other way round produce the same output byte for byte. The text an -imacros file produces is thrown away and only its definitions are kept, which is the whole difference between the two flags.

Each name is looked for the way a quoted include is looked for, starting from the working directory rather than from the directory of the source file. A source in sub/ and a -include of a header sitting beside it is an error, not a file found, because the command line is not written in sub/.

§Errors

When the source map has no room left for the record of the flags.

Source

pub fn run(&mut self, file: FileId, cx: &mut Context<'_>) -> Vec<Tok>

Runs phase 4 over file and everything it includes.

The result is the tokens that survived the conditionals, with macros expanded. Nothing is thrown away silently: an unterminated #if and a stray #endif are both reported.

Trait Implementations§

Source§

impl Debug for Preprocessor

Source§

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

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

impl Default for Preprocessor

Source§

fn default() -> Self

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.