Skip to content

Commit

Permalink
Add Data::merge for merging signature data
Browse files Browse the repository at this point in the history
  • Loading branch information
emesare committed Oct 30, 2024
1 parent dc51fd5 commit 91b35e8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
107 changes: 107 additions & 0 deletions rust/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,43 @@ impl Data {
.map(Into::into)
}

pub fn merge(entries: &[Data]) -> Data {
let mut merged_data = Data::default();
merged_data
.types
.extend(entries.iter().flat_map(|e| &e.types).cloned());
// Now sort and remove types with the same guid.
merged_data
.types
.sort_unstable_by(|a, b| a.guid.cmp(&b.guid));
merged_data.types.dedup_by_key(|ty| ty.guid);
merged_data
.functions
.extend(entries.iter().flat_map(|e| &e.functions).cloned());
// Now sort and remove functions with the same symbol and guid.
merged_data
.functions
.sort_unstable_by(|a, b| a.symbol.name.cmp(&b.symbol.name));
merged_data.functions.dedup_by(|a, b| {
if a.guid == b.guid {
// Keep `a`s constraints.
b.constraints
.adjacent
.extend(a.constraints.adjacent.clone());
b.constraints
.call_sites
.extend(a.constraints.call_sites.clone());
b.constraints
.caller_sites
.extend(a.constraints.caller_sites.clone());
true
} else {
false
}
});
merged_data
}

pub fn to_bytes(&self) -> Vec<u8> {
let mut builder = FlatBufferBuilder::new();
let fb_data = self.create(&mut builder);
Expand Down Expand Up @@ -65,3 +102,73 @@ impl From<fb::Data<'_>> for Data {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::r#type::guid::TypeGUID;
use crate::r#type::ComputedType;
use crate::signature::function::{Function, FunctionGUID};
use crate::symbol::class::SymbolClass;
use crate::symbol::Symbol;
use uuid::{uuid, Uuid};

const FUNC1_GUID: Uuid = uuid!("6b50fa09-c8c5-4e88-b317-5a96c01c52ee");
const FUNC2_GUID: Uuid = uuid!("e0565a4e-d730-4073-916c-fa6cb8ad2407");
const FUNC3_GUID: Uuid = uuid!("5a7eb124-b786-4aa8-af2f-ccffbb600d21");

const TYPE1_GUID: Uuid = uuid!("7aee6520-0443-4a91-910e-da068571fa7a");
const TYPE2_GUID: Uuid = uuid!("9e8a58f0-757d-4fa6-8c41-a4da023c5a32");
const TYPE3_GUID: Uuid = uuid!("f81a46df-ad7b-4d7b-a4a7-23ed22ab01ec");

// Used with `test_merge` test.
fn create_sample_function<T: Into<FunctionGUID>>(name: &str, guid: T) -> Function {
Function {
symbol: Symbol {
name: name.to_string(),
modifiers: Default::default(),
class: SymbolClass::Function,
},
guid: guid.into(),
constraints: Default::default(),
ty: rand::random(),
entry: None,
}
}

// Used with `test_merge` test.
fn create_sample_computed_type<T: Into<TypeGUID>>(guid: T) -> ComputedType {
let mut comp_ty = ComputedType::new(rand::random());
comp_ty.guid = guid.into(); // Adjust the guid for testing.

Check warning on line 142 in rust/signature.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/warp/warp/rust/signature.rs
comp_ty
}

#[test]
fn test_merge() {
let first_data = Data::new(
vec![
create_sample_function("func1", FUNC1_GUID),
create_sample_function("func2", FUNC2_GUID),
],
vec![
create_sample_computed_type(TYPE1_GUID),
create_sample_computed_type(TYPE2_GUID),
],
);

let second_data = Data::new(
vec![
create_sample_function("func2", FUNC2_GUID),
create_sample_function("func3", FUNC3_GUID),
],
vec![
create_sample_computed_type(TYPE1_GUID),
create_sample_computed_type(TYPE3_GUID),
],
);

Check warning on line 168 in rust/signature.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/warp/warp/rust/signature.rs

let merged_data = Data::merge(&[first_data, second_data]);
assert_eq!(merged_data.functions.len(), 3, "{:#?}", merged_data.functions);
assert_eq!(merged_data.types.len(), 3, "{:#?}", merged_data.types);
}
}
2 changes: 1 addition & 1 deletion rust/signature/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod constraints;

pub const NAMESPACE_FUNCTION: Uuid = uuid!("0192a179-61ac-7cef-88ed-012296e9492f");

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct FunctionGUID {
pub guid: Uuid,
}
Expand Down
2 changes: 1 addition & 1 deletion rust/type/guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use uuid::{uuid, Uuid};

pub const NAMESPACE_TYPEBIN: Uuid = uuid!("01929b90-72e6-73e6-9da1-2b6462e407a6");

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct TypeGUID {
guid: Uuid,
}
Expand Down

0 comments on commit 91b35e8

Please sign in to comment.