pub struct TranslationUnit<'i> { /* private fields */ }
Expand description
A preprocessed and parsed source file.
Implementations§
Source§impl<'i> TranslationUnit<'i>
impl<'i> TranslationUnit<'i>
Sourcepub fn from_ast<F: AsRef<Path>>(
index: &'i Index<'_>,
file: F,
) -> Result<TranslationUnit<'i>, ()>
pub fn from_ast<F: AsRef<Path>>( index: &'i Index<'_>, file: F, ) -> Result<TranslationUnit<'i>, ()>
Sourcepub fn get_diagnostics(&'i self) -> Vec<Diagnostic<'i>>
pub fn get_diagnostics(&'i self) -> Vec<Diagnostic<'i>>
Returns the diagnostics for this translation unit.
Sourcepub fn get_entity(&'i self) -> Entity<'i>
pub fn get_entity(&'i self) -> Entity<'i>
Returns the entity for this translation unit.
Examples found in repository?
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}
Sourcepub fn get_file<F: AsRef<Path>>(&'i self, file: F) -> Option<File<'i>>
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.
Sourcepub fn get_memory_usage(&self) -> HashMap<MemoryUsage, usize>
pub fn get_memory_usage(&self) -> HashMap<MemoryUsage, usize>
Returns the memory usage of this translation unit.
Sourcepub fn get_skipped_ranges(&'i self) -> Vec<SourceRange<'i>>
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.
Sourcepub fn get_target(&self) -> Target
pub fn get_target(&self) -> Target
Returns information about the target for this translation unit.
Sourcepub fn annotate(&'i self, tokens: &[Token<'i>]) -> Vec<Option<Entity<'i>>>
pub fn annotate(&'i self, tokens: &[Token<'i>]) -> Vec<Option<Entity<'i>>>
Returns the AST entities which correspond to the supplied tokens, if any.
Sourcepub fn completer<F: Into<PathBuf>>(
&self,
file: F,
line: u32,
column: u32,
) -> Completer<'_>
pub fn completer<F: Into<PathBuf>>( &self, file: F, line: u32, column: u32, ) -> Completer<'_>
Returns a completer which runs code completion.
Sourcepub fn save<F: AsRef<Path>>(&self, file: F) -> Result<(), SaveError>
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
Sourcepub fn reparse(
self,
unsaved: &[Unsaved],
) -> Result<TranslationUnit<'i>, SourceError>
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