Skip to content

Add --blocklist-file option #2097

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

Merged
merged 3 commits into from
Nov 26, 2021
Merged

Conversation

daviddrysdale
Copy link
Contributor

Update Item to hold a clang::SourceLocation and use this to allow
blocklisting based on filename.

The existing code has a special case that always maps <stdint.h> integer
types to corresponding Rust integer types, even if the C types are
blocklisted. To match this special case behaviour, also treat these
C <stdint.h> types as being eligible for derived Copy/Clone/Debug
traits.

Fixes #2096

@highfive
Copy link

highfive commented Sep 8, 2021

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@daviddrysdale daviddrysdale marked this pull request as ready for review September 30, 2021 12:52
@daviddrysdale
Copy link
Contributor Author

r? @emilio

(As far as I can tell the msrv CI failure is unrelated – other PRs hit it too)

@bors-servo
Copy link

☔ The latest upstream changes (presumably 9738fb9) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

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

Sorry for the massive lag here. So my main concern with this is that tracking the location of a type is a bit tricky in presence of forward-declarations.

Should a forward declaration of a type in a blocklisted file be treated as a usual forward declaration, or should it be blocked? Whatever behavior we want, it probably needs tests.

If we're associating a canonical location to every type it might make sense to look into allowlisting by file, which is also a fairly requested feature. But that can be a follow-up.

src/ir/item.rs Outdated
@@ -635,6 +644,15 @@ impl Item {
return true;
}

if let Some(location) = &self.location {
let (file, _, _, _) = location.location();
if let Some(filename) = file.name() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we avoid doing all the checks and so on if the user doesn't provide any blocklisted file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Update Item to hold a `clang::SourceLocation` and use this to allow
blocklisting based on filename.

The existing code has a special case that always maps <stdint.h> integer
types to corresponding Rust integer types, even if the C types are
blocklisted.  To match this special case behaviour, also treat these
C <stdint.h> types as being eligible for derived Copy/Clone/Debug
traits.

Fixes rust-lang#2096
 - struct with normal fwd decl and blocklisted definition ignored
 - struct with blocklisted fwd decl and normal definition emitted
@daviddrysdale
Copy link
Contributor Author

(First commit is just a rebase onto current master, then added two fixup commits to address comments)

Should a forward declaration of a type in a blocklisted file be treated as a usual forward declaration, or should it be blocked? Whatever behavior we want, it probably needs tests.

So I added a test and the current behaviour is that the location of a struct definition is what governs whether the blocklist applies or not – which I think is probably the right behaviour?

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

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

Thanks, I guess this implementation looks reasonable. We could add allowlisted_file in the future based on something similar to this. Not a fan of keeping around clang::SourceLocation objects in the bindgen IR, but I guess it's easier than creating our own.

// Sized integer types from <stdint.h> get mapped to Rust primitive
// types regardless of whether they are blocklisted, so ensure that
// standard traits are considered derivable for them too.
None => match name {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a huge fan of this since it might run very often, but I guess we can optimize it if / when the time comes.

@emilio emilio merged commit 7bd2329 into rust-lang:master Nov 26, 2021
@sfackler
Copy link
Member

I am working on adding a bindgen backend to openssl-sys, and I would very much appreciate allowlisted_file! OpenSSL's API is enormous and doesn't have a consistent naming prefix that can be allowlisted, so it'd be much easier to just .allowlist_file(".*/openssl/[^/]*.h") instead.

@daviddrysdale
Copy link
Contributor Author

OpenSSL's API is enormous and doesn't have a consistent naming prefix that can be allowlisted, so it'd be much easier to just .allowlist_file(".*/openssl/[^/]*.h") instead.

Ah, that's the other way round from how I was trying to do the same thing! The motivation for #2096 was to narrow down the output of @benbrittain's work auto-generating BoringSSL bindings – I was trying to .blocklist_file("/usr/include.*") instead.

@sfackler
Copy link
Member

I think that would have trouble with system headers installed outside of /usr/include and OpenSSL headers inside of /usr/include.

@daviddrysdale daviddrysdale deleted the blocklist-file branch November 29, 2021 12:48
@daviddrysdale
Copy link
Contributor Author

#2122 for --allowlist-file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: allow blocklisting by file
5 participants