TranslationUnit

Struct TranslationUnit 

Source
pub struct TranslationUnit<'i> { /* private fields */ }
Expand description

A preprocessed and parsed source file.

Implementations§

Source§

impl<'i> TranslationUnit<'i>

Source

pub fn from_ast<F: AsRef<Path>>( index: &'i Index<'_>, file: F, ) -> Result<TranslationUnit<'i>, ()>

Constructs a new TranslationUnit from an AST file.

§Failures
  • an unknown error occurs
Source

pub fn get_diagnostics(&'i self) -> Vec<Diagnostic<'i>>

Returns the diagnostics for this translation unit.

Source

pub fn get_entity(&'i self) -> Entity<'i>

Returns the entity for this translation unit.

Examples found in repository?
examples/structs.rs (line 16)
5fn main() {
6    // Acquire an instance of `Clang`
7    let clang = Clang::new().unwrap();
8
9    // Create a new `Index`
10    let index = Index::new(&clang, false, false);
11
12    // Parse a source file into a translation unit
13    let tu = index.parser("examples/structs.c").parse().unwrap();
14
15    // Get the structs in this translation unit
16    let structs = tu.get_entity().get_children().into_iter().filter(|e| {
17        e.get_kind() == EntityKind::StructDecl
18    }).collect::<Vec<_>>();
19
20    // Print information about the structs
21    for struct_ in structs {
22        let type_ =  struct_.get_type().unwrap();
23        let size = type_.get_sizeof().unwrap();
24        println!("struct: {:?} (size: {} bytes)", struct_.get_name().unwrap(), size);
25
26        for field in struct_.get_children() {
27            let name = field.get_name().unwrap();
28            let offset = type_.get_offsetof(&name).unwrap();
29            println!("    field: {:?} (offset: {} bits)", name, offset);
30        }
31    }
32}
Source

pub fn get_file<F: AsRef<Path>>(&'i self, file: F) -> Option<File<'i>>

Returns the file at the supplied path in this translation unit, if any.

Source

pub fn get_memory_usage(&self) -> HashMap<MemoryUsage, usize>

Returns the memory usage of this translation unit.

Source

pub fn get_skipped_ranges(&'i self) -> Vec<SourceRange<'i>>

Returns the source ranges in this translation unit that were skipped by the preprocessor.

This will always return an empty Vec if the translation unit was not constructed with a detailed preprocessing record.

Source

pub fn get_target(&self) -> Target

Returns information about the target for this translation unit.

Source

pub fn annotate(&'i self, tokens: &[Token<'i>]) -> Vec<Option<Entity<'i>>>

Returns the AST entities which correspond to the supplied tokens, if any.

Source

pub fn completer<F: Into<PathBuf>>( &self, file: F, line: u32, column: u32, ) -> Completer<'_>

Returns a completer which runs code completion.

Source

pub fn save<F: AsRef<Path>>(&self, file: F) -> Result<(), SaveError>

Saves this translation unit to an AST file.

§Failures
  • errors in the translation unit prevent saving
  • an unknown error occurs
Source

pub fn reparse( self, unsaved: &[Unsaved], ) -> Result<TranslationUnit<'i>, SourceError>

Consumes this translation unit and reparses the source file it was created from with the same compiler arguments that were used originally.

§Failures
  • an error occurs while deserializing an AST file
  • libclang crashes
  • an unknown error occurs

Trait Implementations§

Source§

impl<'i> Debug for TranslationUnit<'i>

Source§

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

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

impl<'i> Drop for TranslationUnit<'i>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'i> Freeze for TranslationUnit<'i>

§

impl<'i> RefUnwindSafe for TranslationUnit<'i>

§

impl<'i> !Send for TranslationUnit<'i>

§

impl<'i> !Sync for TranslationUnit<'i>

§

impl<'i> Unpin for TranslationUnit<'i>

§

impl<'i> UnwindSafe for TranslationUnit<'i>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.