Skip to content

Commit 8489d42

Browse files
author
bors-servo
authored
Auto merge of #1438 - ekse:item_name, r=emilio
Add item_name parse callback. Issue: #428
2 parents 2a07619 + 5b741da commit 8489d42

File tree

6 files changed

+47
-0
lines changed

6 files changed

+47
-0
lines changed

bindgen-integration/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ impl ParseCallbacks for MacroCallback {
2323

2424
MacroParsingBehavior::Default
2525
}
26+
27+
fn item_name(&self, original_item_name: &str) -> Option<String> {
28+
if original_item_name.starts_with("my_prefixed_") {
29+
Some(original_item_name.trim_start_matches("my_prefixed_").to_string())
30+
} else if original_item_name.starts_with("MY_PREFIXED_") {
31+
Some(original_item_name.trim_start_matches("MY_PREFIXED_").to_string())
32+
} else {
33+
None
34+
}
35+
}
2636
}
2737

2838
fn main() {

bindgen-integration/cpp/Test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ Seventh::assert(bool first,
131131
};
132132

133133
} // namespace bitfields
134+
135+
int my_prefixed_function_name() {
136+
return 4;
137+
}

bindgen-integration/cpp/Test.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,16 @@ struct AutoRestoreBool {
178178
struct WithWChar {
179179
wchar_t foo[30];
180180
};
181+
182+
// The names of the following items are unprefixed by the parse callbacks.
183+
const int MY_PREFIXED_CONST_VALUE = 3;
184+
185+
int my_prefixed_function_name();
186+
187+
struct my_prefixed_bar {
188+
int foo;
189+
};
190+
191+
struct my_prefixed_foo {
192+
my_prefixed_bar member;
193+
};

bindgen-integration/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,13 @@ fn test_virtual_dtor() {
282282
assert_eq!(bindings::VirtualDestructor_sDestructorCount, 1);
283283
}
284284
}
285+
286+
#[test]
287+
fn test_item_rename() {
288+
assert_eq!(bindings::CONST_VALUE, 3);
289+
assert_eq!(unsafe{ bindings::function_name() }, 4);
290+
291+
let _foo = bindings::foo{
292+
member: bindings::bar{foo: 2}
293+
};
294+
}

src/callbacks.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
5555
) -> Option<String> {
5656
None
5757
}
58+
59+
/// Allows to rename an item, replacing `_original_item_name`.
60+
fn item_name(&self, _original_item_name: &str) -> Option<String> {
61+
None
62+
}
5863
}

src/ir/item.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,11 @@ impl Item {
875875

876876
let name = names.join("_");
877877

878+
let name = ctx
879+
.parse_callbacks()
880+
.and_then(|callbacks| callbacks.item_name(&name))
881+
.unwrap_or(name);
882+
878883
ctx.rust_mangle(&name).into_owned()
879884
}
880885

0 commit comments

Comments
 (0)