Skip to main content

structs/
structs.rs

1extern crate clang;
2
3use clang::*;
4
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
17        .get_entity()
18        .get_children()
19        .into_iter()
20        .filter(|e| e.get_kind() == EntityKind::StructDecl)
21        .collect::<Vec<_>>();
22
23    // Print information about the structs
24    for struct_ in structs {
25        let type_ = struct_.get_type().unwrap();
26        let size = type_.get_sizeof().unwrap();
27        println!(
28            "struct: {:?} (size: {} bytes)",
29            struct_.get_name().unwrap(),
30            size
31        );
32
33        for field in struct_.get_children() {
34            let name = field.get_name().unwrap();
35            let offset = type_.get_offsetof(&name).unwrap();
36            println!("    field: {:?} (offset: {} bits)", name, offset);
37        }
38    }
39}