Skip to content

Commit

Permalink
feat: Support query fields_count from union type
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jun 14, 2024
1 parent 7de4937 commit 7e4e2ad
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ SELECT * FROM globals WHERE is_volatile

<summary>Unions table</summary>

| Name | Type | Description |
| ------ | ------- | ------------------------- |
| name | Text | Union name |
| size | Integer | The size of union in bits |
| file | Text | File path |
| line | Integer | Line at the file path |
| column | Integer | Column at the file path |
| offset | Integer | Offset at the file path |
| Name | Type | Description |
| ------------ | ------- | ----------------------------- |
| name | Text | Union name |
| size | Integer | The size of union in bits |
| fields_count | Integer | Number of fields declarations |
| file | Text | File path |
| line | Integer | Line at the file path |
| column | Integer | Column at the file path |
| offset | Integer | Offset at the file path |

</details>

Expand Down
5 changes: 5 additions & 0 deletions src/data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ fn select_unions(
continue;
}

if field_name == "fields_count" {
values.push(Value::Integer(union_node.attributes.fields_count.into()));
continue;
}

if field_name == "size" {
values.push(Value::Integer(union_node.size));
continue;
Expand Down
10 changes: 9 additions & 1 deletion src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ pub fn tables_fields_names() -> &'static HashMap<&'static str, Vec<&'static str>
);
map.insert(
"unions",
vec!["name", "size", "file", "line", "column", "offset"],
vec![
"name",
"fields_count",
"size",
"file",
"line",
"column",
"offset",
],
);
map.insert(
"functions",
Expand Down
33 changes: 33 additions & 0 deletions src/visitor/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ use std::ffi::CStr;
use std::ptr;

use crate::visitor::location;

pub struct UnionNode {
pub name: String,
pub attributes: UnionAttributes,
pub size: i64,
pub location: location::SourceLocation,
}

#[derive(Default)]
pub struct UnionAttributes {
pub fields_count: u32,
}

pub fn select_clang_unions(path: &str) -> Vec<UnionNode> {
let mut unions: Vec<UnionNode> = Vec::new();
let data = &mut unions as *mut Vec<UnionNode> as *mut c_void;
Expand Down Expand Up @@ -61,9 +68,14 @@ extern "C" fn visit_union_declaration(
let union_type = clang_getCursorType(cursor);
let size = clang_Type_getSizeOf(union_type);

let mut attributes = UnionAttributes::default();
let attributes_pointer = &mut attributes as *mut UnionAttributes as *mut c_void;
clang_visitChildren(cursor, visit_union_attributes, attributes_pointer);

let unions: &mut Vec<UnionNode> = &mut *(data as *mut Vec<UnionNode>);
unions.push(UnionNode {
name: union_name.to_string(),
attributes,
size,
location,
});
Expand All @@ -74,3 +86,24 @@ extern "C" fn visit_union_declaration(
}
CXChildVisit_Recurse
}

extern "C" fn visit_union_attributes(
cursor: CXCursor,
_parent: CXCursor,
data: *mut c_void,
) -> CXChildVisitResult {
unsafe {
if clang_Location_isFromMainFile(clang_getCursorLocation(cursor)) == 0 {
return CXChildVisit_Continue;
}

let cursor_kind = clang_getCursorKind(cursor);
if cursor_kind == CXCursor_FieldDecl {
let attributes = &mut *(data as *mut UnionAttributes);
attributes.fields_count += 1;
return CXChildVisit_Continue;
}
}

CXChildVisit_Continue
}

0 comments on commit 7e4e2ad

Please sign in to comment.