Skip to content

Commit 89d625d

Browse files
committed
do all the changes to BindgenState inside build
1 parent a5d1fe7 commit 89d625d

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
@@ -1492,29 +1492,8 @@ impl Builder {
14921492
}
14931493

14941494
/// Generate the Rust bindings using the options built up thus far.
1495-
pub fn generate(mut self) -> Result<Bindings, BindgenError> {
1496-
let mut state = BindgenState::build(&self.options);
1497-
state.parse_callbacks = self.parse_callbacks;
1498-
1499-
// Add any extra arguments from the environment to the clang command line.
1500-
state.clang_args.extend(get_extra_clang_args());
1501-
1502-
// Transform input headers to arguments on the clang command line.
1503-
state.input_header = self.options.input_headers.pop();
1504-
state.extra_input_headers =
1505-
std::mem::take(&mut self.options.input_headers);
1506-
state.clang_args.extend(
1507-
state
1508-
.extra_input_headers
1509-
.iter()
1510-
.flat_map(|header| ["-include".into(), header.to_string()]),
1511-
);
1512-
1513-
state.input_unsaved_files.extend(
1514-
self.options.input_header_contents.drain(..).map(
1515-
|(name, contents)| clang::UnsavedFile::new(&name, &contents),
1516-
),
1517-
);
1495+
pub fn generate(self) -> Result<Bindings, BindgenError> {
1496+
let state = BindgenState::build(&self.options, self.parse_callbacks);
15181497

15191498
Bindings::generate(state, self.options)
15201499
}
@@ -2240,7 +2219,37 @@ struct BindgenState {
22402219
impl ::std::panic::UnwindSafe for BindgenState {}
22412220

22422221
impl BindgenState {
2243-
fn build(options: &BindgenOptions) -> Self {
2222+
fn build(
2223+
options: &BindgenOptions,
2224+
parse_callbacks: Option<Box<dyn callbacks::ParseCallbacks>>,
2225+
) -> Self {
2226+
let mut clang_args = options.clang_args.clone();
2227+
2228+
// Add any extra arguments from the environment to the clang command line.
2229+
clang_args.extend(get_extra_clang_args());
2230+
2231+
// Transform input headers to arguments on the clang command line.
2232+
let (input_header, extra_input_headers) =
2233+
if let Some((input_header, extra_input_headers)) =
2234+
options.input_headers.split_last()
2235+
{
2236+
(Some(input_header.clone()), extra_input_headers.to_vec())
2237+
} else {
2238+
Default::default()
2239+
};
2240+
2241+
clang_args.extend(
2242+
extra_input_headers
2243+
.iter()
2244+
.flat_map(|header| ["-include".into(), header.to_string()]),
2245+
);
2246+
2247+
let input_unsaved_files = options
2248+
.input_header_contents
2249+
.iter()
2250+
.map(|(name, contents)| clang::UnsavedFile::new(&name, &contents))
2251+
.collect();
2252+
22442253
Self {
22452254
allowlisted_vars: RegexSet::new(
22462255
options.allowlisted_vars.clone(),
@@ -2338,11 +2347,11 @@ impl BindgenState {
23382347
options.must_use_types.clone(),
23392348
options.record_matches,
23402349
),
2341-
clang_args: options.clang_args.clone(),
2342-
input_header: Default::default(),
2343-
extra_input_headers: Default::default(),
2344-
input_unsaved_files: Default::default(),
2345-
parse_callbacks: Default::default(),
2350+
clang_args,
2351+
input_header,
2352+
extra_input_headers,
2353+
input_unsaved_files,
2354+
parse_callbacks,
23462355
}
23472356
}
23482357
}

0 commit comments

Comments
 (0)