Skip to content

Commit

Permalink
Don't escape objective-c method names (#2648)
Browse files Browse the repository at this point in the history
* Don't escape objective-c method names

* Still escape `crate`, `self`, and the like

* Don't escape keywords if they are method names

* Update tests

* Update CHANGELOG.md
  • Loading branch information
pvdrz authored Sep 27, 2023
1 parent 4f8709f commit 8b884ea
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
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

0 comments on commit 8b884ea

Please sign in to comment.