Skip to content

Commit a8de128

Browse files
committed
do all the changes to BindgenState inside build
1 parent f7cb6b0 commit a8de128

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
}
@@ -2245,7 +2224,37 @@ struct BindgenState {
22452224
impl ::std::panic::UnwindSafe for BindgenState {}
22462225

22472226
impl BindgenState {
2248-
fn build(options: &BindgenOptions) -> Self {
2227+
fn build(
2228+
options: &BindgenOptions,
2229+
parse_callbacks: Option<Box<dyn callbacks::ParseCallbacks>>,
2230+
) -> Self {
2231+
let mut clang_args = options.clang_args.clone();
2232+
2233+
// Add any extra arguments from the environment to the clang command line.
2234+
clang_args.extend(get_extra_clang_args());
2235+
2236+
// Transform input headers to arguments on the clang command line.
2237+
let (input_header, extra_input_headers) =
2238+
if let Some((input_header, extra_input_headers)) =
2239+
options.input_headers.split_last()
2240+
{
2241+
(Some(input_header.clone()), extra_input_headers.to_vec())
2242+
} else {
2243+
Default::default()
2244+
};
2245+
2246+
clang_args.extend(
2247+
extra_input_headers
2248+
.iter()
2249+
.flat_map(|header| ["-include".into(), header.to_string()]),
2250+
);
2251+
2252+
let input_unsaved_files = options
2253+
.input_header_contents
2254+
.iter()
2255+
.map(|(name, contents)| clang::UnsavedFile::new(&name, &contents))
2256+
.collect();
2257+
22492258
Self {
22502259
allowlisted_vars: RegexSet::new(
22512260
options.allowlisted_vars.clone(),
@@ -2343,11 +2352,11 @@ impl BindgenState {
23432352
options.must_use_types.clone(),
23442353
options.record_matches,
23452354
),
2346-
clang_args: options.clang_args.clone(),
2347-
input_header: Default::default(),
2348-
extra_input_headers: Default::default(),
2349-
input_unsaved_files: Default::default(),
2350-
parse_callbacks: Default::default(),
2355+
clang_args,
2356+
input_header,
2357+
extra_input_headers,
2358+
input_unsaved_files,
2359+
parse_callbacks,
23512360
}
23522361
}
23532362
}

0 commit comments

Comments
 (0)