Skip to content

Commit bb4a22f

Browse files
committed
do all the changes to BindgenState inside build
1 parent 13c87b3 commit bb4a22f

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

src/lib.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,29 +1512,8 @@ impl Builder {
15121512
}
15131513

15141514
/// Generate the Rust bindings using the options built up thus far.
1515-
pub fn generate(mut self) -> Result<Bindings, BindgenError> {
1516-
let mut state = BindgenState::build(&self.options);
1517-
state.parse_callbacks = self.parse_callbacks;
1518-
1519-
// Add any extra arguments from the environment to the clang command line.
1520-
state.clang_args.extend(get_extra_clang_args());
1521-
1522-
// Transform input headers to arguments on the clang command line.
1523-
state.input_header = self.options.input_headers.pop();
1524-
state.extra_input_headers =
1525-
std::mem::take(&mut self.options.input_headers);
1526-
state.clang_args.extend(
1527-
state
1528-
.extra_input_headers
1529-
.iter()
1530-
.flat_map(|header| ["-include".into(), header.to_string()]),
1531-
);
1532-
1533-
state.input_unsaved_files.extend(
1534-
self.options.input_header_contents.drain(..).map(
1535-
|(name, contents)| clang::UnsavedFile::new(&name, &contents),
1536-
),
1537-
);
1515+
pub fn generate(self) -> Result<Bindings, BindgenError> {
1516+
let state = BindgenState::build(&self.options, self.parse_callbacks);
15381517

15391518
Bindings::generate(state, self.options)
15401519
}
@@ -2272,7 +2251,37 @@ struct BindgenState {
22722251
impl ::std::panic::UnwindSafe for BindgenState {}
22732252

22742253
impl BindgenState {
2275-
fn build(options: &BindgenOptions) -> Self {
2254+
fn build(
2255+
options: &BindgenOptions,
2256+
parse_callbacks: Option<Box<dyn callbacks::ParseCallbacks>>,
2257+
) -> Self {
2258+
let mut clang_args = options.clang_args.clone();
2259+
2260+
// Add any extra arguments from the environment to the clang command line.
2261+
clang_args.extend(get_extra_clang_args());
2262+
2263+
// Transform input headers to arguments on the clang command line.
2264+
let (input_header, extra_input_headers) =
2265+
if let Some((input_header, extra_input_headers)) =
2266+
options.input_headers.split_last()
2267+
{
2268+
(Some(input_header.clone()), extra_input_headers.to_vec())
2269+
} else {
2270+
Default::default()
2271+
};
2272+
2273+
clang_args.extend(
2274+
extra_input_headers
2275+
.iter()
2276+
.flat_map(|header| ["-include".into(), header.to_string()]),
2277+
);
2278+
2279+
let input_unsaved_files = options
2280+
.input_header_contents
2281+
.iter()
2282+
.map(|(name, contents)| clang::UnsavedFile::new(&name, &contents))
2283+
.collect();
2284+
22762285
Self {
22772286
allowlisted_vars: RegexSet::new(
22782287
options.allowlisted_vars.clone(),
@@ -2374,11 +2383,11 @@ impl BindgenState {
23742383
options.must_use_types.clone(),
23752384
options.record_matches,
23762385
),
2377-
clang_args: options.clang_args.clone(),
2378-
input_header: Default::default(),
2379-
extra_input_headers: Default::default(),
2380-
input_unsaved_files: Default::default(),
2381-
parse_callbacks: Default::default(),
2386+
clang_args,
2387+
input_header,
2388+
extra_input_headers,
2389+
input_unsaved_files,
2390+
parse_callbacks,
23822391
}
23832392
}
23842393
}

0 commit comments

Comments
 (0)