Skip to content

Commit

Permalink
Add a setting to ignore types from the std namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ergrelet committed Mar 22, 2024
1 parent f35ae68 commit 6d614fb
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 37 deletions.
7 changes: 7 additions & 0 deletions resym/src/resym_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,15 @@ impl ResymApp {
search_query.to_string(),
self.settings.app_settings.search_case_insensitive,
self.settings.app_settings.search_use_regex,
self.settings.app_settings.ignore_std_types,
))
} else {
self.backend.send_command(BackendCommand::ListTypes(
ResymPDBSlots::Main as usize,
search_query.to_string(),
self.settings.app_settings.search_case_insensitive,
self.settings.app_settings.search_use_regex,
self.settings.app_settings.ignore_std_types,
))
};
if let Err(err) = result {
Expand Down Expand Up @@ -263,6 +265,7 @@ impl ResymApp {
self.settings.app_settings.print_header,
self.settings.app_settings.reconstruct_dependencies,
self.settings.app_settings.print_access_specifiers,
self.settings.app_settings.ignore_std_types,
),
) {
log::error!("Failed to reconstruct type: {}", err);
Expand All @@ -278,6 +281,7 @@ impl ResymApp {
self.settings.app_settings.print_header,
self.settings.app_settings.reconstruct_dependencies,
self.settings.app_settings.print_access_specifiers,
self.settings.app_settings.ignore_std_types,
))
{
log::error!("Failed to reconstruct type diff: {}", err);
Expand Down Expand Up @@ -399,6 +403,7 @@ impl ResymApp {
self.settings.app_settings.print_header,
self.settings.app_settings.reconstruct_dependencies,
self.settings.app_settings.print_access_specifiers,
self.settings.app_settings.ignore_std_types,
),
) {
log::error!("Failed to reconstruct type: {}", err);
Expand Down Expand Up @@ -542,6 +547,7 @@ impl ResymApp {
String::default(),
false,
false,
self.settings.app_settings.ignore_std_types,
)) {
log::error!("Failed to update type filter value: {}", err);
}
Expand Down Expand Up @@ -580,6 +586,7 @@ impl ResymApp {
String::default(),
false,
false,
self.settings.app_settings.ignore_std_types,
))
{
log::error!("Failed to update type filter value: {}", err);
Expand Down
4 changes: 4 additions & 0 deletions resym/src/ui_components/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ impl SettingsComponent {
&mut self.app_settings.print_access_specifiers,
"Print access specifiers",
);
ui.checkbox(
&mut self.app_settings.ignore_std_types,
"Ignore types from the std namespace",
);
ui.checkbox(
&mut self.app_settings.print_line_numbers,
"Print line numbers",
Expand Down
85 changes: 62 additions & 23 deletions resym_core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum BackendCommand {
bool,
bool,
bool,
bool,
),
/// Reconstruct a type given its name for a given PDB.
ReconstructTypeByName(
Expand All @@ -64,14 +65,15 @@ pub enum BackendCommand {
bool,
bool,
bool,
bool,
),
/// Reconstruct all types found in a given PDB.
ReconstructAllTypes(PDBSlot, PrimitiveReconstructionFlavor, bool, bool),
ReconstructAllTypes(PDBSlot, PrimitiveReconstructionFlavor, bool, bool, bool),
/// Retrieve a list of types that match the given filter for a given PDB.
ListTypes(PDBSlot, String, bool, bool),
ListTypes(PDBSlot, String, bool, bool, bool),
/// Retrieve a list of types that match the given filter for multiple PDBs
/// and merge the result.
ListTypesMerged(Vec<PDBSlot>, String, bool, bool),
ListTypesMerged(Vec<PDBSlot>, String, bool, bool, bool),
/// Retrieve the list of all modules in a given PDB.
ListModules(PDBSlot, String, bool, bool),
/// Reconstruct a module given its index for a given PDB.
Expand All @@ -85,6 +87,7 @@ pub enum BackendCommand {
bool,
bool,
bool,
bool,
),
/// Reconstruct the diff of a module given its path.
DiffModuleByPath(
Expand Down Expand Up @@ -280,6 +283,7 @@ fn worker_thread_routine(
print_header,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
) => {
if let Some(pdb_file) = pdb_files.get(&pdb_slot) {
let reconstructed_type_result = reconstruct_type_by_index_command(
Expand All @@ -289,6 +293,7 @@ fn worker_thread_routine(
print_header,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
);
frontend_controller.send_command(FrontendCommand::ReconstructTypeResult(
reconstructed_type_result,
Expand All @@ -303,6 +308,7 @@ fn worker_thread_routine(
print_header,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
) => {
if let Some(pdb_file) = pdb_files.get(&pdb_slot) {
let reconstructed_type_result = reconstruct_type_by_name_command(
Expand All @@ -312,6 +318,7 @@ fn worker_thread_routine(
print_header,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
);
frontend_controller.send_command(FrontendCommand::ReconstructTypeResult(
reconstructed_type_result,
Expand All @@ -324,13 +331,15 @@ fn worker_thread_routine(
primitives_flavor,
print_header,
print_access_specifiers,
ignore_std_types,
) => {
if let Some(pdb_file) = pdb_files.get(&pdb_slot) {
let reconstructed_type_result = reconstruct_all_types_command(
pdb_file,
primitives_flavor,
print_header,
print_access_specifiers,
ignore_std_types,
);
frontend_controller.send_command(FrontendCommand::ReconstructTypeResult(
reconstructed_type_result,
Expand All @@ -343,13 +352,15 @@ fn worker_thread_routine(
search_filter,
case_insensitive_search,
use_regex,
ignore_std_types,
) => {
if let Some(pdb_file) = pdb_files.get(&pdb_slot) {
let filtered_type_list = update_type_filter_command(
pdb_file,
&search_filter,
case_insensitive_search,
use_regex,
ignore_std_types,
true,
);
frontend_controller
Expand All @@ -362,6 +373,7 @@ fn worker_thread_routine(
search_filter,
case_insensitive_search,
use_regex,
ignore_std_types,
) => {
let mut filtered_type_set = BTreeSet::default();
for pdb_slot in pdb_slots {
Expand All @@ -371,6 +383,7 @@ fn worker_thread_routine(
&search_filter,
case_insensitive_search,
use_regex,
ignore_std_types,
false,
);
filtered_type_set.extend(filtered_type_list.into_iter().map(|(s, _)| {
Expand All @@ -397,6 +410,7 @@ fn worker_thread_routine(
pdb_file,
module_index,
primitives_flavor,
false,
print_header,
);
frontend_controller.send_command(FrontendCommand::ReconstructModuleResult(
Expand Down Expand Up @@ -431,6 +445,7 @@ fn worker_thread_routine(
print_header,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
) => {
if let Some(pdb_file_from) = pdb_files.get(&pdb_from_slot) {
if let Some(pdb_file_to) = pdb_files.get(&pdb_to_slot) {
Expand All @@ -442,6 +457,7 @@ fn worker_thread_routine(
print_header,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
);
frontend_controller
.send_command(FrontendCommand::DiffResult(type_diff_result))?;
Expand Down Expand Up @@ -491,6 +507,7 @@ fn reconstruct_type_by_index_command<'p, T>(
print_header: bool,
reconstruct_dependencies: bool,
print_access_specifiers: bool,
ignore_std_types: bool,
) -> Result<String>
where
T: io::Seek + io::Read + std::fmt::Debug + 'p,
Expand All @@ -500,9 +517,10 @@ where
primitives_flavor,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
)?;
if print_header {
let file_header = generate_file_header(pdb_file, primitives_flavor, true);
let file_header = generate_file_header(pdb_file, primitives_flavor, true, ignore_std_types);
Ok(format!("{file_header}{data}"))
} else {
Ok(data)
Expand All @@ -516,6 +534,7 @@ fn reconstruct_type_by_name_command<'p, T>(
print_header: bool,
reconstruct_dependencies: bool,
print_access_specifiers: bool,
ignore_std_types: bool,
) -> Result<String>
where
T: io::Seek + io::Read + std::fmt::Debug + 'p,
Expand All @@ -525,9 +544,10 @@ where
primitives_flavor,
reconstruct_dependencies,
print_access_specifiers,
ignore_std_types,
)?;
if print_header {
let file_header = generate_file_header(pdb_file, primitives_flavor, true);
let file_header = generate_file_header(pdb_file, primitives_flavor, true, ignore_std_types);
Ok(format!("{file_header}{data}"))
} else {
Ok(data)
Expand All @@ -539,13 +559,18 @@ fn reconstruct_all_types_command<'p, T>(
primitives_flavor: PrimitiveReconstructionFlavor,
print_header: bool,
print_access_specifiers: bool,
ignore_std_types: bool,
) -> Result<String>
where
T: io::Seek + io::Read + std::fmt::Debug + 'p,
{
let data = pdb_file.reconstruct_all_types(primitives_flavor, print_access_specifiers)?;
let data = pdb_file.reconstruct_all_types(
primitives_flavor,
print_access_specifiers,
ignore_std_types,
)?;
if print_header {
let file_header = generate_file_header(pdb_file, primitives_flavor, true);
let file_header = generate_file_header(pdb_file, primitives_flavor, true, ignore_std_types);
Ok(format!("{file_header}{data}"))
} else {
Ok(data)
Expand All @@ -556,14 +581,15 @@ fn reconstruct_module_by_index_command<'p, T>(
pdb_file: &mut PdbFile<'p, T>,
module_index: usize,
primitives_flavor: PrimitiveReconstructionFlavor,
ignore_std_types: bool,
print_header: bool,
) -> Result<String>
where
T: io::Seek + io::Read + std::fmt::Debug + 'p,
{
let data = pdb_file.reconstruct_module_by_index(module_index, primitives_flavor)?;
if print_header {
let file_header = generate_file_header(pdb_file, primitives_flavor, true);
let file_header = generate_file_header(pdb_file, primitives_flavor, true, ignore_std_types);
Ok(format!("{file_header}\n{data}"))
} else {
Ok(data)
Expand All @@ -574,25 +600,29 @@ fn generate_file_header<T>(
pdb_file: &PdbFile<T>,
primitives_flavor: PrimitiveReconstructionFlavor,
include_header_files: bool,
ignore_std_types: bool,
) -> String
where
T: io::Seek + io::Read,
{
format!(
concat!(
"//\n",
"// Information extracted with resym v{}\n",
"//\n",
"// PDB file: {}\n",
"// Image architecture: {}\n",
"//\n",
"// Information extracted with resym v{}\n",
"//\n",
"{}"
),
PKG_VERSION,
pdb_file.file_path.display(),
pdb_file.machine_type,
PKG_VERSION,
if include_header_files {
format!("\n{}", include_headers_for_flavor(primitives_flavor))
format!(
"\n{}",
include_headers_for_flavor(primitives_flavor, ignore_std_types)
)
} else {
"".to_string()
}
Expand All @@ -604,28 +634,29 @@ fn update_type_filter_command<T>(
search_filter: &str,
case_insensitive_search: bool,
use_regex: bool,
ignore_std_types: bool,
sort_by_index: bool,
) -> Vec<(String, pdb::TypeIndex)>
where
T: io::Seek + io::Read,
{
let filter_start = Instant::now();

// Fitler out std types if needed
let filtered_type_list = if ignore_std_types {
filter_std_types(&pdb_file.complete_type_list)
} else {
pdb_file.complete_type_list.clone()
};

// Filter types following the search filter
let mut filtered_type_list = if search_filter.is_empty() {
// No need to filter
pdb_file.complete_type_list.clone()
filtered_type_list
} else if use_regex {
filter_types_regex(
&pdb_file.complete_type_list,
search_filter,
case_insensitive_search,
)
filter_types_regex(&filtered_type_list, search_filter, case_insensitive_search)
} else {
filter_types_regular(
&pdb_file.complete_type_list,
search_filter,
case_insensitive_search,
)
filter_types_regular(&filtered_type_list, search_filter, case_insensitive_search)
};
if sort_by_index {
// Order types by type index, so the order is deterministic
Expand Down Expand Up @@ -680,6 +711,14 @@ fn filter_types_regular(
}
}

/// Filter type list to remove types in the `std` namespace
fn filter_std_types(type_list: &[(String, pdb::TypeIndex)]) -> Vec<(String, pdb::TypeIndex)> {
par_iter_if_available!(type_list)
.filter(|r| !r.0.starts_with("std::"))
.cloned()
.collect()
}

fn list_modules_command<'p, T>(
pdb_file: &PdbFile<'p, T>,
search_filter: &str,
Expand Down
Loading

0 comments on commit 6d614fb

Please sign in to comment.