Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect unused bindgen/riddle filters #2634

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions crates/libs/bindgen/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,4 @@ impl Error {
pub(crate) fn with_path(self, path: &str) -> Self {
Self { path: path.to_string(), ..self }
}

// pub(crate) fn with_span(self, span: proc_macro2::Span) -> Self {
// let start = span.start();
// Self {
// span: Some((start.line, start.column)),
// ..self
// }
// }
}
12 changes: 12 additions & 0 deletions crates/libs/bindgen/src/winmd/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ use super::*;
use metadata::RowReader;

pub fn verify(reader: &metadata::Reader, filter: &metadata::Filter) -> crate::Result<()> {
let unused: Vec<&str> = filter.unused(reader).collect();

if !unused.is_empty() {
let mut message = "unused filters".to_string();

for unused in unused {
message.push_str(&format!("\n {unused}"));
}

return Err(crate::Error::new(&message));
Copy link
Collaborator

@riverar riverar Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surfacing these as errors versus warnings may make third-party crate maintenance harder than it needs to be. I can see a scenario where folks will want to add a preemptive filter for, say, X.Y.Z.Preview namespaces when they appear/don't appear during a dev cycle. Or simply don't care if they over-specify the filter list.

No change needed right now though, we can take the "lets see what happens" approach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I do prefer the error because I'd like the filter list to reflect what goes into the generated bindings (no more, no less). But I accept that other people may have other preferences.

}

for item in reader.items(filter) {
// TODO: cover all variants
let metadata::Item::Type(def) = item else {
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/metadata/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ impl<'a> Filter<'a> {
fn is_empty(&self) -> bool {
self.0.is_empty()
}

pub fn unused(&self, reader: &'a Reader) -> impl Iterator<Item = &str> + '_ {
self.0.iter().filter_map(|(name, _)| if reader.unused(name) { Some(*name) } else { None })
}
}

fn match_type_name(rule: &str, namespace: &str, name: &str) -> bool {
Expand Down
23 changes: 23 additions & 0 deletions crates/libs/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ impl<'a> Reader<'a> {
self.items.get_key_value(namespace).into_iter().flat_map(move |(namespace, items)| items.iter().filter(move |(name, _)| filter.includes_type_name(TypeName::new(namespace, name)))).flat_map(move |(_, items)| items).cloned()
}

fn unused(&self, filter: &str) -> bool {
// Match namespaces
if self.items.contains_key(filter) {
return false;
}

// Match type names
if let Some((namespace, name)) = filter.rsplit_once('.') {
if self.items.get(namespace).is_some_and(|items| items.contains_key(name)) {
return false;
}
}

// Match empty parent namespaces
for namespace in self.items.keys() {
if namespace.len() > filter.len() && namespace.starts_with(filter) && namespace.as_bytes()[filter.len()] == b'.' {
return false;
}
}

true
}

fn get_item(&self, type_name: TypeName) -> impl Iterator<Item = Item> + '_ {
if let Some(items) = self.items.get(type_name.namespace) {
if let Some(items) = items.get(type_name.name) {
Expand Down
14 changes: 14 additions & 0 deletions crates/tests/metadata/tests/unused.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use metadata::*;

#[test]
fn test() {
let files = tool_lib::default_metadata();
let reader = &Reader::new(&files);
let filter = Filter::new(
&["Windows", "BadNamespace", "Windows.AI"],
&["Windows.Foundation.Rect", "Windows.Foundation.BadType"],
);
let unused: Vec<&str> = filter.unused(reader).collect();

assert_eq!(unused, ["Windows.Foundation.BadType", "BadNamespace"]);
}
3 changes: 3 additions & 0 deletions crates/tests/riddle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ pub fn run_riddle(name: &str, dialect: &str, etc: &[&str]) -> Vec<windows_metada
let before = std::fs::read_to_string(&rdl).expect("Failed to read input");

// Convert .rdl to .winmd
_ = std::fs::remove_file(&winmd);
let mut command = Command::new("cargo");
command.args([
"run", "-p", "riddle", "--", "--in", &rdl, "--out", &winmd, "--filter", "Test",
]);
assert!(command.status().unwrap().success());

// Convert .winmd back to .rdl
std::fs::remove_file(&rdl).expect("Failed to delete output");
let mut command = Command::new("cargo");
command.args([
"run", "-p", "riddle", "--", "--in", &winmd, "--out", &rdl, "--filter", "Test", "--config",
Expand All @@ -37,6 +39,7 @@ pub fn run_riddle(name: &str, dialect: &str, etc: &[&str]) -> Vec<windows_metada
assert_eq!(before, after, "no equal {}", rdl);

// Convert .rdl to .rs
std::fs::remove_file(&rs).expect("Failed to delete output");
let mut command = Command::new("cargo");
command.args([
"run", "-p", "riddle", "--", "--in", &rdl, "--out", &rs, "--filter", "Test",
Expand Down
2 changes: 2 additions & 0 deletions crates/tests/standalone/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ fn write_std(output: &str, filter: &[&str]) {
}

fn riddle(output: &str, filter: &[&str], config: &[&str]) {
std::fs::remove_file(output).expect("Failed to delete output");

let mut command = std::process::Command::new("cargo");

command.args([
Expand Down