Skip to content

Commit 8b884ea

Browse files
authored
Don't escape objective-c method names (#2648)
* 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
1 parent 4f8709f commit 8b884ea

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@
186186
- Allow compiling `bindgen-cli` with a static libclang.
187187
- Emit an opaque integer type for pointer types that don't have the same size
188188
as the target's pointer size.
189+
- Avoid escaping Objective-C method names unless they are `Self`, `self`,
190+
`crate` or `super`.
189191
## Security
190192

191193
# 0.68.1

bindgen-tests/tests/expectations/tests/objc_escape.rs

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/headers/objc_escape.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@
55
-(void)f:(int)arg1 as:(int)arg2;
66
-(void)crate:(int)self;
77
@end
8+
9+
@interface B
10+
11+
@property(nonatomic, retain) id type;
12+
13+
@end

bindgen/ir/objc.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,24 @@ impl ObjCMethod {
253253
let split_name: Vec<Option<Ident>> = self
254254
.name
255255
.split(':')
256-
.map(|name| {
256+
.enumerate()
257+
.map(|(idx, name)| {
257258
if name.is_empty() {
258259
None
260+
} else if idx == 0 {
261+
// Try to parse the method name as an identifier. Having a keyword is ok
262+
// unless it is `crate`, `self`, `super` or `Self`, so we try to add the `_`
263+
// suffix to it and parse it.
264+
if ["crate", "self", "super", "Self"].contains(&name) {
265+
Some(Ident::new(
266+
&format!("{}_", name),
267+
Span::call_site(),
268+
))
269+
} else {
270+
Some(Ident::new(name, Span::call_site()))
271+
}
259272
} else {
260-
// Try to parse the current name as an identifier. This might fail if the name
273+
// Try to parse the current joining name as an identifier. This might fail if the name
261274
// is a keyword, so we try to "r#" to it and parse again, this could also fail
262275
// if the name is `crate`, `self`, `super` or `Self`, so we try to add the `_`
263276
// suffix to it and parse again. If this also fails, we panic with the first

0 commit comments

Comments
 (0)