Skip to content

Commit 00e30ef

Browse files
committed
do all the changes to BindgenState inside build
1 parent 0ffcdf5 commit 00e30ef

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

src/lib.rs

+38-29
Original file line numberDiff line numberDiff line change
@@ -1513,29 +1513,8 @@ impl Builder {
15131513
}
15141514

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

15401519
Bindings::generate(state, self.options)
15411520
}
@@ -2273,7 +2252,37 @@ struct BindgenState {
22732252
impl ::std::panic::UnwindSafe for BindgenState {}
22742253

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

0 commit comments

Comments
 (0)