forked from rust-lang/rust-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Denote types with unused template params.
Downstream postprocessors such as autocxx may want to use bindgen's representation of types to generate additional C++ code. bindgen is remarkably faithful at passing through enough information to make this possible - but for some C++ types, bindgen chooses to elide some template parameters. It's not safe for additional C++ code to be generated which references that type. This adds a callback by which such tools can recognize types where template parameters have been elided like this, so that extra C++ is avoided. This information could be provided in the existing `new_item_found` callback, but it's very niche and unlikely to be used by the majority of consumers, so a new callback is added instead. An alternative approach here is to provide a mode to bindgen where it *always* uses all template params, by adding additional PhantomData fields to structs where those params are not currently used. This is being prototyped in google/autocxx#1425 but is unlikely to be successful, on the assumption that lots of the templated types can't actually be properly represented by bindgen/Rust, so the current strategy of discarding them is more likely to work in the broad strokes. Part of google/autocxx#124
- Loading branch information
Showing
6 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
bindgen-tests/tests/parse_callbacks/unused_template_param.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
class NoTemplateParams { | ||
public: | ||
int a; | ||
}; | ||
|
||
template<class T> | ||
class UsesTemplateParam { | ||
public: | ||
UsesTemplateParam(T); | ||
T t; | ||
}; | ||
|
||
template<class A> | ||
class IgnoresTemplateParam { | ||
public: | ||
IgnoresTemplateParam(); | ||
int a; | ||
}; | ||
|
||
typedef NoTemplateParams TypedefNoTemplateParam; | ||
using TypedefNoTemplateParam2 = NoTemplateParams; | ||
typedef UsesTemplateParam<int> TypedefUsesTemplateParam; | ||
using TypedefUsesTemplateParam2 = UsesTemplateParam<int>; | ||
template <class T> | ||
using TypedefUsesTemplateParam3 = UsesTemplateParam<T>; | ||
typedef IgnoresTemplateParam<int> TypedefIgnoresTemplateParam; | ||
template <class T> | ||
using TypedefIgnoresTemplateParam2 = IgnoresTemplateParam<T>; |
79 changes: 79 additions & 0 deletions
79
bindgen-tests/tests/parse_callbacks/unused_template_param.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::{ | ||
cell::RefCell, | ||
collections::{HashMap, HashSet}, | ||
rc::Rc, | ||
}; | ||
|
||
use bindgen::{ | ||
callbacks::{DiscoveredItem, DiscoveredItemId, ParseCallbacks}, | ||
Builder, | ||
}; | ||
|
||
#[derive(Debug)] | ||
struct UnusedTemplateParamCallback( | ||
Rc<RefCell<HashSet<DiscoveredItemId>>>, | ||
Rc<RefCell<HashMap<DiscoveredItemId, String>>>, | ||
); | ||
|
||
impl ParseCallbacks for UnusedTemplateParamCallback { | ||
fn denote_discards_template_param(&self, id: DiscoveredItemId) { | ||
self.0.borrow_mut().insert(id); | ||
} | ||
|
||
fn new_item_found( | ||
&self, | ||
id: DiscoveredItemId, | ||
item: DiscoveredItem, | ||
_source_location: Option<&bindgen::callbacks::SourceLocation>, | ||
_parent: Option<DiscoveredItemId>, | ||
) { | ||
match item { | ||
DiscoveredItem::Struct { | ||
final_name: name, .. | ||
} | | ||
DiscoveredItem::Alias { | ||
alias_name: name, .. | ||
} => { | ||
self.1.borrow_mut().insert(id, name); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
/// This test could be combined with the `item_discovery_callback` | ||
/// test, but as this is a separate callback we'll keep the tests | ||
/// separate too. | ||
#[test] | ||
fn test_unused_template_param() { | ||
let discovered = Rc::new(RefCell::new(HashSet::new())); | ||
let names = Rc::new(RefCell::new(HashMap::new())); | ||
let header_path = concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/tests/parse_callbacks/unused_template_param.hpp" | ||
); | ||
Builder::default() | ||
.header(header_path) | ||
.parse_callbacks(Box::new(UnusedTemplateParamCallback( | ||
Rc::clone(&discovered), | ||
Rc::clone(&names), | ||
))) | ||
.clang_arg("--std=c++11") | ||
.generate() | ||
.expect("TODO: panic message"); | ||
|
||
let reported_unused_template_parameter_item_names: HashSet<_> = discovered | ||
.borrow() | ||
.iter() | ||
.map(|id| names.borrow().get(id).expect("Name not reported").clone()) | ||
.collect(); | ||
|
||
assert_eq!( | ||
reported_unused_template_parameter_item_names, | ||
HashSet::from([ | ||
"IgnoresTemplateParam".to_string(), | ||
"TypedefIgnoresTemplateParam2".to_string() | ||
]), | ||
"Different items than expected reported unused template params" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters