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

Don't escape objective-c method names #2648

Merged
merged 5 commits into from
Sep 27, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@
- Allow compiling `bindgen-cli` with a static libclang.
- Emit an opaque integer type for pointer types that don't have the same size
as the target's pointer size.
- Avoid escaping Objective-C method names unless they are `Self`, `self`,
`crate` or `super`.
## Security

# 0.68.1
Expand Down
30 changes: 30 additions & 0 deletions bindgen-tests/tests/expectations/tests/objc_escape.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions bindgen-tests/tests/headers/objc_escape.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
-(void)f:(int)arg1 as:(int)arg2;
-(void)crate:(int)self;
@end

@interface B

@property(nonatomic, retain) id type;

@end
17 changes: 15 additions & 2 deletions bindgen/ir/objc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,24 @@ impl ObjCMethod {
let split_name: Vec<Option<Ident>> = self
.name
.split(':')
.map(|name| {
.enumerate()
.map(|(idx, name)| {
if name.is_empty() {
None
} else if idx == 0 {
// Try to parse the method name as an identifier. Having a keyword is ok
// unless it is `crate`, `self`, `super` or `Self`, so we try to add the `_`
// suffix to it and parse it.
if ["crate", "self", "super", "Self"].contains(&name) {
Some(Ident::new(
&format!("{}_", name),
Span::call_site(),
))
} else {
Some(Ident::new(name, Span::call_site()))
}
} else {
// Try to parse the current name as an identifier. This might fail if the name
// Try to parse the current joining name as an identifier. This might fail if the name
// is a keyword, so we try to "r#" to it and parse again, this could also fail
// if the name is `crate`, `self`, `super` or `Self`, so we try to add the `_`
// suffix to it and parse again. If this also fails, we panic with the first
Expand Down
Loading